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 41df24a2b9SChris Mason inline void btrfs_init_path(struct btrfs_path *p) 42df24a2b9SChris Mason { 43df24a2b9SChris Mason memset(p, 0, sizeof(*p)); 44df24a2b9SChris Mason } 45df24a2b9SChris Mason 462c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 472c90e5d6SChris Mason { 48df24a2b9SChris Mason struct btrfs_path *path; 49df24a2b9SChris Mason path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); 502cc58cf2SChris Mason if (path) { 51df24a2b9SChris Mason btrfs_init_path(path); 522cc58cf2SChris Mason path->reada = 1; 532cc58cf2SChris Mason } 54df24a2b9SChris Mason return path; 552c90e5d6SChris Mason } 562c90e5d6SChris Mason 57d352ac68SChris Mason /* this also releases the path */ 582c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 592c90e5d6SChris Mason { 60df24a2b9SChris Mason btrfs_release_path(NULL, p); 612c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 622c90e5d6SChris Mason } 632c90e5d6SChris Mason 64d352ac68SChris Mason /* 65d352ac68SChris Mason * path release drops references on the extent buffers in the path 66d352ac68SChris Mason * and it drops any locks held by this path 67d352ac68SChris Mason * 68d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 69d352ac68SChris Mason */ 70e02119d5SChris Mason void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) 71eb60ceacSChris Mason { 72eb60ceacSChris Mason int i; 73a2135011SChris Mason 74234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 753f157a2fSChris Mason p->slots[i] = 0; 76eb60ceacSChris Mason if (!p->nodes[i]) 77925baeddSChris Mason continue; 78925baeddSChris Mason if (p->locks[i]) { 79925baeddSChris Mason btrfs_tree_unlock(p->nodes[i]); 80925baeddSChris Mason p->locks[i] = 0; 81925baeddSChris Mason } 825f39d397SChris Mason free_extent_buffer(p->nodes[i]); 833f157a2fSChris Mason p->nodes[i] = NULL; 84eb60ceacSChris Mason } 85eb60ceacSChris Mason } 86eb60ceacSChris Mason 87d352ac68SChris Mason /* 88d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 89d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 90d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 91d352ac68SChris Mason * looping required. 92d352ac68SChris Mason * 93d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 94d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 95d352ac68SChris Mason * at any time because there are no locks held. 96d352ac68SChris Mason */ 97925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 98925baeddSChris Mason { 99925baeddSChris Mason struct extent_buffer *eb; 100925baeddSChris Mason spin_lock(&root->node_lock); 101925baeddSChris Mason eb = root->node; 102925baeddSChris Mason extent_buffer_get(eb); 103925baeddSChris Mason spin_unlock(&root->node_lock); 104925baeddSChris Mason return eb; 105925baeddSChris Mason } 106925baeddSChris Mason 107d352ac68SChris Mason /* loop around taking references on and locking the root node of the 108d352ac68SChris Mason * tree until you end up with a lock on the root. A locked buffer 109d352ac68SChris Mason * is returned, with a reference held. 110d352ac68SChris Mason */ 111925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 112925baeddSChris Mason { 113925baeddSChris Mason struct extent_buffer *eb; 114925baeddSChris Mason 115925baeddSChris Mason while(1) { 116925baeddSChris Mason eb = btrfs_root_node(root); 117925baeddSChris Mason btrfs_tree_lock(eb); 118925baeddSChris Mason 119925baeddSChris Mason spin_lock(&root->node_lock); 120925baeddSChris Mason if (eb == root->node) { 121925baeddSChris Mason spin_unlock(&root->node_lock); 122925baeddSChris Mason break; 123925baeddSChris Mason } 124925baeddSChris Mason spin_unlock(&root->node_lock); 125925baeddSChris Mason 126925baeddSChris Mason btrfs_tree_unlock(eb); 127925baeddSChris Mason free_extent_buffer(eb); 128925baeddSChris Mason } 129925baeddSChris Mason return eb; 130925baeddSChris Mason } 131925baeddSChris Mason 132d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get 133d352ac68SChris Mason * put onto a simple dirty list. transaction.c walks this to make sure they 134d352ac68SChris Mason * get properly updated on disk. 135d352ac68SChris Mason */ 1360b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 1370b86a832SChris Mason { 1380b86a832SChris Mason if (root->track_dirty && list_empty(&root->dirty_list)) { 1390b86a832SChris Mason list_add(&root->dirty_list, 1400b86a832SChris Mason &root->fs_info->dirty_cowonly_roots); 1410b86a832SChris Mason } 1420b86a832SChris Mason } 1430b86a832SChris Mason 144d352ac68SChris Mason /* 145d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 146d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 147d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 148d352ac68SChris Mason */ 149be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 150be20aa9dSChris Mason struct btrfs_root *root, 151be20aa9dSChris Mason struct extent_buffer *buf, 152be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 153be20aa9dSChris Mason { 154be20aa9dSChris Mason struct extent_buffer *cow; 155be20aa9dSChris Mason u32 nritems; 156be20aa9dSChris Mason int ret = 0; 157be20aa9dSChris Mason int level; 1584aec2b52SChris Mason struct btrfs_root *new_root; 159be20aa9dSChris Mason 1604aec2b52SChris Mason new_root = kmalloc(sizeof(*new_root), GFP_NOFS); 1614aec2b52SChris Mason if (!new_root) 1624aec2b52SChris Mason return -ENOMEM; 1634aec2b52SChris Mason 1644aec2b52SChris Mason memcpy(new_root, root, sizeof(*new_root)); 1654aec2b52SChris Mason new_root->root_key.objectid = new_root_objectid; 166be20aa9dSChris Mason 167be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != 168be20aa9dSChris Mason root->fs_info->running_transaction->transid); 169be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 170be20aa9dSChris Mason 171be20aa9dSChris Mason level = btrfs_header_level(buf); 172be20aa9dSChris Mason nritems = btrfs_header_nritems(buf); 17331840ae1SZheng Yan 17431840ae1SZheng Yan cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0, 17531840ae1SZheng Yan new_root_objectid, trans->transid, 176be20aa9dSChris Mason level, buf->start, 0); 1774aec2b52SChris Mason if (IS_ERR(cow)) { 1784aec2b52SChris Mason kfree(new_root); 179be20aa9dSChris Mason return PTR_ERR(cow); 1804aec2b52SChris Mason } 181be20aa9dSChris Mason 182be20aa9dSChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 183be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 184be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 185be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 18663b10fc4SChris Mason btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 187be20aa9dSChris Mason 188be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 18931840ae1SZheng Yan ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL); 1904aec2b52SChris Mason kfree(new_root); 1914aec2b52SChris Mason 192be20aa9dSChris Mason if (ret) 193be20aa9dSChris Mason return ret; 194be20aa9dSChris Mason 195be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 196be20aa9dSChris Mason *cow_ret = cow; 197be20aa9dSChris Mason return 0; 198be20aa9dSChris Mason } 199be20aa9dSChris Mason 200d352ac68SChris Mason /* 201d352ac68SChris Mason * does the dirty work in cow of a single block. The parent block 202d352ac68SChris Mason * (if supplied) is updated to point to the new cow copy. The new 203d352ac68SChris Mason * buffer is marked dirty and returned locked. If you modify the block 204d352ac68SChris Mason * it needs to be marked dirty again. 205d352ac68SChris Mason * 206d352ac68SChris Mason * search_start -- an allocation hint for the new block 207d352ac68SChris Mason * 208d352ac68SChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in bytes 209d352ac68SChris Mason * the allocator should try to find free next to the block it returns. This is 210d352ac68SChris Mason * just a hint and may be ignored by the allocator. 211d352ac68SChris Mason * 212d352ac68SChris Mason * prealloc_dest -- if you have already reserved a destination for the cow, 213d352ac68SChris Mason * this uses that block instead of allocating a new one. btrfs_alloc_reserved_extent 214d352ac68SChris Mason * is used to finish the allocation. 215d352ac68SChris Mason */ 216e02119d5SChris Mason int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans, 2175f39d397SChris Mason struct btrfs_root *root, 2185f39d397SChris Mason struct extent_buffer *buf, 2195f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 2205f39d397SChris Mason struct extent_buffer **cow_ret, 22165b51a00SChris Mason u64 search_start, u64 empty_size, 22265b51a00SChris Mason u64 prealloc_dest) 2236702ed49SChris Mason { 22431840ae1SZheng Yan u64 parent_start; 2255f39d397SChris Mason struct extent_buffer *cow; 2267bb86316SChris Mason u32 nritems; 2276702ed49SChris Mason int ret = 0; 2287bb86316SChris Mason int level; 229925baeddSChris Mason int unlock_orig = 0; 2306702ed49SChris Mason 231925baeddSChris Mason if (*cow_ret == buf) 232925baeddSChris Mason unlock_orig = 1; 233925baeddSChris Mason 234925baeddSChris Mason WARN_ON(!btrfs_tree_locked(buf)); 235925baeddSChris Mason 23631840ae1SZheng Yan if (parent) 23731840ae1SZheng Yan parent_start = parent->start; 23831840ae1SZheng Yan else 23931840ae1SZheng Yan parent_start = 0; 24031840ae1SZheng Yan 2417bb86316SChris Mason WARN_ON(root->ref_cows && trans->transid != 2427bb86316SChris Mason root->fs_info->running_transaction->transid); 2436702ed49SChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 2445f39d397SChris Mason 2457bb86316SChris Mason level = btrfs_header_level(buf); 2467bb86316SChris Mason nritems = btrfs_header_nritems(buf); 24731840ae1SZheng Yan 24865b51a00SChris Mason if (prealloc_dest) { 24965b51a00SChris Mason struct btrfs_key ins; 25065b51a00SChris Mason 25165b51a00SChris Mason ins.objectid = prealloc_dest; 25265b51a00SChris Mason ins.offset = buf->len; 25365b51a00SChris Mason ins.type = BTRFS_EXTENT_ITEM_KEY; 25465b51a00SChris Mason 25531840ae1SZheng Yan ret = btrfs_alloc_reserved_extent(trans, root, parent_start, 25665b51a00SChris Mason root->root_key.objectid, 2573bb1a1bcSYan Zheng trans->transid, level, &ins); 25865b51a00SChris Mason BUG_ON(ret); 25965b51a00SChris Mason cow = btrfs_init_new_buffer(trans, root, prealloc_dest, 26065b51a00SChris Mason buf->len); 26165b51a00SChris Mason } else { 262925baeddSChris Mason cow = btrfs_alloc_free_block(trans, root, buf->len, 26331840ae1SZheng Yan parent_start, 2647bb86316SChris Mason root->root_key.objectid, 26531840ae1SZheng Yan trans->transid, level, 26631840ae1SZheng Yan search_start, empty_size); 26765b51a00SChris Mason } 2686702ed49SChris Mason if (IS_ERR(cow)) 2696702ed49SChris Mason return PTR_ERR(cow); 2706702ed49SChris Mason 2715f39d397SChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 272db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 2735f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 2745f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 27563b10fc4SChris Mason btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 2766702ed49SChris Mason 2775f39d397SChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 2785f39d397SChris Mason if (btrfs_header_generation(buf) != trans->transid) { 27931840ae1SZheng Yan u32 nr_extents; 28031840ae1SZheng Yan ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents); 2816702ed49SChris Mason if (ret) 2826702ed49SChris Mason return ret; 28331840ae1SZheng Yan 28431840ae1SZheng Yan ret = btrfs_cache_ref(trans, root, buf, nr_extents); 28531840ae1SZheng Yan WARN_ON(ret); 2861a40e23bSZheng Yan } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) { 2871a40e23bSZheng Yan /* 2881a40e23bSZheng Yan * There are only two places that can drop reference to 2891a40e23bSZheng Yan * tree blocks owned by living reloc trees, one is here, 290*f82d02d9SYan Zheng * the other place is btrfs_drop_subtree. In both places, 2911a40e23bSZheng Yan * we check reference count while tree block is locked. 2921a40e23bSZheng Yan * Furthermore, if reference count is one, it won't get 2931a40e23bSZheng Yan * increased by someone else. 2941a40e23bSZheng Yan */ 2951a40e23bSZheng Yan u32 refs; 2961a40e23bSZheng Yan ret = btrfs_lookup_extent_ref(trans, root, buf->start, 2971a40e23bSZheng Yan buf->len, &refs); 2981a40e23bSZheng Yan BUG_ON(ret); 2991a40e23bSZheng Yan if (refs == 1) { 3001a40e23bSZheng Yan ret = btrfs_update_ref(trans, root, buf, cow, 3011a40e23bSZheng Yan 0, nritems); 3021a40e23bSZheng Yan clean_tree_block(trans, root, buf); 3031a40e23bSZheng Yan } else { 3041a40e23bSZheng Yan ret = btrfs_inc_ref(trans, root, buf, cow, NULL); 3051a40e23bSZheng Yan } 3061a40e23bSZheng Yan BUG_ON(ret); 3076702ed49SChris Mason } else { 30831840ae1SZheng Yan ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems); 30931840ae1SZheng Yan if (ret) 31031840ae1SZheng Yan return ret; 3116702ed49SChris Mason clean_tree_block(trans, root, buf); 3126702ed49SChris Mason } 3136702ed49SChris Mason 3141a40e23bSZheng Yan if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { 3151a40e23bSZheng Yan ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start); 3161a40e23bSZheng Yan WARN_ON(ret); 3171a40e23bSZheng Yan } 3181a40e23bSZheng Yan 3196702ed49SChris Mason if (buf == root->node) { 320925baeddSChris Mason WARN_ON(parent && parent != buf); 321925baeddSChris Mason 322925baeddSChris Mason spin_lock(&root->node_lock); 3236702ed49SChris Mason root->node = cow; 3245f39d397SChris Mason extent_buffer_get(cow); 325925baeddSChris Mason spin_unlock(&root->node_lock); 326925baeddSChris Mason 3276702ed49SChris Mason if (buf != root->commit_root) { 328db94535dSChris Mason btrfs_free_extent(trans, root, buf->start, 32931840ae1SZheng Yan buf->len, buf->start, 33031840ae1SZheng Yan root->root_key.objectid, 33131840ae1SZheng Yan btrfs_header_generation(buf), 3323bb1a1bcSYan Zheng level, 1); 3336702ed49SChris Mason } 3345f39d397SChris Mason free_extent_buffer(buf); 3350b86a832SChris Mason add_root_to_dirty_list(root); 3366702ed49SChris Mason } else { 3375f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 338db94535dSChris Mason cow->start); 33974493f7aSChris Mason WARN_ON(trans->transid == 0); 34074493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 34174493f7aSChris Mason trans->transid); 3426702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 3435f39d397SChris Mason WARN_ON(btrfs_header_generation(parent) != trans->transid); 3447bb86316SChris Mason btrfs_free_extent(trans, root, buf->start, buf->len, 34531840ae1SZheng Yan parent_start, btrfs_header_owner(parent), 3463bb1a1bcSYan Zheng btrfs_header_generation(parent), level, 1); 3476702ed49SChris Mason } 348925baeddSChris Mason if (unlock_orig) 349925baeddSChris Mason btrfs_tree_unlock(buf); 3505f39d397SChris Mason free_extent_buffer(buf); 3516702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 3526702ed49SChris Mason *cow_ret = cow; 3536702ed49SChris Mason return 0; 3546702ed49SChris Mason } 3556702ed49SChris Mason 356d352ac68SChris Mason /* 357d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 358d352ac68SChris Mason * This version of it has extra checks so that a block isn't cow'd more than 359d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 360d352ac68SChris Mason */ 361e02119d5SChris Mason int noinline btrfs_cow_block(struct btrfs_trans_handle *trans, 3625f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 3635f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 36465b51a00SChris Mason struct extent_buffer **cow_ret, u64 prealloc_dest) 36502217ed2SChris Mason { 3666702ed49SChris Mason u64 search_start; 367f510cfecSChris Mason int ret; 368dc17ff8fSChris Mason 369ccd467d6SChris Mason if (trans->transaction != root->fs_info->running_transaction) { 370ccd467d6SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 371ccd467d6SChris Mason root->fs_info->running_transaction->transid); 372ccd467d6SChris Mason WARN_ON(1); 373ccd467d6SChris Mason } 374ccd467d6SChris Mason if (trans->transid != root->fs_info->generation) { 375ccd467d6SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 376ccd467d6SChris Mason root->fs_info->generation); 377ccd467d6SChris Mason WARN_ON(1); 378ccd467d6SChris Mason } 379dc17ff8fSChris Mason 38063b10fc4SChris Mason spin_lock(&root->fs_info->hash_lock); 3815b21f2edSZheng Yan if (btrfs_header_generation(buf) == trans->transid && 3825b21f2edSZheng Yan btrfs_header_owner(buf) == root->root_key.objectid && 38363b10fc4SChris Mason !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 38402217ed2SChris Mason *cow_ret = buf; 38563b10fc4SChris Mason spin_unlock(&root->fs_info->hash_lock); 38665b51a00SChris Mason WARN_ON(prealloc_dest); 38702217ed2SChris Mason return 0; 38802217ed2SChris Mason } 38963b10fc4SChris Mason spin_unlock(&root->fs_info->hash_lock); 3900b86a832SChris Mason search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 391f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 39265b51a00SChris Mason parent_slot, cow_ret, search_start, 0, 39365b51a00SChris Mason prealloc_dest); 394f510cfecSChris Mason return ret; 3952c90e5d6SChris Mason } 3966702ed49SChris Mason 397d352ac68SChris Mason /* 398d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 399d352ac68SChris Mason * node are actually close by 400d352ac68SChris Mason */ 4016b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 4026702ed49SChris Mason { 4036b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 4046702ed49SChris Mason return 1; 4056b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 4066702ed49SChris Mason return 1; 40702217ed2SChris Mason return 0; 40802217ed2SChris Mason } 40902217ed2SChris Mason 410081e9573SChris Mason /* 411081e9573SChris Mason * compare two keys in a memcmp fashion 412081e9573SChris Mason */ 413081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) 414081e9573SChris Mason { 415081e9573SChris Mason struct btrfs_key k1; 416081e9573SChris Mason 417081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 418081e9573SChris Mason 419081e9573SChris Mason if (k1.objectid > k2->objectid) 420081e9573SChris Mason return 1; 421081e9573SChris Mason if (k1.objectid < k2->objectid) 422081e9573SChris Mason return -1; 423081e9573SChris Mason if (k1.type > k2->type) 424081e9573SChris Mason return 1; 425081e9573SChris Mason if (k1.type < k2->type) 426081e9573SChris Mason return -1; 427081e9573SChris Mason if (k1.offset > k2->offset) 428081e9573SChris Mason return 1; 429081e9573SChris Mason if (k1.offset < k2->offset) 430081e9573SChris Mason return -1; 431081e9573SChris Mason return 0; 432081e9573SChris Mason } 433081e9573SChris Mason 434081e9573SChris Mason 435d352ac68SChris Mason /* 436d352ac68SChris Mason * this is used by the defrag code to go through all the 437d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 438d352ac68SChris Mason * disk order is close to key order 439d352ac68SChris Mason */ 4406702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 4415f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 442a6b6e75eSChris Mason int start_slot, int cache_only, u64 *last_ret, 443a6b6e75eSChris Mason struct btrfs_key *progress) 4446702ed49SChris Mason { 4456b80053dSChris Mason struct extent_buffer *cur; 4466702ed49SChris Mason u64 blocknr; 447ca7a79adSChris Mason u64 gen; 448e9d0b13bSChris Mason u64 search_start = *last_ret; 449e9d0b13bSChris Mason u64 last_block = 0; 4506702ed49SChris Mason u64 other; 4516702ed49SChris Mason u32 parent_nritems; 4526702ed49SChris Mason int end_slot; 4536702ed49SChris Mason int i; 4546702ed49SChris Mason int err = 0; 455f2183bdeSChris Mason int parent_level; 4566b80053dSChris Mason int uptodate; 4576b80053dSChris Mason u32 blocksize; 458081e9573SChris Mason int progress_passed = 0; 459081e9573SChris Mason struct btrfs_disk_key disk_key; 4606702ed49SChris Mason 4615708b959SChris Mason parent_level = btrfs_header_level(parent); 4625708b959SChris Mason if (cache_only && parent_level != 1) 4635708b959SChris Mason return 0; 4645708b959SChris Mason 4656702ed49SChris Mason if (trans->transaction != root->fs_info->running_transaction) { 4666702ed49SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 4676702ed49SChris Mason root->fs_info->running_transaction->transid); 4686702ed49SChris Mason WARN_ON(1); 4696702ed49SChris Mason } 4706702ed49SChris Mason if (trans->transid != root->fs_info->generation) { 4716702ed49SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 4726702ed49SChris Mason root->fs_info->generation); 4736702ed49SChris Mason WARN_ON(1); 4746702ed49SChris Mason } 47586479a04SChris Mason 4766b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 4776b80053dSChris Mason blocksize = btrfs_level_size(root, parent_level - 1); 4786702ed49SChris Mason end_slot = parent_nritems; 4796702ed49SChris Mason 4806702ed49SChris Mason if (parent_nritems == 1) 4816702ed49SChris Mason return 0; 4826702ed49SChris Mason 4836702ed49SChris Mason for (i = start_slot; i < end_slot; i++) { 4846702ed49SChris Mason int close = 1; 485a6b6e75eSChris Mason 4865708b959SChris Mason if (!parent->map_token) { 4875708b959SChris Mason map_extent_buffer(parent, 4885708b959SChris Mason btrfs_node_key_ptr_offset(i), 4895708b959SChris Mason sizeof(struct btrfs_key_ptr), 4905708b959SChris Mason &parent->map_token, &parent->kaddr, 4915708b959SChris Mason &parent->map_start, &parent->map_len, 4925708b959SChris Mason KM_USER1); 4935708b959SChris Mason } 494081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 495081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 496081e9573SChris Mason continue; 497081e9573SChris Mason 498081e9573SChris Mason progress_passed = 1; 4996b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 500ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 501e9d0b13bSChris Mason if (last_block == 0) 502e9d0b13bSChris Mason last_block = blocknr; 5035708b959SChris Mason 5046702ed49SChris Mason if (i > 0) { 5056b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 5066b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 5076702ed49SChris Mason } 5080ef3e66bSChris Mason if (!close && i < end_slot - 2) { 5096b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 5106b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 5116702ed49SChris Mason } 512e9d0b13bSChris Mason if (close) { 513e9d0b13bSChris Mason last_block = blocknr; 5146702ed49SChris Mason continue; 515e9d0b13bSChris Mason } 5165708b959SChris Mason if (parent->map_token) { 5175708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 5185708b959SChris Mason KM_USER1); 5195708b959SChris Mason parent->map_token = NULL; 5205708b959SChris Mason } 5216702ed49SChris Mason 5226b80053dSChris Mason cur = btrfs_find_tree_block(root, blocknr, blocksize); 5236b80053dSChris Mason if (cur) 5241259ab75SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen); 5256b80053dSChris Mason else 5266b80053dSChris Mason uptodate = 0; 5275708b959SChris Mason if (!cur || !uptodate) { 5286702ed49SChris Mason if (cache_only) { 5296b80053dSChris Mason free_extent_buffer(cur); 5306702ed49SChris Mason continue; 5316702ed49SChris Mason } 5326b80053dSChris Mason if (!cur) { 5336b80053dSChris Mason cur = read_tree_block(root, blocknr, 534ca7a79adSChris Mason blocksize, gen); 5356b80053dSChris Mason } else if (!uptodate) { 536ca7a79adSChris Mason btrfs_read_buffer(cur, gen); 5376702ed49SChris Mason } 538f2183bdeSChris Mason } 539e9d0b13bSChris Mason if (search_start == 0) 5406b80053dSChris Mason search_start = last_block; 541e9d0b13bSChris Mason 542e7a84565SChris Mason btrfs_tree_lock(cur); 5436b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 544e7a84565SChris Mason &cur, search_start, 5456b80053dSChris Mason min(16 * blocksize, 54665b51a00SChris Mason (end_slot - i) * blocksize), 0); 547252c38f0SYan if (err) { 548e7a84565SChris Mason btrfs_tree_unlock(cur); 5496b80053dSChris Mason free_extent_buffer(cur); 5506702ed49SChris Mason break; 551252c38f0SYan } 552e7a84565SChris Mason search_start = cur->start; 553e7a84565SChris Mason last_block = cur->start; 554f2183bdeSChris Mason *last_ret = search_start; 555e7a84565SChris Mason btrfs_tree_unlock(cur); 556e7a84565SChris Mason free_extent_buffer(cur); 5576702ed49SChris Mason } 5585708b959SChris Mason if (parent->map_token) { 5595708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 5605708b959SChris Mason KM_USER1); 5615708b959SChris Mason parent->map_token = NULL; 5625708b959SChris Mason } 5636702ed49SChris Mason return err; 5646702ed49SChris Mason } 5656702ed49SChris Mason 56674123bd7SChris Mason /* 56774123bd7SChris Mason * The leaf data grows from end-to-front in the node. 56874123bd7SChris Mason * this returns the address of the start of the last item, 56974123bd7SChris Mason * which is the stop of the leaf data stack 57074123bd7SChris Mason */ 571123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root, 5725f39d397SChris Mason struct extent_buffer *leaf) 573be0e5c09SChris Mason { 5745f39d397SChris Mason u32 nr = btrfs_header_nritems(leaf); 575be0e5c09SChris Mason if (nr == 0) 576123abc88SChris Mason return BTRFS_LEAF_DATA_SIZE(root); 5775f39d397SChris Mason return btrfs_item_offset_nr(leaf, nr - 1); 578be0e5c09SChris Mason } 579be0e5c09SChris Mason 580d352ac68SChris Mason /* 581d352ac68SChris Mason * extra debugging checks to make sure all the items in a key are 582d352ac68SChris Mason * well formed and in the proper order 583d352ac68SChris Mason */ 584123abc88SChris Mason static int check_node(struct btrfs_root *root, struct btrfs_path *path, 585123abc88SChris Mason int level) 586aa5d6bedSChris Mason { 5875f39d397SChris Mason struct extent_buffer *parent = NULL; 5885f39d397SChris Mason struct extent_buffer *node = path->nodes[level]; 5895f39d397SChris Mason struct btrfs_disk_key parent_key; 5905f39d397SChris Mason struct btrfs_disk_key node_key; 591aa5d6bedSChris Mason int parent_slot; 5928d7be552SChris Mason int slot; 5938d7be552SChris Mason struct btrfs_key cpukey; 5945f39d397SChris Mason u32 nritems = btrfs_header_nritems(node); 595aa5d6bedSChris Mason 596aa5d6bedSChris Mason if (path->nodes[level + 1]) 5975f39d397SChris Mason parent = path->nodes[level + 1]; 598a1f39630SAneesh 5998d7be552SChris Mason slot = path->slots[level]; 6007518a238SChris Mason BUG_ON(nritems == 0); 6017518a238SChris Mason if (parent) { 602a1f39630SAneesh parent_slot = path->slots[level + 1]; 6035f39d397SChris Mason btrfs_node_key(parent, &parent_key, parent_slot); 6045f39d397SChris Mason btrfs_node_key(node, &node_key, 0); 6055f39d397SChris Mason BUG_ON(memcmp(&parent_key, &node_key, 606e2fa7227SChris Mason sizeof(struct btrfs_disk_key))); 6071d4f8a0cSChris Mason BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 608db94535dSChris Mason btrfs_header_bytenr(node)); 609aa5d6bedSChris Mason } 610123abc88SChris Mason BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); 6118d7be552SChris Mason if (slot != 0) { 6125f39d397SChris Mason btrfs_node_key_to_cpu(node, &cpukey, slot - 1); 6135f39d397SChris Mason btrfs_node_key(node, &node_key, slot); 6145f39d397SChris Mason BUG_ON(comp_keys(&node_key, &cpukey) <= 0); 6158d7be552SChris Mason } 6168d7be552SChris Mason if (slot < nritems - 1) { 6175f39d397SChris Mason btrfs_node_key_to_cpu(node, &cpukey, slot + 1); 6185f39d397SChris Mason btrfs_node_key(node, &node_key, slot); 6195f39d397SChris Mason BUG_ON(comp_keys(&node_key, &cpukey) >= 0); 620aa5d6bedSChris Mason } 621aa5d6bedSChris Mason return 0; 622aa5d6bedSChris Mason } 623aa5d6bedSChris Mason 624d352ac68SChris Mason /* 625d352ac68SChris Mason * extra checking to make sure all the items in a leaf are 626d352ac68SChris Mason * well formed and in the proper order 627d352ac68SChris Mason */ 628123abc88SChris Mason static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, 629123abc88SChris Mason int level) 630aa5d6bedSChris Mason { 6315f39d397SChris Mason struct extent_buffer *leaf = path->nodes[level]; 6325f39d397SChris Mason struct extent_buffer *parent = NULL; 633aa5d6bedSChris Mason int parent_slot; 6348d7be552SChris Mason struct btrfs_key cpukey; 6355f39d397SChris Mason struct btrfs_disk_key parent_key; 6365f39d397SChris Mason struct btrfs_disk_key leaf_key; 6375f39d397SChris Mason int slot = path->slots[0]; 6388d7be552SChris Mason 6395f39d397SChris Mason u32 nritems = btrfs_header_nritems(leaf); 640aa5d6bedSChris Mason 641aa5d6bedSChris Mason if (path->nodes[level + 1]) 6425f39d397SChris Mason parent = path->nodes[level + 1]; 6437518a238SChris Mason 6447518a238SChris Mason if (nritems == 0) 6457518a238SChris Mason return 0; 6467518a238SChris Mason 6477518a238SChris Mason if (parent) { 648a1f39630SAneesh parent_slot = path->slots[level + 1]; 6495f39d397SChris Mason btrfs_node_key(parent, &parent_key, parent_slot); 6505f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, 0); 6516702ed49SChris Mason 6525f39d397SChris Mason BUG_ON(memcmp(&parent_key, &leaf_key, 653e2fa7227SChris Mason sizeof(struct btrfs_disk_key))); 6541d4f8a0cSChris Mason BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 655db94535dSChris Mason btrfs_header_bytenr(leaf)); 656aa5d6bedSChris Mason } 6575f39d397SChris Mason #if 0 6585f39d397SChris Mason for (i = 0; nritems > 1 && i < nritems - 2; i++) { 6595f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, i + 1); 6605f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, i); 6615f39d397SChris Mason if (comp_keys(&leaf_key, &cpukey) >= 0) { 6625f39d397SChris Mason btrfs_print_leaf(root, leaf); 6635f39d397SChris Mason printk("slot %d offset bad key\n", i); 6645f39d397SChris Mason BUG_ON(1); 6655f39d397SChris Mason } 6665f39d397SChris Mason if (btrfs_item_offset_nr(leaf, i) != 6675f39d397SChris Mason btrfs_item_end_nr(leaf, i + 1)) { 6685f39d397SChris Mason btrfs_print_leaf(root, leaf); 6695f39d397SChris Mason printk("slot %d offset bad\n", i); 6705f39d397SChris Mason BUG_ON(1); 6715f39d397SChris Mason } 6725f39d397SChris Mason if (i == 0) { 6735f39d397SChris Mason if (btrfs_item_offset_nr(leaf, i) + 6745f39d397SChris Mason btrfs_item_size_nr(leaf, i) != 6755f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 6765f39d397SChris Mason btrfs_print_leaf(root, leaf); 6775f39d397SChris Mason printk("slot %d first offset bad\n", i); 6785f39d397SChris Mason BUG_ON(1); 6795f39d397SChris Mason } 6805f39d397SChris Mason } 6815f39d397SChris Mason } 6825f39d397SChris Mason if (nritems > 0) { 6835f39d397SChris Mason if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) { 6845f39d397SChris Mason btrfs_print_leaf(root, leaf); 6855f39d397SChris Mason printk("slot %d bad size \n", nritems - 1); 6865f39d397SChris Mason BUG_ON(1); 6875f39d397SChris Mason } 6885f39d397SChris Mason } 6895f39d397SChris Mason #endif 6905f39d397SChris Mason if (slot != 0 && slot < nritems - 1) { 6915f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, slot); 6925f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1); 6935f39d397SChris Mason if (comp_keys(&leaf_key, &cpukey) <= 0) { 6945f39d397SChris Mason btrfs_print_leaf(root, leaf); 6955f39d397SChris Mason printk("slot %d offset bad key\n", slot); 6965f39d397SChris Mason BUG_ON(1); 6975f39d397SChris Mason } 6985f39d397SChris Mason if (btrfs_item_offset_nr(leaf, slot - 1) != 6995f39d397SChris Mason btrfs_item_end_nr(leaf, slot)) { 7005f39d397SChris Mason btrfs_print_leaf(root, leaf); 7015f39d397SChris Mason printk("slot %d offset bad\n", slot); 7025f39d397SChris Mason BUG_ON(1); 7035f39d397SChris Mason } 704aa5d6bedSChris Mason } 7058d7be552SChris Mason if (slot < nritems - 1) { 7065f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, slot); 7075f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1); 7085f39d397SChris Mason BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0); 7095f39d397SChris Mason if (btrfs_item_offset_nr(leaf, slot) != 7105f39d397SChris Mason btrfs_item_end_nr(leaf, slot + 1)) { 7115f39d397SChris Mason btrfs_print_leaf(root, leaf); 7125f39d397SChris Mason printk("slot %d offset bad\n", slot); 7135f39d397SChris Mason BUG_ON(1); 714aa5d6bedSChris Mason } 7155f39d397SChris Mason } 7165f39d397SChris Mason BUG_ON(btrfs_item_offset_nr(leaf, 0) + 7175f39d397SChris Mason btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root)); 718aa5d6bedSChris Mason return 0; 719aa5d6bedSChris Mason } 720aa5d6bedSChris Mason 72198ed5174SChris Mason static int noinline check_block(struct btrfs_root *root, 72298ed5174SChris Mason struct btrfs_path *path, int level) 723aa5d6bedSChris Mason { 724f188591eSChris Mason u64 found_start; 72585d824c4SChris Mason return 0; 726f188591eSChris Mason if (btrfs_header_level(path->nodes[level]) != level) 727f188591eSChris Mason printk("warning: bad level %Lu wanted %d found %d\n", 728f188591eSChris Mason path->nodes[level]->start, level, 729f188591eSChris Mason btrfs_header_level(path->nodes[level])); 730f188591eSChris Mason found_start = btrfs_header_bytenr(path->nodes[level]); 731f188591eSChris Mason if (found_start != path->nodes[level]->start) { 732f188591eSChris Mason printk("warning: bad bytentr %Lu found %Lu\n", 733f188591eSChris Mason path->nodes[level]->start, found_start); 734f188591eSChris Mason } 735db94535dSChris Mason #if 0 7365f39d397SChris Mason struct extent_buffer *buf = path->nodes[level]; 7375f39d397SChris Mason 738479965d6SChris Mason if (memcmp_extent_buffer(buf, root->fs_info->fsid, 739479965d6SChris Mason (unsigned long)btrfs_header_fsid(buf), 740479965d6SChris Mason BTRFS_FSID_SIZE)) { 7415f39d397SChris Mason printk("warning bad block %Lu\n", buf->start); 742db94535dSChris Mason return 1; 7435f39d397SChris Mason } 744db94535dSChris Mason #endif 745aa5d6bedSChris Mason if (level == 0) 746123abc88SChris Mason return check_leaf(root, path, level); 747123abc88SChris Mason return check_node(root, path, level); 748aa5d6bedSChris Mason } 749aa5d6bedSChris Mason 75074123bd7SChris Mason /* 7515f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 7525f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 7535f39d397SChris Mason * 75474123bd7SChris Mason * the slot in the array is returned via slot, and it points to 75574123bd7SChris Mason * the place where you would insert key if it is not found in 75674123bd7SChris Mason * the array. 75774123bd7SChris Mason * 75874123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 75974123bd7SChris Mason */ 760e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb, 761e02119d5SChris Mason unsigned long p, 7625f39d397SChris Mason int item_size, struct btrfs_key *key, 763be0e5c09SChris Mason int max, int *slot) 764be0e5c09SChris Mason { 765be0e5c09SChris Mason int low = 0; 766be0e5c09SChris Mason int high = max; 767be0e5c09SChris Mason int mid; 768be0e5c09SChris Mason int ret; 769479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 7705f39d397SChris Mason struct btrfs_disk_key unaligned; 7715f39d397SChris Mason unsigned long offset; 7725f39d397SChris Mason char *map_token = NULL; 7735f39d397SChris Mason char *kaddr = NULL; 7745f39d397SChris Mason unsigned long map_start = 0; 7755f39d397SChris Mason unsigned long map_len = 0; 776479965d6SChris Mason int err; 777be0e5c09SChris Mason 778be0e5c09SChris Mason while(low < high) { 779be0e5c09SChris Mason mid = (low + high) / 2; 7805f39d397SChris Mason offset = p + mid * item_size; 7815f39d397SChris Mason 7825f39d397SChris Mason if (!map_token || offset < map_start || 7835f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 7845f39d397SChris Mason map_start + map_len) { 785479965d6SChris Mason if (map_token) { 7865f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 787479965d6SChris Mason map_token = NULL; 788479965d6SChris Mason } 789479965d6SChris Mason err = map_extent_buffer(eb, offset, 790479965d6SChris Mason sizeof(struct btrfs_disk_key), 791479965d6SChris Mason &map_token, &kaddr, 7925f39d397SChris Mason &map_start, &map_len, KM_USER0); 7935f39d397SChris Mason 794479965d6SChris Mason if (!err) { 795479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 796479965d6SChris Mason map_start); 797479965d6SChris Mason } else { 7985f39d397SChris Mason read_extent_buffer(eb, &unaligned, 7995f39d397SChris Mason offset, sizeof(unaligned)); 8005f39d397SChris Mason tmp = &unaligned; 801479965d6SChris Mason } 802479965d6SChris Mason 8035f39d397SChris Mason } else { 8045f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 8055f39d397SChris Mason map_start); 8065f39d397SChris Mason } 807be0e5c09SChris Mason ret = comp_keys(tmp, key); 808be0e5c09SChris Mason 809be0e5c09SChris Mason if (ret < 0) 810be0e5c09SChris Mason low = mid + 1; 811be0e5c09SChris Mason else if (ret > 0) 812be0e5c09SChris Mason high = mid; 813be0e5c09SChris Mason else { 814be0e5c09SChris Mason *slot = mid; 815479965d6SChris Mason if (map_token) 8165f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 817be0e5c09SChris Mason return 0; 818be0e5c09SChris Mason } 819be0e5c09SChris Mason } 820be0e5c09SChris Mason *slot = low; 8215f39d397SChris Mason if (map_token) 8225f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 823be0e5c09SChris Mason return 1; 824be0e5c09SChris Mason } 825be0e5c09SChris Mason 82697571fd0SChris Mason /* 82797571fd0SChris Mason * simple bin_search frontend that does the right thing for 82897571fd0SChris Mason * leaves vs nodes 82997571fd0SChris Mason */ 8305f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, 8315f39d397SChris Mason int level, int *slot) 832be0e5c09SChris Mason { 8335f39d397SChris Mason if (level == 0) { 8345f39d397SChris Mason return generic_bin_search(eb, 8355f39d397SChris Mason offsetof(struct btrfs_leaf, items), 8360783fcfcSChris Mason sizeof(struct btrfs_item), 8375f39d397SChris Mason key, btrfs_header_nritems(eb), 8387518a238SChris Mason slot); 839be0e5c09SChris Mason } else { 8405f39d397SChris Mason return generic_bin_search(eb, 8415f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 842123abc88SChris Mason sizeof(struct btrfs_key_ptr), 8435f39d397SChris Mason key, btrfs_header_nritems(eb), 8447518a238SChris Mason slot); 845be0e5c09SChris Mason } 846be0e5c09SChris Mason return -1; 847be0e5c09SChris Mason } 848be0e5c09SChris Mason 849d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 850d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 851d352ac68SChris Mason * NULL is returned on error. 852d352ac68SChris Mason */ 853e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, 8545f39d397SChris Mason struct extent_buffer *parent, int slot) 855bb803951SChris Mason { 856ca7a79adSChris Mason int level = btrfs_header_level(parent); 857bb803951SChris Mason if (slot < 0) 858bb803951SChris Mason return NULL; 8595f39d397SChris Mason if (slot >= btrfs_header_nritems(parent)) 860bb803951SChris Mason return NULL; 861ca7a79adSChris Mason 862ca7a79adSChris Mason BUG_ON(level == 0); 863ca7a79adSChris Mason 864db94535dSChris Mason return read_tree_block(root, btrfs_node_blockptr(parent, slot), 865ca7a79adSChris Mason btrfs_level_size(root, level - 1), 866ca7a79adSChris Mason btrfs_node_ptr_generation(parent, slot)); 867bb803951SChris Mason } 868bb803951SChris Mason 869d352ac68SChris Mason /* 870d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 871d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 872d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 873d352ac68SChris Mason */ 874e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 87598ed5174SChris Mason struct btrfs_root *root, 87698ed5174SChris Mason struct btrfs_path *path, int level) 877bb803951SChris Mason { 8785f39d397SChris Mason struct extent_buffer *right = NULL; 8795f39d397SChris Mason struct extent_buffer *mid; 8805f39d397SChris Mason struct extent_buffer *left = NULL; 8815f39d397SChris Mason struct extent_buffer *parent = NULL; 882bb803951SChris Mason int ret = 0; 883bb803951SChris Mason int wret; 884bb803951SChris Mason int pslot; 885bb803951SChris Mason int orig_slot = path->slots[level]; 88654aa1f4dSChris Mason int err_on_enospc = 0; 88779f95c82SChris Mason u64 orig_ptr; 888bb803951SChris Mason 889bb803951SChris Mason if (level == 0) 890bb803951SChris Mason return 0; 891bb803951SChris Mason 8925f39d397SChris Mason mid = path->nodes[level]; 893925baeddSChris Mason WARN_ON(!path->locks[level]); 8947bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 8957bb86316SChris Mason 8961d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 89779f95c82SChris Mason 898234b63a0SChris Mason if (level < BTRFS_MAX_LEVEL - 1) 8995f39d397SChris Mason parent = path->nodes[level + 1]; 900bb803951SChris Mason pslot = path->slots[level + 1]; 901bb803951SChris Mason 90240689478SChris Mason /* 90340689478SChris Mason * deal with the case where there is only one pointer in the root 90440689478SChris Mason * by promoting the node below to a root 90540689478SChris Mason */ 9065f39d397SChris Mason if (!parent) { 9075f39d397SChris Mason struct extent_buffer *child; 908bb803951SChris Mason 9095f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 910bb803951SChris Mason return 0; 911bb803951SChris Mason 912bb803951SChris Mason /* promote the child to a root */ 9135f39d397SChris Mason child = read_node_slot(root, mid, 0); 914925baeddSChris Mason btrfs_tree_lock(child); 915bb803951SChris Mason BUG_ON(!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); 931925baeddSChris Mason path->locks[level] = 0; 932bb803951SChris Mason path->nodes[level] = NULL; 9335f39d397SChris Mason clean_tree_block(trans, root, mid); 934925baeddSChris Mason btrfs_tree_unlock(mid); 935bb803951SChris Mason /* once for the path */ 9365f39d397SChris Mason free_extent_buffer(mid); 9377bb86316SChris Mason ret = btrfs_free_extent(trans, root, mid->start, mid->len, 93831840ae1SZheng Yan mid->start, root->root_key.objectid, 9393bb1a1bcSYan Zheng btrfs_header_generation(mid), 9403bb1a1bcSYan Zheng level, 1); 941bb803951SChris Mason /* once for the root ptr */ 9425f39d397SChris Mason free_extent_buffer(mid); 943db94535dSChris Mason return ret; 944bb803951SChris Mason } 9455f39d397SChris Mason if (btrfs_header_nritems(mid) > 946123abc88SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 4) 947bb803951SChris Mason return 0; 948bb803951SChris Mason 9495f39d397SChris Mason if (btrfs_header_nritems(mid) < 2) 95054aa1f4dSChris Mason err_on_enospc = 1; 95154aa1f4dSChris Mason 9525f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 9535f39d397SChris Mason if (left) { 954925baeddSChris Mason btrfs_tree_lock(left); 9555f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 95665b51a00SChris Mason parent, pslot - 1, &left, 0); 95754aa1f4dSChris Mason if (wret) { 95854aa1f4dSChris Mason ret = wret; 95954aa1f4dSChris Mason goto enospc; 96054aa1f4dSChris Mason } 9612cc58cf2SChris Mason } 9625f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 9635f39d397SChris Mason if (right) { 964925baeddSChris Mason btrfs_tree_lock(right); 9655f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 96665b51a00SChris Mason parent, pslot + 1, &right, 0); 9672cc58cf2SChris Mason if (wret) { 9682cc58cf2SChris Mason ret = wret; 9692cc58cf2SChris Mason goto enospc; 9702cc58cf2SChris Mason } 9712cc58cf2SChris Mason } 9722cc58cf2SChris Mason 9732cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 9745f39d397SChris Mason if (left) { 9755f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 976bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 97779f95c82SChris Mason if (wret < 0) 97879f95c82SChris Mason ret = wret; 9795f39d397SChris Mason if (btrfs_header_nritems(mid) < 2) 98054aa1f4dSChris Mason err_on_enospc = 1; 981bb803951SChris Mason } 98279f95c82SChris Mason 98379f95c82SChris Mason /* 98479f95c82SChris Mason * then try to empty the right most buffer into the middle 98579f95c82SChris Mason */ 9865f39d397SChris Mason if (right) { 987971a1f66SChris Mason wret = push_node_left(trans, root, mid, right, 1); 98854aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 98979f95c82SChris Mason ret = wret; 9905f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 991db94535dSChris Mason u64 bytenr = right->start; 9927bb86316SChris Mason u64 generation = btrfs_header_generation(parent); 993db94535dSChris Mason u32 blocksize = right->len; 994db94535dSChris Mason 9955f39d397SChris Mason clean_tree_block(trans, root, right); 996925baeddSChris Mason btrfs_tree_unlock(right); 9975f39d397SChris Mason free_extent_buffer(right); 998bb803951SChris Mason right = NULL; 999e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot + 1000e089f05cSChris Mason 1); 1001bb803951SChris Mason if (wret) 1002bb803951SChris Mason ret = wret; 1003db94535dSChris Mason wret = btrfs_free_extent(trans, root, bytenr, 100431840ae1SZheng Yan blocksize, parent->start, 10057bb86316SChris Mason btrfs_header_owner(parent), 10063bb1a1bcSYan Zheng generation, level, 1); 1007bb803951SChris Mason if (wret) 1008bb803951SChris Mason ret = wret; 1009bb803951SChris Mason } else { 10105f39d397SChris Mason struct btrfs_disk_key right_key; 10115f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 10125f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 10135f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1014bb803951SChris Mason } 1015bb803951SChris Mason } 10165f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 101779f95c82SChris Mason /* 101879f95c82SChris Mason * we're not allowed to leave a node with one item in the 101979f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 102079f95c82SChris Mason * could try to delete the only pointer in this node. 102179f95c82SChris Mason * So, pull some keys from the left. 102279f95c82SChris Mason * There has to be a left pointer at this point because 102379f95c82SChris Mason * otherwise we would have pulled some pointers from the 102479f95c82SChris Mason * right 102579f95c82SChris Mason */ 10265f39d397SChris Mason BUG_ON(!left); 10275f39d397SChris Mason wret = balance_node_right(trans, root, mid, left); 102854aa1f4dSChris Mason if (wret < 0) { 102979f95c82SChris Mason ret = wret; 103054aa1f4dSChris Mason goto enospc; 103154aa1f4dSChris Mason } 1032bce4eae9SChris Mason if (wret == 1) { 1033bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 1034bce4eae9SChris Mason if (wret < 0) 1035bce4eae9SChris Mason ret = wret; 1036bce4eae9SChris Mason } 103779f95c82SChris Mason BUG_ON(wret == 1); 103879f95c82SChris Mason } 10395f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 104079f95c82SChris Mason /* we've managed to empty the middle node, drop it */ 10417bb86316SChris Mason u64 root_gen = btrfs_header_generation(parent); 1042db94535dSChris Mason u64 bytenr = mid->start; 1043db94535dSChris Mason u32 blocksize = mid->len; 1044925baeddSChris Mason 10455f39d397SChris Mason clean_tree_block(trans, root, mid); 1046925baeddSChris Mason btrfs_tree_unlock(mid); 10475f39d397SChris Mason free_extent_buffer(mid); 1048bb803951SChris Mason mid = NULL; 1049e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot); 1050bb803951SChris Mason if (wret) 1051bb803951SChris Mason ret = wret; 10527bb86316SChris Mason wret = btrfs_free_extent(trans, root, bytenr, blocksize, 105331840ae1SZheng Yan parent->start, 10547bb86316SChris Mason btrfs_header_owner(parent), 10553bb1a1bcSYan Zheng root_gen, level, 1); 1056bb803951SChris Mason if (wret) 1057bb803951SChris Mason ret = wret; 105879f95c82SChris Mason } else { 105979f95c82SChris Mason /* update the parent key to reflect our changes */ 10605f39d397SChris Mason struct btrfs_disk_key mid_key; 10615f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 10625f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 10635f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 106479f95c82SChris Mason } 1065bb803951SChris Mason 106679f95c82SChris Mason /* update the path */ 10675f39d397SChris Mason if (left) { 10685f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 10695f39d397SChris Mason extent_buffer_get(left); 1070925baeddSChris Mason /* left was locked after cow */ 10715f39d397SChris Mason path->nodes[level] = left; 1072bb803951SChris Mason path->slots[level + 1] -= 1; 1073bb803951SChris Mason path->slots[level] = orig_slot; 1074925baeddSChris Mason if (mid) { 1075925baeddSChris Mason btrfs_tree_unlock(mid); 10765f39d397SChris Mason free_extent_buffer(mid); 1077925baeddSChris Mason } 1078bb803951SChris Mason } else { 10795f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 1080bb803951SChris Mason path->slots[level] = orig_slot; 1081bb803951SChris Mason } 1082bb803951SChris Mason } 108379f95c82SChris Mason /* double check we haven't messed things up */ 1084123abc88SChris Mason check_block(root, path, level); 1085e20d96d6SChris Mason if (orig_ptr != 10865f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 108779f95c82SChris Mason BUG(); 108854aa1f4dSChris Mason enospc: 1089925baeddSChris Mason if (right) { 1090925baeddSChris Mason btrfs_tree_unlock(right); 10915f39d397SChris Mason free_extent_buffer(right); 1092925baeddSChris Mason } 1093925baeddSChris Mason if (left) { 1094925baeddSChris Mason if (path->nodes[level] != left) 1095925baeddSChris Mason btrfs_tree_unlock(left); 10965f39d397SChris Mason free_extent_buffer(left); 1097925baeddSChris Mason } 1098bb803951SChris Mason return ret; 1099bb803951SChris Mason } 1100bb803951SChris Mason 1101d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 1102d352ac68SChris Mason * when they are completely full. This is also done top down, so we 1103d352ac68SChris Mason * have to be pessimistic. 1104d352ac68SChris Mason */ 110598ed5174SChris Mason static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans, 1106e66f709bSChris Mason struct btrfs_root *root, 1107e66f709bSChris Mason struct btrfs_path *path, int level) 1108e66f709bSChris Mason { 11095f39d397SChris Mason struct extent_buffer *right = NULL; 11105f39d397SChris Mason struct extent_buffer *mid; 11115f39d397SChris Mason struct extent_buffer *left = NULL; 11125f39d397SChris Mason struct extent_buffer *parent = NULL; 1113e66f709bSChris Mason int ret = 0; 1114e66f709bSChris Mason int wret; 1115e66f709bSChris Mason int pslot; 1116e66f709bSChris Mason int orig_slot = path->slots[level]; 1117e66f709bSChris Mason u64 orig_ptr; 1118e66f709bSChris Mason 1119e66f709bSChris Mason if (level == 0) 1120e66f709bSChris Mason return 1; 1121e66f709bSChris Mason 11225f39d397SChris Mason mid = path->nodes[level]; 11237bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 1124e66f709bSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 1125e66f709bSChris Mason 1126e66f709bSChris Mason if (level < BTRFS_MAX_LEVEL - 1) 11275f39d397SChris Mason parent = path->nodes[level + 1]; 1128e66f709bSChris Mason pslot = path->slots[level + 1]; 1129e66f709bSChris Mason 11305f39d397SChris Mason if (!parent) 1131e66f709bSChris Mason return 1; 1132e66f709bSChris Mason 11335f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 1134e66f709bSChris Mason 1135e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 11365f39d397SChris Mason if (left) { 1137e66f709bSChris Mason u32 left_nr; 1138925baeddSChris Mason 1139925baeddSChris Mason btrfs_tree_lock(left); 11405f39d397SChris Mason left_nr = btrfs_header_nritems(left); 114133ade1f8SChris Mason if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 114233ade1f8SChris Mason wret = 1; 114333ade1f8SChris Mason } else { 11445f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 114565b51a00SChris Mason pslot - 1, &left, 0); 114654aa1f4dSChris Mason if (ret) 114754aa1f4dSChris Mason wret = 1; 114854aa1f4dSChris Mason else { 114954aa1f4dSChris Mason wret = push_node_left(trans, root, 1150971a1f66SChris Mason left, mid, 0); 115154aa1f4dSChris Mason } 115233ade1f8SChris Mason } 1153e66f709bSChris Mason if (wret < 0) 1154e66f709bSChris Mason ret = wret; 1155e66f709bSChris Mason if (wret == 0) { 11565f39d397SChris Mason struct btrfs_disk_key disk_key; 1157e66f709bSChris Mason orig_slot += left_nr; 11585f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 11595f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 11605f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 11615f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 11625f39d397SChris Mason path->nodes[level] = left; 1163e66f709bSChris Mason path->slots[level + 1] -= 1; 1164e66f709bSChris Mason path->slots[level] = orig_slot; 1165925baeddSChris Mason btrfs_tree_unlock(mid); 11665f39d397SChris Mason free_extent_buffer(mid); 1167e66f709bSChris Mason } else { 1168e66f709bSChris Mason orig_slot -= 11695f39d397SChris Mason btrfs_header_nritems(left); 1170e66f709bSChris Mason path->slots[level] = orig_slot; 1171925baeddSChris Mason btrfs_tree_unlock(left); 11725f39d397SChris Mason free_extent_buffer(left); 1173e66f709bSChris Mason } 1174e66f709bSChris Mason return 0; 1175e66f709bSChris Mason } 1176925baeddSChris Mason btrfs_tree_unlock(left); 11775f39d397SChris Mason free_extent_buffer(left); 1178e66f709bSChris Mason } 11795f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 1180e66f709bSChris Mason 1181e66f709bSChris Mason /* 1182e66f709bSChris Mason * then try to empty the right most buffer into the middle 1183e66f709bSChris Mason */ 11845f39d397SChris Mason if (right) { 118533ade1f8SChris Mason u32 right_nr; 1186925baeddSChris Mason btrfs_tree_lock(right); 11875f39d397SChris Mason right_nr = btrfs_header_nritems(right); 118833ade1f8SChris Mason if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 118933ade1f8SChris Mason wret = 1; 119033ade1f8SChris Mason } else { 11915f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 11925f39d397SChris Mason parent, pslot + 1, 119365b51a00SChris Mason &right, 0); 119454aa1f4dSChris Mason if (ret) 119554aa1f4dSChris Mason wret = 1; 119654aa1f4dSChris Mason else { 119733ade1f8SChris Mason wret = balance_node_right(trans, root, 11985f39d397SChris Mason right, mid); 119933ade1f8SChris Mason } 120054aa1f4dSChris Mason } 1201e66f709bSChris Mason if (wret < 0) 1202e66f709bSChris Mason ret = wret; 1203e66f709bSChris Mason if (wret == 0) { 12045f39d397SChris Mason struct btrfs_disk_key disk_key; 12055f39d397SChris Mason 12065f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 12075f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 12085f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 12095f39d397SChris Mason 12105f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 12115f39d397SChris Mason path->nodes[level] = right; 1212e66f709bSChris Mason path->slots[level + 1] += 1; 1213e66f709bSChris Mason path->slots[level] = orig_slot - 12145f39d397SChris Mason btrfs_header_nritems(mid); 1215925baeddSChris Mason btrfs_tree_unlock(mid); 12165f39d397SChris Mason free_extent_buffer(mid); 1217e66f709bSChris Mason } else { 1218925baeddSChris Mason btrfs_tree_unlock(right); 12195f39d397SChris Mason free_extent_buffer(right); 1220e66f709bSChris Mason } 1221e66f709bSChris Mason return 0; 1222e66f709bSChris Mason } 1223925baeddSChris Mason btrfs_tree_unlock(right); 12245f39d397SChris Mason free_extent_buffer(right); 1225e66f709bSChris Mason } 1226e66f709bSChris Mason return 1; 1227e66f709bSChris Mason } 1228e66f709bSChris Mason 122974123bd7SChris Mason /* 1230d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 1231d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 12323c69faecSChris Mason */ 1233e02119d5SChris Mason static noinline void reada_for_search(struct btrfs_root *root, 1234e02119d5SChris Mason struct btrfs_path *path, 123501f46658SChris Mason int level, int slot, u64 objectid) 12363c69faecSChris Mason { 12375f39d397SChris Mason struct extent_buffer *node; 123801f46658SChris Mason struct btrfs_disk_key disk_key; 12393c69faecSChris Mason u32 nritems; 12403c69faecSChris Mason u64 search; 12416b80053dSChris Mason u64 lowest_read; 12426b80053dSChris Mason u64 highest_read; 12436b80053dSChris Mason u64 nread = 0; 12443c69faecSChris Mason int direction = path->reada; 12455f39d397SChris Mason struct extent_buffer *eb; 12466b80053dSChris Mason u32 nr; 12476b80053dSChris Mason u32 blocksize; 12486b80053dSChris Mason u32 nscan = 0; 1249db94535dSChris Mason 1250a6b6e75eSChris Mason if (level != 1) 12513c69faecSChris Mason return; 12523c69faecSChris Mason 12536702ed49SChris Mason if (!path->nodes[level]) 12546702ed49SChris Mason return; 12556702ed49SChris Mason 12565f39d397SChris Mason node = path->nodes[level]; 1257925baeddSChris Mason 12583c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 12596b80053dSChris Mason blocksize = btrfs_level_size(root, level - 1); 12606b80053dSChris Mason eb = btrfs_find_tree_block(root, search, blocksize); 12615f39d397SChris Mason if (eb) { 12625f39d397SChris Mason free_extent_buffer(eb); 12633c69faecSChris Mason return; 12643c69faecSChris Mason } 12653c69faecSChris Mason 12666b80053dSChris Mason highest_read = search; 12676b80053dSChris Mason lowest_read = search; 12686b80053dSChris Mason 12695f39d397SChris Mason nritems = btrfs_header_nritems(node); 12706b80053dSChris Mason nr = slot; 12713c69faecSChris Mason while(1) { 12726b80053dSChris Mason if (direction < 0) { 12736b80053dSChris Mason if (nr == 0) 12743c69faecSChris Mason break; 12756b80053dSChris Mason nr--; 12766b80053dSChris Mason } else if (direction > 0) { 12776b80053dSChris Mason nr++; 12786b80053dSChris Mason if (nr >= nritems) 12796b80053dSChris Mason break; 12803c69faecSChris Mason } 128101f46658SChris Mason if (path->reada < 0 && objectid) { 128201f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 128301f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 128401f46658SChris Mason break; 128501f46658SChris Mason } 12866b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 12876b80053dSChris Mason if ((search >= lowest_read && search <= highest_read) || 12886b80053dSChris Mason (search < lowest_read && lowest_read - search <= 32768) || 12896b80053dSChris Mason (search > highest_read && search - highest_read <= 32768)) { 1290ca7a79adSChris Mason readahead_tree_block(root, search, blocksize, 1291ca7a79adSChris Mason btrfs_node_ptr_generation(node, nr)); 12926b80053dSChris Mason nread += blocksize; 12933c69faecSChris Mason } 12946b80053dSChris Mason nscan++; 12956b80053dSChris Mason if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32)) 12966b80053dSChris Mason break; 12976b80053dSChris Mason if(nread > (1024 * 1024) || nscan > 128) 12986b80053dSChris Mason break; 12996b80053dSChris Mason 13006b80053dSChris Mason if (search < lowest_read) 13016b80053dSChris Mason lowest_read = search; 13026b80053dSChris Mason if (search > highest_read) 13036b80053dSChris Mason highest_read = search; 13043c69faecSChris Mason } 13053c69faecSChris Mason } 1306925baeddSChris Mason 1307d352ac68SChris Mason /* 1308d352ac68SChris Mason * when we walk down the tree, it is usually safe to unlock the higher layers in 1309d352ac68SChris Mason * the tree. The exceptions are when our path goes through slot 0, because operations 1310d352ac68SChris Mason * on the tree might require changing key pointers higher up in the tree. 1311d352ac68SChris Mason * 1312d352ac68SChris Mason * callers might also have set path->keep_locks, which tells this code to 1313d352ac68SChris Mason * keep the lock if the path points to the last slot in the block. This is 1314d352ac68SChris Mason * part of walking through the tree, and selecting the next slot in the higher 1315d352ac68SChris Mason * block. 1316d352ac68SChris Mason * 1317d352ac68SChris Mason * lowest_unlock sets the lowest level in the tree we're allowed to unlock. 1318d352ac68SChris Mason * so if lowest_unlock is 1, level 0 won't be unlocked 1319d352ac68SChris Mason */ 1320e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level, 1321e02119d5SChris Mason int lowest_unlock) 1322925baeddSChris Mason { 1323925baeddSChris Mason int i; 1324925baeddSChris Mason int skip_level = level; 1325051e1b9fSChris Mason int no_skips = 0; 1326925baeddSChris Mason struct extent_buffer *t; 1327925baeddSChris Mason 1328925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1329925baeddSChris Mason if (!path->nodes[i]) 1330925baeddSChris Mason break; 1331925baeddSChris Mason if (!path->locks[i]) 1332925baeddSChris Mason break; 1333051e1b9fSChris Mason if (!no_skips && path->slots[i] == 0) { 1334925baeddSChris Mason skip_level = i + 1; 1335925baeddSChris Mason continue; 1336925baeddSChris Mason } 1337051e1b9fSChris Mason if (!no_skips && path->keep_locks) { 1338925baeddSChris Mason u32 nritems; 1339925baeddSChris Mason t = path->nodes[i]; 1340925baeddSChris Mason nritems = btrfs_header_nritems(t); 1341051e1b9fSChris Mason if (nritems < 1 || path->slots[i] >= nritems - 1) { 1342925baeddSChris Mason skip_level = i + 1; 1343925baeddSChris Mason continue; 1344925baeddSChris Mason } 1345925baeddSChris Mason } 1346051e1b9fSChris Mason if (skip_level < i && i >= lowest_unlock) 1347051e1b9fSChris Mason no_skips = 1; 1348051e1b9fSChris Mason 1349925baeddSChris Mason t = path->nodes[i]; 1350925baeddSChris Mason if (i >= lowest_unlock && i > skip_level && path->locks[i]) { 1351925baeddSChris Mason btrfs_tree_unlock(t); 1352925baeddSChris Mason path->locks[i] = 0; 1353925baeddSChris Mason } 1354925baeddSChris Mason } 1355925baeddSChris Mason } 1356925baeddSChris Mason 13573c69faecSChris Mason /* 135874123bd7SChris Mason * look for key in the tree. path is filled in with nodes along the way 135974123bd7SChris Mason * if key is found, we return zero and you can find the item in the leaf 136074123bd7SChris Mason * level of the path (level 0) 136174123bd7SChris Mason * 136274123bd7SChris Mason * If the key isn't found, the path points to the slot where it should 1363aa5d6bedSChris Mason * be inserted, and 1 is returned. If there are other errors during the 1364aa5d6bedSChris Mason * search a negative error number is returned. 136597571fd0SChris Mason * 136697571fd0SChris Mason * if ins_len > 0, nodes and leaves will be split as we walk down the 136797571fd0SChris Mason * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if 136897571fd0SChris Mason * possible) 136974123bd7SChris Mason */ 1370e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root 1371e089f05cSChris Mason *root, struct btrfs_key *key, struct btrfs_path *p, int 1372e089f05cSChris Mason ins_len, int cow) 1373be0e5c09SChris Mason { 13745f39d397SChris Mason struct extent_buffer *b; 1375051e1b9fSChris Mason struct extent_buffer *tmp; 1376be0e5c09SChris Mason int slot; 1377be0e5c09SChris Mason int ret; 1378be0e5c09SChris Mason int level; 13793c69faecSChris Mason int should_reada = p->reada; 1380925baeddSChris Mason int lowest_unlock = 1; 1381594a24ebSChris Mason int blocksize; 13829f3a7427SChris Mason u8 lowest_level = 0; 1383594a24ebSChris Mason u64 blocknr; 1384594a24ebSChris Mason u64 gen; 138565b51a00SChris Mason struct btrfs_key prealloc_block; 13869f3a7427SChris Mason 13876702ed49SChris Mason lowest_level = p->lowest_level; 1388323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 138922b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 1390333db94cSChris Mason WARN_ON(cow && root == root->fs_info->extent_root && 1391925baeddSChris Mason !mutex_is_locked(&root->fs_info->alloc_mutex)); 1392925baeddSChris Mason if (ins_len < 0) 1393925baeddSChris Mason lowest_unlock = 2; 139465b51a00SChris Mason 139565b51a00SChris Mason prealloc_block.objectid = 0; 139665b51a00SChris Mason 1397bb803951SChris Mason again: 13985cd57b2cSChris Mason if (p->skip_locking) 13995cd57b2cSChris Mason b = btrfs_root_node(root); 14005cd57b2cSChris Mason else 1401925baeddSChris Mason b = btrfs_lock_root_node(root); 1402925baeddSChris Mason 1403eb60ceacSChris Mason while (b) { 14045f39d397SChris Mason level = btrfs_header_level(b); 140565b51a00SChris Mason 140665b51a00SChris Mason /* 140765b51a00SChris Mason * setup the path here so we can release it under lock 140865b51a00SChris Mason * contention with the cow code 140965b51a00SChris Mason */ 141065b51a00SChris Mason p->nodes[level] = b; 141165b51a00SChris Mason if (!p->skip_locking) 141265b51a00SChris Mason p->locks[level] = 1; 141365b51a00SChris Mason 141402217ed2SChris Mason if (cow) { 141502217ed2SChris Mason int wret; 141665b51a00SChris Mason 141765b51a00SChris Mason /* is a cow on this block not required */ 141865b51a00SChris Mason spin_lock(&root->fs_info->hash_lock); 141965b51a00SChris Mason if (btrfs_header_generation(b) == trans->transid && 14205b21f2edSZheng Yan btrfs_header_owner(b) == root->root_key.objectid && 142165b51a00SChris Mason !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) { 142265b51a00SChris Mason spin_unlock(&root->fs_info->hash_lock); 142365b51a00SChris Mason goto cow_done; 142465b51a00SChris Mason } 142565b51a00SChris Mason spin_unlock(&root->fs_info->hash_lock); 142665b51a00SChris Mason 142765b51a00SChris Mason /* ok, we have to cow, is our old prealloc the right 142865b51a00SChris Mason * size? 142965b51a00SChris Mason */ 143065b51a00SChris Mason if (prealloc_block.objectid && 143165b51a00SChris Mason prealloc_block.offset != b->len) { 143265b51a00SChris Mason btrfs_free_reserved_extent(root, 143365b51a00SChris Mason prealloc_block.objectid, 143465b51a00SChris Mason prealloc_block.offset); 143565b51a00SChris Mason prealloc_block.objectid = 0; 143665b51a00SChris Mason } 143765b51a00SChris Mason 143865b51a00SChris Mason /* 143965b51a00SChris Mason * for higher level blocks, try not to allocate blocks 144065b51a00SChris Mason * with the block and the parent locks held. 144165b51a00SChris Mason */ 144265b51a00SChris Mason if (level > 1 && !prealloc_block.objectid && 144365b51a00SChris Mason btrfs_path_lock_waiting(p, level)) { 144465b51a00SChris Mason u32 size = b->len; 144565b51a00SChris Mason u64 hint = b->start; 144665b51a00SChris Mason 144765b51a00SChris Mason btrfs_release_path(root, p); 144865b51a00SChris Mason ret = btrfs_reserve_extent(trans, root, 144965b51a00SChris Mason size, size, 0, 145065b51a00SChris Mason hint, (u64)-1, 145165b51a00SChris Mason &prealloc_block, 0); 145265b51a00SChris Mason BUG_ON(ret); 145365b51a00SChris Mason goto again; 145465b51a00SChris Mason } 145565b51a00SChris Mason 1456e20d96d6SChris Mason wret = btrfs_cow_block(trans, root, b, 1457e20d96d6SChris Mason p->nodes[level + 1], 1458e20d96d6SChris Mason p->slots[level + 1], 145965b51a00SChris Mason &b, prealloc_block.objectid); 146065b51a00SChris Mason prealloc_block.objectid = 0; 146154aa1f4dSChris Mason if (wret) { 14625f39d397SChris Mason free_extent_buffer(b); 146365b51a00SChris Mason ret = wret; 146465b51a00SChris Mason goto done; 146554aa1f4dSChris Mason } 146602217ed2SChris Mason } 146765b51a00SChris Mason cow_done: 146802217ed2SChris Mason BUG_ON(!cow && ins_len); 14695f39d397SChris Mason if (level != btrfs_header_level(b)) 14702c90e5d6SChris Mason WARN_ON(1); 14715f39d397SChris Mason level = btrfs_header_level(b); 147265b51a00SChris Mason 1473eb60ceacSChris Mason p->nodes[level] = b; 14745cd57b2cSChris Mason if (!p->skip_locking) 1475925baeddSChris Mason p->locks[level] = 1; 147665b51a00SChris Mason 1477123abc88SChris Mason ret = check_block(root, p, level); 147865b51a00SChris Mason if (ret) { 147965b51a00SChris Mason ret = -1; 148065b51a00SChris Mason goto done; 148165b51a00SChris Mason } 1482925baeddSChris Mason 14835f39d397SChris Mason ret = bin_search(b, key, level, &slot); 14845f39d397SChris Mason if (level != 0) { 1485be0e5c09SChris Mason if (ret && slot > 0) 1486be0e5c09SChris Mason slot -= 1; 1487be0e5c09SChris Mason p->slots[level] = slot; 14885f39d397SChris Mason if (ins_len > 0 && btrfs_header_nritems(b) >= 14891514794eSChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 1490e089f05cSChris Mason int sret = split_node(trans, root, p, level); 14915c680ed6SChris Mason BUG_ON(sret > 0); 149265b51a00SChris Mason if (sret) { 149365b51a00SChris Mason ret = sret; 149465b51a00SChris Mason goto done; 149565b51a00SChris Mason } 14965c680ed6SChris Mason b = p->nodes[level]; 14975c680ed6SChris Mason slot = p->slots[level]; 1498bb803951SChris Mason } else if (ins_len < 0) { 1499e089f05cSChris Mason int sret = balance_level(trans, root, p, 1500e089f05cSChris Mason level); 150165b51a00SChris Mason if (sret) { 150265b51a00SChris Mason ret = sret; 150365b51a00SChris Mason goto done; 150465b51a00SChris Mason } 1505bb803951SChris Mason b = p->nodes[level]; 1506f510cfecSChris Mason if (!b) { 1507f510cfecSChris Mason btrfs_release_path(NULL, p); 1508bb803951SChris Mason goto again; 1509f510cfecSChris Mason } 1510bb803951SChris Mason slot = p->slots[level]; 15115f39d397SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 15125c680ed6SChris Mason } 1513f9efa9c7SChris Mason unlock_up(p, level, lowest_unlock); 1514f9efa9c7SChris Mason 15159f3a7427SChris Mason /* this is only true while dropping a snapshot */ 1516925baeddSChris Mason if (level == lowest_level) { 15175b21f2edSZheng Yan ret = 0; 15185b21f2edSZheng Yan goto done; 1519925baeddSChris Mason } 1520ca7a79adSChris Mason 1521594a24ebSChris Mason blocknr = btrfs_node_blockptr(b, slot); 1522594a24ebSChris Mason gen = btrfs_node_ptr_generation(b, slot); 1523594a24ebSChris Mason blocksize = btrfs_level_size(root, level - 1); 1524594a24ebSChris Mason 1525594a24ebSChris Mason tmp = btrfs_find_tree_block(root, blocknr, blocksize); 1526594a24ebSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 1527051e1b9fSChris Mason b = tmp; 1528051e1b9fSChris Mason } else { 1529051e1b9fSChris Mason /* 1530051e1b9fSChris Mason * reduce lock contention at high levels 1531051e1b9fSChris Mason * of the btree by dropping locks before 1532051e1b9fSChris Mason * we read. 1533051e1b9fSChris Mason */ 1534051e1b9fSChris Mason if (level > 1) { 1535051e1b9fSChris Mason btrfs_release_path(NULL, p); 1536051e1b9fSChris Mason if (tmp) 1537051e1b9fSChris Mason free_extent_buffer(tmp); 1538f9efa9c7SChris Mason if (should_reada) 1539f9efa9c7SChris Mason reada_for_search(root, p, 1540f9efa9c7SChris Mason level, slot, 1541f9efa9c7SChris Mason key->objectid); 1542f9efa9c7SChris Mason 1543594a24ebSChris Mason tmp = read_tree_block(root, blocknr, 1544594a24ebSChris Mason blocksize, gen); 1545594a24ebSChris Mason if (tmp) 1546594a24ebSChris Mason free_extent_buffer(tmp); 1547051e1b9fSChris Mason goto again; 1548051e1b9fSChris Mason } else { 1549a74a4b97SChris Mason if (tmp) 1550a74a4b97SChris Mason free_extent_buffer(tmp); 1551f9efa9c7SChris Mason if (should_reada) 1552f9efa9c7SChris Mason reada_for_search(root, p, 1553f9efa9c7SChris Mason level, slot, 1554f9efa9c7SChris Mason key->objectid); 1555ca7a79adSChris Mason b = read_node_slot(root, b, slot); 1556051e1b9fSChris Mason } 1557051e1b9fSChris Mason } 15585cd57b2cSChris Mason if (!p->skip_locking) 1559925baeddSChris Mason btrfs_tree_lock(b); 1560be0e5c09SChris Mason } else { 1561be0e5c09SChris Mason p->slots[level] = slot; 15625f39d397SChris Mason if (ins_len > 0 && btrfs_leaf_free_space(root, b) < 15630783fcfcSChris Mason sizeof(struct btrfs_item) + ins_len) { 1564d4dbff95SChris Mason int sret = split_leaf(trans, root, key, 1565cc0c5538SChris Mason p, ins_len, ret == 0); 15665c680ed6SChris Mason BUG_ON(sret > 0); 156765b51a00SChris Mason if (sret) { 156865b51a00SChris Mason ret = sret; 156965b51a00SChris Mason goto done; 157065b51a00SChris Mason } 15715c680ed6SChris Mason } 1572925baeddSChris Mason unlock_up(p, level, lowest_unlock); 157365b51a00SChris Mason goto done; 157465b51a00SChris Mason } 157565b51a00SChris Mason } 157665b51a00SChris Mason ret = 1; 157765b51a00SChris Mason done: 157865b51a00SChris Mason if (prealloc_block.objectid) { 157965b51a00SChris Mason btrfs_free_reserved_extent(root, 158065b51a00SChris Mason prealloc_block.objectid, 158165b51a00SChris Mason prealloc_block.offset); 158265b51a00SChris Mason } 158365b51a00SChris Mason 1584be0e5c09SChris Mason return ret; 1585be0e5c09SChris Mason } 1586be0e5c09SChris Mason 15871a40e23bSZheng Yan int btrfs_merge_path(struct btrfs_trans_handle *trans, 15881a40e23bSZheng Yan struct btrfs_root *root, 15891a40e23bSZheng Yan struct btrfs_key *node_keys, 15901a40e23bSZheng Yan u64 *nodes, int lowest_level) 15911a40e23bSZheng Yan { 15921a40e23bSZheng Yan struct extent_buffer *eb; 15931a40e23bSZheng Yan struct extent_buffer *parent; 15941a40e23bSZheng Yan struct btrfs_key key; 15951a40e23bSZheng Yan u64 bytenr; 15961a40e23bSZheng Yan u64 generation; 15971a40e23bSZheng Yan u32 blocksize; 15981a40e23bSZheng Yan int level; 15991a40e23bSZheng Yan int slot; 16001a40e23bSZheng Yan int key_match; 16011a40e23bSZheng Yan int ret; 16021a40e23bSZheng Yan 16031a40e23bSZheng Yan eb = btrfs_lock_root_node(root); 16041a40e23bSZheng Yan ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0); 16051a40e23bSZheng Yan BUG_ON(ret); 16061a40e23bSZheng Yan 16071a40e23bSZheng Yan parent = eb; 16081a40e23bSZheng Yan while (1) { 16091a40e23bSZheng Yan level = btrfs_header_level(parent); 16101a40e23bSZheng Yan if (level == 0 || level <= lowest_level) 16111a40e23bSZheng Yan break; 16121a40e23bSZheng Yan 16131a40e23bSZheng Yan ret = bin_search(parent, &node_keys[lowest_level], level, 16141a40e23bSZheng Yan &slot); 16151a40e23bSZheng Yan if (ret && slot > 0) 16161a40e23bSZheng Yan slot--; 16171a40e23bSZheng Yan 16181a40e23bSZheng Yan bytenr = btrfs_node_blockptr(parent, slot); 16191a40e23bSZheng Yan if (nodes[level - 1] == bytenr) 16201a40e23bSZheng Yan break; 16211a40e23bSZheng Yan 16221a40e23bSZheng Yan blocksize = btrfs_level_size(root, level - 1); 16231a40e23bSZheng Yan generation = btrfs_node_ptr_generation(parent, slot); 16241a40e23bSZheng Yan btrfs_node_key_to_cpu(eb, &key, slot); 16251a40e23bSZheng Yan key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key)); 16261a40e23bSZheng Yan 1627*f82d02d9SYan Zheng if (generation == trans->transid) { 16281a40e23bSZheng Yan eb = read_tree_block(root, bytenr, blocksize, 16291a40e23bSZheng Yan generation); 16301a40e23bSZheng Yan btrfs_tree_lock(eb); 1631*f82d02d9SYan Zheng } 1632*f82d02d9SYan Zheng 1633*f82d02d9SYan Zheng /* 1634*f82d02d9SYan Zheng * if node keys match and node pointer hasn't been modified 1635*f82d02d9SYan Zheng * in the running transaction, we can merge the path. for 1636*f82d02d9SYan Zheng * blocks owened by reloc trees, the node pointer check is 1637*f82d02d9SYan Zheng * skipped, this is because these blocks are fully controlled 1638*f82d02d9SYan Zheng * by the space balance code, no one else can modify them. 1639*f82d02d9SYan Zheng */ 1640*f82d02d9SYan Zheng if (!nodes[level - 1] || !key_match || 1641*f82d02d9SYan Zheng (generation == trans->transid && 1642*f82d02d9SYan Zheng btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) { 1643*f82d02d9SYan Zheng if (level == 1 || level == lowest_level + 1) { 1644*f82d02d9SYan Zheng if (generation == trans->transid) { 1645*f82d02d9SYan Zheng btrfs_tree_unlock(eb); 1646*f82d02d9SYan Zheng free_extent_buffer(eb); 1647*f82d02d9SYan Zheng } 1648*f82d02d9SYan Zheng break; 1649*f82d02d9SYan Zheng } 1650*f82d02d9SYan Zheng 1651*f82d02d9SYan Zheng if (generation != trans->transid) { 1652*f82d02d9SYan Zheng eb = read_tree_block(root, bytenr, blocksize, 1653*f82d02d9SYan Zheng generation); 1654*f82d02d9SYan Zheng btrfs_tree_lock(eb); 1655*f82d02d9SYan Zheng } 16561a40e23bSZheng Yan 16571a40e23bSZheng Yan ret = btrfs_cow_block(trans, root, eb, parent, slot, 16581a40e23bSZheng Yan &eb, 0); 16591a40e23bSZheng Yan BUG_ON(ret); 16601a40e23bSZheng Yan 1661*f82d02d9SYan Zheng if (root->root_key.objectid == 1662*f82d02d9SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 1663*f82d02d9SYan Zheng if (!nodes[level - 1]) { 1664*f82d02d9SYan Zheng nodes[level - 1] = eb->start; 1665*f82d02d9SYan Zheng memcpy(&node_keys[level - 1], &key, 1666*f82d02d9SYan Zheng sizeof(node_keys[0])); 1667*f82d02d9SYan Zheng } else { 1668*f82d02d9SYan Zheng WARN_ON(1); 1669*f82d02d9SYan Zheng } 1670*f82d02d9SYan Zheng } 1671*f82d02d9SYan Zheng 16721a40e23bSZheng Yan btrfs_tree_unlock(parent); 16731a40e23bSZheng Yan free_extent_buffer(parent); 16741a40e23bSZheng Yan parent = eb; 16751a40e23bSZheng Yan continue; 16761a40e23bSZheng Yan } 16771a40e23bSZheng Yan 16781a40e23bSZheng Yan btrfs_set_node_blockptr(parent, slot, nodes[level - 1]); 16791a40e23bSZheng Yan btrfs_set_node_ptr_generation(parent, slot, trans->transid); 16801a40e23bSZheng Yan btrfs_mark_buffer_dirty(parent); 16811a40e23bSZheng Yan 16821a40e23bSZheng Yan ret = btrfs_inc_extent_ref(trans, root, 16831a40e23bSZheng Yan nodes[level - 1], 16841a40e23bSZheng Yan blocksize, parent->start, 16851a40e23bSZheng Yan btrfs_header_owner(parent), 16861a40e23bSZheng Yan btrfs_header_generation(parent), 16873bb1a1bcSYan Zheng level - 1); 16881a40e23bSZheng Yan BUG_ON(ret); 1689*f82d02d9SYan Zheng 1690*f82d02d9SYan Zheng /* 1691*f82d02d9SYan Zheng * If the block was created in the running transaction, 1692*f82d02d9SYan Zheng * it's possible this is the last reference to it, so we 1693*f82d02d9SYan Zheng * should drop the subtree. 1694*f82d02d9SYan Zheng */ 1695*f82d02d9SYan Zheng if (generation == trans->transid) { 1696*f82d02d9SYan Zheng ret = btrfs_drop_subtree(trans, root, eb, parent); 1697*f82d02d9SYan Zheng BUG_ON(ret); 1698*f82d02d9SYan Zheng btrfs_tree_unlock(eb); 1699*f82d02d9SYan Zheng free_extent_buffer(eb); 1700*f82d02d9SYan Zheng } else { 17011a40e23bSZheng Yan ret = btrfs_free_extent(trans, root, bytenr, 17021a40e23bSZheng Yan blocksize, parent->start, 17031a40e23bSZheng Yan btrfs_header_owner(parent), 17041a40e23bSZheng Yan btrfs_header_generation(parent), 17053bb1a1bcSYan Zheng level - 1, 1); 17061a40e23bSZheng Yan BUG_ON(ret); 17071a40e23bSZheng Yan } 17081a40e23bSZheng Yan break; 17091a40e23bSZheng Yan } 17101a40e23bSZheng Yan btrfs_tree_unlock(parent); 17111a40e23bSZheng Yan free_extent_buffer(parent); 17121a40e23bSZheng Yan return 0; 17131a40e23bSZheng Yan } 17141a40e23bSZheng Yan 171574123bd7SChris Mason /* 171674123bd7SChris Mason * adjust the pointers going up the tree, starting at level 171774123bd7SChris Mason * making sure the right key of each node is points to 'key'. 171874123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 171974123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 172074123bd7SChris Mason * higher levels 1721aa5d6bedSChris Mason * 1722aa5d6bedSChris Mason * If this fails to write a tree block, it returns -1, but continues 1723aa5d6bedSChris Mason * fixing up the blocks in ram so the tree is consistent. 172474123bd7SChris Mason */ 17255f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans, 17265f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 17275f39d397SChris Mason struct btrfs_disk_key *key, int level) 1728be0e5c09SChris Mason { 1729be0e5c09SChris Mason int i; 1730aa5d6bedSChris Mason int ret = 0; 17315f39d397SChris Mason struct extent_buffer *t; 17325f39d397SChris Mason 1733234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1734be0e5c09SChris Mason int tslot = path->slots[i]; 1735eb60ceacSChris Mason if (!path->nodes[i]) 1736be0e5c09SChris Mason break; 17375f39d397SChris Mason t = path->nodes[i]; 17385f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 1739d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 1740be0e5c09SChris Mason if (tslot != 0) 1741be0e5c09SChris Mason break; 1742be0e5c09SChris Mason } 1743aa5d6bedSChris Mason return ret; 1744be0e5c09SChris Mason } 1745be0e5c09SChris Mason 174674123bd7SChris Mason /* 174731840ae1SZheng Yan * update item key. 174831840ae1SZheng Yan * 174931840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 175031840ae1SZheng Yan * that the new key won't break the order 175131840ae1SZheng Yan */ 175231840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans, 175331840ae1SZheng Yan struct btrfs_root *root, struct btrfs_path *path, 175431840ae1SZheng Yan struct btrfs_key *new_key) 175531840ae1SZheng Yan { 175631840ae1SZheng Yan struct btrfs_disk_key disk_key; 175731840ae1SZheng Yan struct extent_buffer *eb; 175831840ae1SZheng Yan int slot; 175931840ae1SZheng Yan 176031840ae1SZheng Yan eb = path->nodes[0]; 176131840ae1SZheng Yan slot = path->slots[0]; 176231840ae1SZheng Yan if (slot > 0) { 176331840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 176431840ae1SZheng Yan if (comp_keys(&disk_key, new_key) >= 0) 176531840ae1SZheng Yan return -1; 176631840ae1SZheng Yan } 176731840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 176831840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 176931840ae1SZheng Yan if (comp_keys(&disk_key, new_key) <= 0) 177031840ae1SZheng Yan return -1; 177131840ae1SZheng Yan } 177231840ae1SZheng Yan 177331840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 177431840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 177531840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 177631840ae1SZheng Yan if (slot == 0) 177731840ae1SZheng Yan fixup_low_keys(trans, root, path, &disk_key, 1); 177831840ae1SZheng Yan return 0; 177931840ae1SZheng Yan } 178031840ae1SZheng Yan 178131840ae1SZheng Yan /* 178274123bd7SChris Mason * try to push data from one node into the next node left in the 178379f95c82SChris Mason * tree. 1784aa5d6bedSChris Mason * 1785aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 1786aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 178774123bd7SChris Mason */ 178898ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 178998ed5174SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 1790971a1f66SChris Mason struct extent_buffer *src, int empty) 1791be0e5c09SChris Mason { 1792be0e5c09SChris Mason int push_items = 0; 1793bb803951SChris Mason int src_nritems; 1794bb803951SChris Mason int dst_nritems; 1795aa5d6bedSChris Mason int ret = 0; 1796be0e5c09SChris Mason 17975f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 17985f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1799123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 18007bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 18017bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 180254aa1f4dSChris Mason 1803bce4eae9SChris Mason if (!empty && src_nritems <= 8) 1804971a1f66SChris Mason return 1; 1805971a1f66SChris Mason 1806eb60ceacSChris Mason if (push_items <= 0) { 1807be0e5c09SChris Mason return 1; 1808eb60ceacSChris Mason } 1809be0e5c09SChris Mason 1810bce4eae9SChris Mason if (empty) { 1811971a1f66SChris Mason push_items = min(src_nritems, push_items); 1812bce4eae9SChris Mason if (push_items < src_nritems) { 1813bce4eae9SChris Mason /* leave at least 8 pointers in the node if 1814bce4eae9SChris Mason * we aren't going to empty it 1815bce4eae9SChris Mason */ 1816bce4eae9SChris Mason if (src_nritems - push_items < 8) { 1817bce4eae9SChris Mason if (push_items <= 8) 1818bce4eae9SChris Mason return 1; 1819bce4eae9SChris Mason push_items -= 8; 1820bce4eae9SChris Mason } 1821bce4eae9SChris Mason } 1822bce4eae9SChris Mason } else 1823bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 182479f95c82SChris Mason 18255f39d397SChris Mason copy_extent_buffer(dst, src, 18265f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 18275f39d397SChris Mason btrfs_node_key_ptr_offset(0), 1828123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 18295f39d397SChris Mason 1830bb803951SChris Mason if (push_items < src_nritems) { 18315f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 18325f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 1833e2fa7227SChris Mason (src_nritems - push_items) * 1834123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 1835bb803951SChris Mason } 18365f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 18375f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 18385f39d397SChris Mason btrfs_mark_buffer_dirty(src); 18395f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 184031840ae1SZheng Yan 184131840ae1SZheng Yan ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items); 184231840ae1SZheng Yan BUG_ON(ret); 184331840ae1SZheng Yan 1844bb803951SChris Mason return ret; 1845be0e5c09SChris Mason } 1846be0e5c09SChris Mason 184797571fd0SChris Mason /* 184879f95c82SChris Mason * try to push data from one node into the next node right in the 184979f95c82SChris Mason * tree. 185079f95c82SChris Mason * 185179f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 185279f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 185379f95c82SChris Mason * 185479f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 185579f95c82SChris Mason */ 18565f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 18575f39d397SChris Mason struct btrfs_root *root, 18585f39d397SChris Mason struct extent_buffer *dst, 18595f39d397SChris Mason struct extent_buffer *src) 186079f95c82SChris Mason { 186179f95c82SChris Mason int push_items = 0; 186279f95c82SChris Mason int max_push; 186379f95c82SChris Mason int src_nritems; 186479f95c82SChris Mason int dst_nritems; 186579f95c82SChris Mason int ret = 0; 186679f95c82SChris Mason 18677bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 18687bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 18697bb86316SChris Mason 18705f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 18715f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1872123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 1873bce4eae9SChris Mason if (push_items <= 0) { 187479f95c82SChris Mason return 1; 1875bce4eae9SChris Mason } 1876bce4eae9SChris Mason 1877bce4eae9SChris Mason if (src_nritems < 4) { 1878bce4eae9SChris Mason return 1; 1879bce4eae9SChris Mason } 188079f95c82SChris Mason 188179f95c82SChris Mason max_push = src_nritems / 2 + 1; 188279f95c82SChris Mason /* don't try to empty the node */ 1883bce4eae9SChris Mason if (max_push >= src_nritems) { 188479f95c82SChris Mason return 1; 1885bce4eae9SChris Mason } 1886252c38f0SYan 188779f95c82SChris Mason if (max_push < push_items) 188879f95c82SChris Mason push_items = max_push; 188979f95c82SChris Mason 18905f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 18915f39d397SChris Mason btrfs_node_key_ptr_offset(0), 18925f39d397SChris Mason (dst_nritems) * 18935f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 1894d6025579SChris Mason 18955f39d397SChris Mason copy_extent_buffer(dst, src, 18965f39d397SChris Mason btrfs_node_key_ptr_offset(0), 18975f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 1898123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 189979f95c82SChris Mason 19005f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 19015f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 190279f95c82SChris Mason 19035f39d397SChris Mason btrfs_mark_buffer_dirty(src); 19045f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 190531840ae1SZheng Yan 190631840ae1SZheng Yan ret = btrfs_update_ref(trans, root, src, dst, 0, push_items); 190731840ae1SZheng Yan BUG_ON(ret); 190831840ae1SZheng Yan 190979f95c82SChris Mason return ret; 191079f95c82SChris Mason } 191179f95c82SChris Mason 191279f95c82SChris Mason /* 191397571fd0SChris Mason * helper function to insert a new root level in the tree. 191497571fd0SChris Mason * A new node is allocated, and a single item is inserted to 191597571fd0SChris Mason * point to the existing root 1916aa5d6bedSChris Mason * 1917aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 191897571fd0SChris Mason */ 191998ed5174SChris Mason static int noinline insert_new_root(struct btrfs_trans_handle *trans, 19205f39d397SChris Mason struct btrfs_root *root, 19215f39d397SChris Mason struct btrfs_path *path, int level) 192274123bd7SChris Mason { 19237bb86316SChris Mason u64 lower_gen; 19245f39d397SChris Mason struct extent_buffer *lower; 19255f39d397SChris Mason struct extent_buffer *c; 1926925baeddSChris Mason struct extent_buffer *old; 19275f39d397SChris Mason struct btrfs_disk_key lower_key; 192831840ae1SZheng Yan int ret; 19295c680ed6SChris Mason 19305c680ed6SChris Mason BUG_ON(path->nodes[level]); 19315c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 19325c680ed6SChris Mason 19337bb86316SChris Mason lower = path->nodes[level-1]; 19347bb86316SChris Mason if (level == 1) 19357bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 19367bb86316SChris Mason else 19377bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 19387bb86316SChris Mason 193931840ae1SZheng Yan c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 194031840ae1SZheng Yan root->root_key.objectid, trans->transid, 1941ad3d81baSChristoph Hellwig level, root->node->start, 0); 19425f39d397SChris Mason if (IS_ERR(c)) 19435f39d397SChris Mason return PTR_ERR(c); 1944925baeddSChris Mason 19455f39d397SChris Mason memset_extent_buffer(c, 0, 0, root->nodesize); 19465f39d397SChris Mason btrfs_set_header_nritems(c, 1); 19475f39d397SChris Mason btrfs_set_header_level(c, level); 1948db94535dSChris Mason btrfs_set_header_bytenr(c, c->start); 19495f39d397SChris Mason btrfs_set_header_generation(c, trans->transid); 19505f39d397SChris Mason btrfs_set_header_owner(c, root->root_key.objectid); 1951d5719762SChris Mason 19525f39d397SChris Mason write_extent_buffer(c, root->fs_info->fsid, 19535f39d397SChris Mason (unsigned long)btrfs_header_fsid(c), 19545f39d397SChris Mason BTRFS_FSID_SIZE); 1955e17cade2SChris Mason 1956e17cade2SChris Mason write_extent_buffer(c, root->fs_info->chunk_tree_uuid, 1957e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(c), 1958e17cade2SChris Mason BTRFS_UUID_SIZE); 1959e17cade2SChris Mason 19605f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 1961db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 19627bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 196331840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 19647bb86316SChris Mason 19657bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 19665f39d397SChris Mason 19675f39d397SChris Mason btrfs_mark_buffer_dirty(c); 1968d5719762SChris Mason 1969925baeddSChris Mason spin_lock(&root->node_lock); 1970925baeddSChris Mason old = root->node; 19715f39d397SChris Mason root->node = c; 1972925baeddSChris Mason spin_unlock(&root->node_lock); 1973925baeddSChris Mason 197431840ae1SZheng Yan ret = btrfs_update_extent_ref(trans, root, lower->start, 197531840ae1SZheng Yan lower->start, c->start, 197631840ae1SZheng Yan root->root_key.objectid, 19773bb1a1bcSYan Zheng trans->transid, level - 1); 197831840ae1SZheng Yan BUG_ON(ret); 197931840ae1SZheng Yan 1980925baeddSChris Mason /* the super has an extra ref to root->node */ 1981925baeddSChris Mason free_extent_buffer(old); 1982925baeddSChris Mason 19830b86a832SChris Mason add_root_to_dirty_list(root); 19845f39d397SChris Mason extent_buffer_get(c); 19855f39d397SChris Mason path->nodes[level] = c; 1986925baeddSChris Mason path->locks[level] = 1; 198774123bd7SChris Mason path->slots[level] = 0; 198874123bd7SChris Mason return 0; 198974123bd7SChris Mason } 19905c680ed6SChris Mason 19915c680ed6SChris Mason /* 19925c680ed6SChris Mason * worker function to insert a single pointer in a node. 19935c680ed6SChris Mason * the node should have enough room for the pointer already 199497571fd0SChris Mason * 19955c680ed6SChris Mason * slot and level indicate where you want the key to go, and 19965c680ed6SChris Mason * blocknr is the block the key points to. 1997aa5d6bedSChris Mason * 1998aa5d6bedSChris Mason * returns zero on success and < 0 on any error 19995c680ed6SChris Mason */ 2000e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root 2001e089f05cSChris Mason *root, struct btrfs_path *path, struct btrfs_disk_key 2002db94535dSChris Mason *key, u64 bytenr, int slot, int level) 20035c680ed6SChris Mason { 20045f39d397SChris Mason struct extent_buffer *lower; 20055c680ed6SChris Mason int nritems; 20065c680ed6SChris Mason 20075c680ed6SChris Mason BUG_ON(!path->nodes[level]); 20085f39d397SChris Mason lower = path->nodes[level]; 20095f39d397SChris Mason nritems = btrfs_header_nritems(lower); 201074123bd7SChris Mason if (slot > nritems) 201174123bd7SChris Mason BUG(); 2012123abc88SChris Mason if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) 201374123bd7SChris Mason BUG(); 201474123bd7SChris Mason if (slot != nritems) { 20155f39d397SChris Mason memmove_extent_buffer(lower, 20165f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 20175f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 2018123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 201974123bd7SChris Mason } 20205f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 2021db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 202274493f7aSChris Mason WARN_ON(trans->transid == 0); 202374493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 20245f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 20255f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 202674123bd7SChris Mason return 0; 202774123bd7SChris Mason } 202874123bd7SChris Mason 202997571fd0SChris Mason /* 203097571fd0SChris Mason * split the node at the specified level in path in two. 203197571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 203297571fd0SChris Mason * 203397571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 203497571fd0SChris Mason * left and right, if either one works, it returns right away. 2035aa5d6bedSChris Mason * 2036aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 203797571fd0SChris Mason */ 2038e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 2039e02119d5SChris Mason struct btrfs_root *root, 2040e02119d5SChris Mason struct btrfs_path *path, int level) 2041be0e5c09SChris Mason { 20425f39d397SChris Mason struct extent_buffer *c; 20435f39d397SChris Mason struct extent_buffer *split; 20445f39d397SChris Mason struct btrfs_disk_key disk_key; 2045be0e5c09SChris Mason int mid; 20465c680ed6SChris Mason int ret; 2047aa5d6bedSChris Mason int wret; 20487518a238SChris Mason u32 c_nritems; 2049be0e5c09SChris Mason 20505f39d397SChris Mason c = path->nodes[level]; 20517bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 20525f39d397SChris Mason if (c == root->node) { 20535c680ed6SChris Mason /* trying to split the root, lets make a new one */ 2054e089f05cSChris Mason ret = insert_new_root(trans, root, path, level + 1); 20555c680ed6SChris Mason if (ret) 20565c680ed6SChris Mason return ret; 2057e66f709bSChris Mason } else { 2058e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 20595f39d397SChris Mason c = path->nodes[level]; 20605f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 2061c448acf0SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) 2062e66f709bSChris Mason return 0; 206354aa1f4dSChris Mason if (ret < 0) 206454aa1f4dSChris Mason return ret; 20655c680ed6SChris Mason } 2066e66f709bSChris Mason 20675f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 20687bb86316SChris Mason 2069925baeddSChris Mason split = btrfs_alloc_free_block(trans, root, root->nodesize, 207031840ae1SZheng Yan path->nodes[level + 1]->start, 20717bb86316SChris Mason root->root_key.objectid, 207231840ae1SZheng Yan trans->transid, level, c->start, 0); 20735f39d397SChris Mason if (IS_ERR(split)) 20745f39d397SChris Mason return PTR_ERR(split); 207554aa1f4dSChris Mason 20765f39d397SChris Mason btrfs_set_header_flags(split, btrfs_header_flags(c)); 20775f39d397SChris Mason btrfs_set_header_level(split, btrfs_header_level(c)); 2078db94535dSChris Mason btrfs_set_header_bytenr(split, split->start); 20795f39d397SChris Mason btrfs_set_header_generation(split, trans->transid); 20805f39d397SChris Mason btrfs_set_header_owner(split, root->root_key.objectid); 208163b10fc4SChris Mason btrfs_set_header_flags(split, 0); 20825f39d397SChris Mason write_extent_buffer(split, root->fs_info->fsid, 20835f39d397SChris Mason (unsigned long)btrfs_header_fsid(split), 20845f39d397SChris Mason BTRFS_FSID_SIZE); 2085e17cade2SChris Mason write_extent_buffer(split, root->fs_info->chunk_tree_uuid, 2086e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(split), 2087e17cade2SChris Mason BTRFS_UUID_SIZE); 20885f39d397SChris Mason 20897518a238SChris Mason mid = (c_nritems + 1) / 2; 20905f39d397SChris Mason 20915f39d397SChris Mason copy_extent_buffer(split, c, 20925f39d397SChris Mason btrfs_node_key_ptr_offset(0), 20935f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 2094123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 20955f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 20965f39d397SChris Mason btrfs_set_header_nritems(c, mid); 2097aa5d6bedSChris Mason ret = 0; 2098aa5d6bedSChris Mason 20995f39d397SChris Mason btrfs_mark_buffer_dirty(c); 21005f39d397SChris Mason btrfs_mark_buffer_dirty(split); 21015f39d397SChris Mason 21025f39d397SChris Mason btrfs_node_key(split, &disk_key, 0); 2103db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, split->start, 21045f39d397SChris Mason path->slots[level + 1] + 1, 2105123abc88SChris Mason level + 1); 2106aa5d6bedSChris Mason if (wret) 2107aa5d6bedSChris Mason ret = wret; 2108aa5d6bedSChris Mason 210931840ae1SZheng Yan ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid); 211031840ae1SZheng Yan BUG_ON(ret); 211131840ae1SZheng Yan 21125de08d7dSChris Mason if (path->slots[level] >= mid) { 21135c680ed6SChris Mason path->slots[level] -= mid; 2114925baeddSChris Mason btrfs_tree_unlock(c); 21155f39d397SChris Mason free_extent_buffer(c); 21165f39d397SChris Mason path->nodes[level] = split; 21175c680ed6SChris Mason path->slots[level + 1] += 1; 2118eb60ceacSChris Mason } else { 2119925baeddSChris Mason btrfs_tree_unlock(split); 21205f39d397SChris Mason free_extent_buffer(split); 2121be0e5c09SChris Mason } 2122aa5d6bedSChris Mason return ret; 2123be0e5c09SChris Mason } 2124be0e5c09SChris Mason 212574123bd7SChris Mason /* 212674123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 212774123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 212874123bd7SChris Mason * space used both by the item structs and the item data 212974123bd7SChris Mason */ 21305f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 2131be0e5c09SChris Mason { 2132be0e5c09SChris Mason int data_len; 21335f39d397SChris Mason int nritems = btrfs_header_nritems(l); 2134d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 2135be0e5c09SChris Mason 2136be0e5c09SChris Mason if (!nr) 2137be0e5c09SChris Mason return 0; 21385f39d397SChris Mason data_len = btrfs_item_end_nr(l, start); 21395f39d397SChris Mason data_len = data_len - btrfs_item_offset_nr(l, end); 21400783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 2141d4dbff95SChris Mason WARN_ON(data_len < 0); 2142be0e5c09SChris Mason return data_len; 2143be0e5c09SChris Mason } 2144be0e5c09SChris Mason 214574123bd7SChris Mason /* 2146d4dbff95SChris Mason * The space between the end of the leaf items and 2147d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 2148d4dbff95SChris Mason * the leaf has left for both items and data 2149d4dbff95SChris Mason */ 2150e02119d5SChris Mason int noinline btrfs_leaf_free_space(struct btrfs_root *root, 2151e02119d5SChris Mason struct extent_buffer *leaf) 2152d4dbff95SChris Mason { 21535f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 21545f39d397SChris Mason int ret; 21555f39d397SChris Mason ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); 21565f39d397SChris Mason if (ret < 0) { 21575f39d397SChris Mason printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n", 2158ae2f5411SJens Axboe ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), 21595f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 21605f39d397SChris Mason } 21615f39d397SChris Mason return ret; 2162d4dbff95SChris Mason } 2163d4dbff95SChris Mason 2164d4dbff95SChris Mason /* 216500ec4c51SChris Mason * push some data in the path leaf to the right, trying to free up at 216600ec4c51SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 2167aa5d6bedSChris Mason * 2168aa5d6bedSChris Mason * returns 1 if the push failed because the other node didn't have enough 2169aa5d6bedSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 217000ec4c51SChris Mason */ 2171e089f05cSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 217234a38218SChris Mason *root, struct btrfs_path *path, int data_size, 217334a38218SChris Mason int empty) 217400ec4c51SChris Mason { 21755f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 21765f39d397SChris Mason struct extent_buffer *right; 21775f39d397SChris Mason struct extent_buffer *upper; 21785f39d397SChris Mason struct btrfs_disk_key disk_key; 217900ec4c51SChris Mason int slot; 218034a38218SChris Mason u32 i; 218100ec4c51SChris Mason int free_space; 218200ec4c51SChris Mason int push_space = 0; 218300ec4c51SChris Mason int push_items = 0; 21840783fcfcSChris Mason struct btrfs_item *item; 21857518a238SChris Mason u32 left_nritems; 218634a38218SChris Mason u32 nr; 21877518a238SChris Mason u32 right_nritems; 21885f39d397SChris Mason u32 data_end; 2189db94535dSChris Mason u32 this_item_size; 219054aa1f4dSChris Mason int ret; 219100ec4c51SChris Mason 219200ec4c51SChris Mason slot = path->slots[1]; 219300ec4c51SChris Mason if (!path->nodes[1]) { 219400ec4c51SChris Mason return 1; 219500ec4c51SChris Mason } 219600ec4c51SChris Mason upper = path->nodes[1]; 21975f39d397SChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 219800ec4c51SChris Mason return 1; 21995f39d397SChris Mason 2200a2135011SChris Mason WARN_ON(!btrfs_tree_locked(path->nodes[1])); 2201a2135011SChris Mason 2202ca7a79adSChris Mason right = read_node_slot(root, upper, slot + 1); 2203925baeddSChris Mason btrfs_tree_lock(right); 2204123abc88SChris Mason free_space = btrfs_leaf_free_space(root, right); 2205925baeddSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) 2206925baeddSChris Mason goto out_unlock; 220702217ed2SChris Mason 22085f39d397SChris Mason /* cow and double check */ 22095f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, upper, 221065b51a00SChris Mason slot + 1, &right, 0); 2211925baeddSChris Mason if (ret) 2212925baeddSChris Mason goto out_unlock; 2213925baeddSChris Mason 22145f39d397SChris Mason free_space = btrfs_leaf_free_space(root, right); 2215925baeddSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) 2216925baeddSChris Mason goto out_unlock; 22175f39d397SChris Mason 22185f39d397SChris Mason left_nritems = btrfs_header_nritems(left); 2219925baeddSChris Mason if (left_nritems == 0) 2220925baeddSChris Mason goto out_unlock; 22215f39d397SChris Mason 222234a38218SChris Mason if (empty) 222334a38218SChris Mason nr = 0; 222434a38218SChris Mason else 222534a38218SChris Mason nr = 1; 222634a38218SChris Mason 222731840ae1SZheng Yan if (path->slots[0] >= left_nritems) 222831840ae1SZheng Yan push_space += data_size + sizeof(*item); 222931840ae1SZheng Yan 223034a38218SChris Mason i = left_nritems - 1; 223134a38218SChris Mason while (i >= nr) { 22325f39d397SChris Mason item = btrfs_item_nr(left, i); 2233db94535dSChris Mason 223431840ae1SZheng Yan if (!empty && push_items > 0) { 223531840ae1SZheng Yan if (path->slots[0] > i) 223631840ae1SZheng Yan break; 223731840ae1SZheng Yan if (path->slots[0] == i) { 223831840ae1SZheng Yan int space = btrfs_leaf_free_space(root, left); 223931840ae1SZheng Yan if (space + push_space * 2 > free_space) 224031840ae1SZheng Yan break; 224131840ae1SZheng Yan } 224231840ae1SZheng Yan } 224331840ae1SZheng Yan 224400ec4c51SChris Mason if (path->slots[0] == i) 224500ec4c51SChris Mason push_space += data_size + sizeof(*item); 2246db94535dSChris Mason 2247db94535dSChris Mason if (!left->map_token) { 2248db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 2249db94535dSChris Mason sizeof(struct btrfs_item), 2250db94535dSChris Mason &left->map_token, &left->kaddr, 2251db94535dSChris Mason &left->map_start, &left->map_len, 2252db94535dSChris Mason KM_USER1); 2253db94535dSChris Mason } 2254db94535dSChris Mason 2255db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 2256db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 225700ec4c51SChris Mason break; 225831840ae1SZheng Yan 225900ec4c51SChris Mason push_items++; 2260db94535dSChris Mason push_space += this_item_size + sizeof(*item); 226134a38218SChris Mason if (i == 0) 226234a38218SChris Mason break; 226334a38218SChris Mason i--; 2264db94535dSChris Mason } 2265db94535dSChris Mason if (left->map_token) { 2266db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 2267db94535dSChris Mason left->map_token = NULL; 226800ec4c51SChris Mason } 22695f39d397SChris Mason 2270925baeddSChris Mason if (push_items == 0) 2271925baeddSChris Mason goto out_unlock; 22725f39d397SChris Mason 227334a38218SChris Mason if (!empty && push_items == left_nritems) 2274a429e513SChris Mason WARN_ON(1); 22755f39d397SChris Mason 227600ec4c51SChris Mason /* push left to right */ 22775f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 227834a38218SChris Mason 22795f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 2280123abc88SChris Mason push_space -= leaf_data_end(root, left); 22815f39d397SChris Mason 228200ec4c51SChris Mason /* make room in the right data area */ 22835f39d397SChris Mason data_end = leaf_data_end(root, right); 22845f39d397SChris Mason memmove_extent_buffer(right, 22855f39d397SChris Mason btrfs_leaf_data(right) + data_end - push_space, 22865f39d397SChris Mason btrfs_leaf_data(right) + data_end, 22875f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root) - data_end); 22885f39d397SChris Mason 228900ec4c51SChris Mason /* copy from the left data area */ 22905f39d397SChris Mason copy_extent_buffer(right, left, btrfs_leaf_data(right) + 2291d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2292d6025579SChris Mason btrfs_leaf_data(left) + leaf_data_end(root, left), 2293d6025579SChris Mason push_space); 22945f39d397SChris Mason 22955f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 22965f39d397SChris Mason btrfs_item_nr_offset(0), 22970783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 22985f39d397SChris Mason 229900ec4c51SChris Mason /* copy the items from left to right */ 23005f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 23015f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 23020783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 230300ec4c51SChris Mason 230400ec4c51SChris Mason /* update the item pointers */ 23057518a238SChris Mason right_nritems += push_items; 23065f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 2307123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 23087518a238SChris Mason for (i = 0; i < right_nritems; i++) { 23095f39d397SChris Mason item = btrfs_item_nr(right, i); 2310db94535dSChris Mason if (!right->map_token) { 2311db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2312db94535dSChris Mason sizeof(struct btrfs_item), 2313db94535dSChris Mason &right->map_token, &right->kaddr, 2314db94535dSChris Mason &right->map_start, &right->map_len, 2315db94535dSChris Mason KM_USER1); 2316db94535dSChris Mason } 2317db94535dSChris Mason push_space -= btrfs_item_size(right, item); 2318db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2319db94535dSChris Mason } 2320db94535dSChris Mason 2321db94535dSChris Mason if (right->map_token) { 2322db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2323db94535dSChris Mason right->map_token = NULL; 232400ec4c51SChris Mason } 23257518a238SChris Mason left_nritems -= push_items; 23265f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 232700ec4c51SChris Mason 232834a38218SChris Mason if (left_nritems) 23295f39d397SChris Mason btrfs_mark_buffer_dirty(left); 23305f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2331a429e513SChris Mason 233231840ae1SZheng Yan ret = btrfs_update_ref(trans, root, left, right, 0, push_items); 233331840ae1SZheng Yan BUG_ON(ret); 233431840ae1SZheng Yan 23355f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 23365f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 2337d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 233802217ed2SChris Mason 233900ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 23407518a238SChris Mason if (path->slots[0] >= left_nritems) { 23417518a238SChris Mason path->slots[0] -= left_nritems; 2342925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 2343925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 2344925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 23455f39d397SChris Mason free_extent_buffer(path->nodes[0]); 23465f39d397SChris Mason path->nodes[0] = right; 234700ec4c51SChris Mason path->slots[1] += 1; 234800ec4c51SChris Mason } else { 2349925baeddSChris Mason btrfs_tree_unlock(right); 23505f39d397SChris Mason free_extent_buffer(right); 235100ec4c51SChris Mason } 235200ec4c51SChris Mason return 0; 2353925baeddSChris Mason 2354925baeddSChris Mason out_unlock: 2355925baeddSChris Mason btrfs_tree_unlock(right); 2356925baeddSChris Mason free_extent_buffer(right); 2357925baeddSChris Mason return 1; 235800ec4c51SChris Mason } 2359925baeddSChris Mason 236000ec4c51SChris Mason /* 236174123bd7SChris Mason * push some data in the path leaf to the left, trying to free up at 236274123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 236374123bd7SChris Mason */ 2364e089f05cSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 236534a38218SChris Mason *root, struct btrfs_path *path, int data_size, 236634a38218SChris Mason int empty) 2367be0e5c09SChris Mason { 23685f39d397SChris Mason struct btrfs_disk_key disk_key; 23695f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 23705f39d397SChris Mason struct extent_buffer *left; 2371be0e5c09SChris Mason int slot; 2372be0e5c09SChris Mason int i; 2373be0e5c09SChris Mason int free_space; 2374be0e5c09SChris Mason int push_space = 0; 2375be0e5c09SChris Mason int push_items = 0; 23760783fcfcSChris Mason struct btrfs_item *item; 23777518a238SChris Mason u32 old_left_nritems; 23785f39d397SChris Mason u32 right_nritems; 237934a38218SChris Mason u32 nr; 2380aa5d6bedSChris Mason int ret = 0; 2381aa5d6bedSChris Mason int wret; 2382db94535dSChris Mason u32 this_item_size; 2383db94535dSChris Mason u32 old_left_item_size; 2384be0e5c09SChris Mason 2385be0e5c09SChris Mason slot = path->slots[1]; 23865f39d397SChris Mason if (slot == 0) 2387be0e5c09SChris Mason return 1; 23885f39d397SChris Mason if (!path->nodes[1]) 2389be0e5c09SChris Mason return 1; 23905f39d397SChris Mason 23913685f791SChris Mason right_nritems = btrfs_header_nritems(right); 23923685f791SChris Mason if (right_nritems == 0) { 23933685f791SChris Mason return 1; 23943685f791SChris Mason } 23953685f791SChris Mason 2396a2135011SChris Mason WARN_ON(!btrfs_tree_locked(path->nodes[1])); 2397a2135011SChris Mason 2398ca7a79adSChris Mason left = read_node_slot(root, path->nodes[1], slot - 1); 2399925baeddSChris Mason btrfs_tree_lock(left); 2400123abc88SChris Mason free_space = btrfs_leaf_free_space(root, left); 24010783fcfcSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) { 2402925baeddSChris Mason ret = 1; 2403925baeddSChris Mason goto out; 2404be0e5c09SChris Mason } 240502217ed2SChris Mason 240602217ed2SChris Mason /* cow and double check */ 24075f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, 240865b51a00SChris Mason path->nodes[1], slot - 1, &left, 0); 240954aa1f4dSChris Mason if (ret) { 241054aa1f4dSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 2411925baeddSChris Mason ret = 1; 2412925baeddSChris Mason goto out; 241354aa1f4dSChris Mason } 24143685f791SChris Mason 2415123abc88SChris Mason free_space = btrfs_leaf_free_space(root, left); 24160783fcfcSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) { 2417925baeddSChris Mason ret = 1; 2418925baeddSChris Mason goto out; 241902217ed2SChris Mason } 242002217ed2SChris Mason 242134a38218SChris Mason if (empty) 242234a38218SChris Mason nr = right_nritems; 242334a38218SChris Mason else 242434a38218SChris Mason nr = right_nritems - 1; 242534a38218SChris Mason 242634a38218SChris Mason for (i = 0; i < nr; i++) { 24275f39d397SChris Mason item = btrfs_item_nr(right, i); 2428db94535dSChris Mason if (!right->map_token) { 2429db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2430db94535dSChris Mason sizeof(struct btrfs_item), 2431db94535dSChris Mason &right->map_token, &right->kaddr, 2432db94535dSChris Mason &right->map_start, &right->map_len, 2433db94535dSChris Mason KM_USER1); 2434db94535dSChris Mason } 2435db94535dSChris Mason 243631840ae1SZheng Yan if (!empty && push_items > 0) { 243731840ae1SZheng Yan if (path->slots[0] < i) 243831840ae1SZheng Yan break; 243931840ae1SZheng Yan if (path->slots[0] == i) { 244031840ae1SZheng Yan int space = btrfs_leaf_free_space(root, right); 244131840ae1SZheng Yan if (space + push_space * 2 > free_space) 244231840ae1SZheng Yan break; 244331840ae1SZheng Yan } 244431840ae1SZheng Yan } 244531840ae1SZheng Yan 2446be0e5c09SChris Mason if (path->slots[0] == i) 2447be0e5c09SChris Mason push_space += data_size + sizeof(*item); 2448db94535dSChris Mason 2449db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 2450db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 2451be0e5c09SChris Mason break; 2452db94535dSChris Mason 2453be0e5c09SChris Mason push_items++; 2454db94535dSChris Mason push_space += this_item_size + sizeof(*item); 2455be0e5c09SChris Mason } 2456db94535dSChris Mason 2457db94535dSChris Mason if (right->map_token) { 2458db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2459db94535dSChris Mason right->map_token = NULL; 2460db94535dSChris Mason } 2461db94535dSChris Mason 2462be0e5c09SChris Mason if (push_items == 0) { 2463925baeddSChris Mason ret = 1; 2464925baeddSChris Mason goto out; 2465be0e5c09SChris Mason } 246634a38218SChris Mason if (!empty && push_items == btrfs_header_nritems(right)) 2467a429e513SChris Mason WARN_ON(1); 24685f39d397SChris Mason 2469be0e5c09SChris Mason /* push data from right to left */ 24705f39d397SChris Mason copy_extent_buffer(left, right, 24715f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 24725f39d397SChris Mason btrfs_item_nr_offset(0), 24735f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 24745f39d397SChris Mason 2475123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root) - 24765f39d397SChris Mason btrfs_item_offset_nr(right, push_items -1); 24775f39d397SChris Mason 24785f39d397SChris Mason copy_extent_buffer(left, right, btrfs_leaf_data(left) + 2479d6025579SChris Mason leaf_data_end(root, left) - push_space, 2480123abc88SChris Mason btrfs_leaf_data(right) + 24815f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 2482be0e5c09SChris Mason push_space); 24835f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 2484eb60ceacSChris Mason BUG_ON(old_left_nritems < 0); 2485eb60ceacSChris Mason 2486db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 2487be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 24885f39d397SChris Mason u32 ioff; 2489db94535dSChris Mason 24905f39d397SChris Mason item = btrfs_item_nr(left, i); 2491db94535dSChris Mason if (!left->map_token) { 2492db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 2493db94535dSChris Mason sizeof(struct btrfs_item), 2494db94535dSChris Mason &left->map_token, &left->kaddr, 2495db94535dSChris Mason &left->map_start, &left->map_len, 2496db94535dSChris Mason KM_USER1); 2497db94535dSChris Mason } 2498db94535dSChris Mason 24995f39d397SChris Mason ioff = btrfs_item_offset(left, item); 25005f39d397SChris Mason btrfs_set_item_offset(left, item, 2501db94535dSChris Mason ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); 2502be0e5c09SChris Mason } 25035f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 2504db94535dSChris Mason if (left->map_token) { 2505db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 2506db94535dSChris Mason left->map_token = NULL; 2507db94535dSChris Mason } 2508be0e5c09SChris Mason 2509be0e5c09SChris Mason /* fixup right node */ 251034a38218SChris Mason if (push_items > right_nritems) { 251134a38218SChris Mason printk("push items %d nr %u\n", push_items, right_nritems); 251234a38218SChris Mason WARN_ON(1); 251334a38218SChris Mason } 251434a38218SChris Mason 251534a38218SChris Mason if (push_items < right_nritems) { 25165f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 2517123abc88SChris Mason leaf_data_end(root, right); 25185f39d397SChris Mason memmove_extent_buffer(right, btrfs_leaf_data(right) + 2519d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2520d6025579SChris Mason btrfs_leaf_data(right) + 2521123abc88SChris Mason leaf_data_end(root, right), push_space); 25225f39d397SChris Mason 25235f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 25245f39d397SChris Mason btrfs_item_nr_offset(push_items), 25255f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 25260783fcfcSChris Mason sizeof(struct btrfs_item)); 252734a38218SChris Mason } 2528eef1c494SYan right_nritems -= push_items; 2529eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 2530123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 25315f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 25325f39d397SChris Mason item = btrfs_item_nr(right, i); 2533db94535dSChris Mason 2534db94535dSChris Mason if (!right->map_token) { 2535db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2536db94535dSChris Mason sizeof(struct btrfs_item), 2537db94535dSChris Mason &right->map_token, &right->kaddr, 2538db94535dSChris Mason &right->map_start, &right->map_len, 2539db94535dSChris Mason KM_USER1); 2540db94535dSChris Mason } 2541db94535dSChris Mason 2542db94535dSChris Mason push_space = push_space - btrfs_item_size(right, item); 2543db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2544db94535dSChris Mason } 2545db94535dSChris Mason if (right->map_token) { 2546db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2547db94535dSChris Mason right->map_token = NULL; 2548be0e5c09SChris Mason } 2549eb60ceacSChris Mason 25505f39d397SChris Mason btrfs_mark_buffer_dirty(left); 255134a38218SChris Mason if (right_nritems) 25525f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2553098f59c2SChris Mason 255431840ae1SZheng Yan ret = btrfs_update_ref(trans, root, right, left, 255531840ae1SZheng Yan old_left_nritems, push_items); 255631840ae1SZheng Yan BUG_ON(ret); 255731840ae1SZheng Yan 25585f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 25595f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, 1); 2560aa5d6bedSChris Mason if (wret) 2561aa5d6bedSChris Mason ret = wret; 2562be0e5c09SChris Mason 2563be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 2564be0e5c09SChris Mason if (path->slots[0] < push_items) { 2565be0e5c09SChris Mason path->slots[0] += old_left_nritems; 2566925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 2567925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 2568925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 25695f39d397SChris Mason free_extent_buffer(path->nodes[0]); 25705f39d397SChris Mason path->nodes[0] = left; 2571be0e5c09SChris Mason path->slots[1] -= 1; 2572be0e5c09SChris Mason } else { 2573925baeddSChris Mason btrfs_tree_unlock(left); 25745f39d397SChris Mason free_extent_buffer(left); 2575be0e5c09SChris Mason path->slots[0] -= push_items; 2576be0e5c09SChris Mason } 2577eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2578aa5d6bedSChris Mason return ret; 2579925baeddSChris Mason out: 2580925baeddSChris Mason btrfs_tree_unlock(left); 2581925baeddSChris Mason free_extent_buffer(left); 2582925baeddSChris Mason return ret; 2583be0e5c09SChris Mason } 2584be0e5c09SChris Mason 258574123bd7SChris Mason /* 258674123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 258774123bd7SChris Mason * available for the resulting leaf level of the path. 2588aa5d6bedSChris Mason * 2589aa5d6bedSChris Mason * returns 0 if all went well and < 0 on failure. 259074123bd7SChris Mason */ 2591e02119d5SChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 2592e02119d5SChris Mason struct btrfs_root *root, 2593e02119d5SChris Mason struct btrfs_key *ins_key, 2594e02119d5SChris Mason struct btrfs_path *path, int data_size, 2595e02119d5SChris Mason int extend) 2596be0e5c09SChris Mason { 25975f39d397SChris Mason struct extent_buffer *l; 25987518a238SChris Mason u32 nritems; 2599eb60ceacSChris Mason int mid; 2600eb60ceacSChris Mason int slot; 26015f39d397SChris Mason struct extent_buffer *right; 26020783fcfcSChris Mason int space_needed = data_size + sizeof(struct btrfs_item); 2603be0e5c09SChris Mason int data_copy_size; 2604be0e5c09SChris Mason int rt_data_off; 2605be0e5c09SChris Mason int i; 2606d4dbff95SChris Mason int ret = 0; 2607aa5d6bedSChris Mason int wret; 2608cc0c5538SChris Mason int double_split; 2609cc0c5538SChris Mason int num_doubles = 0; 2610d4dbff95SChris Mason struct btrfs_disk_key disk_key; 2611be0e5c09SChris Mason 2612cc0c5538SChris Mason if (extend) 2613cc0c5538SChris Mason space_needed = data_size; 2614cc0c5538SChris Mason 261540689478SChris Mason /* first try to make some room by pushing left and right */ 26163685f791SChris Mason if (ins_key->type != BTRFS_DIR_ITEM_KEY) { 261734a38218SChris Mason wret = push_leaf_right(trans, root, path, data_size, 0); 26183326d1b0SChris Mason if (wret < 0) { 2619eaee50e8SChris Mason return wret; 26203326d1b0SChris Mason } 2621eaee50e8SChris Mason if (wret) { 262234a38218SChris Mason wret = push_leaf_left(trans, root, path, data_size, 0); 2623eaee50e8SChris Mason if (wret < 0) 2624eaee50e8SChris Mason return wret; 2625eaee50e8SChris Mason } 26265f39d397SChris Mason l = path->nodes[0]; 2627aa5d6bedSChris Mason 2628aa5d6bedSChris Mason /* did the pushes work? */ 2629cc0c5538SChris Mason if (btrfs_leaf_free_space(root, l) >= space_needed) 2630be0e5c09SChris Mason return 0; 26313326d1b0SChris Mason } 2632aa5d6bedSChris Mason 26335c680ed6SChris Mason if (!path->nodes[1]) { 2634e089f05cSChris Mason ret = insert_new_root(trans, root, path, 1); 26355c680ed6SChris Mason if (ret) 26365c680ed6SChris Mason return ret; 26375c680ed6SChris Mason } 2638cc0c5538SChris Mason again: 2639cc0c5538SChris Mason double_split = 0; 2640cc0c5538SChris Mason l = path->nodes[0]; 2641eb60ceacSChris Mason slot = path->slots[0]; 26425f39d397SChris Mason nritems = btrfs_header_nritems(l); 2643eb60ceacSChris Mason mid = (nritems + 1)/ 2; 264454aa1f4dSChris Mason 2645925baeddSChris Mason right = btrfs_alloc_free_block(trans, root, root->leafsize, 264631840ae1SZheng Yan path->nodes[1]->start, 26477bb86316SChris Mason root->root_key.objectid, 264831840ae1SZheng Yan trans->transid, 0, l->start, 0); 2649cea9e445SChris Mason if (IS_ERR(right)) { 2650cea9e445SChris Mason BUG_ON(1); 26515f39d397SChris Mason return PTR_ERR(right); 2652cea9e445SChris Mason } 265354aa1f4dSChris Mason 26545f39d397SChris Mason memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); 2655db94535dSChris Mason btrfs_set_header_bytenr(right, right->start); 26565f39d397SChris Mason btrfs_set_header_generation(right, trans->transid); 26575f39d397SChris Mason btrfs_set_header_owner(right, root->root_key.objectid); 26585f39d397SChris Mason btrfs_set_header_level(right, 0); 26595f39d397SChris Mason write_extent_buffer(right, root->fs_info->fsid, 26605f39d397SChris Mason (unsigned long)btrfs_header_fsid(right), 26615f39d397SChris Mason BTRFS_FSID_SIZE); 2662e17cade2SChris Mason 2663e17cade2SChris Mason write_extent_buffer(right, root->fs_info->chunk_tree_uuid, 2664e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(right), 2665e17cade2SChris Mason BTRFS_UUID_SIZE); 2666d4dbff95SChris Mason if (mid <= slot) { 2667d4dbff95SChris Mason if (nritems == 1 || 2668d4dbff95SChris Mason leaf_space_used(l, mid, nritems - mid) + space_needed > 2669d4dbff95SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 2670d4dbff95SChris Mason if (slot >= nritems) { 2671d4dbff95SChris Mason btrfs_cpu_key_to_disk(&disk_key, ins_key); 26725f39d397SChris Mason btrfs_set_header_nritems(right, 0); 2673d4dbff95SChris Mason wret = insert_ptr(trans, root, path, 2674db94535dSChris Mason &disk_key, right->start, 2675d4dbff95SChris Mason path->slots[1] + 1, 1); 2676d4dbff95SChris Mason if (wret) 2677d4dbff95SChris Mason ret = wret; 2678925baeddSChris Mason 2679925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 26805f39d397SChris Mason free_extent_buffer(path->nodes[0]); 26815f39d397SChris Mason path->nodes[0] = right; 2682d4dbff95SChris Mason path->slots[0] = 0; 2683d4dbff95SChris Mason path->slots[1] += 1; 26840ef8b242SChris Mason btrfs_mark_buffer_dirty(right); 2685d4dbff95SChris Mason return ret; 2686d4dbff95SChris Mason } 2687d4dbff95SChris Mason mid = slot; 26883326d1b0SChris Mason if (mid != nritems && 26893326d1b0SChris Mason leaf_space_used(l, mid, nritems - mid) + 26903326d1b0SChris Mason space_needed > BTRFS_LEAF_DATA_SIZE(root)) { 2691d4dbff95SChris Mason double_split = 1; 2692d4dbff95SChris Mason } 26933326d1b0SChris Mason } 2694d4dbff95SChris Mason } else { 2695d4dbff95SChris Mason if (leaf_space_used(l, 0, mid + 1) + space_needed > 2696d4dbff95SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 2697cc0c5538SChris Mason if (!extend && slot == 0) { 2698d4dbff95SChris Mason btrfs_cpu_key_to_disk(&disk_key, ins_key); 26995f39d397SChris Mason btrfs_set_header_nritems(right, 0); 2700d4dbff95SChris Mason wret = insert_ptr(trans, root, path, 2701d4dbff95SChris Mason &disk_key, 2702db94535dSChris Mason right->start, 2703098f59c2SChris Mason path->slots[1], 1); 2704d4dbff95SChris Mason if (wret) 2705d4dbff95SChris Mason ret = wret; 2706925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 27075f39d397SChris Mason free_extent_buffer(path->nodes[0]); 27085f39d397SChris Mason path->nodes[0] = right; 2709d4dbff95SChris Mason path->slots[0] = 0; 2710a429e513SChris Mason if (path->slots[1] == 0) { 2711a429e513SChris Mason wret = fixup_low_keys(trans, root, 2712a429e513SChris Mason path, &disk_key, 1); 2713a429e513SChris Mason if (wret) 2714a429e513SChris Mason ret = wret; 2715a429e513SChris Mason } 27160ef8b242SChris Mason btrfs_mark_buffer_dirty(right); 2717d4dbff95SChris Mason return ret; 2718cc0c5538SChris Mason } else if (extend && slot == 0) { 2719cc0c5538SChris Mason mid = 1; 2720cc0c5538SChris Mason } else { 2721d4dbff95SChris Mason mid = slot; 27225ee78ac7SChris Mason if (mid != nritems && 27235ee78ac7SChris Mason leaf_space_used(l, mid, nritems - mid) + 27245ee78ac7SChris Mason space_needed > BTRFS_LEAF_DATA_SIZE(root)) { 2725d4dbff95SChris Mason double_split = 1; 2726d4dbff95SChris Mason } 2727d4dbff95SChris Mason } 27285ee78ac7SChris Mason } 2729cc0c5538SChris Mason } 27305f39d397SChris Mason nritems = nritems - mid; 27315f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 27325f39d397SChris Mason data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 27335f39d397SChris Mason 27345f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 27355f39d397SChris Mason btrfs_item_nr_offset(mid), 27365f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 27375f39d397SChris Mason 27385f39d397SChris Mason copy_extent_buffer(right, l, 2739d6025579SChris Mason btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - 2740123abc88SChris Mason data_copy_size, btrfs_leaf_data(l) + 2741123abc88SChris Mason leaf_data_end(root, l), data_copy_size); 274274123bd7SChris Mason 27435f39d397SChris Mason rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - 27445f39d397SChris Mason btrfs_item_end_nr(l, mid); 27455f39d397SChris Mason 27465f39d397SChris Mason for (i = 0; i < nritems; i++) { 27475f39d397SChris Mason struct btrfs_item *item = btrfs_item_nr(right, i); 2748db94535dSChris Mason u32 ioff; 2749db94535dSChris Mason 2750db94535dSChris Mason if (!right->map_token) { 2751db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2752db94535dSChris Mason sizeof(struct btrfs_item), 2753db94535dSChris Mason &right->map_token, &right->kaddr, 2754db94535dSChris Mason &right->map_start, &right->map_len, 2755db94535dSChris Mason KM_USER1); 2756db94535dSChris Mason } 2757db94535dSChris Mason 2758db94535dSChris Mason ioff = btrfs_item_offset(right, item); 27595f39d397SChris Mason btrfs_set_item_offset(right, item, ioff + rt_data_off); 27600783fcfcSChris Mason } 276174123bd7SChris Mason 2762db94535dSChris Mason if (right->map_token) { 2763db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2764db94535dSChris Mason right->map_token = NULL; 2765db94535dSChris Mason } 2766db94535dSChris Mason 27675f39d397SChris Mason btrfs_set_header_nritems(l, mid); 2768aa5d6bedSChris Mason ret = 0; 27695f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 2770db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, right->start, 2771db94535dSChris Mason path->slots[1] + 1, 1); 2772aa5d6bedSChris Mason if (wret) 2773aa5d6bedSChris Mason ret = wret; 27745f39d397SChris Mason 27755f39d397SChris Mason btrfs_mark_buffer_dirty(right); 27765f39d397SChris Mason btrfs_mark_buffer_dirty(l); 2777eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 27785f39d397SChris Mason 277931840ae1SZheng Yan ret = btrfs_update_ref(trans, root, l, right, 0, nritems); 278031840ae1SZheng Yan BUG_ON(ret); 278131840ae1SZheng Yan 2782be0e5c09SChris Mason if (mid <= slot) { 2783925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 27845f39d397SChris Mason free_extent_buffer(path->nodes[0]); 27855f39d397SChris Mason path->nodes[0] = right; 2786be0e5c09SChris Mason path->slots[0] -= mid; 2787be0e5c09SChris Mason path->slots[1] += 1; 2788925baeddSChris Mason } else { 2789925baeddSChris Mason btrfs_tree_unlock(right); 27905f39d397SChris Mason free_extent_buffer(right); 2791925baeddSChris Mason } 27925f39d397SChris Mason 2793eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2794d4dbff95SChris Mason 2795cc0c5538SChris Mason if (double_split) { 2796cc0c5538SChris Mason BUG_ON(num_doubles != 0); 2797cc0c5538SChris Mason num_doubles++; 2798cc0c5538SChris Mason goto again; 27993326d1b0SChris Mason } 2800be0e5c09SChris Mason return ret; 2801be0e5c09SChris Mason } 2802be0e5c09SChris Mason 2803d352ac68SChris Mason /* 2804d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 2805d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 2806d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 2807d352ac68SChris Mason * the front. 2808d352ac68SChris Mason */ 2809b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans, 2810b18c6685SChris Mason struct btrfs_root *root, 2811b18c6685SChris Mason struct btrfs_path *path, 2812179e29e4SChris Mason u32 new_size, int from_end) 2813b18c6685SChris Mason { 2814b18c6685SChris Mason int ret = 0; 2815b18c6685SChris Mason int slot; 2816b18c6685SChris Mason int slot_orig; 28175f39d397SChris Mason struct extent_buffer *leaf; 28185f39d397SChris Mason struct btrfs_item *item; 2819b18c6685SChris Mason u32 nritems; 2820b18c6685SChris Mason unsigned int data_end; 2821b18c6685SChris Mason unsigned int old_data_start; 2822b18c6685SChris Mason unsigned int old_size; 2823b18c6685SChris Mason unsigned int size_diff; 2824b18c6685SChris Mason int i; 2825b18c6685SChris Mason 2826b18c6685SChris Mason slot_orig = path->slots[0]; 28275f39d397SChris Mason leaf = path->nodes[0]; 2828179e29e4SChris Mason slot = path->slots[0]; 2829179e29e4SChris Mason 2830179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 2831179e29e4SChris Mason if (old_size == new_size) 2832179e29e4SChris Mason return 0; 2833b18c6685SChris Mason 28345f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 2835b18c6685SChris Mason data_end = leaf_data_end(root, leaf); 2836b18c6685SChris Mason 28375f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 2838179e29e4SChris Mason 2839b18c6685SChris Mason size_diff = old_size - new_size; 2840b18c6685SChris Mason 2841b18c6685SChris Mason BUG_ON(slot < 0); 2842b18c6685SChris Mason BUG_ON(slot >= nritems); 2843b18c6685SChris Mason 2844b18c6685SChris Mason /* 2845b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 2846b18c6685SChris Mason */ 2847b18c6685SChris Mason /* first correct the data pointers */ 2848b18c6685SChris Mason for (i = slot; i < nritems; i++) { 28495f39d397SChris Mason u32 ioff; 28505f39d397SChris Mason item = btrfs_item_nr(leaf, i); 2851db94535dSChris Mason 2852db94535dSChris Mason if (!leaf->map_token) { 2853db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 2854db94535dSChris Mason sizeof(struct btrfs_item), 2855db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 2856db94535dSChris Mason &leaf->map_start, &leaf->map_len, 2857db94535dSChris Mason KM_USER1); 2858db94535dSChris Mason } 2859db94535dSChris Mason 28605f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 28615f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + size_diff); 2862b18c6685SChris Mason } 2863db94535dSChris Mason 2864db94535dSChris Mason if (leaf->map_token) { 2865db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2866db94535dSChris Mason leaf->map_token = NULL; 2867db94535dSChris Mason } 2868db94535dSChris Mason 2869b18c6685SChris Mason /* shift the data */ 2870179e29e4SChris Mason if (from_end) { 28715f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2872b18c6685SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 2873b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 2874179e29e4SChris Mason } else { 2875179e29e4SChris Mason struct btrfs_disk_key disk_key; 2876179e29e4SChris Mason u64 offset; 2877179e29e4SChris Mason 2878179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 2879179e29e4SChris Mason 2880179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 2881179e29e4SChris Mason unsigned long ptr; 2882179e29e4SChris Mason struct btrfs_file_extent_item *fi; 2883179e29e4SChris Mason 2884179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 2885179e29e4SChris Mason struct btrfs_file_extent_item); 2886179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 2887179e29e4SChris Mason (unsigned long)fi - size_diff); 2888179e29e4SChris Mason 2889179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 2890179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 2891179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 2892179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 2893179e29e4SChris Mason (unsigned long)fi, 2894179e29e4SChris Mason offsetof(struct btrfs_file_extent_item, 2895179e29e4SChris Mason disk_bytenr)); 2896179e29e4SChris Mason } 2897179e29e4SChris Mason } 2898179e29e4SChris Mason 2899179e29e4SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2900179e29e4SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 2901179e29e4SChris Mason data_end, old_data_start - data_end); 2902179e29e4SChris Mason 2903179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 2904179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 2905179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 2906179e29e4SChris Mason if (slot == 0) 2907179e29e4SChris Mason fixup_low_keys(trans, root, path, &disk_key, 1); 2908179e29e4SChris Mason } 29095f39d397SChris Mason 29105f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 29115f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 29125f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 2913b18c6685SChris Mason 2914b18c6685SChris Mason ret = 0; 29155f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 29165f39d397SChris Mason btrfs_print_leaf(root, leaf); 2917b18c6685SChris Mason BUG(); 29185f39d397SChris Mason } 2919b18c6685SChris Mason return ret; 2920b18c6685SChris Mason } 2921b18c6685SChris Mason 2922d352ac68SChris Mason /* 2923d352ac68SChris Mason * make the item pointed to by the path bigger, data_size is the new size. 2924d352ac68SChris Mason */ 29255f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans, 29265f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 29275f39d397SChris Mason u32 data_size) 29286567e837SChris Mason { 29296567e837SChris Mason int ret = 0; 29306567e837SChris Mason int slot; 29316567e837SChris Mason int slot_orig; 29325f39d397SChris Mason struct extent_buffer *leaf; 29335f39d397SChris Mason struct btrfs_item *item; 29346567e837SChris Mason u32 nritems; 29356567e837SChris Mason unsigned int data_end; 29366567e837SChris Mason unsigned int old_data; 29376567e837SChris Mason unsigned int old_size; 29386567e837SChris Mason int i; 29396567e837SChris Mason 29406567e837SChris Mason slot_orig = path->slots[0]; 29415f39d397SChris Mason leaf = path->nodes[0]; 29426567e837SChris Mason 29435f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 29446567e837SChris Mason data_end = leaf_data_end(root, leaf); 29456567e837SChris Mason 29465f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < data_size) { 29475f39d397SChris Mason btrfs_print_leaf(root, leaf); 29486567e837SChris Mason BUG(); 29495f39d397SChris Mason } 29506567e837SChris Mason slot = path->slots[0]; 29515f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 29526567e837SChris Mason 29536567e837SChris Mason BUG_ON(slot < 0); 29543326d1b0SChris Mason if (slot >= nritems) { 29553326d1b0SChris Mason btrfs_print_leaf(root, leaf); 29563326d1b0SChris Mason printk("slot %d too large, nritems %d\n", slot, nritems); 29573326d1b0SChris Mason BUG_ON(1); 29583326d1b0SChris Mason } 29596567e837SChris Mason 29606567e837SChris Mason /* 29616567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 29626567e837SChris Mason */ 29636567e837SChris Mason /* first correct the data pointers */ 29646567e837SChris Mason for (i = slot; i < nritems; i++) { 29655f39d397SChris Mason u32 ioff; 29665f39d397SChris Mason item = btrfs_item_nr(leaf, i); 2967db94535dSChris Mason 2968db94535dSChris Mason if (!leaf->map_token) { 2969db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 2970db94535dSChris Mason sizeof(struct btrfs_item), 2971db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 2972db94535dSChris Mason &leaf->map_start, &leaf->map_len, 2973db94535dSChris Mason KM_USER1); 2974db94535dSChris Mason } 29755f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 29765f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff - data_size); 29776567e837SChris Mason } 29785f39d397SChris Mason 2979db94535dSChris Mason if (leaf->map_token) { 2980db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2981db94535dSChris Mason leaf->map_token = NULL; 2982db94535dSChris Mason } 2983db94535dSChris Mason 29846567e837SChris Mason /* shift the data */ 29855f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 29866567e837SChris Mason data_end - data_size, btrfs_leaf_data(leaf) + 29876567e837SChris Mason data_end, old_data - data_end); 29885f39d397SChris Mason 29896567e837SChris Mason data_end = old_data; 29905f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 29915f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 29925f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 29935f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 29946567e837SChris Mason 29956567e837SChris Mason ret = 0; 29965f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 29975f39d397SChris Mason btrfs_print_leaf(root, leaf); 29986567e837SChris Mason BUG(); 29995f39d397SChris Mason } 30006567e837SChris Mason return ret; 30016567e837SChris Mason } 30026567e837SChris Mason 300374123bd7SChris Mason /* 3004d352ac68SChris Mason * Given a key and some data, insert items into the tree. 300574123bd7SChris Mason * This does all the path init required, making room in the tree if needed. 300674123bd7SChris Mason */ 30079c58309dSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 30085f39d397SChris Mason struct btrfs_root *root, 30095f39d397SChris Mason struct btrfs_path *path, 30109c58309dSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 30119c58309dSChris Mason int nr) 3012be0e5c09SChris Mason { 30135f39d397SChris Mason struct extent_buffer *leaf; 30145f39d397SChris Mason struct btrfs_item *item; 3015aa5d6bedSChris Mason int ret = 0; 3016be0e5c09SChris Mason int slot; 3017eb60ceacSChris Mason int slot_orig; 30189c58309dSChris Mason int i; 30197518a238SChris Mason u32 nritems; 30209c58309dSChris Mason u32 total_size = 0; 30219c58309dSChris Mason u32 total_data = 0; 3022be0e5c09SChris Mason unsigned int data_end; 3023e2fa7227SChris Mason struct btrfs_disk_key disk_key; 3024e2fa7227SChris Mason 30259c58309dSChris Mason for (i = 0; i < nr; i++) { 30269c58309dSChris Mason total_data += data_size[i]; 30279c58309dSChris Mason } 3028be0e5c09SChris Mason 30297b128766SJosef Bacik total_size = total_data + (nr * sizeof(struct btrfs_item)); 30309c58309dSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 30310f9dd46cSJosef Bacik if (ret == 0) 3032f0930a37SChris Mason return -EEXIST; 3033ed2ff2cbSChris Mason if (ret < 0) 3034ed2ff2cbSChris Mason goto out; 3035be0e5c09SChris Mason 303662e2749eSChris Mason slot_orig = path->slots[0]; 30375f39d397SChris Mason leaf = path->nodes[0]; 303874123bd7SChris Mason 30395f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3040123abc88SChris Mason data_end = leaf_data_end(root, leaf); 3041eb60ceacSChris Mason 3042f25956ccSChris Mason if (btrfs_leaf_free_space(root, leaf) < total_size) { 30433326d1b0SChris Mason btrfs_print_leaf(root, leaf); 30443326d1b0SChris Mason printk("not enough freespace need %u have %d\n", 30459c58309dSChris Mason total_size, btrfs_leaf_free_space(root, leaf)); 3046be0e5c09SChris Mason BUG(); 3047d4dbff95SChris Mason } 30485f39d397SChris Mason 304962e2749eSChris Mason slot = path->slots[0]; 3050eb60ceacSChris Mason BUG_ON(slot < 0); 30515f39d397SChris Mason 3052be0e5c09SChris Mason if (slot != nritems) { 30535f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 3054be0e5c09SChris Mason 30555f39d397SChris Mason if (old_data < data_end) { 30565f39d397SChris Mason btrfs_print_leaf(root, leaf); 30575f39d397SChris Mason printk("slot %d old_data %d data_end %d\n", 30585f39d397SChris Mason slot, old_data, data_end); 30595f39d397SChris Mason BUG_ON(1); 30605f39d397SChris Mason } 3061be0e5c09SChris Mason /* 3062be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 3063be0e5c09SChris Mason */ 3064be0e5c09SChris Mason /* first correct the data pointers */ 3065db94535dSChris Mason WARN_ON(leaf->map_token); 30660783fcfcSChris Mason for (i = slot; i < nritems; i++) { 30675f39d397SChris Mason u32 ioff; 3068db94535dSChris Mason 30695f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3070db94535dSChris Mason if (!leaf->map_token) { 3071db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3072db94535dSChris Mason sizeof(struct btrfs_item), 3073db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3074db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3075db94535dSChris Mason KM_USER1); 3076db94535dSChris Mason } 3077db94535dSChris Mason 30785f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 30799c58309dSChris Mason btrfs_set_item_offset(leaf, item, ioff - total_data); 30800783fcfcSChris Mason } 3081db94535dSChris Mason if (leaf->map_token) { 3082db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3083db94535dSChris Mason leaf->map_token = NULL; 3084db94535dSChris Mason } 3085be0e5c09SChris Mason 3086be0e5c09SChris Mason /* shift the items */ 30879c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 30885f39d397SChris Mason btrfs_item_nr_offset(slot), 30890783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 3090be0e5c09SChris Mason 3091be0e5c09SChris Mason /* shift the data */ 30925f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 30939c58309dSChris Mason data_end - total_data, btrfs_leaf_data(leaf) + 3094be0e5c09SChris Mason data_end, old_data - data_end); 3095be0e5c09SChris Mason data_end = old_data; 3096be0e5c09SChris Mason } 30975f39d397SChris Mason 309862e2749eSChris Mason /* setup the item for the new data */ 30999c58309dSChris Mason for (i = 0; i < nr; i++) { 31009c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 31019c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 31029c58309dSChris Mason item = btrfs_item_nr(leaf, slot + i); 31039c58309dSChris Mason btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 31049c58309dSChris Mason data_end -= data_size[i]; 31059c58309dSChris Mason btrfs_set_item_size(leaf, item, data_size[i]); 31069c58309dSChris Mason } 31079c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 31085f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3109aa5d6bedSChris Mason 3110aa5d6bedSChris Mason ret = 0; 31115a01a2e3SChris Mason if (slot == 0) { 31125a01a2e3SChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key); 3113e089f05cSChris Mason ret = fixup_low_keys(trans, root, path, &disk_key, 1); 31145a01a2e3SChris Mason } 3115aa5d6bedSChris Mason 31165f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 31175f39d397SChris Mason btrfs_print_leaf(root, leaf); 3118be0e5c09SChris Mason BUG(); 31195f39d397SChris Mason } 3120ed2ff2cbSChris Mason out: 312162e2749eSChris Mason return ret; 312262e2749eSChris Mason } 312362e2749eSChris Mason 312462e2749eSChris Mason /* 312562e2749eSChris Mason * Given a key and some data, insert an item into the tree. 312662e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 312762e2749eSChris Mason */ 3128e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root 3129e089f05cSChris Mason *root, struct btrfs_key *cpu_key, void *data, u32 3130e089f05cSChris Mason data_size) 313162e2749eSChris Mason { 313262e2749eSChris Mason int ret = 0; 31332c90e5d6SChris Mason struct btrfs_path *path; 31345f39d397SChris Mason struct extent_buffer *leaf; 31355f39d397SChris Mason unsigned long ptr; 313662e2749eSChris Mason 31372c90e5d6SChris Mason path = btrfs_alloc_path(); 31382c90e5d6SChris Mason BUG_ON(!path); 31392c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 314062e2749eSChris Mason if (!ret) { 31415f39d397SChris Mason leaf = path->nodes[0]; 31425f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 31435f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 31445f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 314562e2749eSChris Mason } 31462c90e5d6SChris Mason btrfs_free_path(path); 3147aa5d6bedSChris Mason return ret; 3148be0e5c09SChris Mason } 3149be0e5c09SChris Mason 315074123bd7SChris Mason /* 31515de08d7dSChris Mason * delete the pointer from a given node. 315274123bd7SChris Mason * 3153d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 3154d352ac68SChris Mason * empty a node. 315574123bd7SChris Mason */ 3156e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3157e089f05cSChris Mason struct btrfs_path *path, int level, int slot) 3158be0e5c09SChris Mason { 31595f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 31607518a238SChris Mason u32 nritems; 3161aa5d6bedSChris Mason int ret = 0; 3162bb803951SChris Mason int wret; 3163be0e5c09SChris Mason 31645f39d397SChris Mason nritems = btrfs_header_nritems(parent); 3165be0e5c09SChris Mason if (slot != nritems -1) { 31665f39d397SChris Mason memmove_extent_buffer(parent, 31675f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 31685f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 3169d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 3170d6025579SChris Mason (nritems - slot - 1)); 3171be0e5c09SChris Mason } 31727518a238SChris Mason nritems--; 31735f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 31747518a238SChris Mason if (nritems == 0 && parent == root->node) { 31755f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 3176eb60ceacSChris Mason /* just turn the root into a leaf and break */ 31775f39d397SChris Mason btrfs_set_header_level(root->node, 0); 3178bb803951SChris Mason } else if (slot == 0) { 31795f39d397SChris Mason struct btrfs_disk_key disk_key; 31805f39d397SChris Mason 31815f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 31825f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); 31830f70abe2SChris Mason if (wret) 31840f70abe2SChris Mason ret = wret; 3185be0e5c09SChris Mason } 3186d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 3187aa5d6bedSChris Mason return ret; 3188be0e5c09SChris Mason } 3189be0e5c09SChris Mason 319074123bd7SChris Mason /* 3191323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 3192323ac95bSChris Mason * path->nodes[1]. bytenr is the node block pointer, but since the callers 3193323ac95bSChris Mason * already know it, it is faster to have them pass it down than to 3194323ac95bSChris Mason * read it out of the node again. 3195323ac95bSChris Mason * 3196323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 3197323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 3198323ac95bSChris Mason * 3199323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 3200323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 3201323ac95bSChris Mason */ 3202323ac95bSChris Mason noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans, 3203323ac95bSChris Mason struct btrfs_root *root, 3204323ac95bSChris Mason struct btrfs_path *path, u64 bytenr) 3205323ac95bSChris Mason { 3206323ac95bSChris Mason int ret; 3207323ac95bSChris Mason u64 root_gen = btrfs_header_generation(path->nodes[1]); 3208323ac95bSChris Mason 3209323ac95bSChris Mason ret = del_ptr(trans, root, path, 1, path->slots[1]); 3210323ac95bSChris Mason if (ret) 3211323ac95bSChris Mason return ret; 3212323ac95bSChris Mason 3213323ac95bSChris Mason ret = btrfs_free_extent(trans, root, bytenr, 3214323ac95bSChris Mason btrfs_level_size(root, 0), 3215323ac95bSChris Mason path->nodes[1]->start, 3216323ac95bSChris Mason btrfs_header_owner(path->nodes[1]), 32173bb1a1bcSYan Zheng root_gen, 0, 1); 3218323ac95bSChris Mason return ret; 3219323ac95bSChris Mason } 3220323ac95bSChris Mason /* 322174123bd7SChris Mason * delete the item at the leaf level in path. If that empties 322274123bd7SChris Mason * the leaf, remove it from the tree 322374123bd7SChris Mason */ 322485e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 322585e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 3226be0e5c09SChris Mason { 32275f39d397SChris Mason struct extent_buffer *leaf; 32285f39d397SChris Mason struct btrfs_item *item; 322985e21bacSChris Mason int last_off; 323085e21bacSChris Mason int dsize = 0; 3231aa5d6bedSChris Mason int ret = 0; 3232aa5d6bedSChris Mason int wret; 323385e21bacSChris Mason int i; 32347518a238SChris Mason u32 nritems; 3235be0e5c09SChris Mason 32365f39d397SChris Mason leaf = path->nodes[0]; 323785e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 323885e21bacSChris Mason 323985e21bacSChris Mason for (i = 0; i < nr; i++) 324085e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 324185e21bacSChris Mason 32425f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3243be0e5c09SChris Mason 324485e21bacSChris Mason if (slot + nr != nritems) { 3245123abc88SChris Mason int data_end = leaf_data_end(root, leaf); 32465f39d397SChris Mason 32475f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3248d6025579SChris Mason data_end + dsize, 3249123abc88SChris Mason btrfs_leaf_data(leaf) + data_end, 325085e21bacSChris Mason last_off - data_end); 32515f39d397SChris Mason 325285e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 32535f39d397SChris Mason u32 ioff; 3254db94535dSChris Mason 32555f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3256db94535dSChris Mason if (!leaf->map_token) { 3257db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3258db94535dSChris Mason sizeof(struct btrfs_item), 3259db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3260db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3261db94535dSChris Mason KM_USER1); 3262db94535dSChris Mason } 32635f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 32645f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + dsize); 32650783fcfcSChris Mason } 3266db94535dSChris Mason 3267db94535dSChris Mason if (leaf->map_token) { 3268db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3269db94535dSChris Mason leaf->map_token = NULL; 3270db94535dSChris Mason } 3271db94535dSChris Mason 32725f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 327385e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 32740783fcfcSChris Mason sizeof(struct btrfs_item) * 327585e21bacSChris Mason (nritems - slot - nr)); 3276be0e5c09SChris Mason } 327785e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 327885e21bacSChris Mason nritems -= nr; 32795f39d397SChris Mason 328074123bd7SChris Mason /* delete the leaf if we've emptied it */ 32817518a238SChris Mason if (nritems == 0) { 32825f39d397SChris Mason if (leaf == root->node) { 32835f39d397SChris Mason btrfs_set_header_level(leaf, 0); 32849a8dd150SChris Mason } else { 3285323ac95bSChris Mason ret = btrfs_del_leaf(trans, root, path, leaf->start); 3286323ac95bSChris Mason BUG_ON(ret); 32879a8dd150SChris Mason } 3288be0e5c09SChris Mason } else { 32897518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 3290aa5d6bedSChris Mason if (slot == 0) { 32915f39d397SChris Mason struct btrfs_disk_key disk_key; 32925f39d397SChris Mason 32935f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 3294e089f05cSChris Mason wret = fixup_low_keys(trans, root, path, 32955f39d397SChris Mason &disk_key, 1); 3296aa5d6bedSChris Mason if (wret) 3297aa5d6bedSChris Mason ret = wret; 3298aa5d6bedSChris Mason } 3299aa5d6bedSChris Mason 330074123bd7SChris Mason /* delete the leaf if it is mostly empty */ 330185e21bacSChris Mason if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) { 3302be0e5c09SChris Mason /* push_leaf_left fixes the path. 3303be0e5c09SChris Mason * make sure the path still points to our leaf 3304be0e5c09SChris Mason * for possible call to del_ptr below 3305be0e5c09SChris Mason */ 33064920c9acSChris Mason slot = path->slots[1]; 33075f39d397SChris Mason extent_buffer_get(leaf); 33085f39d397SChris Mason 330985e21bacSChris Mason wret = push_leaf_left(trans, root, path, 1, 1); 331054aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3311aa5d6bedSChris Mason ret = wret; 33125f39d397SChris Mason 33135f39d397SChris Mason if (path->nodes[0] == leaf && 33145f39d397SChris Mason btrfs_header_nritems(leaf)) { 331585e21bacSChris Mason wret = push_leaf_right(trans, root, path, 1, 1); 331654aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3317aa5d6bedSChris Mason ret = wret; 3318aa5d6bedSChris Mason } 33195f39d397SChris Mason 33205f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 3321323ac95bSChris Mason path->slots[1] = slot; 3322323ac95bSChris Mason ret = btrfs_del_leaf(trans, root, path, leaf->start); 3323323ac95bSChris Mason BUG_ON(ret); 33245f39d397SChris Mason free_extent_buffer(leaf); 33255de08d7dSChris Mason } else { 3326925baeddSChris Mason /* if we're still in the path, make sure 3327925baeddSChris Mason * we're dirty. Otherwise, one of the 3328925baeddSChris Mason * push_leaf functions must have already 3329925baeddSChris Mason * dirtied this buffer 3330925baeddSChris Mason */ 3331925baeddSChris Mason if (path->nodes[0] == leaf) 33325f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 33335f39d397SChris Mason free_extent_buffer(leaf); 3334be0e5c09SChris Mason } 3335d5719762SChris Mason } else { 33365f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3337be0e5c09SChris Mason } 33389a8dd150SChris Mason } 3339aa5d6bedSChris Mason return ret; 33409a8dd150SChris Mason } 33419a8dd150SChris Mason 334297571fd0SChris Mason /* 3343925baeddSChris Mason * search the tree again to find a leaf with lesser keys 33447bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 33457bb86316SChris Mason * returns < 0 on io errors. 3346d352ac68SChris Mason * 3347d352ac68SChris Mason * This may release the path, and so you may lose any locks held at the 3348d352ac68SChris Mason * time you call it. 33497bb86316SChris Mason */ 33507bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 33517bb86316SChris Mason { 3352925baeddSChris Mason struct btrfs_key key; 3353925baeddSChris Mason struct btrfs_disk_key found_key; 3354925baeddSChris Mason int ret; 33557bb86316SChris Mason 3356925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 3357925baeddSChris Mason 3358925baeddSChris Mason if (key.offset > 0) 3359925baeddSChris Mason key.offset--; 3360925baeddSChris Mason else if (key.type > 0) 3361925baeddSChris Mason key.type--; 3362925baeddSChris Mason else if (key.objectid > 0) 3363925baeddSChris Mason key.objectid--; 3364925baeddSChris Mason else 33657bb86316SChris Mason return 1; 33667bb86316SChris Mason 3367925baeddSChris Mason btrfs_release_path(root, path); 3368925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 3369925baeddSChris Mason if (ret < 0) 3370925baeddSChris Mason return ret; 3371925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 3372925baeddSChris Mason ret = comp_keys(&found_key, &key); 3373925baeddSChris Mason if (ret < 0) 33747bb86316SChris Mason return 0; 3375925baeddSChris Mason return 1; 33767bb86316SChris Mason } 33777bb86316SChris Mason 33783f157a2fSChris Mason /* 33793f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 33803f157a2fSChris Mason * for nodes or leaves that are either in cache or have a minimum 3381d352ac68SChris Mason * transaction id. This is used by the btree defrag code, and tree logging 33823f157a2fSChris Mason * 33833f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 33843f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 33853f157a2fSChris Mason * key and get a writable path. 33863f157a2fSChris Mason * 33873f157a2fSChris Mason * This does lock as it descends, and path->keep_locks should be set 33883f157a2fSChris Mason * to 1 by the caller. 33893f157a2fSChris Mason * 33903f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 33913f157a2fSChris Mason * of the tree. 33923f157a2fSChris Mason * 3393d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 3394d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 3395d352ac68SChris Mason * skipped over (without reading them). 3396d352ac68SChris Mason * 33973f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 33983f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 33993f157a2fSChris Mason */ 34003f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 3401e02119d5SChris Mason struct btrfs_key *max_key, 34023f157a2fSChris Mason struct btrfs_path *path, int cache_only, 34033f157a2fSChris Mason u64 min_trans) 34043f157a2fSChris Mason { 34053f157a2fSChris Mason struct extent_buffer *cur; 34063f157a2fSChris Mason struct btrfs_key found_key; 34073f157a2fSChris Mason int slot; 34089652480bSYan int sret; 34093f157a2fSChris Mason u32 nritems; 34103f157a2fSChris Mason int level; 34113f157a2fSChris Mason int ret = 1; 34123f157a2fSChris Mason 34133f157a2fSChris Mason again: 34143f157a2fSChris Mason cur = btrfs_lock_root_node(root); 34153f157a2fSChris Mason level = btrfs_header_level(cur); 3416e02119d5SChris Mason WARN_ON(path->nodes[level]); 34173f157a2fSChris Mason path->nodes[level] = cur; 34183f157a2fSChris Mason path->locks[level] = 1; 34193f157a2fSChris Mason 34203f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 34213f157a2fSChris Mason ret = 1; 34223f157a2fSChris Mason goto out; 34233f157a2fSChris Mason } 34243f157a2fSChris Mason while(1) { 34253f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 34263f157a2fSChris Mason level = btrfs_header_level(cur); 34279652480bSYan sret = bin_search(cur, min_key, level, &slot); 34283f157a2fSChris Mason 3429323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 3430323ac95bSChris Mason if (level == path->lowest_level) { 3431e02119d5SChris Mason if (slot >= nritems) 3432e02119d5SChris Mason goto find_next_key; 34333f157a2fSChris Mason ret = 0; 34343f157a2fSChris Mason path->slots[level] = slot; 34353f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 34363f157a2fSChris Mason goto out; 34373f157a2fSChris Mason } 34389652480bSYan if (sret && slot > 0) 34399652480bSYan slot--; 34403f157a2fSChris Mason /* 34413f157a2fSChris Mason * check this node pointer against the cache_only and 34423f157a2fSChris Mason * min_trans parameters. If it isn't in cache or is too 34433f157a2fSChris Mason * old, skip to the next one. 34443f157a2fSChris Mason */ 34453f157a2fSChris Mason while(slot < nritems) { 34463f157a2fSChris Mason u64 blockptr; 34473f157a2fSChris Mason u64 gen; 34483f157a2fSChris Mason struct extent_buffer *tmp; 3449e02119d5SChris Mason struct btrfs_disk_key disk_key; 3450e02119d5SChris Mason 34513f157a2fSChris Mason blockptr = btrfs_node_blockptr(cur, slot); 34523f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 34533f157a2fSChris Mason if (gen < min_trans) { 34543f157a2fSChris Mason slot++; 34553f157a2fSChris Mason continue; 34563f157a2fSChris Mason } 34573f157a2fSChris Mason if (!cache_only) 34583f157a2fSChris Mason break; 34593f157a2fSChris Mason 3460e02119d5SChris Mason if (max_key) { 3461e02119d5SChris Mason btrfs_node_key(cur, &disk_key, slot); 3462e02119d5SChris Mason if (comp_keys(&disk_key, max_key) >= 0) { 3463e02119d5SChris Mason ret = 1; 3464e02119d5SChris Mason goto out; 3465e02119d5SChris Mason } 3466e02119d5SChris Mason } 3467e02119d5SChris Mason 34683f157a2fSChris Mason tmp = btrfs_find_tree_block(root, blockptr, 34693f157a2fSChris Mason btrfs_level_size(root, level - 1)); 34703f157a2fSChris Mason 34713f157a2fSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 34723f157a2fSChris Mason free_extent_buffer(tmp); 34733f157a2fSChris Mason break; 34743f157a2fSChris Mason } 34753f157a2fSChris Mason if (tmp) 34763f157a2fSChris Mason free_extent_buffer(tmp); 34773f157a2fSChris Mason slot++; 34783f157a2fSChris Mason } 3479e02119d5SChris Mason find_next_key: 34803f157a2fSChris Mason /* 34813f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 34823f157a2fSChris Mason * and find another one 34833f157a2fSChris Mason */ 34843f157a2fSChris Mason if (slot >= nritems) { 3485e02119d5SChris Mason path->slots[level] = slot; 3486e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 34873f157a2fSChris Mason cache_only, min_trans); 3488e02119d5SChris Mason if (sret == 0) { 34893f157a2fSChris Mason btrfs_release_path(root, path); 34903f157a2fSChris Mason goto again; 34913f157a2fSChris Mason } else { 34923f157a2fSChris Mason goto out; 34933f157a2fSChris Mason } 34943f157a2fSChris Mason } 34953f157a2fSChris Mason /* save our key for returning back */ 34963f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 34973f157a2fSChris Mason path->slots[level] = slot; 34983f157a2fSChris Mason if (level == path->lowest_level) { 34993f157a2fSChris Mason ret = 0; 35003f157a2fSChris Mason unlock_up(path, level, 1); 35013f157a2fSChris Mason goto out; 35023f157a2fSChris Mason } 35033f157a2fSChris Mason cur = read_node_slot(root, cur, slot); 35043f157a2fSChris Mason 35053f157a2fSChris Mason btrfs_tree_lock(cur); 35063f157a2fSChris Mason path->locks[level - 1] = 1; 35073f157a2fSChris Mason path->nodes[level - 1] = cur; 35083f157a2fSChris Mason unlock_up(path, level, 1); 35093f157a2fSChris Mason } 35103f157a2fSChris Mason out: 35113f157a2fSChris Mason if (ret == 0) 35123f157a2fSChris Mason memcpy(min_key, &found_key, sizeof(found_key)); 35133f157a2fSChris Mason return ret; 35143f157a2fSChris Mason } 35153f157a2fSChris Mason 35163f157a2fSChris Mason /* 35173f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 35183f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 35193f157a2fSChris Mason * tree based on the current path and the cache_only and min_trans 35203f157a2fSChris Mason * parameters. 35213f157a2fSChris Mason * 35223f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 35233f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 35243f157a2fSChris Mason * 35253f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 35263f157a2fSChris Mason * calling this function. 35273f157a2fSChris Mason */ 3528e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 35293f157a2fSChris Mason struct btrfs_key *key, int lowest_level, 35303f157a2fSChris Mason int cache_only, u64 min_trans) 3531e7a84565SChris Mason { 3532e7a84565SChris Mason int level = lowest_level; 3533e7a84565SChris Mason int slot; 3534e7a84565SChris Mason struct extent_buffer *c; 3535e7a84565SChris Mason 3536e7a84565SChris Mason while(level < BTRFS_MAX_LEVEL) { 3537e7a84565SChris Mason if (!path->nodes[level]) 3538e7a84565SChris Mason return 1; 3539e7a84565SChris Mason 3540e7a84565SChris Mason slot = path->slots[level] + 1; 3541e7a84565SChris Mason c = path->nodes[level]; 35423f157a2fSChris Mason next: 3543e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 3544e7a84565SChris Mason level++; 3545e7a84565SChris Mason if (level == BTRFS_MAX_LEVEL) { 3546e7a84565SChris Mason return 1; 3547e7a84565SChris Mason } 3548e7a84565SChris Mason continue; 3549e7a84565SChris Mason } 3550e7a84565SChris Mason if (level == 0) 3551e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 35523f157a2fSChris Mason else { 35533f157a2fSChris Mason u64 blockptr = btrfs_node_blockptr(c, slot); 35543f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 35553f157a2fSChris Mason 35563f157a2fSChris Mason if (cache_only) { 35573f157a2fSChris Mason struct extent_buffer *cur; 35583f157a2fSChris Mason cur = btrfs_find_tree_block(root, blockptr, 35593f157a2fSChris Mason btrfs_level_size(root, level - 1)); 35603f157a2fSChris Mason if (!cur || !btrfs_buffer_uptodate(cur, gen)) { 35613f157a2fSChris Mason slot++; 35623f157a2fSChris Mason if (cur) 35633f157a2fSChris Mason free_extent_buffer(cur); 35643f157a2fSChris Mason goto next; 35653f157a2fSChris Mason } 35663f157a2fSChris Mason free_extent_buffer(cur); 35673f157a2fSChris Mason } 35683f157a2fSChris Mason if (gen < min_trans) { 35693f157a2fSChris Mason slot++; 35703f157a2fSChris Mason goto next; 35713f157a2fSChris Mason } 3572e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 35733f157a2fSChris Mason } 3574e7a84565SChris Mason return 0; 3575e7a84565SChris Mason } 3576e7a84565SChris Mason return 1; 3577e7a84565SChris Mason } 3578e7a84565SChris Mason 35797bb86316SChris Mason /* 3580925baeddSChris Mason * search the tree again to find a leaf with greater keys 35810f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 35820f70abe2SChris Mason * returns < 0 on io errors. 358397571fd0SChris Mason */ 3584234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 3585d97e63b6SChris Mason { 3586d97e63b6SChris Mason int slot; 3587d97e63b6SChris Mason int level = 1; 35885f39d397SChris Mason struct extent_buffer *c; 35895f39d397SChris Mason struct extent_buffer *next = NULL; 3590925baeddSChris Mason struct btrfs_key key; 3591925baeddSChris Mason u32 nritems; 3592925baeddSChris Mason int ret; 3593925baeddSChris Mason 3594925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 3595925baeddSChris Mason if (nritems == 0) { 3596925baeddSChris Mason return 1; 3597925baeddSChris Mason } 3598925baeddSChris Mason 3599925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 3600925baeddSChris Mason 3601925baeddSChris Mason btrfs_release_path(root, path); 3602a2135011SChris Mason path->keep_locks = 1; 3603925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 3604925baeddSChris Mason path->keep_locks = 0; 3605925baeddSChris Mason 3606925baeddSChris Mason if (ret < 0) 3607925baeddSChris Mason return ret; 3608925baeddSChris Mason 3609a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 3610168fd7d2SChris Mason /* 3611168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 3612168fd7d2SChris Mason * could have added more items next to the key that used to be 3613168fd7d2SChris Mason * at the very end of the block. So, check again here and 3614168fd7d2SChris Mason * advance the path if there are now more items available. 3615168fd7d2SChris Mason */ 3616a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 3617168fd7d2SChris Mason path->slots[0]++; 3618925baeddSChris Mason goto done; 3619925baeddSChris Mason } 3620d97e63b6SChris Mason 3621234b63a0SChris Mason while(level < BTRFS_MAX_LEVEL) { 3622d97e63b6SChris Mason if (!path->nodes[level]) 36230f70abe2SChris Mason return 1; 36245f39d397SChris Mason 3625d97e63b6SChris Mason slot = path->slots[level] + 1; 3626d97e63b6SChris Mason c = path->nodes[level]; 36275f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 3628d97e63b6SChris Mason level++; 3629925baeddSChris Mason if (level == BTRFS_MAX_LEVEL) { 36307bb86316SChris Mason return 1; 3631925baeddSChris Mason } 3632d97e63b6SChris Mason continue; 3633d97e63b6SChris Mason } 36345f39d397SChris Mason 3635925baeddSChris Mason if (next) { 3636925baeddSChris Mason btrfs_tree_unlock(next); 36375f39d397SChris Mason free_extent_buffer(next); 3638925baeddSChris Mason } 36395f39d397SChris Mason 36400bd40a71SChris Mason if (level == 1 && (path->locks[1] || path->skip_locking) && 36410bd40a71SChris Mason path->reada) 364201f46658SChris Mason reada_for_search(root, path, level, slot, 0); 36435f39d397SChris Mason 3644ca7a79adSChris Mason next = read_node_slot(root, c, slot); 36455cd57b2cSChris Mason if (!path->skip_locking) { 3646051e1b9fSChris Mason WARN_ON(!btrfs_tree_locked(c)); 3647925baeddSChris Mason btrfs_tree_lock(next); 36485cd57b2cSChris Mason } 3649d97e63b6SChris Mason break; 3650d97e63b6SChris Mason } 3651d97e63b6SChris Mason path->slots[level] = slot; 3652d97e63b6SChris Mason while(1) { 3653d97e63b6SChris Mason level--; 3654d97e63b6SChris Mason c = path->nodes[level]; 3655925baeddSChris Mason if (path->locks[level]) 3656925baeddSChris Mason btrfs_tree_unlock(c); 36575f39d397SChris Mason free_extent_buffer(c); 3658d97e63b6SChris Mason path->nodes[level] = next; 3659d97e63b6SChris Mason path->slots[level] = 0; 3660a74a4b97SChris Mason if (!path->skip_locking) 3661925baeddSChris Mason path->locks[level] = 1; 3662d97e63b6SChris Mason if (!level) 3663d97e63b6SChris Mason break; 3664925baeddSChris Mason if (level == 1 && path->locks[1] && path->reada) 3665925baeddSChris Mason reada_for_search(root, path, level, slot, 0); 3666ca7a79adSChris Mason next = read_node_slot(root, next, 0); 36675cd57b2cSChris Mason if (!path->skip_locking) { 3668a2135011SChris Mason WARN_ON(!btrfs_tree_locked(path->nodes[level])); 3669925baeddSChris Mason btrfs_tree_lock(next); 3670d97e63b6SChris Mason } 36715cd57b2cSChris Mason } 3672925baeddSChris Mason done: 3673925baeddSChris Mason unlock_up(path, 0, 1); 3674d97e63b6SChris Mason return 0; 3675d97e63b6SChris Mason } 36760b86a832SChris Mason 36773f157a2fSChris Mason /* 36783f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 36793f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 36803f157a2fSChris Mason * 36813f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 36823f157a2fSChris Mason */ 36830b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 36840b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 36850b86a832SChris Mason int type) 36860b86a832SChris Mason { 36870b86a832SChris Mason struct btrfs_key found_key; 36880b86a832SChris Mason struct extent_buffer *leaf; 3689e02119d5SChris Mason u32 nritems; 36900b86a832SChris Mason int ret; 36910b86a832SChris Mason 36920b86a832SChris Mason while(1) { 36930b86a832SChris Mason if (path->slots[0] == 0) { 36940b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 36950b86a832SChris Mason if (ret != 0) 36960b86a832SChris Mason return ret; 36970b86a832SChris Mason } else { 36980b86a832SChris Mason path->slots[0]--; 36990b86a832SChris Mason } 37000b86a832SChris Mason leaf = path->nodes[0]; 3701e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 3702e02119d5SChris Mason if (nritems == 0) 3703e02119d5SChris Mason return 1; 3704e02119d5SChris Mason if (path->slots[0] == nritems) 3705e02119d5SChris Mason path->slots[0]--; 3706e02119d5SChris Mason 37070b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 37080b86a832SChris Mason if (found_key.type == type) 37090b86a832SChris Mason return 0; 3710e02119d5SChris Mason if (found_key.objectid < min_objectid) 3711e02119d5SChris Mason break; 3712e02119d5SChris Mason if (found_key.objectid == min_objectid && 3713e02119d5SChris Mason found_key.type < type) 3714e02119d5SChris Mason break; 37150b86a832SChris Mason } 37160b86a832SChris Mason return 1; 37170b86a832SChris Mason } 3718