16cbd5570SChris Mason /* 2d352ac68SChris Mason * Copyright (C) 2007,2008 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 19a6b6e75eSChris Mason #include <linux/sched.h> 20eb60ceacSChris Mason #include "ctree.h" 21eb60ceacSChris Mason #include "disk-io.h" 227f5c1516SChris Mason #include "transaction.h" 235f39d397SChris Mason #include "print-tree.h" 24925baeddSChris Mason #include "locking.h" 259a8dd150SChris Mason 26e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 27e089f05cSChris Mason *root, struct btrfs_path *path, int level); 28e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root 29d4dbff95SChris Mason *root, struct btrfs_key *ins_key, 30cc0c5538SChris Mason struct btrfs_path *path, int data_size, int extend); 315f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 325f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 33971a1f66SChris Mason struct extent_buffer *src, int empty); 345f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 355f39d397SChris Mason struct btrfs_root *root, 365f39d397SChris Mason struct extent_buffer *dst_buf, 375f39d397SChris Mason struct extent_buffer *src_buf); 38e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 39e089f05cSChris Mason struct btrfs_path *path, int level, int slot); 40d97e63b6SChris Mason 412c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 422c90e5d6SChris Mason { 43df24a2b9SChris Mason struct btrfs_path *path; 44*e00f7308SJeff Mahoney path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 45*e00f7308SJeff Mahoney if (path) 462cc58cf2SChris Mason path->reada = 1; 47df24a2b9SChris Mason return path; 482c90e5d6SChris Mason } 492c90e5d6SChris Mason 50b4ce94deSChris Mason /* 51b4ce94deSChris Mason * set all locked nodes in the path to blocking locks. This should 52b4ce94deSChris Mason * be done before scheduling 53b4ce94deSChris Mason */ 54b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p) 55b4ce94deSChris Mason { 56b4ce94deSChris Mason int i; 57b4ce94deSChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 58b4ce94deSChris Mason if (p->nodes[i] && p->locks[i]) 59b4ce94deSChris Mason btrfs_set_lock_blocking(p->nodes[i]); 60b4ce94deSChris Mason } 61b4ce94deSChris Mason } 62b4ce94deSChris Mason 63b4ce94deSChris Mason /* 64b4ce94deSChris Mason * reset all the locked nodes in the patch to spinning locks. 65b4ce94deSChris Mason */ 66b4ce94deSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p) 67b4ce94deSChris Mason { 68b4ce94deSChris Mason int i; 69b4ce94deSChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 70b4ce94deSChris Mason if (p->nodes[i] && p->locks[i]) 71b4ce94deSChris Mason btrfs_clear_lock_blocking(p->nodes[i]); 72b4ce94deSChris Mason } 73b4ce94deSChris Mason } 74b4ce94deSChris Mason 75d352ac68SChris Mason /* this also releases the path */ 762c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 772c90e5d6SChris Mason { 78df24a2b9SChris Mason btrfs_release_path(NULL, p); 792c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 802c90e5d6SChris Mason } 812c90e5d6SChris Mason 82d352ac68SChris Mason /* 83d352ac68SChris Mason * path release drops references on the extent buffers in the path 84d352ac68SChris Mason * and it drops any locks held by this path 85d352ac68SChris Mason * 86d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 87d352ac68SChris Mason */ 88d397712bSChris Mason noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) 89eb60ceacSChris Mason { 90eb60ceacSChris Mason int i; 91a2135011SChris Mason 92234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 933f157a2fSChris Mason p->slots[i] = 0; 94eb60ceacSChris Mason if (!p->nodes[i]) 95925baeddSChris Mason continue; 96925baeddSChris Mason if (p->locks[i]) { 97925baeddSChris Mason btrfs_tree_unlock(p->nodes[i]); 98925baeddSChris Mason p->locks[i] = 0; 99925baeddSChris Mason } 1005f39d397SChris Mason free_extent_buffer(p->nodes[i]); 1013f157a2fSChris Mason p->nodes[i] = NULL; 102eb60ceacSChris Mason } 103eb60ceacSChris Mason } 104eb60ceacSChris Mason 105d352ac68SChris Mason /* 106d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 107d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 108d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 109d352ac68SChris Mason * looping required. 110d352ac68SChris Mason * 111d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 112d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 113d352ac68SChris Mason * at any time because there are no locks held. 114d352ac68SChris Mason */ 115925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 116925baeddSChris Mason { 117925baeddSChris Mason struct extent_buffer *eb; 118925baeddSChris Mason spin_lock(&root->node_lock); 119925baeddSChris Mason eb = root->node; 120925baeddSChris Mason extent_buffer_get(eb); 121925baeddSChris Mason spin_unlock(&root->node_lock); 122925baeddSChris Mason return eb; 123925baeddSChris Mason } 124925baeddSChris Mason 125d352ac68SChris Mason /* loop around taking references on and locking the root node of the 126d352ac68SChris Mason * tree until you end up with a lock on the root. A locked buffer 127d352ac68SChris Mason * is returned, with a reference held. 128d352ac68SChris Mason */ 129925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 130925baeddSChris Mason { 131925baeddSChris Mason struct extent_buffer *eb; 132925baeddSChris Mason 133925baeddSChris Mason while (1) { 134925baeddSChris Mason eb = btrfs_root_node(root); 135925baeddSChris Mason btrfs_tree_lock(eb); 136925baeddSChris Mason 137925baeddSChris Mason spin_lock(&root->node_lock); 138925baeddSChris Mason if (eb == root->node) { 139925baeddSChris Mason spin_unlock(&root->node_lock); 140925baeddSChris Mason break; 141925baeddSChris Mason } 142925baeddSChris Mason spin_unlock(&root->node_lock); 143925baeddSChris Mason 144925baeddSChris Mason btrfs_tree_unlock(eb); 145925baeddSChris Mason free_extent_buffer(eb); 146925baeddSChris Mason } 147925baeddSChris Mason return eb; 148925baeddSChris Mason } 149925baeddSChris Mason 150d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get 151d352ac68SChris Mason * put onto a simple dirty list. transaction.c walks this to make sure they 152d352ac68SChris Mason * get properly updated on disk. 153d352ac68SChris Mason */ 1540b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 1550b86a832SChris Mason { 1560b86a832SChris Mason if (root->track_dirty && list_empty(&root->dirty_list)) { 1570b86a832SChris Mason list_add(&root->dirty_list, 1580b86a832SChris Mason &root->fs_info->dirty_cowonly_roots); 1590b86a832SChris Mason } 1600b86a832SChris Mason } 1610b86a832SChris Mason 162d352ac68SChris Mason /* 163d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 164d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 165d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 166d352ac68SChris Mason */ 167be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 168be20aa9dSChris Mason struct btrfs_root *root, 169be20aa9dSChris Mason struct extent_buffer *buf, 170be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 171be20aa9dSChris Mason { 172be20aa9dSChris Mason struct extent_buffer *cow; 173be20aa9dSChris Mason u32 nritems; 174be20aa9dSChris Mason int ret = 0; 175be20aa9dSChris Mason int level; 1764aec2b52SChris Mason struct btrfs_root *new_root; 177be20aa9dSChris Mason 1784aec2b52SChris Mason new_root = kmalloc(sizeof(*new_root), GFP_NOFS); 1794aec2b52SChris Mason if (!new_root) 1804aec2b52SChris Mason return -ENOMEM; 1814aec2b52SChris Mason 1824aec2b52SChris Mason memcpy(new_root, root, sizeof(*new_root)); 1834aec2b52SChris Mason new_root->root_key.objectid = new_root_objectid; 184be20aa9dSChris Mason 185be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != 186be20aa9dSChris Mason root->fs_info->running_transaction->transid); 187be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 188be20aa9dSChris Mason 189be20aa9dSChris Mason level = btrfs_header_level(buf); 190be20aa9dSChris Mason nritems = btrfs_header_nritems(buf); 19131840ae1SZheng Yan 19231840ae1SZheng Yan cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0, 19331840ae1SZheng Yan new_root_objectid, trans->transid, 194be20aa9dSChris Mason level, buf->start, 0); 1954aec2b52SChris Mason if (IS_ERR(cow)) { 1964aec2b52SChris Mason kfree(new_root); 197be20aa9dSChris Mason return PTR_ERR(cow); 1984aec2b52SChris Mason } 199be20aa9dSChris Mason 200be20aa9dSChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 201be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 202be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 203be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 20463b10fc4SChris Mason btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 205be20aa9dSChris Mason 2062b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 2072b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 2082b82032cSYan Zheng BTRFS_FSID_SIZE); 2092b82032cSYan Zheng 210be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 21131840ae1SZheng Yan ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL); 2124aec2b52SChris Mason kfree(new_root); 2134aec2b52SChris Mason 214be20aa9dSChris Mason if (ret) 215be20aa9dSChris Mason return ret; 216be20aa9dSChris Mason 217be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 218be20aa9dSChris Mason *cow_ret = cow; 219be20aa9dSChris Mason return 0; 220be20aa9dSChris Mason } 221be20aa9dSChris Mason 222d352ac68SChris Mason /* 223d397712bSChris Mason * does the dirty work in cow of a single block. The parent block (if 224d397712bSChris Mason * supplied) is updated to point to the new cow copy. The new buffer is marked 225d397712bSChris Mason * dirty and returned locked. If you modify the block it needs to be marked 226d397712bSChris Mason * dirty again. 227d352ac68SChris Mason * 228d352ac68SChris Mason * search_start -- an allocation hint for the new block 229d352ac68SChris Mason * 230d397712bSChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in 231d397712bSChris Mason * bytes the allocator should try to find free next to the block it returns. 232d397712bSChris Mason * This is just a hint and may be ignored by the allocator. 233d352ac68SChris Mason * 234d352ac68SChris Mason * prealloc_dest -- if you have already reserved a destination for the cow, 235d397712bSChris Mason * this uses that block instead of allocating a new one. 236d397712bSChris Mason * btrfs_alloc_reserved_extent is used to finish the allocation. 237d352ac68SChris Mason */ 238d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, 2395f39d397SChris Mason struct btrfs_root *root, 2405f39d397SChris Mason struct extent_buffer *buf, 2415f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 2425f39d397SChris Mason struct extent_buffer **cow_ret, 24365b51a00SChris Mason u64 search_start, u64 empty_size, 24465b51a00SChris Mason u64 prealloc_dest) 2456702ed49SChris Mason { 24631840ae1SZheng Yan u64 parent_start; 2475f39d397SChris Mason struct extent_buffer *cow; 2487bb86316SChris Mason u32 nritems; 2496702ed49SChris Mason int ret = 0; 2507bb86316SChris Mason int level; 251925baeddSChris Mason int unlock_orig = 0; 2526702ed49SChris Mason 253925baeddSChris Mason if (*cow_ret == buf) 254925baeddSChris Mason unlock_orig = 1; 255925baeddSChris Mason 256925baeddSChris Mason WARN_ON(!btrfs_tree_locked(buf)); 257925baeddSChris Mason 25831840ae1SZheng Yan if (parent) 25931840ae1SZheng Yan parent_start = parent->start; 26031840ae1SZheng Yan else 26131840ae1SZheng Yan parent_start = 0; 26231840ae1SZheng Yan 2637bb86316SChris Mason WARN_ON(root->ref_cows && trans->transid != 2647bb86316SChris Mason root->fs_info->running_transaction->transid); 2656702ed49SChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 2665f39d397SChris Mason 2677bb86316SChris Mason level = btrfs_header_level(buf); 2687bb86316SChris Mason nritems = btrfs_header_nritems(buf); 26931840ae1SZheng Yan 27065b51a00SChris Mason if (prealloc_dest) { 27165b51a00SChris Mason struct btrfs_key ins; 27265b51a00SChris Mason 27365b51a00SChris Mason ins.objectid = prealloc_dest; 27465b51a00SChris Mason ins.offset = buf->len; 27565b51a00SChris Mason ins.type = BTRFS_EXTENT_ITEM_KEY; 27665b51a00SChris Mason 27731840ae1SZheng Yan ret = btrfs_alloc_reserved_extent(trans, root, parent_start, 27865b51a00SChris Mason root->root_key.objectid, 2793bb1a1bcSYan Zheng trans->transid, level, &ins); 28065b51a00SChris Mason BUG_ON(ret); 28165b51a00SChris Mason cow = btrfs_init_new_buffer(trans, root, prealloc_dest, 28265b51a00SChris Mason buf->len); 28365b51a00SChris Mason } else { 284925baeddSChris Mason cow = btrfs_alloc_free_block(trans, root, buf->len, 28531840ae1SZheng Yan parent_start, 2867bb86316SChris Mason root->root_key.objectid, 28731840ae1SZheng Yan trans->transid, level, 28831840ae1SZheng Yan search_start, empty_size); 28965b51a00SChris Mason } 2906702ed49SChris Mason if (IS_ERR(cow)) 2916702ed49SChris Mason return PTR_ERR(cow); 2926702ed49SChris Mason 293b4ce94deSChris Mason /* cow is set to blocking by btrfs_init_new_buffer */ 294b4ce94deSChris Mason 2955f39d397SChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 296db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 2975f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 2985f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 29963b10fc4SChris Mason btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 3006702ed49SChris Mason 3012b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 3022b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 3032b82032cSYan Zheng BTRFS_FSID_SIZE); 3042b82032cSYan Zheng 3055f39d397SChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 3065f39d397SChris Mason if (btrfs_header_generation(buf) != trans->transid) { 30731840ae1SZheng Yan u32 nr_extents; 30831840ae1SZheng Yan ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents); 3096702ed49SChris Mason if (ret) 3106702ed49SChris Mason return ret; 31131840ae1SZheng Yan 31231840ae1SZheng Yan ret = btrfs_cache_ref(trans, root, buf, nr_extents); 31331840ae1SZheng Yan WARN_ON(ret); 3141a40e23bSZheng Yan } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) { 3151a40e23bSZheng Yan /* 3161a40e23bSZheng Yan * There are only two places that can drop reference to 3171a40e23bSZheng Yan * tree blocks owned by living reloc trees, one is here, 318f82d02d9SYan Zheng * the other place is btrfs_drop_subtree. In both places, 3191a40e23bSZheng Yan * we check reference count while tree block is locked. 3201a40e23bSZheng Yan * Furthermore, if reference count is one, it won't get 3211a40e23bSZheng Yan * increased by someone else. 3221a40e23bSZheng Yan */ 3231a40e23bSZheng Yan u32 refs; 3241a40e23bSZheng Yan ret = btrfs_lookup_extent_ref(trans, root, buf->start, 3251a40e23bSZheng Yan buf->len, &refs); 3261a40e23bSZheng Yan BUG_ON(ret); 3271a40e23bSZheng Yan if (refs == 1) { 3281a40e23bSZheng Yan ret = btrfs_update_ref(trans, root, buf, cow, 3291a40e23bSZheng Yan 0, nritems); 3301a40e23bSZheng Yan clean_tree_block(trans, root, buf); 3311a40e23bSZheng Yan } else { 3321a40e23bSZheng Yan ret = btrfs_inc_ref(trans, root, buf, cow, NULL); 3331a40e23bSZheng Yan } 3341a40e23bSZheng Yan BUG_ON(ret); 3356702ed49SChris Mason } else { 33631840ae1SZheng Yan ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems); 33731840ae1SZheng Yan if (ret) 33831840ae1SZheng Yan return ret; 3396702ed49SChris Mason clean_tree_block(trans, root, buf); 3406702ed49SChris Mason } 3416702ed49SChris Mason 3421a40e23bSZheng Yan if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { 3431a40e23bSZheng Yan ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start); 3441a40e23bSZheng Yan WARN_ON(ret); 3451a40e23bSZheng Yan } 3461a40e23bSZheng Yan 3476702ed49SChris Mason if (buf == root->node) { 348925baeddSChris Mason WARN_ON(parent && parent != buf); 349925baeddSChris Mason 350925baeddSChris Mason spin_lock(&root->node_lock); 3516702ed49SChris Mason root->node = cow; 3525f39d397SChris Mason extent_buffer_get(cow); 353925baeddSChris Mason spin_unlock(&root->node_lock); 354925baeddSChris Mason 3556702ed49SChris Mason if (buf != root->commit_root) { 356db94535dSChris Mason btrfs_free_extent(trans, root, buf->start, 35731840ae1SZheng Yan buf->len, buf->start, 35831840ae1SZheng Yan root->root_key.objectid, 35931840ae1SZheng Yan btrfs_header_generation(buf), 3603bb1a1bcSYan Zheng level, 1); 3616702ed49SChris Mason } 3625f39d397SChris Mason free_extent_buffer(buf); 3630b86a832SChris Mason add_root_to_dirty_list(root); 3646702ed49SChris Mason } else { 3655f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 366db94535dSChris Mason cow->start); 36774493f7aSChris Mason WARN_ON(trans->transid == 0); 36874493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 36974493f7aSChris Mason trans->transid); 3706702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 3715f39d397SChris Mason WARN_ON(btrfs_header_generation(parent) != trans->transid); 3727bb86316SChris Mason btrfs_free_extent(trans, root, buf->start, buf->len, 37331840ae1SZheng Yan parent_start, btrfs_header_owner(parent), 3743bb1a1bcSYan Zheng btrfs_header_generation(parent), level, 1); 3756702ed49SChris Mason } 376925baeddSChris Mason if (unlock_orig) 377925baeddSChris Mason btrfs_tree_unlock(buf); 3785f39d397SChris Mason free_extent_buffer(buf); 3796702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 3806702ed49SChris Mason *cow_ret = cow; 3816702ed49SChris Mason return 0; 3826702ed49SChris Mason } 3836702ed49SChris Mason 384d352ac68SChris Mason /* 385d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 386d352ac68SChris Mason * This version of it has extra checks so that a block isn't cow'd more than 387d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 388d352ac68SChris Mason */ 389d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, 3905f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 3915f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 39265b51a00SChris Mason struct extent_buffer **cow_ret, u64 prealloc_dest) 39302217ed2SChris Mason { 3946702ed49SChris Mason u64 search_start; 395f510cfecSChris Mason int ret; 396dc17ff8fSChris Mason 397ccd467d6SChris Mason if (trans->transaction != root->fs_info->running_transaction) { 398d397712bSChris Mason printk(KERN_CRIT "trans %llu running %llu\n", 399d397712bSChris Mason (unsigned long long)trans->transid, 400d397712bSChris Mason (unsigned long long) 401ccd467d6SChris Mason root->fs_info->running_transaction->transid); 402ccd467d6SChris Mason WARN_ON(1); 403ccd467d6SChris Mason } 404ccd467d6SChris Mason if (trans->transid != root->fs_info->generation) { 405d397712bSChris Mason printk(KERN_CRIT "trans %llu running %llu\n", 406d397712bSChris Mason (unsigned long long)trans->transid, 407d397712bSChris Mason (unsigned long long)root->fs_info->generation); 408ccd467d6SChris Mason WARN_ON(1); 409ccd467d6SChris Mason } 410dc17ff8fSChris Mason 4115b21f2edSZheng Yan if (btrfs_header_generation(buf) == trans->transid && 4125b21f2edSZheng Yan btrfs_header_owner(buf) == root->root_key.objectid && 41363b10fc4SChris Mason !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 41402217ed2SChris Mason *cow_ret = buf; 41565b51a00SChris Mason WARN_ON(prealloc_dest); 41602217ed2SChris Mason return 0; 41702217ed2SChris Mason } 418c487685dSChris Mason 4190b86a832SChris Mason search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 420b4ce94deSChris Mason 421b4ce94deSChris Mason if (parent) 422b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 423b4ce94deSChris Mason btrfs_set_lock_blocking(buf); 424b4ce94deSChris Mason 425f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 42665b51a00SChris Mason parent_slot, cow_ret, search_start, 0, 42765b51a00SChris Mason prealloc_dest); 428f510cfecSChris Mason return ret; 4292c90e5d6SChris Mason } 4306702ed49SChris Mason 431d352ac68SChris Mason /* 432d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 433d352ac68SChris Mason * node are actually close by 434d352ac68SChris Mason */ 4356b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 4366702ed49SChris Mason { 4376b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 4386702ed49SChris Mason return 1; 4396b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 4406702ed49SChris Mason return 1; 44102217ed2SChris Mason return 0; 44202217ed2SChris Mason } 44302217ed2SChris Mason 444081e9573SChris Mason /* 445081e9573SChris Mason * compare two keys in a memcmp fashion 446081e9573SChris Mason */ 447081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) 448081e9573SChris Mason { 449081e9573SChris Mason struct btrfs_key k1; 450081e9573SChris Mason 451081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 452081e9573SChris Mason 453081e9573SChris Mason if (k1.objectid > k2->objectid) 454081e9573SChris Mason return 1; 455081e9573SChris Mason if (k1.objectid < k2->objectid) 456081e9573SChris Mason return -1; 457081e9573SChris Mason if (k1.type > k2->type) 458081e9573SChris Mason return 1; 459081e9573SChris Mason if (k1.type < k2->type) 460081e9573SChris Mason return -1; 461081e9573SChris Mason if (k1.offset > k2->offset) 462081e9573SChris Mason return 1; 463081e9573SChris Mason if (k1.offset < k2->offset) 464081e9573SChris Mason return -1; 465081e9573SChris Mason return 0; 466081e9573SChris Mason } 467081e9573SChris Mason 468f3465ca4SJosef Bacik /* 469f3465ca4SJosef Bacik * same as comp_keys only with two btrfs_key's 470f3465ca4SJosef Bacik */ 471f3465ca4SJosef Bacik static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2) 472f3465ca4SJosef Bacik { 473f3465ca4SJosef Bacik if (k1->objectid > k2->objectid) 474f3465ca4SJosef Bacik return 1; 475f3465ca4SJosef Bacik if (k1->objectid < k2->objectid) 476f3465ca4SJosef Bacik return -1; 477f3465ca4SJosef Bacik if (k1->type > k2->type) 478f3465ca4SJosef Bacik return 1; 479f3465ca4SJosef Bacik if (k1->type < k2->type) 480f3465ca4SJosef Bacik return -1; 481f3465ca4SJosef Bacik if (k1->offset > k2->offset) 482f3465ca4SJosef Bacik return 1; 483f3465ca4SJosef Bacik if (k1->offset < k2->offset) 484f3465ca4SJosef Bacik return -1; 485f3465ca4SJosef Bacik return 0; 486f3465ca4SJosef Bacik } 487081e9573SChris Mason 488d352ac68SChris Mason /* 489d352ac68SChris Mason * this is used by the defrag code to go through all the 490d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 491d352ac68SChris Mason * disk order is close to key order 492d352ac68SChris Mason */ 4936702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 4945f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 495a6b6e75eSChris Mason int start_slot, int cache_only, u64 *last_ret, 496a6b6e75eSChris Mason struct btrfs_key *progress) 4976702ed49SChris Mason { 4986b80053dSChris Mason struct extent_buffer *cur; 4996702ed49SChris Mason u64 blocknr; 500ca7a79adSChris Mason u64 gen; 501e9d0b13bSChris Mason u64 search_start = *last_ret; 502e9d0b13bSChris Mason u64 last_block = 0; 5036702ed49SChris Mason u64 other; 5046702ed49SChris Mason u32 parent_nritems; 5056702ed49SChris Mason int end_slot; 5066702ed49SChris Mason int i; 5076702ed49SChris Mason int err = 0; 508f2183bdeSChris Mason int parent_level; 5096b80053dSChris Mason int uptodate; 5106b80053dSChris Mason u32 blocksize; 511081e9573SChris Mason int progress_passed = 0; 512081e9573SChris Mason struct btrfs_disk_key disk_key; 5136702ed49SChris Mason 5145708b959SChris Mason parent_level = btrfs_header_level(parent); 5155708b959SChris Mason if (cache_only && parent_level != 1) 5165708b959SChris Mason return 0; 5175708b959SChris Mason 518d397712bSChris Mason if (trans->transaction != root->fs_info->running_transaction) 5196702ed49SChris Mason WARN_ON(1); 520d397712bSChris Mason if (trans->transid != root->fs_info->generation) 5216702ed49SChris Mason WARN_ON(1); 52286479a04SChris Mason 5236b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 5246b80053dSChris Mason blocksize = btrfs_level_size(root, parent_level - 1); 5256702ed49SChris Mason end_slot = parent_nritems; 5266702ed49SChris Mason 5276702ed49SChris Mason if (parent_nritems == 1) 5286702ed49SChris Mason return 0; 5296702ed49SChris Mason 530b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 531b4ce94deSChris Mason 5326702ed49SChris Mason for (i = start_slot; i < end_slot; i++) { 5336702ed49SChris Mason int close = 1; 534a6b6e75eSChris Mason 5355708b959SChris Mason if (!parent->map_token) { 5365708b959SChris Mason map_extent_buffer(parent, 5375708b959SChris Mason btrfs_node_key_ptr_offset(i), 5385708b959SChris Mason sizeof(struct btrfs_key_ptr), 5395708b959SChris Mason &parent->map_token, &parent->kaddr, 5405708b959SChris Mason &parent->map_start, &parent->map_len, 5415708b959SChris Mason KM_USER1); 5425708b959SChris Mason } 543081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 544081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 545081e9573SChris Mason continue; 546081e9573SChris Mason 547081e9573SChris Mason progress_passed = 1; 5486b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 549ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 550e9d0b13bSChris Mason if (last_block == 0) 551e9d0b13bSChris Mason last_block = blocknr; 5525708b959SChris Mason 5536702ed49SChris Mason if (i > 0) { 5546b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 5556b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 5566702ed49SChris Mason } 5570ef3e66bSChris Mason if (!close && i < end_slot - 2) { 5586b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 5596b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 5606702ed49SChris Mason } 561e9d0b13bSChris Mason if (close) { 562e9d0b13bSChris Mason last_block = blocknr; 5636702ed49SChris Mason continue; 564e9d0b13bSChris Mason } 5655708b959SChris Mason if (parent->map_token) { 5665708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 5675708b959SChris Mason KM_USER1); 5685708b959SChris Mason parent->map_token = NULL; 5695708b959SChris Mason } 5706702ed49SChris Mason 5716b80053dSChris Mason cur = btrfs_find_tree_block(root, blocknr, blocksize); 5726b80053dSChris Mason if (cur) 5731259ab75SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen); 5746b80053dSChris Mason else 5756b80053dSChris Mason uptodate = 0; 5765708b959SChris Mason if (!cur || !uptodate) { 5776702ed49SChris Mason if (cache_only) { 5786b80053dSChris Mason free_extent_buffer(cur); 5796702ed49SChris Mason continue; 5806702ed49SChris Mason } 5816b80053dSChris Mason if (!cur) { 5826b80053dSChris Mason cur = read_tree_block(root, blocknr, 583ca7a79adSChris Mason blocksize, gen); 5846b80053dSChris Mason } else if (!uptodate) { 585ca7a79adSChris Mason btrfs_read_buffer(cur, gen); 5866702ed49SChris Mason } 587f2183bdeSChris Mason } 588e9d0b13bSChris Mason if (search_start == 0) 5896b80053dSChris Mason search_start = last_block; 590e9d0b13bSChris Mason 591e7a84565SChris Mason btrfs_tree_lock(cur); 592b4ce94deSChris Mason btrfs_set_lock_blocking(cur); 5936b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 594e7a84565SChris Mason &cur, search_start, 5956b80053dSChris Mason min(16 * blocksize, 59665b51a00SChris Mason (end_slot - i) * blocksize), 0); 597252c38f0SYan if (err) { 598e7a84565SChris Mason btrfs_tree_unlock(cur); 5996b80053dSChris Mason free_extent_buffer(cur); 6006702ed49SChris Mason break; 601252c38f0SYan } 602e7a84565SChris Mason search_start = cur->start; 603e7a84565SChris Mason last_block = cur->start; 604f2183bdeSChris Mason *last_ret = search_start; 605e7a84565SChris Mason btrfs_tree_unlock(cur); 606e7a84565SChris Mason free_extent_buffer(cur); 6076702ed49SChris Mason } 6085708b959SChris Mason if (parent->map_token) { 6095708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 6105708b959SChris Mason KM_USER1); 6115708b959SChris Mason parent->map_token = NULL; 6125708b959SChris Mason } 6136702ed49SChris Mason return err; 6146702ed49SChris Mason } 6156702ed49SChris Mason 61674123bd7SChris Mason /* 61774123bd7SChris Mason * The leaf data grows from end-to-front in the node. 61874123bd7SChris Mason * this returns the address of the start of the last item, 61974123bd7SChris Mason * which is the stop of the leaf data stack 62074123bd7SChris Mason */ 621123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root, 6225f39d397SChris Mason struct extent_buffer *leaf) 623be0e5c09SChris Mason { 6245f39d397SChris Mason u32 nr = btrfs_header_nritems(leaf); 625be0e5c09SChris Mason if (nr == 0) 626123abc88SChris Mason return BTRFS_LEAF_DATA_SIZE(root); 6275f39d397SChris Mason return btrfs_item_offset_nr(leaf, nr - 1); 628be0e5c09SChris Mason } 629be0e5c09SChris Mason 630d352ac68SChris Mason /* 631d352ac68SChris Mason * extra debugging checks to make sure all the items in a key are 632d352ac68SChris Mason * well formed and in the proper order 633d352ac68SChris Mason */ 634123abc88SChris Mason static int check_node(struct btrfs_root *root, struct btrfs_path *path, 635123abc88SChris Mason int level) 636aa5d6bedSChris Mason { 6375f39d397SChris Mason struct extent_buffer *parent = NULL; 6385f39d397SChris Mason struct extent_buffer *node = path->nodes[level]; 6395f39d397SChris Mason struct btrfs_disk_key parent_key; 6405f39d397SChris Mason struct btrfs_disk_key node_key; 641aa5d6bedSChris Mason int parent_slot; 6428d7be552SChris Mason int slot; 6438d7be552SChris Mason struct btrfs_key cpukey; 6445f39d397SChris Mason u32 nritems = btrfs_header_nritems(node); 645aa5d6bedSChris Mason 646aa5d6bedSChris Mason if (path->nodes[level + 1]) 6475f39d397SChris Mason parent = path->nodes[level + 1]; 648a1f39630SAneesh 6498d7be552SChris Mason slot = path->slots[level]; 6507518a238SChris Mason BUG_ON(nritems == 0); 6517518a238SChris Mason if (parent) { 652a1f39630SAneesh parent_slot = path->slots[level + 1]; 6535f39d397SChris Mason btrfs_node_key(parent, &parent_key, parent_slot); 6545f39d397SChris Mason btrfs_node_key(node, &node_key, 0); 6555f39d397SChris Mason BUG_ON(memcmp(&parent_key, &node_key, 656e2fa7227SChris Mason sizeof(struct btrfs_disk_key))); 6571d4f8a0cSChris Mason BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 658db94535dSChris Mason btrfs_header_bytenr(node)); 659aa5d6bedSChris Mason } 660123abc88SChris Mason BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); 6618d7be552SChris Mason if (slot != 0) { 6625f39d397SChris Mason btrfs_node_key_to_cpu(node, &cpukey, slot - 1); 6635f39d397SChris Mason btrfs_node_key(node, &node_key, slot); 6645f39d397SChris Mason BUG_ON(comp_keys(&node_key, &cpukey) <= 0); 6658d7be552SChris Mason } 6668d7be552SChris Mason if (slot < nritems - 1) { 6675f39d397SChris Mason btrfs_node_key_to_cpu(node, &cpukey, slot + 1); 6685f39d397SChris Mason btrfs_node_key(node, &node_key, slot); 6695f39d397SChris Mason BUG_ON(comp_keys(&node_key, &cpukey) >= 0); 670aa5d6bedSChris Mason } 671aa5d6bedSChris Mason return 0; 672aa5d6bedSChris Mason } 673aa5d6bedSChris Mason 674d352ac68SChris Mason /* 675d352ac68SChris Mason * extra checking to make sure all the items in a leaf are 676d352ac68SChris Mason * well formed and in the proper order 677d352ac68SChris Mason */ 678123abc88SChris Mason static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, 679123abc88SChris Mason int level) 680aa5d6bedSChris Mason { 6815f39d397SChris Mason struct extent_buffer *leaf = path->nodes[level]; 6825f39d397SChris Mason struct extent_buffer *parent = NULL; 683aa5d6bedSChris Mason int parent_slot; 6848d7be552SChris Mason struct btrfs_key cpukey; 6855f39d397SChris Mason struct btrfs_disk_key parent_key; 6865f39d397SChris Mason struct btrfs_disk_key leaf_key; 6875f39d397SChris Mason int slot = path->slots[0]; 6888d7be552SChris Mason 6895f39d397SChris Mason u32 nritems = btrfs_header_nritems(leaf); 690aa5d6bedSChris Mason 691aa5d6bedSChris Mason if (path->nodes[level + 1]) 6925f39d397SChris Mason parent = path->nodes[level + 1]; 6937518a238SChris Mason 6947518a238SChris Mason if (nritems == 0) 6957518a238SChris Mason return 0; 6967518a238SChris Mason 6977518a238SChris Mason if (parent) { 698a1f39630SAneesh parent_slot = path->slots[level + 1]; 6995f39d397SChris Mason btrfs_node_key(parent, &parent_key, parent_slot); 7005f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, 0); 7016702ed49SChris Mason 7025f39d397SChris Mason BUG_ON(memcmp(&parent_key, &leaf_key, 703e2fa7227SChris Mason sizeof(struct btrfs_disk_key))); 7041d4f8a0cSChris Mason BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 705db94535dSChris Mason btrfs_header_bytenr(leaf)); 706aa5d6bedSChris Mason } 7075f39d397SChris Mason if (slot != 0 && slot < nritems - 1) { 7085f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, slot); 7095f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1); 7105f39d397SChris Mason if (comp_keys(&leaf_key, &cpukey) <= 0) { 7115f39d397SChris Mason btrfs_print_leaf(root, leaf); 712d397712bSChris Mason printk(KERN_CRIT "slot %d offset bad key\n", slot); 7135f39d397SChris Mason BUG_ON(1); 7145f39d397SChris Mason } 7155f39d397SChris Mason if (btrfs_item_offset_nr(leaf, slot - 1) != 7165f39d397SChris Mason btrfs_item_end_nr(leaf, slot)) { 7175f39d397SChris Mason btrfs_print_leaf(root, leaf); 718d397712bSChris Mason printk(KERN_CRIT "slot %d offset bad\n", slot); 7195f39d397SChris Mason BUG_ON(1); 7205f39d397SChris Mason } 721aa5d6bedSChris Mason } 7228d7be552SChris Mason if (slot < nritems - 1) { 7235f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, slot); 7245f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1); 7255f39d397SChris Mason BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0); 7265f39d397SChris Mason if (btrfs_item_offset_nr(leaf, slot) != 7275f39d397SChris Mason btrfs_item_end_nr(leaf, slot + 1)) { 7285f39d397SChris Mason btrfs_print_leaf(root, leaf); 729d397712bSChris Mason printk(KERN_CRIT "slot %d offset bad\n", slot); 7305f39d397SChris Mason BUG_ON(1); 731aa5d6bedSChris Mason } 7325f39d397SChris Mason } 7335f39d397SChris Mason BUG_ON(btrfs_item_offset_nr(leaf, 0) + 7345f39d397SChris Mason btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root)); 735aa5d6bedSChris Mason return 0; 736aa5d6bedSChris Mason } 737aa5d6bedSChris Mason 738d397712bSChris Mason static noinline int check_block(struct btrfs_root *root, 73998ed5174SChris Mason struct btrfs_path *path, int level) 740aa5d6bedSChris Mason { 74185d824c4SChris Mason return 0; 742aa5d6bedSChris Mason if (level == 0) 743123abc88SChris Mason return check_leaf(root, path, level); 744123abc88SChris Mason return check_node(root, path, level); 745aa5d6bedSChris Mason } 746aa5d6bedSChris Mason 74774123bd7SChris Mason /* 7485f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 7495f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 7505f39d397SChris Mason * 75174123bd7SChris Mason * the slot in the array is returned via slot, and it points to 75274123bd7SChris Mason * the place where you would insert key if it is not found in 75374123bd7SChris Mason * the array. 75474123bd7SChris Mason * 75574123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 75674123bd7SChris Mason */ 757e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb, 758e02119d5SChris Mason unsigned long p, 7595f39d397SChris Mason int item_size, struct btrfs_key *key, 760be0e5c09SChris Mason int max, int *slot) 761be0e5c09SChris Mason { 762be0e5c09SChris Mason int low = 0; 763be0e5c09SChris Mason int high = max; 764be0e5c09SChris Mason int mid; 765be0e5c09SChris Mason int ret; 766479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 7675f39d397SChris Mason struct btrfs_disk_key unaligned; 7685f39d397SChris Mason unsigned long offset; 7695f39d397SChris Mason char *map_token = NULL; 7705f39d397SChris Mason char *kaddr = NULL; 7715f39d397SChris Mason unsigned long map_start = 0; 7725f39d397SChris Mason unsigned long map_len = 0; 773479965d6SChris Mason int err; 774be0e5c09SChris Mason 775be0e5c09SChris Mason while (low < high) { 776be0e5c09SChris Mason mid = (low + high) / 2; 7775f39d397SChris Mason offset = p + mid * item_size; 7785f39d397SChris Mason 7795f39d397SChris Mason if (!map_token || offset < map_start || 7805f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 7815f39d397SChris Mason map_start + map_len) { 782479965d6SChris Mason if (map_token) { 7835f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 784479965d6SChris Mason map_token = NULL; 785479965d6SChris Mason } 786934d375bSChris Mason 787934d375bSChris Mason err = map_private_extent_buffer(eb, offset, 788479965d6SChris Mason sizeof(struct btrfs_disk_key), 789479965d6SChris Mason &map_token, &kaddr, 7905f39d397SChris Mason &map_start, &map_len, KM_USER0); 7915f39d397SChris Mason 792479965d6SChris Mason if (!err) { 793479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 794479965d6SChris Mason map_start); 795479965d6SChris Mason } else { 7965f39d397SChris Mason read_extent_buffer(eb, &unaligned, 7975f39d397SChris Mason offset, sizeof(unaligned)); 7985f39d397SChris Mason tmp = &unaligned; 799479965d6SChris Mason } 800479965d6SChris Mason 8015f39d397SChris Mason } else { 8025f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 8035f39d397SChris Mason map_start); 8045f39d397SChris Mason } 805be0e5c09SChris Mason ret = comp_keys(tmp, key); 806be0e5c09SChris Mason 807be0e5c09SChris Mason if (ret < 0) 808be0e5c09SChris Mason low = mid + 1; 809be0e5c09SChris Mason else if (ret > 0) 810be0e5c09SChris Mason high = mid; 811be0e5c09SChris Mason else { 812be0e5c09SChris Mason *slot = mid; 813479965d6SChris Mason if (map_token) 8145f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 815be0e5c09SChris Mason return 0; 816be0e5c09SChris Mason } 817be0e5c09SChris Mason } 818be0e5c09SChris Mason *slot = low; 8195f39d397SChris Mason if (map_token) 8205f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 821be0e5c09SChris Mason return 1; 822be0e5c09SChris Mason } 823be0e5c09SChris Mason 82497571fd0SChris Mason /* 82597571fd0SChris Mason * simple bin_search frontend that does the right thing for 82697571fd0SChris Mason * leaves vs nodes 82797571fd0SChris Mason */ 8285f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, 8295f39d397SChris Mason int level, int *slot) 830be0e5c09SChris Mason { 8315f39d397SChris Mason if (level == 0) { 8325f39d397SChris Mason return generic_bin_search(eb, 8335f39d397SChris Mason offsetof(struct btrfs_leaf, items), 8340783fcfcSChris Mason sizeof(struct btrfs_item), 8355f39d397SChris Mason key, btrfs_header_nritems(eb), 8367518a238SChris Mason slot); 837be0e5c09SChris Mason } else { 8385f39d397SChris Mason return generic_bin_search(eb, 8395f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 840123abc88SChris Mason sizeof(struct btrfs_key_ptr), 8415f39d397SChris Mason key, btrfs_header_nritems(eb), 8427518a238SChris Mason slot); 843be0e5c09SChris Mason } 844be0e5c09SChris Mason return -1; 845be0e5c09SChris Mason } 846be0e5c09SChris Mason 847d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 848d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 849d352ac68SChris Mason * NULL is returned on error. 850d352ac68SChris Mason */ 851e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, 8525f39d397SChris Mason struct extent_buffer *parent, int slot) 853bb803951SChris Mason { 854ca7a79adSChris Mason int level = btrfs_header_level(parent); 855bb803951SChris Mason if (slot < 0) 856bb803951SChris Mason return NULL; 8575f39d397SChris Mason if (slot >= btrfs_header_nritems(parent)) 858bb803951SChris Mason return NULL; 859ca7a79adSChris Mason 860ca7a79adSChris Mason BUG_ON(level == 0); 861ca7a79adSChris Mason 862db94535dSChris Mason return read_tree_block(root, btrfs_node_blockptr(parent, slot), 863ca7a79adSChris Mason btrfs_level_size(root, level - 1), 864ca7a79adSChris Mason btrfs_node_ptr_generation(parent, slot)); 865bb803951SChris Mason } 866bb803951SChris Mason 867d352ac68SChris Mason /* 868d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 869d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 870d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 871d352ac68SChris Mason */ 872e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 87398ed5174SChris Mason struct btrfs_root *root, 87498ed5174SChris Mason struct btrfs_path *path, int level) 875bb803951SChris Mason { 8765f39d397SChris Mason struct extent_buffer *right = NULL; 8775f39d397SChris Mason struct extent_buffer *mid; 8785f39d397SChris Mason struct extent_buffer *left = NULL; 8795f39d397SChris Mason struct extent_buffer *parent = NULL; 880bb803951SChris Mason int ret = 0; 881bb803951SChris Mason int wret; 882bb803951SChris Mason int pslot; 883bb803951SChris Mason int orig_slot = path->slots[level]; 88454aa1f4dSChris Mason int err_on_enospc = 0; 88579f95c82SChris Mason u64 orig_ptr; 886bb803951SChris Mason 887bb803951SChris Mason if (level == 0) 888bb803951SChris Mason return 0; 889bb803951SChris Mason 8905f39d397SChris Mason mid = path->nodes[level]; 891b4ce94deSChris Mason 892925baeddSChris Mason WARN_ON(!path->locks[level]); 8937bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 8947bb86316SChris Mason 8951d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 89679f95c82SChris Mason 897234b63a0SChris Mason if (level < BTRFS_MAX_LEVEL - 1) 8985f39d397SChris Mason parent = path->nodes[level + 1]; 899bb803951SChris Mason pslot = path->slots[level + 1]; 900bb803951SChris Mason 90140689478SChris Mason /* 90240689478SChris Mason * deal with the case where there is only one pointer in the root 90340689478SChris Mason * by promoting the node below to a root 90440689478SChris Mason */ 9055f39d397SChris Mason if (!parent) { 9065f39d397SChris Mason struct extent_buffer *child; 907bb803951SChris Mason 9085f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 909bb803951SChris Mason return 0; 910bb803951SChris Mason 911bb803951SChris Mason /* promote the child to a root */ 9125f39d397SChris Mason child = read_node_slot(root, mid, 0); 9137951f3ceSJeff Mahoney BUG_ON(!child); 914925baeddSChris Mason btrfs_tree_lock(child); 915b4ce94deSChris Mason btrfs_set_lock_blocking(child); 91665b51a00SChris Mason ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0); 9172f375ab9SYan BUG_ON(ret); 9182f375ab9SYan 919925baeddSChris Mason spin_lock(&root->node_lock); 920bb803951SChris Mason root->node = child; 921925baeddSChris Mason spin_unlock(&root->node_lock); 922925baeddSChris Mason 92331840ae1SZheng Yan ret = btrfs_update_extent_ref(trans, root, child->start, 92431840ae1SZheng Yan mid->start, child->start, 92531840ae1SZheng Yan root->root_key.objectid, 9263bb1a1bcSYan Zheng trans->transid, level - 1); 92731840ae1SZheng Yan BUG_ON(ret); 92831840ae1SZheng Yan 9290b86a832SChris Mason add_root_to_dirty_list(root); 930925baeddSChris Mason btrfs_tree_unlock(child); 931b4ce94deSChris Mason 932925baeddSChris Mason path->locks[level] = 0; 933bb803951SChris Mason path->nodes[level] = NULL; 9345f39d397SChris Mason clean_tree_block(trans, root, mid); 935925baeddSChris Mason btrfs_tree_unlock(mid); 936bb803951SChris Mason /* once for the path */ 9375f39d397SChris Mason free_extent_buffer(mid); 9387bb86316SChris Mason ret = btrfs_free_extent(trans, root, mid->start, mid->len, 93931840ae1SZheng Yan mid->start, root->root_key.objectid, 9403bb1a1bcSYan Zheng btrfs_header_generation(mid), 9413bb1a1bcSYan Zheng level, 1); 942bb803951SChris Mason /* once for the root ptr */ 9435f39d397SChris Mason free_extent_buffer(mid); 944db94535dSChris Mason return ret; 945bb803951SChris Mason } 9465f39d397SChris Mason if (btrfs_header_nritems(mid) > 947123abc88SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 4) 948bb803951SChris Mason return 0; 949bb803951SChris Mason 9505f39d397SChris Mason if (btrfs_header_nritems(mid) < 2) 95154aa1f4dSChris Mason err_on_enospc = 1; 95254aa1f4dSChris Mason 9535f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 9545f39d397SChris Mason if (left) { 955925baeddSChris Mason btrfs_tree_lock(left); 956b4ce94deSChris Mason btrfs_set_lock_blocking(left); 9575f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 95865b51a00SChris Mason parent, pslot - 1, &left, 0); 95954aa1f4dSChris Mason if (wret) { 96054aa1f4dSChris Mason ret = wret; 96154aa1f4dSChris Mason goto enospc; 96254aa1f4dSChris Mason } 9632cc58cf2SChris Mason } 9645f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 9655f39d397SChris Mason if (right) { 966925baeddSChris Mason btrfs_tree_lock(right); 967b4ce94deSChris Mason btrfs_set_lock_blocking(right); 9685f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 96965b51a00SChris Mason parent, pslot + 1, &right, 0); 9702cc58cf2SChris Mason if (wret) { 9712cc58cf2SChris Mason ret = wret; 9722cc58cf2SChris Mason goto enospc; 9732cc58cf2SChris Mason } 9742cc58cf2SChris Mason } 9752cc58cf2SChris Mason 9762cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 9775f39d397SChris Mason if (left) { 9785f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 979bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 98079f95c82SChris Mason if (wret < 0) 98179f95c82SChris Mason ret = wret; 9825f39d397SChris Mason if (btrfs_header_nritems(mid) < 2) 98354aa1f4dSChris Mason err_on_enospc = 1; 984bb803951SChris Mason } 98579f95c82SChris Mason 98679f95c82SChris Mason /* 98779f95c82SChris Mason * then try to empty the right most buffer into the middle 98879f95c82SChris Mason */ 9895f39d397SChris Mason if (right) { 990971a1f66SChris Mason wret = push_node_left(trans, root, mid, right, 1); 99154aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 99279f95c82SChris Mason ret = wret; 9935f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 994db94535dSChris Mason u64 bytenr = right->start; 9957bb86316SChris Mason u64 generation = btrfs_header_generation(parent); 996db94535dSChris Mason u32 blocksize = right->len; 997db94535dSChris Mason 9985f39d397SChris Mason clean_tree_block(trans, root, right); 999925baeddSChris Mason btrfs_tree_unlock(right); 10005f39d397SChris Mason free_extent_buffer(right); 1001bb803951SChris Mason right = NULL; 1002e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot + 1003e089f05cSChris Mason 1); 1004bb803951SChris Mason if (wret) 1005bb803951SChris Mason ret = wret; 1006db94535dSChris Mason wret = btrfs_free_extent(trans, root, bytenr, 100731840ae1SZheng Yan blocksize, parent->start, 10087bb86316SChris Mason btrfs_header_owner(parent), 10093bb1a1bcSYan Zheng generation, level, 1); 1010bb803951SChris Mason if (wret) 1011bb803951SChris Mason ret = wret; 1012bb803951SChris Mason } else { 10135f39d397SChris Mason struct btrfs_disk_key right_key; 10145f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 10155f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 10165f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1017bb803951SChris Mason } 1018bb803951SChris Mason } 10195f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 102079f95c82SChris Mason /* 102179f95c82SChris Mason * we're not allowed to leave a node with one item in the 102279f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 102379f95c82SChris Mason * could try to delete the only pointer in this node. 102479f95c82SChris Mason * So, pull some keys from the left. 102579f95c82SChris Mason * There has to be a left pointer at this point because 102679f95c82SChris Mason * otherwise we would have pulled some pointers from the 102779f95c82SChris Mason * right 102879f95c82SChris Mason */ 10295f39d397SChris Mason BUG_ON(!left); 10305f39d397SChris Mason wret = balance_node_right(trans, root, mid, left); 103154aa1f4dSChris Mason if (wret < 0) { 103279f95c82SChris Mason ret = wret; 103354aa1f4dSChris Mason goto enospc; 103454aa1f4dSChris Mason } 1035bce4eae9SChris Mason if (wret == 1) { 1036bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 1037bce4eae9SChris Mason if (wret < 0) 1038bce4eae9SChris Mason ret = wret; 1039bce4eae9SChris Mason } 104079f95c82SChris Mason BUG_ON(wret == 1); 104179f95c82SChris Mason } 10425f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 104379f95c82SChris Mason /* we've managed to empty the middle node, drop it */ 10447bb86316SChris Mason u64 root_gen = btrfs_header_generation(parent); 1045db94535dSChris Mason u64 bytenr = mid->start; 1046db94535dSChris Mason u32 blocksize = mid->len; 1047925baeddSChris Mason 10485f39d397SChris Mason clean_tree_block(trans, root, mid); 1049925baeddSChris Mason btrfs_tree_unlock(mid); 10505f39d397SChris Mason free_extent_buffer(mid); 1051bb803951SChris Mason mid = NULL; 1052e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot); 1053bb803951SChris Mason if (wret) 1054bb803951SChris Mason ret = wret; 10557bb86316SChris Mason wret = btrfs_free_extent(trans, root, bytenr, blocksize, 105631840ae1SZheng Yan parent->start, 10577bb86316SChris Mason btrfs_header_owner(parent), 10583bb1a1bcSYan Zheng root_gen, level, 1); 1059bb803951SChris Mason if (wret) 1060bb803951SChris Mason ret = wret; 106179f95c82SChris Mason } else { 106279f95c82SChris Mason /* update the parent key to reflect our changes */ 10635f39d397SChris Mason struct btrfs_disk_key mid_key; 10645f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 10655f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 10665f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 106779f95c82SChris Mason } 1068bb803951SChris Mason 106979f95c82SChris Mason /* update the path */ 10705f39d397SChris Mason if (left) { 10715f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 10725f39d397SChris Mason extent_buffer_get(left); 1073925baeddSChris Mason /* left was locked after cow */ 10745f39d397SChris Mason path->nodes[level] = left; 1075bb803951SChris Mason path->slots[level + 1] -= 1; 1076bb803951SChris Mason path->slots[level] = orig_slot; 1077925baeddSChris Mason if (mid) { 1078925baeddSChris Mason btrfs_tree_unlock(mid); 10795f39d397SChris Mason free_extent_buffer(mid); 1080925baeddSChris Mason } 1081bb803951SChris Mason } else { 10825f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 1083bb803951SChris Mason path->slots[level] = orig_slot; 1084bb803951SChris Mason } 1085bb803951SChris Mason } 108679f95c82SChris Mason /* double check we haven't messed things up */ 1087123abc88SChris Mason check_block(root, path, level); 1088e20d96d6SChris Mason if (orig_ptr != 10895f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 109079f95c82SChris Mason BUG(); 109154aa1f4dSChris Mason enospc: 1092925baeddSChris Mason if (right) { 1093925baeddSChris Mason btrfs_tree_unlock(right); 10945f39d397SChris Mason free_extent_buffer(right); 1095925baeddSChris Mason } 1096925baeddSChris Mason if (left) { 1097925baeddSChris Mason if (path->nodes[level] != left) 1098925baeddSChris Mason btrfs_tree_unlock(left); 10995f39d397SChris Mason free_extent_buffer(left); 1100925baeddSChris Mason } 1101bb803951SChris Mason return ret; 1102bb803951SChris Mason } 1103bb803951SChris Mason 1104d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 1105d352ac68SChris Mason * when they are completely full. This is also done top down, so we 1106d352ac68SChris Mason * have to be pessimistic. 1107d352ac68SChris Mason */ 1108d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, 1109e66f709bSChris Mason struct btrfs_root *root, 1110e66f709bSChris Mason struct btrfs_path *path, int level) 1111e66f709bSChris Mason { 11125f39d397SChris Mason struct extent_buffer *right = NULL; 11135f39d397SChris Mason struct extent_buffer *mid; 11145f39d397SChris Mason struct extent_buffer *left = NULL; 11155f39d397SChris Mason struct extent_buffer *parent = NULL; 1116e66f709bSChris Mason int ret = 0; 1117e66f709bSChris Mason int wret; 1118e66f709bSChris Mason int pslot; 1119e66f709bSChris Mason int orig_slot = path->slots[level]; 1120e66f709bSChris Mason u64 orig_ptr; 1121e66f709bSChris Mason 1122e66f709bSChris Mason if (level == 0) 1123e66f709bSChris Mason return 1; 1124e66f709bSChris Mason 11255f39d397SChris Mason mid = path->nodes[level]; 11267bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 1127e66f709bSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 1128e66f709bSChris Mason 1129e66f709bSChris Mason if (level < BTRFS_MAX_LEVEL - 1) 11305f39d397SChris Mason parent = path->nodes[level + 1]; 1131e66f709bSChris Mason pslot = path->slots[level + 1]; 1132e66f709bSChris Mason 11335f39d397SChris Mason if (!parent) 1134e66f709bSChris Mason return 1; 1135e66f709bSChris Mason 11365f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 1137e66f709bSChris Mason 1138e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 11395f39d397SChris Mason if (left) { 1140e66f709bSChris Mason u32 left_nr; 1141925baeddSChris Mason 1142925baeddSChris Mason btrfs_tree_lock(left); 1143b4ce94deSChris Mason btrfs_set_lock_blocking(left); 1144b4ce94deSChris Mason 11455f39d397SChris Mason left_nr = btrfs_header_nritems(left); 114633ade1f8SChris Mason if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 114733ade1f8SChris Mason wret = 1; 114833ade1f8SChris Mason } else { 11495f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 115065b51a00SChris Mason pslot - 1, &left, 0); 115154aa1f4dSChris Mason if (ret) 115254aa1f4dSChris Mason wret = 1; 115354aa1f4dSChris Mason else { 115454aa1f4dSChris Mason wret = push_node_left(trans, root, 1155971a1f66SChris Mason left, mid, 0); 115654aa1f4dSChris Mason } 115733ade1f8SChris Mason } 1158e66f709bSChris Mason if (wret < 0) 1159e66f709bSChris Mason ret = wret; 1160e66f709bSChris Mason if (wret == 0) { 11615f39d397SChris Mason struct btrfs_disk_key disk_key; 1162e66f709bSChris Mason orig_slot += left_nr; 11635f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 11645f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 11655f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 11665f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 11675f39d397SChris Mason path->nodes[level] = left; 1168e66f709bSChris Mason path->slots[level + 1] -= 1; 1169e66f709bSChris Mason path->slots[level] = orig_slot; 1170925baeddSChris Mason btrfs_tree_unlock(mid); 11715f39d397SChris Mason free_extent_buffer(mid); 1172e66f709bSChris Mason } else { 1173e66f709bSChris Mason orig_slot -= 11745f39d397SChris Mason btrfs_header_nritems(left); 1175e66f709bSChris Mason path->slots[level] = orig_slot; 1176925baeddSChris Mason btrfs_tree_unlock(left); 11775f39d397SChris Mason free_extent_buffer(left); 1178e66f709bSChris Mason } 1179e66f709bSChris Mason return 0; 1180e66f709bSChris Mason } 1181925baeddSChris Mason btrfs_tree_unlock(left); 11825f39d397SChris Mason free_extent_buffer(left); 1183e66f709bSChris Mason } 11845f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 1185e66f709bSChris Mason 1186e66f709bSChris Mason /* 1187e66f709bSChris Mason * then try to empty the right most buffer into the middle 1188e66f709bSChris Mason */ 11895f39d397SChris Mason if (right) { 119033ade1f8SChris Mason u32 right_nr; 1191b4ce94deSChris Mason 1192925baeddSChris Mason btrfs_tree_lock(right); 1193b4ce94deSChris Mason btrfs_set_lock_blocking(right); 1194b4ce94deSChris Mason 11955f39d397SChris Mason right_nr = btrfs_header_nritems(right); 119633ade1f8SChris Mason if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 119733ade1f8SChris Mason wret = 1; 119833ade1f8SChris Mason } else { 11995f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 12005f39d397SChris Mason parent, pslot + 1, 120165b51a00SChris Mason &right, 0); 120254aa1f4dSChris Mason if (ret) 120354aa1f4dSChris Mason wret = 1; 120454aa1f4dSChris Mason else { 120533ade1f8SChris Mason wret = balance_node_right(trans, root, 12065f39d397SChris Mason right, mid); 120733ade1f8SChris Mason } 120854aa1f4dSChris Mason } 1209e66f709bSChris Mason if (wret < 0) 1210e66f709bSChris Mason ret = wret; 1211e66f709bSChris Mason if (wret == 0) { 12125f39d397SChris Mason struct btrfs_disk_key disk_key; 12135f39d397SChris Mason 12145f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 12155f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 12165f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 12175f39d397SChris Mason 12185f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 12195f39d397SChris Mason path->nodes[level] = right; 1220e66f709bSChris Mason path->slots[level + 1] += 1; 1221e66f709bSChris Mason path->slots[level] = orig_slot - 12225f39d397SChris Mason btrfs_header_nritems(mid); 1223925baeddSChris Mason btrfs_tree_unlock(mid); 12245f39d397SChris Mason free_extent_buffer(mid); 1225e66f709bSChris Mason } else { 1226925baeddSChris Mason btrfs_tree_unlock(right); 12275f39d397SChris Mason free_extent_buffer(right); 1228e66f709bSChris Mason } 1229e66f709bSChris Mason return 0; 1230e66f709bSChris Mason } 1231925baeddSChris Mason btrfs_tree_unlock(right); 12325f39d397SChris Mason free_extent_buffer(right); 1233e66f709bSChris Mason } 1234e66f709bSChris Mason return 1; 1235e66f709bSChris Mason } 1236e66f709bSChris Mason 123774123bd7SChris Mason /* 1238d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 1239d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 12403c69faecSChris Mason */ 1241e02119d5SChris Mason static noinline void reada_for_search(struct btrfs_root *root, 1242e02119d5SChris Mason struct btrfs_path *path, 124301f46658SChris Mason int level, int slot, u64 objectid) 12443c69faecSChris Mason { 12455f39d397SChris Mason struct extent_buffer *node; 124601f46658SChris Mason struct btrfs_disk_key disk_key; 12473c69faecSChris Mason u32 nritems; 12483c69faecSChris Mason u64 search; 1249a7175319SChris Mason u64 target; 12506b80053dSChris Mason u64 nread = 0; 12513c69faecSChris Mason int direction = path->reada; 12525f39d397SChris Mason struct extent_buffer *eb; 12536b80053dSChris Mason u32 nr; 12546b80053dSChris Mason u32 blocksize; 12556b80053dSChris Mason u32 nscan = 0; 1256db94535dSChris Mason 1257a6b6e75eSChris Mason if (level != 1) 12583c69faecSChris Mason return; 12593c69faecSChris Mason 12606702ed49SChris Mason if (!path->nodes[level]) 12616702ed49SChris Mason return; 12626702ed49SChris Mason 12635f39d397SChris Mason node = path->nodes[level]; 1264925baeddSChris Mason 12653c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 12666b80053dSChris Mason blocksize = btrfs_level_size(root, level - 1); 12676b80053dSChris Mason eb = btrfs_find_tree_block(root, search, blocksize); 12685f39d397SChris Mason if (eb) { 12695f39d397SChris Mason free_extent_buffer(eb); 12703c69faecSChris Mason return; 12713c69faecSChris Mason } 12723c69faecSChris Mason 1273a7175319SChris Mason target = search; 12746b80053dSChris Mason 12755f39d397SChris Mason nritems = btrfs_header_nritems(node); 12766b80053dSChris Mason nr = slot; 12773c69faecSChris Mason while (1) { 12786b80053dSChris Mason if (direction < 0) { 12796b80053dSChris Mason if (nr == 0) 12803c69faecSChris Mason break; 12816b80053dSChris Mason nr--; 12826b80053dSChris Mason } else if (direction > 0) { 12836b80053dSChris Mason nr++; 12846b80053dSChris Mason if (nr >= nritems) 12856b80053dSChris Mason break; 12863c69faecSChris Mason } 128701f46658SChris Mason if (path->reada < 0 && objectid) { 128801f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 128901f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 129001f46658SChris Mason break; 129101f46658SChris Mason } 12926b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 1293a7175319SChris Mason if ((search <= target && target - search <= 65536) || 1294a7175319SChris Mason (search > target && search - target <= 65536)) { 1295ca7a79adSChris Mason readahead_tree_block(root, search, blocksize, 1296ca7a79adSChris Mason btrfs_node_ptr_generation(node, nr)); 12976b80053dSChris Mason nread += blocksize; 12983c69faecSChris Mason } 12996b80053dSChris Mason nscan++; 1300a7175319SChris Mason if ((nread > 65536 || nscan > 32)) 13016b80053dSChris Mason break; 13023c69faecSChris Mason } 13033c69faecSChris Mason } 1304925baeddSChris Mason 1305d352ac68SChris Mason /* 1306b4ce94deSChris Mason * returns -EAGAIN if it had to drop the path, or zero if everything was in 1307b4ce94deSChris Mason * cache 1308b4ce94deSChris Mason */ 1309b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root, 1310b4ce94deSChris Mason struct btrfs_path *path, int level) 1311b4ce94deSChris Mason { 1312b4ce94deSChris Mason int slot; 1313b4ce94deSChris Mason int nritems; 1314b4ce94deSChris Mason struct extent_buffer *parent; 1315b4ce94deSChris Mason struct extent_buffer *eb; 1316b4ce94deSChris Mason u64 gen; 1317b4ce94deSChris Mason u64 block1 = 0; 1318b4ce94deSChris Mason u64 block2 = 0; 1319b4ce94deSChris Mason int ret = 0; 1320b4ce94deSChris Mason int blocksize; 1321b4ce94deSChris Mason 1322b4ce94deSChris Mason parent = path->nodes[level - 1]; 1323b4ce94deSChris Mason if (!parent) 1324b4ce94deSChris Mason return 0; 1325b4ce94deSChris Mason 1326b4ce94deSChris Mason nritems = btrfs_header_nritems(parent); 1327b4ce94deSChris Mason slot = path->slots[level]; 1328b4ce94deSChris Mason blocksize = btrfs_level_size(root, level); 1329b4ce94deSChris Mason 1330b4ce94deSChris Mason if (slot > 0) { 1331b4ce94deSChris Mason block1 = btrfs_node_blockptr(parent, slot - 1); 1332b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot - 1); 1333b4ce94deSChris Mason eb = btrfs_find_tree_block(root, block1, blocksize); 1334b4ce94deSChris Mason if (eb && btrfs_buffer_uptodate(eb, gen)) 1335b4ce94deSChris Mason block1 = 0; 1336b4ce94deSChris Mason free_extent_buffer(eb); 1337b4ce94deSChris Mason } 1338b4ce94deSChris Mason if (slot < nritems) { 1339b4ce94deSChris Mason block2 = btrfs_node_blockptr(parent, slot + 1); 1340b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot + 1); 1341b4ce94deSChris Mason eb = btrfs_find_tree_block(root, block2, blocksize); 1342b4ce94deSChris Mason if (eb && btrfs_buffer_uptodate(eb, gen)) 1343b4ce94deSChris Mason block2 = 0; 1344b4ce94deSChris Mason free_extent_buffer(eb); 1345b4ce94deSChris Mason } 1346b4ce94deSChris Mason if (block1 || block2) { 1347b4ce94deSChris Mason ret = -EAGAIN; 1348b4ce94deSChris Mason btrfs_release_path(root, path); 1349b4ce94deSChris Mason if (block1) 1350b4ce94deSChris Mason readahead_tree_block(root, block1, blocksize, 0); 1351b4ce94deSChris Mason if (block2) 1352b4ce94deSChris Mason readahead_tree_block(root, block2, blocksize, 0); 1353b4ce94deSChris Mason 1354b4ce94deSChris Mason if (block1) { 1355b4ce94deSChris Mason eb = read_tree_block(root, block1, blocksize, 0); 1356b4ce94deSChris Mason free_extent_buffer(eb); 1357b4ce94deSChris Mason } 1358b4ce94deSChris Mason if (block1) { 1359b4ce94deSChris Mason eb = read_tree_block(root, block2, blocksize, 0); 1360b4ce94deSChris Mason free_extent_buffer(eb); 1361b4ce94deSChris Mason } 1362b4ce94deSChris Mason } 1363b4ce94deSChris Mason return ret; 1364b4ce94deSChris Mason } 1365b4ce94deSChris Mason 1366b4ce94deSChris Mason 1367b4ce94deSChris Mason /* 1368d397712bSChris Mason * when we walk down the tree, it is usually safe to unlock the higher layers 1369d397712bSChris Mason * in the tree. The exceptions are when our path goes through slot 0, because 1370d397712bSChris Mason * operations on the tree might require changing key pointers higher up in the 1371d397712bSChris Mason * tree. 1372d352ac68SChris Mason * 1373d397712bSChris Mason * callers might also have set path->keep_locks, which tells this code to keep 1374d397712bSChris Mason * the lock if the path points to the last slot in the block. This is part of 1375d397712bSChris Mason * walking through the tree, and selecting the next slot in the higher block. 1376d352ac68SChris Mason * 1377d397712bSChris Mason * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so 1378d397712bSChris Mason * if lowest_unlock is 1, level 0 won't be unlocked 1379d352ac68SChris Mason */ 1380e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level, 1381e02119d5SChris Mason int lowest_unlock) 1382925baeddSChris Mason { 1383925baeddSChris Mason int i; 1384925baeddSChris Mason int skip_level = level; 1385051e1b9fSChris Mason int no_skips = 0; 1386925baeddSChris Mason struct extent_buffer *t; 1387925baeddSChris Mason 1388925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1389925baeddSChris Mason if (!path->nodes[i]) 1390925baeddSChris Mason break; 1391925baeddSChris Mason if (!path->locks[i]) 1392925baeddSChris Mason break; 1393051e1b9fSChris Mason if (!no_skips && path->slots[i] == 0) { 1394925baeddSChris Mason skip_level = i + 1; 1395925baeddSChris Mason continue; 1396925baeddSChris Mason } 1397051e1b9fSChris Mason if (!no_skips && path->keep_locks) { 1398925baeddSChris Mason u32 nritems; 1399925baeddSChris Mason t = path->nodes[i]; 1400925baeddSChris Mason nritems = btrfs_header_nritems(t); 1401051e1b9fSChris Mason if (nritems < 1 || path->slots[i] >= nritems - 1) { 1402925baeddSChris Mason skip_level = i + 1; 1403925baeddSChris Mason continue; 1404925baeddSChris Mason } 1405925baeddSChris Mason } 1406051e1b9fSChris Mason if (skip_level < i && i >= lowest_unlock) 1407051e1b9fSChris Mason no_skips = 1; 1408051e1b9fSChris Mason 1409925baeddSChris Mason t = path->nodes[i]; 1410925baeddSChris Mason if (i >= lowest_unlock && i > skip_level && path->locks[i]) { 1411925baeddSChris Mason btrfs_tree_unlock(t); 1412925baeddSChris Mason path->locks[i] = 0; 1413925baeddSChris Mason } 1414925baeddSChris Mason } 1415925baeddSChris Mason } 1416925baeddSChris Mason 14173c69faecSChris Mason /* 1418b4ce94deSChris Mason * This releases any locks held in the path starting at level and 1419b4ce94deSChris Mason * going all the way up to the root. 1420b4ce94deSChris Mason * 1421b4ce94deSChris Mason * btrfs_search_slot will keep the lock held on higher nodes in a few 1422b4ce94deSChris Mason * corner cases, such as COW of the block at slot zero in the node. This 1423b4ce94deSChris Mason * ignores those rules, and it should only be called when there are no 1424b4ce94deSChris Mason * more updates to be done higher up in the tree. 1425b4ce94deSChris Mason */ 1426b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) 1427b4ce94deSChris Mason { 1428b4ce94deSChris Mason int i; 1429b4ce94deSChris Mason 1430b4ce94deSChris Mason if (path->keep_locks || path->lowest_level) 1431b4ce94deSChris Mason return; 1432b4ce94deSChris Mason 1433b4ce94deSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1434b4ce94deSChris Mason if (!path->nodes[i]) 143512f4daccSChris Mason continue; 1436b4ce94deSChris Mason if (!path->locks[i]) 143712f4daccSChris Mason continue; 1438b4ce94deSChris Mason btrfs_tree_unlock(path->nodes[i]); 1439b4ce94deSChris Mason path->locks[i] = 0; 1440b4ce94deSChris Mason } 1441b4ce94deSChris Mason } 1442b4ce94deSChris Mason 1443b4ce94deSChris Mason /* 144474123bd7SChris Mason * look for key in the tree. path is filled in with nodes along the way 144574123bd7SChris Mason * if key is found, we return zero and you can find the item in the leaf 144674123bd7SChris Mason * level of the path (level 0) 144774123bd7SChris Mason * 144874123bd7SChris Mason * If the key isn't found, the path points to the slot where it should 1449aa5d6bedSChris Mason * be inserted, and 1 is returned. If there are other errors during the 1450aa5d6bedSChris Mason * search a negative error number is returned. 145197571fd0SChris Mason * 145297571fd0SChris Mason * if ins_len > 0, nodes and leaves will be split as we walk down the 145397571fd0SChris Mason * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if 145497571fd0SChris Mason * possible) 145574123bd7SChris Mason */ 1456e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root 1457e089f05cSChris Mason *root, struct btrfs_key *key, struct btrfs_path *p, int 1458e089f05cSChris Mason ins_len, int cow) 1459be0e5c09SChris Mason { 14605f39d397SChris Mason struct extent_buffer *b; 1461051e1b9fSChris Mason struct extent_buffer *tmp; 1462be0e5c09SChris Mason int slot; 1463be0e5c09SChris Mason int ret; 1464be0e5c09SChris Mason int level; 14653c69faecSChris Mason int should_reada = p->reada; 1466925baeddSChris Mason int lowest_unlock = 1; 1467594a24ebSChris Mason int blocksize; 14689f3a7427SChris Mason u8 lowest_level = 0; 1469594a24ebSChris Mason u64 blocknr; 1470594a24ebSChris Mason u64 gen; 147165b51a00SChris Mason struct btrfs_key prealloc_block; 14729f3a7427SChris Mason 14736702ed49SChris Mason lowest_level = p->lowest_level; 1474323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 147522b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 147625179201SJosef Bacik 1477925baeddSChris Mason if (ins_len < 0) 1478925baeddSChris Mason lowest_unlock = 2; 147965b51a00SChris Mason 148065b51a00SChris Mason prealloc_block.objectid = 0; 148165b51a00SChris Mason 1482bb803951SChris Mason again: 14835cd57b2cSChris Mason if (p->skip_locking) 14845cd57b2cSChris Mason b = btrfs_root_node(root); 14855cd57b2cSChris Mason else 1486925baeddSChris Mason b = btrfs_lock_root_node(root); 1487925baeddSChris Mason 1488eb60ceacSChris Mason while (b) { 14895f39d397SChris Mason level = btrfs_header_level(b); 149065b51a00SChris Mason 149165b51a00SChris Mason /* 149265b51a00SChris Mason * setup the path here so we can release it under lock 149365b51a00SChris Mason * contention with the cow code 149465b51a00SChris Mason */ 149565b51a00SChris Mason p->nodes[level] = b; 149665b51a00SChris Mason if (!p->skip_locking) 149765b51a00SChris Mason p->locks[level] = 1; 149865b51a00SChris Mason 149902217ed2SChris Mason if (cow) { 150002217ed2SChris Mason int wret; 150165b51a00SChris Mason 150265b51a00SChris Mason /* is a cow on this block not required */ 150365b51a00SChris Mason if (btrfs_header_generation(b) == trans->transid && 15045b21f2edSZheng Yan btrfs_header_owner(b) == root->root_key.objectid && 150565b51a00SChris Mason !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) { 150665b51a00SChris Mason goto cow_done; 150765b51a00SChris Mason } 150865b51a00SChris Mason 150965b51a00SChris Mason /* ok, we have to cow, is our old prealloc the right 151065b51a00SChris Mason * size? 151165b51a00SChris Mason */ 151265b51a00SChris Mason if (prealloc_block.objectid && 151365b51a00SChris Mason prealloc_block.offset != b->len) { 15147b78c170SChris Mason btrfs_release_path(root, p); 151565b51a00SChris Mason btrfs_free_reserved_extent(root, 151665b51a00SChris Mason prealloc_block.objectid, 151765b51a00SChris Mason prealloc_block.offset); 151865b51a00SChris Mason prealloc_block.objectid = 0; 15197b78c170SChris Mason goto again; 152065b51a00SChris Mason } 152165b51a00SChris Mason 152265b51a00SChris Mason /* 152365b51a00SChris Mason * for higher level blocks, try not to allocate blocks 152465b51a00SChris Mason * with the block and the parent locks held. 152565b51a00SChris Mason */ 1526284b066aSChris Mason if (level > 0 && !prealloc_block.objectid) { 152765b51a00SChris Mason u32 size = b->len; 152865b51a00SChris Mason u64 hint = b->start; 152965b51a00SChris Mason 153065b51a00SChris Mason btrfs_release_path(root, p); 153165b51a00SChris Mason ret = btrfs_reserve_extent(trans, root, 153265b51a00SChris Mason size, size, 0, 153365b51a00SChris Mason hint, (u64)-1, 153465b51a00SChris Mason &prealloc_block, 0); 153565b51a00SChris Mason BUG_ON(ret); 153665b51a00SChris Mason goto again; 153765b51a00SChris Mason } 153865b51a00SChris Mason 1539b4ce94deSChris Mason btrfs_set_path_blocking(p); 1540b4ce94deSChris Mason 1541e20d96d6SChris Mason wret = btrfs_cow_block(trans, root, b, 1542e20d96d6SChris Mason p->nodes[level + 1], 1543e20d96d6SChris Mason p->slots[level + 1], 154465b51a00SChris Mason &b, prealloc_block.objectid); 154565b51a00SChris Mason prealloc_block.objectid = 0; 154654aa1f4dSChris Mason if (wret) { 15475f39d397SChris Mason free_extent_buffer(b); 154865b51a00SChris Mason ret = wret; 154965b51a00SChris Mason goto done; 155054aa1f4dSChris Mason } 155102217ed2SChris Mason } 155265b51a00SChris Mason cow_done: 155302217ed2SChris Mason BUG_ON(!cow && ins_len); 15545f39d397SChris Mason if (level != btrfs_header_level(b)) 15552c90e5d6SChris Mason WARN_ON(1); 15565f39d397SChris Mason level = btrfs_header_level(b); 155765b51a00SChris Mason 1558eb60ceacSChris Mason p->nodes[level] = b; 15595cd57b2cSChris Mason if (!p->skip_locking) 1560925baeddSChris Mason p->locks[level] = 1; 156165b51a00SChris Mason 1562b4ce94deSChris Mason btrfs_clear_path_blocking(p); 1563b4ce94deSChris Mason 1564b4ce94deSChris Mason /* 1565b4ce94deSChris Mason * we have a lock on b and as long as we aren't changing 1566b4ce94deSChris Mason * the tree, there is no way to for the items in b to change. 1567b4ce94deSChris Mason * It is safe to drop the lock on our parent before we 1568b4ce94deSChris Mason * go through the expensive btree search on b. 1569b4ce94deSChris Mason * 1570b4ce94deSChris Mason * If cow is true, then we might be changing slot zero, 1571b4ce94deSChris Mason * which may require changing the parent. So, we can't 1572b4ce94deSChris Mason * drop the lock until after we know which slot we're 1573b4ce94deSChris Mason * operating on. 1574b4ce94deSChris Mason */ 1575b4ce94deSChris Mason if (!cow) 1576b4ce94deSChris Mason btrfs_unlock_up_safe(p, level + 1); 1577b4ce94deSChris Mason 1578123abc88SChris Mason ret = check_block(root, p, level); 157965b51a00SChris Mason if (ret) { 158065b51a00SChris Mason ret = -1; 158165b51a00SChris Mason goto done; 158265b51a00SChris Mason } 1583925baeddSChris Mason 15845f39d397SChris Mason ret = bin_search(b, key, level, &slot); 1585b4ce94deSChris Mason 15865f39d397SChris Mason if (level != 0) { 1587be0e5c09SChris Mason if (ret && slot > 0) 1588be0e5c09SChris Mason slot -= 1; 1589be0e5c09SChris Mason p->slots[level] = slot; 1590459931ecSChris Mason if ((p->search_for_split || ins_len > 0) && 1591459931ecSChris Mason btrfs_header_nritems(b) >= 15921514794eSChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 1593b4ce94deSChris Mason int sret; 1594b4ce94deSChris Mason 1595b4ce94deSChris Mason sret = reada_for_balance(root, p, level); 1596b4ce94deSChris Mason if (sret) 1597b4ce94deSChris Mason goto again; 1598b4ce94deSChris Mason 1599b4ce94deSChris Mason btrfs_set_path_blocking(p); 1600b4ce94deSChris Mason sret = split_node(trans, root, p, level); 1601b4ce94deSChris Mason btrfs_clear_path_blocking(p); 1602b4ce94deSChris Mason 16035c680ed6SChris Mason BUG_ON(sret > 0); 160465b51a00SChris Mason if (sret) { 160565b51a00SChris Mason ret = sret; 160665b51a00SChris Mason goto done; 160765b51a00SChris Mason } 16085c680ed6SChris Mason b = p->nodes[level]; 16095c680ed6SChris Mason slot = p->slots[level]; 16107b78c170SChris Mason } else if (ins_len < 0 && 16117b78c170SChris Mason btrfs_header_nritems(b) < 16127b78c170SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 4) { 1613b4ce94deSChris Mason int sret; 1614b4ce94deSChris Mason 1615b4ce94deSChris Mason sret = reada_for_balance(root, p, level); 1616b4ce94deSChris Mason if (sret) 1617b4ce94deSChris Mason goto again; 1618b4ce94deSChris Mason 1619b4ce94deSChris Mason btrfs_set_path_blocking(p); 1620b4ce94deSChris Mason sret = balance_level(trans, root, p, level); 1621b4ce94deSChris Mason btrfs_clear_path_blocking(p); 1622b4ce94deSChris Mason 162365b51a00SChris Mason if (sret) { 162465b51a00SChris Mason ret = sret; 162565b51a00SChris Mason goto done; 162665b51a00SChris Mason } 1627bb803951SChris Mason b = p->nodes[level]; 1628f510cfecSChris Mason if (!b) { 1629f510cfecSChris Mason btrfs_release_path(NULL, p); 1630bb803951SChris Mason goto again; 1631f510cfecSChris Mason } 1632bb803951SChris Mason slot = p->slots[level]; 16335f39d397SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 16345c680ed6SChris Mason } 1635f9efa9c7SChris Mason unlock_up(p, level, lowest_unlock); 1636f9efa9c7SChris Mason 16379f3a7427SChris Mason /* this is only true while dropping a snapshot */ 1638925baeddSChris Mason if (level == lowest_level) { 16395b21f2edSZheng Yan ret = 0; 16405b21f2edSZheng Yan goto done; 1641925baeddSChris Mason } 1642ca7a79adSChris Mason 1643594a24ebSChris Mason blocknr = btrfs_node_blockptr(b, slot); 1644594a24ebSChris Mason gen = btrfs_node_ptr_generation(b, slot); 1645594a24ebSChris Mason blocksize = btrfs_level_size(root, level - 1); 1646594a24ebSChris Mason 1647594a24ebSChris Mason tmp = btrfs_find_tree_block(root, blocknr, blocksize); 1648594a24ebSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 1649051e1b9fSChris Mason b = tmp; 1650051e1b9fSChris Mason } else { 1651051e1b9fSChris Mason /* 1652051e1b9fSChris Mason * reduce lock contention at high levels 1653051e1b9fSChris Mason * of the btree by dropping locks before 1654051e1b9fSChris Mason * we read. 1655051e1b9fSChris Mason */ 1656b4ce94deSChris Mason if (level > 0) { 1657051e1b9fSChris Mason btrfs_release_path(NULL, p); 1658051e1b9fSChris Mason if (tmp) 1659051e1b9fSChris Mason free_extent_buffer(tmp); 1660f9efa9c7SChris Mason if (should_reada) 1661f9efa9c7SChris Mason reada_for_search(root, p, 1662f9efa9c7SChris Mason level, slot, 1663f9efa9c7SChris Mason key->objectid); 1664f9efa9c7SChris Mason 1665594a24ebSChris Mason tmp = read_tree_block(root, blocknr, 1666594a24ebSChris Mason blocksize, gen); 1667594a24ebSChris Mason if (tmp) 1668594a24ebSChris Mason free_extent_buffer(tmp); 1669051e1b9fSChris Mason goto again; 1670051e1b9fSChris Mason } else { 1671b4ce94deSChris Mason btrfs_set_path_blocking(p); 1672a74a4b97SChris Mason if (tmp) 1673a74a4b97SChris Mason free_extent_buffer(tmp); 1674f9efa9c7SChris Mason if (should_reada) 1675f9efa9c7SChris Mason reada_for_search(root, p, 1676f9efa9c7SChris Mason level, slot, 1677f9efa9c7SChris Mason key->objectid); 1678ca7a79adSChris Mason b = read_node_slot(root, b, slot); 1679051e1b9fSChris Mason } 1680051e1b9fSChris Mason } 1681b4ce94deSChris Mason if (!p->skip_locking) { 1682b4ce94deSChris Mason int lret; 1683b4ce94deSChris Mason 1684b4ce94deSChris Mason btrfs_clear_path_blocking(p); 1685b4ce94deSChris Mason lret = btrfs_try_spin_lock(b); 1686b4ce94deSChris Mason 1687b4ce94deSChris Mason if (!lret) { 1688b4ce94deSChris Mason btrfs_set_path_blocking(p); 1689925baeddSChris Mason btrfs_tree_lock(b); 1690b4ce94deSChris Mason btrfs_clear_path_blocking(p); 1691b4ce94deSChris Mason } 1692b4ce94deSChris Mason } 1693be0e5c09SChris Mason } else { 1694be0e5c09SChris Mason p->slots[level] = slot; 169587b29b20SYan Zheng if (ins_len > 0 && 169687b29b20SYan Zheng btrfs_leaf_free_space(root, b) < ins_len) { 1697b4ce94deSChris Mason int sret; 1698b4ce94deSChris Mason 1699b4ce94deSChris Mason btrfs_set_path_blocking(p); 1700b4ce94deSChris Mason sret = split_leaf(trans, root, key, 1701cc0c5538SChris Mason p, ins_len, ret == 0); 1702b4ce94deSChris Mason btrfs_clear_path_blocking(p); 1703b4ce94deSChris Mason 17045c680ed6SChris Mason BUG_ON(sret > 0); 170565b51a00SChris Mason if (sret) { 170665b51a00SChris Mason ret = sret; 170765b51a00SChris Mason goto done; 170865b51a00SChris Mason } 17095c680ed6SChris Mason } 1710459931ecSChris Mason if (!p->search_for_split) 1711925baeddSChris Mason unlock_up(p, level, lowest_unlock); 171265b51a00SChris Mason goto done; 171365b51a00SChris Mason } 171465b51a00SChris Mason } 171565b51a00SChris Mason ret = 1; 171665b51a00SChris Mason done: 1717b4ce94deSChris Mason /* 1718b4ce94deSChris Mason * we don't really know what they plan on doing with the path 1719b4ce94deSChris Mason * from here on, so for now just mark it as blocking 1720b4ce94deSChris Mason */ 1721b4ce94deSChris Mason btrfs_set_path_blocking(p); 172265b51a00SChris Mason if (prealloc_block.objectid) { 172365b51a00SChris Mason btrfs_free_reserved_extent(root, 172465b51a00SChris Mason prealloc_block.objectid, 172565b51a00SChris Mason prealloc_block.offset); 172665b51a00SChris Mason } 1727be0e5c09SChris Mason return ret; 1728be0e5c09SChris Mason } 1729be0e5c09SChris Mason 17301a40e23bSZheng Yan int btrfs_merge_path(struct btrfs_trans_handle *trans, 17311a40e23bSZheng Yan struct btrfs_root *root, 17321a40e23bSZheng Yan struct btrfs_key *node_keys, 17331a40e23bSZheng Yan u64 *nodes, int lowest_level) 17341a40e23bSZheng Yan { 17351a40e23bSZheng Yan struct extent_buffer *eb; 17361a40e23bSZheng Yan struct extent_buffer *parent; 17371a40e23bSZheng Yan struct btrfs_key key; 17381a40e23bSZheng Yan u64 bytenr; 17391a40e23bSZheng Yan u64 generation; 17401a40e23bSZheng Yan u32 blocksize; 17411a40e23bSZheng Yan int level; 17421a40e23bSZheng Yan int slot; 17431a40e23bSZheng Yan int key_match; 17441a40e23bSZheng Yan int ret; 17451a40e23bSZheng Yan 17461a40e23bSZheng Yan eb = btrfs_lock_root_node(root); 17471a40e23bSZheng Yan ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0); 17481a40e23bSZheng Yan BUG_ON(ret); 17491a40e23bSZheng Yan 1750b4ce94deSChris Mason btrfs_set_lock_blocking(eb); 1751b4ce94deSChris Mason 17521a40e23bSZheng Yan parent = eb; 17531a40e23bSZheng Yan while (1) { 17541a40e23bSZheng Yan level = btrfs_header_level(parent); 17551a40e23bSZheng Yan if (level == 0 || level <= lowest_level) 17561a40e23bSZheng Yan break; 17571a40e23bSZheng Yan 17581a40e23bSZheng Yan ret = bin_search(parent, &node_keys[lowest_level], level, 17591a40e23bSZheng Yan &slot); 17601a40e23bSZheng Yan if (ret && slot > 0) 17611a40e23bSZheng Yan slot--; 17621a40e23bSZheng Yan 17631a40e23bSZheng Yan bytenr = btrfs_node_blockptr(parent, slot); 17641a40e23bSZheng Yan if (nodes[level - 1] == bytenr) 17651a40e23bSZheng Yan break; 17661a40e23bSZheng Yan 17671a40e23bSZheng Yan blocksize = btrfs_level_size(root, level - 1); 17681a40e23bSZheng Yan generation = btrfs_node_ptr_generation(parent, slot); 17691a40e23bSZheng Yan btrfs_node_key_to_cpu(eb, &key, slot); 17701a40e23bSZheng Yan key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key)); 17711a40e23bSZheng Yan 1772f82d02d9SYan Zheng if (generation == trans->transid) { 17731a40e23bSZheng Yan eb = read_tree_block(root, bytenr, blocksize, 17741a40e23bSZheng Yan generation); 17751a40e23bSZheng Yan btrfs_tree_lock(eb); 1776b4ce94deSChris Mason btrfs_set_lock_blocking(eb); 1777f82d02d9SYan Zheng } 1778f82d02d9SYan Zheng 1779f82d02d9SYan Zheng /* 1780f82d02d9SYan Zheng * if node keys match and node pointer hasn't been modified 1781f82d02d9SYan Zheng * in the running transaction, we can merge the path. for 1782f82d02d9SYan Zheng * blocks owened by reloc trees, the node pointer check is 1783f82d02d9SYan Zheng * skipped, this is because these blocks are fully controlled 1784f82d02d9SYan Zheng * by the space balance code, no one else can modify them. 1785f82d02d9SYan Zheng */ 1786f82d02d9SYan Zheng if (!nodes[level - 1] || !key_match || 1787f82d02d9SYan Zheng (generation == trans->transid && 1788f82d02d9SYan Zheng btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) { 1789f82d02d9SYan Zheng if (level == 1 || level == lowest_level + 1) { 1790f82d02d9SYan Zheng if (generation == trans->transid) { 1791f82d02d9SYan Zheng btrfs_tree_unlock(eb); 1792f82d02d9SYan Zheng free_extent_buffer(eb); 1793f82d02d9SYan Zheng } 1794f82d02d9SYan Zheng break; 1795f82d02d9SYan Zheng } 1796f82d02d9SYan Zheng 1797f82d02d9SYan Zheng if (generation != trans->transid) { 1798f82d02d9SYan Zheng eb = read_tree_block(root, bytenr, blocksize, 1799f82d02d9SYan Zheng generation); 1800f82d02d9SYan Zheng btrfs_tree_lock(eb); 1801b4ce94deSChris Mason btrfs_set_lock_blocking(eb); 1802f82d02d9SYan Zheng } 18031a40e23bSZheng Yan 18041a40e23bSZheng Yan ret = btrfs_cow_block(trans, root, eb, parent, slot, 18051a40e23bSZheng Yan &eb, 0); 18061a40e23bSZheng Yan BUG_ON(ret); 18071a40e23bSZheng Yan 1808f82d02d9SYan Zheng if (root->root_key.objectid == 1809f82d02d9SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 1810f82d02d9SYan Zheng if (!nodes[level - 1]) { 1811f82d02d9SYan Zheng nodes[level - 1] = eb->start; 1812f82d02d9SYan Zheng memcpy(&node_keys[level - 1], &key, 1813f82d02d9SYan Zheng sizeof(node_keys[0])); 1814f82d02d9SYan Zheng } else { 1815f82d02d9SYan Zheng WARN_ON(1); 1816f82d02d9SYan Zheng } 1817f82d02d9SYan Zheng } 1818f82d02d9SYan Zheng 18191a40e23bSZheng Yan btrfs_tree_unlock(parent); 18201a40e23bSZheng Yan free_extent_buffer(parent); 18211a40e23bSZheng Yan parent = eb; 18221a40e23bSZheng Yan continue; 18231a40e23bSZheng Yan } 18241a40e23bSZheng Yan 18251a40e23bSZheng Yan btrfs_set_node_blockptr(parent, slot, nodes[level - 1]); 18261a40e23bSZheng Yan btrfs_set_node_ptr_generation(parent, slot, trans->transid); 18271a40e23bSZheng Yan btrfs_mark_buffer_dirty(parent); 18281a40e23bSZheng Yan 18291a40e23bSZheng Yan ret = btrfs_inc_extent_ref(trans, root, 18301a40e23bSZheng Yan nodes[level - 1], 18311a40e23bSZheng Yan blocksize, parent->start, 18321a40e23bSZheng Yan btrfs_header_owner(parent), 18331a40e23bSZheng Yan btrfs_header_generation(parent), 18343bb1a1bcSYan Zheng level - 1); 18351a40e23bSZheng Yan BUG_ON(ret); 1836f82d02d9SYan Zheng 1837f82d02d9SYan Zheng /* 1838f82d02d9SYan Zheng * If the block was created in the running transaction, 1839f82d02d9SYan Zheng * it's possible this is the last reference to it, so we 1840f82d02d9SYan Zheng * should drop the subtree. 1841f82d02d9SYan Zheng */ 1842f82d02d9SYan Zheng if (generation == trans->transid) { 1843f82d02d9SYan Zheng ret = btrfs_drop_subtree(trans, root, eb, parent); 1844f82d02d9SYan Zheng BUG_ON(ret); 1845f82d02d9SYan Zheng btrfs_tree_unlock(eb); 1846f82d02d9SYan Zheng free_extent_buffer(eb); 1847f82d02d9SYan Zheng } else { 18481a40e23bSZheng Yan ret = btrfs_free_extent(trans, root, bytenr, 18491a40e23bSZheng Yan blocksize, parent->start, 18501a40e23bSZheng Yan btrfs_header_owner(parent), 18511a40e23bSZheng Yan btrfs_header_generation(parent), 18523bb1a1bcSYan Zheng level - 1, 1); 18531a40e23bSZheng Yan BUG_ON(ret); 18541a40e23bSZheng Yan } 18551a40e23bSZheng Yan break; 18561a40e23bSZheng Yan } 18571a40e23bSZheng Yan btrfs_tree_unlock(parent); 18581a40e23bSZheng Yan free_extent_buffer(parent); 18591a40e23bSZheng Yan return 0; 18601a40e23bSZheng Yan } 18611a40e23bSZheng Yan 186274123bd7SChris Mason /* 186374123bd7SChris Mason * adjust the pointers going up the tree, starting at level 186474123bd7SChris Mason * making sure the right key of each node is points to 'key'. 186574123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 186674123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 186774123bd7SChris Mason * higher levels 1868aa5d6bedSChris Mason * 1869aa5d6bedSChris Mason * If this fails to write a tree block, it returns -1, but continues 1870aa5d6bedSChris Mason * fixing up the blocks in ram so the tree is consistent. 187174123bd7SChris Mason */ 18725f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans, 18735f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 18745f39d397SChris Mason struct btrfs_disk_key *key, int level) 1875be0e5c09SChris Mason { 1876be0e5c09SChris Mason int i; 1877aa5d6bedSChris Mason int ret = 0; 18785f39d397SChris Mason struct extent_buffer *t; 18795f39d397SChris Mason 1880234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1881be0e5c09SChris Mason int tslot = path->slots[i]; 1882eb60ceacSChris Mason if (!path->nodes[i]) 1883be0e5c09SChris Mason break; 18845f39d397SChris Mason t = path->nodes[i]; 18855f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 1886d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 1887be0e5c09SChris Mason if (tslot != 0) 1888be0e5c09SChris Mason break; 1889be0e5c09SChris Mason } 1890aa5d6bedSChris Mason return ret; 1891be0e5c09SChris Mason } 1892be0e5c09SChris Mason 189374123bd7SChris Mason /* 189431840ae1SZheng Yan * update item key. 189531840ae1SZheng Yan * 189631840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 189731840ae1SZheng Yan * that the new key won't break the order 189831840ae1SZheng Yan */ 189931840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans, 190031840ae1SZheng Yan struct btrfs_root *root, struct btrfs_path *path, 190131840ae1SZheng Yan struct btrfs_key *new_key) 190231840ae1SZheng Yan { 190331840ae1SZheng Yan struct btrfs_disk_key disk_key; 190431840ae1SZheng Yan struct extent_buffer *eb; 190531840ae1SZheng Yan int slot; 190631840ae1SZheng Yan 190731840ae1SZheng Yan eb = path->nodes[0]; 190831840ae1SZheng Yan slot = path->slots[0]; 190931840ae1SZheng Yan if (slot > 0) { 191031840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 191131840ae1SZheng Yan if (comp_keys(&disk_key, new_key) >= 0) 191231840ae1SZheng Yan return -1; 191331840ae1SZheng Yan } 191431840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 191531840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 191631840ae1SZheng Yan if (comp_keys(&disk_key, new_key) <= 0) 191731840ae1SZheng Yan return -1; 191831840ae1SZheng Yan } 191931840ae1SZheng Yan 192031840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 192131840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 192231840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 192331840ae1SZheng Yan if (slot == 0) 192431840ae1SZheng Yan fixup_low_keys(trans, root, path, &disk_key, 1); 192531840ae1SZheng Yan return 0; 192631840ae1SZheng Yan } 192731840ae1SZheng Yan 192831840ae1SZheng Yan /* 192974123bd7SChris Mason * try to push data from one node into the next node left in the 193079f95c82SChris Mason * tree. 1931aa5d6bedSChris Mason * 1932aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 1933aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 193474123bd7SChris Mason */ 193598ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 193698ed5174SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 1937971a1f66SChris Mason struct extent_buffer *src, int empty) 1938be0e5c09SChris Mason { 1939be0e5c09SChris Mason int push_items = 0; 1940bb803951SChris Mason int src_nritems; 1941bb803951SChris Mason int dst_nritems; 1942aa5d6bedSChris Mason int ret = 0; 1943be0e5c09SChris Mason 19445f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 19455f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1946123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 19477bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 19487bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 194954aa1f4dSChris Mason 1950bce4eae9SChris Mason if (!empty && src_nritems <= 8) 1951971a1f66SChris Mason return 1; 1952971a1f66SChris Mason 1953d397712bSChris Mason if (push_items <= 0) 1954be0e5c09SChris Mason return 1; 1955be0e5c09SChris Mason 1956bce4eae9SChris Mason if (empty) { 1957971a1f66SChris Mason push_items = min(src_nritems, push_items); 1958bce4eae9SChris Mason if (push_items < src_nritems) { 1959bce4eae9SChris Mason /* leave at least 8 pointers in the node if 1960bce4eae9SChris Mason * we aren't going to empty it 1961bce4eae9SChris Mason */ 1962bce4eae9SChris Mason if (src_nritems - push_items < 8) { 1963bce4eae9SChris Mason if (push_items <= 8) 1964bce4eae9SChris Mason return 1; 1965bce4eae9SChris Mason push_items -= 8; 1966bce4eae9SChris Mason } 1967bce4eae9SChris Mason } 1968bce4eae9SChris Mason } else 1969bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 197079f95c82SChris Mason 19715f39d397SChris Mason copy_extent_buffer(dst, src, 19725f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 19735f39d397SChris Mason btrfs_node_key_ptr_offset(0), 1974123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 19755f39d397SChris Mason 1976bb803951SChris Mason if (push_items < src_nritems) { 19775f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 19785f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 1979e2fa7227SChris Mason (src_nritems - push_items) * 1980123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 1981bb803951SChris Mason } 19825f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 19835f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 19845f39d397SChris Mason btrfs_mark_buffer_dirty(src); 19855f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 198631840ae1SZheng Yan 198731840ae1SZheng Yan ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items); 198831840ae1SZheng Yan BUG_ON(ret); 198931840ae1SZheng Yan 1990bb803951SChris Mason return ret; 1991be0e5c09SChris Mason } 1992be0e5c09SChris Mason 199397571fd0SChris Mason /* 199479f95c82SChris Mason * try to push data from one node into the next node right in the 199579f95c82SChris Mason * tree. 199679f95c82SChris Mason * 199779f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 199879f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 199979f95c82SChris Mason * 200079f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 200179f95c82SChris Mason */ 20025f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 20035f39d397SChris Mason struct btrfs_root *root, 20045f39d397SChris Mason struct extent_buffer *dst, 20055f39d397SChris Mason struct extent_buffer *src) 200679f95c82SChris Mason { 200779f95c82SChris Mason int push_items = 0; 200879f95c82SChris Mason int max_push; 200979f95c82SChris Mason int src_nritems; 201079f95c82SChris Mason int dst_nritems; 201179f95c82SChris Mason int ret = 0; 201279f95c82SChris Mason 20137bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 20147bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 20157bb86316SChris Mason 20165f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 20175f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 2018123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 2019d397712bSChris Mason if (push_items <= 0) 202079f95c82SChris Mason return 1; 2021bce4eae9SChris Mason 2022d397712bSChris Mason if (src_nritems < 4) 2023bce4eae9SChris Mason return 1; 202479f95c82SChris Mason 202579f95c82SChris Mason max_push = src_nritems / 2 + 1; 202679f95c82SChris Mason /* don't try to empty the node */ 2027d397712bSChris Mason if (max_push >= src_nritems) 202879f95c82SChris Mason return 1; 2029252c38f0SYan 203079f95c82SChris Mason if (max_push < push_items) 203179f95c82SChris Mason push_items = max_push; 203279f95c82SChris Mason 20335f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 20345f39d397SChris Mason btrfs_node_key_ptr_offset(0), 20355f39d397SChris Mason (dst_nritems) * 20365f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 2037d6025579SChris Mason 20385f39d397SChris Mason copy_extent_buffer(dst, src, 20395f39d397SChris Mason btrfs_node_key_ptr_offset(0), 20405f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 2041123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 204279f95c82SChris Mason 20435f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 20445f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 204579f95c82SChris Mason 20465f39d397SChris Mason btrfs_mark_buffer_dirty(src); 20475f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 204831840ae1SZheng Yan 204931840ae1SZheng Yan ret = btrfs_update_ref(trans, root, src, dst, 0, push_items); 205031840ae1SZheng Yan BUG_ON(ret); 205131840ae1SZheng Yan 205279f95c82SChris Mason return ret; 205379f95c82SChris Mason } 205479f95c82SChris Mason 205579f95c82SChris Mason /* 205697571fd0SChris Mason * helper function to insert a new root level in the tree. 205797571fd0SChris Mason * A new node is allocated, and a single item is inserted to 205897571fd0SChris Mason * point to the existing root 2059aa5d6bedSChris Mason * 2060aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 206197571fd0SChris Mason */ 2062d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans, 20635f39d397SChris Mason struct btrfs_root *root, 20645f39d397SChris Mason struct btrfs_path *path, int level) 206574123bd7SChris Mason { 20667bb86316SChris Mason u64 lower_gen; 20675f39d397SChris Mason struct extent_buffer *lower; 20685f39d397SChris Mason struct extent_buffer *c; 2069925baeddSChris Mason struct extent_buffer *old; 20705f39d397SChris Mason struct btrfs_disk_key lower_key; 207131840ae1SZheng Yan int ret; 20725c680ed6SChris Mason 20735c680ed6SChris Mason BUG_ON(path->nodes[level]); 20745c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 20755c680ed6SChris Mason 20767bb86316SChris Mason lower = path->nodes[level-1]; 20777bb86316SChris Mason if (level == 1) 20787bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 20797bb86316SChris Mason else 20807bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 20817bb86316SChris Mason 208231840ae1SZheng Yan c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 208331840ae1SZheng Yan root->root_key.objectid, trans->transid, 2084ad3d81baSChristoph Hellwig level, root->node->start, 0); 20855f39d397SChris Mason if (IS_ERR(c)) 20865f39d397SChris Mason return PTR_ERR(c); 2087925baeddSChris Mason 20885f39d397SChris Mason memset_extent_buffer(c, 0, 0, root->nodesize); 20895f39d397SChris Mason btrfs_set_header_nritems(c, 1); 20905f39d397SChris Mason btrfs_set_header_level(c, level); 2091db94535dSChris Mason btrfs_set_header_bytenr(c, c->start); 20925f39d397SChris Mason btrfs_set_header_generation(c, trans->transid); 20935f39d397SChris Mason btrfs_set_header_owner(c, root->root_key.objectid); 2094d5719762SChris Mason 20955f39d397SChris Mason write_extent_buffer(c, root->fs_info->fsid, 20965f39d397SChris Mason (unsigned long)btrfs_header_fsid(c), 20975f39d397SChris Mason BTRFS_FSID_SIZE); 2098e17cade2SChris Mason 2099e17cade2SChris Mason write_extent_buffer(c, root->fs_info->chunk_tree_uuid, 2100e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(c), 2101e17cade2SChris Mason BTRFS_UUID_SIZE); 2102e17cade2SChris Mason 21035f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 2104db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 21057bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 210631840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 21077bb86316SChris Mason 21087bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 21095f39d397SChris Mason 21105f39d397SChris Mason btrfs_mark_buffer_dirty(c); 2111d5719762SChris Mason 2112925baeddSChris Mason spin_lock(&root->node_lock); 2113925baeddSChris Mason old = root->node; 21145f39d397SChris Mason root->node = c; 2115925baeddSChris Mason spin_unlock(&root->node_lock); 2116925baeddSChris Mason 211731840ae1SZheng Yan ret = btrfs_update_extent_ref(trans, root, lower->start, 211831840ae1SZheng Yan lower->start, c->start, 211931840ae1SZheng Yan root->root_key.objectid, 21203bb1a1bcSYan Zheng trans->transid, level - 1); 212131840ae1SZheng Yan BUG_ON(ret); 212231840ae1SZheng Yan 2123925baeddSChris Mason /* the super has an extra ref to root->node */ 2124925baeddSChris Mason free_extent_buffer(old); 2125925baeddSChris Mason 21260b86a832SChris Mason add_root_to_dirty_list(root); 21275f39d397SChris Mason extent_buffer_get(c); 21285f39d397SChris Mason path->nodes[level] = c; 2129925baeddSChris Mason path->locks[level] = 1; 213074123bd7SChris Mason path->slots[level] = 0; 213174123bd7SChris Mason return 0; 213274123bd7SChris Mason } 21335c680ed6SChris Mason 21345c680ed6SChris Mason /* 21355c680ed6SChris Mason * worker function to insert a single pointer in a node. 21365c680ed6SChris Mason * the node should have enough room for the pointer already 213797571fd0SChris Mason * 21385c680ed6SChris Mason * slot and level indicate where you want the key to go, and 21395c680ed6SChris Mason * blocknr is the block the key points to. 2140aa5d6bedSChris Mason * 2141aa5d6bedSChris Mason * returns zero on success and < 0 on any error 21425c680ed6SChris Mason */ 2143e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root 2144e089f05cSChris Mason *root, struct btrfs_path *path, struct btrfs_disk_key 2145db94535dSChris Mason *key, u64 bytenr, int slot, int level) 21465c680ed6SChris Mason { 21475f39d397SChris Mason struct extent_buffer *lower; 21485c680ed6SChris Mason int nritems; 21495c680ed6SChris Mason 21505c680ed6SChris Mason BUG_ON(!path->nodes[level]); 21515f39d397SChris Mason lower = path->nodes[level]; 21525f39d397SChris Mason nritems = btrfs_header_nritems(lower); 215374123bd7SChris Mason if (slot > nritems) 215474123bd7SChris Mason BUG(); 2155123abc88SChris Mason if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) 215674123bd7SChris Mason BUG(); 215774123bd7SChris Mason if (slot != nritems) { 21585f39d397SChris Mason memmove_extent_buffer(lower, 21595f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 21605f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 2161123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 216274123bd7SChris Mason } 21635f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 2164db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 216574493f7aSChris Mason WARN_ON(trans->transid == 0); 216674493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 21675f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 21685f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 216974123bd7SChris Mason return 0; 217074123bd7SChris Mason } 217174123bd7SChris Mason 217297571fd0SChris Mason /* 217397571fd0SChris Mason * split the node at the specified level in path in two. 217497571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 217597571fd0SChris Mason * 217697571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 217797571fd0SChris Mason * left and right, if either one works, it returns right away. 2178aa5d6bedSChris Mason * 2179aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 218097571fd0SChris Mason */ 2181e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 2182e02119d5SChris Mason struct btrfs_root *root, 2183e02119d5SChris Mason struct btrfs_path *path, int level) 2184be0e5c09SChris Mason { 21855f39d397SChris Mason struct extent_buffer *c; 21865f39d397SChris Mason struct extent_buffer *split; 21875f39d397SChris Mason struct btrfs_disk_key disk_key; 2188be0e5c09SChris Mason int mid; 21895c680ed6SChris Mason int ret; 2190aa5d6bedSChris Mason int wret; 21917518a238SChris Mason u32 c_nritems; 2192be0e5c09SChris Mason 21935f39d397SChris Mason c = path->nodes[level]; 21947bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 21955f39d397SChris Mason if (c == root->node) { 21965c680ed6SChris Mason /* trying to split the root, lets make a new one */ 2197e089f05cSChris Mason ret = insert_new_root(trans, root, path, level + 1); 21985c680ed6SChris Mason if (ret) 21995c680ed6SChris Mason return ret; 2200e66f709bSChris Mason } else { 2201e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 22025f39d397SChris Mason c = path->nodes[level]; 22035f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 2204c448acf0SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) 2205e66f709bSChris Mason return 0; 220654aa1f4dSChris Mason if (ret < 0) 220754aa1f4dSChris Mason return ret; 22085c680ed6SChris Mason } 2209e66f709bSChris Mason 22105f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 22117bb86316SChris Mason 2212925baeddSChris Mason split = btrfs_alloc_free_block(trans, root, root->nodesize, 221331840ae1SZheng Yan path->nodes[level + 1]->start, 22147bb86316SChris Mason root->root_key.objectid, 221531840ae1SZheng Yan trans->transid, level, c->start, 0); 22165f39d397SChris Mason if (IS_ERR(split)) 22175f39d397SChris Mason return PTR_ERR(split); 221854aa1f4dSChris Mason 22195f39d397SChris Mason btrfs_set_header_flags(split, btrfs_header_flags(c)); 22205f39d397SChris Mason btrfs_set_header_level(split, btrfs_header_level(c)); 2221db94535dSChris Mason btrfs_set_header_bytenr(split, split->start); 22225f39d397SChris Mason btrfs_set_header_generation(split, trans->transid); 22235f39d397SChris Mason btrfs_set_header_owner(split, root->root_key.objectid); 222463b10fc4SChris Mason btrfs_set_header_flags(split, 0); 22255f39d397SChris Mason write_extent_buffer(split, root->fs_info->fsid, 22265f39d397SChris Mason (unsigned long)btrfs_header_fsid(split), 22275f39d397SChris Mason BTRFS_FSID_SIZE); 2228e17cade2SChris Mason write_extent_buffer(split, root->fs_info->chunk_tree_uuid, 2229e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(split), 2230e17cade2SChris Mason BTRFS_UUID_SIZE); 22315f39d397SChris Mason 22327518a238SChris Mason mid = (c_nritems + 1) / 2; 22335f39d397SChris Mason 22345f39d397SChris Mason copy_extent_buffer(split, c, 22355f39d397SChris Mason btrfs_node_key_ptr_offset(0), 22365f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 2237123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 22385f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 22395f39d397SChris Mason btrfs_set_header_nritems(c, mid); 2240aa5d6bedSChris Mason ret = 0; 2241aa5d6bedSChris Mason 22425f39d397SChris Mason btrfs_mark_buffer_dirty(c); 22435f39d397SChris Mason btrfs_mark_buffer_dirty(split); 22445f39d397SChris Mason 22455f39d397SChris Mason btrfs_node_key(split, &disk_key, 0); 2246db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, split->start, 22475f39d397SChris Mason path->slots[level + 1] + 1, 2248123abc88SChris Mason level + 1); 2249aa5d6bedSChris Mason if (wret) 2250aa5d6bedSChris Mason ret = wret; 2251aa5d6bedSChris Mason 225231840ae1SZheng Yan ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid); 225331840ae1SZheng Yan BUG_ON(ret); 225431840ae1SZheng Yan 22555de08d7dSChris Mason if (path->slots[level] >= mid) { 22565c680ed6SChris Mason path->slots[level] -= mid; 2257925baeddSChris Mason btrfs_tree_unlock(c); 22585f39d397SChris Mason free_extent_buffer(c); 22595f39d397SChris Mason path->nodes[level] = split; 22605c680ed6SChris Mason path->slots[level + 1] += 1; 2261eb60ceacSChris Mason } else { 2262925baeddSChris Mason btrfs_tree_unlock(split); 22635f39d397SChris Mason free_extent_buffer(split); 2264be0e5c09SChris Mason } 2265aa5d6bedSChris Mason return ret; 2266be0e5c09SChris Mason } 2267be0e5c09SChris Mason 226874123bd7SChris Mason /* 226974123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 227074123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 227174123bd7SChris Mason * space used both by the item structs and the item data 227274123bd7SChris Mason */ 22735f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 2274be0e5c09SChris Mason { 2275be0e5c09SChris Mason int data_len; 22765f39d397SChris Mason int nritems = btrfs_header_nritems(l); 2277d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 2278be0e5c09SChris Mason 2279be0e5c09SChris Mason if (!nr) 2280be0e5c09SChris Mason return 0; 22815f39d397SChris Mason data_len = btrfs_item_end_nr(l, start); 22825f39d397SChris Mason data_len = data_len - btrfs_item_offset_nr(l, end); 22830783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 2284d4dbff95SChris Mason WARN_ON(data_len < 0); 2285be0e5c09SChris Mason return data_len; 2286be0e5c09SChris Mason } 2287be0e5c09SChris Mason 228874123bd7SChris Mason /* 2289d4dbff95SChris Mason * The space between the end of the leaf items and 2290d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 2291d4dbff95SChris Mason * the leaf has left for both items and data 2292d4dbff95SChris Mason */ 2293d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root, 2294e02119d5SChris Mason struct extent_buffer *leaf) 2295d4dbff95SChris Mason { 22965f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 22975f39d397SChris Mason int ret; 22985f39d397SChris Mason ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); 22995f39d397SChris Mason if (ret < 0) { 2300d397712bSChris Mason printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " 2301d397712bSChris Mason "used %d nritems %d\n", 2302ae2f5411SJens Axboe ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), 23035f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 23045f39d397SChris Mason } 23055f39d397SChris Mason return ret; 2306d4dbff95SChris Mason } 2307d4dbff95SChris Mason 2308d4dbff95SChris Mason /* 230900ec4c51SChris Mason * push some data in the path leaf to the right, trying to free up at 231000ec4c51SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 2311aa5d6bedSChris Mason * 2312aa5d6bedSChris Mason * returns 1 if the push failed because the other node didn't have enough 2313aa5d6bedSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 231400ec4c51SChris Mason */ 2315e089f05cSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 231634a38218SChris Mason *root, struct btrfs_path *path, int data_size, 231734a38218SChris Mason int empty) 231800ec4c51SChris Mason { 23195f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 23205f39d397SChris Mason struct extent_buffer *right; 23215f39d397SChris Mason struct extent_buffer *upper; 23225f39d397SChris Mason struct btrfs_disk_key disk_key; 232300ec4c51SChris Mason int slot; 232434a38218SChris Mason u32 i; 232500ec4c51SChris Mason int free_space; 232600ec4c51SChris Mason int push_space = 0; 232700ec4c51SChris Mason int push_items = 0; 23280783fcfcSChris Mason struct btrfs_item *item; 23297518a238SChris Mason u32 left_nritems; 233034a38218SChris Mason u32 nr; 23317518a238SChris Mason u32 right_nritems; 23325f39d397SChris Mason u32 data_end; 2333db94535dSChris Mason u32 this_item_size; 233454aa1f4dSChris Mason int ret; 233500ec4c51SChris Mason 233600ec4c51SChris Mason slot = path->slots[1]; 2337d397712bSChris Mason if (!path->nodes[1]) 233800ec4c51SChris Mason return 1; 2339d397712bSChris Mason 234000ec4c51SChris Mason upper = path->nodes[1]; 23415f39d397SChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 234200ec4c51SChris Mason return 1; 23435f39d397SChris Mason 2344a2135011SChris Mason WARN_ON(!btrfs_tree_locked(path->nodes[1])); 2345a2135011SChris Mason 2346ca7a79adSChris Mason right = read_node_slot(root, upper, slot + 1); 2347925baeddSChris Mason btrfs_tree_lock(right); 2348b4ce94deSChris Mason btrfs_set_lock_blocking(right); 2349b4ce94deSChris Mason 2350123abc88SChris Mason free_space = btrfs_leaf_free_space(root, right); 235187b29b20SYan Zheng if (free_space < data_size) 2352925baeddSChris Mason goto out_unlock; 235302217ed2SChris Mason 23545f39d397SChris Mason /* cow and double check */ 23555f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, upper, 235665b51a00SChris Mason slot + 1, &right, 0); 2357925baeddSChris Mason if (ret) 2358925baeddSChris Mason goto out_unlock; 2359925baeddSChris Mason 23605f39d397SChris Mason free_space = btrfs_leaf_free_space(root, right); 236187b29b20SYan Zheng if (free_space < data_size) 2362925baeddSChris Mason goto out_unlock; 23635f39d397SChris Mason 23645f39d397SChris Mason left_nritems = btrfs_header_nritems(left); 2365925baeddSChris Mason if (left_nritems == 0) 2366925baeddSChris Mason goto out_unlock; 23675f39d397SChris Mason 236834a38218SChris Mason if (empty) 236934a38218SChris Mason nr = 0; 237034a38218SChris Mason else 237134a38218SChris Mason nr = 1; 237234a38218SChris Mason 237331840ae1SZheng Yan if (path->slots[0] >= left_nritems) 237487b29b20SYan Zheng push_space += data_size; 237531840ae1SZheng Yan 237634a38218SChris Mason i = left_nritems - 1; 237734a38218SChris Mason while (i >= nr) { 23785f39d397SChris Mason item = btrfs_item_nr(left, i); 2379db94535dSChris Mason 238031840ae1SZheng Yan if (!empty && push_items > 0) { 238131840ae1SZheng Yan if (path->slots[0] > i) 238231840ae1SZheng Yan break; 238331840ae1SZheng Yan if (path->slots[0] == i) { 238431840ae1SZheng Yan int space = btrfs_leaf_free_space(root, left); 238531840ae1SZheng Yan if (space + push_space * 2 > free_space) 238631840ae1SZheng Yan break; 238731840ae1SZheng Yan } 238831840ae1SZheng Yan } 238931840ae1SZheng Yan 239000ec4c51SChris Mason if (path->slots[0] == i) 239187b29b20SYan Zheng push_space += data_size; 2392db94535dSChris Mason 2393db94535dSChris Mason if (!left->map_token) { 2394db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 2395db94535dSChris Mason sizeof(struct btrfs_item), 2396db94535dSChris Mason &left->map_token, &left->kaddr, 2397db94535dSChris Mason &left->map_start, &left->map_len, 2398db94535dSChris Mason KM_USER1); 2399db94535dSChris Mason } 2400db94535dSChris Mason 2401db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 2402db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 240300ec4c51SChris Mason break; 240431840ae1SZheng Yan 240500ec4c51SChris Mason push_items++; 2406db94535dSChris Mason push_space += this_item_size + sizeof(*item); 240734a38218SChris Mason if (i == 0) 240834a38218SChris Mason break; 240934a38218SChris Mason i--; 2410db94535dSChris Mason } 2411db94535dSChris Mason if (left->map_token) { 2412db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 2413db94535dSChris Mason left->map_token = NULL; 241400ec4c51SChris Mason } 24155f39d397SChris Mason 2416925baeddSChris Mason if (push_items == 0) 2417925baeddSChris Mason goto out_unlock; 24185f39d397SChris Mason 241934a38218SChris Mason if (!empty && push_items == left_nritems) 2420a429e513SChris Mason WARN_ON(1); 24215f39d397SChris Mason 242200ec4c51SChris Mason /* push left to right */ 24235f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 242434a38218SChris Mason 24255f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 2426123abc88SChris Mason push_space -= leaf_data_end(root, left); 24275f39d397SChris Mason 242800ec4c51SChris Mason /* make room in the right data area */ 24295f39d397SChris Mason data_end = leaf_data_end(root, right); 24305f39d397SChris Mason memmove_extent_buffer(right, 24315f39d397SChris Mason btrfs_leaf_data(right) + data_end - push_space, 24325f39d397SChris Mason btrfs_leaf_data(right) + data_end, 24335f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root) - data_end); 24345f39d397SChris Mason 243500ec4c51SChris Mason /* copy from the left data area */ 24365f39d397SChris Mason copy_extent_buffer(right, left, btrfs_leaf_data(right) + 2437d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2438d6025579SChris Mason btrfs_leaf_data(left) + leaf_data_end(root, left), 2439d6025579SChris Mason push_space); 24405f39d397SChris Mason 24415f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 24425f39d397SChris Mason btrfs_item_nr_offset(0), 24430783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 24445f39d397SChris Mason 244500ec4c51SChris Mason /* copy the items from left to right */ 24465f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 24475f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 24480783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 244900ec4c51SChris Mason 245000ec4c51SChris Mason /* update the item pointers */ 24517518a238SChris Mason right_nritems += push_items; 24525f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 2453123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 24547518a238SChris Mason for (i = 0; i < right_nritems; i++) { 24555f39d397SChris Mason item = btrfs_item_nr(right, i); 2456db94535dSChris Mason if (!right->map_token) { 2457db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2458db94535dSChris Mason sizeof(struct btrfs_item), 2459db94535dSChris Mason &right->map_token, &right->kaddr, 2460db94535dSChris Mason &right->map_start, &right->map_len, 2461db94535dSChris Mason KM_USER1); 2462db94535dSChris Mason } 2463db94535dSChris Mason push_space -= btrfs_item_size(right, item); 2464db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2465db94535dSChris Mason } 2466db94535dSChris Mason 2467db94535dSChris Mason if (right->map_token) { 2468db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2469db94535dSChris Mason right->map_token = NULL; 247000ec4c51SChris Mason } 24717518a238SChris Mason left_nritems -= push_items; 24725f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 247300ec4c51SChris Mason 247434a38218SChris Mason if (left_nritems) 24755f39d397SChris Mason btrfs_mark_buffer_dirty(left); 24765f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2477a429e513SChris Mason 247831840ae1SZheng Yan ret = btrfs_update_ref(trans, root, left, right, 0, push_items); 247931840ae1SZheng Yan BUG_ON(ret); 248031840ae1SZheng Yan 24815f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 24825f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 2483d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 248402217ed2SChris Mason 248500ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 24867518a238SChris Mason if (path->slots[0] >= left_nritems) { 24877518a238SChris Mason path->slots[0] -= left_nritems; 2488925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 2489925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 2490925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 24915f39d397SChris Mason free_extent_buffer(path->nodes[0]); 24925f39d397SChris Mason path->nodes[0] = right; 249300ec4c51SChris Mason path->slots[1] += 1; 249400ec4c51SChris Mason } else { 2495925baeddSChris Mason btrfs_tree_unlock(right); 24965f39d397SChris Mason free_extent_buffer(right); 249700ec4c51SChris Mason } 249800ec4c51SChris Mason return 0; 2499925baeddSChris Mason 2500925baeddSChris Mason out_unlock: 2501925baeddSChris Mason btrfs_tree_unlock(right); 2502925baeddSChris Mason free_extent_buffer(right); 2503925baeddSChris Mason return 1; 250400ec4c51SChris Mason } 2505925baeddSChris Mason 250600ec4c51SChris Mason /* 250774123bd7SChris Mason * push some data in the path leaf to the left, trying to free up at 250874123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 250974123bd7SChris Mason */ 2510e089f05cSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 251134a38218SChris Mason *root, struct btrfs_path *path, int data_size, 251234a38218SChris Mason int empty) 2513be0e5c09SChris Mason { 25145f39d397SChris Mason struct btrfs_disk_key disk_key; 25155f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 25165f39d397SChris Mason struct extent_buffer *left; 2517be0e5c09SChris Mason int slot; 2518be0e5c09SChris Mason int i; 2519be0e5c09SChris Mason int free_space; 2520be0e5c09SChris Mason int push_space = 0; 2521be0e5c09SChris Mason int push_items = 0; 25220783fcfcSChris Mason struct btrfs_item *item; 25237518a238SChris Mason u32 old_left_nritems; 25245f39d397SChris Mason u32 right_nritems; 252534a38218SChris Mason u32 nr; 2526aa5d6bedSChris Mason int ret = 0; 2527aa5d6bedSChris Mason int wret; 2528db94535dSChris Mason u32 this_item_size; 2529db94535dSChris Mason u32 old_left_item_size; 2530be0e5c09SChris Mason 2531be0e5c09SChris Mason slot = path->slots[1]; 25325f39d397SChris Mason if (slot == 0) 2533be0e5c09SChris Mason return 1; 25345f39d397SChris Mason if (!path->nodes[1]) 2535be0e5c09SChris Mason return 1; 25365f39d397SChris Mason 25373685f791SChris Mason right_nritems = btrfs_header_nritems(right); 2538d397712bSChris Mason if (right_nritems == 0) 25393685f791SChris Mason return 1; 25403685f791SChris Mason 2541a2135011SChris Mason WARN_ON(!btrfs_tree_locked(path->nodes[1])); 2542a2135011SChris Mason 2543ca7a79adSChris Mason left = read_node_slot(root, path->nodes[1], slot - 1); 2544925baeddSChris Mason btrfs_tree_lock(left); 2545b4ce94deSChris Mason btrfs_set_lock_blocking(left); 2546b4ce94deSChris Mason 2547123abc88SChris Mason free_space = btrfs_leaf_free_space(root, left); 254887b29b20SYan Zheng if (free_space < data_size) { 2549925baeddSChris Mason ret = 1; 2550925baeddSChris Mason goto out; 2551be0e5c09SChris Mason } 255202217ed2SChris Mason 255302217ed2SChris Mason /* cow and double check */ 25545f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, 255565b51a00SChris Mason path->nodes[1], slot - 1, &left, 0); 255654aa1f4dSChris Mason if (ret) { 255754aa1f4dSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 2558925baeddSChris Mason ret = 1; 2559925baeddSChris Mason goto out; 256054aa1f4dSChris Mason } 25613685f791SChris Mason 2562123abc88SChris Mason free_space = btrfs_leaf_free_space(root, left); 256387b29b20SYan Zheng if (free_space < data_size) { 2564925baeddSChris Mason ret = 1; 2565925baeddSChris Mason goto out; 256602217ed2SChris Mason } 256702217ed2SChris Mason 256834a38218SChris Mason if (empty) 256934a38218SChris Mason nr = right_nritems; 257034a38218SChris Mason else 257134a38218SChris Mason nr = right_nritems - 1; 257234a38218SChris Mason 257334a38218SChris Mason for (i = 0; i < nr; i++) { 25745f39d397SChris Mason item = btrfs_item_nr(right, i); 2575db94535dSChris Mason if (!right->map_token) { 2576db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2577db94535dSChris Mason sizeof(struct btrfs_item), 2578db94535dSChris Mason &right->map_token, &right->kaddr, 2579db94535dSChris Mason &right->map_start, &right->map_len, 2580db94535dSChris Mason KM_USER1); 2581db94535dSChris Mason } 2582db94535dSChris Mason 258331840ae1SZheng Yan if (!empty && push_items > 0) { 258431840ae1SZheng Yan if (path->slots[0] < i) 258531840ae1SZheng Yan break; 258631840ae1SZheng Yan if (path->slots[0] == i) { 258731840ae1SZheng Yan int space = btrfs_leaf_free_space(root, right); 258831840ae1SZheng Yan if (space + push_space * 2 > free_space) 258931840ae1SZheng Yan break; 259031840ae1SZheng Yan } 259131840ae1SZheng Yan } 259231840ae1SZheng Yan 2593be0e5c09SChris Mason if (path->slots[0] == i) 259487b29b20SYan Zheng push_space += data_size; 2595db94535dSChris Mason 2596db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 2597db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 2598be0e5c09SChris Mason break; 2599db94535dSChris Mason 2600be0e5c09SChris Mason push_items++; 2601db94535dSChris Mason push_space += this_item_size + sizeof(*item); 2602be0e5c09SChris Mason } 2603db94535dSChris Mason 2604db94535dSChris Mason if (right->map_token) { 2605db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2606db94535dSChris Mason right->map_token = NULL; 2607db94535dSChris Mason } 2608db94535dSChris Mason 2609be0e5c09SChris Mason if (push_items == 0) { 2610925baeddSChris Mason ret = 1; 2611925baeddSChris Mason goto out; 2612be0e5c09SChris Mason } 261334a38218SChris Mason if (!empty && push_items == btrfs_header_nritems(right)) 2614a429e513SChris Mason WARN_ON(1); 26155f39d397SChris Mason 2616be0e5c09SChris Mason /* push data from right to left */ 26175f39d397SChris Mason copy_extent_buffer(left, right, 26185f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 26195f39d397SChris Mason btrfs_item_nr_offset(0), 26205f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 26215f39d397SChris Mason 2622123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root) - 26235f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1); 26245f39d397SChris Mason 26255f39d397SChris Mason copy_extent_buffer(left, right, btrfs_leaf_data(left) + 2626d6025579SChris Mason leaf_data_end(root, left) - push_space, 2627123abc88SChris Mason btrfs_leaf_data(right) + 26285f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 2629be0e5c09SChris Mason push_space); 26305f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 263187b29b20SYan Zheng BUG_ON(old_left_nritems <= 0); 2632eb60ceacSChris Mason 2633db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 2634be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 26355f39d397SChris Mason u32 ioff; 2636db94535dSChris Mason 26375f39d397SChris Mason item = btrfs_item_nr(left, i); 2638db94535dSChris Mason if (!left->map_token) { 2639db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 2640db94535dSChris Mason sizeof(struct btrfs_item), 2641db94535dSChris Mason &left->map_token, &left->kaddr, 2642db94535dSChris Mason &left->map_start, &left->map_len, 2643db94535dSChris Mason KM_USER1); 2644db94535dSChris Mason } 2645db94535dSChris Mason 26465f39d397SChris Mason ioff = btrfs_item_offset(left, item); 26475f39d397SChris Mason btrfs_set_item_offset(left, item, 2648db94535dSChris Mason ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); 2649be0e5c09SChris Mason } 26505f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 2651db94535dSChris Mason if (left->map_token) { 2652db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 2653db94535dSChris Mason left->map_token = NULL; 2654db94535dSChris Mason } 2655be0e5c09SChris Mason 2656be0e5c09SChris Mason /* fixup right node */ 265734a38218SChris Mason if (push_items > right_nritems) { 2658d397712bSChris Mason printk(KERN_CRIT "push items %d nr %u\n", push_items, 2659d397712bSChris Mason right_nritems); 266034a38218SChris Mason WARN_ON(1); 266134a38218SChris Mason } 266234a38218SChris Mason 266334a38218SChris Mason if (push_items < right_nritems) { 26645f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 2665123abc88SChris Mason leaf_data_end(root, right); 26665f39d397SChris Mason memmove_extent_buffer(right, btrfs_leaf_data(right) + 2667d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2668d6025579SChris Mason btrfs_leaf_data(right) + 2669123abc88SChris Mason leaf_data_end(root, right), push_space); 26705f39d397SChris Mason 26715f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 26725f39d397SChris Mason btrfs_item_nr_offset(push_items), 26735f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 26740783fcfcSChris Mason sizeof(struct btrfs_item)); 267534a38218SChris Mason } 2676eef1c494SYan right_nritems -= push_items; 2677eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 2678123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 26795f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 26805f39d397SChris Mason item = btrfs_item_nr(right, i); 2681db94535dSChris Mason 2682db94535dSChris Mason if (!right->map_token) { 2683db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2684db94535dSChris Mason sizeof(struct btrfs_item), 2685db94535dSChris Mason &right->map_token, &right->kaddr, 2686db94535dSChris Mason &right->map_start, &right->map_len, 2687db94535dSChris Mason KM_USER1); 2688db94535dSChris Mason } 2689db94535dSChris Mason 2690db94535dSChris Mason push_space = push_space - btrfs_item_size(right, item); 2691db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2692db94535dSChris Mason } 2693db94535dSChris Mason if (right->map_token) { 2694db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2695db94535dSChris Mason right->map_token = NULL; 2696be0e5c09SChris Mason } 2697eb60ceacSChris Mason 26985f39d397SChris Mason btrfs_mark_buffer_dirty(left); 269934a38218SChris Mason if (right_nritems) 27005f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2701098f59c2SChris Mason 270231840ae1SZheng Yan ret = btrfs_update_ref(trans, root, right, left, 270331840ae1SZheng Yan old_left_nritems, push_items); 270431840ae1SZheng Yan BUG_ON(ret); 270531840ae1SZheng Yan 27065f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 27075f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, 1); 2708aa5d6bedSChris Mason if (wret) 2709aa5d6bedSChris Mason ret = wret; 2710be0e5c09SChris Mason 2711be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 2712be0e5c09SChris Mason if (path->slots[0] < push_items) { 2713be0e5c09SChris Mason path->slots[0] += old_left_nritems; 2714925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 2715925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 2716925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 27175f39d397SChris Mason free_extent_buffer(path->nodes[0]); 27185f39d397SChris Mason path->nodes[0] = left; 2719be0e5c09SChris Mason path->slots[1] -= 1; 2720be0e5c09SChris Mason } else { 2721925baeddSChris Mason btrfs_tree_unlock(left); 27225f39d397SChris Mason free_extent_buffer(left); 2723be0e5c09SChris Mason path->slots[0] -= push_items; 2724be0e5c09SChris Mason } 2725eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2726aa5d6bedSChris Mason return ret; 2727925baeddSChris Mason out: 2728925baeddSChris Mason btrfs_tree_unlock(left); 2729925baeddSChris Mason free_extent_buffer(left); 2730925baeddSChris Mason return ret; 2731be0e5c09SChris Mason } 2732be0e5c09SChris Mason 273374123bd7SChris Mason /* 273474123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 273574123bd7SChris Mason * available for the resulting leaf level of the path. 2736aa5d6bedSChris Mason * 2737aa5d6bedSChris Mason * returns 0 if all went well and < 0 on failure. 273874123bd7SChris Mason */ 2739e02119d5SChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 2740e02119d5SChris Mason struct btrfs_root *root, 2741e02119d5SChris Mason struct btrfs_key *ins_key, 2742e02119d5SChris Mason struct btrfs_path *path, int data_size, 2743e02119d5SChris Mason int extend) 2744be0e5c09SChris Mason { 27455f39d397SChris Mason struct extent_buffer *l; 27467518a238SChris Mason u32 nritems; 2747eb60ceacSChris Mason int mid; 2748eb60ceacSChris Mason int slot; 27495f39d397SChris Mason struct extent_buffer *right; 2750be0e5c09SChris Mason int data_copy_size; 2751be0e5c09SChris Mason int rt_data_off; 2752be0e5c09SChris Mason int i; 2753d4dbff95SChris Mason int ret = 0; 2754aa5d6bedSChris Mason int wret; 2755cc0c5538SChris Mason int double_split; 2756cc0c5538SChris Mason int num_doubles = 0; 2757d4dbff95SChris Mason struct btrfs_disk_key disk_key; 2758be0e5c09SChris Mason 275940689478SChris Mason /* first try to make some room by pushing left and right */ 2760459931ecSChris Mason if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) { 276134a38218SChris Mason wret = push_leaf_right(trans, root, path, data_size, 0); 2762d397712bSChris Mason if (wret < 0) 2763eaee50e8SChris Mason return wret; 2764eaee50e8SChris Mason if (wret) { 276534a38218SChris Mason wret = push_leaf_left(trans, root, path, data_size, 0); 2766eaee50e8SChris Mason if (wret < 0) 2767eaee50e8SChris Mason return wret; 2768eaee50e8SChris Mason } 27695f39d397SChris Mason l = path->nodes[0]; 2770aa5d6bedSChris Mason 2771aa5d6bedSChris Mason /* did the pushes work? */ 277287b29b20SYan Zheng if (btrfs_leaf_free_space(root, l) >= data_size) 2773be0e5c09SChris Mason return 0; 27743326d1b0SChris Mason } 2775aa5d6bedSChris Mason 27765c680ed6SChris Mason if (!path->nodes[1]) { 2777e089f05cSChris Mason ret = insert_new_root(trans, root, path, 1); 27785c680ed6SChris Mason if (ret) 27795c680ed6SChris Mason return ret; 27805c680ed6SChris Mason } 2781cc0c5538SChris Mason again: 2782cc0c5538SChris Mason double_split = 0; 2783cc0c5538SChris Mason l = path->nodes[0]; 2784eb60ceacSChris Mason slot = path->slots[0]; 27855f39d397SChris Mason nritems = btrfs_header_nritems(l); 2786eb60ceacSChris Mason mid = (nritems + 1) / 2; 278754aa1f4dSChris Mason 2788925baeddSChris Mason right = btrfs_alloc_free_block(trans, root, root->leafsize, 278931840ae1SZheng Yan path->nodes[1]->start, 27907bb86316SChris Mason root->root_key.objectid, 279131840ae1SZheng Yan trans->transid, 0, l->start, 0); 2792cea9e445SChris Mason if (IS_ERR(right)) { 2793cea9e445SChris Mason BUG_ON(1); 27945f39d397SChris Mason return PTR_ERR(right); 2795cea9e445SChris Mason } 279654aa1f4dSChris Mason 27975f39d397SChris Mason memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); 2798db94535dSChris Mason btrfs_set_header_bytenr(right, right->start); 27995f39d397SChris Mason btrfs_set_header_generation(right, trans->transid); 28005f39d397SChris Mason btrfs_set_header_owner(right, root->root_key.objectid); 28015f39d397SChris Mason btrfs_set_header_level(right, 0); 28025f39d397SChris Mason write_extent_buffer(right, root->fs_info->fsid, 28035f39d397SChris Mason (unsigned long)btrfs_header_fsid(right), 28045f39d397SChris Mason BTRFS_FSID_SIZE); 2805e17cade2SChris Mason 2806e17cade2SChris Mason write_extent_buffer(right, root->fs_info->chunk_tree_uuid, 2807e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(right), 2808e17cade2SChris Mason BTRFS_UUID_SIZE); 2809d4dbff95SChris Mason if (mid <= slot) { 2810d4dbff95SChris Mason if (nritems == 1 || 281187b29b20SYan Zheng leaf_space_used(l, mid, nritems - mid) + data_size > 2812d4dbff95SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 2813d4dbff95SChris Mason if (slot >= nritems) { 2814d4dbff95SChris Mason btrfs_cpu_key_to_disk(&disk_key, ins_key); 28155f39d397SChris Mason btrfs_set_header_nritems(right, 0); 2816d4dbff95SChris Mason wret = insert_ptr(trans, root, path, 2817db94535dSChris Mason &disk_key, right->start, 2818d4dbff95SChris Mason path->slots[1] + 1, 1); 2819d4dbff95SChris Mason if (wret) 2820d4dbff95SChris Mason ret = wret; 2821925baeddSChris Mason 2822925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 28235f39d397SChris Mason free_extent_buffer(path->nodes[0]); 28245f39d397SChris Mason path->nodes[0] = right; 2825d4dbff95SChris Mason path->slots[0] = 0; 2826d4dbff95SChris Mason path->slots[1] += 1; 28270ef8b242SChris Mason btrfs_mark_buffer_dirty(right); 2828d4dbff95SChris Mason return ret; 2829d4dbff95SChris Mason } 2830d4dbff95SChris Mason mid = slot; 28313326d1b0SChris Mason if (mid != nritems && 28323326d1b0SChris Mason leaf_space_used(l, mid, nritems - mid) + 283387b29b20SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 2834d4dbff95SChris Mason double_split = 1; 2835d4dbff95SChris Mason } 28363326d1b0SChris Mason } 2837d4dbff95SChris Mason } else { 283887b29b20SYan Zheng if (leaf_space_used(l, 0, mid) + data_size > 2839d4dbff95SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 2840459931ecSChris Mason if (!extend && data_size && slot == 0) { 2841d4dbff95SChris Mason btrfs_cpu_key_to_disk(&disk_key, ins_key); 28425f39d397SChris Mason btrfs_set_header_nritems(right, 0); 2843d4dbff95SChris Mason wret = insert_ptr(trans, root, path, 2844d4dbff95SChris Mason &disk_key, 2845db94535dSChris Mason right->start, 2846098f59c2SChris Mason path->slots[1], 1); 2847d4dbff95SChris Mason if (wret) 2848d4dbff95SChris Mason ret = wret; 2849925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 28505f39d397SChris Mason free_extent_buffer(path->nodes[0]); 28515f39d397SChris Mason path->nodes[0] = right; 2852d4dbff95SChris Mason path->slots[0] = 0; 2853a429e513SChris Mason if (path->slots[1] == 0) { 2854a429e513SChris Mason wret = fixup_low_keys(trans, root, 2855a429e513SChris Mason path, &disk_key, 1); 2856a429e513SChris Mason if (wret) 2857a429e513SChris Mason ret = wret; 2858a429e513SChris Mason } 28590ef8b242SChris Mason btrfs_mark_buffer_dirty(right); 2860d4dbff95SChris Mason return ret; 2861459931ecSChris Mason } else if ((extend || !data_size) && slot == 0) { 2862cc0c5538SChris Mason mid = 1; 2863cc0c5538SChris Mason } else { 2864d4dbff95SChris Mason mid = slot; 28655ee78ac7SChris Mason if (mid != nritems && 28665ee78ac7SChris Mason leaf_space_used(l, mid, nritems - mid) + 286787b29b20SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 2868d4dbff95SChris Mason double_split = 1; 2869d4dbff95SChris Mason } 2870d4dbff95SChris Mason } 28715ee78ac7SChris Mason } 2872cc0c5538SChris Mason } 28735f39d397SChris Mason nritems = nritems - mid; 28745f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 28755f39d397SChris Mason data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 28765f39d397SChris Mason 28775f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 28785f39d397SChris Mason btrfs_item_nr_offset(mid), 28795f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 28805f39d397SChris Mason 28815f39d397SChris Mason copy_extent_buffer(right, l, 2882d6025579SChris Mason btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - 2883123abc88SChris Mason data_copy_size, btrfs_leaf_data(l) + 2884123abc88SChris Mason leaf_data_end(root, l), data_copy_size); 288574123bd7SChris Mason 28865f39d397SChris Mason rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - 28875f39d397SChris Mason btrfs_item_end_nr(l, mid); 28885f39d397SChris Mason 28895f39d397SChris Mason for (i = 0; i < nritems; i++) { 28905f39d397SChris Mason struct btrfs_item *item = btrfs_item_nr(right, i); 2891db94535dSChris Mason u32 ioff; 2892db94535dSChris Mason 2893db94535dSChris Mason if (!right->map_token) { 2894db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2895db94535dSChris Mason sizeof(struct btrfs_item), 2896db94535dSChris Mason &right->map_token, &right->kaddr, 2897db94535dSChris Mason &right->map_start, &right->map_len, 2898db94535dSChris Mason KM_USER1); 2899db94535dSChris Mason } 2900db94535dSChris Mason 2901db94535dSChris Mason ioff = btrfs_item_offset(right, item); 29025f39d397SChris Mason btrfs_set_item_offset(right, item, ioff + rt_data_off); 29030783fcfcSChris Mason } 290474123bd7SChris Mason 2905db94535dSChris Mason if (right->map_token) { 2906db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2907db94535dSChris Mason right->map_token = NULL; 2908db94535dSChris Mason } 2909db94535dSChris Mason 29105f39d397SChris Mason btrfs_set_header_nritems(l, mid); 2911aa5d6bedSChris Mason ret = 0; 29125f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 2913db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, right->start, 2914db94535dSChris Mason path->slots[1] + 1, 1); 2915aa5d6bedSChris Mason if (wret) 2916aa5d6bedSChris Mason ret = wret; 29175f39d397SChris Mason 29185f39d397SChris Mason btrfs_mark_buffer_dirty(right); 29195f39d397SChris Mason btrfs_mark_buffer_dirty(l); 2920eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 29215f39d397SChris Mason 292231840ae1SZheng Yan ret = btrfs_update_ref(trans, root, l, right, 0, nritems); 292331840ae1SZheng Yan BUG_ON(ret); 292431840ae1SZheng Yan 2925be0e5c09SChris Mason if (mid <= slot) { 2926925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 29275f39d397SChris Mason free_extent_buffer(path->nodes[0]); 29285f39d397SChris Mason path->nodes[0] = right; 2929be0e5c09SChris Mason path->slots[0] -= mid; 2930be0e5c09SChris Mason path->slots[1] += 1; 2931925baeddSChris Mason } else { 2932925baeddSChris Mason btrfs_tree_unlock(right); 29335f39d397SChris Mason free_extent_buffer(right); 2934925baeddSChris Mason } 29355f39d397SChris Mason 2936eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2937d4dbff95SChris Mason 2938cc0c5538SChris Mason if (double_split) { 2939cc0c5538SChris Mason BUG_ON(num_doubles != 0); 2940cc0c5538SChris Mason num_doubles++; 2941cc0c5538SChris Mason goto again; 29423326d1b0SChris Mason } 2943be0e5c09SChris Mason return ret; 2944be0e5c09SChris Mason } 2945be0e5c09SChris Mason 2946d352ac68SChris Mason /* 2947459931ecSChris Mason * This function splits a single item into two items, 2948459931ecSChris Mason * giving 'new_key' to the new item and splitting the 2949459931ecSChris Mason * old one at split_offset (from the start of the item). 2950459931ecSChris Mason * 2951459931ecSChris Mason * The path may be released by this operation. After 2952459931ecSChris Mason * the split, the path is pointing to the old item. The 2953459931ecSChris Mason * new item is going to be in the same node as the old one. 2954459931ecSChris Mason * 2955459931ecSChris Mason * Note, the item being split must be smaller enough to live alone on 2956459931ecSChris Mason * a tree block with room for one extra struct btrfs_item 2957459931ecSChris Mason * 2958459931ecSChris Mason * This allows us to split the item in place, keeping a lock on the 2959459931ecSChris Mason * leaf the entire time. 2960459931ecSChris Mason */ 2961459931ecSChris Mason int btrfs_split_item(struct btrfs_trans_handle *trans, 2962459931ecSChris Mason struct btrfs_root *root, 2963459931ecSChris Mason struct btrfs_path *path, 2964459931ecSChris Mason struct btrfs_key *new_key, 2965459931ecSChris Mason unsigned long split_offset) 2966459931ecSChris Mason { 2967459931ecSChris Mason u32 item_size; 2968459931ecSChris Mason struct extent_buffer *leaf; 2969459931ecSChris Mason struct btrfs_key orig_key; 2970459931ecSChris Mason struct btrfs_item *item; 2971459931ecSChris Mason struct btrfs_item *new_item; 2972459931ecSChris Mason int ret = 0; 2973459931ecSChris Mason int slot; 2974459931ecSChris Mason u32 nritems; 2975459931ecSChris Mason u32 orig_offset; 2976459931ecSChris Mason struct btrfs_disk_key disk_key; 2977459931ecSChris Mason char *buf; 2978459931ecSChris Mason 2979459931ecSChris Mason leaf = path->nodes[0]; 2980459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]); 2981459931ecSChris Mason if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item)) 2982459931ecSChris Mason goto split; 2983459931ecSChris Mason 2984459931ecSChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 2985459931ecSChris Mason btrfs_release_path(root, path); 2986459931ecSChris Mason 2987459931ecSChris Mason path->search_for_split = 1; 2988459931ecSChris Mason path->keep_locks = 1; 2989459931ecSChris Mason 2990459931ecSChris Mason ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1); 2991459931ecSChris Mason path->search_for_split = 0; 2992459931ecSChris Mason 2993459931ecSChris Mason /* if our item isn't there or got smaller, return now */ 2994459931ecSChris Mason if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0], 2995459931ecSChris Mason path->slots[0])) { 2996459931ecSChris Mason path->keep_locks = 0; 2997459931ecSChris Mason return -EAGAIN; 2998459931ecSChris Mason } 2999459931ecSChris Mason 300087b29b20SYan Zheng ret = split_leaf(trans, root, &orig_key, path, 300187b29b20SYan Zheng sizeof(struct btrfs_item), 1); 3002459931ecSChris Mason path->keep_locks = 0; 3003459931ecSChris Mason BUG_ON(ret); 3004459931ecSChris Mason 3005b4ce94deSChris Mason /* 3006b4ce94deSChris Mason * make sure any changes to the path from split_leaf leave it 3007b4ce94deSChris Mason * in a blocking state 3008b4ce94deSChris Mason */ 3009b4ce94deSChris Mason btrfs_set_path_blocking(path); 3010b4ce94deSChris Mason 3011459931ecSChris Mason leaf = path->nodes[0]; 301242dc7babSChris Mason BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); 3013459931ecSChris Mason 3014459931ecSChris Mason split: 3015459931ecSChris Mason item = btrfs_item_nr(leaf, path->slots[0]); 3016459931ecSChris Mason orig_offset = btrfs_item_offset(leaf, item); 3017459931ecSChris Mason item_size = btrfs_item_size(leaf, item); 3018459931ecSChris Mason 3019459931ecSChris Mason 3020459931ecSChris Mason buf = kmalloc(item_size, GFP_NOFS); 3021459931ecSChris Mason read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, 3022459931ecSChris Mason path->slots[0]), item_size); 3023459931ecSChris Mason slot = path->slots[0] + 1; 3024459931ecSChris Mason leaf = path->nodes[0]; 3025459931ecSChris Mason 3026459931ecSChris Mason nritems = btrfs_header_nritems(leaf); 3027459931ecSChris Mason 3028459931ecSChris Mason if (slot != nritems) { 3029459931ecSChris Mason /* shift the items */ 3030459931ecSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), 3031459931ecSChris Mason btrfs_item_nr_offset(slot), 3032459931ecSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 3033459931ecSChris Mason 3034459931ecSChris Mason } 3035459931ecSChris Mason 3036459931ecSChris Mason btrfs_cpu_key_to_disk(&disk_key, new_key); 3037459931ecSChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 3038459931ecSChris Mason 3039459931ecSChris Mason new_item = btrfs_item_nr(leaf, slot); 3040459931ecSChris Mason 3041459931ecSChris Mason btrfs_set_item_offset(leaf, new_item, orig_offset); 3042459931ecSChris Mason btrfs_set_item_size(leaf, new_item, item_size - split_offset); 3043459931ecSChris Mason 3044459931ecSChris Mason btrfs_set_item_offset(leaf, item, 3045459931ecSChris Mason orig_offset + item_size - split_offset); 3046459931ecSChris Mason btrfs_set_item_size(leaf, item, split_offset); 3047459931ecSChris Mason 3048459931ecSChris Mason btrfs_set_header_nritems(leaf, nritems + 1); 3049459931ecSChris Mason 3050459931ecSChris Mason /* write the data for the start of the original item */ 3051459931ecSChris Mason write_extent_buffer(leaf, buf, 3052459931ecSChris Mason btrfs_item_ptr_offset(leaf, path->slots[0]), 3053459931ecSChris Mason split_offset); 3054459931ecSChris Mason 3055459931ecSChris Mason /* write the data for the new item */ 3056459931ecSChris Mason write_extent_buffer(leaf, buf + split_offset, 3057459931ecSChris Mason btrfs_item_ptr_offset(leaf, slot), 3058459931ecSChris Mason item_size - split_offset); 3059459931ecSChris Mason btrfs_mark_buffer_dirty(leaf); 3060459931ecSChris Mason 3061459931ecSChris Mason ret = 0; 3062459931ecSChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 3063459931ecSChris Mason btrfs_print_leaf(root, leaf); 3064459931ecSChris Mason BUG(); 3065459931ecSChris Mason } 3066459931ecSChris Mason kfree(buf); 3067459931ecSChris Mason return ret; 3068459931ecSChris Mason } 3069459931ecSChris Mason 3070459931ecSChris Mason /* 3071d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 3072d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 3073d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 3074d352ac68SChris Mason * the front. 3075d352ac68SChris Mason */ 3076b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans, 3077b18c6685SChris Mason struct btrfs_root *root, 3078b18c6685SChris Mason struct btrfs_path *path, 3079179e29e4SChris Mason u32 new_size, int from_end) 3080b18c6685SChris Mason { 3081b18c6685SChris Mason int ret = 0; 3082b18c6685SChris Mason int slot; 3083b18c6685SChris Mason int slot_orig; 30845f39d397SChris Mason struct extent_buffer *leaf; 30855f39d397SChris Mason struct btrfs_item *item; 3086b18c6685SChris Mason u32 nritems; 3087b18c6685SChris Mason unsigned int data_end; 3088b18c6685SChris Mason unsigned int old_data_start; 3089b18c6685SChris Mason unsigned int old_size; 3090b18c6685SChris Mason unsigned int size_diff; 3091b18c6685SChris Mason int i; 3092b18c6685SChris Mason 3093b18c6685SChris Mason slot_orig = path->slots[0]; 30945f39d397SChris Mason leaf = path->nodes[0]; 3095179e29e4SChris Mason slot = path->slots[0]; 3096179e29e4SChris Mason 3097179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 3098179e29e4SChris Mason if (old_size == new_size) 3099179e29e4SChris Mason return 0; 3100b18c6685SChris Mason 31015f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3102b18c6685SChris Mason data_end = leaf_data_end(root, leaf); 3103b18c6685SChris Mason 31045f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 3105179e29e4SChris Mason 3106b18c6685SChris Mason size_diff = old_size - new_size; 3107b18c6685SChris Mason 3108b18c6685SChris Mason BUG_ON(slot < 0); 3109b18c6685SChris Mason BUG_ON(slot >= nritems); 3110b18c6685SChris Mason 3111b18c6685SChris Mason /* 3112b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 3113b18c6685SChris Mason */ 3114b18c6685SChris Mason /* first correct the data pointers */ 3115b18c6685SChris Mason for (i = slot; i < nritems; i++) { 31165f39d397SChris Mason u32 ioff; 31175f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3118db94535dSChris Mason 3119db94535dSChris Mason if (!leaf->map_token) { 3120db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3121db94535dSChris Mason sizeof(struct btrfs_item), 3122db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3123db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3124db94535dSChris Mason KM_USER1); 3125db94535dSChris Mason } 3126db94535dSChris Mason 31275f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 31285f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + size_diff); 3129b18c6685SChris Mason } 3130db94535dSChris Mason 3131db94535dSChris Mason if (leaf->map_token) { 3132db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3133db94535dSChris Mason leaf->map_token = NULL; 3134db94535dSChris Mason } 3135db94535dSChris Mason 3136b18c6685SChris Mason /* shift the data */ 3137179e29e4SChris Mason if (from_end) { 31385f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3139b18c6685SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 3140b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 3141179e29e4SChris Mason } else { 3142179e29e4SChris Mason struct btrfs_disk_key disk_key; 3143179e29e4SChris Mason u64 offset; 3144179e29e4SChris Mason 3145179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 3146179e29e4SChris Mason 3147179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 3148179e29e4SChris Mason unsigned long ptr; 3149179e29e4SChris Mason struct btrfs_file_extent_item *fi; 3150179e29e4SChris Mason 3151179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 3152179e29e4SChris Mason struct btrfs_file_extent_item); 3153179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 3154179e29e4SChris Mason (unsigned long)fi - size_diff); 3155179e29e4SChris Mason 3156179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 3157179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 3158179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 3159179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 3160179e29e4SChris Mason (unsigned long)fi, 3161179e29e4SChris Mason offsetof(struct btrfs_file_extent_item, 3162179e29e4SChris Mason disk_bytenr)); 3163179e29e4SChris Mason } 3164179e29e4SChris Mason } 3165179e29e4SChris Mason 3166179e29e4SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3167179e29e4SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 3168179e29e4SChris Mason data_end, old_data_start - data_end); 3169179e29e4SChris Mason 3170179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 3171179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 3172179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 3173179e29e4SChris Mason if (slot == 0) 3174179e29e4SChris Mason fixup_low_keys(trans, root, path, &disk_key, 1); 3175179e29e4SChris Mason } 31765f39d397SChris Mason 31775f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 31785f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 31795f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3180b18c6685SChris Mason 3181b18c6685SChris Mason ret = 0; 31825f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 31835f39d397SChris Mason btrfs_print_leaf(root, leaf); 3184b18c6685SChris Mason BUG(); 31855f39d397SChris Mason } 3186b18c6685SChris Mason return ret; 3187b18c6685SChris Mason } 3188b18c6685SChris Mason 3189d352ac68SChris Mason /* 3190d352ac68SChris Mason * make the item pointed to by the path bigger, data_size is the new size. 3191d352ac68SChris Mason */ 31925f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans, 31935f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 31945f39d397SChris Mason u32 data_size) 31956567e837SChris Mason { 31966567e837SChris Mason int ret = 0; 31976567e837SChris Mason int slot; 31986567e837SChris Mason int slot_orig; 31995f39d397SChris Mason struct extent_buffer *leaf; 32005f39d397SChris Mason struct btrfs_item *item; 32016567e837SChris Mason u32 nritems; 32026567e837SChris Mason unsigned int data_end; 32036567e837SChris Mason unsigned int old_data; 32046567e837SChris Mason unsigned int old_size; 32056567e837SChris Mason int i; 32066567e837SChris Mason 32076567e837SChris Mason slot_orig = path->slots[0]; 32085f39d397SChris Mason leaf = path->nodes[0]; 32096567e837SChris Mason 32105f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 32116567e837SChris Mason data_end = leaf_data_end(root, leaf); 32126567e837SChris Mason 32135f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < data_size) { 32145f39d397SChris Mason btrfs_print_leaf(root, leaf); 32156567e837SChris Mason BUG(); 32165f39d397SChris Mason } 32176567e837SChris Mason slot = path->slots[0]; 32185f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 32196567e837SChris Mason 32206567e837SChris Mason BUG_ON(slot < 0); 32213326d1b0SChris Mason if (slot >= nritems) { 32223326d1b0SChris Mason btrfs_print_leaf(root, leaf); 3223d397712bSChris Mason printk(KERN_CRIT "slot %d too large, nritems %d\n", 3224d397712bSChris Mason slot, nritems); 32253326d1b0SChris Mason BUG_ON(1); 32263326d1b0SChris Mason } 32276567e837SChris Mason 32286567e837SChris Mason /* 32296567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 32306567e837SChris Mason */ 32316567e837SChris Mason /* first correct the data pointers */ 32326567e837SChris Mason for (i = slot; i < nritems; i++) { 32335f39d397SChris Mason u32 ioff; 32345f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3235db94535dSChris Mason 3236db94535dSChris Mason if (!leaf->map_token) { 3237db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3238db94535dSChris Mason sizeof(struct btrfs_item), 3239db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3240db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3241db94535dSChris Mason KM_USER1); 3242db94535dSChris Mason } 32435f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 32445f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff - data_size); 32456567e837SChris Mason } 32465f39d397SChris Mason 3247db94535dSChris Mason if (leaf->map_token) { 3248db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3249db94535dSChris Mason leaf->map_token = NULL; 3250db94535dSChris Mason } 3251db94535dSChris Mason 32526567e837SChris Mason /* shift the data */ 32535f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 32546567e837SChris Mason data_end - data_size, btrfs_leaf_data(leaf) + 32556567e837SChris Mason data_end, old_data - data_end); 32565f39d397SChris Mason 32576567e837SChris Mason data_end = old_data; 32585f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 32595f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 32605f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 32615f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 32626567e837SChris Mason 32636567e837SChris Mason ret = 0; 32645f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 32655f39d397SChris Mason btrfs_print_leaf(root, leaf); 32666567e837SChris Mason BUG(); 32675f39d397SChris Mason } 32686567e837SChris Mason return ret; 32696567e837SChris Mason } 32706567e837SChris Mason 327174123bd7SChris Mason /* 3272d352ac68SChris Mason * Given a key and some data, insert items into the tree. 327374123bd7SChris Mason * This does all the path init required, making room in the tree if needed. 3274f3465ca4SJosef Bacik * Returns the number of keys that were inserted. 3275f3465ca4SJosef Bacik */ 3276f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans, 3277f3465ca4SJosef Bacik struct btrfs_root *root, 3278f3465ca4SJosef Bacik struct btrfs_path *path, 3279f3465ca4SJosef Bacik struct btrfs_key *cpu_key, u32 *data_size, 3280f3465ca4SJosef Bacik int nr) 3281f3465ca4SJosef Bacik { 3282f3465ca4SJosef Bacik struct extent_buffer *leaf; 3283f3465ca4SJosef Bacik struct btrfs_item *item; 3284f3465ca4SJosef Bacik int ret = 0; 3285f3465ca4SJosef Bacik int slot; 3286f3465ca4SJosef Bacik int i; 3287f3465ca4SJosef Bacik u32 nritems; 3288f3465ca4SJosef Bacik u32 total_data = 0; 3289f3465ca4SJosef Bacik u32 total_size = 0; 3290f3465ca4SJosef Bacik unsigned int data_end; 3291f3465ca4SJosef Bacik struct btrfs_disk_key disk_key; 3292f3465ca4SJosef Bacik struct btrfs_key found_key; 3293f3465ca4SJosef Bacik 329487b29b20SYan Zheng for (i = 0; i < nr; i++) { 329587b29b20SYan Zheng if (total_size + data_size[i] + sizeof(struct btrfs_item) > 329687b29b20SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 329787b29b20SYan Zheng break; 329887b29b20SYan Zheng nr = i; 329987b29b20SYan Zheng } 3300f3465ca4SJosef Bacik total_data += data_size[i]; 330187b29b20SYan Zheng total_size += data_size[i] + sizeof(struct btrfs_item); 330287b29b20SYan Zheng } 330387b29b20SYan Zheng BUG_ON(nr == 0); 3304f3465ca4SJosef Bacik 3305f3465ca4SJosef Bacik ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 3306f3465ca4SJosef Bacik if (ret == 0) 3307f3465ca4SJosef Bacik return -EEXIST; 3308f3465ca4SJosef Bacik if (ret < 0) 3309f3465ca4SJosef Bacik goto out; 3310f3465ca4SJosef Bacik 3311f3465ca4SJosef Bacik leaf = path->nodes[0]; 3312f3465ca4SJosef Bacik 3313f3465ca4SJosef Bacik nritems = btrfs_header_nritems(leaf); 3314f3465ca4SJosef Bacik data_end = leaf_data_end(root, leaf); 3315f3465ca4SJosef Bacik 3316f3465ca4SJosef Bacik if (btrfs_leaf_free_space(root, leaf) < total_size) { 3317f3465ca4SJosef Bacik for (i = nr; i >= 0; i--) { 3318f3465ca4SJosef Bacik total_data -= data_size[i]; 3319f3465ca4SJosef Bacik total_size -= data_size[i] + sizeof(struct btrfs_item); 3320f3465ca4SJosef Bacik if (total_size < btrfs_leaf_free_space(root, leaf)) 3321f3465ca4SJosef Bacik break; 3322f3465ca4SJosef Bacik } 3323f3465ca4SJosef Bacik nr = i; 3324f3465ca4SJosef Bacik } 3325f3465ca4SJosef Bacik 3326f3465ca4SJosef Bacik slot = path->slots[0]; 3327f3465ca4SJosef Bacik BUG_ON(slot < 0); 3328f3465ca4SJosef Bacik 3329f3465ca4SJosef Bacik if (slot != nritems) { 3330f3465ca4SJosef Bacik unsigned int old_data = btrfs_item_end_nr(leaf, slot); 3331f3465ca4SJosef Bacik 3332f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, slot); 3333f3465ca4SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 3334f3465ca4SJosef Bacik 3335f3465ca4SJosef Bacik /* figure out how many keys we can insert in here */ 3336f3465ca4SJosef Bacik total_data = data_size[0]; 3337f3465ca4SJosef Bacik for (i = 1; i < nr; i++) { 3338f3465ca4SJosef Bacik if (comp_cpu_keys(&found_key, cpu_key + i) <= 0) 3339f3465ca4SJosef Bacik break; 3340f3465ca4SJosef Bacik total_data += data_size[i]; 3341f3465ca4SJosef Bacik } 3342f3465ca4SJosef Bacik nr = i; 3343f3465ca4SJosef Bacik 3344f3465ca4SJosef Bacik if (old_data < data_end) { 3345f3465ca4SJosef Bacik btrfs_print_leaf(root, leaf); 3346d397712bSChris Mason printk(KERN_CRIT "slot %d old_data %d data_end %d\n", 3347f3465ca4SJosef Bacik slot, old_data, data_end); 3348f3465ca4SJosef Bacik BUG_ON(1); 3349f3465ca4SJosef Bacik } 3350f3465ca4SJosef Bacik /* 3351f3465ca4SJosef Bacik * item0..itemN ... dataN.offset..dataN.size .. data0.size 3352f3465ca4SJosef Bacik */ 3353f3465ca4SJosef Bacik /* first correct the data pointers */ 3354f3465ca4SJosef Bacik WARN_ON(leaf->map_token); 3355f3465ca4SJosef Bacik for (i = slot; i < nritems; i++) { 3356f3465ca4SJosef Bacik u32 ioff; 3357f3465ca4SJosef Bacik 3358f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, i); 3359f3465ca4SJosef Bacik if (!leaf->map_token) { 3360f3465ca4SJosef Bacik map_extent_buffer(leaf, (unsigned long)item, 3361f3465ca4SJosef Bacik sizeof(struct btrfs_item), 3362f3465ca4SJosef Bacik &leaf->map_token, &leaf->kaddr, 3363f3465ca4SJosef Bacik &leaf->map_start, &leaf->map_len, 3364f3465ca4SJosef Bacik KM_USER1); 3365f3465ca4SJosef Bacik } 3366f3465ca4SJosef Bacik 3367f3465ca4SJosef Bacik ioff = btrfs_item_offset(leaf, item); 3368f3465ca4SJosef Bacik btrfs_set_item_offset(leaf, item, ioff - total_data); 3369f3465ca4SJosef Bacik } 3370f3465ca4SJosef Bacik if (leaf->map_token) { 3371f3465ca4SJosef Bacik unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3372f3465ca4SJosef Bacik leaf->map_token = NULL; 3373f3465ca4SJosef Bacik } 3374f3465ca4SJosef Bacik 3375f3465ca4SJosef Bacik /* shift the items */ 3376f3465ca4SJosef Bacik memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 3377f3465ca4SJosef Bacik btrfs_item_nr_offset(slot), 3378f3465ca4SJosef Bacik (nritems - slot) * sizeof(struct btrfs_item)); 3379f3465ca4SJosef Bacik 3380f3465ca4SJosef Bacik /* shift the data */ 3381f3465ca4SJosef Bacik memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3382f3465ca4SJosef Bacik data_end - total_data, btrfs_leaf_data(leaf) + 3383f3465ca4SJosef Bacik data_end, old_data - data_end); 3384f3465ca4SJosef Bacik data_end = old_data; 3385f3465ca4SJosef Bacik } else { 3386f3465ca4SJosef Bacik /* 3387f3465ca4SJosef Bacik * this sucks but it has to be done, if we are inserting at 3388f3465ca4SJosef Bacik * the end of the leaf only insert 1 of the items, since we 3389f3465ca4SJosef Bacik * have no way of knowing whats on the next leaf and we'd have 3390f3465ca4SJosef Bacik * to drop our current locks to figure it out 3391f3465ca4SJosef Bacik */ 3392f3465ca4SJosef Bacik nr = 1; 3393f3465ca4SJosef Bacik } 3394f3465ca4SJosef Bacik 3395f3465ca4SJosef Bacik /* setup the item for the new data */ 3396f3465ca4SJosef Bacik for (i = 0; i < nr; i++) { 3397f3465ca4SJosef Bacik btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 3398f3465ca4SJosef Bacik btrfs_set_item_key(leaf, &disk_key, slot + i); 3399f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, slot + i); 3400f3465ca4SJosef Bacik btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 3401f3465ca4SJosef Bacik data_end -= data_size[i]; 3402f3465ca4SJosef Bacik btrfs_set_item_size(leaf, item, data_size[i]); 3403f3465ca4SJosef Bacik } 3404f3465ca4SJosef Bacik btrfs_set_header_nritems(leaf, nritems + nr); 3405f3465ca4SJosef Bacik btrfs_mark_buffer_dirty(leaf); 3406f3465ca4SJosef Bacik 3407f3465ca4SJosef Bacik ret = 0; 3408f3465ca4SJosef Bacik if (slot == 0) { 3409f3465ca4SJosef Bacik btrfs_cpu_key_to_disk(&disk_key, cpu_key); 3410f3465ca4SJosef Bacik ret = fixup_low_keys(trans, root, path, &disk_key, 1); 3411f3465ca4SJosef Bacik } 3412f3465ca4SJosef Bacik 3413f3465ca4SJosef Bacik if (btrfs_leaf_free_space(root, leaf) < 0) { 3414f3465ca4SJosef Bacik btrfs_print_leaf(root, leaf); 3415f3465ca4SJosef Bacik BUG(); 3416f3465ca4SJosef Bacik } 3417f3465ca4SJosef Bacik out: 3418f3465ca4SJosef Bacik if (!ret) 3419f3465ca4SJosef Bacik ret = nr; 3420f3465ca4SJosef Bacik return ret; 3421f3465ca4SJosef Bacik } 3422f3465ca4SJosef Bacik 3423f3465ca4SJosef Bacik /* 3424f3465ca4SJosef Bacik * Given a key and some data, insert items into the tree. 3425f3465ca4SJosef Bacik * This does all the path init required, making room in the tree if needed. 342674123bd7SChris Mason */ 34279c58309dSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 34285f39d397SChris Mason struct btrfs_root *root, 34295f39d397SChris Mason struct btrfs_path *path, 34309c58309dSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 34319c58309dSChris Mason int nr) 3432be0e5c09SChris Mason { 34335f39d397SChris Mason struct extent_buffer *leaf; 34345f39d397SChris Mason struct btrfs_item *item; 3435aa5d6bedSChris Mason int ret = 0; 3436be0e5c09SChris Mason int slot; 3437eb60ceacSChris Mason int slot_orig; 34389c58309dSChris Mason int i; 34397518a238SChris Mason u32 nritems; 34409c58309dSChris Mason u32 total_size = 0; 34419c58309dSChris Mason u32 total_data = 0; 3442be0e5c09SChris Mason unsigned int data_end; 3443e2fa7227SChris Mason struct btrfs_disk_key disk_key; 3444e2fa7227SChris Mason 3445d397712bSChris Mason for (i = 0; i < nr; i++) 34469c58309dSChris Mason total_data += data_size[i]; 3447be0e5c09SChris Mason 34487b128766SJosef Bacik total_size = total_data + (nr * sizeof(struct btrfs_item)); 34499c58309dSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 34500f9dd46cSJosef Bacik if (ret == 0) 3451f0930a37SChris Mason return -EEXIST; 3452ed2ff2cbSChris Mason if (ret < 0) 3453ed2ff2cbSChris Mason goto out; 3454be0e5c09SChris Mason 345562e2749eSChris Mason slot_orig = path->slots[0]; 34565f39d397SChris Mason leaf = path->nodes[0]; 345774123bd7SChris Mason 34585f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3459123abc88SChris Mason data_end = leaf_data_end(root, leaf); 3460eb60ceacSChris Mason 3461f25956ccSChris Mason if (btrfs_leaf_free_space(root, leaf) < total_size) { 34623326d1b0SChris Mason btrfs_print_leaf(root, leaf); 3463d397712bSChris Mason printk(KERN_CRIT "not enough freespace need %u have %d\n", 34649c58309dSChris Mason total_size, btrfs_leaf_free_space(root, leaf)); 3465be0e5c09SChris Mason BUG(); 3466d4dbff95SChris Mason } 34675f39d397SChris Mason 346862e2749eSChris Mason slot = path->slots[0]; 3469eb60ceacSChris Mason BUG_ON(slot < 0); 34705f39d397SChris Mason 3471be0e5c09SChris Mason if (slot != nritems) { 34725f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 3473be0e5c09SChris Mason 34745f39d397SChris Mason if (old_data < data_end) { 34755f39d397SChris Mason btrfs_print_leaf(root, leaf); 3476d397712bSChris Mason printk(KERN_CRIT "slot %d old_data %d data_end %d\n", 34775f39d397SChris Mason slot, old_data, data_end); 34785f39d397SChris Mason BUG_ON(1); 34795f39d397SChris Mason } 3480be0e5c09SChris Mason /* 3481be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 3482be0e5c09SChris Mason */ 3483be0e5c09SChris Mason /* first correct the data pointers */ 3484db94535dSChris Mason WARN_ON(leaf->map_token); 34850783fcfcSChris Mason for (i = slot; i < nritems; i++) { 34865f39d397SChris Mason u32 ioff; 3487db94535dSChris Mason 34885f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3489db94535dSChris Mason if (!leaf->map_token) { 3490db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3491db94535dSChris Mason sizeof(struct btrfs_item), 3492db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3493db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3494db94535dSChris Mason KM_USER1); 3495db94535dSChris Mason } 3496db94535dSChris Mason 34975f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 34989c58309dSChris Mason btrfs_set_item_offset(leaf, item, ioff - total_data); 34990783fcfcSChris Mason } 3500db94535dSChris Mason if (leaf->map_token) { 3501db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3502db94535dSChris Mason leaf->map_token = NULL; 3503db94535dSChris Mason } 3504be0e5c09SChris Mason 3505be0e5c09SChris Mason /* shift the items */ 35069c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 35075f39d397SChris Mason btrfs_item_nr_offset(slot), 35080783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 3509be0e5c09SChris Mason 3510be0e5c09SChris Mason /* shift the data */ 35115f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 35129c58309dSChris Mason data_end - total_data, btrfs_leaf_data(leaf) + 3513be0e5c09SChris Mason data_end, old_data - data_end); 3514be0e5c09SChris Mason data_end = old_data; 3515be0e5c09SChris Mason } 35165f39d397SChris Mason 351762e2749eSChris Mason /* setup the item for the new data */ 35189c58309dSChris Mason for (i = 0; i < nr; i++) { 35199c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 35209c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 35219c58309dSChris Mason item = btrfs_item_nr(leaf, slot + i); 35229c58309dSChris Mason btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 35239c58309dSChris Mason data_end -= data_size[i]; 35249c58309dSChris Mason btrfs_set_item_size(leaf, item, data_size[i]); 35259c58309dSChris Mason } 35269c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 35275f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3528aa5d6bedSChris Mason 3529aa5d6bedSChris Mason ret = 0; 35305a01a2e3SChris Mason if (slot == 0) { 35315a01a2e3SChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key); 3532e089f05cSChris Mason ret = fixup_low_keys(trans, root, path, &disk_key, 1); 35335a01a2e3SChris Mason } 3534aa5d6bedSChris Mason 35355f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 35365f39d397SChris Mason btrfs_print_leaf(root, leaf); 3537be0e5c09SChris Mason BUG(); 35385f39d397SChris Mason } 3539ed2ff2cbSChris Mason out: 3540b4ce94deSChris Mason btrfs_unlock_up_safe(path, 1); 354162e2749eSChris Mason return ret; 354262e2749eSChris Mason } 354362e2749eSChris Mason 354462e2749eSChris Mason /* 354562e2749eSChris Mason * Given a key and some data, insert an item into the tree. 354662e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 354762e2749eSChris Mason */ 3548e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root 3549e089f05cSChris Mason *root, struct btrfs_key *cpu_key, void *data, u32 3550e089f05cSChris Mason data_size) 355162e2749eSChris Mason { 355262e2749eSChris Mason int ret = 0; 35532c90e5d6SChris Mason struct btrfs_path *path; 35545f39d397SChris Mason struct extent_buffer *leaf; 35555f39d397SChris Mason unsigned long ptr; 355662e2749eSChris Mason 35572c90e5d6SChris Mason path = btrfs_alloc_path(); 35582c90e5d6SChris Mason BUG_ON(!path); 35592c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 356062e2749eSChris Mason if (!ret) { 35615f39d397SChris Mason leaf = path->nodes[0]; 35625f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 35635f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 35645f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 356562e2749eSChris Mason } 35662c90e5d6SChris Mason btrfs_free_path(path); 3567aa5d6bedSChris Mason return ret; 3568be0e5c09SChris Mason } 3569be0e5c09SChris Mason 357074123bd7SChris Mason /* 35715de08d7dSChris Mason * delete the pointer from a given node. 357274123bd7SChris Mason * 3573d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 3574d352ac68SChris Mason * empty a node. 357574123bd7SChris Mason */ 3576e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3577e089f05cSChris Mason struct btrfs_path *path, int level, int slot) 3578be0e5c09SChris Mason { 35795f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 35807518a238SChris Mason u32 nritems; 3581aa5d6bedSChris Mason int ret = 0; 3582bb803951SChris Mason int wret; 3583be0e5c09SChris Mason 35845f39d397SChris Mason nritems = btrfs_header_nritems(parent); 3585be0e5c09SChris Mason if (slot != nritems - 1) { 35865f39d397SChris Mason memmove_extent_buffer(parent, 35875f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 35885f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 3589d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 3590d6025579SChris Mason (nritems - slot - 1)); 3591be0e5c09SChris Mason } 35927518a238SChris Mason nritems--; 35935f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 35947518a238SChris Mason if (nritems == 0 && parent == root->node) { 35955f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 3596eb60ceacSChris Mason /* just turn the root into a leaf and break */ 35975f39d397SChris Mason btrfs_set_header_level(root->node, 0); 3598bb803951SChris Mason } else if (slot == 0) { 35995f39d397SChris Mason struct btrfs_disk_key disk_key; 36005f39d397SChris Mason 36015f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 36025f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); 36030f70abe2SChris Mason if (wret) 36040f70abe2SChris Mason ret = wret; 3605be0e5c09SChris Mason } 3606d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 3607aa5d6bedSChris Mason return ret; 3608be0e5c09SChris Mason } 3609be0e5c09SChris Mason 361074123bd7SChris Mason /* 3611323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 3612323ac95bSChris Mason * path->nodes[1]. bytenr is the node block pointer, but since the callers 3613323ac95bSChris Mason * already know it, it is faster to have them pass it down than to 3614323ac95bSChris Mason * read it out of the node again. 3615323ac95bSChris Mason * 3616323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 3617323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 3618323ac95bSChris Mason * 3619323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 3620323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 3621323ac95bSChris Mason */ 3622323ac95bSChris Mason noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans, 3623323ac95bSChris Mason struct btrfs_root *root, 3624323ac95bSChris Mason struct btrfs_path *path, u64 bytenr) 3625323ac95bSChris Mason { 3626323ac95bSChris Mason int ret; 3627323ac95bSChris Mason u64 root_gen = btrfs_header_generation(path->nodes[1]); 36284d081c41SChris Mason u64 parent_start = path->nodes[1]->start; 36294d081c41SChris Mason u64 parent_owner = btrfs_header_owner(path->nodes[1]); 3630323ac95bSChris Mason 3631323ac95bSChris Mason ret = del_ptr(trans, root, path, 1, path->slots[1]); 3632323ac95bSChris Mason if (ret) 3633323ac95bSChris Mason return ret; 3634323ac95bSChris Mason 36354d081c41SChris Mason /* 36364d081c41SChris Mason * btrfs_free_extent is expensive, we want to make sure we 36374d081c41SChris Mason * aren't holding any locks when we call it 36384d081c41SChris Mason */ 36394d081c41SChris Mason btrfs_unlock_up_safe(path, 0); 36404d081c41SChris Mason 3641323ac95bSChris Mason ret = btrfs_free_extent(trans, root, bytenr, 3642323ac95bSChris Mason btrfs_level_size(root, 0), 36434d081c41SChris Mason parent_start, parent_owner, 36443bb1a1bcSYan Zheng root_gen, 0, 1); 3645323ac95bSChris Mason return ret; 3646323ac95bSChris Mason } 3647323ac95bSChris Mason /* 364874123bd7SChris Mason * delete the item at the leaf level in path. If that empties 364974123bd7SChris Mason * the leaf, remove it from the tree 365074123bd7SChris Mason */ 365185e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 365285e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 3653be0e5c09SChris Mason { 36545f39d397SChris Mason struct extent_buffer *leaf; 36555f39d397SChris Mason struct btrfs_item *item; 365685e21bacSChris Mason int last_off; 365785e21bacSChris Mason int dsize = 0; 3658aa5d6bedSChris Mason int ret = 0; 3659aa5d6bedSChris Mason int wret; 366085e21bacSChris Mason int i; 36617518a238SChris Mason u32 nritems; 3662be0e5c09SChris Mason 36635f39d397SChris Mason leaf = path->nodes[0]; 366485e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 366585e21bacSChris Mason 366685e21bacSChris Mason for (i = 0; i < nr; i++) 366785e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 366885e21bacSChris Mason 36695f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3670be0e5c09SChris Mason 367185e21bacSChris Mason if (slot + nr != nritems) { 3672123abc88SChris Mason int data_end = leaf_data_end(root, leaf); 36735f39d397SChris Mason 36745f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3675d6025579SChris Mason data_end + dsize, 3676123abc88SChris Mason btrfs_leaf_data(leaf) + data_end, 367785e21bacSChris Mason last_off - data_end); 36785f39d397SChris Mason 367985e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 36805f39d397SChris Mason u32 ioff; 3681db94535dSChris Mason 36825f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3683db94535dSChris Mason if (!leaf->map_token) { 3684db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3685db94535dSChris Mason sizeof(struct btrfs_item), 3686db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3687db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3688db94535dSChris Mason KM_USER1); 3689db94535dSChris Mason } 36905f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 36915f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + dsize); 36920783fcfcSChris Mason } 3693db94535dSChris Mason 3694db94535dSChris Mason if (leaf->map_token) { 3695db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3696db94535dSChris Mason leaf->map_token = NULL; 3697db94535dSChris Mason } 3698db94535dSChris Mason 36995f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 370085e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 37010783fcfcSChris Mason sizeof(struct btrfs_item) * 370285e21bacSChris Mason (nritems - slot - nr)); 3703be0e5c09SChris Mason } 370485e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 370585e21bacSChris Mason nritems -= nr; 37065f39d397SChris Mason 370774123bd7SChris Mason /* delete the leaf if we've emptied it */ 37087518a238SChris Mason if (nritems == 0) { 37095f39d397SChris Mason if (leaf == root->node) { 37105f39d397SChris Mason btrfs_set_header_level(leaf, 0); 37119a8dd150SChris Mason } else { 3712323ac95bSChris Mason ret = btrfs_del_leaf(trans, root, path, leaf->start); 3713323ac95bSChris Mason BUG_ON(ret); 37149a8dd150SChris Mason } 3715be0e5c09SChris Mason } else { 37167518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 3717aa5d6bedSChris Mason if (slot == 0) { 37185f39d397SChris Mason struct btrfs_disk_key disk_key; 37195f39d397SChris Mason 37205f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 3721e089f05cSChris Mason wret = fixup_low_keys(trans, root, path, 37225f39d397SChris Mason &disk_key, 1); 3723aa5d6bedSChris Mason if (wret) 3724aa5d6bedSChris Mason ret = wret; 3725aa5d6bedSChris Mason } 3726aa5d6bedSChris Mason 372774123bd7SChris Mason /* delete the leaf if it is mostly empty */ 372885e21bacSChris Mason if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) { 3729be0e5c09SChris Mason /* push_leaf_left fixes the path. 3730be0e5c09SChris Mason * make sure the path still points to our leaf 3731be0e5c09SChris Mason * for possible call to del_ptr below 3732be0e5c09SChris Mason */ 37334920c9acSChris Mason slot = path->slots[1]; 37345f39d397SChris Mason extent_buffer_get(leaf); 37355f39d397SChris Mason 373685e21bacSChris Mason wret = push_leaf_left(trans, root, path, 1, 1); 373754aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3738aa5d6bedSChris Mason ret = wret; 37395f39d397SChris Mason 37405f39d397SChris Mason if (path->nodes[0] == leaf && 37415f39d397SChris Mason btrfs_header_nritems(leaf)) { 374285e21bacSChris Mason wret = push_leaf_right(trans, root, path, 1, 1); 374354aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3744aa5d6bedSChris Mason ret = wret; 3745aa5d6bedSChris Mason } 37465f39d397SChris Mason 37475f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 3748323ac95bSChris Mason path->slots[1] = slot; 3749d397712bSChris Mason ret = btrfs_del_leaf(trans, root, path, 3750d397712bSChris Mason leaf->start); 3751323ac95bSChris Mason BUG_ON(ret); 37525f39d397SChris Mason free_extent_buffer(leaf); 37535de08d7dSChris Mason } else { 3754925baeddSChris Mason /* if we're still in the path, make sure 3755925baeddSChris Mason * we're dirty. Otherwise, one of the 3756925baeddSChris Mason * push_leaf functions must have already 3757925baeddSChris Mason * dirtied this buffer 3758925baeddSChris Mason */ 3759925baeddSChris Mason if (path->nodes[0] == leaf) 37605f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 37615f39d397SChris Mason free_extent_buffer(leaf); 3762be0e5c09SChris Mason } 3763d5719762SChris Mason } else { 37645f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3765be0e5c09SChris Mason } 37669a8dd150SChris Mason } 3767aa5d6bedSChris Mason return ret; 37689a8dd150SChris Mason } 37699a8dd150SChris Mason 377097571fd0SChris Mason /* 3771925baeddSChris Mason * search the tree again to find a leaf with lesser keys 37727bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 37737bb86316SChris Mason * returns < 0 on io errors. 3774d352ac68SChris Mason * 3775d352ac68SChris Mason * This may release the path, and so you may lose any locks held at the 3776d352ac68SChris Mason * time you call it. 37777bb86316SChris Mason */ 37787bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 37797bb86316SChris Mason { 3780925baeddSChris Mason struct btrfs_key key; 3781925baeddSChris Mason struct btrfs_disk_key found_key; 3782925baeddSChris Mason int ret; 37837bb86316SChris Mason 3784925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 3785925baeddSChris Mason 3786925baeddSChris Mason if (key.offset > 0) 3787925baeddSChris Mason key.offset--; 3788925baeddSChris Mason else if (key.type > 0) 3789925baeddSChris Mason key.type--; 3790925baeddSChris Mason else if (key.objectid > 0) 3791925baeddSChris Mason key.objectid--; 3792925baeddSChris Mason else 37937bb86316SChris Mason return 1; 37947bb86316SChris Mason 3795925baeddSChris Mason btrfs_release_path(root, path); 3796925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 3797925baeddSChris Mason if (ret < 0) 3798925baeddSChris Mason return ret; 3799925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 3800925baeddSChris Mason ret = comp_keys(&found_key, &key); 3801925baeddSChris Mason if (ret < 0) 38027bb86316SChris Mason return 0; 3803925baeddSChris Mason return 1; 38047bb86316SChris Mason } 38057bb86316SChris Mason 38063f157a2fSChris Mason /* 38073f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 38083f157a2fSChris Mason * for nodes or leaves that are either in cache or have a minimum 3809d352ac68SChris Mason * transaction id. This is used by the btree defrag code, and tree logging 38103f157a2fSChris Mason * 38113f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 38123f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 38133f157a2fSChris Mason * key and get a writable path. 38143f157a2fSChris Mason * 38153f157a2fSChris Mason * This does lock as it descends, and path->keep_locks should be set 38163f157a2fSChris Mason * to 1 by the caller. 38173f157a2fSChris Mason * 38183f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 38193f157a2fSChris Mason * of the tree. 38203f157a2fSChris Mason * 3821d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 3822d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 3823d352ac68SChris Mason * skipped over (without reading them). 3824d352ac68SChris Mason * 38253f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 38263f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 38273f157a2fSChris Mason */ 38283f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 3829e02119d5SChris Mason struct btrfs_key *max_key, 38303f157a2fSChris Mason struct btrfs_path *path, int cache_only, 38313f157a2fSChris Mason u64 min_trans) 38323f157a2fSChris Mason { 38333f157a2fSChris Mason struct extent_buffer *cur; 38343f157a2fSChris Mason struct btrfs_key found_key; 38353f157a2fSChris Mason int slot; 38369652480bSYan int sret; 38373f157a2fSChris Mason u32 nritems; 38383f157a2fSChris Mason int level; 38393f157a2fSChris Mason int ret = 1; 38403f157a2fSChris Mason 3841934d375bSChris Mason WARN_ON(!path->keep_locks); 38423f157a2fSChris Mason again: 38433f157a2fSChris Mason cur = btrfs_lock_root_node(root); 38443f157a2fSChris Mason level = btrfs_header_level(cur); 3845e02119d5SChris Mason WARN_ON(path->nodes[level]); 38463f157a2fSChris Mason path->nodes[level] = cur; 38473f157a2fSChris Mason path->locks[level] = 1; 38483f157a2fSChris Mason 38493f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 38503f157a2fSChris Mason ret = 1; 38513f157a2fSChris Mason goto out; 38523f157a2fSChris Mason } 38533f157a2fSChris Mason while (1) { 38543f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 38553f157a2fSChris Mason level = btrfs_header_level(cur); 38569652480bSYan sret = bin_search(cur, min_key, level, &slot); 38573f157a2fSChris Mason 3858323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 3859323ac95bSChris Mason if (level == path->lowest_level) { 3860e02119d5SChris Mason if (slot >= nritems) 3861e02119d5SChris Mason goto find_next_key; 38623f157a2fSChris Mason ret = 0; 38633f157a2fSChris Mason path->slots[level] = slot; 38643f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 38653f157a2fSChris Mason goto out; 38663f157a2fSChris Mason } 38679652480bSYan if (sret && slot > 0) 38689652480bSYan slot--; 38693f157a2fSChris Mason /* 38703f157a2fSChris Mason * check this node pointer against the cache_only and 38713f157a2fSChris Mason * min_trans parameters. If it isn't in cache or is too 38723f157a2fSChris Mason * old, skip to the next one. 38733f157a2fSChris Mason */ 38743f157a2fSChris Mason while (slot < nritems) { 38753f157a2fSChris Mason u64 blockptr; 38763f157a2fSChris Mason u64 gen; 38773f157a2fSChris Mason struct extent_buffer *tmp; 3878e02119d5SChris Mason struct btrfs_disk_key disk_key; 3879e02119d5SChris Mason 38803f157a2fSChris Mason blockptr = btrfs_node_blockptr(cur, slot); 38813f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 38823f157a2fSChris Mason if (gen < min_trans) { 38833f157a2fSChris Mason slot++; 38843f157a2fSChris Mason continue; 38853f157a2fSChris Mason } 38863f157a2fSChris Mason if (!cache_only) 38873f157a2fSChris Mason break; 38883f157a2fSChris Mason 3889e02119d5SChris Mason if (max_key) { 3890e02119d5SChris Mason btrfs_node_key(cur, &disk_key, slot); 3891e02119d5SChris Mason if (comp_keys(&disk_key, max_key) >= 0) { 3892e02119d5SChris Mason ret = 1; 3893e02119d5SChris Mason goto out; 3894e02119d5SChris Mason } 3895e02119d5SChris Mason } 3896e02119d5SChris Mason 38973f157a2fSChris Mason tmp = btrfs_find_tree_block(root, blockptr, 38983f157a2fSChris Mason btrfs_level_size(root, level - 1)); 38993f157a2fSChris Mason 39003f157a2fSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 39013f157a2fSChris Mason free_extent_buffer(tmp); 39023f157a2fSChris Mason break; 39033f157a2fSChris Mason } 39043f157a2fSChris Mason if (tmp) 39053f157a2fSChris Mason free_extent_buffer(tmp); 39063f157a2fSChris Mason slot++; 39073f157a2fSChris Mason } 3908e02119d5SChris Mason find_next_key: 39093f157a2fSChris Mason /* 39103f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 39113f157a2fSChris Mason * and find another one 39123f157a2fSChris Mason */ 39133f157a2fSChris Mason if (slot >= nritems) { 3914e02119d5SChris Mason path->slots[level] = slot; 3915b4ce94deSChris Mason btrfs_set_path_blocking(path); 3916e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 39173f157a2fSChris Mason cache_only, min_trans); 3918e02119d5SChris Mason if (sret == 0) { 39193f157a2fSChris Mason btrfs_release_path(root, path); 39203f157a2fSChris Mason goto again; 39213f157a2fSChris Mason } else { 3922b4ce94deSChris Mason btrfs_clear_path_blocking(path); 39233f157a2fSChris Mason goto out; 39243f157a2fSChris Mason } 39253f157a2fSChris Mason } 39263f157a2fSChris Mason /* save our key for returning back */ 39273f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 39283f157a2fSChris Mason path->slots[level] = slot; 39293f157a2fSChris Mason if (level == path->lowest_level) { 39303f157a2fSChris Mason ret = 0; 39313f157a2fSChris Mason unlock_up(path, level, 1); 39323f157a2fSChris Mason goto out; 39333f157a2fSChris Mason } 3934b4ce94deSChris Mason btrfs_set_path_blocking(path); 39353f157a2fSChris Mason cur = read_node_slot(root, cur, slot); 39363f157a2fSChris Mason 39373f157a2fSChris Mason btrfs_tree_lock(cur); 3938b4ce94deSChris Mason 39393f157a2fSChris Mason path->locks[level - 1] = 1; 39403f157a2fSChris Mason path->nodes[level - 1] = cur; 39413f157a2fSChris Mason unlock_up(path, level, 1); 3942b4ce94deSChris Mason btrfs_clear_path_blocking(path); 39433f157a2fSChris Mason } 39443f157a2fSChris Mason out: 39453f157a2fSChris Mason if (ret == 0) 39463f157a2fSChris Mason memcpy(min_key, &found_key, sizeof(found_key)); 3947b4ce94deSChris Mason btrfs_set_path_blocking(path); 39483f157a2fSChris Mason return ret; 39493f157a2fSChris Mason } 39503f157a2fSChris Mason 39513f157a2fSChris Mason /* 39523f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 39533f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 39543f157a2fSChris Mason * tree based on the current path and the cache_only and min_trans 39553f157a2fSChris Mason * parameters. 39563f157a2fSChris Mason * 39573f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 39583f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 39593f157a2fSChris Mason * 39603f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 39613f157a2fSChris Mason * calling this function. 39623f157a2fSChris Mason */ 3963e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 39643f157a2fSChris Mason struct btrfs_key *key, int lowest_level, 39653f157a2fSChris Mason int cache_only, u64 min_trans) 3966e7a84565SChris Mason { 3967e7a84565SChris Mason int level = lowest_level; 3968e7a84565SChris Mason int slot; 3969e7a84565SChris Mason struct extent_buffer *c; 3970e7a84565SChris Mason 3971934d375bSChris Mason WARN_ON(!path->keep_locks); 3972e7a84565SChris Mason while (level < BTRFS_MAX_LEVEL) { 3973e7a84565SChris Mason if (!path->nodes[level]) 3974e7a84565SChris Mason return 1; 3975e7a84565SChris Mason 3976e7a84565SChris Mason slot = path->slots[level] + 1; 3977e7a84565SChris Mason c = path->nodes[level]; 39783f157a2fSChris Mason next: 3979e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 3980e7a84565SChris Mason level++; 3981d397712bSChris Mason if (level == BTRFS_MAX_LEVEL) 3982e7a84565SChris Mason return 1; 3983e7a84565SChris Mason continue; 3984e7a84565SChris Mason } 3985e7a84565SChris Mason if (level == 0) 3986e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 39873f157a2fSChris Mason else { 39883f157a2fSChris Mason u64 blockptr = btrfs_node_blockptr(c, slot); 39893f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 39903f157a2fSChris Mason 39913f157a2fSChris Mason if (cache_only) { 39923f157a2fSChris Mason struct extent_buffer *cur; 39933f157a2fSChris Mason cur = btrfs_find_tree_block(root, blockptr, 39943f157a2fSChris Mason btrfs_level_size(root, level - 1)); 39953f157a2fSChris Mason if (!cur || !btrfs_buffer_uptodate(cur, gen)) { 39963f157a2fSChris Mason slot++; 39973f157a2fSChris Mason if (cur) 39983f157a2fSChris Mason free_extent_buffer(cur); 39993f157a2fSChris Mason goto next; 40003f157a2fSChris Mason } 40013f157a2fSChris Mason free_extent_buffer(cur); 40023f157a2fSChris Mason } 40033f157a2fSChris Mason if (gen < min_trans) { 40043f157a2fSChris Mason slot++; 40053f157a2fSChris Mason goto next; 40063f157a2fSChris Mason } 4007e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 40083f157a2fSChris Mason } 4009e7a84565SChris Mason return 0; 4010e7a84565SChris Mason } 4011e7a84565SChris Mason return 1; 4012e7a84565SChris Mason } 4013e7a84565SChris Mason 40147bb86316SChris Mason /* 4015925baeddSChris Mason * search the tree again to find a leaf with greater keys 40160f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 40170f70abe2SChris Mason * returns < 0 on io errors. 401897571fd0SChris Mason */ 4019234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 4020d97e63b6SChris Mason { 4021d97e63b6SChris Mason int slot; 4022d97e63b6SChris Mason int level = 1; 40235f39d397SChris Mason struct extent_buffer *c; 40245f39d397SChris Mason struct extent_buffer *next = NULL; 4025925baeddSChris Mason struct btrfs_key key; 4026925baeddSChris Mason u32 nritems; 4027925baeddSChris Mason int ret; 4028925baeddSChris Mason 4029925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4030d397712bSChris Mason if (nritems == 0) 4031925baeddSChris Mason return 1; 4032925baeddSChris Mason 4033925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 4034925baeddSChris Mason 4035925baeddSChris Mason btrfs_release_path(root, path); 4036a2135011SChris Mason path->keep_locks = 1; 4037925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4038925baeddSChris Mason path->keep_locks = 0; 4039925baeddSChris Mason 4040925baeddSChris Mason if (ret < 0) 4041925baeddSChris Mason return ret; 4042925baeddSChris Mason 4043b4ce94deSChris Mason btrfs_set_path_blocking(path); 4044a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4045168fd7d2SChris Mason /* 4046168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 4047168fd7d2SChris Mason * could have added more items next to the key that used to be 4048168fd7d2SChris Mason * at the very end of the block. So, check again here and 4049168fd7d2SChris Mason * advance the path if there are now more items available. 4050168fd7d2SChris Mason */ 4051a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 4052168fd7d2SChris Mason path->slots[0]++; 4053925baeddSChris Mason goto done; 4054925baeddSChris Mason } 4055d97e63b6SChris Mason 4056234b63a0SChris Mason while (level < BTRFS_MAX_LEVEL) { 4057d97e63b6SChris Mason if (!path->nodes[level]) 40580f70abe2SChris Mason return 1; 40595f39d397SChris Mason 4060d97e63b6SChris Mason slot = path->slots[level] + 1; 4061d97e63b6SChris Mason c = path->nodes[level]; 40625f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 4063d97e63b6SChris Mason level++; 4064d397712bSChris Mason if (level == BTRFS_MAX_LEVEL) 40657bb86316SChris Mason return 1; 4066d97e63b6SChris Mason continue; 4067d97e63b6SChris Mason } 40685f39d397SChris Mason 4069925baeddSChris Mason if (next) { 4070925baeddSChris Mason btrfs_tree_unlock(next); 40715f39d397SChris Mason free_extent_buffer(next); 4072925baeddSChris Mason } 40735f39d397SChris Mason 4074b4ce94deSChris Mason /* the path was set to blocking above */ 40750bd40a71SChris Mason if (level == 1 && (path->locks[1] || path->skip_locking) && 40760bd40a71SChris Mason path->reada) 407701f46658SChris Mason reada_for_search(root, path, level, slot, 0); 40785f39d397SChris Mason 4079ca7a79adSChris Mason next = read_node_slot(root, c, slot); 40805cd57b2cSChris Mason if (!path->skip_locking) { 4081051e1b9fSChris Mason WARN_ON(!btrfs_tree_locked(c)); 4082925baeddSChris Mason btrfs_tree_lock(next); 4083b4ce94deSChris Mason btrfs_set_lock_blocking(next); 40845cd57b2cSChris Mason } 4085d97e63b6SChris Mason break; 4086d97e63b6SChris Mason } 4087d97e63b6SChris Mason path->slots[level] = slot; 4088d97e63b6SChris Mason while (1) { 4089d97e63b6SChris Mason level--; 4090d97e63b6SChris Mason c = path->nodes[level]; 4091925baeddSChris Mason if (path->locks[level]) 4092925baeddSChris Mason btrfs_tree_unlock(c); 40935f39d397SChris Mason free_extent_buffer(c); 4094d97e63b6SChris Mason path->nodes[level] = next; 4095d97e63b6SChris Mason path->slots[level] = 0; 4096a74a4b97SChris Mason if (!path->skip_locking) 4097925baeddSChris Mason path->locks[level] = 1; 4098d97e63b6SChris Mason if (!level) 4099d97e63b6SChris Mason break; 4100b4ce94deSChris Mason 4101b4ce94deSChris Mason btrfs_set_path_blocking(path); 4102925baeddSChris Mason if (level == 1 && path->locks[1] && path->reada) 4103925baeddSChris Mason reada_for_search(root, path, level, slot, 0); 4104ca7a79adSChris Mason next = read_node_slot(root, next, 0); 41055cd57b2cSChris Mason if (!path->skip_locking) { 4106a2135011SChris Mason WARN_ON(!btrfs_tree_locked(path->nodes[level])); 4107925baeddSChris Mason btrfs_tree_lock(next); 4108b4ce94deSChris Mason btrfs_set_lock_blocking(next); 4109d97e63b6SChris Mason } 41105cd57b2cSChris Mason } 4111925baeddSChris Mason done: 4112925baeddSChris Mason unlock_up(path, 0, 1); 4113d97e63b6SChris Mason return 0; 4114d97e63b6SChris Mason } 41150b86a832SChris Mason 41163f157a2fSChris Mason /* 41173f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 41183f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 41193f157a2fSChris Mason * 41203f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 41213f157a2fSChris Mason */ 41220b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 41230b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 41240b86a832SChris Mason int type) 41250b86a832SChris Mason { 41260b86a832SChris Mason struct btrfs_key found_key; 41270b86a832SChris Mason struct extent_buffer *leaf; 4128e02119d5SChris Mason u32 nritems; 41290b86a832SChris Mason int ret; 41300b86a832SChris Mason 41310b86a832SChris Mason while (1) { 41320b86a832SChris Mason if (path->slots[0] == 0) { 4133b4ce94deSChris Mason btrfs_set_path_blocking(path); 41340b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 41350b86a832SChris Mason if (ret != 0) 41360b86a832SChris Mason return ret; 41370b86a832SChris Mason } else { 41380b86a832SChris Mason path->slots[0]--; 41390b86a832SChris Mason } 41400b86a832SChris Mason leaf = path->nodes[0]; 4141e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 4142e02119d5SChris Mason if (nritems == 0) 4143e02119d5SChris Mason return 1; 4144e02119d5SChris Mason if (path->slots[0] == nritems) 4145e02119d5SChris Mason path->slots[0]--; 4146e02119d5SChris Mason 41470b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 41480b86a832SChris Mason if (found_key.type == type) 41490b86a832SChris Mason return 0; 4150e02119d5SChris Mason if (found_key.objectid < min_objectid) 4151e02119d5SChris Mason break; 4152e02119d5SChris Mason if (found_key.objectid == min_objectid && 4153e02119d5SChris Mason found_key.type < type) 4154e02119d5SChris Mason break; 41550b86a832SChris Mason } 41560b86a832SChris Mason return 1; 41570b86a832SChris Mason } 4158