^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2007 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/percpu_counter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/lockdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/crc32c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "tree-log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "raid56.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "free-space-cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "free-space-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "ref-verify.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "space-info.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "block-rsv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "delalloc-space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "block-group.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "discard.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "rcu-string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #undef SCRAMBLE_DELAYED_REFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct btrfs_delayed_ref_node *node, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u64 root_objectid, u64 owner_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u64 owner_offset, int refs_to_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct btrfs_delayed_extent_op *extra_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct btrfs_extent_item *ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) u64 parent, u64 root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) u64 flags, u64 owner, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct btrfs_key *ins, int ref_mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct btrfs_delayed_ref_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct btrfs_delayed_extent_op *extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int find_next_key(struct btrfs_path *path, int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct btrfs_key *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return (cache->flags & bits) == bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) u64 start, u64 num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) u64 end = start + num_bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) set_extent_bits(&fs_info->excluded_extents, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) EXTENT_UPTODATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct btrfs_fs_info *fs_info = cache->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) start = cache->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) end = start + cache->length - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) clear_extent_bits(&fs_info->excluded_extents, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) EXTENT_UPTODATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* simple helper to search for an existing data extent at a given offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) key.objectid = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) key.offset = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * helper function to lookup reference count and flags of a tree block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * the head node for delayed ref is used to store the sum of all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * reference count modifications queued up in the rbtree. the head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * node may also store the extent flags to set. This way you can check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * to see what the reference count and extent flags would be if all of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * the delayed refs are not processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct btrfs_fs_info *fs_info, u64 bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) u64 offset, int metadata, u64 *refs, u64 *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct btrfs_delayed_ref_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct btrfs_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) u64 num_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) u64 extent_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * If we don't have skinny metadata, don't bother doing anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) offset = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) metadata = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) path->skip_locking = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) path->search_commit_root = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) search_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) key.offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) key.type = BTRFS_METADATA_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (path->slots[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) btrfs_item_key_to_cpu(path->nodes[0], &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (key.objectid == bytenr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) key.type == BTRFS_EXTENT_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) key.offset == fs_info->nodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (item_size >= sizeof(*ei)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ei = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) num_refs = btrfs_extent_refs(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) extent_flags = btrfs_extent_flags(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) btrfs_print_v0_err(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) btrfs_handle_fs_error(fs_info, ret, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) BUG_ON(num_refs == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) num_refs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) extent_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!mutex_trylock(&head->mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) refcount_inc(&head->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Mutex was contended, block until it's released and try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) mutex_lock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) mutex_unlock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) btrfs_put_delayed_ref_head(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) spin_lock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (head->extent_op && head->extent_op->update_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) extent_flags |= head->extent_op->flags_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) BUG_ON(num_refs == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) num_refs += head->ref_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) mutex_unlock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) WARN_ON(num_refs == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) *refs = num_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) *flags = extent_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * Back reference rules. Back refs have three main goals:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * 1) differentiate between all holders of references to an extent so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * when a reference is dropped we can make sure it was a valid reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * before freeing the extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * 2) Provide enough information to quickly find the holders of an extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * if we notice a given block is corrupted or bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * 3) Make it easy to migrate blocks for FS shrinking or storage pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * maintenance. This is actually the same as #2, but with a slightly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * different use case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * There are two kinds of back refs. The implicit back refs is optimized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * for pointers in non-shared tree blocks. For a given pointer in a block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * back refs of this kind provide information about the block's owner tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * and the pointer's key. These information allow us to find the block by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * b-tree searching. The full back refs is for pointers in tree blocks not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * referenced by their owner trees. The location of tree block is recorded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * in the back refs. Actually the full back refs is generic, and can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * used in all cases the implicit back refs is used. The major shortcoming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * of the full back refs is its overhead. Every time a tree block gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * COWed, we have to update back refs entry for all pointers in it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * For a newly allocated tree block, we use implicit back refs for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * pointers in it. This means most tree related operations only involve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * implicit back refs. For a tree block created in old transaction, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * only way to drop a reference to it is COW it. So we can detect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * event that tree block loses its owner tree's reference and do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * back refs conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * When a tree block is COWed through a tree, there are four cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * The reference count of the block is one and the tree is the block's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * owner tree. Nothing to do in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * The reference count of the block is one and the tree is not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * block's owner tree. In this case, full back refs is used for pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * in the block. Remove these full back refs, add implicit back refs for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * every pointers in the new block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * The reference count of the block is greater than one and the tree is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * the block's owner tree. In this case, implicit back refs is used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * pointers in the block. Add full back refs for every pointers in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * block, increase lower level extents' reference counts. The original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * implicit back refs are entailed to the new block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * The reference count of the block is greater than one and the tree is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * not the block's owner tree. Add implicit back refs for every pointer in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * the new block, increase lower level extents' reference count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Back Reference Key composing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * The key objectid corresponds to the first byte in the extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * The key type is used to differentiate between types of back refs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * There are different meanings of the key offset for different types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * of back refs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * File extents can be referenced by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * - multiple snapshots, subvolumes, or different generations in one subvol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * - different files inside a single subvolume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * - different offsets inside a file (bookend extents in file.c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * The extent ref structure for the implicit back refs has fields for:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * - Objectid of the subvolume root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * - objectid of the file holding the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * - original offset in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * - how many bookend extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * The key offset for the implicit back refs is hash of the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * three fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * The extent ref structure for the full back refs has field for:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * - number of pointers in the tree leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * The key offset for the implicit back refs is the first byte of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * the tree leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * When a file extent is allocated, The implicit back refs is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * the fields are filled in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * (root_key.objectid, inode objectid, offset in file, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * When a file extent is removed file truncation, we find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * corresponding implicit back refs and check the following fields:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * (btrfs_header_owner(leaf), inode objectid, offset in file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * Btree extents can be referenced by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * - Different subvolumes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * Both the implicit back refs and the full back refs for tree blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * only consist of key. The key offset for the implicit back refs is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * objectid of block's owner tree. The key offset for the full back refs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * is the first byte of parent block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * When implicit back refs is used, information about the lowest key and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * level of the tree block are required. These information are stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * tree block info structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct btrfs_extent_inline_ref *iref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) enum btrfs_inline_ref_type is_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int type = btrfs_extent_inline_ref_type(eb, iref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (type == BTRFS_TREE_BLOCK_REF_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) type == BTRFS_SHARED_BLOCK_REF_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) type == BTRFS_SHARED_DATA_REF_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) type == BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (is_data == BTRFS_REF_TYPE_BLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (type == BTRFS_TREE_BLOCK_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ASSERT(eb->fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * Every shared one has parent tree block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * which must be aligned to sector size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) IS_ALIGNED(offset, eb->fs_info->sectorsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) } else if (is_data == BTRFS_REF_TYPE_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (type == BTRFS_EXTENT_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (type == BTRFS_SHARED_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) ASSERT(eb->fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * Every shared one has parent tree block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * which must be aligned to sector size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) IS_ALIGNED(offset, eb->fs_info->sectorsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ASSERT(is_data == BTRFS_REF_TYPE_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) btrfs_print_leaf((struct extent_buffer *)eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) btrfs_err(eb->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) "eb %llu iref 0x%lx invalid extent inline ref type %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) eb->start, (unsigned long)iref, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return BTRFS_REF_TYPE_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) u32 high_crc = ~(u32)0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) u32 low_crc = ~(u32)0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) __le64 lenum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) lenum = cpu_to_le64(root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) lenum = cpu_to_le64(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) lenum = cpu_to_le64(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return ((u64)high_crc << 31) ^ (u64)low_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct btrfs_extent_data_ref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) btrfs_extent_data_ref_objectid(leaf, ref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) btrfs_extent_data_ref_offset(leaf, ref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static int match_extent_data_ref(struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct btrfs_extent_data_ref *ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) u64 root_objectid, u64 owner, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) btrfs_extent_data_ref_offset(leaf, ref) != offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) u64 bytenr, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) u64 root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) u64 owner, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) struct btrfs_root *root = trans->fs_info->extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct btrfs_extent_data_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) int recow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) key.type = BTRFS_SHARED_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) key.offset = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) key.type = BTRFS_EXTENT_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) key.offset = hash_extent_data_ref(root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) owner, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) recow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (path->slots[0] >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) recow = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (key.objectid != bytenr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) key.type != BTRFS_EXTENT_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ref = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct btrfs_extent_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (match_extent_data_ref(leaf, ref, root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) owner, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (recow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) u64 bytenr, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) u64 root_objectid, u64 owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) u64 offset, int refs_to_add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct btrfs_root *root = trans->fs_info->extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) u32 num_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) key.type = BTRFS_SHARED_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) key.offset = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) size = sizeof(struct btrfs_shared_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) key.type = BTRFS_EXTENT_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) key.offset = hash_extent_data_ref(root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) owner, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) size = sizeof(struct btrfs_extent_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) ret = btrfs_insert_empty_item(trans, root, path, &key, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (ret && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct btrfs_shared_data_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) ref = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct btrfs_shared_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) num_refs = btrfs_shared_data_ref_count(leaf, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) num_refs += refs_to_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) struct btrfs_extent_data_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) while (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) ref = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) struct btrfs_extent_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (match_extent_data_ref(leaf, ref, root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) owner, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) key.offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) ret = btrfs_insert_empty_item(trans, root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (ret && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) ref = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct btrfs_extent_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) btrfs_set_extent_data_ref_root(leaf, ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) btrfs_set_extent_data_ref_offset(leaf, ref, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) num_refs = btrfs_extent_data_ref_count(leaf, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) num_refs += refs_to_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) int refs_to_drop, int *last_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct btrfs_extent_data_ref *ref1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct btrfs_shared_data_ref *ref2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) u32 num_refs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ref1 = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) struct btrfs_extent_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) num_refs = btrfs_extent_data_ref_count(leaf, ref1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) ref2 = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct btrfs_shared_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) num_refs = btrfs_shared_data_ref_count(leaf, ref2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) btrfs_print_v0_err(trans->fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) btrfs_abort_transaction(trans, -EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) BUG_ON(num_refs < refs_to_drop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) num_refs -= refs_to_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (num_refs == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) *last_ref = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) static noinline u32 extent_data_ref_count(struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct btrfs_extent_inline_ref *iref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct btrfs_extent_data_ref *ref1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) struct btrfs_shared_data_ref *ref2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) u32 num_refs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (iref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * If type is invalid, we should have bailed out earlier than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * this call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ASSERT(type != BTRFS_REF_TYPE_INVALID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (type == BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) num_refs = btrfs_extent_data_ref_count(leaf, ref1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) num_refs = btrfs_shared_data_ref_count(leaf, ref2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) ref1 = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct btrfs_extent_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) num_refs = btrfs_extent_data_ref_count(leaf, ref1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) ref2 = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) struct btrfs_shared_data_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) num_refs = btrfs_shared_data_ref_count(leaf, ref2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return num_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) u64 bytenr, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) u64 root_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct btrfs_root *root = trans->fs_info->extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) key.type = BTRFS_SHARED_BLOCK_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) key.offset = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) key.type = BTRFS_TREE_BLOCK_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) key.offset = root_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) u64 bytenr, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) u64 root_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) key.type = BTRFS_SHARED_BLOCK_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) key.offset = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) key.type = BTRFS_TREE_BLOCK_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) key.offset = root_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) path, &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) static inline int extent_ref_type(u64 parent, u64 owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (owner < BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (parent > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) type = BTRFS_SHARED_BLOCK_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) type = BTRFS_TREE_BLOCK_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (parent > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) type = BTRFS_SHARED_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) type = BTRFS_EXTENT_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) static int find_next_key(struct btrfs_path *path, int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) for (; level < BTRFS_MAX_LEVEL; level++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (!path->nodes[level])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (path->slots[level] + 1 >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) btrfs_header_nritems(path->nodes[level]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) btrfs_item_key_to_cpu(path->nodes[level], key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) path->slots[level] + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) btrfs_node_key_to_cpu(path->nodes[level], key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) path->slots[level] + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * look for inline back ref. if back ref is found, *ref_ret is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * to the address of inline back ref, and 0 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * if back ref isn't found, *ref_ret is set to the address where it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * should be inserted, and -ENOENT is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * if insert is true and there are too many inline back refs, the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * points to the extent item, and -EAGAIN is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * NOTE: inline back refs are ordered in the same way that back ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * items in the tree are ordered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) static noinline_for_stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct btrfs_extent_inline_ref **ref_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) u64 bytenr, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) u64 parent, u64 root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) u64 owner, u64 offset, int insert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) struct btrfs_root *root = fs_info->extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) struct btrfs_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) struct btrfs_extent_inline_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) u64 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) int extra_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) int want;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) int needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) key.offset = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) want = extent_ref_type(parent, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (insert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) extra_size = btrfs_extent_inline_ref_size(want);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) path->keep_locks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) extra_size = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * Owner is our level, so we can just add one to get the level for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * block we are interested in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) key.type = BTRFS_METADATA_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) key.offset = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * We may be a newly converted file system which still has the old fat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * extent entries for metadata, so try and see if we have one of those.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (ret > 0 && skinny_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) skinny_metadata = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (path->slots[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) btrfs_item_key_to_cpu(path->nodes[0], &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) if (key.objectid == bytenr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) key.type == BTRFS_EXTENT_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) key.offset == num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) key.offset = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (ret && !insert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) } else if (WARN_ON(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (unlikely(item_size < sizeof(*ei))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) btrfs_print_v0_err(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) btrfs_abort_transaction(trans, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) flags = btrfs_extent_flags(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) ptr = (unsigned long)(ei + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) end = (unsigned long)ei + item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) ptr += sizeof(struct btrfs_tree_block_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) BUG_ON(ptr > end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (owner >= BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) needed = BTRFS_REF_TYPE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) needed = BTRFS_REF_TYPE_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (ptr >= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) WARN_ON(ptr > end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) iref = (struct btrfs_extent_inline_ref *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (type == BTRFS_REF_TYPE_INVALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) err = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (want < type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (want > type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) ptr += btrfs_extent_inline_ref_size(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (type == BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct btrfs_extent_data_ref *dref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) dref = (struct btrfs_extent_data_ref *)(&iref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (match_extent_data_ref(leaf, dref, root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) owner, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) if (hash_extent_data_ref_item(leaf, dref) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) hash_extent_data_ref(root_objectid, owner, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) u64 ref_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (parent > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (parent == ref_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (ref_offset < parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (root_objectid == ref_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (ref_offset < root_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) ptr += btrfs_extent_inline_ref_size(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (err == -ENOENT && insert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (item_size + extra_size >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * To add new inline back ref, we have to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * there is no corresponding back ref item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * For simplicity, we just do not add new inline back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * ref if there is any kind of item for this block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (find_next_key(path, 0, &key) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) key.objectid == bytenr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (insert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) path->keep_locks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) btrfs_unlock_up_safe(path, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) * helper to add new inline back ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) static noinline_for_stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) struct btrfs_extent_inline_ref *iref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) u64 parent, u64 root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) u64 owner, u64 offset, int refs_to_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct btrfs_delayed_extent_op *extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) struct btrfs_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) unsigned long item_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) u64 refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) item_offset = (unsigned long)iref - (unsigned long)ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) type = extent_ref_type(parent, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) size = btrfs_extent_inline_ref_size(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) btrfs_extend_item(path, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) refs = btrfs_extent_refs(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) refs += refs_to_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) btrfs_set_extent_refs(leaf, ei, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) if (extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) __run_delayed_extent_op(extent_op, leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) ptr = (unsigned long)ei + item_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) if (ptr < end - size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) memmove_extent_buffer(leaf, ptr + size, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) end - size - ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) iref = (struct btrfs_extent_inline_ref *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) btrfs_set_extent_inline_ref_type(leaf, iref, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (type == BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct btrfs_extent_data_ref *dref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) dref = (struct btrfs_extent_data_ref *)(&iref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) btrfs_set_extent_data_ref_offset(leaf, dref, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) struct btrfs_shared_data_ref *sref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) sref = (struct btrfs_shared_data_ref *)(iref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) static int lookup_extent_backref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) struct btrfs_extent_inline_ref **ref_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) u64 bytenr, u64 num_bytes, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) u64 root_objectid, u64 owner, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) num_bytes, parent, root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) owner, offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) *ref_ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (owner < BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) ret = lookup_tree_block_ref(trans, path, bytenr, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) ret = lookup_extent_data_ref(trans, path, bytenr, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) root_objectid, owner, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * helper to update/remove inline back ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) static noinline_for_stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) void update_inline_extent_backref(struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) struct btrfs_extent_inline_ref *iref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) int refs_to_mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) struct btrfs_delayed_extent_op *extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) int *last_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) struct btrfs_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) struct btrfs_extent_data_ref *dref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) struct btrfs_shared_data_ref *sref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) u64 refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) refs = btrfs_extent_refs(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) refs += refs_to_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) btrfs_set_extent_refs(leaf, ei, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) __run_delayed_extent_op(extent_op, leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) * If type is invalid, we should have bailed out after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) * lookup_inline_extent_backref().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) ASSERT(type != BTRFS_REF_TYPE_INVALID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (type == BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) dref = (struct btrfs_extent_data_ref *)(&iref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) refs = btrfs_extent_data_ref_count(leaf, dref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) sref = (struct btrfs_shared_data_ref *)(iref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) refs = btrfs_shared_data_ref_count(leaf, sref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) refs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) BUG_ON(refs_to_mod != -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) refs += refs_to_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (refs > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (type == BTRFS_EXTENT_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) btrfs_set_extent_data_ref_count(leaf, dref, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) btrfs_set_shared_data_ref_count(leaf, sref, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) *last_ref = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) size = btrfs_extent_inline_ref_size(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) ptr = (unsigned long)iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) end = (unsigned long)ei + item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) if (ptr + size < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) memmove_extent_buffer(leaf, ptr, ptr + size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) end - ptr - size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) item_size -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) btrfs_truncate_item(path, item_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) static noinline_for_stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) u64 bytenr, u64 num_bytes, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) u64 root_objectid, u64 owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) u64 offset, int refs_to_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) struct btrfs_delayed_extent_op *extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) struct btrfs_extent_inline_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) num_bytes, parent, root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) owner, offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * We're adding refs to a tree block we already own, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * should not happen at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (owner < BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) btrfs_crit(trans->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) "adding refs to an existing tree ref, bytenr %llu num_bytes %llu root_objectid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) bytenr, num_bytes, root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) btrfs_crit(trans->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) "path->slots[0]=%d path->nodes[0]:", path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) btrfs_print_leaf(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) update_inline_extent_backref(path, iref, refs_to_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) extent_op, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) } else if (ret == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) setup_inline_extent_backref(trans->fs_info, path, iref, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) root_objectid, owner, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) refs_to_add, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) static int remove_extent_backref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) struct btrfs_extent_inline_ref *iref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) int refs_to_drop, int is_data, int *last_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) BUG_ON(!is_data && refs_to_drop != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) if (iref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) last_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) } else if (is_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) ret = remove_extent_data_ref(trans, path, refs_to_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) last_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) *last_ref = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) u64 *discarded_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) int j, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) u64 bytes_left, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) u64 aligned_start = ALIGN(start, 1 << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (WARN_ON(start != aligned_start)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) len -= aligned_start - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) len = round_down(len, 1 << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) start = aligned_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) *discarded_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) end = start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) bytes_left = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) /* Skip any superblocks on this device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) u64 sb_start = btrfs_sb_offset(j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) u64 size = sb_start - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (!in_range(sb_start, start, bytes_left) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) !in_range(sb_end, start, bytes_left) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) * Superblock spans beginning of range. Adjust start and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) * try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) if (sb_start <= start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) start += sb_end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) if (start > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) bytes_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) bytes_left = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) *discarded_bytes += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) else if (ret != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) start = sb_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) if (start > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) bytes_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) bytes_left = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) if (bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) *discarded_bytes += bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) u64 num_bytes, u64 *actual_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) u64 discarded_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) u64 end = bytenr + num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) u64 cur = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) struct btrfs_bio *bbio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) * Avoid races with device replace and make sure our bbio has devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) * associated to its stripes that don't go away while we are discarding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) btrfs_bio_counter_inc_blocked(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) while (cur < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) struct btrfs_bio_stripe *stripe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) num_bytes = end - cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) /* Tell the block device(s) that the sectors can be discarded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) &num_bytes, &bbio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) * thus we can't continue anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) stripe = bbio->stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) for (i = 0; i < bbio->num_stripes; i++, stripe++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) u64 bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) struct request_queue *req_q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) struct btrfs_device *device = stripe->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) if (!device->bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) ASSERT(btrfs_test_opt(fs_info, DEGRADED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) req_q = bdev_get_queue(device->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) if (!blk_queue_discard(req_q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) ret = btrfs_issue_discard(device->bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) stripe->physical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) stripe->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) &bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) discarded_bytes += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) } else if (ret != -EOPNOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) * Logic errors or -ENOMEM, or -EIO, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) * unlikely to happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) * And since there are two loops, explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) * go to out to avoid confusion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) btrfs_put_bbio(bbio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) * Just in case we get back EOPNOTSUPP for some reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) * just ignore the return value so we don't screw up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) * people calling discard_extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) btrfs_put_bbio(bbio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) cur += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) btrfs_bio_counter_dec(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) if (actual_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) *actual_bytes = discarded_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) if (ret == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) /* Can return -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) struct btrfs_ref *generic_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) generic_ref->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) if (generic_ref->type == BTRFS_REF_METADATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) ret = btrfs_add_delayed_tree_ref(trans, generic_ref, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) btrfs_ref_tree_mod(fs_info, generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) * __btrfs_inc_extent_ref - insert backreference for a given extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) * The counterpart is in __btrfs_free_extent(), with examples and more details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) * how it works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) * @trans: Handle of transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) * @node: The delayed ref node used to get the bytenr/length for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) * extent whose references are incremented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) * bytenr of the parent block. Since new extents are always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) * created with indirect references, this will only be the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) * when relocating a shared extent. In that case, root_objectid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) * be 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) * @root_objectid: The id of the root where this modification has originated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) * this can be either one of the well-known metadata trees or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) * the subvolume id which references this extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) * @owner: For data extents it is the inode number of the owning file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) * For metadata extents this parameter holds the level in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) * tree of the extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) * @offset: For metadata extents the offset is ignored and is currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) * always passed as 0. For data extents it is the fileoffset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) * this extent belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) * @refs_to_add Number of references to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) * @extent_op Pointer to a structure, holding information necessary when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) * updating a tree block's flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) struct btrfs_delayed_ref_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) u64 parent, u64 root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) u64 owner, u64 offset, int refs_to_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) struct btrfs_delayed_extent_op *extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) struct btrfs_extent_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) u64 bytenr = node->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) u64 num_bytes = node->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) u64 refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) /* this will setup the path even if it fails to insert the back ref */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) parent, root_objectid, owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) offset, refs_to_add, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) if ((ret < 0 && ret != -EAGAIN) || !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) * Ok we had -EAGAIN which means we didn't have space to insert and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) * inline extent ref, so just update the reference count and add a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) * normal backref.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) refs = btrfs_extent_refs(leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) if (extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) __run_delayed_extent_op(extent_op, leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) /* now insert the actual backref */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) if (owner < BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) BUG_ON(refs_to_add != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) ret = insert_tree_block_ref(trans, path, bytenr, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) ret = insert_extent_data_ref(trans, path, bytenr, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) root_objectid, owner, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) refs_to_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) struct btrfs_delayed_ref_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) struct btrfs_delayed_extent_op *extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) int insert_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) struct btrfs_delayed_data_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) u64 parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) u64 ref_root = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) u64 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) ins.objectid = node->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) ins.offset = node->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) ins.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) ref = btrfs_delayed_node_to_data_ref(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) if (node->type == BTRFS_SHARED_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) parent = ref->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) ref_root = ref->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) if (extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) flags |= extent_op->flags_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) ret = alloc_reserved_file_extent(trans, parent, ref_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) flags, ref->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) ref->offset, &ins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) node->ref_mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) } else if (node->action == BTRFS_ADD_DELAYED_REF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) ref->objectid, ref->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) node->ref_mod, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) } else if (node->action == BTRFS_DROP_DELAYED_REF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) ret = __btrfs_free_extent(trans, node, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) ref_root, ref->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) ref->offset, node->ref_mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) struct btrfs_extent_item *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) u64 flags = btrfs_extent_flags(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) if (extent_op->update_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) flags |= extent_op->flags_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) btrfs_set_extent_flags(leaf, ei, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) if (extent_op->update_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) struct btrfs_tree_block_info *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) bi = (struct btrfs_tree_block_info *)(ei + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) struct btrfs_delayed_ref_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) struct btrfs_delayed_extent_op *extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) struct btrfs_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) int metadata = !extent_op->is_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) if (TRANS_ABORTED(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) metadata = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) key.objectid = head->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) if (metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) key.type = BTRFS_METADATA_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) key.offset = extent_op->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) key.offset = head->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) if (metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (path->slots[0] > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) btrfs_item_key_to_cpu(path->nodes[0], &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) if (key.objectid == head->bytenr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) key.type == BTRFS_EXTENT_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) key.offset == head->num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) metadata = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) key.objectid = head->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) key.offset = head->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) if (unlikely(item_size < sizeof(*ei))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) btrfs_print_v0_err(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) btrfs_abort_transaction(trans, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) __run_delayed_extent_op(extent_op, leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) struct btrfs_delayed_ref_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) struct btrfs_delayed_extent_op *extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) int insert_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) struct btrfs_delayed_tree_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) u64 parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) u64 ref_root = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) ref = btrfs_delayed_node_to_tree_ref(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) parent = ref->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) ref_root = ref->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) if (node->ref_mod != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) btrfs_err(trans->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) node->bytenr, node->ref_mod, node->action, ref_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) BUG_ON(!extent_op || !extent_op->update_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) ret = alloc_reserved_tree_block(trans, node, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) } else if (node->action == BTRFS_ADD_DELAYED_REF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) ref->level, 0, 1, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) } else if (node->action == BTRFS_DROP_DELAYED_REF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) ret = __btrfs_free_extent(trans, node, parent, ref_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) ref->level, 0, 1, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) /* helper function to actually process a single delayed ref entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) struct btrfs_delayed_ref_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) struct btrfs_delayed_extent_op *extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) int insert_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) if (TRANS_ABORTED(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (insert_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) node->type == BTRFS_SHARED_BLOCK_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) ret = run_delayed_tree_ref(trans, node, extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) insert_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) node->type == BTRFS_SHARED_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) ret = run_delayed_data_ref(trans, node, extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) insert_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) if (ret && insert_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) static inline struct btrfs_delayed_ref_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) select_delayed_ref(struct btrfs_delayed_ref_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) struct btrfs_delayed_ref_node *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) * This is to prevent a ref count from going down to zero, which deletes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) * the extent item from the extent tree, when there still are references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) * to add, which would fail because they would not find the extent item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) if (!list_empty(&head->ref_add_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) return list_first_entry(&head->ref_add_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) struct btrfs_delayed_ref_node, add_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) ref = rb_entry(rb_first_cached(&head->ref_tree),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) struct btrfs_delayed_ref_node, ref_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) ASSERT(list_empty(&ref->add_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) return ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) struct btrfs_delayed_ref_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) head->processing = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) delayed_refs->num_heads_ready++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) btrfs_delayed_ref_unlock(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) static struct btrfs_delayed_extent_op *cleanup_extent_op(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) struct btrfs_delayed_ref_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) struct btrfs_delayed_extent_op *extent_op = head->extent_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) if (!extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) if (head->must_insert_reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) head->extent_op = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) btrfs_free_delayed_extent_op(extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) return extent_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) struct btrfs_delayed_ref_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) struct btrfs_delayed_extent_op *extent_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) extent_op = cleanup_extent_op(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) if (!extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) head->extent_op = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) ret = run_delayed_extent_op(trans, head, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) btrfs_free_delayed_extent_op(extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) return ret ? ret : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) struct btrfs_delayed_ref_root *delayed_refs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) struct btrfs_delayed_ref_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) int nr_items = 1; /* Dropping this ref head update. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) * We had csum deletions accounted for in our delayed refs rsv, we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) * to drop the csum leaves for this update from our delayed_refs_rsv.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) if (head->total_ref_mod < 0 && head->is_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) delayed_refs->pending_csums -= head->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) nr_items += btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) * We were dropping refs, or had a new ref and dropped it, and thus must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) * adjust down our total_bytes_pinned, the space may or may not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) * been pinned and so is accounted for properly in the pinned space by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) * now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) if (head->total_ref_mod < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) (head->total_ref_mod == 0 && head->must_insert_reserved)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) u64 flags = btrfs_ref_head_to_space_flags(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) btrfs_mod_total_bytes_pinned(fs_info, flags, -head->num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) btrfs_delayed_refs_rsv_release(fs_info, nr_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) static int cleanup_ref_head(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) struct btrfs_delayed_ref_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) ret = run_and_cleanup_extent_op(trans, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) unselect_delayed_ref_head(delayed_refs, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) } else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) * Need to drop our head ref lock and re-acquire the delayed ref lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) * and then re-check to make sure nobody got added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) spin_lock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) btrfs_delete_ref_head(delayed_refs, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) if (head->must_insert_reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) if (head->is_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) ret = btrfs_del_csums(trans, fs_info->csum_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) head->bytenr, head->num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) trace_run_delayed_ref_head(fs_info, head, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) btrfs_delayed_ref_unlock(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) btrfs_put_delayed_ref_head(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) struct btrfs_trans_handle *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) struct btrfs_delayed_ref_root *delayed_refs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) struct btrfs_delayed_ref_head *head = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) head = btrfs_select_ref_head(delayed_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) if (!head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) return head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) * Grab the lock that says we are going to process all the refs for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) * this head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) ret = btrfs_delayed_ref_lock(delayed_refs, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) * We may have dropped the spin lock to get the head mutex lock, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) * that might have given someone else time to free the head. If that's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) * true, it has been removed from our list and we can move on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) head = ERR_PTR(-EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) return head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) struct btrfs_delayed_ref_head *locked_ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) unsigned long *run_refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) struct btrfs_delayed_extent_op *extent_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) struct btrfs_delayed_ref_node *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) int must_insert_reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) lockdep_assert_held(&locked_ref->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) lockdep_assert_held(&locked_ref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) while ((ref = select_delayed_ref(locked_ref))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (ref->seq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) btrfs_check_delayed_seq(fs_info, ref->seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) spin_unlock(&locked_ref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) unselect_delayed_ref_head(delayed_refs, locked_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) (*run_refs)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) ref->in_tree = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) RB_CLEAR_NODE(&ref->ref_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) if (!list_empty(&ref->add_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) list_del(&ref->add_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) * When we play the delayed ref, also correct the ref_mod on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) * head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) switch (ref->action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) case BTRFS_ADD_DELAYED_REF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) case BTRFS_ADD_DELAYED_EXTENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) locked_ref->ref_mod -= ref->ref_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) case BTRFS_DROP_DELAYED_REF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) locked_ref->ref_mod += ref->ref_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) atomic_dec(&delayed_refs->num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) * Record the must_insert_reserved flag before we drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) * spin lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) must_insert_reserved = locked_ref->must_insert_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) locked_ref->must_insert_reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) extent_op = locked_ref->extent_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) locked_ref->extent_op = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) spin_unlock(&locked_ref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) ret = run_one_delayed_ref(trans, ref, extent_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) must_insert_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) btrfs_free_delayed_extent_op(extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) unselect_delayed_ref_head(delayed_refs, locked_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) btrfs_put_delayed_ref(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) btrfs_put_delayed_ref(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) spin_lock(&locked_ref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) * Returns 0 on success or if called with an already aborted transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) * Returns -ENOMEM or -EIO on failure and will abort the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) unsigned long nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) struct btrfs_delayed_ref_head *locked_ref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) ktime_t start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) unsigned long count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) unsigned long actual_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) if (!locked_ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) locked_ref = btrfs_obtain_ref_head(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) if (IS_ERR_OR_NULL(locked_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) if (PTR_ERR(locked_ref) == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) * We need to try and merge add/drops of the same ref since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) * can run into issues with relocate dropping the implicit ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) * and then it being added back again before the drop can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) * finish. If we merged anything we need to re-loop so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) * get a good ref.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) * Or we can get node references of the same type that weren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) * merged when created due to bumps in the tree mod seq, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) * we need to merge them to prevent adding an inline extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) * backref before dropping it (triggering a BUG_ON at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) * insert_inline_extent_backref()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) spin_lock(&locked_ref->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) &actual_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) if (ret < 0 && ret != -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) * Error, btrfs_run_delayed_refs_for_head already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) * unlocked everything so just bail out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) } else if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) * Success, perform the usual cleanup of a processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) * head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) ret = cleanup_ref_head(trans, locked_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) if (ret > 0 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) /* We dropped our lock, we need to loop. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) } else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) * Either success case or btrfs_run_delayed_refs_for_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) * returned -EAGAIN, meaning we need to select another head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) locked_ref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) } while ((nr != -1 && count < nr) || locked_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) * We don't want to include ref heads since we can have empty ref heads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) * and those will drastically skew our runtime down since we just do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) * accounting, no actual extent tree updates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) if (actual_count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) u64 avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) * We weigh the current average higher than our current runtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) * to avoid large swings in the average.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) #ifdef SCRAMBLE_DELAYED_REFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) * Normally delayed refs get processed in ascending bytenr order. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) * correlates in most cases to the order added. To expose dependencies on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) * order, we start to process the tree in the middle instead of the beginning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) static u64 find_middle(struct rb_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) struct rb_node *n = root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) struct btrfs_delayed_ref_node *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) int alt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) u64 middle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) u64 first = 0, last = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) n = rb_first(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) first = entry->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) n = rb_last(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) last = entry->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) n = root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) WARN_ON(!entry->in_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) middle = entry->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) if (alt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) alt = 1 - alt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) return middle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) * Takes the number of bytes to be csumm'ed and figures out how many leaves it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) * would require to store the csums for that many bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) u64 csum_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) u64 num_csums_per_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) u64 num_csums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) num_csums_per_leaf = div64_u64(csum_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) (u64)btrfs_super_csum_size(fs_info->super_copy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) num_csums += num_csums_per_leaf - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) num_csums = div64_u64(num_csums, num_csums_per_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) return num_csums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) * this starts processing the delayed reference count updates and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) * extent insertions we have queued up so far. count can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) * 0, which means to process everything in the tree at the start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) * of the run (but not newly added entries), or it can be some target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) * number you'd like to process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) * Returns 0 on success or if called with an aborted transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) * Returns <0 on error and aborts the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) unsigned long count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) struct btrfs_delayed_ref_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) int run_all = count == (unsigned long)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) /* We'll clean this up in btrfs_cleanup_transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) if (TRANS_ABORTED(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) count = atomic_read(&delayed_refs->num_entries) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) #ifdef SCRAMBLE_DELAYED_REFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) ret = __btrfs_run_delayed_refs(trans, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) if (run_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) btrfs_create_pending_block_groups(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) node = rb_first_cached(&delayed_refs->href_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) head = rb_entry(node, struct btrfs_delayed_ref_head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) href_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) refcount_inc(&head->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) /* Mutex was contended, block until it's released and retry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) mutex_lock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) mutex_unlock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) btrfs_put_delayed_ref_head(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) struct extent_buffer *eb, u64 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) int level, int is_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) struct btrfs_delayed_extent_op *extent_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) extent_op = btrfs_alloc_delayed_extent_op();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) if (!extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) extent_op->flags_to_set = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) extent_op->update_flags = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) extent_op->update_key = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) extent_op->is_data = is_data ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) extent_op->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) btrfs_free_delayed_extent_op(extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) static noinline int check_delayed_ref(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) u64 objectid, u64 offset, u64 bytenr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) struct btrfs_delayed_ref_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) struct btrfs_delayed_ref_node *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) struct btrfs_delayed_data_ref *data_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) struct btrfs_transaction *cur_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) spin_lock(&root->fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) cur_trans = root->fs_info->running_transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) if (cur_trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) refcount_inc(&cur_trans->use_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) spin_unlock(&root->fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) if (!cur_trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) delayed_refs = &cur_trans->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) if (!head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) btrfs_put_transaction(cur_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) if (!mutex_trylock(&head->mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) refcount_inc(&head->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) * Mutex was contended, block until it's released and let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) * caller try again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) mutex_lock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) mutex_unlock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) btrfs_put_delayed_ref_head(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) btrfs_put_transaction(cur_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) spin_lock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) * XXX: We should replace this with a proper search function in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) * future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) for (node = rb_first_cached(&head->ref_tree); node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) node = rb_next(node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) /* If it's a shared ref we know a cross reference exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) data_ref = btrfs_delayed_node_to_data_ref(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) * If our ref doesn't match the one we're currently looking at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) * then we have a cross reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) if (data_ref->root != root->root_key.objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) data_ref->objectid != objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) data_ref->offset != offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) mutex_unlock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) btrfs_put_transaction(cur_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) static noinline int check_committed_ref(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) u64 objectid, u64 offset, u64 bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) bool strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) struct btrfs_root *extent_root = fs_info->extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) struct btrfs_extent_data_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) struct btrfs_extent_inline_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) struct btrfs_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) BUG_ON(ret == 0); /* Corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) /* If extent item has more than 1 inline ref then it's shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) if (item_size != sizeof(*ei) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) * If extent created before last snapshot => it's shared unless the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) * snapshot has been deleted. Use the heuristic if strict is false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) if (!strict &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) (btrfs_extent_generation(leaf, ei) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) btrfs_root_last_snapshot(&root->root_item)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) iref = (struct btrfs_extent_inline_ref *)(ei + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) /* If this extent has SHARED_DATA_REF then it's shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) if (type != BTRFS_EXTENT_DATA_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) ref = (struct btrfs_extent_data_ref *)(&iref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) if (btrfs_extent_refs(leaf, ei) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) btrfs_extent_data_ref_count(leaf, ref) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) btrfs_extent_data_ref_root(leaf, ref) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) root->root_key.objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) btrfs_extent_data_ref_offset(leaf, ref) != offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) u64 bytenr, bool strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) ret = check_committed_ref(root, path, objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) offset, bytenr, strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) if (ret && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) ret = check_delayed_ref(root, path, objectid, offset, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) } while (ret == -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) WARN_ON(ret > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) struct extent_buffer *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) int full_backref, int inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) u64 bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) u64 parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) u64 ref_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) struct btrfs_ref generic_ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) int action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) if (btrfs_is_testing(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) ref_root = btrfs_header_owner(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) nritems = btrfs_header_nritems(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) level = btrfs_header_level(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) if (full_backref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) parent = buf->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) if (inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) action = BTRFS_ADD_DELAYED_REF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) action = BTRFS_DROP_DELAYED_REF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) for (i = 0; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) if (level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) btrfs_item_key_to_cpu(buf, &key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) if (key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) fi = btrfs_item_ptr(buf, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) if (btrfs_file_extent_type(buf, fi) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) BTRFS_FILE_EXTENT_INLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) if (bytenr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) key.offset -= btrfs_file_extent_offset(buf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) btrfs_init_generic_ref(&generic_ref, action, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) num_bytes, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) generic_ref.real_root = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) generic_ref.skip_qgroup = for_reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) if (inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) ret = btrfs_inc_extent_ref(trans, &generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) ret = btrfs_free_extent(trans, &generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) bytenr = btrfs_node_blockptr(buf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) num_bytes = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) btrfs_init_generic_ref(&generic_ref, action, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) num_bytes, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) generic_ref.real_root = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) generic_ref.skip_qgroup = for_reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) if (inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) ret = btrfs_inc_extent_ref(trans, &generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) ret = btrfs_free_extent(trans, &generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) struct extent_buffer *buf, int full_backref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) struct extent_buffer *buf, int full_backref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) struct btrfs_block_group *block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) int readonly = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) block_group = btrfs_lookup_block_group(fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) if (!block_group || block_group->ro)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) readonly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) if (block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) btrfs_put_block_group(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) return readonly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) u64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) if (data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) flags = BTRFS_BLOCK_GROUP_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) else if (root == fs_info->chunk_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) flags = BTRFS_BLOCK_GROUP_SYSTEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) flags = BTRFS_BLOCK_GROUP_METADATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) ret = btrfs_get_alloc_profile(fs_info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) u64 bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) spin_lock(&fs_info->block_group_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) bytenr = fs_info->first_logical_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) spin_unlock(&fs_info->block_group_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) if (bytenr < (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) return bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) cache = btrfs_lookup_first_block_group(fs_info, search_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) if (!cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) bytenr = cache->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) return bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) static int pin_down_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) struct btrfs_block_group *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) u64 bytenr, u64 num_bytes, int reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) struct btrfs_fs_info *fs_info = cache->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) spin_lock(&cache->space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) spin_lock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) cache->pinned += num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) if (reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) cache->reserved -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) cache->space_info->bytes_reserved -= num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) spin_unlock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) spin_unlock(&cache->space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) __btrfs_mod_total_bytes_pinned(cache->space_info, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) set_extent_dirty(&trans->transaction->pinned_extents, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) int btrfs_pin_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) u64 bytenr, u64 num_bytes, int reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) BUG_ON(!cache); /* Logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) pin_down_extent(trans, cache, bytenr, num_bytes, reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) * this function must be called within transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) u64 bytenr, u64 num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) btrfs_add_excluded_extent(trans->fs_info, bytenr, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) if (!cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) * pull in the free space cache (if any) so that our pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) * removes the free space from the cache. We have load_only set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) * to one because the slow code to read in the free extents does check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) * the pinned extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) btrfs_cache_block_group(cache, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) pin_down_extent(trans, cache, bytenr, num_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) /* remove us from the free space cache (if we're there at all) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) u64 start, u64 num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) struct btrfs_block_group *block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) struct btrfs_caching_control *caching_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) block_group = btrfs_lookup_block_group(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) if (!block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) btrfs_cache_block_group(block_group, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) caching_ctl = btrfs_get_caching_control(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) if (!caching_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) /* Logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) BUG_ON(!btrfs_block_group_done(block_group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) ret = btrfs_remove_free_space(block_group, start, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) mutex_lock(&caching_ctl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) if (start >= caching_ctl->progress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) ret = btrfs_add_excluded_extent(fs_info, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) } else if (start + num_bytes <= caching_ctl->progress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) ret = btrfs_remove_free_space(block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) start, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) num_bytes = caching_ctl->progress - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) ret = btrfs_remove_free_space(block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) start, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) num_bytes = (start + num_bytes) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) caching_ctl->progress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) start = caching_ctl->progress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) ret = btrfs_add_excluded_extent(fs_info, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) out_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) mutex_unlock(&caching_ctl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) btrfs_put_caching_control(caching_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) btrfs_put_block_group(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) int btrfs_exclude_logged_extents(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) struct btrfs_fs_info *fs_info = eb->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) struct btrfs_file_extent_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) int found_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) for (i = 0; i < btrfs_header_nritems(eb); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) btrfs_item_key_to_cpu(eb, &key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) if (key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) found_type = btrfs_file_extent_type(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) if (found_type == BTRFS_FILE_EXTENT_INLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) atomic_inc(&bg->reservations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) * Returns the free cluster for the given space info and sets empty_cluster to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) * what it should be based on the mount options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) static struct btrfs_free_cluster *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) fetch_cluster_info(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) struct btrfs_space_info *space_info, u64 *empty_cluster)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) struct btrfs_free_cluster *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) *empty_cluster = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) if (btrfs_mixed_space_info(space_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) ret = &fs_info->meta_alloc_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) if (btrfs_test_opt(fs_info, SSD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) *empty_cluster = SZ_2M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) *empty_cluster = SZ_64K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) btrfs_test_opt(fs_info, SSD_SPREAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) *empty_cluster = SZ_2M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) ret = &fs_info->data_alloc_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) static int unpin_extent_range(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) const bool return_free_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) struct btrfs_block_group *cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) struct btrfs_space_info *space_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) struct btrfs_free_cluster *cluster = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) u64 total_unpinned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) u64 empty_cluster = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) bool readonly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) while (start <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) readonly = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) if (!cache ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) start >= cache->start + cache->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) total_unpinned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) cache = btrfs_lookup_block_group(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) BUG_ON(!cache); /* Logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) cluster = fetch_cluster_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) cache->space_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) &empty_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) empty_cluster <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) len = cache->start + cache->length - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) len = min(len, end + 1 - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) if (start < cache->last_byte_to_unpin && return_free_space) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) u64 add_len = min(len, cache->last_byte_to_unpin - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) btrfs_add_free_space(cache, start, add_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) start += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) total_unpinned += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) space_info = cache->space_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) * If this space cluster has been marked as fragmented and we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) * unpinned enough in this block group to potentially allow a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) * cluster to be created inside of it go ahead and clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) * fragmented check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) if (cluster && cluster->fragmented &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) total_unpinned > empty_cluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) spin_lock(&cluster->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) cluster->fragmented = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) spin_unlock(&cluster->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) spin_lock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) spin_lock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) cache->pinned -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) space_info->max_extent_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) __btrfs_mod_total_bytes_pinned(space_info, -len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) if (cache->ro) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) space_info->bytes_readonly += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) readonly = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) spin_unlock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) if (!readonly && return_free_space &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) global_rsv->space_info == space_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) u64 to_add = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) spin_lock(&global_rsv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) if (!global_rsv->full) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) to_add = min(len, global_rsv->size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) global_rsv->reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) global_rsv->reserved += to_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) btrfs_space_info_update_bytes_may_use(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) space_info, to_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) if (global_rsv->reserved >= global_rsv->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) global_rsv->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) len -= to_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) spin_unlock(&global_rsv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) /* Add to any tickets we may have */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) if (!readonly && return_free_space && len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) btrfs_try_granting_tickets(fs_info, space_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) spin_unlock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) struct btrfs_block_group *block_group, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) struct list_head *deleted_bgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) struct extent_io_tree *unpin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) unpin = &trans->transaction->pinned_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) while (!TRANS_ABORTED(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) mutex_lock(&fs_info->unused_bg_unpin_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) ret = find_first_extent_bit(unpin, 0, &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) EXTENT_DIRTY, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) mutex_unlock(&fs_info->unused_bg_unpin_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) clear_extent_bits(&fs_info->excluded_extents, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) end, EXTENT_UPTODATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) if (btrfs_test_opt(fs_info, DISCARD_SYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) ret = btrfs_discard_extent(fs_info, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) end + 1 - start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) clear_extent_dirty(unpin, start, end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) unpin_extent_range(fs_info, start, end, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) mutex_unlock(&fs_info->unused_bg_unpin_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) free_extent_state(cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) btrfs_discard_calc_delay(&fs_info->discard_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) * Transaction is finished. We don't need the lock anymore. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) * do need to clean up the block groups in case of a transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) * abort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) deleted_bgs = &trans->transaction->deleted_bgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) u64 trimmed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) if (!TRANS_ABORTED(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) ret = btrfs_discard_extent(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) block_group->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) block_group->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) &trimmed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) list_del_init(&block_group->bg_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) btrfs_unfreeze_block_group(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) btrfs_put_block_group(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) const char *errstr = btrfs_decode_error(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) "discard failed while removing blockgroup: errno=%d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) ret, errstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) * Drop one or more refs of @node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) * 1. Locate the extent refs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) * It's either inline in EXTENT/METADATA_ITEM or in keyed SHARED_* item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) * Locate it, then reduce the refs number or remove the ref line completely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) * 2. Update the refs count in EXTENT/METADATA_ITEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) * Inline backref case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) * in extent tree we have:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) * refs 2 gen 6 flags DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) * extent data backref root FS_TREE objectid 258 offset 0 count 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) * extent data backref root FS_TREE objectid 257 offset 0 count 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) * This function gets called with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) * node->bytenr = 13631488
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) * node->num_bytes = 1048576
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) * root_objectid = FS_TREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) * owner_objectid = 257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) * owner_offset = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) * refs_to_drop = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) * Then we should get some like:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) * refs 1 gen 6 flags DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) * extent data backref root FS_TREE objectid 258 offset 0 count 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) * Keyed backref case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) * in extent tree we have:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) * refs 754 gen 6 flags DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) * [...]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) * item 2 key (13631488 EXTENT_DATA_REF <HASH>) itemoff 3915 itemsize 28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) * extent data backref root FS_TREE objectid 866 offset 0 count 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) * This function get called with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) * node->bytenr = 13631488
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) * node->num_bytes = 1048576
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) * root_objectid = FS_TREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) * owner_objectid = 866
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) * owner_offset = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) * refs_to_drop = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) * Then we should get some like:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) * refs 753 gen 6 flags DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) struct btrfs_delayed_ref_node *node, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) u64 root_objectid, u64 owner_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) u64 owner_offset, int refs_to_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) struct btrfs_delayed_extent_op *extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) struct btrfs_fs_info *info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) struct btrfs_root *extent_root = info->extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) struct btrfs_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) struct btrfs_extent_inline_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) int is_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) int extent_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) int found_extent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) int num_to_del = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) u64 refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) u64 bytenr = node->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) u64 num_bytes = node->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) int last_ref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) if (!is_data && refs_to_drop != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) "invalid refs_to_drop, dropping more than 1 refs for tree block %llu refs_to_drop %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) node->bytenr, refs_to_drop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) if (is_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) skinny_metadata = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) parent, root_objectid, owner_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) owner_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) * Either the inline backref or the SHARED_DATA_REF/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) * SHARED_BLOCK_REF is found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) * Here is a quick path to locate EXTENT/METADATA_ITEM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) * It's possible the EXTENT/METADATA_ITEM is near current slot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) extent_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) while (extent_slot >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) btrfs_item_key_to_cpu(path->nodes[0], &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) extent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) if (key.objectid != bytenr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) if (key.type == BTRFS_EXTENT_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) key.offset == num_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) found_extent = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) if (key.type == BTRFS_METADATA_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) key.offset == owner_objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) found_extent = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) /* Quick path didn't find the EXTEMT/METADATA_ITEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) if (path->slots[0] - extent_slot > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) extent_slot--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) if (!found_extent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) if (iref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) "invalid iref, no EXTENT/METADATA_ITEM found but has inline extent ref");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) btrfs_abort_transaction(trans, -EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) goto err_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) /* Must be SHARED_* item, remove the backref first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) ret = remove_extent_backref(trans, path, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) refs_to_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) is_data, &last_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) /* Slow path to locate EXTENT/METADATA_ITEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) key.offset = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) if (!is_data && skinny_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) key.type = BTRFS_METADATA_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) key.offset = owner_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) ret = btrfs_search_slot(trans, extent_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) if (ret > 0 && skinny_metadata && path->slots[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) * Couldn't find our skinny metadata item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) * see if we have ye olde extent item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) btrfs_item_key_to_cpu(path->nodes[0], &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) if (key.objectid == bytenr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) key.type == BTRFS_EXTENT_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) key.offset == num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) if (ret > 0 && skinny_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) skinny_metadata = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) key.objectid = bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) key.offset = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) ret = btrfs_search_slot(trans, extent_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) btrfs_err(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) "umm, got %d back from search, was looking for %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) ret, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) btrfs_print_leaf(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) extent_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) } else if (WARN_ON(ret == -ENOENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) btrfs_print_leaf(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) btrfs_err(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) bytenr, parent, root_objectid, owner_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) owner_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) item_size = btrfs_item_size_nr(leaf, extent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) if (unlikely(item_size < sizeof(*ei))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) btrfs_print_v0_err(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) ei = btrfs_item_ptr(leaf, extent_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) key.type == BTRFS_EXTENT_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) struct btrfs_tree_block_info *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) if (item_size < sizeof(*ei) + sizeof(*bi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) "invalid extent item size for key (%llu, %u, %llu) owner %llu, has %u expect >= %zu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) key.objectid, key.type, key.offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) owner_objectid, item_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) sizeof(*ei) + sizeof(*bi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) btrfs_abort_transaction(trans, -EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) goto err_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) bi = (struct btrfs_tree_block_info *)(ei + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) refs = btrfs_extent_refs(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) if (refs < refs_to_drop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) "trying to drop %d refs but we only have %llu for bytenr %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) refs_to_drop, refs, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) btrfs_abort_transaction(trans, -EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) goto err_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) refs -= refs_to_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) if (refs > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) if (extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) __run_delayed_extent_op(extent_op, leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) * In the case of inline back ref, reference count will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) * be updated by remove_extent_backref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) if (iref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) if (!found_extent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) "invalid iref, got inlined extent ref but no EXTENT/METADATA_ITEM found");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) btrfs_abort_transaction(trans, -EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) goto err_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) btrfs_set_extent_refs(leaf, ei, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) if (found_extent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) ret = remove_extent_backref(trans, path, iref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) refs_to_drop, is_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) &last_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) /* In this branch refs == 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) if (found_extent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) if (is_data && refs_to_drop !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) extent_data_ref_count(path, iref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) "invalid refs_to_drop, current refs %u refs_to_drop %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) extent_data_ref_count(path, iref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) refs_to_drop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) btrfs_abort_transaction(trans, -EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) goto err_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) if (iref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) if (path->slots[0] != extent_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) "invalid iref, extent item key (%llu %u %llu) doesn't have wanted iref",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) key.objectid, key.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) btrfs_abort_transaction(trans, -EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) goto err_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) * No inline ref, we must be at SHARED_* item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) * And it's single ref, it must be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) * | extent_slot ||extent_slot + 1|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) * [ EXTENT/METADATA_ITEM ][ SHARED_* ITEM ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) if (path->slots[0] != extent_slot + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) btrfs_crit(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) "invalid SHARED_* item, previous item is not EXTENT/METADATA_ITEM");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) btrfs_abort_transaction(trans, -EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) goto err_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) path->slots[0] = extent_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) num_to_del = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) last_ref = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) num_to_del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) if (is_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) ret = btrfs_del_csums(trans, info->csum_root, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) ret = add_to_free_space_tree(trans, bytenr, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) err_dump:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) * Leaf dump can take up a lot of log buffer, so we only do full leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) * dump for debug build.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) btrfs_crit(info, "path->slots[0]=%d extent_slot=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) path->slots[0], extent_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) btrfs_print_leaf(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) * when we free an block, it is possible (and likely) that we free the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) * delayed ref for that extent as well. This searches the delayed ref tree for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) * a given extent, and if there are no other delayed refs to be processed, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) * removes it from the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) u64 bytenr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) struct btrfs_delayed_ref_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) struct btrfs_delayed_ref_root *delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) delayed_refs = &trans->transaction->delayed_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) spin_lock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) goto out_delayed_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) spin_lock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) if (cleanup_extent_op(head) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) * waiting for the lock here would deadlock. If someone else has it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) * locked they are already in the process of dropping it anyway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) if (!mutex_trylock(&head->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) btrfs_delete_ref_head(delayed_refs, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) head->processing = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) BUG_ON(head->extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) if (head->must_insert_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) mutex_unlock(&head->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) btrfs_put_delayed_ref_head(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) spin_unlock(&head->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) out_delayed_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) spin_unlock(&delayed_refs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) struct extent_buffer *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) u64 parent, int last_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) struct btrfs_ref generic_ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) buf->start, buf->len, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) btrfs_ref_tree_mod(fs_info, &generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) BUG_ON(ret); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) if (last_ref && btrfs_header_generation(buf) == trans->transid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) ret = check_ref_cleanup(trans, buf->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) cache = btrfs_lookup_block_group(fs_info, buf->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) pin_down_extent(trans, cache, buf->start, buf->len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) btrfs_add_free_space(cache, buf->start, buf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) btrfs_free_reserved_bytes(cache, buf->len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) if (last_ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) * Deleting the buffer, clear the corrupt flag since it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) * matter anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) /* Can return -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) if (btrfs_is_testing(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) * tree log blocks never actually go into the extent allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) * tree, just update pinning info and exit early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) if ((ref->type == BTRFS_REF_METADATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) (ref->type == BTRFS_REF_DATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) /* unlocks the pinned mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) } else if (ref->type == BTRFS_REF_METADATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) ret = btrfs_add_delayed_tree_ref(trans, ref, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) ret = btrfs_add_delayed_data_ref(trans, ref, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) if (!((ref->type == BTRFS_REF_METADATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) (ref->type == BTRFS_REF_DATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) btrfs_ref_tree_mod(fs_info, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) enum btrfs_loop_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) LOOP_CACHING_NOWAIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) LOOP_CACHING_WAIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) LOOP_ALLOC_CHUNK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) LOOP_NO_EMPTY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) btrfs_lock_block_group(struct btrfs_block_group *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) if (delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) down_read(&cache->data_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) btrfs_get_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) if (delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) down_read(&cache->data_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) static struct btrfs_block_group *btrfs_lock_cluster(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) struct btrfs_block_group *block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) struct btrfs_free_cluster *cluster,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) __acquires(&cluster->refill_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) struct btrfs_block_group *used_bg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) spin_lock(&cluster->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) used_bg = cluster->block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) if (!used_bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) if (used_bg == block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) return used_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) btrfs_get_block_group(used_bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) if (!delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) return used_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) if (down_read_trylock(&used_bg->data_rwsem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) return used_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) spin_unlock(&cluster->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) /* We should only have one-level nested. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) spin_lock(&cluster->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) if (used_bg == cluster->block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) return used_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) up_read(&used_bg->data_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) btrfs_put_block_group(used_bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) btrfs_release_block_group(struct btrfs_block_group *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) if (delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) up_read(&cache->data_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) enum btrfs_extent_allocation_policy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) BTRFS_EXTENT_ALLOC_CLUSTERED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) * Structure used internally for find_free_extent() function. Wraps needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) * parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) struct find_free_extent_ctl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) /* Basic allocation info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) u64 empty_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) int delalloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) /* Where to start the search inside the bg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) u64 search_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) /* For clustered allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) u64 empty_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) struct btrfs_free_cluster *last_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) bool use_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) bool have_caching_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) bool orig_have_caching_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) /* RAID index, converted from flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) * Current loop number, check find_free_extent_update_loop() for details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) int loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) * Whether we're refilling a cluster, if true we need to re-search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) * current block group but don't try to refill the cluster again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) bool retry_clustered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) * Whether we're updating free space cache, if true we need to re-search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) * current block group but don't try updating free space cache again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) bool retry_unclustered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) /* If current block group is cached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) int cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) /* Max contiguous hole found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) u64 max_extent_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) /* Total free space from free space cache, not always contiguous */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) u64 total_free_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) /* Found result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) u64 found_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) /* Hint where to start looking for an empty space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) u64 hint_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) /* Allocation policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) enum btrfs_extent_allocation_policy policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) * Helper function for find_free_extent().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) * Return -ENOENT to inform caller that we need fallback to unclustered mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) * Return -EAGAIN to inform caller that we need to re-search this block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) * Return >0 to inform caller that we find nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) * Return 0 means we have found a location and set ffe_ctl->found_offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) static int find_free_extent_clustered(struct btrfs_block_group *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) struct btrfs_block_group **cluster_bg_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) struct btrfs_block_group *cluster_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) u64 aligned_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) if (!cluster_bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) goto refill_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) if (cluster_bg != bg && (cluster_bg->ro ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) !block_group_bits(cluster_bg, ffe_ctl->flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) goto release_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) ffe_ctl->num_bytes, cluster_bg->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) &ffe_ctl->max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) /* We have a block, we're done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) spin_unlock(&last_ptr->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) trace_btrfs_reserve_extent_cluster(cluster_bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) ffe_ctl->search_start, ffe_ctl->num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) *cluster_bg_ret = cluster_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) ffe_ctl->found_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) WARN_ON(last_ptr->block_group != cluster_bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) release_cluster:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) * lets just skip it and let the allocator find whatever block it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) * find. If we reach this point, we will have tried the cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) * allocator plenty of times and not have found anything, so we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) * likely way too fragmented for the clustering stuff to find anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) * However, if the cluster is taken from the current block group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) * release the cluster first, so that we stand a better chance of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) * succeeding in the unclustered allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) spin_unlock(&last_ptr->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) /* This cluster didn't work out, free it and start over */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) btrfs_return_cluster_to_free_space(NULL, last_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) if (cluster_bg != bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) refill_cluster:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) spin_unlock(&last_ptr->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) aligned_cluster = max_t(u64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) ffe_ctl->empty_cluster + ffe_ctl->empty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) bg->full_stripe_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) ffe_ctl->num_bytes, aligned_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) /* Now pull our allocation out of this cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) offset = btrfs_alloc_from_cluster(bg, last_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) ffe_ctl->num_bytes, ffe_ctl->search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) &ffe_ctl->max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) /* We found one, proceed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) spin_unlock(&last_ptr->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) trace_btrfs_reserve_extent_cluster(bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) ffe_ctl->search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) ffe_ctl->num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) ffe_ctl->found_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) !ffe_ctl->retry_clustered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) spin_unlock(&last_ptr->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) ffe_ctl->retry_clustered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) ffe_ctl->empty_cluster + ffe_ctl->empty_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) * At this point we either didn't find a cluster or we weren't able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) * allocate a block from our cluster. Free the cluster we've been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) * trying to use, and go to the next block group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) btrfs_return_cluster_to_free_space(NULL, last_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) spin_unlock(&last_ptr->refill_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) * Return >0 to inform caller that we find nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) * Return 0 when we found an free extent and set ffe_ctrl->found_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) * Return -EAGAIN to inform caller that we need to re-search this block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) static int find_free_extent_unclustered(struct btrfs_block_group *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) struct find_free_extent_ctl *ffe_ctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) * We are doing an unclustered allocation, set the fragmented flag so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) * we don't bother trying to setup a cluster again until we get more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) * space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) if (unlikely(last_ptr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) spin_lock(&last_ptr->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) last_ptr->fragmented = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) spin_unlock(&last_ptr->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) if (ffe_ctl->cached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) struct btrfs_free_space_ctl *free_space_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) free_space_ctl = bg->free_space_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) spin_lock(&free_space_ctl->tree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) if (free_space_ctl->free_space <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) ffe_ctl->empty_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) ffe_ctl->total_free_space = max_t(u64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) ffe_ctl->total_free_space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) free_space_ctl->free_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) spin_unlock(&free_space_ctl->tree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) spin_unlock(&free_space_ctl->tree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) ffe_ctl->num_bytes, ffe_ctl->empty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) &ffe_ctl->max_extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) * If we didn't find a chunk, and we haven't failed on this block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) * before, and this block group is in the middle of caching and we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) * ok with waiting, then go ahead and wait for progress to be made, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) * set @retry_unclustered to true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) * If @retry_unclustered is true then we've already waited on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) * block group once and should move on to the next block group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) ffe_ctl->empty_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) ffe_ctl->retry_unclustered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) } else if (!offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) ffe_ctl->found_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) static int do_allocation_clustered(struct btrfs_block_group *block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) struct btrfs_block_group **bg_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) /* We want to try and use the cluster allocator, so lets look there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) if (ret >= 0 || ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) /* ret == -ENOENT case falls through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) return find_free_extent_unclustered(block_group, ffe_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) static int do_allocation(struct btrfs_block_group *block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) struct btrfs_block_group **bg_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) switch (ffe_ctl->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) case BTRFS_EXTENT_ALLOC_CLUSTERED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) static void release_block_group(struct btrfs_block_group *block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) switch (ffe_ctl->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) case BTRFS_EXTENT_ALLOC_CLUSTERED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) ffe_ctl->retry_clustered = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) ffe_ctl->retry_unclustered = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) ffe_ctl->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) btrfs_release_block_group(block_group, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) struct btrfs_key *ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) if (!ffe_ctl->use_cluster && last_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) spin_lock(&last_ptr->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) last_ptr->window_start = ins->objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) spin_unlock(&last_ptr->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) static void found_extent(struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) struct btrfs_key *ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) switch (ffe_ctl->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) case BTRFS_EXTENT_ALLOC_CLUSTERED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) found_extent_clustered(ffe_ctl, ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) static int chunk_allocation_failed(struct find_free_extent_ctl *ffe_ctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) switch (ffe_ctl->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) case BTRFS_EXTENT_ALLOC_CLUSTERED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) * If we can't allocate a new chunk we've already looped through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) * at least once, move on to the NO_EMPTY_SIZE case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) * Return >0 means caller needs to re-search for free extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) * Return 0 means we have the needed free extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) * Return <0 means we failed to locate any free extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) struct btrfs_key *ins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) bool full_search)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) struct btrfs_root *root = fs_info->extent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) ffe_ctl->orig_have_caching_bg = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) ffe_ctl->have_caching_bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) if (ins->objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) found_extent(ffe_ctl, ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) * caching kthreads as we move along
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) * again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) ffe_ctl->index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) * We want to skip the LOOP_CACHING_WAIT step if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) * don't have any uncached bgs and we've already done a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) * full search through.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) if (ffe_ctl->orig_have_caching_bg || !full_search)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) ffe_ctl->loop = LOOP_CACHING_WAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) ffe_ctl->loop = LOOP_ALLOC_CHUNK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) ffe_ctl->loop++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) int exist = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) trans = current->journal_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) exist = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) CHUNK_ALLOC_FORCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) /* Do not bail out on ENOSPC since we can do more. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) if (ret == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) ret = chunk_allocation_failed(ffe_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) else if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) if (!exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) * Don't loop again if we already have no empty_size and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) * no empty_cluster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) if (ffe_ctl->empty_size == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) ffe_ctl->empty_cluster == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) ffe_ctl->empty_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) ffe_ctl->empty_cluster = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) struct btrfs_space_info *space_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) struct btrfs_key *ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) * If our free space is heavily fragmented we may not be able to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) * big contiguous allocations, so instead of doing the expensive search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) * for free space, simply return ENOSPC with our max_extent_size so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) * can go ahead and search for a more manageable chunk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) * If our max_extent_size is large enough for our allocation simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) * disable clustering since we will likely not be able to find enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) * space to create a cluster and induce latency trying.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) if (space_info->max_extent_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) spin_lock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) if (space_info->max_extent_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) ffe_ctl->num_bytes > space_info->max_extent_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) ins->offset = space_info->max_extent_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) spin_unlock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) } else if (space_info->max_extent_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) ffe_ctl->use_cluster = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) spin_unlock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) &ffe_ctl->empty_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) if (ffe_ctl->last_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) spin_lock(&last_ptr->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) if (last_ptr->block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) ffe_ctl->hint_byte = last_ptr->window_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) if (last_ptr->fragmented) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) * We still set window_start so we can keep track of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) * last place we found an allocation to try and save
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) * some time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) ffe_ctl->hint_byte = last_ptr->window_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) ffe_ctl->use_cluster = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) spin_unlock(&last_ptr->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) static int prepare_allocation(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) struct find_free_extent_ctl *ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) struct btrfs_space_info *space_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) struct btrfs_key *ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) switch (ffe_ctl->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) case BTRFS_EXTENT_ALLOC_CLUSTERED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) return prepare_allocation_clustered(fs_info, ffe_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) space_info, ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) * walks the btree of allocated extents and find a hole of a given size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) * The key ins is changed to record the hole:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) * ins->objectid == start position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) * ins->flags = BTRFS_EXTENT_ITEM_KEY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) * ins->offset == the size of the hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) * Any available blocks before search_start are skipped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) * If there is no suitable free space, we will record the max size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) * the free space extent currently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) * The overall logic and call chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) * find_free_extent()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) * |- Iterate through all block groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) * | |- Get a valid block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) * | |- Try to do clustered allocation in that block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) * | |- Try to do unclustered allocation in that block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) * | |- Check if the result is valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) * | | |- If valid, then exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) * | |- Jump to next block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) * |- Push harder to find free extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) * |- If not found, re-iterate all block groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) static noinline int find_free_extent(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) u64 ram_bytes, u64 num_bytes, u64 empty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) u64 hint_byte_orig, struct btrfs_key *ins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) u64 flags, int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) int cache_block_group_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) struct btrfs_block_group *block_group = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) struct find_free_extent_ctl ffe_ctl = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) struct btrfs_space_info *space_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) bool full_search = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) WARN_ON(num_bytes < fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) ffe_ctl.num_bytes = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) ffe_ctl.empty_size = empty_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) ffe_ctl.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) ffe_ctl.search_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) ffe_ctl.delalloc = delalloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) ffe_ctl.have_caching_bg = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) ffe_ctl.orig_have_caching_bg = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) ffe_ctl.found_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) ffe_ctl.hint_byte = hint_byte_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) /* For clustered allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) ffe_ctl.retry_clustered = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) ffe_ctl.retry_unclustered = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) ffe_ctl.last_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) ffe_ctl.use_cluster = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) ins->type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) ins->objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) ins->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) trace_find_free_extent(root, num_bytes, empty_size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) space_info = btrfs_find_space_info(fs_info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) if (!space_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) btrfs_err(fs_info, "No space info for %llu", flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) ret = prepare_allocation(fs_info, &ffe_ctl, space_info, ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) ffe_ctl.search_start = max(ffe_ctl.search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) first_logical_byte(fs_info, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) ffe_ctl.search_start = max(ffe_ctl.search_start, ffe_ctl.hint_byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) if (ffe_ctl.search_start == ffe_ctl.hint_byte) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) block_group = btrfs_lookup_block_group(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) ffe_ctl.search_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) * we don't want to use the block group if it doesn't match our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) * allocation bits, or if its not cached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) * However if we are re-searching with an ideal block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) * picked out then we don't care that the block group is cached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) if (block_group && block_group_bits(block_group, flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) block_group->cached != BTRFS_CACHE_NO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) down_read(&space_info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) if (list_empty(&block_group->list) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) block_group->ro) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) * someone is removing this block group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) * we can't jump into the have_block_group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) * target because our list pointers are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) * valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) btrfs_put_block_group(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) up_read(&space_info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) ffe_ctl.index = btrfs_bg_flags_to_raid_index(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) block_group->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) btrfs_lock_block_group(block_group, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) goto have_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) } else if (block_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) btrfs_put_block_group(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) search:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) ffe_ctl.have_caching_bg = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) ffe_ctl.index == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) full_search = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) down_read(&space_info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) list_for_each_entry(block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) &space_info->block_groups[ffe_ctl.index], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) struct btrfs_block_group *bg_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) /* If the block group is read-only, we can skip it entirely. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) if (unlikely(block_group->ro))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) btrfs_grab_block_group(block_group, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) ffe_ctl.search_start = block_group->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) * this can happen if we end up cycling through all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) * raid types, but we want to make sure we only allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) * for the proper type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) if (!block_group_bits(block_group, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) u64 extra = BTRFS_BLOCK_GROUP_DUP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) BTRFS_BLOCK_GROUP_RAID1_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) BTRFS_BLOCK_GROUP_RAID56_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) BTRFS_BLOCK_GROUP_RAID10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) * if they asked for extra copies and this block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) * doesn't provide them, bail. This does allow us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) * fill raid0 from raid1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) if ((flags & extra) && !(block_group->flags & extra))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) goto loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) * This block group has different flags than we want.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) * It's possible that we have MIXED_GROUP flag but no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) * block group is mixed. Just skip such block group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) btrfs_release_block_group(block_group, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) have_block_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) ffe_ctl.cached = btrfs_block_group_done(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) if (unlikely(!ffe_ctl.cached)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) ffe_ctl.have_caching_bg = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) ret = btrfs_cache_block_group(block_group, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) * If we get ENOMEM here or something else we want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) * try other block groups, because it may not be fatal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) * However if we can't find anything else we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) * save our return here so that we return the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) * error that caused problems, not ENOSPC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) if (!cache_block_group_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) cache_block_group_error = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) goto loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) goto loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) bg_ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) ret = do_allocation(block_group, &ffe_ctl, &bg_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) if (bg_ret && bg_ret != block_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) btrfs_release_block_group(block_group, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) block_group = bg_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) } else if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) goto have_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) goto loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) /* Checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) fs_info->stripesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) /* move on to the next group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) if (ffe_ctl.search_start + num_bytes >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) block_group->start + block_group->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) btrfs_add_free_space(block_group, ffe_ctl.found_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) goto loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) if (ffe_ctl.found_offset < ffe_ctl.search_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) btrfs_add_free_space(block_group, ffe_ctl.found_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) ffe_ctl.search_start - ffe_ctl.found_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) num_bytes, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) btrfs_add_free_space(block_group, ffe_ctl.found_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) goto loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) btrfs_inc_block_group_reservations(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) /* we are all good, lets return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) ins->objectid = ffe_ctl.search_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) ins->offset = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) btrfs_release_block_group(block_group, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) loop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) release_block_group(block_group, &ffe_ctl, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) up_read(&space_info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) ret = find_free_extent_update_loop(fs_info, ins, &ffe_ctl, full_search);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) goto search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) if (ret == -ENOSPC && !cache_block_group_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) * Use ffe_ctl->total_free_space as fallback if we can't find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) * any contiguous hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) if (!ffe_ctl.max_extent_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) spin_lock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) space_info->max_extent_size = ffe_ctl.max_extent_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) spin_unlock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) ins->offset = ffe_ctl.max_extent_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) } else if (ret == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) ret = cache_block_group_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) * hole that is at least as big as @num_bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) * @root - The root that will contain this extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) * @ram_bytes - The amount of space in ram that @num_bytes take. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) * is used for accounting purposes. This value differs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) * from @num_bytes only in the case of compressed extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) * @num_bytes - Number of bytes to allocate on-disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) * @min_alloc_size - Indicates the minimum amount of space that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) * allocator should try to satisfy. In some cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) * @num_bytes may be larger than what is required and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) * the filesystem is fragmented then allocation fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) * However, the presence of @min_alloc_size gives a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) * chance to try and satisfy the smaller allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) * @empty_size - A hint that you plan on doing more COW. This is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) * size in bytes the allocator should try to find free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) * next to the block it returns. This is just a hint and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) * may be ignored by the allocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) * @hint_byte - Hint to the allocator to start searching above the byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) * address passed. It might be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) * @ins - This key is modified to record the found hole. It will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) * have the following values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) * ins->objectid == start position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) * ins->flags = BTRFS_EXTENT_ITEM_KEY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) * ins->offset == the size of the hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) * @is_data - Boolean flag indicating whether an extent is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) * allocated for data (true) or metadata (false)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) * @delalloc - Boolean flag indicating whether this allocation is for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) * delalloc or not. If 'true' data_rwsem of block groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) * is going to be acquired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) * case -ENOSPC is returned then @ins->offset will contain the size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) * largest available hole the allocator managed to find.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) u64 num_bytes, u64 min_alloc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) u64 empty_size, u64 hint_byte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) struct btrfs_key *ins, int is_data, int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) bool final_tried = num_bytes == min_alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) flags = get_alloc_profile_by_root(root, is_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) WARN_ON(num_bytes < fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) hint_byte, ins, flags, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) if (!ret && !is_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) btrfs_dec_block_group_reservations(fs_info, ins->objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) } else if (ret == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) if (!final_tried && ins->offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) num_bytes = min(num_bytes >> 1, ins->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) num_bytes = round_down(num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) num_bytes = max(num_bytes, min_alloc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) ram_bytes = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) if (num_bytes == min_alloc_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) final_tried = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) struct btrfs_space_info *sinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) sinfo = btrfs_find_space_info(fs_info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) "allocation failed flags %llu, wanted %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) flags, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) if (sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) btrfs_dump_space_info(fs_info, sinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) num_bytes, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) u64 start, u64 len, int delalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) cache = btrfs_lookup_block_group(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) btrfs_err(fs_info, "Unable to find block group for %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) btrfs_add_free_space(cache, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) btrfs_free_reserved_bytes(cache, len, delalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) trace_btrfs_reserved_extent_free(fs_info, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) cache = btrfs_lookup_block_group(trans->fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) btrfs_err(trans->fs_info, "unable to find block group for %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) ret = pin_down_extent(trans, cache, start, len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) u64 parent, u64 root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) u64 flags, u64 owner, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) struct btrfs_key *ins, int ref_mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) struct btrfs_extent_item *extent_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) struct btrfs_extent_inline_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) if (parent > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) type = BTRFS_SHARED_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) type = BTRFS_EXTENT_DATA_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) ins, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) extent_item = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) btrfs_set_extent_refs(leaf, extent_item, ref_mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) btrfs_set_extent_generation(leaf, extent_item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) btrfs_set_extent_flags(leaf, extent_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) flags | BTRFS_EXTENT_FLAG_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) btrfs_set_extent_inline_ref_type(leaf, iref, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) if (parent > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) struct btrfs_shared_data_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) ref = (struct btrfs_shared_data_ref *)(iref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) struct btrfs_extent_data_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) ref = (struct btrfs_extent_data_ref *)(&iref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) btrfs_set_extent_data_ref_offset(leaf, ref, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) if (ret) { /* -ENOENT, logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) btrfs_err(fs_info, "update block group failed for %llu %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) ins->objectid, ins->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) struct btrfs_delayed_ref_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) struct btrfs_delayed_extent_op *extent_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) struct btrfs_extent_item *extent_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) struct btrfs_key extent_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) struct btrfs_tree_block_info *block_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) struct btrfs_extent_inline_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) struct btrfs_delayed_tree_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) u32 size = sizeof(*extent_item) + sizeof(*iref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) u64 flags = extent_op->flags_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) ref = btrfs_delayed_node_to_tree_ref(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) extent_key.objectid = node->bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) if (skinny_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) extent_key.offset = ref->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) extent_key.type = BTRFS_METADATA_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) num_bytes = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) extent_key.offset = node->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) extent_key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) size += sizeof(*block_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) num_bytes = node->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) &extent_key, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) extent_item = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) struct btrfs_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) btrfs_set_extent_refs(leaf, extent_item, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) btrfs_set_extent_generation(leaf, extent_item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) btrfs_set_extent_flags(leaf, extent_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) if (skinny_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) btrfs_set_tree_block_level(leaf, block_info, ref->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) btrfs_set_extent_inline_ref_type(leaf, iref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) BTRFS_SHARED_BLOCK_REF_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) btrfs_set_extent_inline_ref_type(leaf, iref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) BTRFS_TREE_BLOCK_REF_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) ret = remove_from_free_space_tree(trans, extent_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) ret = btrfs_update_block_group(trans, extent_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) fs_info->nodesize, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) if (ret) { /* -ENOENT, logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) btrfs_err(fs_info, "update block group failed for %llu %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) extent_key.objectid, extent_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) fs_info->nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) struct btrfs_root *root, u64 owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) u64 offset, u64 ram_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) struct btrfs_key *ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) struct btrfs_ref generic_ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) ins->objectid, ins->offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) btrfs_ref_tree_mod(root->fs_info, &generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) return btrfs_add_delayed_data_ref(trans, &generic_ref, ram_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) * this is used by the tree logging recovery code. It records that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) * an extent has been allocated and makes sure to clear the free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) * space cache bits as well
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) u64 root_objectid, u64 owner, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) struct btrfs_key *ins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) struct btrfs_block_group *block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) struct btrfs_space_info *space_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) * Mixed block groups will exclude before processing the log so we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) * need to do the exclude dance if this fs isn't mixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) ret = __exclude_logged_extent(fs_info, ins->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) ins->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) if (!block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) space_info = block_group->space_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) spin_lock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) spin_lock(&block_group->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) space_info->bytes_reserved += ins->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) block_group->reserved += ins->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) spin_unlock(&block_group->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) spin_unlock(&space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) offset, ins, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) btrfs_put_block_group(block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) static struct extent_buffer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) u64 bytenr, int level, u64 owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) enum btrfs_lock_nesting nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) struct extent_buffer *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) buf = btrfs_find_create_tree_block(fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) if (IS_ERR(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) * Extra safety check in case the extent tree is corrupted and extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) * allocator chooses to use a tree block which is already used and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) * locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) if (buf->lock_owner == current->pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) buf->start, btrfs_header_owner(buf), current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) free_extent_buffer(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) return ERR_PTR(-EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) btrfs_set_buffer_lockdep_class(owner, buf, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) __btrfs_tree_lock(buf, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) btrfs_clean_tree_block(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) btrfs_set_lock_blocking_write(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) set_extent_buffer_uptodate(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) btrfs_set_header_level(buf, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) btrfs_set_header_bytenr(buf, buf->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) btrfs_set_header_generation(buf, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) btrfs_set_header_owner(buf, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) buf->log_index = root->log_transid % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) * we allow two log transactions at a time, use different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) * EXTENT bit to differentiate dirty pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) if (buf->log_index == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) set_extent_dirty(&root->dirty_log_pages, buf->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) buf->start + buf->len - 1, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) set_extent_new(&root->dirty_log_pages, buf->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) buf->start + buf->len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) buf->log_index = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) buf->start + buf->len - 1, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) trans->dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) /* this returns a buffer locked for blocking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) * finds a free extent and does all the dirty work required for allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) * returns the tree buffer or an ERR_PTR on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) u64 parent, u64 root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) const struct btrfs_disk_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) int level, u64 hint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) u64 empty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) enum btrfs_lock_nesting nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) struct btrfs_block_rsv *block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) struct extent_buffer *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) struct btrfs_delayed_extent_op *extent_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) struct btrfs_ref generic_ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) u64 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) u32 blocksize = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) if (btrfs_is_testing(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) level, root_objectid, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) if (!IS_ERR(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) root->alloc_bytenr += blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) if (IS_ERR(block_rsv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) return ERR_CAST(block_rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) empty_size, hint, &ins, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) goto out_unuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) root_objectid, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) if (IS_ERR(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) ret = PTR_ERR(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) goto out_free_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) if (parent == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) parent = ins.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) BUG_ON(parent > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) extent_op = btrfs_alloc_delayed_extent_op();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) if (!extent_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) goto out_free_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) if (key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) memcpy(&extent_op->key, key, sizeof(extent_op->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) memset(&extent_op->key, 0, sizeof(extent_op->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) extent_op->flags_to_set = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) extent_op->update_key = skinny_metadata ? false : true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) extent_op->update_flags = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) extent_op->is_data = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) extent_op->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) ins.objectid, ins.offset, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) generic_ref.real_root = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) btrfs_init_tree_ref(&generic_ref, level, root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) btrfs_ref_tree_mod(fs_info, &generic_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) goto out_free_delayed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) out_free_delayed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) btrfs_free_delayed_extent_op(extent_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) out_free_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) btrfs_tree_unlock(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) free_extent_buffer(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) out_free_reserved:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) out_unuse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) struct walk_control {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) u64 refs[BTRFS_MAX_LEVEL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) u64 flags[BTRFS_MAX_LEVEL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) struct btrfs_key update_progress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) struct btrfs_key drop_progress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) int drop_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) int stage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) int shared_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) int update_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) int keep_locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) int reada_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) int reada_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) int restarted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) #define DROP_REFERENCE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) #define UPDATE_BACKREF 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) struct walk_control *wc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) u64 bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) u64 generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) u64 refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) int nread = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) if (path->slots[wc->level] < wc->reada_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) wc->reada_count = wc->reada_count * 2 / 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) wc->reada_count = max(wc->reada_count, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) wc->reada_count = wc->reada_count * 3 / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) wc->reada_count = min_t(int, wc->reada_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) BTRFS_NODEPTRS_PER_BLOCK(fs_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) eb = path->nodes[wc->level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) nritems = btrfs_header_nritems(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) for (slot = path->slots[wc->level]; slot < nritems; slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) if (nread >= wc->reada_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) bytenr = btrfs_node_blockptr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) generation = btrfs_node_ptr_generation(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) if (slot == path->slots[wc->level])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) goto reada;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) if (wc->stage == UPDATE_BACKREF &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) generation <= root->root_key.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) /* We don't lock the tree block, it's OK to be racy here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) wc->level - 1, 1, &refs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) /* We don't care about errors in readahead. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) BUG_ON(refs == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) if (wc->stage == DROP_REFERENCE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) if (refs == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) goto reada;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) if (wc->level == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) if (!wc->update_ref ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) generation <= root->root_key.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) btrfs_node_key_to_cpu(eb, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) ret = btrfs_comp_cpu_keys(&key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) &wc->update_progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) if (wc->level == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) reada:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) readahead_tree_block(fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) nread++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) wc->reada_slot = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) * helper to process tree block while walking down the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) * when wc->stage == UPDATE_BACKREF, this function updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) * back refs for pointers in the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) * NOTE: return value 1 means we should stop walking down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) struct walk_control *wc, int lookup_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) int level = wc->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) struct extent_buffer *eb = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) if (wc->stage == UPDATE_BACKREF &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) btrfs_header_owner(eb) != root->root_key.objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) * when reference count of tree block is 1, it won't increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) * again. once full backref flag is set, we never clear it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) if (lookup_info &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) BUG_ON(!path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) ret = btrfs_lookup_extent_info(trans, fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) eb->start, level, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) &wc->refs[level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) &wc->flags[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) BUG_ON(ret == -ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) BUG_ON(wc->refs[level] == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) if (wc->stage == DROP_REFERENCE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) if (wc->refs[level] > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) if (path->locks[level] && !wc->keep_locks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) btrfs_tree_unlock_rw(eb, path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) /* wc->stage == UPDATE_BACKREF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) if (!(wc->flags[level] & flag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) BUG_ON(!path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) ret = btrfs_inc_ref(trans, root, eb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) BUG_ON(ret); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) ret = btrfs_dec_ref(trans, root, eb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) BUG_ON(ret); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) ret = btrfs_set_disk_extent_flags(trans, eb, flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) btrfs_header_level(eb), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) BUG_ON(ret); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) wc->flags[level] |= flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) * the block is shared by multiple trees, so it's not good to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) * keep the tree lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) if (path->locks[level] && level > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) btrfs_tree_unlock_rw(eb, path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) * This is used to verify a ref exists for this root to deal with a bug where we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) * would have a drop_progress key that hadn't been updated properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) static int check_ref_exists(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) struct btrfs_root *root, u64 bytenr, u64 parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) struct btrfs_extent_inline_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) ret = lookup_extent_backref(trans, path, &iref, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) root->fs_info->nodesize, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) root->root_key.objectid, level, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930) * helper to process tree block pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) * when wc->stage == DROP_REFERENCE, this function checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) * reference count of the block pointed to. if the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) * is shared and we need update back refs for the subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) * rooted at the block, this function changes wc->stage to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) * UPDATE_BACKREF. if the block is shared and there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) * need to update back, this function drops the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) * to the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) * NOTE: return value 1 means we should stop walking down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) static noinline int do_walk_down(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) struct walk_control *wc, int *lookup_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) u64 bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) u64 generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) u64 parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) struct btrfs_ref ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) struct extent_buffer *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) int level = wc->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) int reada = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) bool need_account = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) generation = btrfs_node_ptr_generation(path->nodes[level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) path->slots[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) * if the lower level block was created before the snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) * was created, we know there is no need to update back refs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) * for the subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) if (wc->stage == UPDATE_BACKREF &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) generation <= root->root_key.offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) *lookup_info = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) btrfs_node_key_to_cpu(path->nodes[level], &first_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) path->slots[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) next = find_extent_buffer(fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) if (!next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) next = btrfs_find_create_tree_block(fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) if (IS_ERR(next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) return PTR_ERR(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) level - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) reada = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) btrfs_tree_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) btrfs_set_lock_blocking_write(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) &wc->refs[level - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) &wc->flags[level - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) if (unlikely(wc->refs[level - 1] == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) btrfs_err(fs_info, "Missing references.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) *lookup_info = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) if (wc->stage == DROP_REFERENCE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) if (wc->refs[level - 1] > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) need_account = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) if (level == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) if (!wc->update_ref ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) generation <= root->root_key.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) btrfs_node_key_to_cpu(path->nodes[level], &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) path->slots[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) wc->stage = UPDATE_BACKREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) wc->shared_level = level - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) if (level == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) if (!btrfs_buffer_uptodate(next, generation, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) btrfs_tree_unlock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) *lookup_info = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) if (!next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) if (reada && level == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) reada_walk_down(trans, root, wc, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) next = read_tree_block(fs_info, bytenr, generation, level - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) if (IS_ERR(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) return PTR_ERR(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) } else if (!extent_buffer_uptodate(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) btrfs_tree_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) btrfs_set_lock_blocking_write(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) level--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) ASSERT(level == btrfs_header_level(next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) if (level != btrfs_header_level(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) btrfs_err(root->fs_info, "mismatched level");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) path->nodes[level] = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059) path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) wc->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) if (wc->level == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) wc->reada_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) wc->refs[level - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) wc->flags[level - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) if (wc->stage == DROP_REFERENCE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) parent = path->nodes[level]->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) ASSERT(root->root_key.objectid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) btrfs_header_owner(path->nodes[level]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) if (root->root_key.objectid !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) btrfs_header_owner(path->nodes[level])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) btrfs_err(root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) "mismatched block owner");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) * If we had a drop_progress we need to verify the refs are set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086) * as expected. If we find our ref then we know that from here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087) * on out everything should be correct, and we can clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088) * ->restarted flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) if (wc->restarted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091) ret = check_ref_exists(trans, root, bytenr, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) level - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) wc->restarted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) * Reloc tree doesn't contribute to qgroup numbers, and we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) * already accounted them at merge time (replace_path),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) * thus we could skip expensive subtree trace here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) need_account) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) ret = btrfs_qgroup_trace_subtree(trans, next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109) generation, level - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) * We need to update the next key in our walk control so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) * update the drop_progress key accordingly. We don't care if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) * find_next_key doesn't find a key because that means we're at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) * the end and are going to clean up now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) wc->drop_level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) find_next_key(path, level, &wc->drop_progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) fs_info->nodesize, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) ret = btrfs_free_extent(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) no_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) *lookup_info = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) btrfs_tree_unlock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) * helper to process tree block while walking up the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) * when wc->stage == DROP_REFERENCE, this function drops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) * reference count on the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) * when wc->stage == UPDATE_BACKREF, this function changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) * wc->stage back to DROP_REFERENCE if we changed wc->stage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) * to UPDATE_BACKREF previously while processing the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) * NOTE: return value 1 means we should stop walking up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) struct walk_control *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) int level = wc->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) struct extent_buffer *eb = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) u64 parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167) if (wc->stage == UPDATE_BACKREF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) BUG_ON(wc->shared_level < level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) if (level < wc->shared_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) ret = find_next_key(path, level + 1, &wc->update_progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) wc->update_ref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) wc->stage = DROP_REFERENCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) wc->shared_level = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) * check reference count again if the block isn't locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) * we should start walking down the tree again if reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) * count is one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) if (!path->locks[level]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) BUG_ON(level == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) btrfs_tree_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) btrfs_set_lock_blocking_write(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) ret = btrfs_lookup_extent_info(trans, fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) eb->start, level, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) &wc->refs[level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) &wc->flags[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) btrfs_tree_unlock_rw(eb, path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) BUG_ON(wc->refs[level] == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) if (wc->refs[level] == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) btrfs_tree_unlock_rw(eb, path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) /* wc->stage == DROP_REFERENCE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) if (wc->refs[level] == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) if (level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) ret = btrfs_dec_ref(trans, root, eb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) ret = btrfs_dec_ref(trans, root, eb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) BUG_ON(ret); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) if (is_fstree(root->root_key.objectid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) ret = btrfs_qgroup_trace_leaf_items(trans, eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) btrfs_err_rl(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) "error %d accounting leaf items, quota is out of sync, rescan required",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) /* make block locked assertion in btrfs_clean_tree_block happy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) if (!path->locks[level] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) btrfs_header_generation(eb) == trans->transid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) btrfs_tree_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) btrfs_set_lock_blocking_write(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) btrfs_clean_tree_block(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) if (eb == root->node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239) if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) parent = eb->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) else if (root->root_key.objectid != btrfs_header_owner(eb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242) goto owner_mismatch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) parent = path->nodes[level + 1]->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) else if (root->root_key.objectid !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) btrfs_header_owner(path->nodes[level + 1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) goto owner_mismatch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253) wc->refs[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254) wc->flags[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257) owner_mismatch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) btrfs_header_owner(eb), root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) struct walk_control *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) int level = wc->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) int lookup_info = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) while (level >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) ret = walk_down_proc(trans, root, path, wc, lookup_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) if (path->slots[level] >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) btrfs_header_nritems(path->nodes[level]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) ret = do_walk_down(trans, root, path, wc, &lookup_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) path->slots[level]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) } else if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) level = wc->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) struct walk_control *wc, int max_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) int level = wc->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) path->slots[level] = btrfs_header_nritems(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) while (level < max_level && path->nodes[level]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) wc->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) if (path->slots[level] + 1 <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) btrfs_header_nritems(path->nodes[level])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) path->slots[level]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) ret = walk_up_proc(trans, root, path, wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) if (path->locks[level]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318) btrfs_tree_unlock_rw(path->nodes[level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) free_extent_buffer(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) path->nodes[level] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) level++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) * drop a subvolume tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) * this function traverses the tree freeing any blocks that only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) * referenced by the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) * when a shared tree block is found. this function decreases its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) * reference count by one. if update_ref is true, this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) * also make sure backrefs for the shared block and all lower level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) * blocks are properly updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) * If called with for_reloc == 0, may exit early with -EAGAIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) struct btrfs_root *tree_root = fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) struct btrfs_root_item *root_item = &root->root_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) struct walk_control *wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) bool root_dropped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) wc = kzalloc(sizeof(*wc), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) if (!wc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373) * Use join to avoid potential EINTR from transaction start. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) * wait_reserve_ticket and the whole reservation callchain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) if (for_reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) trans = btrfs_join_transaction(tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) trans = btrfs_start_transaction(tree_root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) err = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) err = btrfs_run_delayed_items(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) * This will help us catch people modifying the fs tree while we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) * dropping it. It is unsafe to mess with the fs tree while it's being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) * dropped as we unlock the root node and parent nodes as we walk down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) * the tree, assuming nothing will change. If something does change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) * then we'll have stale information and drop references to blocks we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) * already dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) set_bit(BTRFS_ROOT_DELETING, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) level = btrfs_header_level(root->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) path->nodes[level] = btrfs_lock_root_node(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) btrfs_set_lock_blocking_write(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) memset(&wc->update_progress, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) sizeof(wc->update_progress));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) memcpy(&wc->update_progress, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) sizeof(wc->update_progress));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) level = root_item->drop_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) BUG_ON(level == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) path->lowest_level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) path->lowest_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) WARN_ON(ret > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) * unlock our path, this is safe because only this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) * function is allowed to delete this snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) btrfs_unlock_up_safe(path, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) level = btrfs_header_level(root->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) btrfs_tree_lock(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) btrfs_set_lock_blocking_write(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) ret = btrfs_lookup_extent_info(trans, fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) path->nodes[level]->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) level, 1, &wc->refs[level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) &wc->flags[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) BUG_ON(wc->refs[level] == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) if (level == root_item->drop_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) btrfs_tree_unlock(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) WARN_ON(wc->refs[level] != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) level--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) wc->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) wc->shared_level = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) wc->stage = DROP_REFERENCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) wc->update_ref = update_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) wc->keep_locks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) ret = walk_down_tree(trans, root, path, wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) BUG_ON(wc->stage != DROP_REFERENCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) if (wc->stage == DROP_REFERENCE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) wc->drop_level = wc->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) &wc->drop_progress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) path->slots[wc->drop_level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) btrfs_cpu_key_to_disk(&root_item->drop_progress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) &wc->drop_progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) root_item->drop_level = wc->drop_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) BUG_ON(wc->level == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) if (btrfs_should_end_transaction(trans) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) ret = btrfs_update_root(trans, tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) &root->root_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) btrfs_end_transaction_throttle(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) "drop snapshot early exit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) * Use join to avoid potential EINTR from transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) * start. See wait_reserve_ticket and the whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) * reservation callchain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) if (for_reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) trans = btrfs_join_transaction(tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) trans = btrfs_start_transaction(tree_root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) err = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) ret = btrfs_del_root(trans, &root->root_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) ret = btrfs_find_root(tree_root, &root->root_key, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) /* if we fail to delete the orphan item this time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) * around, it'll get picked up the next time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) * The most common failure here is just -ENOENT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) btrfs_del_orphan_item(trans, tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) * This subvolume is going to be completely dropped, and won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) * recorded as dirty roots, thus pertrans meta rsv will not be freed at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) * commit transaction time. So free it here manually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) btrfs_qgroup_convert_reserved_meta(root, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) btrfs_qgroup_free_meta_all_pertrans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) btrfs_add_dropped_root(trans, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) root_dropped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) out_end_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) btrfs_end_transaction_throttle(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) kfree(wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) * So if we need to stop dropping the snapshot for whatever reason we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) * need to make sure to add it back to the dead root list so that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577) * keep trying to do the work later. This also cleans up roots if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) * don't have it in the radix (like when we recover after a power fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) * or unmount) so we don't leak memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) if (!for_reloc && !root_dropped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) btrfs_add_dead_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) * drop subtree rooted at tree block 'node'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) * NOTE: this function will unlock and release tree block 'node'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) * only used by relocation code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) struct extent_buffer *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) struct extent_buffer *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) struct walk_control *wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) int parent_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) int wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) wc = kzalloc(sizeof(*wc), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) if (!wc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) btrfs_assert_tree_locked(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) parent_level = btrfs_header_level(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) atomic_inc(&parent->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) path->nodes[parent_level] = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) path->slots[parent_level] = btrfs_header_nritems(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) btrfs_assert_tree_locked(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) level = btrfs_header_level(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) path->nodes[level] = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) wc->refs[parent_level] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) wc->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) wc->shared_level = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) wc->stage = DROP_REFERENCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) wc->update_ref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) wc->keep_locks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) wret = walk_down_tree(trans, root, path, wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) if (wret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) wret = walk_up_tree(trans, root, path, wc, parent_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) if (wret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) if (wret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) kfree(wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) * helper to account the unused space of all the readonly block group in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) * space_info. takes mirrors into account.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) struct btrfs_block_group *block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) u64 free_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) int factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) /* It's df, we don't care if it's racy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) if (list_empty(&sinfo->ro_bgs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) spin_lock(&sinfo->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) spin_lock(&block_group->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) if (!block_group->ro) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) spin_unlock(&block_group->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) factor = btrfs_bg_type_to_factor(block_group->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) free_bytes += (block_group->length -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) block_group->used) * factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) spin_unlock(&block_group->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686) spin_unlock(&sinfo->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) return free_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) return unpin_extent_range(fs_info, start, end, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) * It used to be that old block groups would be left around forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) * Iterating over them would be enough to trim unused space. Since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) * now automatically remove them, we also need to iterate over unallocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) * space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) * We don't want a transaction for this since the discard may take a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) * substantial amount of time. We don't require that a transaction be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) * running, but we do need to take a running transaction into account
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) * to ensure that we're not discarding chunks that were released or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) * allocated in the current transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) * Holding the chunks lock will prevent other threads from allocating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) * or releasing chunks, but it won't prevent a running transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) * from committing and releasing the memory that the pending chunks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712) * list head uses. For that, we need to take a reference to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) * transaction and hold the commit root sem. We only need to hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) * it while performing the free space search since we have already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) * held back allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) u64 start = SZ_1M, len = 0, end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) *trimmed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724) /* Discard not supported = nothing to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) if (!blk_queue_discard(bdev_get_queue(device->bdev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) /* Not writable = nothing to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729) if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) /* No free space = nothing to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) if (device->total_bytes <= device->bytes_used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739) struct btrfs_fs_info *fs_info = device->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) u64 bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) find_first_clear_extent_bit(&device->alloc_state, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) CHUNK_TRIMMED | CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) /* Check if there are any CHUNK_* bits left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) if (start > device->total_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753) btrfs_warn_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) "ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755) start, end - start + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756) rcu_str_deref(device->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757) device->total_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758) mutex_unlock(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) /* Ensure we skip the reserved area in the first 1M */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764) start = max_t(u64, start, SZ_1M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) * If find_first_clear_extent_bit find a range that spans the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768) * end of the device it will set end to -1, in this case it's up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769) * to the caller to trim the value to the size of the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771) end = min(end, device->total_bytes - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) len = end - start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775) /* We didn't find any extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) if (!len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) mutex_unlock(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) ret = btrfs_issue_discard(device->bdev, start, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) &bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) set_extent_bits(&device->alloc_state, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) start + bytes - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787) CHUNK_TRIMMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) mutex_unlock(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793) start += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794) *trimmed += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796) if (fatal_signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) * Trim the whole filesystem by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809) * 1) trimming the free space in each block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810) * 2) trimming the unallocated space on each device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812) * This will also continue trimming even if a block group or device encounters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813) * an error. The return value will be the last error, or 0 if nothing bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814) * happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816) int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818) struct btrfs_block_group *cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819) struct btrfs_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820) struct list_head *devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821) u64 group_trimmed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822) u64 range_end = U64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825) u64 trimmed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826) u64 bg_failed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827) u64 dev_failed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828) int bg_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829) int dev_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833) * Check range overflow if range->len is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834) * The default range->len is U64_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836) if (range->len != U64_MAX &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837) check_add_overflow(range->start, range->len, &range_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840) cache = btrfs_lookup_first_block_group(fs_info, range->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841) for (; cache; cache = btrfs_next_block_group(cache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842) if (cache->start >= range_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) start = max(range->start, cache->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) end = min(range_end, cache->start + cache->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) if (end - start >= range->minlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) if (!btrfs_block_group_done(cache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) ret = btrfs_cache_block_group(cache, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) bg_failed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) bg_ret = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) ret = btrfs_wait_block_group_cache_done(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) bg_failed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861) bg_ret = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) ret = btrfs_trim_block_group(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) &group_trimmed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869) range->minlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) trimmed += group_trimmed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) bg_failed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874) bg_ret = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) if (bg_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881) btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882) "failed to trim %llu block group(s), last error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) bg_failed, bg_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) mutex_lock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885) devices = &fs_info->fs_devices->devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) list_for_each_entry(device, devices, dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887) if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) ret = btrfs_trim_free_extents(device, &group_trimmed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) dev_failed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) dev_ret = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) trimmed += group_trimmed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899) mutex_unlock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901) if (dev_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902) btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903) "failed to trim %llu device(s), last error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904) dev_failed, dev_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905) range->len = trimmed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906) if (bg_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907) return bg_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) return dev_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) }