^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,2008 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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *root, struct btrfs_path *path, int level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) const struct btrfs_key *ins_key, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int data_size, int extend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int push_node_left(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct extent_buffer *src, int empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int balance_node_right(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct extent_buffer *dst_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct extent_buffer *src_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int level, int slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static const struct btrfs_csums {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u16 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) const char name[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const char driver[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) } btrfs_csums[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .driver = "blake2b-256" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int btrfs_super_csum_size(const struct btrfs_super_block *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u16 t = btrfs_super_csum_type(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * csum type is validated at mount time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return btrfs_csums[t].size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) const char *btrfs_super_csum_name(u16 csum_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* csum type is validated at mount time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return btrfs_csums[csum_type].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Return driver name if defined, otherwise the name that's also a valid driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) const char *btrfs_super_csum_driver(u16 csum_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* csum type is validated at mount time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return btrfs_csums[csum_type].driver[0] ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) btrfs_csums[csum_type].driver :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) btrfs_csums[csum_type].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) size_t __attribute_const__ btrfs_get_num_csums(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return ARRAY_SIZE(btrfs_csums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct btrfs_path *btrfs_alloc_path(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* this also releases the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) void btrfs_free_path(struct btrfs_path *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) kmem_cache_free(btrfs_path_cachep, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * path release drops references on the extent buffers in the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * and it drops any locks held by this path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * It is safe to call this on paths that no locks or extent buffers held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) noinline void btrfs_release_path(struct btrfs_path *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) p->slots[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!p->nodes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (p->locks[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) p->locks[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) free_extent_buffer(p->nodes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) p->nodes[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * safely gets a reference on the root node of a tree. A lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * is not taken, so a concurrent writer may put a different node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * at the root of the tree. See btrfs_lock_root_node for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * looping required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * The extent buffer returned by this has a reference taken, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * it won't disappear. It may stop being the root of the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * at any time because there are no locks held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) eb = rcu_dereference(root->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * RCU really hurts here, we could free up the root node because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * it was COWed but we may not get the new root node yet so do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * the inc_not_zero dance and if it doesn't work then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * synchronize_rcu and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (atomic_inc_not_zero(&eb->refs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * just get put onto a simple dirty list. Transaction walks this list to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * sure they get properly updated on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void add_root_to_dirty_list(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) spin_lock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* Want the extent tree to be the last on the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) list_move_tail(&root->dirty_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) &fs_info->dirty_cowonly_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) list_move(&root->dirty_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) &fs_info->dirty_cowonly_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) spin_unlock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * used by snapshot creation to make a copy of a root for a tree with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * a given objectid. The buffer with the new root node is returned in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * cow_ret, and this func returns zero on success or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int btrfs_copy_root(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct extent_buffer *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct extent_buffer **cow_ret, u64 new_root_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct extent_buffer *cow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) trans->transid != fs_info->running_transaction->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) trans->transid != root->last_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) level = btrfs_header_level(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) btrfs_item_key(buf, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) btrfs_node_key(buf, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) &disk_key, level, buf->start, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) BTRFS_NESTING_NEW_ROOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (IS_ERR(cow))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return PTR_ERR(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) copy_extent_buffer_full(cow, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) btrfs_set_header_bytenr(cow, cow->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) btrfs_set_header_generation(cow, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) BTRFS_HEADER_FLAG_RELOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) btrfs_set_header_owner(cow, new_root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) WARN_ON(btrfs_header_generation(buf) > trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ret = btrfs_inc_ref(trans, root, cow, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ret = btrfs_inc_ref(trans, root, cow, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) btrfs_tree_unlock(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) free_extent_buffer(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) btrfs_mark_buffer_dirty(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) *cow_ret = cow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) enum mod_log_op {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) MOD_LOG_KEY_REPLACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) MOD_LOG_KEY_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) MOD_LOG_KEY_REMOVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) MOD_LOG_KEY_REMOVE_WHILE_FREEING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) MOD_LOG_KEY_REMOVE_WHILE_MOVING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) MOD_LOG_MOVE_KEYS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) MOD_LOG_ROOT_REPLACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct tree_mod_root {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) u64 logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) u8 level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct tree_mod_elem {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct rb_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) u64 logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) enum mod_log_op op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) u64 generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct btrfs_disk_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) u64 blockptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* this is used for op == MOD_LOG_MOVE_KEYS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int dst_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int nr_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) } move;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* this is used for op == MOD_LOG_ROOT_REPLACE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct tree_mod_root old_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * Pull a new tree mod seq number for our operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return atomic64_inc_return(&fs_info->tree_mod_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * This adds a new blocker to the tree mod log's blocker list if the @elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * passed does not already have a sequence number set. So when a caller expects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * to record tree modifications, it should ensure to set elem->seq to zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * before calling btrfs_get_tree_mod_seq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * Returns a fresh, unused tree log modification sequence number, even if no new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * blocker was added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct seq_list *elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) write_lock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!elem->seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) elem->seq = btrfs_inc_tree_mod_seq(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) write_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return elem->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct seq_list *elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct rb_root *tm_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct rb_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct tree_mod_elem *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) u64 min_seq = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) u64 seq_putting = elem->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!seq_putting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) write_lock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) list_del(&elem->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) elem->seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!list_empty(&fs_info->tree_mod_seq_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct seq_list *first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) first = list_first_entry(&fs_info->tree_mod_seq_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct seq_list, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (seq_putting > first->seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * Blocker with lower sequence number exists, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * cannot remove anything from the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) write_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) min_seq = first->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * anything that's lower than the lowest existing (read: blocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * sequence number can be removed from the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) tm_root = &fs_info->tree_mod_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) for (node = rb_first(tm_root); node; node = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) next = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) tm = rb_entry(node, struct tree_mod_elem, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (tm->seq >= min_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) rb_erase(node, tm_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) kfree(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) write_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * key order of the log:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * node/leaf start address -> sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * The 'start address' is the logical address of the *new* root node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * for root replace operations, or the logical address of the affected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * block for all other operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static noinline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct rb_root *tm_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct rb_node **new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct tree_mod_elem *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) tm->seq = btrfs_inc_tree_mod_seq(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) tm_root = &fs_info->tree_mod_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) new = &tm_root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) while (*new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) cur = rb_entry(*new, struct tree_mod_elem, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) parent = *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (cur->logical < tm->logical)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) new = &((*new)->rb_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) else if (cur->logical > tm->logical)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) new = &((*new)->rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) else if (cur->seq < tm->seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) new = &((*new)->rb_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) else if (cur->seq > tm->seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) new = &((*new)->rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) rb_link_node(&tm->node, parent, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) rb_insert_color(&tm->node, tm_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * returns zero with the tree_mod_log_lock acquired. The caller must hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * this until all tree mod log insertions are recorded in the rb tree and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * write unlock fs_info::tree_mod_log_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct extent_buffer *eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (list_empty(&(fs_info)->tree_mod_seq_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (eb && btrfs_header_level(eb) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) write_lock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (list_empty(&(fs_info)->tree_mod_seq_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) write_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (list_empty(&(fs_info)->tree_mod_seq_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (eb && btrfs_header_level(eb) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static struct tree_mod_elem *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) enum mod_log_op op, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct tree_mod_elem *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) tm = kzalloc(sizeof(*tm), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (!tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) tm->logical = eb->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (op != MOD_LOG_KEY_ADD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) btrfs_node_key(eb, &tm->key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) tm->blockptr = btrfs_node_blockptr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) tm->op = op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) tm->slot = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) tm->generation = btrfs_node_ptr_generation(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) RB_CLEAR_NODE(&tm->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) enum mod_log_op op, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) struct tree_mod_elem *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (!tree_mod_need_log(eb->fs_info, eb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) tm = alloc_tree_mod_elem(eb, slot, op, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (!tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (tree_mod_dont_log(eb->fs_info, eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) kfree(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) ret = __tree_mod_log_insert(eb->fs_info, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) write_unlock(&eb->fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) kfree(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) int dst_slot, int src_slot, int nr_items)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct tree_mod_elem *tm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct tree_mod_elem **tm_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int locked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (!tree_mod_need_log(eb->fs_info, eb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (!tm_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) tm = kzalloc(sizeof(*tm), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!tm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) tm->logical = eb->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) tm->slot = src_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) tm->move.dst_slot = dst_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) tm->move.nr_items = nr_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) tm->op = MOD_LOG_MOVE_KEYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (!tm_list[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (tree_mod_dont_log(eb->fs_info, eb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) locked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * When we override something during the move, we log these removals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * This can only happen when we move towards the beginning of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * buffer, i.e. dst_slot < src_slot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ret = __tree_mod_log_insert(eb->fs_info, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) write_unlock(&eb->fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) free_tms:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) for (i = 0; i < nr_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) kfree(tm_list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) write_unlock(&eb->fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) kfree(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) struct tree_mod_elem **tm_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) int nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) for (i = nritems - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) ret = __tree_mod_log_insert(fs_info, tm_list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) for (j = nritems - 1; j > i; j--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) rb_erase(&tm_list[j]->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) &fs_info->tree_mod_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct extent_buffer *new_root, int log_removal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct btrfs_fs_info *fs_info = old_root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct tree_mod_elem *tm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct tree_mod_elem **tm_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) int nritems = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (!tree_mod_need_log(fs_info, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (log_removal && btrfs_header_level(old_root) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) nritems = btrfs_header_nritems(old_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (!tm_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) for (i = 0; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) tm_list[i] = alloc_tree_mod_elem(old_root, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (!tm_list[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) tm = kzalloc(sizeof(*tm), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (!tm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) tm->logical = new_root->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) tm->old_root.logical = old_root->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) tm->old_root.level = btrfs_header_level(old_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) tm->generation = btrfs_header_generation(old_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) tm->op = MOD_LOG_ROOT_REPLACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (tree_mod_dont_log(fs_info, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (tm_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) ret = __tree_mod_log_insert(fs_info, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) write_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) free_tms:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (tm_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) for (i = 0; i < nritems; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) kfree(tm_list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) kfree(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) static struct tree_mod_elem *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) int smallest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct rb_root *tm_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct tree_mod_elem *cur = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) struct tree_mod_elem *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) read_lock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) tm_root = &fs_info->tree_mod_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) node = tm_root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) cur = rb_entry(node, struct tree_mod_elem, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (cur->logical < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) } else if (cur->logical > start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) } else if (cur->seq < min_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) } else if (!smallest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) /* we want the node with the highest seq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) BUG_ON(found->seq > cur->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) found = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) } else if (cur->seq > min_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /* we want the node with the smallest seq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) BUG_ON(found->seq < cur->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) found = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) found = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) read_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * this returns the element from the log with the smallest time sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * value that's in the log (the oldest log item). any element with a time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * sequence lower than min_seq will be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) static struct tree_mod_elem *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) u64 min_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return __tree_mod_log_search(fs_info, start, min_seq, 1);
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * this returns the element from the log with the largest time sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * value that's in the log (the most recent log item). any element with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * a time sequence lower than min_seq will be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) static struct tree_mod_elem *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return __tree_mod_log_search(fs_info, start, min_seq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) struct extent_buffer *src, unsigned long dst_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) unsigned long src_offset, int nr_items)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) struct btrfs_fs_info *fs_info = dst->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) struct tree_mod_elem **tm_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct tree_mod_elem **tm_list_add, **tm_list_rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) int locked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (!tree_mod_need_log(fs_info, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (!tm_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) tm_list_add = tm_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) tm_list_rem = tm_list + nr_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) for (i = 0; i < nr_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) MOD_LOG_KEY_REMOVE, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (!tm_list_rem[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) MOD_LOG_KEY_ADD, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!tm_list_add[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) goto free_tms;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (tree_mod_dont_log(fs_info, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) locked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) for (i = 0; i < nr_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) write_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) free_tms:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) for (i = 0; i < nr_items * 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) kfree(tm_list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) write_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) struct tree_mod_elem **tm_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) int nritems = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (btrfs_header_level(eb) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (!tree_mod_need_log(eb->fs_info, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) nritems = btrfs_header_nritems(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (!tm_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) for (i = 0; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) tm_list[i] = alloc_tree_mod_elem(eb, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (!tm_list[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (tree_mod_dont_log(eb->fs_info, eb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) write_unlock(&eb->fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) goto free_tms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) free_tms:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) for (i = 0; i < nritems; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) kfree(tm_list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) kfree(tm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * check if the tree block can be shared by multiple trees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) int btrfs_block_can_be_shared(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) struct extent_buffer *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * Tree blocks not in shareable trees and tree roots are never shared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * If a block was allocated after the last snapshot and the block was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * not allocated by tree relocation, we know the block is not shared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) buf != root->node && buf != root->commit_root &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) (btrfs_header_generation(buf) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) btrfs_root_last_snapshot(&root->root_item) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) struct extent_buffer *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct extent_buffer *cow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) int *last_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) u64 refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) u64 owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) u64 new_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * Backrefs update rules:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * Always use full backrefs for extent pointers in tree block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * allocated by tree relocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) * If a shared tree block is no longer referenced by its owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * tree (btrfs_header_owner(buf) == root->root_key.objectid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * use full backrefs for extent pointers in tree block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) * If a tree block is been relocating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * use full backrefs for extent pointers in tree block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) * The reason for this is some operations (such as drop tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) * are only allowed for blocks use full backrefs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (btrfs_block_can_be_shared(root, buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) btrfs_header_level(buf), 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) &refs, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (refs == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) btrfs_handle_fs_error(fs_info, ret, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) refs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) owner = btrfs_header_owner(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (refs > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if ((owner == root->root_key.objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ret = btrfs_inc_ref(trans, root, buf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (root->root_key.objectid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) BTRFS_TREE_RELOC_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) ret = btrfs_dec_ref(trans, root, buf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) ret = btrfs_inc_ref(trans, root, cow, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) if (root->root_key.objectid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) BTRFS_TREE_RELOC_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) ret = btrfs_inc_ref(trans, root, cow, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) ret = btrfs_inc_ref(trans, root, cow, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (new_flags != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) int level = btrfs_header_level(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) ret = btrfs_set_disk_extent_flags(trans, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) new_flags, level, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (root->root_key.objectid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) BTRFS_TREE_RELOC_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) ret = btrfs_inc_ref(trans, root, cow, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) ret = btrfs_inc_ref(trans, root, cow, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) ret = btrfs_dec_ref(trans, root, buf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) btrfs_clean_tree_block(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) *last_ref = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) static struct extent_buffer *alloc_tree_block_no_bg_flush(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) u64 parent_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) const struct btrfs_disk_key *disk_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) u64 hint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) u64 empty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) enum btrfs_lock_nesting nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) struct extent_buffer *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * If we are COWing a node/leaf from the extent, chunk, device or free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * space trees, make sure that we do not finish block group creation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * pending block groups. We do this to avoid a deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * COWing can result in allocation of a new chunk, and flushing pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) * block groups (btrfs_create_pending_block_groups()) can be triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * when finishing allocation of a new chunk. Creation of a pending block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * group modifies the extent, chunk, device and free space trees,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) * therefore we could deadlock with ourselves since we are holding a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * lock on an extent buffer that btrfs_create_pending_block_groups() may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * try to COW later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * For similar reasons, we also need to delay flushing pending block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * groups when splitting a leaf or node, from one of those trees, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * we are holding a write lock on it and its parent or when inserting a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * new root node for one of those trees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (root == fs_info->extent_root ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) root == fs_info->chunk_root ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) root == fs_info->dev_root ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) root == fs_info->free_space_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) trans->can_flush_pending_bgs = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) ret = btrfs_alloc_tree_block(trans, root, parent_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) root->root_key.objectid, disk_key, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) hint, empty_size, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) trans->can_flush_pending_bgs = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) * does the dirty work in cow of a single block. The parent block (if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) * supplied) is updated to point to the new cow copy. The new buffer is marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * dirty and returned locked. If you modify the block it needs to be marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * dirty again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * search_start -- an allocation hint for the new block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * empty_size -- a hint that you plan on doing more cow. This is the size in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * bytes the allocator should try to find free next to the block it returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * This is just a hint and may be ignored by the allocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct extent_buffer *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) struct extent_buffer *parent, int parent_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) struct extent_buffer **cow_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) u64 search_start, u64 empty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) enum btrfs_lock_nesting nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) struct extent_buffer *cow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) int level, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) int last_ref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) int unlock_orig = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) u64 parent_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (*cow_ret == buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) unlock_orig = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) btrfs_assert_tree_locked(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) trans->transid != fs_info->running_transaction->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) trans->transid != root->last_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) level = btrfs_header_level(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) btrfs_item_key(buf, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) btrfs_node_key(buf, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) parent_start = parent->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) level, search_start, empty_size, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (IS_ERR(cow))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) return PTR_ERR(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) /* cow is set to blocking by btrfs_init_new_buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) copy_extent_buffer_full(cow, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) btrfs_set_header_bytenr(cow, cow->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) btrfs_set_header_generation(cow, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) BTRFS_HEADER_FLAG_RELOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) btrfs_set_header_owner(cow, root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) btrfs_tree_unlock(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) free_extent_buffer(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) ret = btrfs_reloc_cow_block(trans, root, buf, cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) btrfs_tree_unlock(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) free_extent_buffer(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (buf == root->node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) WARN_ON(parent && parent != buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) parent_start = buf->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) atomic_inc(&cow->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) ret = tree_mod_log_insert_root(root->node, cow, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) rcu_assign_pointer(root->node, cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) btrfs_free_tree_block(trans, root, buf, parent_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) last_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) free_extent_buffer(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) add_root_to_dirty_list(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) WARN_ON(trans->transid != btrfs_header_generation(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) tree_mod_log_insert_key(parent, parent_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) MOD_LOG_KEY_REPLACE, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) btrfs_set_node_blockptr(parent, parent_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) cow->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) btrfs_set_node_ptr_generation(parent, parent_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) btrfs_mark_buffer_dirty(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (last_ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) ret = tree_mod_log_free_eb(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) btrfs_tree_unlock(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) free_extent_buffer(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) btrfs_free_tree_block(trans, root, buf, parent_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) last_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) if (unlock_orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) btrfs_tree_unlock(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) free_extent_buffer_stale(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) btrfs_mark_buffer_dirty(cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) *cow_ret = cow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) return 0;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) * returns the logical address of the oldest predecessor of the given root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) * entries older than time_seq are ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static struct tree_mod_elem *__tree_mod_log_oldest_root(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) struct extent_buffer *eb_root, u64 time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) struct tree_mod_elem *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) struct tree_mod_elem *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) u64 root_logical = eb_root->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) int looped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) if (!time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * the very last operation that's logged for a root is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * replacement operation (if it is replaced at all). this has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) * the logical address of the *new* root, making it the very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * first operation that's logged for this root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) if (!looped && !tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) * if there are no tree operation for the oldest root, we simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) * return it. this should only happen if that (old) root is at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) * level 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) if (!tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * if there's an operation that's not a root replacement, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * found the oldest version of our root. normally, we'll find a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) if (tm->op != MOD_LOG_ROOT_REPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) found = tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) root_logical = tm->old_root.logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) looped = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) /* if there's no old root to return, return what we found instead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) found = tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * tm is a pointer to the first operation to rewind within eb. then, all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) * previous operations will be rewound (until we reach something older than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) * time_seq).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) u64 time_seq, struct tree_mod_elem *first_tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) u32 n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) struct rb_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct tree_mod_elem *tm = first_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) unsigned long o_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) unsigned long o_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) unsigned long p_size = sizeof(struct btrfs_key_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) n = btrfs_header_nritems(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) read_lock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) while (tm && tm->seq >= time_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * all the operations are recorded with the operator used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * the modification. as we're going backwards, we do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) * opposite of each operation here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) switch (tm->op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) BUG_ON(tm->slot < n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) case MOD_LOG_KEY_REMOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) btrfs_set_node_key(eb, &tm->key, tm->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) btrfs_set_node_ptr_generation(eb, tm->slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) tm->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) case MOD_LOG_KEY_REPLACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) BUG_ON(tm->slot >= n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) btrfs_set_node_key(eb, &tm->key, tm->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) btrfs_set_node_ptr_generation(eb, tm->slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) tm->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) case MOD_LOG_KEY_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) /* if a move operation is needed it's in the log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) n--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) case MOD_LOG_MOVE_KEYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) o_dst = btrfs_node_key_ptr_offset(tm->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) memmove_extent_buffer(eb, o_dst, o_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) tm->move.nr_items * p_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) case MOD_LOG_ROOT_REPLACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) * this operation is special. for roots, this must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) * handled explicitly before rewinding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * for non-roots, this operation may exist if the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * was a root: root A -> child B; then A gets empty and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) * B is promoted to the new root. in the mod log, we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) * have a root-replace operation for B, a tree block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) * that is no root. we simply ignore that operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) next = rb_next(&tm->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) if (!next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) tm = rb_entry(next, struct tree_mod_elem, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) if (tm->logical != first_tm->logical)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) read_unlock(&fs_info->tree_mod_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) btrfs_set_header_nritems(eb, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) * Called with eb read locked. If the buffer cannot be rewound, the same buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) * is returned. If rewind operations happen, a fresh buffer is returned. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) * returned buffer is always read-locked. If the returned buffer is not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) * input buffer, the lock on the input buffer is released and the input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) * is freed (its refcount is decremented).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) static struct extent_buffer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) struct extent_buffer *eb, u64 time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) struct extent_buffer *eb_rewin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) struct tree_mod_elem *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) if (!time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) if (btrfs_header_level(eb) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) tm = tree_mod_log_search(fs_info, eb->start, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) if (!tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) btrfs_set_lock_blocking_read(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) BUG_ON(tm->slot != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) if (!eb_rewin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) btrfs_tree_read_unlock_blocking(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) btrfs_set_header_bytenr(eb_rewin, eb->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) btrfs_set_header_backref_rev(eb_rewin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) btrfs_header_backref_rev(eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) eb_rewin = btrfs_clone_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) if (!eb_rewin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) btrfs_tree_read_unlock_blocking(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) btrfs_tree_read_unlock_blocking(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) eb_rewin, btrfs_header_level(eb_rewin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) btrfs_tree_read_lock(eb_rewin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) WARN_ON(btrfs_header_nritems(eb_rewin) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) BTRFS_NODEPTRS_PER_BLOCK(fs_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) return eb_rewin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) * get_old_root() rewinds the state of @root's root node to the given @time_seq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) * value. If there are no changes, the current root->root_node is returned. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) * anything changed in between, there's a fresh buffer allocated on which the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) * rewind operations are done. In any case, the returned buffer is read locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) * Returns NULL on error (with no locks held).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) static inline struct extent_buffer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) get_old_root(struct btrfs_root *root, u64 time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) struct tree_mod_elem *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) struct extent_buffer *eb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct extent_buffer *eb_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) u64 eb_root_owner = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) struct extent_buffer *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) struct tree_mod_root *old_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) u64 old_generation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) u64 logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) eb_root = btrfs_read_lock_root_node(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) tm = __tree_mod_log_oldest_root(eb_root, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) if (!tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) return eb_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) if (tm->op == MOD_LOG_ROOT_REPLACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) old_root = &tm->old_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) old_generation = tm->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) logical = old_root->logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) level = old_root->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) logical = eb_root->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) level = btrfs_header_level(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) tm = tree_mod_log_search(fs_info, logical, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) btrfs_tree_read_unlock(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) free_extent_buffer(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) old = read_tree_block(fs_info, logical, 0, level, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) if (!IS_ERR(old))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) free_extent_buffer(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) "failed to read tree block %llu from get_old_root",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) logical);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) struct tree_mod_elem *tm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) btrfs_tree_read_lock(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) eb = btrfs_clone_extent_buffer(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) * After the lookup for the most recent tree mod operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) * above and before we locked and cloned the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) * 'old', a new tree mod log operation may have been added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) * So lookup for a more recent one to make sure the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) * of mod log operations we replay is consistent with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) * number of items we have in the cloned extent buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) * otherwise we can hit a BUG_ON when rewinding the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) * buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) tm2 = tree_mod_log_search(fs_info, logical, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) btrfs_tree_read_unlock(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) free_extent_buffer(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) ASSERT(tm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) ASSERT(tm2 == tm || tm2->seq > tm->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) if (!tm2 || tm2->seq < tm->seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) tm = tm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) } else if (old_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) eb_root_owner = btrfs_header_owner(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) btrfs_tree_read_unlock(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) free_extent_buffer(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) eb = alloc_dummy_extent_buffer(fs_info, logical);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) btrfs_set_lock_blocking_read(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) eb = btrfs_clone_extent_buffer(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) btrfs_tree_read_unlock_blocking(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) free_extent_buffer(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (!eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) if (old_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) btrfs_set_header_bytenr(eb, eb->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) btrfs_set_header_owner(eb, eb_root_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) btrfs_set_header_level(eb, old_root->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) btrfs_set_header_generation(eb, old_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) btrfs_header_level(eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) btrfs_tree_read_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) if (tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) WARN_ON(btrfs_header_level(eb) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) struct tree_mod_elem *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) struct extent_buffer *eb_root = btrfs_root_node(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) tm = __tree_mod_log_oldest_root(eb_root, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) level = tm->old_root.level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) level = btrfs_header_level(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) free_extent_buffer(eb_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) static inline int should_cow_block(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) struct extent_buffer *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) if (btrfs_is_testing(root->fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) /* Ensure we can see the FORCE_COW bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) smp_mb__before_atomic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) * We do not need to cow a block if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) * 1) this block is not created or changed in this transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) * 2) this block does not belong to TREE_RELOC tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) * 3) the root is not forced COW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) * What is forced COW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) * when we create snapshot during committing the transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) * after we've finished copying src root, we must COW the shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) * block to ensure the metadata consistency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) if (btrfs_header_generation(buf) == trans->transid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) * cows a single block, see __btrfs_cow_block for the real work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) * This version of it has extra checks so that a block isn't COWed more than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) * once per transaction, as long as it hasn't been written yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) struct btrfs_root *root, struct extent_buffer *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) struct extent_buffer *parent, int parent_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) struct extent_buffer **cow_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) enum btrfs_lock_nesting nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) u64 search_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) if (test_bit(BTRFS_ROOT_DELETING, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) "COW'ing blocks on a fs root that's being dropped");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) if (trans->transaction != fs_info->running_transaction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) WARN(1, KERN_CRIT "trans %llu running %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) trans->transid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) fs_info->running_transaction->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) if (trans->transid != fs_info->generation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) WARN(1, KERN_CRIT "trans %llu running %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) trans->transid, fs_info->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) if (!should_cow_block(trans, root, buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) trans->dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) *cow_ret = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) search_start = buf->start & ~((u64)SZ_1G - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) btrfs_set_lock_blocking_write(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) btrfs_set_lock_blocking_write(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) * Before CoWing this block for later modification, check if it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) * the subtree root and do the delayed subtree trace if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) * Also We don't care about the error, as it's handled internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) ret = __btrfs_cow_block(trans, root, buf, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) parent_slot, cow_ret, search_start, 0, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) trace_btrfs_cow_block(root, buf, *cow_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) * helper function for defrag to decide if two blocks pointed to by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) * node are actually close by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) if (blocknr < other && other - (blocknr + blocksize) < 32768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) if (blocknr > other && blocknr - (other + blocksize) < 32768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) #ifdef __LITTLE_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) * Compare two keys, on little-endian the disk order is same as CPU order and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) * we can avoid the conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) static int comp_keys(const struct btrfs_disk_key *disk_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) const struct btrfs_key *k2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) return btrfs_comp_cpu_keys(k1, k2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) * compare two keys in a memcmp fashion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) static int comp_keys(const struct btrfs_disk_key *disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) const struct btrfs_key *k2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) struct btrfs_key k1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) btrfs_disk_key_to_cpu(&k1, disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) return btrfs_comp_cpu_keys(&k1, k2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) * same as comp_keys only with two btrfs_key's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) if (k1->objectid > k2->objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) if (k1->objectid < k2->objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) if (k1->type > k2->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) if (k1->type < k2->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) if (k1->offset > k2->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) if (k1->offset < k2->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) * this is used by the defrag code to go through all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) * leaves pointed to by a node and reallocate them so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) * disk order is close to key order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) int btrfs_realloc_node(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) struct btrfs_root *root, struct extent_buffer *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) int start_slot, u64 *last_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) struct btrfs_key *progress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) struct extent_buffer *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) u64 blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) u64 gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) u64 search_start = *last_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) u64 last_block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) u64 other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) u32 parent_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) int end_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) int parent_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) int uptodate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) u32 blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) int progress_passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) parent_level = btrfs_header_level(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) WARN_ON(trans->transaction != fs_info->running_transaction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) WARN_ON(trans->transid != fs_info->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) parent_nritems = btrfs_header_nritems(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) blocksize = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) end_slot = parent_nritems - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) if (parent_nritems <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) btrfs_set_lock_blocking_write(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) for (i = start_slot; i <= end_slot; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) int close = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) btrfs_node_key(parent, &disk_key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) if (!progress_passed && comp_keys(&disk_key, progress) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) progress_passed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) blocknr = btrfs_node_blockptr(parent, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) gen = btrfs_node_ptr_generation(parent, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) btrfs_node_key_to_cpu(parent, &first_key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) if (last_block == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) last_block = blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) if (i > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) other = btrfs_node_blockptr(parent, i - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) close = close_blocks(blocknr, other, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) if (!close && i < end_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) other = btrfs_node_blockptr(parent, i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) close = close_blocks(blocknr, other, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) if (close) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) last_block = blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) cur = find_extent_buffer(fs_info, blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) if (cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) uptodate = btrfs_buffer_uptodate(cur, gen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) uptodate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) if (!cur || !uptodate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) if (!cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) cur = read_tree_block(fs_info, blocknr, gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) parent_level - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) if (IS_ERR(cur)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) return PTR_ERR(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) } else if (!extent_buffer_uptodate(cur)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) free_extent_buffer(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) } else if (!uptodate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) err = btrfs_read_buffer(cur, gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) parent_level - 1,&first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) free_extent_buffer(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (search_start == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) search_start = last_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) btrfs_tree_lock(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) btrfs_set_lock_blocking_write(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) err = __btrfs_cow_block(trans, root, cur, parent, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) &cur, search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) min(16 * blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) (end_slot - i) * blocksize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) BTRFS_NESTING_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) btrfs_tree_unlock(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) free_extent_buffer(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) search_start = cur->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) last_block = cur->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) *last_ret = search_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) btrfs_tree_unlock(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) free_extent_buffer(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) * search for key in the extent_buffer. The items start at offset p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) * and they are item_size apart. There are 'max' items in p.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) * the slot in the array is returned via slot, and it points to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) * the place where you would insert key if it is not found in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) * the array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) * slot may point to max if the key is bigger than all of the keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) static noinline int generic_bin_search(struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) unsigned long p, int item_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) const struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) int max, int *slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) int low = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) int high = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) const int key_size = sizeof(struct btrfs_disk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) if (low > high) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) btrfs_err(eb->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) __func__, low, high, eb->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) btrfs_header_owner(eb), btrfs_header_level(eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) while (low < high) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) unsigned long oip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) struct btrfs_disk_key *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) struct btrfs_disk_key unaligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) int mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) mid = (low + high) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) offset = p + mid * item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) oip = offset_in_page(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) if (oip + key_size <= PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) const unsigned long idx = offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) char *kaddr = page_address(eb->pages[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) tmp = (struct btrfs_disk_key *)(kaddr + oip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) read_extent_buffer(eb, &unaligned, offset, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) tmp = &unaligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) ret = comp_keys(tmp, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) low = mid + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) high = mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) *slot = mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) *slot = low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) * simple bin_search frontend that does the right thing for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) * leaves vs nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) int *slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) if (btrfs_header_level(eb) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) return generic_bin_search(eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) offsetof(struct btrfs_leaf, items),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) sizeof(struct btrfs_item),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) key, btrfs_header_nritems(eb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) return generic_bin_search(eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) offsetof(struct btrfs_node, ptrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) sizeof(struct btrfs_key_ptr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) key, btrfs_header_nritems(eb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) static void root_add_used(struct btrfs_root *root, u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) spin_lock(&root->accounting_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) btrfs_set_root_used(&root->root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) btrfs_root_used(&root->root_item) + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) spin_unlock(&root->accounting_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) static void root_sub_used(struct btrfs_root *root, u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) spin_lock(&root->accounting_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) btrfs_set_root_used(&root->root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) btrfs_root_used(&root->root_item) - size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) spin_unlock(&root->accounting_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) /* given a node and slot number, this reads the blocks it points to. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) * extent buffer is returned with a reference taken (but unlocked).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) int slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) int level = btrfs_header_level(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) if (slot < 0 || slot >= btrfs_header_nritems(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) BUG_ON(level == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) btrfs_node_key_to_cpu(parent, &first_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) btrfs_node_ptr_generation(parent, slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) level - 1, &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) eb = ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) }
^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) * node level balancing, used to make sure nodes are in proper order for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) * item deletion. We balance from the top down, so we have to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) * that a deletion won't leave an node completely empty later on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) static noinline int balance_level(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) struct btrfs_path *path, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) struct extent_buffer *right = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) struct extent_buffer *mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) struct extent_buffer *left = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) struct extent_buffer *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) int wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) int pslot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) int orig_slot = path->slots[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) u64 orig_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) ASSERT(level > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) mid = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) WARN_ON(btrfs_header_generation(mid) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) orig_ptr = btrfs_node_blockptr(mid, orig_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) if (level < BTRFS_MAX_LEVEL - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) parent = path->nodes[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) pslot = path->slots[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) * deal with the case where there is only one pointer in the root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) * by promoting the node below to a root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) if (!parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) struct extent_buffer *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (btrfs_header_nritems(mid) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) /* promote the child to a root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) child = btrfs_read_node_slot(mid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if (IS_ERR(child)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) ret = PTR_ERR(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) btrfs_handle_fs_error(fs_info, ret, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) goto enospc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) btrfs_tree_lock(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) btrfs_set_lock_blocking_write(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) BTRFS_NESTING_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) btrfs_tree_unlock(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) free_extent_buffer(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) goto enospc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) ret = tree_mod_log_insert_root(root->node, child, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) rcu_assign_pointer(root->node, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) add_root_to_dirty_list(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) btrfs_tree_unlock(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) path->locks[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) path->nodes[level] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) btrfs_clean_tree_block(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) btrfs_tree_unlock(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) /* once for the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) free_extent_buffer(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) root_sub_used(root, mid->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) btrfs_free_tree_block(trans, root, mid, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) /* once for the root ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) free_extent_buffer_stale(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) if (btrfs_header_nritems(mid) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) left = btrfs_read_node_slot(parent, pslot - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) if (IS_ERR(left))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) left = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) if (left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) btrfs_set_lock_blocking_write(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) wret = btrfs_cow_block(trans, root, left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) parent, pslot - 1, &left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) BTRFS_NESTING_LEFT_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) if (wret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) goto enospc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) right = btrfs_read_node_slot(parent, pslot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) if (IS_ERR(right))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) right = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) if (right) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) btrfs_set_lock_blocking_write(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) wret = btrfs_cow_block(trans, root, right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) parent, pslot + 1, &right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) BTRFS_NESTING_RIGHT_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) if (wret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) goto enospc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) /* first, try to make some room in the middle buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) if (left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) orig_slot += btrfs_header_nritems(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) wret = push_node_left(trans, left, mid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) if (wret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) * then try to empty the right most buffer into the middle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) if (right) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) wret = push_node_left(trans, mid, right, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) if (wret < 0 && wret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) if (btrfs_header_nritems(right) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) btrfs_clean_tree_block(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) del_ptr(root, path, level + 1, pslot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) root_sub_used(root, right->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) btrfs_free_tree_block(trans, root, right, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) free_extent_buffer_stale(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) right = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) struct btrfs_disk_key right_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) btrfs_node_key(right, &right_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) ret = tree_mod_log_insert_key(parent, pslot + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) MOD_LOG_KEY_REPLACE, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) btrfs_set_node_key(parent, &right_key, pslot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) btrfs_mark_buffer_dirty(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) if (btrfs_header_nritems(mid) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) * we're not allowed to leave a node with one item in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) * tree during a delete. A deletion from lower in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) * could try to delete the only pointer in this node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) * So, pull some keys from the left.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) * There has to be a left pointer at this point because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) * otherwise we would have pulled some pointers from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) * right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) if (!left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) btrfs_handle_fs_error(fs_info, ret, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) goto enospc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) wret = balance_node_right(trans, mid, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) if (wret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) goto enospc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) if (wret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) wret = push_node_left(trans, left, mid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) if (wret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) BUG_ON(wret == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) if (btrfs_header_nritems(mid) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) btrfs_clean_tree_block(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) btrfs_tree_unlock(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) del_ptr(root, path, level + 1, pslot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) root_sub_used(root, mid->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) btrfs_free_tree_block(trans, root, mid, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) free_extent_buffer_stale(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) mid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) /* update the parent key to reflect our changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) struct btrfs_disk_key mid_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) btrfs_node_key(mid, &mid_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) ret = tree_mod_log_insert_key(parent, pslot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) MOD_LOG_KEY_REPLACE, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) btrfs_set_node_key(parent, &mid_key, pslot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) btrfs_mark_buffer_dirty(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) /* update the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) if (left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) if (btrfs_header_nritems(left) > orig_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) atomic_inc(&left->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) /* left was locked after cow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) path->nodes[level] = left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) path->slots[level + 1] -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) path->slots[level] = orig_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) if (mid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) btrfs_tree_unlock(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) free_extent_buffer(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) orig_slot -= btrfs_header_nritems(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) path->slots[level] = orig_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) /* double check we haven't messed things up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) if (orig_ptr !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) btrfs_node_blockptr(path->nodes[level], path->slots[level]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) enospc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) if (right) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) if (left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) if (path->nodes[level] != left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) btrfs_tree_unlock(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) free_extent_buffer(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) /* Node balancing for insertion. Here we only split or push nodes around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) * when they are completely full. This is also done top down, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) * have to be pessimistic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) struct btrfs_path *path, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) struct extent_buffer *right = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) struct extent_buffer *mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) struct extent_buffer *left = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) struct extent_buffer *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) int wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) int pslot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) int orig_slot = path->slots[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) mid = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) WARN_ON(btrfs_header_generation(mid) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) if (level < BTRFS_MAX_LEVEL - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) parent = path->nodes[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) pslot = path->slots[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) left = btrfs_read_node_slot(parent, pslot - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) if (IS_ERR(left))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) left = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) /* first, try to make some room in the middle buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) if (left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) u32 left_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) btrfs_set_lock_blocking_write(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) left_nr = btrfs_header_nritems(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) wret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) ret = btrfs_cow_block(trans, root, left, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) pslot - 1, &left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) BTRFS_NESTING_LEFT_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) wret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) wret = push_node_left(trans, left, mid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) if (wret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) if (wret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) orig_slot += left_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) btrfs_node_key(mid, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) ret = tree_mod_log_insert_key(parent, pslot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) MOD_LOG_KEY_REPLACE, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) btrfs_set_node_key(parent, &disk_key, pslot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) btrfs_mark_buffer_dirty(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) if (btrfs_header_nritems(left) > orig_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) path->nodes[level] = left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) path->slots[level + 1] -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) path->slots[level] = orig_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) btrfs_tree_unlock(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) free_extent_buffer(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) orig_slot -=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) btrfs_header_nritems(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) path->slots[level] = orig_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) btrfs_tree_unlock(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) free_extent_buffer(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) btrfs_tree_unlock(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) free_extent_buffer(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) right = btrfs_read_node_slot(parent, pslot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) if (IS_ERR(right))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) right = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) * then try to empty the right most buffer into the middle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) if (right) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) u32 right_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) btrfs_set_lock_blocking_write(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) right_nr = btrfs_header_nritems(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) wret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) ret = btrfs_cow_block(trans, root, right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) parent, pslot + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) &right, BTRFS_NESTING_RIGHT_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) wret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) wret = balance_node_right(trans, right, mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) if (wret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) if (wret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) btrfs_node_key(right, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) ret = tree_mod_log_insert_key(parent, pslot + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) MOD_LOG_KEY_REPLACE, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) btrfs_set_node_key(parent, &disk_key, pslot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) btrfs_mark_buffer_dirty(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) if (btrfs_header_nritems(mid) <= orig_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) path->nodes[level] = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) path->slots[level + 1] += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) path->slots[level] = orig_slot -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) btrfs_header_nritems(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) btrfs_tree_unlock(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) free_extent_buffer(mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) * readahead one full node of leaves, finding things that are close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) * to the block in 'slot', and triggering ra on them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) static void reada_for_search(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) int level, int slot, u64 objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) struct extent_buffer *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) u64 search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) u64 target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) u64 nread = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) u32 nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) u32 blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) u32 nscan = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) if (level != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) if (!path->nodes[level])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) node = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) search = btrfs_node_blockptr(node, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) blocksize = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) eb = find_extent_buffer(fs_info, search);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) if (eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) target = search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) nritems = btrfs_header_nritems(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) nr = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) if (path->reada == READA_BACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) if (nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) } else if (path->reada == READA_FORWARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) if (nr >= nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) if (path->reada == READA_BACK && objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) btrfs_node_key(node, &disk_key, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) if (btrfs_disk_key_objectid(&disk_key) != objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) search = btrfs_node_blockptr(node, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) if ((search <= target && target - search <= 65536) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) (search > target && search - target <= 65536)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) readahead_tree_block(fs_info, search);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) nread += blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) nscan++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) if ((nread > 65536 || nscan > 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) struct btrfs_path *path, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) struct extent_buffer *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) u64 gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) u64 block1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) u64 block2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) parent = path->nodes[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) nritems = btrfs_header_nritems(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) slot = path->slots[level + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) if (slot > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) block1 = btrfs_node_blockptr(parent, slot - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) gen = btrfs_node_ptr_generation(parent, slot - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) eb = find_extent_buffer(fs_info, block1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) * if we get -eagain from btrfs_buffer_uptodate, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) * don't want to return eagain here. That will loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) * forever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) block1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) if (slot + 1 < nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) block2 = btrfs_node_blockptr(parent, slot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) gen = btrfs_node_ptr_generation(parent, slot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) eb = find_extent_buffer(fs_info, block2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) block2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) if (block1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) readahead_tree_block(fs_info, block1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) if (block2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) readahead_tree_block(fs_info, block2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) }
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) * when we walk down the tree, it is usually safe to unlock the higher layers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) * in the tree. The exceptions are when our path goes through slot 0, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) * operations on the tree might require changing key pointers higher up in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) * tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) * callers might also have set path->keep_locks, which tells this code to keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) * the lock if the path points to the last slot in the block. This is part of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) * walking through the tree, and selecting the next slot in the higher block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) * if lowest_unlock is 1, level 0 won't be unlocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) static noinline void unlock_up(struct btrfs_path *path, int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) int lowest_unlock, int min_write_lock_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) int *write_lock_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) int skip_level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) int no_skips = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) struct extent_buffer *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) for (i = level; i < BTRFS_MAX_LEVEL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) if (!path->nodes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) if (!path->locks[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) if (!no_skips && path->slots[i] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) skip_level = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) if (!no_skips && path->keep_locks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) t = path->nodes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) nritems = btrfs_header_nritems(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) if (nritems < 1 || path->slots[i] >= nritems - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) skip_level = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) if (skip_level < i && i >= lowest_unlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) no_skips = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) t = path->nodes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) if (i >= lowest_unlock && i > skip_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) btrfs_tree_unlock_rw(t, path->locks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) path->locks[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) if (write_lock_level &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) i > min_write_lock_level &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) i <= *write_lock_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) *write_lock_level = i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) }
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) * helper function for btrfs_search_slot. The goal is to find a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) * in cache without setting the path to blocking. If we find the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) * we return zero and the path is unchanged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) * If we can't find the block, we set the path blocking and do some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) * reada. -EAGAIN is returned and the search must be repeated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) struct extent_buffer **eb_ret, int level, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) const struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) u64 blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) u64 gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) struct extent_buffer *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) int parent_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) blocknr = btrfs_node_blockptr(*eb_ret, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) gen = btrfs_node_ptr_generation(*eb_ret, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) parent_level = btrfs_header_level(*eb_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) tmp = find_extent_buffer(fs_info, blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) /* first we do an atomic uptodate check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) * Do extra check for first_key, eb can be stale due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) * being cached, read from scrub, or have multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) * parents (shared tree blocks).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) if (btrfs_verify_level_key(tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) parent_level - 1, &first_key, gen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) free_extent_buffer(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) *eb_ret = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) /* the pages were up to date, but we failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) * the generation number check. Do a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) * read for the generation number that is correct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) * We must do this without dropping locks so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) * we can trust our generation number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) /* now we're allowed to do a blocking uptodate check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) *eb_ret = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) free_extent_buffer(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) * reduce lock contention at high levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) * of the btree by dropping locks before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) * we read. Don't release the lock on the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) * level because we need to walk this node to figure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) * out which blocks to read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) btrfs_unlock_up_safe(p, level + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) if (p->reada != READA_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) reada_for_search(fs_info, p, level, slot, key->objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) if (!IS_ERR(tmp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) * If the read above didn't mark this buffer up to date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) * it will never end up being up to date. Set ret to EIO now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) * and give up so that our caller doesn't loop forever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) * on our EAGAINs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) if (!extent_buffer_uptodate(tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) free_extent_buffer(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) ret = PTR_ERR(tmp);
^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) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) * helper function for btrfs_search_slot. This does all of the checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) * for node-level blocks and does any balancing required based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) * the ins_len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) * If no extra work was required, zero is returned. If we had to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) * drop the path, -EAGAIN is returned and btrfs_search_slot must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) * start over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) setup_nodes_for_search(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) struct btrfs_root *root, struct btrfs_path *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) struct extent_buffer *b, int level, int ins_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) int *write_lock_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) int sret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) if (*write_lock_level < level + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) *write_lock_level = level + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) reada_for_balance(fs_info, p, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) sret = split_node(trans, root, p, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) BUG_ON(sret > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) if (sret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) ret = sret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) b = p->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) } else if (ins_len < 0 && btrfs_header_nritems(b) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) int sret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) if (*write_lock_level < level + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) *write_lock_level = level + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) reada_for_balance(fs_info, p, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) sret = balance_level(trans, root, p, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) if (sret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) ret = sret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) b = p->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) if (!b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) BUG_ON(btrfs_header_nritems(b) == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) u64 iobjectid, u64 ioff, u8 key_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) struct btrfs_key *found_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) ASSERT(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) ASSERT(found_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) key.type = key_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) key.objectid = iobjectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) key.offset = ioff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) ret = btrfs_next_leaf(fs_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) if (found_key->type != key.type ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) found_key->objectid != key.objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) struct btrfs_path *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) int write_lock_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) struct extent_buffer *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) int root_lock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) int level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) if (p->search_commit_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) * The commit roots are read only so we always do read locks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) * and we always must hold the commit_root_sem when doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) * searches on them, the only exception is send where we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) * want to block transaction commits for a long time, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) * we need to clone the commit root in order to avoid races
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) * with transaction commits that create a snapshot of one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) * the roots used by a send operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) if (p->need_commit_sem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) down_read(&fs_info->commit_root_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) b = btrfs_clone_extent_buffer(root->commit_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) up_read(&fs_info->commit_root_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) if (!b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) b = root->commit_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) atomic_inc(&b->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) * Ensure that all callers have set skip_locking when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) * p->search_commit_root = 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) ASSERT(p->skip_locking == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) if (p->skip_locking) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) b = btrfs_root_node(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) /* We try very hard to do read locks on the root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) root_lock = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) * If the level is set to maximum, we can skip trying to get the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) * lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) if (write_lock_level < BTRFS_MAX_LEVEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) * We don't know the level of the root node until we actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) * have it read locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) b = __btrfs_read_lock_root_node(root, p->recurse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) if (level > write_lock_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) /* Whoops, must trade for write lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) btrfs_tree_read_unlock(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) free_extent_buffer(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) b = btrfs_lock_root_node(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) root_lock = BTRFS_WRITE_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) /* The level might have changed, check again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) * The root may have failed to write out at some point, and thus is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) * longer valid, return an error in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) if (!extent_buffer_uptodate(b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) if (root_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) btrfs_tree_unlock_rw(b, root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) free_extent_buffer(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) p->nodes[level] = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) if (!p->skip_locking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) p->locks[level] = root_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) * Callers are responsible for dropping b's references.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) return b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680)
^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) * btrfs_search_slot - look for a key in a tree and perform necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) * modifications to preserve tree invariants.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) * @trans: Handle of transaction, used when modifying the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) * @p: Holds all btree nodes along the search path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) * @root: The root node of the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) * @key: The key we are looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) * @ins_len: Indicates purpose of search, for inserts it is 1, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) * deletions it's -1. 0 for plain searches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) * @cow: boolean should CoW operations be performed. Must always be 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) * when modifying the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) * If @key is found, 0 is returned and you can find the item in the leaf level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) * of the path (level 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) * points to the slot where it should be inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) * If an error is encountered while searching the tree a negative error number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) * is returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) const struct btrfs_key *key, struct btrfs_path *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) int ins_len, int cow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) struct extent_buffer *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) int lowest_unlock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) /* everything at write_lock_level or lower must be write locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) int write_lock_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) u8 lowest_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) int min_write_lock_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) int prev_cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) lowest_level = p->lowest_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) WARN_ON(lowest_level && ins_len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) WARN_ON(p->nodes[0] != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) BUG_ON(!cow && ins_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) if (ins_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) lowest_unlock = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) /* when we are removing items, we might have to go up to level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) * two as we update tree pointers Make sure we keep write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) * for those levels as well
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) write_lock_level = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) } else if (ins_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) * for inserting items, make sure we have a write lock on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) * level 1 so we can update keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) write_lock_level = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) if (!cow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) write_lock_level = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) if (cow && (p->keep_locks || p->lowest_level))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) write_lock_level = BTRFS_MAX_LEVEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) min_write_lock_level = write_lock_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) prev_cmp = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) b = btrfs_search_slot_get_root(root, p, write_lock_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) if (IS_ERR(b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) ret = PTR_ERR(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) while (b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) int dec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) if (cow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) * if we don't really need to cow this block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) * then we don't want to set the path blocking,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) * so we test it here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) if (!should_cow_block(trans, root, b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) trans->dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) goto cow_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) * must have write locks on this node and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) * parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) if (level > write_lock_level ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) (level + 1 > write_lock_level &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) level + 1 < BTRFS_MAX_LEVEL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) p->nodes[level + 1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) write_lock_level = level + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) if (last_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) err = btrfs_cow_block(trans, root, b, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) &b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) BTRFS_NESTING_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) err = btrfs_cow_block(trans, root, b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) p->nodes[level + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) p->slots[level + 1], &b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) BTRFS_NESTING_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) cow_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) p->nodes[level] = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) * Leave path with blocking locks to avoid massive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) * lock context switch, this is made on purpose.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) * we have a lock on b and as long as we aren't changing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) * the tree, there is no way to for the items in b to change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) * It is safe to drop the lock on our parent before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) * go through the expensive btree search on b.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) * If we're inserting or deleting (ins_len != 0), then we might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) * be changing slot zero, which may require changing the parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) * So, we can't drop the lock until after we know which slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) * we're operating on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) if (!ins_len && !p->keep_locks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) int u = level + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) p->locks[u] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) * If btrfs_bin_search returns an exact match (prev_cmp == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) * we can safely assume the target key will always be in slot 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) * on lower levels due to the invariants BTRFS' btree provides,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) * namely that a btrfs_key_ptr entry always points to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) * lowest key in the child node, thus we can skip searching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) * lower levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) if (prev_cmp == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) ret = btrfs_bin_search(b, key, &slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) prev_cmp = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) if (level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) p->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) if (ins_len > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) btrfs_leaf_free_space(b) < ins_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) if (write_lock_level < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) write_lock_level = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) err = split_leaf(trans, root, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) p, ins_len, ret == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) BUG_ON(err > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) if (!p->search_for_split)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) unlock_up(p, level, lowest_unlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) min_write_lock_level, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) if (ret && slot > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) dec = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) slot--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) p->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) &write_lock_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) if (err == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) b = p->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) slot = p->slots[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) * Slot 0 is special, if we change the key we have to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) * the parent pointer which means we must have a write lock on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) * the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) if (slot == 0 && ins_len && write_lock_level < level + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) write_lock_level = level + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) unlock_up(p, level, lowest_unlock, min_write_lock_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) &write_lock_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) if (level == lowest_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) if (dec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) p->slots[level]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) err = read_block_for_search(root, p, &b, level, slot, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) if (err == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) if (!p->skip_locking) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) if (level <= write_lock_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) if (!btrfs_try_tree_write_lock(b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) btrfs_tree_lock(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) p->locks[level] = BTRFS_WRITE_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) if (!btrfs_tree_read_lock_atomic(b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) __btrfs_tree_read_lock(b, BTRFS_NESTING_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) p->recurse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) p->locks[level] = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) p->nodes[level] = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) * we don't really know what they plan on doing with the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) * from here on, so for now just mark it as blocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) if (!p->leave_spinning)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) if (ret < 0 && !p->skip_release_on_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) * current state of the tree together with the operations recorded in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) * modification log to search for the key in a previous version of this tree, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) * denoted by the time_seq parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) * Naturally, there is no support for insert, delete or cow operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) * The resulting path and return value will be set up as if we called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) struct btrfs_path *p, u64 time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) struct extent_buffer *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) int lowest_unlock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) u8 lowest_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) lowest_level = p->lowest_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) WARN_ON(p->nodes[0] != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) if (p->search_commit_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) BUG_ON(time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) return btrfs_search_slot(NULL, root, key, p, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) b = get_old_root(root, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) if (!b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) p->locks[level] = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) while (b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) int dec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) p->nodes[level] = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) * we have a lock on b and as long as we aren't changing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) * the tree, there is no way to for the items in b to change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) * It is safe to drop the lock on our parent before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) * go through the expensive btree search on b.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) btrfs_unlock_up_safe(p, level + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) ret = btrfs_bin_search(b, key, &slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) if (level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) p->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) unlock_up(p, level, lowest_unlock, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) if (ret && slot > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) dec = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) slot--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) p->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) unlock_up(p, level, lowest_unlock, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) if (level == lowest_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) if (dec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) p->slots[level]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) err = read_block_for_search(root, p, &b, level, slot, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) if (err == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) level = btrfs_header_level(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) if (!btrfs_tree_read_lock_atomic(b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) btrfs_tree_read_lock(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) b = tree_mod_log_rewind(fs_info, p, b, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) if (!b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) p->locks[level] = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) p->nodes[level] = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) if (!p->leave_spinning)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) btrfs_set_path_blocking(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) * helper to use instead of search slot if no exact match is needed but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) * instead the next or previous item should be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) * When find_higher is true, the next higher item is returned, the next lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) * When return_any and find_higher are both true, and no higher item is found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) * return the next lower instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) * When return_any is true and find_higher is false, and no lower item is found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) * return the next higher instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) * It returns 0 if any item is found, 1 if none is found (tree empty), and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) * < 0 on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) int btrfs_search_slot_for_read(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) const struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) struct btrfs_path *p, int find_higher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) int return_any)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) * a return value of 1 means the path is at the position where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) * item should be inserted. Normally this is the next bigger item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) * but in case the previous item is the last in a leaf, path points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) * to the first free slot in the previous leaf, i.e. at an invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) * item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) leaf = p->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) if (find_higher) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) if (p->slots[0] >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) ret = btrfs_next_leaf(root, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) if (!return_any)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) * no higher item found, return the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) * lower instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) return_any = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) find_higher = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) if (p->slots[0] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) ret = btrfs_prev_leaf(root, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) leaf = p->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) if (p->slots[0] == btrfs_header_nritems(leaf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) p->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) if (!return_any)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) * no lower item found, return the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) * higher instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) return_any = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) find_higher = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) btrfs_release_path(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) --p->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) * adjust the pointers going up the tree, starting at level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) * making sure the right key of each node is points to 'key'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) * This is used after shifting pointers to the left, so it stops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) * fixing up pointers when a given leaf/node is not in slot 0 of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) * higher levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) static void fixup_low_keys(struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) struct btrfs_disk_key *key, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) struct extent_buffer *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) for (i = level; i < BTRFS_MAX_LEVEL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) int tslot = path->slots[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) if (!path->nodes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) t = path->nodes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) btrfs_set_node_key(t, key, tslot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) btrfs_mark_buffer_dirty(path->nodes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) if (tslot != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) * update item key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) * This function isn't completely safe. It's the caller's responsibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) * that the new key won't break the order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) const struct btrfs_key *new_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) if (slot > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) btrfs_item_key(eb, &disk_key, slot - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) slot, btrfs_disk_key_objectid(&disk_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) btrfs_disk_key_type(&disk_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) btrfs_disk_key_offset(&disk_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) new_key->objectid, new_key->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) new_key->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) btrfs_print_leaf(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) if (slot < btrfs_header_nritems(eb) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) btrfs_item_key(eb, &disk_key, slot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) slot, btrfs_disk_key_objectid(&disk_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) btrfs_disk_key_type(&disk_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) btrfs_disk_key_offset(&disk_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) new_key->objectid, new_key->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) new_key->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) btrfs_print_leaf(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) btrfs_cpu_key_to_disk(&disk_key, new_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) btrfs_set_item_key(eb, &disk_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) btrfs_mark_buffer_dirty(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) if (slot == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) fixup_low_keys(path, &disk_key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) * Check key order of two sibling extent buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) * Return true if something is wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) * Return false if everything is fine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) * Tree-checker only works inside one tree block, thus the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) * corruption can not be detected by tree-checker:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) * Leaf @left | Leaf @right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) * --------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) * Key f6 in leaf @left itself is valid, but not valid when the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) * key in leaf @right is 7.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) * This can only be checked at tree block merge time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) * And since tree checker has ensured all key order in each tree block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) * is correct, we only need to bother the last key of @left and the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) * key of @right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) static bool check_sibling_keys(struct extent_buffer *left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) struct extent_buffer *right)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) struct btrfs_key left_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) struct btrfs_key right_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) int level = btrfs_header_level(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) int nr_left = btrfs_header_nritems(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) int nr_right = btrfs_header_nritems(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) /* No key to check in one of the tree blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) if (!nr_left || !nr_right)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) if (level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) btrfs_node_key_to_cpu(right, &right_first, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) btrfs_item_key_to_cpu(right, &right_first, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) btrfs_crit(left->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) left_last.objectid, left_last.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) left_last.offset, right_first.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) right_first.type, right_first.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) * try to push data from one node into the next node left in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) * tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) * error, and > 0 if there was no room in the left hand block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) static int push_node_left(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) struct extent_buffer *src, int empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) int push_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) int src_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) int dst_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) src_nritems = btrfs_header_nritems(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) dst_nritems = btrfs_header_nritems(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) WARN_ON(btrfs_header_generation(src) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) WARN_ON(btrfs_header_generation(dst) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) if (!empty && src_nritems <= 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) if (push_items <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) if (empty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) push_items = min(src_nritems, push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) if (push_items < src_nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) /* leave at least 8 pointers in the node if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) * we aren't going to empty it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) if (src_nritems - push_items < 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) if (push_items <= 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) push_items -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) push_items = min(src_nritems - 8, push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) /* dst is the left eb, src is the middle eb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) if (check_sibling_keys(dst, src)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) copy_extent_buffer(dst, src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) btrfs_node_key_ptr_offset(dst_nritems),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) btrfs_node_key_ptr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) push_items * sizeof(struct btrfs_key_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) if (push_items < src_nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) * Don't call tree_mod_log_insert_move here, key removal was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) * already fully logged by tree_mod_log_eb_copy above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) btrfs_node_key_ptr_offset(push_items),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) (src_nritems - push_items) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) sizeof(struct btrfs_key_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) btrfs_set_header_nritems(src, src_nritems - push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) btrfs_set_header_nritems(dst, dst_nritems + push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) btrfs_mark_buffer_dirty(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) btrfs_mark_buffer_dirty(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) * try to push data from one node into the next node right in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) * tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) * returns 0 if some ptrs were pushed, < 0 if there was some horrible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) * error, and > 0 if there was no room in the right hand block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) * this will only push up to 1/2 the contents of the left node over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) static int balance_node_right(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) struct extent_buffer *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) int push_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) int max_push;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) int src_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) int dst_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) WARN_ON(btrfs_header_generation(src) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) WARN_ON(btrfs_header_generation(dst) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) src_nritems = btrfs_header_nritems(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) dst_nritems = btrfs_header_nritems(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) if (push_items <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) if (src_nritems < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) max_push = src_nritems / 2 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) /* don't try to empty the node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) if (max_push >= src_nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) if (max_push < push_items)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) push_items = max_push;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) /* dst is the right eb, src is the middle eb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) if (check_sibling_keys(src, dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) btrfs_node_key_ptr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) (dst_nritems) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) sizeof(struct btrfs_key_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) copy_extent_buffer(dst, src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) btrfs_node_key_ptr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) btrfs_node_key_ptr_offset(src_nritems - push_items),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) push_items * sizeof(struct btrfs_key_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) btrfs_set_header_nritems(src, src_nritems - push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) btrfs_set_header_nritems(dst, dst_nritems + push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) btrfs_mark_buffer_dirty(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) btrfs_mark_buffer_dirty(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) * helper function to insert a new root level in the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) * A new node is allocated, and a single item is inserted to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) * point to the existing root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) * returns zero on success or < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) static noinline int insert_new_root(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) struct btrfs_path *path, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) u64 lower_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) struct extent_buffer *lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) struct extent_buffer *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) struct extent_buffer *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) struct btrfs_disk_key lower_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) BUG_ON(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) BUG_ON(path->nodes[level-1] != root->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) lower = path->nodes[level-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) if (level == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) btrfs_item_key(lower, &lower_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) btrfs_node_key(lower, &lower_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) root->node->start, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) BTRFS_NESTING_NEW_ROOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) if (IS_ERR(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) return PTR_ERR(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) root_add_used(root, fs_info->nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) btrfs_set_header_nritems(c, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) btrfs_set_node_key(c, &lower_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) btrfs_set_node_blockptr(c, 0, lower->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) lower_gen = btrfs_header_generation(lower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) WARN_ON(lower_gen != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) btrfs_set_node_ptr_generation(c, 0, lower_gen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) btrfs_mark_buffer_dirty(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) old = root->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) ret = tree_mod_log_insert_root(root->node, c, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) rcu_assign_pointer(root->node, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) /* the super has an extra ref to root->node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) free_extent_buffer(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) add_root_to_dirty_list(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) atomic_inc(&c->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) path->nodes[level] = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) * worker function to insert a single pointer in a node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) * the node should have enough room for the pointer already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) * slot and level indicate where you want the key to go, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) * blocknr is the block the key points to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) static void insert_ptr(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) struct btrfs_disk_key *key, u64 bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) int slot, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) struct extent_buffer *lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) BUG_ON(!path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) btrfs_assert_tree_locked(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) lower = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) nritems = btrfs_header_nritems(lower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) BUG_ON(slot > nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) if (slot != nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) if (level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) ret = tree_mod_log_insert_move(lower, slot + 1, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) nritems - slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) memmove_extent_buffer(lower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) btrfs_node_key_ptr_offset(slot + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) btrfs_node_key_ptr_offset(slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) (nritems - slot) * sizeof(struct btrfs_key_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) if (level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) btrfs_set_node_key(lower, key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) btrfs_set_node_blockptr(lower, slot, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) WARN_ON(trans->transid == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) btrfs_set_node_ptr_generation(lower, slot, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) btrfs_set_header_nritems(lower, nritems + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) btrfs_mark_buffer_dirty(lower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) * split the node at the specified level in path in two.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) * The path is corrected to point to the appropriate node after the split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) * Before splitting this tries to make some room in the node by pushing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) * left and right, if either one works, it returns right away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) * returns 0 on success and < 0 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) static noinline int split_node(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) struct btrfs_path *path, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) struct extent_buffer *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) struct extent_buffer *split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) int mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) u32 c_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) c = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) WARN_ON(btrfs_header_generation(c) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) if (c == root->node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) * trying to split the root, lets make a new one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) * tree mod log: We don't log_removal old root in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) * insert_new_root, because that root buffer will be kept as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) * normal node. We are going to log removal of half of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) * elements below with tree_mod_log_eb_copy. We're holding a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) * tree lock on the buffer, which is why we cannot race with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) * other tree_mod_log users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) ret = insert_new_root(trans, root, path, level + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) ret = push_nodes_for_insert(trans, root, path, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) c = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) if (!ret && btrfs_header_nritems(c) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) c_nritems = btrfs_header_nritems(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) mid = (c_nritems + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) btrfs_node_key(c, &disk_key, mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) c->start, 0, BTRFS_NESTING_SPLIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) if (IS_ERR(split))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) return PTR_ERR(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) root_add_used(root, fs_info->nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) ASSERT(btrfs_header_level(c) == level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) copy_extent_buffer(split, c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) btrfs_node_key_ptr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) btrfs_node_key_ptr_offset(mid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) btrfs_set_header_nritems(split, c_nritems - mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) btrfs_set_header_nritems(c, mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) btrfs_mark_buffer_dirty(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) btrfs_mark_buffer_dirty(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) insert_ptr(trans, path, &disk_key, split->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) path->slots[level + 1] + 1, level + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) if (path->slots[level] >= mid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) path->slots[level] -= mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) btrfs_tree_unlock(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) free_extent_buffer(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) path->nodes[level] = split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) path->slots[level + 1] += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) btrfs_tree_unlock(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) free_extent_buffer(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) * how many bytes are required to store the items in a leaf. start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) * and nr indicate which items in the leaf to check. This totals up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) * space used both by the item structs and the item data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) static int leaf_space_used(struct extent_buffer *l, int start, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) struct btrfs_item *start_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) struct btrfs_item *end_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) int data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) int nritems = btrfs_header_nritems(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) int end = min(nritems, start + nr) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) if (!nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) start_item = btrfs_item_nr(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) end_item = btrfs_item_nr(end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) data_len = btrfs_item_offset(l, start_item) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) btrfs_item_size(l, start_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) data_len = data_len - btrfs_item_offset(l, end_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) data_len += sizeof(struct btrfs_item) * nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) WARN_ON(data_len < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) return data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) * The space between the end of the leaf items and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) * the start of the leaf data. IOW, how much room
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) * the leaf has left for both items and data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) struct btrfs_fs_info *fs_info = leaf->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) int nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) leaf_space_used(leaf, 0, nritems), nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) * min slot controls the lowest index we're willing to push to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) * right. We'll push up to and including min_slot, but no lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) static noinline int __push_leaf_right(struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) int data_size, int empty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) struct extent_buffer *right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) int free_space, u32 left_nritems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) u32 min_slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) struct btrfs_fs_info *fs_info = right->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) struct extent_buffer *left = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) struct extent_buffer *upper = path->nodes[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) int push_space = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) int push_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) u32 nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) u32 right_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) u32 data_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) u32 this_item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) if (empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) nr = max_t(u32, 1, min_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) if (path->slots[0] >= left_nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) push_space += data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) slot = path->slots[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) i = left_nritems - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) while (i >= nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) if (!empty && push_items > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) if (path->slots[0] > i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) if (path->slots[0] == i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) int space = btrfs_leaf_free_space(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) if (space + push_space * 2 > free_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) if (path->slots[0] == i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) push_space += data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) this_item_size = btrfs_item_size(left, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) if (this_item_size + sizeof(*item) + push_space > free_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) push_items++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) push_space += this_item_size + sizeof(*item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) if (push_items == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) WARN_ON(!empty && push_items == left_nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) /* push left to right */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) right_nritems = btrfs_header_nritems(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) push_space = btrfs_item_end_nr(left, left_nritems - push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) push_space -= leaf_data_end(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) /* make room in the right data area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) data_end = leaf_data_end(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) memmove_extent_buffer(right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) BTRFS_LEAF_DATA_OFFSET + data_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) /* copy from the left data area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) push_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) btrfs_item_nr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) right_nritems * sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) /* copy the items from left to right */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) btrfs_item_nr_offset(left_nritems - push_items),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) push_items * sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) /* update the item pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) btrfs_init_map_token(&token, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) right_nritems += push_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) btrfs_set_header_nritems(right, right_nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) for (i = 0; i < right_nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) push_space -= btrfs_token_item_size(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) btrfs_set_token_item_offset(&token, item, push_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) left_nritems -= push_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) btrfs_set_header_nritems(left, left_nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) if (left_nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) btrfs_mark_buffer_dirty(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) btrfs_clean_tree_block(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) btrfs_mark_buffer_dirty(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) btrfs_item_key(right, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) btrfs_set_node_key(upper, &disk_key, slot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) btrfs_mark_buffer_dirty(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) /* then fixup the leaf pointer in the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) if (path->slots[0] >= left_nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) path->slots[0] -= left_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) if (btrfs_header_nritems(path->nodes[0]) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) btrfs_clean_tree_block(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) btrfs_tree_unlock(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) free_extent_buffer(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) path->nodes[0] = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) path->slots[1] += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) * push some data in the path leaf to the right, trying to free up at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) * least data_size bytes. returns zero if the push worked, nonzero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) * returns 1 if the push failed because the other node didn't have enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) * room, 0 if everything worked out and < 0 if there were major errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) * this will push starting from min_slot to the end of the leaf. It won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) * push any slot lower than min_slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) *root, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) int min_data_size, int data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) int empty, u32 min_slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) struct extent_buffer *left = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) struct extent_buffer *right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) struct extent_buffer *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) int free_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) u32 left_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) if (!path->nodes[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) slot = path->slots[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) upper = path->nodes[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) if (slot >= btrfs_header_nritems(upper) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) btrfs_assert_tree_locked(path->nodes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) right = btrfs_read_node_slot(upper, slot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) * slot + 1 is not valid or we fail to read the right node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) * no big deal, just return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) if (IS_ERR(right))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) btrfs_set_lock_blocking_write(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) free_space = btrfs_leaf_free_space(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) if (free_space < data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) /* cow and double check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) ret = btrfs_cow_block(trans, root, right, upper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) free_space = btrfs_leaf_free_space(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) if (free_space < data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) left_nritems = btrfs_header_nritems(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) if (left_nritems == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) if (check_sibling_keys(left, right)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) if (path->slots[0] == left_nritems && !empty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) /* Key greater than all keys in the leaf, right neighbor has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) * enough room for it and we're not emptying our leaf to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) * it, therefore use right neighbor to insert the new item and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) * no need to touch/dirty our left leaf. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) btrfs_tree_unlock(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) free_extent_buffer(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) path->nodes[0] = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) path->slots[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) path->slots[1]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) return __push_leaf_right(path, min_data_size, empty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) right, free_space, left_nritems, min_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) * push some data in the path leaf to the left, trying to free up at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) * least data_size bytes. returns zero if the push worked, nonzero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) * max_slot can put a limit on how far into the leaf we'll push items. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) * items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) int empty, struct extent_buffer *left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) int free_space, u32 right_nritems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) u32 max_slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) struct btrfs_fs_info *fs_info = left->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) struct extent_buffer *right = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) int push_space = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) int push_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) u32 old_left_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) u32 nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) u32 this_item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) u32 old_left_item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) if (empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) nr = min(right_nritems, max_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) nr = min(right_nritems - 1, max_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) if (!empty && push_items > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) if (path->slots[0] < i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) if (path->slots[0] == i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) int space = btrfs_leaf_free_space(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) if (space + push_space * 2 > free_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) if (path->slots[0] == i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) push_space += data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) this_item_size = btrfs_item_size(right, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) if (this_item_size + sizeof(*item) + push_space > free_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) push_items++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) push_space += this_item_size + sizeof(*item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) if (push_items == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) WARN_ON(!empty && push_items == btrfs_header_nritems(right));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) /* push data from right to left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) copy_extent_buffer(left, right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) btrfs_item_nr_offset(btrfs_header_nritems(left)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) btrfs_item_nr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) push_items * sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) btrfs_item_offset_nr(right, push_items - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) leaf_data_end(left) - push_space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) btrfs_item_offset_nr(right, push_items - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) push_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) old_left_nritems = btrfs_header_nritems(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) BUG_ON(old_left_nritems <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) btrfs_init_map_token(&token, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) u32 ioff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) ioff = btrfs_token_item_offset(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) btrfs_set_token_item_offset(&token, item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) btrfs_set_header_nritems(left, old_left_nritems + push_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) /* fixup right node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) if (push_items > right_nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) right_nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) if (push_items < right_nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) push_space = btrfs_item_offset_nr(right, push_items - 1) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) leaf_data_end(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) leaf_data_end(right), push_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) memmove_extent_buffer(right, btrfs_item_nr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) btrfs_item_nr_offset(push_items),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) (btrfs_header_nritems(right) - push_items) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) btrfs_init_map_token(&token, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) right_nritems -= push_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) btrfs_set_header_nritems(right, right_nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) for (i = 0; i < right_nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) push_space = push_space - btrfs_token_item_size(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) btrfs_set_token_item_offset(&token, item, push_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) btrfs_mark_buffer_dirty(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) if (right_nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) btrfs_mark_buffer_dirty(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) btrfs_clean_tree_block(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) btrfs_item_key(right, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) fixup_low_keys(path, &disk_key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) /* then fixup the leaf pointer in the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) if (path->slots[0] < push_items) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) path->slots[0] += old_left_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) btrfs_tree_unlock(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) free_extent_buffer(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) path->nodes[0] = left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) path->slots[1] -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) btrfs_tree_unlock(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) free_extent_buffer(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) path->slots[0] -= push_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) BUG_ON(path->slots[0] < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) btrfs_tree_unlock(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) free_extent_buffer(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) * push some data in the path leaf to the left, trying to free up at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) * least data_size bytes. returns zero if the push worked, nonzero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) * max_slot can put a limit on how far into the leaf we'll push items. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) * items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) *root, struct btrfs_path *path, int min_data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) int data_size, int empty, u32 max_slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) struct extent_buffer *right = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) struct extent_buffer *left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) int free_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) u32 right_nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) slot = path->slots[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) if (slot == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) if (!path->nodes[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) right_nritems = btrfs_header_nritems(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) if (right_nritems == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) btrfs_assert_tree_locked(path->nodes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) left = btrfs_read_node_slot(path->nodes[1], slot - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) * slot - 1 is not valid or we fail to read the left node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) * no big deal, just return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) if (IS_ERR(left))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) btrfs_set_lock_blocking_write(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) free_space = btrfs_leaf_free_space(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) if (free_space < data_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) goto out;
^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) /* cow and double check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) ret = btrfs_cow_block(trans, root, left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) path->nodes[1], slot - 1, &left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) BTRFS_NESTING_LEFT_COW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) /* we hit -ENOSPC, but it isn't fatal here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) if (ret == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) free_space = btrfs_leaf_free_space(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) if (free_space < data_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) if (check_sibling_keys(left, right)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) return __push_leaf_left(path, min_data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) empty, left, free_space, right_nritems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) max_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) btrfs_tree_unlock(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) free_extent_buffer(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) * split the path's leaf in two, making sure there is at least data_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) * available for the resulting leaf level of the path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) static noinline void copy_for_split(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) struct extent_buffer *l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) struct extent_buffer *right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) int slot, int mid, int nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) int data_copy_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) int rt_data_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) nritems = nritems - mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) btrfs_set_header_nritems(right, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) btrfs_item_nr_offset(mid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) nritems * sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) copy_extent_buffer(right, l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) data_copy_size, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) leaf_data_end(l), data_copy_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) btrfs_init_map_token(&token, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) for (i = 0; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) struct btrfs_item *item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) u32 ioff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) ioff = btrfs_token_item_offset(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) btrfs_set_token_item_offset(&token, item, ioff + rt_data_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) btrfs_set_header_nritems(l, mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) btrfs_item_key(right, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) btrfs_mark_buffer_dirty(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) btrfs_mark_buffer_dirty(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) BUG_ON(path->slots[0] != slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) if (mid <= slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) btrfs_tree_unlock(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) free_extent_buffer(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) path->nodes[0] = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) path->slots[0] -= mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) path->slots[1] += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) btrfs_tree_unlock(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) free_extent_buffer(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) BUG_ON(path->slots[0] < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) * double splits happen when we need to insert a big item in the middle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) * of a leaf. A double split can leave us with 3 mostly empty leaves:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) * A B C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) * We avoid this by trying to push the items on either side of our target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) * into the adjacent leaves. If all goes well we can avoid the double split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) * completely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) int data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) int progress = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) int space_needed = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) if (slot < btrfs_header_nritems(path->nodes[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) space_needed -= btrfs_leaf_free_space(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) * try to push all the items after our slot into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) * right leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) progress++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) nritems = btrfs_header_nritems(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) * our goal is to get our slot at the start or end of a leaf. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) * we've done so we're done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) if (path->slots[0] == 0 || path->slots[0] == nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) /* try to push all the items before our slot into the next leaf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) space_needed = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) if (slot > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) space_needed -= btrfs_leaf_free_space(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) progress++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) if (progress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) * split the path's leaf in two, making sure there is at least data_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) * available for the resulting leaf level of the path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) * returns 0 if all went well and < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) static noinline int split_leaf(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) const struct btrfs_key *ins_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) struct btrfs_path *path, int data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) int extend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) int mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) struct extent_buffer *right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) int wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) int split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) int num_doubles = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) int tried_avoid_double = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) if (extend && data_size + btrfs_item_size_nr(l, slot) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) /* first try to make some room by pushing left and right */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) if (data_size && path->nodes[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) int space_needed = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) if (slot < btrfs_header_nritems(l))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) space_needed -= btrfs_leaf_free_space(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) wret = push_leaf_right(trans, root, path, space_needed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) space_needed, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) if (wret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) return wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) if (wret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) space_needed = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) if (slot > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) space_needed -= btrfs_leaf_free_space(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) wret = push_leaf_left(trans, root, path, space_needed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) space_needed, 0, (u32)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) if (wret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) return wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) /* did the pushes work? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) if (btrfs_leaf_free_space(l) >= data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) if (!path->nodes[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) ret = insert_new_root(trans, root, path, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) split = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) nritems = btrfs_header_nritems(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) mid = (nritems + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) if (mid <= slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) if (nritems == 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) leaf_space_used(l, mid, nritems - mid) + data_size >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) BTRFS_LEAF_DATA_SIZE(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) if (slot >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) split = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) mid = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) if (mid != nritems &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) leaf_space_used(l, mid, nritems - mid) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) if (data_size && !tried_avoid_double)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) goto push_for_double;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) split = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) if (leaf_space_used(l, 0, mid) + data_size >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) BTRFS_LEAF_DATA_SIZE(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) if (!extend && data_size && slot == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) split = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) } else if ((extend || !data_size) && slot == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) mid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) mid = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) if (mid != nritems &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) leaf_space_used(l, mid, nritems - mid) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) if (data_size && !tried_avoid_double)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) goto push_for_double;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) split = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) if (split == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) btrfs_cpu_key_to_disk(&disk_key, ins_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) btrfs_item_key(l, &disk_key, mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) * subclasses, which is 8 at the time of this patch, and we've maxed it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) * out. In the future we could add a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) * use BTRFS_NESTING_NEW_ROOT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) l->start, 0, num_doubles ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) BTRFS_NESTING_NEW_ROOT :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) BTRFS_NESTING_SPLIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) if (IS_ERR(right))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) return PTR_ERR(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) root_add_used(root, fs_info->nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) if (split == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) if (mid <= slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) btrfs_set_header_nritems(right, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) insert_ptr(trans, path, &disk_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) right->start, path->slots[1] + 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) btrfs_tree_unlock(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) free_extent_buffer(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) path->nodes[0] = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) path->slots[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) path->slots[1] += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) btrfs_set_header_nritems(right, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) insert_ptr(trans, path, &disk_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) right->start, path->slots[1], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) btrfs_tree_unlock(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) free_extent_buffer(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) path->nodes[0] = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) path->slots[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) if (path->slots[1] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) fixup_low_keys(path, &disk_key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) * We create a new leaf 'right' for the required ins_len and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) * the content of ins_len to 'right'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) copy_for_split(trans, path, l, right, slot, mid, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) if (split == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) BUG_ON(num_doubles != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) num_doubles++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) push_for_double:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) push_for_double_split(trans, root, path, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) tried_avoid_double = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) struct btrfs_path *path, int ins_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) u64 extent_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) key.type != BTRFS_EXTENT_CSUM_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) if (btrfs_leaf_free_space(leaf) >= ins_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) if (key.type == BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) extent_len = btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) path->keep_locks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) path->search_for_split = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) path->search_for_split = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) /* if our item isn't there, return now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) /* the leaf has changed, it now has room. return now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) if (key.type == BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) ret = split_leaf(trans, root, &key, path, ins_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) path->keep_locks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) btrfs_unlock_up_safe(path, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) path->keep_locks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) static noinline int split_item(struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) const struct btrfs_key *new_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) unsigned long split_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) struct btrfs_item *new_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) u32 orig_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) item = btrfs_item_nr(path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) orig_offset = btrfs_item_offset(leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) item_size = btrfs_item_size(leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) buf = kmalloc(item_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) path->slots[0]), item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) slot = path->slots[0] + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) if (slot != nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) /* shift the items */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) btrfs_item_nr_offset(slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) (nritems - slot) * sizeof(struct btrfs_item));
^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) btrfs_cpu_key_to_disk(&disk_key, new_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) btrfs_set_item_key(leaf, &disk_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) new_item = btrfs_item_nr(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) btrfs_set_item_offset(leaf, new_item, orig_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) btrfs_set_item_size(leaf, new_item, item_size - split_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) btrfs_set_item_offset(leaf, item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) orig_offset + item_size - split_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) btrfs_set_item_size(leaf, item, split_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) btrfs_set_header_nritems(leaf, nritems + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) /* write the data for the start of the original item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) write_extent_buffer(leaf, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) btrfs_item_ptr_offset(leaf, path->slots[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) split_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) /* write the data for the new item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) write_extent_buffer(leaf, buf + split_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) btrfs_item_ptr_offset(leaf, slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) item_size - split_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) BUG_ON(btrfs_leaf_free_space(leaf) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) return 0;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) * This function splits a single item into two items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) * giving 'new_key' to the new item and splitting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) * old one at split_offset (from the start of the item).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) * The path may be released by this operation. After
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) * the split, the path is pointing to the old item. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) * new item is going to be in the same node as the old one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) * Note, the item being split must be smaller enough to live alone on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) * a tree block with room for one extra struct btrfs_item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) * This allows us to split the item in place, keeping a lock on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) * leaf the entire time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) int btrfs_split_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) const struct btrfs_key *new_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) unsigned long split_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) ret = setup_leaf_for_split(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) ret = split_item(path, new_key, split_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) * This function duplicate a item, giving 'new_key' to the new item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) * It guarantees both items live in the same tree leaf and the new item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) * is contiguous with the original item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) * This allows us to split file extent in place, keeping a lock on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) * leaf the entire time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) const struct btrfs_key *new_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) ret = setup_leaf_for_split(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) item_size + sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) setup_items_for_insert(root, path, new_key, &item_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) memcpy_extent_buffer(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) btrfs_item_ptr_offset(leaf, path->slots[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) * make the item pointed to by the path smaller. new_size indicates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) * how small to make it, and from_end tells us if we just chop bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) * off the end of the item or if we shift the item to chop bytes off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) * the front.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) unsigned int data_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) unsigned int old_data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) unsigned int old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) unsigned int size_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) old_size = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) if (old_size == new_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) data_end = leaf_data_end(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) old_data_start = btrfs_item_offset_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) size_diff = old_size - new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) BUG_ON(slot < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) BUG_ON(slot >= nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) * item0..itemN ... dataN.offset..dataN.size .. data0.size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) /* first correct the data pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) btrfs_init_map_token(&token, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) for (i = slot; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) u32 ioff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) ioff = btrfs_token_item_offset(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) btrfs_set_token_item_offset(&token, item, ioff + size_diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) /* shift the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) if (from_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) data_end, old_data_start + new_size - data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) btrfs_item_key(leaf, &disk_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) fi = btrfs_item_ptr(leaf, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) fi = (struct btrfs_file_extent_item *)(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) (unsigned long)fi - size_diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) if (btrfs_file_extent_type(leaf, fi) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) ptr = btrfs_item_ptr_offset(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) memmove_extent_buffer(leaf, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) (unsigned long)fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) BTRFS_FILE_EXTENT_INLINE_DATA_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) data_end, old_data_start - data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) offset = btrfs_disk_key_offset(&disk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) btrfs_set_item_key(leaf, &disk_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) if (slot == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) fixup_low_keys(path, &disk_key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) item = btrfs_item_nr(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) btrfs_set_item_size(leaf, item, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) if (btrfs_leaf_free_space(leaf) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) btrfs_print_leaf(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) * make the item pointed to by the path bigger, data_size is the added size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) unsigned int data_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) unsigned int old_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) unsigned int old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) data_end = leaf_data_end(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) if (btrfs_leaf_free_space(leaf) < data_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) btrfs_print_leaf(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) old_data = btrfs_item_end_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) BUG_ON(slot < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) if (slot >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) btrfs_print_leaf(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) slot, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) * item0..itemN ... dataN.offset..dataN.size .. data0.size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) /* first correct the data pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) btrfs_init_map_token(&token, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) for (i = slot; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) u32 ioff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) ioff = btrfs_token_item_offset(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) btrfs_set_token_item_offset(&token, item, ioff - data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) /* shift the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) data_end, old_data - data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) data_end = old_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) old_size = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) item = btrfs_item_nr(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) btrfs_set_item_size(leaf, item, old_size + data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) if (btrfs_leaf_free_space(leaf) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) btrfs_print_leaf(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) * setup_items_for_insert - Helper called before inserting one or more items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) * in a function that doesn't call btrfs_search_slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) * @root: root we are inserting items to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) * @path: points to the leaf/slot where we are going to insert new items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) * @cpu_key: array of keys for items to be inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) * @data_size: size of the body of each item we are going to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) * @nr: size of @cpu_key/@data_size arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) const struct btrfs_key *cpu_key, u32 *data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) unsigned int data_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) u32 total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) u32 total_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) for (i = 0; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) total_data += data_size[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) total_size = total_data + (nr * sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) if (path->slots[0] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) btrfs_cpu_key_to_disk(&disk_key, cpu_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) fixup_low_keys(path, &disk_key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) btrfs_unlock_up_safe(path, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) data_end = leaf_data_end(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) if (btrfs_leaf_free_space(leaf) < total_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) btrfs_print_leaf(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) btrfs_crit(fs_info, "not enough freespace need %u have %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) total_size, btrfs_leaf_free_space(leaf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) btrfs_init_map_token(&token, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) if (slot != nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) unsigned int old_data = btrfs_item_end_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) if (old_data < data_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) btrfs_print_leaf(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) "item at slot %d with data offset %u beyond data end of leaf %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) slot, old_data, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) * item0..itemN ... dataN.offset..dataN.size .. data0.size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) /* first correct the data pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) for (i = slot; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) u32 ioff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) ioff = btrfs_token_item_offset(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) btrfs_set_token_item_offset(&token, item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) ioff - total_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) /* shift the items */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) btrfs_item_nr_offset(slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) (nritems - slot) * sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) /* shift the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) data_end, old_data - data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) data_end = old_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) /* setup the item for the new data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) btrfs_set_item_key(leaf, &disk_key, slot + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) item = btrfs_item_nr(slot + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) data_end -= data_size[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) btrfs_set_token_item_offset(&token, item, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) btrfs_set_token_item_size(&token, item, data_size[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) btrfs_set_header_nritems(leaf, nritems + nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) if (btrfs_leaf_free_space(leaf) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) btrfs_print_leaf(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) }
^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) * Given a key and some data, insert items into the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) * This does all the path init required, making room in the tree if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) const struct btrfs_key *cpu_key, u32 *data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) u32 total_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) u32 total_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) for (i = 0; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) total_data += data_size[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) total_size = total_data + (nr * sizeof(struct btrfs_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) BUG_ON(slot < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) setup_items_for_insert(root, path, cpu_key, data_size, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) * Given a key and some data, insert an item into the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) * This does all the path init required, making room in the tree if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) const struct btrfs_key *cpu_key, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) u32 data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) int ret = 0;
^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 extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) write_extent_buffer(leaf, data, ptr, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) * delete the pointer from a given node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) * the tree should have been previously balanced so the deletion does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) * empty a node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) int level, int slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) struct extent_buffer *parent = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) nritems = btrfs_header_nritems(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) if (slot != nritems - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) if (level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) ret = tree_mod_log_insert_move(parent, slot, slot + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) nritems - slot - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) memmove_extent_buffer(parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) btrfs_node_key_ptr_offset(slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) btrfs_node_key_ptr_offset(slot + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) sizeof(struct btrfs_key_ptr) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) (nritems - slot - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) } else if (level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) nritems--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) btrfs_set_header_nritems(parent, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) if (nritems == 0 && parent == root->node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) BUG_ON(btrfs_header_level(root->node) != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) /* just turn the root into a leaf and break */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) btrfs_set_header_level(root->node, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) } else if (slot == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) btrfs_node_key(parent, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) fixup_low_keys(path, &disk_key, level + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) btrfs_mark_buffer_dirty(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) * a helper function to delete the leaf pointed to by path->slots[1] and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) * path->nodes[1].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) * This deletes the pointer in path->nodes[1] and frees the leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) * block extent. zero is returned if it all worked out, < 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) * The path must have already been setup for deleting the leaf, including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) * all the proper balancing. path->nodes[1] must be locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) struct extent_buffer *leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) WARN_ON(btrfs_header_generation(leaf) != trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) del_ptr(root, path, 1, path->slots[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) * btrfs_free_extent is expensive, we want to make sure we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) * aren't holding any locks when we call it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) btrfs_unlock_up_safe(path, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) root_sub_used(root, leaf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) atomic_inc(&leaf->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) btrfs_free_tree_block(trans, root, leaf, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) free_extent_buffer_stale(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) * delete the item at the leaf level in path. If that empties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) * the leaf, remove it from the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) struct btrfs_path *path, int slot, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) u32 last_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) u32 dsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) int wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) for (i = 0; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059) dsize += btrfs_item_size_nr(leaf, slot + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) if (slot + nr != nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) int data_end = leaf_data_end(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) data_end + dsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) BTRFS_LEAF_DATA_OFFSET + data_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) last_off - data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) btrfs_init_map_token(&token, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) for (i = slot + nr; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) u32 ioff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) item = btrfs_item_nr(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) ioff = btrfs_token_item_offset(&token, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) btrfs_set_token_item_offset(&token, item, ioff + dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) btrfs_item_nr_offset(slot + nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) sizeof(struct btrfs_item) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) (nritems - slot - nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086) btrfs_set_header_nritems(leaf, nritems - nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087) nritems -= nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) /* delete the leaf if we've emptied it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) if (nritems == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091) if (leaf == root->node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) btrfs_set_header_level(leaf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) btrfs_clean_tree_block(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) btrfs_del_leaf(trans, root, path, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) int used = leaf_space_used(leaf, 0, nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) if (slot == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) btrfs_item_key(leaf, &disk_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) fixup_low_keys(path, &disk_key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) /* delete the leaf if it is mostly empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109) /* push_leaf_left fixes the path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) * make sure the path still points to our leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) * for possible call to del_ptr below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) slot = path->slots[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) atomic_inc(&leaf->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) wret = push_leaf_left(trans, root, path, 1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) 1, (u32)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) if (wret < 0 && wret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) if (path->nodes[0] == leaf &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) wret = push_leaf_right(trans, root, path, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) 1, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) if (wret < 0 && wret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) if (btrfs_header_nritems(leaf) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) path->slots[1] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) btrfs_del_leaf(trans, root, path, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) free_extent_buffer(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) /* if we're still in the path, make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) * we're dirty. Otherwise, one of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) * push_leaf functions must have already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) * dirtied this buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) if (path->nodes[0] == leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143) free_extent_buffer(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) * search the tree again to find a leaf with lesser keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) * returns 0 if it found something or 1 if there are no lesser leaves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) * returns < 0 on io errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) * This may release the path, and so you may lose any locks held at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) * time you call it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) struct btrfs_disk_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) if (key.offset > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) key.offset--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) } else if (key.type > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) key.type--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) } else if (key.objectid > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) key.objectid--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) key.type = (u8)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) return 1;
^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) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) btrfs_item_key(path->nodes[0], &found_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) ret = comp_keys(&found_key, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) * We might have had an item with the previous key in the tree right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) * before we released our path. And after we released our path, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) * item might have been pushed to the first slot (0) of the leaf we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) * were holding due to a tree balance. Alternatively, an item with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) * previous key can exist as the only element of a leaf (big fat item).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) * Therefore account for these 2 cases, so that our callers (like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) * btrfs_previous_item) don't miss an existing item with a key matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) * the previous key we computed above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) * A helper function to walk down the tree starting at min_key, and looking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) * for nodes or leaves that are have a minimum transaction id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) * This is used by the btree defrag code, and tree logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) * This does not cow, but it does stuff the starting key it finds back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) * into min_key, so you can call btrfs_search_slot with cow=1 on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) * key and get a writable path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211) * This honors path->lowest_level to prevent descent past a given level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) * of the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) * min_trans indicates the oldest transaction that you are interested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) * in walking through. Any nodes or leaves older than min_trans are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) * skipped over (without reading them).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) * returns zero if something useful was found, < 0 on error and 1 if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) * was nothing in the tree that matched the search criteria.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) u64 min_trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) struct extent_buffer *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) int sret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) int keep_locks = path->keep_locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) path->keep_locks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) cur = btrfs_read_lock_root_node(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) level = btrfs_header_level(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) WARN_ON(path->nodes[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239) path->nodes[level] = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) path->locks[level] = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242) if (btrfs_header_generation(cur) < min_trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) nritems = btrfs_header_nritems(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) level = btrfs_header_level(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) sret = btrfs_bin_search(cur, min_key, &slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) if (sret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) ret = sret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) /* at the lowest level, we're done, setup the path and exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256) if (level == path->lowest_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257) if (slot >= nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) goto find_next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) path->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261) btrfs_item_key_to_cpu(cur, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) if (sret && slot > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) slot--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) * check this node pointer against the min_trans parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) * If it is too old, skip to the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) while (slot < nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) u64 gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) gen = btrfs_node_ptr_generation(cur, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) if (gen < min_trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) slot++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) }
^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) find_next_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) * we didn't find a candidate key in this node, walk forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) * and find another one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) if (slot >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) path->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) sret = btrfs_find_next_key(root, path, min_key, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) min_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) if (sret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) /* save our key for returning back */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) btrfs_node_key_to_cpu(cur, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) path->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) if (level == path->lowest_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) cur = btrfs_read_node_slot(cur, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) if (IS_ERR(cur)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) ret = PTR_ERR(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) btrfs_tree_read_lock(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) path->locks[level - 1] = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) path->nodes[level - 1] = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) unlock_up(path, level, 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318) path->keep_locks = keep_locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) btrfs_unlock_up_safe(path, path->lowest_level + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) memcpy(min_key, &found_key, sizeof(found_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) return ret;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) * this is similar to btrfs_next_leaf, but does not try to preserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) * and fixup the path. It looks for and returns the next key in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) * tree based on the current path and the min_trans parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) * 0 is returned if another key is found, < 0 if there are any errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) * and 1 is returned if there are no higher keys in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335) * path->keep_locks should be set to 1 on the search made before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) * calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) struct btrfs_key *key, int level, u64 min_trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) struct extent_buffer *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) WARN_ON(!path->keep_locks && !path->skip_locking);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) while (level < BTRFS_MAX_LEVEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) if (!path->nodes[level])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) slot = path->slots[level] + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) c = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) if (slot >= btrfs_header_nritems(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) int orig_lowest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) struct btrfs_key cur_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) if (level + 1 >= BTRFS_MAX_LEVEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) !path->nodes[level + 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) if (path->locks[level + 1] || path->skip_locking) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) level++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) continue;
^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) slot = btrfs_header_nritems(c) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) btrfs_item_key_to_cpu(c, &cur_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) btrfs_node_key_to_cpu(c, &cur_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371) orig_lowest = path->lowest_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373) path->lowest_level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) ret = btrfs_search_slot(NULL, root, &cur_key, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) path->lowest_level = orig_lowest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) c = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) slot = path->slots[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) slot++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) btrfs_item_key_to_cpu(c, key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) u64 gen = btrfs_node_ptr_generation(c, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) if (gen < min_trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) slot++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) btrfs_node_key_to_cpu(c, key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) * search the tree again to find a leaf with greater keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) * returns 0 if it found something or 1 if there are no greater leaves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) * returns < 0 on io errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) return btrfs_next_old_leaf(root, path, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) u64 time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) struct extent_buffer *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) struct extent_buffer *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) int old_spinning = path->leave_spinning;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) int next_rw_lock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) nritems = btrfs_header_nritems(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) if (nritems == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) level = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) next_rw_lock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) path->keep_locks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) if (time_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) ret = btrfs_search_old_slot(root, &key, path, time_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) path->keep_locks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) nritems = btrfs_header_nritems(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451) * by releasing the path above we dropped all our locks. A balance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) * could have added more items next to the key that used to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) * at the very end of the block. So, check again here and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) * advance the path if there are now more items available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) if (nritems > 0 && path->slots[0] < nritems - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) * So the above check misses one case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) * - after releasing the path above, someone has removed the item that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) * used to be at the very end of the block, and balance between leafs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) * gets another one with bigger key.offset to replace it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) * This one should be returned as well, or we can get leaf corruption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) * later(esp. in __btrfs_drop_extents()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) * And a bit more explanation about this check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) * with ret > 0, the key isn't found, the path points to the slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) * where it should be inserted, so the path->slots[0] item must be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) * bigger one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) goto done;
^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) while (level < BTRFS_MAX_LEVEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) if (!path->nodes[level]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) slot = path->slots[level] + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) c = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) if (slot >= btrfs_header_nritems(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) level++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) if (level == BTRFS_MAX_LEVEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) if (next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) btrfs_tree_unlock_rw(next, next_rw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) free_extent_buffer(next);
^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) next = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) next_rw_lock = path->locks[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) ret = read_block_for_search(root, path, &next, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) slot, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) if (!path->skip_locking) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) ret = btrfs_try_tree_read_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) if (!ret && time_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) * If we don't get the lock, we may be racing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) * with push_leaf_left, holding that lock while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) * itself waiting for the leaf we've currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) * locked. To solve this situation, we give up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) * on our lock and cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) __btrfs_tree_read_lock(next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) BTRFS_NESTING_RIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) path->recurse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) next_rw_lock = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) path->slots[level] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) level--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) c = path->nodes[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) if (path->locks[level])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) btrfs_tree_unlock_rw(c, path->locks[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) free_extent_buffer(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) path->nodes[level] = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) if (!path->skip_locking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) path->locks[level] = next_rw_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) if (!level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) ret = read_block_for_search(root, path, &next, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) 0, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) if (!path->skip_locking) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) ret = btrfs_try_tree_read_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) __btrfs_tree_read_lock(next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) BTRFS_NESTING_RIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) path->recurse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) next_rw_lock = BTRFS_READ_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) unlock_up(path, 0, 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) path->leave_spinning = old_spinning;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) if (!old_spinning)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) return ret;
^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) * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) * searching until it gets past min_objectid or finds an item of 'type'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) * returns 0 if something is found, 1 if nothing was found and < 0 on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) int btrfs_previous_item(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) struct btrfs_path *path, u64 min_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) if (path->slots[0] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) ret = btrfs_prev_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) if (nritems == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) if (path->slots[0] == nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) if (found_key.objectid < min_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) if (found_key.type == type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) if (found_key.objectid == min_objectid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) found_key.type < type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) * search in extent tree to find a previous Metadata/Data extent item with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) * min objecitd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) * returns 0 if something is found, 1 if nothing was found and < 0 on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) int btrfs_previous_extent_item(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) struct btrfs_path *path, u64 min_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) if (path->slots[0] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) ret = btrfs_prev_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) if (nritems == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) if (path->slots[0] == nritems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) if (found_key.objectid < min_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) found_key.type == BTRFS_METADATA_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) if (found_key.objectid == min_objectid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) found_key.type < BTRFS_EXTENT_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) }