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> 205a0e3ad6STejun Heo #include <linux/slab.h> 21eb60ceacSChris Mason #include "ctree.h" 22eb60ceacSChris Mason #include "disk-io.h" 237f5c1516SChris Mason #include "transaction.h" 245f39d397SChris Mason #include "print-tree.h" 25925baeddSChris Mason #include "locking.h" 269a8dd150SChris Mason 27e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 28e089f05cSChris Mason *root, struct btrfs_path *path, int level); 29e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root 30d4dbff95SChris Mason *root, struct btrfs_key *ins_key, 31cc0c5538SChris Mason struct btrfs_path *path, int data_size, int extend); 325f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 335f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 34971a1f66SChris Mason struct extent_buffer *src, int empty); 355f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 365f39d397SChris Mason struct btrfs_root *root, 375f39d397SChris Mason struct extent_buffer *dst_buf, 385f39d397SChris Mason struct extent_buffer *src_buf); 39e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 40e089f05cSChris Mason struct btrfs_path *path, int level, int slot); 41d97e63b6SChris Mason 422c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 432c90e5d6SChris Mason { 44df24a2b9SChris Mason struct btrfs_path *path; 45e00f7308SJeff Mahoney path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 46df24a2b9SChris Mason return path; 472c90e5d6SChris Mason } 482c90e5d6SChris Mason 49b4ce94deSChris Mason /* 50b4ce94deSChris Mason * set all locked nodes in the path to blocking locks. This should 51b4ce94deSChris Mason * be done before scheduling 52b4ce94deSChris Mason */ 53b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p) 54b4ce94deSChris Mason { 55b4ce94deSChris Mason int i; 56b4ce94deSChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 57bd681513SChris Mason if (!p->nodes[i] || !p->locks[i]) 58bd681513SChris Mason continue; 59bd681513SChris Mason btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]); 60bd681513SChris Mason if (p->locks[i] == BTRFS_READ_LOCK) 61bd681513SChris Mason p->locks[i] = BTRFS_READ_LOCK_BLOCKING; 62bd681513SChris Mason else if (p->locks[i] == BTRFS_WRITE_LOCK) 63bd681513SChris Mason p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; 64b4ce94deSChris Mason } 65b4ce94deSChris Mason } 66b4ce94deSChris Mason 67b4ce94deSChris Mason /* 68b4ce94deSChris Mason * reset all the locked nodes in the patch to spinning locks. 694008c04aSChris Mason * 704008c04aSChris Mason * held is used to keep lockdep happy, when lockdep is enabled 714008c04aSChris Mason * we set held to a blocking lock before we go around and 724008c04aSChris Mason * retake all the spinlocks in the path. You can safely use NULL 734008c04aSChris Mason * for held 74b4ce94deSChris Mason */ 754008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p, 76bd681513SChris Mason struct extent_buffer *held, int held_rw) 77b4ce94deSChris Mason { 78b4ce94deSChris Mason int i; 794008c04aSChris Mason 804008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 814008c04aSChris Mason /* lockdep really cares that we take all of these spinlocks 824008c04aSChris Mason * in the right order. If any of the locks in the path are not 834008c04aSChris Mason * currently blocking, it is going to complain. So, make really 844008c04aSChris Mason * really sure by forcing the path to blocking before we clear 854008c04aSChris Mason * the path blocking. 864008c04aSChris Mason */ 87bd681513SChris Mason if (held) { 88bd681513SChris Mason btrfs_set_lock_blocking_rw(held, held_rw); 89bd681513SChris Mason if (held_rw == BTRFS_WRITE_LOCK) 90bd681513SChris Mason held_rw = BTRFS_WRITE_LOCK_BLOCKING; 91bd681513SChris Mason else if (held_rw == BTRFS_READ_LOCK) 92bd681513SChris Mason held_rw = BTRFS_READ_LOCK_BLOCKING; 93bd681513SChris Mason } 944008c04aSChris Mason btrfs_set_path_blocking(p); 954008c04aSChris Mason #endif 964008c04aSChris Mason 974008c04aSChris Mason for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) { 98bd681513SChris Mason if (p->nodes[i] && p->locks[i]) { 99bd681513SChris Mason btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]); 100bd681513SChris Mason if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING) 101bd681513SChris Mason p->locks[i] = BTRFS_WRITE_LOCK; 102bd681513SChris Mason else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING) 103bd681513SChris Mason p->locks[i] = BTRFS_READ_LOCK; 104bd681513SChris Mason } 105b4ce94deSChris Mason } 1064008c04aSChris Mason 1074008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1084008c04aSChris Mason if (held) 109bd681513SChris Mason btrfs_clear_lock_blocking_rw(held, held_rw); 1104008c04aSChris Mason #endif 111b4ce94deSChris Mason } 112b4ce94deSChris Mason 113d352ac68SChris Mason /* this also releases the path */ 1142c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 1152c90e5d6SChris Mason { 116ff175d57SJesper Juhl if (!p) 117ff175d57SJesper Juhl return; 118b3b4aa74SDavid Sterba btrfs_release_path(p); 1192c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 1202c90e5d6SChris Mason } 1212c90e5d6SChris Mason 122d352ac68SChris Mason /* 123d352ac68SChris Mason * path release drops references on the extent buffers in the path 124d352ac68SChris Mason * and it drops any locks held by this path 125d352ac68SChris Mason * 126d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 127d352ac68SChris Mason */ 128b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p) 129eb60ceacSChris Mason { 130eb60ceacSChris Mason int i; 131a2135011SChris Mason 132234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 1333f157a2fSChris Mason p->slots[i] = 0; 134eb60ceacSChris Mason if (!p->nodes[i]) 135925baeddSChris Mason continue; 136925baeddSChris Mason if (p->locks[i]) { 137bd681513SChris Mason btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); 138925baeddSChris Mason p->locks[i] = 0; 139925baeddSChris Mason } 1405f39d397SChris Mason free_extent_buffer(p->nodes[i]); 1413f157a2fSChris Mason p->nodes[i] = NULL; 142eb60ceacSChris Mason } 143eb60ceacSChris Mason } 144eb60ceacSChris Mason 145d352ac68SChris Mason /* 146d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 147d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 148d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 149d352ac68SChris Mason * looping required. 150d352ac68SChris Mason * 151d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 152d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 153d352ac68SChris Mason * at any time because there are no locks held. 154d352ac68SChris Mason */ 155925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 156925baeddSChris Mason { 157925baeddSChris Mason struct extent_buffer *eb; 158240f62c8SChris Mason 159240f62c8SChris Mason rcu_read_lock(); 160240f62c8SChris Mason eb = rcu_dereference(root->node); 161925baeddSChris Mason extent_buffer_get(eb); 162240f62c8SChris Mason rcu_read_unlock(); 163925baeddSChris Mason return eb; 164925baeddSChris Mason } 165925baeddSChris Mason 166d352ac68SChris Mason /* loop around taking references on and locking the root node of the 167d352ac68SChris Mason * tree until you end up with a lock on the root. A locked buffer 168d352ac68SChris Mason * is returned, with a reference held. 169d352ac68SChris Mason */ 170925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 171925baeddSChris Mason { 172925baeddSChris Mason struct extent_buffer *eb; 173925baeddSChris Mason 174925baeddSChris Mason while (1) { 175925baeddSChris Mason eb = btrfs_root_node(root); 176925baeddSChris Mason btrfs_tree_lock(eb); 177240f62c8SChris Mason if (eb == root->node) 178925baeddSChris Mason break; 179925baeddSChris Mason btrfs_tree_unlock(eb); 180925baeddSChris Mason free_extent_buffer(eb); 181925baeddSChris Mason } 182925baeddSChris Mason return eb; 183925baeddSChris Mason } 184925baeddSChris Mason 185bd681513SChris Mason /* loop around taking references on and locking the root node of the 186bd681513SChris Mason * tree until you end up with a lock on the root. A locked buffer 187bd681513SChris Mason * is returned, with a reference held. 188bd681513SChris Mason */ 189bd681513SChris Mason struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root) 190bd681513SChris Mason { 191bd681513SChris Mason struct extent_buffer *eb; 192bd681513SChris Mason 193bd681513SChris Mason while (1) { 194bd681513SChris Mason eb = btrfs_root_node(root); 195bd681513SChris Mason btrfs_tree_read_lock(eb); 196bd681513SChris Mason if (eb == root->node) 197bd681513SChris Mason break; 198bd681513SChris Mason btrfs_tree_read_unlock(eb); 199bd681513SChris Mason free_extent_buffer(eb); 200bd681513SChris Mason } 201bd681513SChris Mason return eb; 202bd681513SChris Mason } 203bd681513SChris Mason 204d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get 205d352ac68SChris Mason * put onto a simple dirty list. transaction.c walks this to make sure they 206d352ac68SChris Mason * get properly updated on disk. 207d352ac68SChris Mason */ 2080b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 2090b86a832SChris Mason { 2100b86a832SChris Mason if (root->track_dirty && list_empty(&root->dirty_list)) { 2110b86a832SChris Mason list_add(&root->dirty_list, 2120b86a832SChris Mason &root->fs_info->dirty_cowonly_roots); 2130b86a832SChris Mason } 2140b86a832SChris Mason } 2150b86a832SChris Mason 216d352ac68SChris Mason /* 217d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 218d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 219d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 220d352ac68SChris Mason */ 221be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 222be20aa9dSChris Mason struct btrfs_root *root, 223be20aa9dSChris Mason struct extent_buffer *buf, 224be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 225be20aa9dSChris Mason { 226be20aa9dSChris Mason struct extent_buffer *cow; 227be20aa9dSChris Mason int ret = 0; 228be20aa9dSChris Mason int level; 2295d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 230be20aa9dSChris Mason 231be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != 232be20aa9dSChris Mason root->fs_info->running_transaction->transid); 233be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 234be20aa9dSChris Mason 235be20aa9dSChris Mason level = btrfs_header_level(buf); 2365d4f98a2SYan Zheng if (level == 0) 2375d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 2385d4f98a2SYan Zheng else 2395d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 24031840ae1SZheng Yan 2415d4f98a2SYan Zheng cow = btrfs_alloc_free_block(trans, root, buf->len, 0, 2425d4f98a2SYan Zheng new_root_objectid, &disk_key, level, 2435d4f98a2SYan Zheng buf->start, 0); 2445d4f98a2SYan Zheng if (IS_ERR(cow)) 245be20aa9dSChris Mason return PTR_ERR(cow); 246be20aa9dSChris Mason 247be20aa9dSChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 248be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 249be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 2505d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 2515d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 2525d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 2535d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 2545d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 2555d4f98a2SYan Zheng else 256be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 257be20aa9dSChris Mason 2582b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 2592b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 2602b82032cSYan Zheng BTRFS_FSID_SIZE); 2612b82032cSYan Zheng 262be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 2635d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 2645d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 2655d4f98a2SYan Zheng else 2665d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 0); 2674aec2b52SChris Mason 268be20aa9dSChris Mason if (ret) 269be20aa9dSChris Mason return ret; 270be20aa9dSChris Mason 271be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 272be20aa9dSChris Mason *cow_ret = cow; 273be20aa9dSChris Mason return 0; 274be20aa9dSChris Mason } 275be20aa9dSChris Mason 276d352ac68SChris Mason /* 2775d4f98a2SYan Zheng * check if the tree block can be shared by multiple trees 2785d4f98a2SYan Zheng */ 2795d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root, 2805d4f98a2SYan Zheng struct extent_buffer *buf) 2815d4f98a2SYan Zheng { 2825d4f98a2SYan Zheng /* 2835d4f98a2SYan Zheng * Tree blocks not in refernece counted trees and tree roots 2845d4f98a2SYan Zheng * are never shared. If a block was allocated after the last 2855d4f98a2SYan Zheng * snapshot and the block was not allocated by tree relocation, 2865d4f98a2SYan Zheng * we know the block is not shared. 2875d4f98a2SYan Zheng */ 2885d4f98a2SYan Zheng if (root->ref_cows && 2895d4f98a2SYan Zheng buf != root->node && buf != root->commit_root && 2905d4f98a2SYan Zheng (btrfs_header_generation(buf) <= 2915d4f98a2SYan Zheng btrfs_root_last_snapshot(&root->root_item) || 2925d4f98a2SYan Zheng btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) 2935d4f98a2SYan Zheng return 1; 2945d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 2955d4f98a2SYan Zheng if (root->ref_cows && 2965d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 2975d4f98a2SYan Zheng return 1; 2985d4f98a2SYan Zheng #endif 2995d4f98a2SYan Zheng return 0; 3005d4f98a2SYan Zheng } 3015d4f98a2SYan Zheng 3025d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, 3035d4f98a2SYan Zheng struct btrfs_root *root, 3045d4f98a2SYan Zheng struct extent_buffer *buf, 305f0486c68SYan, Zheng struct extent_buffer *cow, 306f0486c68SYan, Zheng int *last_ref) 3075d4f98a2SYan Zheng { 3085d4f98a2SYan Zheng u64 refs; 3095d4f98a2SYan Zheng u64 owner; 3105d4f98a2SYan Zheng u64 flags; 3115d4f98a2SYan Zheng u64 new_flags = 0; 3125d4f98a2SYan Zheng int ret; 3135d4f98a2SYan Zheng 3145d4f98a2SYan Zheng /* 3155d4f98a2SYan Zheng * Backrefs update rules: 3165d4f98a2SYan Zheng * 3175d4f98a2SYan Zheng * Always use full backrefs for extent pointers in tree block 3185d4f98a2SYan Zheng * allocated by tree relocation. 3195d4f98a2SYan Zheng * 3205d4f98a2SYan Zheng * If a shared tree block is no longer referenced by its owner 3215d4f98a2SYan Zheng * tree (btrfs_header_owner(buf) == root->root_key.objectid), 3225d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 3235d4f98a2SYan Zheng * 3245d4f98a2SYan Zheng * If a tree block is been relocating 3255d4f98a2SYan Zheng * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), 3265d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 3275d4f98a2SYan Zheng * The reason for this is some operations (such as drop tree) 3285d4f98a2SYan Zheng * are only allowed for blocks use full backrefs. 3295d4f98a2SYan Zheng */ 3305d4f98a2SYan Zheng 3315d4f98a2SYan Zheng if (btrfs_block_can_be_shared(root, buf)) { 3325d4f98a2SYan Zheng ret = btrfs_lookup_extent_info(trans, root, buf->start, 3335d4f98a2SYan Zheng buf->len, &refs, &flags); 3345d4f98a2SYan Zheng BUG_ON(ret); 3355d4f98a2SYan Zheng BUG_ON(refs == 0); 3365d4f98a2SYan Zheng } else { 3375d4f98a2SYan Zheng refs = 1; 3385d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 3395d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 3405d4f98a2SYan Zheng flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; 3415d4f98a2SYan Zheng else 3425d4f98a2SYan Zheng flags = 0; 3435d4f98a2SYan Zheng } 3445d4f98a2SYan Zheng 3455d4f98a2SYan Zheng owner = btrfs_header_owner(buf); 3465d4f98a2SYan Zheng BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && 3475d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); 3485d4f98a2SYan Zheng 3495d4f98a2SYan Zheng if (refs > 1) { 3505d4f98a2SYan Zheng if ((owner == root->root_key.objectid || 3515d4f98a2SYan Zheng root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && 3525d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 3535d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, buf, 1); 3545d4f98a2SYan Zheng BUG_ON(ret); 3555d4f98a2SYan Zheng 3565d4f98a2SYan Zheng if (root->root_key.objectid == 3575d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 3585d4f98a2SYan Zheng ret = btrfs_dec_ref(trans, root, buf, 0); 3595d4f98a2SYan Zheng BUG_ON(ret); 3605d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 3615d4f98a2SYan Zheng BUG_ON(ret); 3625d4f98a2SYan Zheng } 3635d4f98a2SYan Zheng new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; 3645d4f98a2SYan Zheng } else { 3655d4f98a2SYan Zheng 3665d4f98a2SYan Zheng if (root->root_key.objectid == 3675d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 3685d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 3695d4f98a2SYan Zheng else 3705d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 0); 3715d4f98a2SYan Zheng BUG_ON(ret); 3725d4f98a2SYan Zheng } 3735d4f98a2SYan Zheng if (new_flags != 0) { 3745d4f98a2SYan Zheng ret = btrfs_set_disk_extent_flags(trans, root, 3755d4f98a2SYan Zheng buf->start, 3765d4f98a2SYan Zheng buf->len, 3775d4f98a2SYan Zheng new_flags, 0); 3785d4f98a2SYan Zheng BUG_ON(ret); 3795d4f98a2SYan Zheng } 3805d4f98a2SYan Zheng } else { 3815d4f98a2SYan Zheng if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { 3825d4f98a2SYan Zheng if (root->root_key.objectid == 3835d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 3845d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 3855d4f98a2SYan Zheng else 3865d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 0); 3875d4f98a2SYan Zheng BUG_ON(ret); 3885d4f98a2SYan Zheng ret = btrfs_dec_ref(trans, root, buf, 1); 3895d4f98a2SYan Zheng BUG_ON(ret); 3905d4f98a2SYan Zheng } 3915d4f98a2SYan Zheng clean_tree_block(trans, root, buf); 392f0486c68SYan, Zheng *last_ref = 1; 3935d4f98a2SYan Zheng } 3945d4f98a2SYan Zheng return 0; 3955d4f98a2SYan Zheng } 3965d4f98a2SYan Zheng 3975d4f98a2SYan Zheng /* 398d397712bSChris Mason * does the dirty work in cow of a single block. The parent block (if 399d397712bSChris Mason * supplied) is updated to point to the new cow copy. The new buffer is marked 400d397712bSChris Mason * dirty and returned locked. If you modify the block it needs to be marked 401d397712bSChris Mason * dirty again. 402d352ac68SChris Mason * 403d352ac68SChris Mason * search_start -- an allocation hint for the new block 404d352ac68SChris Mason * 405d397712bSChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in 406d397712bSChris Mason * bytes the allocator should try to find free next to the block it returns. 407d397712bSChris Mason * This is just a hint and may be ignored by the allocator. 408d352ac68SChris Mason */ 409d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, 4105f39d397SChris Mason struct btrfs_root *root, 4115f39d397SChris Mason struct extent_buffer *buf, 4125f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 4135f39d397SChris Mason struct extent_buffer **cow_ret, 4149fa8cfe7SChris Mason u64 search_start, u64 empty_size) 4156702ed49SChris Mason { 4165d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 4175f39d397SChris Mason struct extent_buffer *cow; 4187bb86316SChris Mason int level; 419f0486c68SYan, Zheng int last_ref = 0; 420925baeddSChris Mason int unlock_orig = 0; 4215d4f98a2SYan Zheng u64 parent_start; 4226702ed49SChris Mason 423925baeddSChris Mason if (*cow_ret == buf) 424925baeddSChris Mason unlock_orig = 1; 425925baeddSChris Mason 426b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 427925baeddSChris Mason 4287bb86316SChris Mason WARN_ON(root->ref_cows && trans->transid != 4297bb86316SChris Mason root->fs_info->running_transaction->transid); 4306702ed49SChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 4315f39d397SChris Mason 4327bb86316SChris Mason level = btrfs_header_level(buf); 43331840ae1SZheng Yan 4345d4f98a2SYan Zheng if (level == 0) 4355d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 4365d4f98a2SYan Zheng else 4375d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 4385d4f98a2SYan Zheng 4395d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { 4405d4f98a2SYan Zheng if (parent) 4415d4f98a2SYan Zheng parent_start = parent->start; 4425d4f98a2SYan Zheng else 4435d4f98a2SYan Zheng parent_start = 0; 4445d4f98a2SYan Zheng } else 4455d4f98a2SYan Zheng parent_start = 0; 4465d4f98a2SYan Zheng 4475d4f98a2SYan Zheng cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start, 4485d4f98a2SYan Zheng root->root_key.objectid, &disk_key, 4495d4f98a2SYan Zheng level, search_start, empty_size); 4506702ed49SChris Mason if (IS_ERR(cow)) 4516702ed49SChris Mason return PTR_ERR(cow); 4526702ed49SChris Mason 453b4ce94deSChris Mason /* cow is set to blocking by btrfs_init_new_buffer */ 454b4ce94deSChris Mason 4555f39d397SChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 456db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 4575f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 4585d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 4595d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 4605d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 4615d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 4625d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 4635d4f98a2SYan Zheng else 4645f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 4656702ed49SChris Mason 4662b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 4672b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 4682b82032cSYan Zheng BTRFS_FSID_SIZE); 4692b82032cSYan Zheng 470f0486c68SYan, Zheng update_ref_for_cow(trans, root, buf, cow, &last_ref); 4711a40e23bSZheng Yan 4723fd0a558SYan, Zheng if (root->ref_cows) 4733fd0a558SYan, Zheng btrfs_reloc_cow_block(trans, root, buf, cow); 4743fd0a558SYan, Zheng 4756702ed49SChris Mason if (buf == root->node) { 476925baeddSChris Mason WARN_ON(parent && parent != buf); 4775d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 4785d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 4795d4f98a2SYan Zheng parent_start = buf->start; 4805d4f98a2SYan Zheng else 4815d4f98a2SYan Zheng parent_start = 0; 482925baeddSChris Mason 4835f39d397SChris Mason extent_buffer_get(cow); 484240f62c8SChris Mason rcu_assign_pointer(root->node, cow); 485925baeddSChris Mason 486f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 487f0486c68SYan, Zheng last_ref); 4885f39d397SChris Mason free_extent_buffer(buf); 4890b86a832SChris Mason add_root_to_dirty_list(root); 4906702ed49SChris Mason } else { 4915d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 4925d4f98a2SYan Zheng parent_start = parent->start; 4935d4f98a2SYan Zheng else 4945d4f98a2SYan Zheng parent_start = 0; 4955d4f98a2SYan Zheng 4965d4f98a2SYan Zheng WARN_ON(trans->transid != btrfs_header_generation(parent)); 4975f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 498db94535dSChris Mason cow->start); 49974493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 50074493f7aSChris Mason trans->transid); 5016702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 502f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 503f0486c68SYan, Zheng last_ref); 5046702ed49SChris Mason } 505925baeddSChris Mason if (unlock_orig) 506925baeddSChris Mason btrfs_tree_unlock(buf); 5075f39d397SChris Mason free_extent_buffer(buf); 5086702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 5096702ed49SChris Mason *cow_ret = cow; 5106702ed49SChris Mason return 0; 5116702ed49SChris Mason } 5126702ed49SChris Mason 5135d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans, 5145d4f98a2SYan Zheng struct btrfs_root *root, 5155d4f98a2SYan Zheng struct extent_buffer *buf) 5165d4f98a2SYan Zheng { 517*f1ebcc74SLiu Bo /* ensure we can see the force_cow */ 518*f1ebcc74SLiu Bo smp_rmb(); 519*f1ebcc74SLiu Bo 520*f1ebcc74SLiu Bo /* 521*f1ebcc74SLiu Bo * We do not need to cow a block if 522*f1ebcc74SLiu Bo * 1) this block is not created or changed in this transaction; 523*f1ebcc74SLiu Bo * 2) this block does not belong to TREE_RELOC tree; 524*f1ebcc74SLiu Bo * 3) the root is not forced COW. 525*f1ebcc74SLiu Bo * 526*f1ebcc74SLiu Bo * What is forced COW: 527*f1ebcc74SLiu Bo * when we create snapshot during commiting the transaction, 528*f1ebcc74SLiu Bo * after we've finished coping src root, we must COW the shared 529*f1ebcc74SLiu Bo * block to ensure the metadata consistency. 530*f1ebcc74SLiu Bo */ 5315d4f98a2SYan Zheng if (btrfs_header_generation(buf) == trans->transid && 5325d4f98a2SYan Zheng !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && 5335d4f98a2SYan Zheng !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 534*f1ebcc74SLiu Bo btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && 535*f1ebcc74SLiu Bo !root->force_cow) 5365d4f98a2SYan Zheng return 0; 5375d4f98a2SYan Zheng return 1; 5385d4f98a2SYan Zheng } 5395d4f98a2SYan Zheng 540d352ac68SChris Mason /* 541d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 542d352ac68SChris Mason * This version of it has extra checks so that a block isn't cow'd more than 543d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 544d352ac68SChris Mason */ 545d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, 5465f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 5475f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 5489fa8cfe7SChris Mason struct extent_buffer **cow_ret) 54902217ed2SChris Mason { 5506702ed49SChris Mason u64 search_start; 551f510cfecSChris Mason int ret; 552dc17ff8fSChris Mason 553ccd467d6SChris Mason if (trans->transaction != root->fs_info->running_transaction) { 554d397712bSChris Mason printk(KERN_CRIT "trans %llu running %llu\n", 555d397712bSChris Mason (unsigned long long)trans->transid, 556d397712bSChris Mason (unsigned long long) 557ccd467d6SChris Mason root->fs_info->running_transaction->transid); 558ccd467d6SChris Mason WARN_ON(1); 559ccd467d6SChris Mason } 560ccd467d6SChris Mason if (trans->transid != root->fs_info->generation) { 561d397712bSChris Mason printk(KERN_CRIT "trans %llu running %llu\n", 562d397712bSChris Mason (unsigned long long)trans->transid, 563d397712bSChris Mason (unsigned long long)root->fs_info->generation); 564ccd467d6SChris Mason WARN_ON(1); 565ccd467d6SChris Mason } 566dc17ff8fSChris Mason 5675d4f98a2SYan Zheng if (!should_cow_block(trans, root, buf)) { 56802217ed2SChris Mason *cow_ret = buf; 56902217ed2SChris Mason return 0; 57002217ed2SChris Mason } 571c487685dSChris Mason 5720b86a832SChris Mason search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 573b4ce94deSChris Mason 574b4ce94deSChris Mason if (parent) 575b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 576b4ce94deSChris Mason btrfs_set_lock_blocking(buf); 577b4ce94deSChris Mason 578f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 5799fa8cfe7SChris Mason parent_slot, cow_ret, search_start, 0); 5801abe9b8aSliubo 5811abe9b8aSliubo trace_btrfs_cow_block(root, buf, *cow_ret); 5821abe9b8aSliubo 583f510cfecSChris Mason return ret; 5842c90e5d6SChris Mason } 5856702ed49SChris Mason 586d352ac68SChris Mason /* 587d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 588d352ac68SChris Mason * node are actually close by 589d352ac68SChris Mason */ 5906b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 5916702ed49SChris Mason { 5926b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 5936702ed49SChris Mason return 1; 5946b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 5956702ed49SChris Mason return 1; 59602217ed2SChris Mason return 0; 59702217ed2SChris Mason } 59802217ed2SChris Mason 599081e9573SChris Mason /* 600081e9573SChris Mason * compare two keys in a memcmp fashion 601081e9573SChris Mason */ 602081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) 603081e9573SChris Mason { 604081e9573SChris Mason struct btrfs_key k1; 605081e9573SChris Mason 606081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 607081e9573SChris Mason 60820736abaSDiego Calleja return btrfs_comp_cpu_keys(&k1, k2); 609081e9573SChris Mason } 610081e9573SChris Mason 611f3465ca4SJosef Bacik /* 612f3465ca4SJosef Bacik * same as comp_keys only with two btrfs_key's 613f3465ca4SJosef Bacik */ 6145d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2) 615f3465ca4SJosef Bacik { 616f3465ca4SJosef Bacik if (k1->objectid > k2->objectid) 617f3465ca4SJosef Bacik return 1; 618f3465ca4SJosef Bacik if (k1->objectid < k2->objectid) 619f3465ca4SJosef Bacik return -1; 620f3465ca4SJosef Bacik if (k1->type > k2->type) 621f3465ca4SJosef Bacik return 1; 622f3465ca4SJosef Bacik if (k1->type < k2->type) 623f3465ca4SJosef Bacik return -1; 624f3465ca4SJosef Bacik if (k1->offset > k2->offset) 625f3465ca4SJosef Bacik return 1; 626f3465ca4SJosef Bacik if (k1->offset < k2->offset) 627f3465ca4SJosef Bacik return -1; 628f3465ca4SJosef Bacik return 0; 629f3465ca4SJosef Bacik } 630081e9573SChris Mason 631d352ac68SChris Mason /* 632d352ac68SChris Mason * this is used by the defrag code to go through all the 633d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 634d352ac68SChris Mason * disk order is close to key order 635d352ac68SChris Mason */ 6366702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 6375f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 638a6b6e75eSChris Mason int start_slot, int cache_only, u64 *last_ret, 639a6b6e75eSChris Mason struct btrfs_key *progress) 6406702ed49SChris Mason { 6416b80053dSChris Mason struct extent_buffer *cur; 6426702ed49SChris Mason u64 blocknr; 643ca7a79adSChris Mason u64 gen; 644e9d0b13bSChris Mason u64 search_start = *last_ret; 645e9d0b13bSChris Mason u64 last_block = 0; 6466702ed49SChris Mason u64 other; 6476702ed49SChris Mason u32 parent_nritems; 6486702ed49SChris Mason int end_slot; 6496702ed49SChris Mason int i; 6506702ed49SChris Mason int err = 0; 651f2183bdeSChris Mason int parent_level; 6526b80053dSChris Mason int uptodate; 6536b80053dSChris Mason u32 blocksize; 654081e9573SChris Mason int progress_passed = 0; 655081e9573SChris Mason struct btrfs_disk_key disk_key; 6566702ed49SChris Mason 6575708b959SChris Mason parent_level = btrfs_header_level(parent); 6585708b959SChris Mason if (cache_only && parent_level != 1) 6595708b959SChris Mason return 0; 6605708b959SChris Mason 661d397712bSChris Mason if (trans->transaction != root->fs_info->running_transaction) 6626702ed49SChris Mason WARN_ON(1); 663d397712bSChris Mason if (trans->transid != root->fs_info->generation) 6646702ed49SChris Mason WARN_ON(1); 66586479a04SChris Mason 6666b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 6676b80053dSChris Mason blocksize = btrfs_level_size(root, parent_level - 1); 6686702ed49SChris Mason end_slot = parent_nritems; 6696702ed49SChris Mason 6706702ed49SChris Mason if (parent_nritems == 1) 6716702ed49SChris Mason return 0; 6726702ed49SChris Mason 673b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 674b4ce94deSChris Mason 6756702ed49SChris Mason for (i = start_slot; i < end_slot; i++) { 6766702ed49SChris Mason int close = 1; 677a6b6e75eSChris Mason 678081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 679081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 680081e9573SChris Mason continue; 681081e9573SChris Mason 682081e9573SChris Mason progress_passed = 1; 6836b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 684ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 685e9d0b13bSChris Mason if (last_block == 0) 686e9d0b13bSChris Mason last_block = blocknr; 6875708b959SChris Mason 6886702ed49SChris Mason if (i > 0) { 6896b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 6906b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 6916702ed49SChris Mason } 6920ef3e66bSChris Mason if (!close && i < end_slot - 2) { 6936b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 6946b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 6956702ed49SChris Mason } 696e9d0b13bSChris Mason if (close) { 697e9d0b13bSChris Mason last_block = blocknr; 6986702ed49SChris Mason continue; 699e9d0b13bSChris Mason } 7006702ed49SChris Mason 7016b80053dSChris Mason cur = btrfs_find_tree_block(root, blocknr, blocksize); 7026b80053dSChris Mason if (cur) 7031259ab75SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen); 7046b80053dSChris Mason else 7056b80053dSChris Mason uptodate = 0; 7065708b959SChris Mason if (!cur || !uptodate) { 7076702ed49SChris Mason if (cache_only) { 7086b80053dSChris Mason free_extent_buffer(cur); 7096702ed49SChris Mason continue; 7106702ed49SChris Mason } 7116b80053dSChris Mason if (!cur) { 7126b80053dSChris Mason cur = read_tree_block(root, blocknr, 713ca7a79adSChris Mason blocksize, gen); 71497d9a8a4STsutomu Itoh if (!cur) 71597d9a8a4STsutomu Itoh return -EIO; 7166b80053dSChris Mason } else if (!uptodate) { 717ca7a79adSChris Mason btrfs_read_buffer(cur, gen); 7186702ed49SChris Mason } 719f2183bdeSChris Mason } 720e9d0b13bSChris Mason if (search_start == 0) 7216b80053dSChris Mason search_start = last_block; 722e9d0b13bSChris Mason 723e7a84565SChris Mason btrfs_tree_lock(cur); 724b4ce94deSChris Mason btrfs_set_lock_blocking(cur); 7256b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 726e7a84565SChris Mason &cur, search_start, 7276b80053dSChris Mason min(16 * blocksize, 7289fa8cfe7SChris Mason (end_slot - i) * blocksize)); 729252c38f0SYan if (err) { 730e7a84565SChris Mason btrfs_tree_unlock(cur); 7316b80053dSChris Mason free_extent_buffer(cur); 7326702ed49SChris Mason break; 733252c38f0SYan } 734e7a84565SChris Mason search_start = cur->start; 735e7a84565SChris Mason last_block = cur->start; 736f2183bdeSChris Mason *last_ret = search_start; 737e7a84565SChris Mason btrfs_tree_unlock(cur); 738e7a84565SChris Mason free_extent_buffer(cur); 7396702ed49SChris Mason } 7406702ed49SChris Mason return err; 7416702ed49SChris Mason } 7426702ed49SChris Mason 74374123bd7SChris Mason /* 74474123bd7SChris Mason * The leaf data grows from end-to-front in the node. 74574123bd7SChris Mason * this returns the address of the start of the last item, 74674123bd7SChris Mason * which is the stop of the leaf data stack 74774123bd7SChris Mason */ 748123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root, 7495f39d397SChris Mason struct extent_buffer *leaf) 750be0e5c09SChris Mason { 7515f39d397SChris Mason u32 nr = btrfs_header_nritems(leaf); 752be0e5c09SChris Mason if (nr == 0) 753123abc88SChris Mason return BTRFS_LEAF_DATA_SIZE(root); 7545f39d397SChris Mason return btrfs_item_offset_nr(leaf, nr - 1); 755be0e5c09SChris Mason } 756be0e5c09SChris Mason 757aa5d6bedSChris Mason 75874123bd7SChris Mason /* 7595f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 7605f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 7615f39d397SChris Mason * 76274123bd7SChris Mason * the slot in the array is returned via slot, and it points to 76374123bd7SChris Mason * the place where you would insert key if it is not found in 76474123bd7SChris Mason * the array. 76574123bd7SChris Mason * 76674123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 76774123bd7SChris Mason */ 768e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb, 769e02119d5SChris Mason unsigned long p, 7705f39d397SChris Mason int item_size, struct btrfs_key *key, 771be0e5c09SChris Mason int max, int *slot) 772be0e5c09SChris Mason { 773be0e5c09SChris Mason int low = 0; 774be0e5c09SChris Mason int high = max; 775be0e5c09SChris Mason int mid; 776be0e5c09SChris Mason int ret; 777479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 7785f39d397SChris Mason struct btrfs_disk_key unaligned; 7795f39d397SChris Mason unsigned long offset; 7805f39d397SChris Mason char *kaddr = NULL; 7815f39d397SChris Mason unsigned long map_start = 0; 7825f39d397SChris Mason unsigned long map_len = 0; 783479965d6SChris Mason int err; 784be0e5c09SChris Mason 785be0e5c09SChris Mason while (low < high) { 786be0e5c09SChris Mason mid = (low + high) / 2; 7875f39d397SChris Mason offset = p + mid * item_size; 7885f39d397SChris Mason 789a6591715SChris Mason if (!kaddr || offset < map_start || 7905f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 7915f39d397SChris Mason map_start + map_len) { 792934d375bSChris Mason 793934d375bSChris Mason err = map_private_extent_buffer(eb, offset, 794479965d6SChris Mason sizeof(struct btrfs_disk_key), 795a6591715SChris Mason &kaddr, &map_start, &map_len); 7965f39d397SChris Mason 797479965d6SChris Mason if (!err) { 798479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 799479965d6SChris Mason map_start); 800479965d6SChris Mason } else { 8015f39d397SChris Mason read_extent_buffer(eb, &unaligned, 8025f39d397SChris Mason offset, sizeof(unaligned)); 8035f39d397SChris Mason tmp = &unaligned; 804479965d6SChris Mason } 805479965d6SChris Mason 8065f39d397SChris Mason } else { 8075f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 8085f39d397SChris Mason map_start); 8095f39d397SChris Mason } 810be0e5c09SChris Mason ret = comp_keys(tmp, key); 811be0e5c09SChris Mason 812be0e5c09SChris Mason if (ret < 0) 813be0e5c09SChris Mason low = mid + 1; 814be0e5c09SChris Mason else if (ret > 0) 815be0e5c09SChris Mason high = mid; 816be0e5c09SChris Mason else { 817be0e5c09SChris Mason *slot = mid; 818be0e5c09SChris Mason return 0; 819be0e5c09SChris Mason } 820be0e5c09SChris Mason } 821be0e5c09SChris Mason *slot = low; 822be0e5c09SChris Mason return 1; 823be0e5c09SChris Mason } 824be0e5c09SChris Mason 82597571fd0SChris Mason /* 82697571fd0SChris Mason * simple bin_search frontend that does the right thing for 82797571fd0SChris Mason * leaves vs nodes 82897571fd0SChris Mason */ 8295f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, 8305f39d397SChris Mason int level, int *slot) 831be0e5c09SChris Mason { 8325f39d397SChris Mason if (level == 0) { 8335f39d397SChris Mason return generic_bin_search(eb, 8345f39d397SChris Mason offsetof(struct btrfs_leaf, items), 8350783fcfcSChris Mason sizeof(struct btrfs_item), 8365f39d397SChris Mason key, btrfs_header_nritems(eb), 8377518a238SChris Mason slot); 838be0e5c09SChris Mason } else { 8395f39d397SChris Mason return generic_bin_search(eb, 8405f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 841123abc88SChris Mason sizeof(struct btrfs_key_ptr), 8425f39d397SChris Mason key, btrfs_header_nritems(eb), 8437518a238SChris Mason slot); 844be0e5c09SChris Mason } 845be0e5c09SChris Mason return -1; 846be0e5c09SChris Mason } 847be0e5c09SChris Mason 8485d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, 8495d4f98a2SYan Zheng int level, int *slot) 8505d4f98a2SYan Zheng { 8515d4f98a2SYan Zheng return bin_search(eb, key, level, slot); 8525d4f98a2SYan Zheng } 8535d4f98a2SYan Zheng 854f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size) 855f0486c68SYan, Zheng { 856f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 857f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 858f0486c68SYan, Zheng btrfs_root_used(&root->root_item) + size); 859f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 860f0486c68SYan, Zheng } 861f0486c68SYan, Zheng 862f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size) 863f0486c68SYan, Zheng { 864f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 865f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 866f0486c68SYan, Zheng btrfs_root_used(&root->root_item) - size); 867f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 868f0486c68SYan, Zheng } 869f0486c68SYan, Zheng 870d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 871d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 872d352ac68SChris Mason * NULL is returned on error. 873d352ac68SChris Mason */ 874e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, 8755f39d397SChris Mason struct extent_buffer *parent, int slot) 876bb803951SChris Mason { 877ca7a79adSChris Mason int level = btrfs_header_level(parent); 878bb803951SChris Mason if (slot < 0) 879bb803951SChris Mason return NULL; 8805f39d397SChris Mason if (slot >= btrfs_header_nritems(parent)) 881bb803951SChris Mason return NULL; 882ca7a79adSChris Mason 883ca7a79adSChris Mason BUG_ON(level == 0); 884ca7a79adSChris Mason 885db94535dSChris Mason return read_tree_block(root, btrfs_node_blockptr(parent, slot), 886ca7a79adSChris Mason btrfs_level_size(root, level - 1), 887ca7a79adSChris Mason btrfs_node_ptr_generation(parent, slot)); 888bb803951SChris Mason } 889bb803951SChris Mason 890d352ac68SChris Mason /* 891d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 892d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 893d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 894d352ac68SChris Mason */ 895e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 89698ed5174SChris Mason struct btrfs_root *root, 89798ed5174SChris Mason struct btrfs_path *path, int level) 898bb803951SChris Mason { 8995f39d397SChris Mason struct extent_buffer *right = NULL; 9005f39d397SChris Mason struct extent_buffer *mid; 9015f39d397SChris Mason struct extent_buffer *left = NULL; 9025f39d397SChris Mason struct extent_buffer *parent = NULL; 903bb803951SChris Mason int ret = 0; 904bb803951SChris Mason int wret; 905bb803951SChris Mason int pslot; 906bb803951SChris Mason int orig_slot = path->slots[level]; 90779f95c82SChris Mason u64 orig_ptr; 908bb803951SChris Mason 909bb803951SChris Mason if (level == 0) 910bb803951SChris Mason return 0; 911bb803951SChris Mason 9125f39d397SChris Mason mid = path->nodes[level]; 913b4ce94deSChris Mason 914bd681513SChris Mason WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && 915bd681513SChris Mason path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); 9167bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 9177bb86316SChris Mason 9181d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 91979f95c82SChris Mason 920a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 9215f39d397SChris Mason parent = path->nodes[level + 1]; 922bb803951SChris Mason pslot = path->slots[level + 1]; 923a05a9bb1SLi Zefan } 924bb803951SChris Mason 92540689478SChris Mason /* 92640689478SChris Mason * deal with the case where there is only one pointer in the root 92740689478SChris Mason * by promoting the node below to a root 92840689478SChris Mason */ 9295f39d397SChris Mason if (!parent) { 9305f39d397SChris Mason struct extent_buffer *child; 931bb803951SChris Mason 9325f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 933bb803951SChris Mason return 0; 934bb803951SChris Mason 935bb803951SChris Mason /* promote the child to a root */ 9365f39d397SChris Mason child = read_node_slot(root, mid, 0); 9377951f3ceSJeff Mahoney BUG_ON(!child); 938925baeddSChris Mason btrfs_tree_lock(child); 939b4ce94deSChris Mason btrfs_set_lock_blocking(child); 9409fa8cfe7SChris Mason ret = btrfs_cow_block(trans, root, child, mid, 0, &child); 941f0486c68SYan, Zheng if (ret) { 942f0486c68SYan, Zheng btrfs_tree_unlock(child); 943f0486c68SYan, Zheng free_extent_buffer(child); 944f0486c68SYan, Zheng goto enospc; 945f0486c68SYan, Zheng } 9462f375ab9SYan 947240f62c8SChris Mason rcu_assign_pointer(root->node, child); 948925baeddSChris Mason 9490b86a832SChris Mason add_root_to_dirty_list(root); 950925baeddSChris Mason btrfs_tree_unlock(child); 951b4ce94deSChris Mason 952925baeddSChris Mason path->locks[level] = 0; 953bb803951SChris Mason path->nodes[level] = NULL; 9545f39d397SChris Mason clean_tree_block(trans, root, mid); 955925baeddSChris Mason btrfs_tree_unlock(mid); 956bb803951SChris Mason /* once for the path */ 9575f39d397SChris Mason free_extent_buffer(mid); 958f0486c68SYan, Zheng 959f0486c68SYan, Zheng root_sub_used(root, mid->len); 960f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, mid, 0, 1); 961bb803951SChris Mason /* once for the root ptr */ 9625f39d397SChris Mason free_extent_buffer(mid); 963f0486c68SYan, Zheng return 0; 964bb803951SChris Mason } 9655f39d397SChris Mason if (btrfs_header_nritems(mid) > 966123abc88SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 4) 967bb803951SChris Mason return 0; 968bb803951SChris Mason 969559af821SAndi Kleen btrfs_header_nritems(mid); 97054aa1f4dSChris Mason 9715f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 9725f39d397SChris Mason if (left) { 973925baeddSChris Mason btrfs_tree_lock(left); 974b4ce94deSChris Mason btrfs_set_lock_blocking(left); 9755f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 9769fa8cfe7SChris Mason parent, pslot - 1, &left); 97754aa1f4dSChris Mason if (wret) { 97854aa1f4dSChris Mason ret = wret; 97954aa1f4dSChris Mason goto enospc; 98054aa1f4dSChris Mason } 9812cc58cf2SChris Mason } 9825f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 9835f39d397SChris Mason if (right) { 984925baeddSChris Mason btrfs_tree_lock(right); 985b4ce94deSChris Mason btrfs_set_lock_blocking(right); 9865f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 9879fa8cfe7SChris Mason parent, pslot + 1, &right); 9882cc58cf2SChris Mason if (wret) { 9892cc58cf2SChris Mason ret = wret; 9902cc58cf2SChris Mason goto enospc; 9912cc58cf2SChris Mason } 9922cc58cf2SChris Mason } 9932cc58cf2SChris Mason 9942cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 9955f39d397SChris Mason if (left) { 9965f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 997bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 99879f95c82SChris Mason if (wret < 0) 99979f95c82SChris Mason ret = wret; 1000559af821SAndi Kleen btrfs_header_nritems(mid); 1001bb803951SChris Mason } 100279f95c82SChris Mason 100379f95c82SChris Mason /* 100479f95c82SChris Mason * then try to empty the right most buffer into the middle 100579f95c82SChris Mason */ 10065f39d397SChris Mason if (right) { 1007971a1f66SChris Mason wret = push_node_left(trans, root, mid, right, 1); 100854aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 100979f95c82SChris Mason ret = wret; 10105f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 10115f39d397SChris Mason clean_tree_block(trans, root, right); 1012925baeddSChris Mason btrfs_tree_unlock(right); 1013e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot + 1014e089f05cSChris Mason 1); 1015bb803951SChris Mason if (wret) 1016bb803951SChris Mason ret = wret; 1017f0486c68SYan, Zheng root_sub_used(root, right->len); 1018f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, right, 0, 1); 1019f0486c68SYan, Zheng free_extent_buffer(right); 1020f0486c68SYan, Zheng right = NULL; 1021bb803951SChris Mason } else { 10225f39d397SChris Mason struct btrfs_disk_key right_key; 10235f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 10245f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 10255f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1026bb803951SChris Mason } 1027bb803951SChris Mason } 10285f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 102979f95c82SChris Mason /* 103079f95c82SChris Mason * we're not allowed to leave a node with one item in the 103179f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 103279f95c82SChris Mason * could try to delete the only pointer in this node. 103379f95c82SChris Mason * So, pull some keys from the left. 103479f95c82SChris Mason * There has to be a left pointer at this point because 103579f95c82SChris Mason * otherwise we would have pulled some pointers from the 103679f95c82SChris Mason * right 103779f95c82SChris Mason */ 10385f39d397SChris Mason BUG_ON(!left); 10395f39d397SChris Mason wret = balance_node_right(trans, root, mid, left); 104054aa1f4dSChris Mason if (wret < 0) { 104179f95c82SChris Mason ret = wret; 104254aa1f4dSChris Mason goto enospc; 104354aa1f4dSChris Mason } 1044bce4eae9SChris Mason if (wret == 1) { 1045bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 1046bce4eae9SChris Mason if (wret < 0) 1047bce4eae9SChris Mason ret = wret; 1048bce4eae9SChris Mason } 104979f95c82SChris Mason BUG_ON(wret == 1); 105079f95c82SChris Mason } 10515f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 10525f39d397SChris Mason clean_tree_block(trans, root, mid); 1053925baeddSChris Mason btrfs_tree_unlock(mid); 1054e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot); 1055bb803951SChris Mason if (wret) 1056bb803951SChris Mason ret = wret; 1057f0486c68SYan, Zheng root_sub_used(root, mid->len); 1058f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, mid, 0, 1); 1059f0486c68SYan, Zheng free_extent_buffer(mid); 1060f0486c68SYan, Zheng mid = NULL; 106179f95c82SChris Mason } else { 106279f95c82SChris Mason /* update the parent key to reflect our changes */ 10635f39d397SChris Mason struct btrfs_disk_key mid_key; 10645f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 10655f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 10665f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 106779f95c82SChris Mason } 1068bb803951SChris Mason 106979f95c82SChris Mason /* update the path */ 10705f39d397SChris Mason if (left) { 10715f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 10725f39d397SChris Mason extent_buffer_get(left); 1073925baeddSChris Mason /* left was locked after cow */ 10745f39d397SChris Mason path->nodes[level] = left; 1075bb803951SChris Mason path->slots[level + 1] -= 1; 1076bb803951SChris Mason path->slots[level] = orig_slot; 1077925baeddSChris Mason if (mid) { 1078925baeddSChris Mason btrfs_tree_unlock(mid); 10795f39d397SChris Mason free_extent_buffer(mid); 1080925baeddSChris Mason } 1081bb803951SChris Mason } else { 10825f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 1083bb803951SChris Mason path->slots[level] = orig_slot; 1084bb803951SChris Mason } 1085bb803951SChris Mason } 108679f95c82SChris Mason /* double check we haven't messed things up */ 1087e20d96d6SChris Mason if (orig_ptr != 10885f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 108979f95c82SChris Mason BUG(); 109054aa1f4dSChris Mason enospc: 1091925baeddSChris Mason if (right) { 1092925baeddSChris Mason btrfs_tree_unlock(right); 10935f39d397SChris Mason free_extent_buffer(right); 1094925baeddSChris Mason } 1095925baeddSChris Mason if (left) { 1096925baeddSChris Mason if (path->nodes[level] != left) 1097925baeddSChris Mason btrfs_tree_unlock(left); 10985f39d397SChris Mason free_extent_buffer(left); 1099925baeddSChris Mason } 1100bb803951SChris Mason return ret; 1101bb803951SChris Mason } 1102bb803951SChris Mason 1103d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 1104d352ac68SChris Mason * when they are completely full. This is also done top down, so we 1105d352ac68SChris Mason * have to be pessimistic. 1106d352ac68SChris Mason */ 1107d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, 1108e66f709bSChris Mason struct btrfs_root *root, 1109e66f709bSChris Mason struct btrfs_path *path, int level) 1110e66f709bSChris Mason { 11115f39d397SChris Mason struct extent_buffer *right = NULL; 11125f39d397SChris Mason struct extent_buffer *mid; 11135f39d397SChris Mason struct extent_buffer *left = NULL; 11145f39d397SChris Mason struct extent_buffer *parent = NULL; 1115e66f709bSChris Mason int ret = 0; 1116e66f709bSChris Mason int wret; 1117e66f709bSChris Mason int pslot; 1118e66f709bSChris Mason int orig_slot = path->slots[level]; 1119e66f709bSChris Mason 1120e66f709bSChris Mason if (level == 0) 1121e66f709bSChris Mason return 1; 1122e66f709bSChris Mason 11235f39d397SChris Mason mid = path->nodes[level]; 11247bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 1125e66f709bSChris Mason 1126a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 11275f39d397SChris Mason parent = path->nodes[level + 1]; 1128e66f709bSChris Mason pslot = path->slots[level + 1]; 1129a05a9bb1SLi Zefan } 1130e66f709bSChris Mason 11315f39d397SChris Mason if (!parent) 1132e66f709bSChris Mason return 1; 1133e66f709bSChris Mason 11345f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 1135e66f709bSChris Mason 1136e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 11375f39d397SChris Mason if (left) { 1138e66f709bSChris Mason u32 left_nr; 1139925baeddSChris Mason 1140925baeddSChris Mason btrfs_tree_lock(left); 1141b4ce94deSChris Mason btrfs_set_lock_blocking(left); 1142b4ce94deSChris Mason 11435f39d397SChris Mason left_nr = btrfs_header_nritems(left); 114433ade1f8SChris Mason if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 114533ade1f8SChris Mason wret = 1; 114633ade1f8SChris Mason } else { 11475f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 11489fa8cfe7SChris Mason pslot - 1, &left); 114954aa1f4dSChris Mason if (ret) 115054aa1f4dSChris Mason wret = 1; 115154aa1f4dSChris Mason else { 115254aa1f4dSChris Mason wret = push_node_left(trans, root, 1153971a1f66SChris Mason left, mid, 0); 115454aa1f4dSChris Mason } 115533ade1f8SChris Mason } 1156e66f709bSChris Mason if (wret < 0) 1157e66f709bSChris Mason ret = wret; 1158e66f709bSChris Mason if (wret == 0) { 11595f39d397SChris Mason struct btrfs_disk_key disk_key; 1160e66f709bSChris Mason orig_slot += left_nr; 11615f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 11625f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 11635f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 11645f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 11655f39d397SChris Mason path->nodes[level] = left; 1166e66f709bSChris Mason path->slots[level + 1] -= 1; 1167e66f709bSChris Mason path->slots[level] = orig_slot; 1168925baeddSChris Mason btrfs_tree_unlock(mid); 11695f39d397SChris Mason free_extent_buffer(mid); 1170e66f709bSChris Mason } else { 1171e66f709bSChris Mason orig_slot -= 11725f39d397SChris Mason btrfs_header_nritems(left); 1173e66f709bSChris Mason path->slots[level] = orig_slot; 1174925baeddSChris Mason btrfs_tree_unlock(left); 11755f39d397SChris Mason free_extent_buffer(left); 1176e66f709bSChris Mason } 1177e66f709bSChris Mason return 0; 1178e66f709bSChris Mason } 1179925baeddSChris Mason btrfs_tree_unlock(left); 11805f39d397SChris Mason free_extent_buffer(left); 1181e66f709bSChris Mason } 11825f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 1183e66f709bSChris Mason 1184e66f709bSChris Mason /* 1185e66f709bSChris Mason * then try to empty the right most buffer into the middle 1186e66f709bSChris Mason */ 11875f39d397SChris Mason if (right) { 118833ade1f8SChris Mason u32 right_nr; 1189b4ce94deSChris Mason 1190925baeddSChris Mason btrfs_tree_lock(right); 1191b4ce94deSChris Mason btrfs_set_lock_blocking(right); 1192b4ce94deSChris Mason 11935f39d397SChris Mason right_nr = btrfs_header_nritems(right); 119433ade1f8SChris Mason if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 119533ade1f8SChris Mason wret = 1; 119633ade1f8SChris Mason } else { 11975f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 11985f39d397SChris Mason parent, pslot + 1, 11999fa8cfe7SChris Mason &right); 120054aa1f4dSChris Mason if (ret) 120154aa1f4dSChris Mason wret = 1; 120254aa1f4dSChris Mason else { 120333ade1f8SChris Mason wret = balance_node_right(trans, root, 12045f39d397SChris Mason right, mid); 120533ade1f8SChris Mason } 120654aa1f4dSChris Mason } 1207e66f709bSChris Mason if (wret < 0) 1208e66f709bSChris Mason ret = wret; 1209e66f709bSChris Mason if (wret == 0) { 12105f39d397SChris Mason struct btrfs_disk_key disk_key; 12115f39d397SChris Mason 12125f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 12135f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 12145f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 12155f39d397SChris Mason 12165f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 12175f39d397SChris Mason path->nodes[level] = right; 1218e66f709bSChris Mason path->slots[level + 1] += 1; 1219e66f709bSChris Mason path->slots[level] = orig_slot - 12205f39d397SChris Mason btrfs_header_nritems(mid); 1221925baeddSChris Mason btrfs_tree_unlock(mid); 12225f39d397SChris Mason free_extent_buffer(mid); 1223e66f709bSChris Mason } else { 1224925baeddSChris Mason btrfs_tree_unlock(right); 12255f39d397SChris Mason free_extent_buffer(right); 1226e66f709bSChris Mason } 1227e66f709bSChris Mason return 0; 1228e66f709bSChris Mason } 1229925baeddSChris Mason btrfs_tree_unlock(right); 12305f39d397SChris Mason free_extent_buffer(right); 1231e66f709bSChris Mason } 1232e66f709bSChris Mason return 1; 1233e66f709bSChris Mason } 1234e66f709bSChris Mason 123574123bd7SChris Mason /* 1236d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 1237d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 12383c69faecSChris Mason */ 1239c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root, 1240e02119d5SChris Mason struct btrfs_path *path, 124101f46658SChris Mason int level, int slot, u64 objectid) 12423c69faecSChris Mason { 12435f39d397SChris Mason struct extent_buffer *node; 124401f46658SChris Mason struct btrfs_disk_key disk_key; 12453c69faecSChris Mason u32 nritems; 12463c69faecSChris Mason u64 search; 1247a7175319SChris Mason u64 target; 12486b80053dSChris Mason u64 nread = 0; 1249cb25c2eaSJosef Bacik u64 gen; 12503c69faecSChris Mason int direction = path->reada; 12515f39d397SChris Mason struct extent_buffer *eb; 12526b80053dSChris Mason u32 nr; 12536b80053dSChris Mason u32 blocksize; 12546b80053dSChris Mason u32 nscan = 0; 1255db94535dSChris Mason 1256a6b6e75eSChris Mason if (level != 1) 12573c69faecSChris Mason return; 12583c69faecSChris Mason 12596702ed49SChris Mason if (!path->nodes[level]) 12606702ed49SChris Mason return; 12616702ed49SChris Mason 12625f39d397SChris Mason node = path->nodes[level]; 1263925baeddSChris Mason 12643c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 12656b80053dSChris Mason blocksize = btrfs_level_size(root, level - 1); 12666b80053dSChris Mason eb = btrfs_find_tree_block(root, search, blocksize); 12675f39d397SChris Mason if (eb) { 12685f39d397SChris Mason free_extent_buffer(eb); 12693c69faecSChris Mason return; 12703c69faecSChris Mason } 12713c69faecSChris Mason 1272a7175319SChris Mason target = search; 12736b80053dSChris Mason 12745f39d397SChris Mason nritems = btrfs_header_nritems(node); 12756b80053dSChris Mason nr = slot; 127625b8b936SJosef Bacik 12773c69faecSChris Mason while (1) { 12786b80053dSChris Mason if (direction < 0) { 12796b80053dSChris Mason if (nr == 0) 12803c69faecSChris Mason break; 12816b80053dSChris Mason nr--; 12826b80053dSChris Mason } else if (direction > 0) { 12836b80053dSChris Mason nr++; 12846b80053dSChris Mason if (nr >= nritems) 12856b80053dSChris Mason break; 12863c69faecSChris Mason } 128701f46658SChris Mason if (path->reada < 0 && objectid) { 128801f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 128901f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 129001f46658SChris Mason break; 129101f46658SChris Mason } 12926b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 1293a7175319SChris Mason if ((search <= target && target - search <= 65536) || 1294a7175319SChris Mason (search > target && search - target <= 65536)) { 1295cb25c2eaSJosef Bacik gen = btrfs_node_ptr_generation(node, nr); 1296cb25c2eaSJosef Bacik readahead_tree_block(root, search, blocksize, gen); 12976b80053dSChris Mason nread += blocksize; 12983c69faecSChris Mason } 12996b80053dSChris Mason nscan++; 1300a7175319SChris Mason if ((nread > 65536 || nscan > 32)) 13016b80053dSChris Mason break; 13023c69faecSChris Mason } 13033c69faecSChris Mason } 1304925baeddSChris Mason 1305d352ac68SChris Mason /* 1306b4ce94deSChris Mason * returns -EAGAIN if it had to drop the path, or zero if everything was in 1307b4ce94deSChris Mason * cache 1308b4ce94deSChris Mason */ 1309b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root, 1310b4ce94deSChris Mason struct btrfs_path *path, int level) 1311b4ce94deSChris Mason { 1312b4ce94deSChris Mason int slot; 1313b4ce94deSChris Mason int nritems; 1314b4ce94deSChris Mason struct extent_buffer *parent; 1315b4ce94deSChris Mason struct extent_buffer *eb; 1316b4ce94deSChris Mason u64 gen; 1317b4ce94deSChris Mason u64 block1 = 0; 1318b4ce94deSChris Mason u64 block2 = 0; 1319b4ce94deSChris Mason int ret = 0; 1320b4ce94deSChris Mason int blocksize; 1321b4ce94deSChris Mason 13228c594ea8SChris Mason parent = path->nodes[level + 1]; 1323b4ce94deSChris Mason if (!parent) 1324b4ce94deSChris Mason return 0; 1325b4ce94deSChris Mason 1326b4ce94deSChris Mason nritems = btrfs_header_nritems(parent); 13278c594ea8SChris Mason slot = path->slots[level + 1]; 1328b4ce94deSChris Mason blocksize = btrfs_level_size(root, level); 1329b4ce94deSChris Mason 1330b4ce94deSChris Mason if (slot > 0) { 1331b4ce94deSChris Mason block1 = btrfs_node_blockptr(parent, slot - 1); 1332b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot - 1); 1333b4ce94deSChris Mason eb = btrfs_find_tree_block(root, block1, blocksize); 1334b4ce94deSChris Mason if (eb && btrfs_buffer_uptodate(eb, gen)) 1335b4ce94deSChris Mason block1 = 0; 1336b4ce94deSChris Mason free_extent_buffer(eb); 1337b4ce94deSChris Mason } 13388c594ea8SChris Mason if (slot + 1 < nritems) { 1339b4ce94deSChris Mason block2 = btrfs_node_blockptr(parent, slot + 1); 1340b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot + 1); 1341b4ce94deSChris Mason eb = btrfs_find_tree_block(root, block2, blocksize); 1342b4ce94deSChris Mason if (eb && btrfs_buffer_uptodate(eb, gen)) 1343b4ce94deSChris Mason block2 = 0; 1344b4ce94deSChris Mason free_extent_buffer(eb); 1345b4ce94deSChris Mason } 1346b4ce94deSChris Mason if (block1 || block2) { 1347b4ce94deSChris Mason ret = -EAGAIN; 13488c594ea8SChris Mason 13498c594ea8SChris Mason /* release the whole path */ 1350b3b4aa74SDavid Sterba btrfs_release_path(path); 13518c594ea8SChris Mason 13528c594ea8SChris Mason /* read the blocks */ 1353b4ce94deSChris Mason if (block1) 1354b4ce94deSChris Mason readahead_tree_block(root, block1, blocksize, 0); 1355b4ce94deSChris Mason if (block2) 1356b4ce94deSChris Mason readahead_tree_block(root, block2, blocksize, 0); 1357b4ce94deSChris Mason 1358b4ce94deSChris Mason if (block1) { 1359b4ce94deSChris Mason eb = read_tree_block(root, block1, blocksize, 0); 1360b4ce94deSChris Mason free_extent_buffer(eb); 1361b4ce94deSChris Mason } 13628c594ea8SChris Mason if (block2) { 1363b4ce94deSChris Mason eb = read_tree_block(root, block2, blocksize, 0); 1364b4ce94deSChris Mason free_extent_buffer(eb); 1365b4ce94deSChris Mason } 1366b4ce94deSChris Mason } 1367b4ce94deSChris Mason return ret; 1368b4ce94deSChris Mason } 1369b4ce94deSChris Mason 1370b4ce94deSChris Mason 1371b4ce94deSChris Mason /* 1372d397712bSChris Mason * when we walk down the tree, it is usually safe to unlock the higher layers 1373d397712bSChris Mason * in the tree. The exceptions are when our path goes through slot 0, because 1374d397712bSChris Mason * operations on the tree might require changing key pointers higher up in the 1375d397712bSChris Mason * tree. 1376d352ac68SChris Mason * 1377d397712bSChris Mason * callers might also have set path->keep_locks, which tells this code to keep 1378d397712bSChris Mason * the lock if the path points to the last slot in the block. This is part of 1379d397712bSChris Mason * walking through the tree, and selecting the next slot in the higher block. 1380d352ac68SChris Mason * 1381d397712bSChris Mason * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so 1382d397712bSChris Mason * if lowest_unlock is 1, level 0 won't be unlocked 1383d352ac68SChris Mason */ 1384e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level, 1385e02119d5SChris Mason int lowest_unlock) 1386925baeddSChris Mason { 1387925baeddSChris Mason int i; 1388925baeddSChris Mason int skip_level = level; 1389051e1b9fSChris Mason int no_skips = 0; 1390925baeddSChris Mason struct extent_buffer *t; 1391925baeddSChris Mason 1392925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1393925baeddSChris Mason if (!path->nodes[i]) 1394925baeddSChris Mason break; 1395925baeddSChris Mason if (!path->locks[i]) 1396925baeddSChris Mason break; 1397051e1b9fSChris Mason if (!no_skips && path->slots[i] == 0) { 1398925baeddSChris Mason skip_level = i + 1; 1399925baeddSChris Mason continue; 1400925baeddSChris Mason } 1401051e1b9fSChris Mason if (!no_skips && path->keep_locks) { 1402925baeddSChris Mason u32 nritems; 1403925baeddSChris Mason t = path->nodes[i]; 1404925baeddSChris Mason nritems = btrfs_header_nritems(t); 1405051e1b9fSChris Mason if (nritems < 1 || path->slots[i] >= nritems - 1) { 1406925baeddSChris Mason skip_level = i + 1; 1407925baeddSChris Mason continue; 1408925baeddSChris Mason } 1409925baeddSChris Mason } 1410051e1b9fSChris Mason if (skip_level < i && i >= lowest_unlock) 1411051e1b9fSChris Mason no_skips = 1; 1412051e1b9fSChris Mason 1413925baeddSChris Mason t = path->nodes[i]; 1414925baeddSChris Mason if (i >= lowest_unlock && i > skip_level && path->locks[i]) { 1415bd681513SChris Mason btrfs_tree_unlock_rw(t, path->locks[i]); 1416925baeddSChris Mason path->locks[i] = 0; 1417925baeddSChris Mason } 1418925baeddSChris Mason } 1419925baeddSChris Mason } 1420925baeddSChris Mason 14213c69faecSChris Mason /* 1422b4ce94deSChris Mason * This releases any locks held in the path starting at level and 1423b4ce94deSChris Mason * going all the way up to the root. 1424b4ce94deSChris Mason * 1425b4ce94deSChris Mason * btrfs_search_slot will keep the lock held on higher nodes in a few 1426b4ce94deSChris Mason * corner cases, such as COW of the block at slot zero in the node. This 1427b4ce94deSChris Mason * ignores those rules, and it should only be called when there are no 1428b4ce94deSChris Mason * more updates to be done higher up in the tree. 1429b4ce94deSChris Mason */ 1430b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) 1431b4ce94deSChris Mason { 1432b4ce94deSChris Mason int i; 1433b4ce94deSChris Mason 14345d4f98a2SYan Zheng if (path->keep_locks) 1435b4ce94deSChris Mason return; 1436b4ce94deSChris Mason 1437b4ce94deSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1438b4ce94deSChris Mason if (!path->nodes[i]) 143912f4daccSChris Mason continue; 1440b4ce94deSChris Mason if (!path->locks[i]) 144112f4daccSChris Mason continue; 1442bd681513SChris Mason btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); 1443b4ce94deSChris Mason path->locks[i] = 0; 1444b4ce94deSChris Mason } 1445b4ce94deSChris Mason } 1446b4ce94deSChris Mason 1447b4ce94deSChris Mason /* 1448c8c42864SChris Mason * helper function for btrfs_search_slot. The goal is to find a block 1449c8c42864SChris Mason * in cache without setting the path to blocking. If we find the block 1450c8c42864SChris Mason * we return zero and the path is unchanged. 1451c8c42864SChris Mason * 1452c8c42864SChris Mason * If we can't find the block, we set the path blocking and do some 1453c8c42864SChris Mason * reada. -EAGAIN is returned and the search must be repeated. 1454c8c42864SChris Mason */ 1455c8c42864SChris Mason static int 1456c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans, 1457c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 1458c8c42864SChris Mason struct extent_buffer **eb_ret, int level, int slot, 1459c8c42864SChris Mason struct btrfs_key *key) 1460c8c42864SChris Mason { 1461c8c42864SChris Mason u64 blocknr; 1462c8c42864SChris Mason u64 gen; 1463c8c42864SChris Mason u32 blocksize; 1464c8c42864SChris Mason struct extent_buffer *b = *eb_ret; 1465c8c42864SChris Mason struct extent_buffer *tmp; 146676a05b35SChris Mason int ret; 1467c8c42864SChris Mason 1468c8c42864SChris Mason blocknr = btrfs_node_blockptr(b, slot); 1469c8c42864SChris Mason gen = btrfs_node_ptr_generation(b, slot); 1470c8c42864SChris Mason blocksize = btrfs_level_size(root, level - 1); 1471c8c42864SChris Mason 1472c8c42864SChris Mason tmp = btrfs_find_tree_block(root, blocknr, blocksize); 1473cb44921aSChris Mason if (tmp) { 1474cb44921aSChris Mason if (btrfs_buffer_uptodate(tmp, 0)) { 1475cb44921aSChris Mason if (btrfs_buffer_uptodate(tmp, gen)) { 147676a05b35SChris Mason /* 1477cb44921aSChris Mason * we found an up to date block without 1478cb44921aSChris Mason * sleeping, return 147976a05b35SChris Mason * right away 148076a05b35SChris Mason */ 1481c8c42864SChris Mason *eb_ret = tmp; 1482c8c42864SChris Mason return 0; 1483c8c42864SChris Mason } 1484cb44921aSChris Mason /* the pages were up to date, but we failed 1485cb44921aSChris Mason * the generation number check. Do a full 1486cb44921aSChris Mason * read for the generation number that is correct. 1487cb44921aSChris Mason * We must do this without dropping locks so 1488cb44921aSChris Mason * we can trust our generation number 1489cb44921aSChris Mason */ 1490cb44921aSChris Mason free_extent_buffer(tmp); 1491bd681513SChris Mason btrfs_set_path_blocking(p); 1492bd681513SChris Mason 1493cb44921aSChris Mason tmp = read_tree_block(root, blocknr, blocksize, gen); 1494cb44921aSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 1495cb44921aSChris Mason *eb_ret = tmp; 1496cb44921aSChris Mason return 0; 1497cb44921aSChris Mason } 1498cb44921aSChris Mason free_extent_buffer(tmp); 1499b3b4aa74SDavid Sterba btrfs_release_path(p); 1500cb44921aSChris Mason return -EIO; 1501cb44921aSChris Mason } 1502cb44921aSChris Mason } 1503c8c42864SChris Mason 1504c8c42864SChris Mason /* 1505c8c42864SChris Mason * reduce lock contention at high levels 1506c8c42864SChris Mason * of the btree by dropping locks before 150776a05b35SChris Mason * we read. Don't release the lock on the current 150876a05b35SChris Mason * level because we need to walk this node to figure 150976a05b35SChris Mason * out which blocks to read. 1510c8c42864SChris Mason */ 15118c594ea8SChris Mason btrfs_unlock_up_safe(p, level + 1); 15128c594ea8SChris Mason btrfs_set_path_blocking(p); 15138c594ea8SChris Mason 1514c8c42864SChris Mason free_extent_buffer(tmp); 1515c8c42864SChris Mason if (p->reada) 1516c8c42864SChris Mason reada_for_search(root, p, level, slot, key->objectid); 1517c8c42864SChris Mason 1518b3b4aa74SDavid Sterba btrfs_release_path(p); 151976a05b35SChris Mason 152076a05b35SChris Mason ret = -EAGAIN; 15215bdd3536SYan, Zheng tmp = read_tree_block(root, blocknr, blocksize, 0); 152276a05b35SChris Mason if (tmp) { 152376a05b35SChris Mason /* 152476a05b35SChris Mason * If the read above didn't mark this buffer up to date, 152576a05b35SChris Mason * it will never end up being up to date. Set ret to EIO now 152676a05b35SChris Mason * and give up so that our caller doesn't loop forever 152776a05b35SChris Mason * on our EAGAINs. 152876a05b35SChris Mason */ 152976a05b35SChris Mason if (!btrfs_buffer_uptodate(tmp, 0)) 153076a05b35SChris Mason ret = -EIO; 1531c8c42864SChris Mason free_extent_buffer(tmp); 153276a05b35SChris Mason } 153376a05b35SChris Mason return ret; 1534c8c42864SChris Mason } 1535c8c42864SChris Mason 1536c8c42864SChris Mason /* 1537c8c42864SChris Mason * helper function for btrfs_search_slot. This does all of the checks 1538c8c42864SChris Mason * for node-level blocks and does any balancing required based on 1539c8c42864SChris Mason * the ins_len. 1540c8c42864SChris Mason * 1541c8c42864SChris Mason * If no extra work was required, zero is returned. If we had to 1542c8c42864SChris Mason * drop the path, -EAGAIN is returned and btrfs_search_slot must 1543c8c42864SChris Mason * start over 1544c8c42864SChris Mason */ 1545c8c42864SChris Mason static int 1546c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans, 1547c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 1548bd681513SChris Mason struct extent_buffer *b, int level, int ins_len, 1549bd681513SChris Mason int *write_lock_level) 1550c8c42864SChris Mason { 1551c8c42864SChris Mason int ret; 1552c8c42864SChris Mason if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= 1553c8c42864SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 1554c8c42864SChris Mason int sret; 1555c8c42864SChris Mason 1556bd681513SChris Mason if (*write_lock_level < level + 1) { 1557bd681513SChris Mason *write_lock_level = level + 1; 1558bd681513SChris Mason btrfs_release_path(p); 1559bd681513SChris Mason goto again; 1560bd681513SChris Mason } 1561bd681513SChris Mason 1562c8c42864SChris Mason sret = reada_for_balance(root, p, level); 1563c8c42864SChris Mason if (sret) 1564c8c42864SChris Mason goto again; 1565c8c42864SChris Mason 1566c8c42864SChris Mason btrfs_set_path_blocking(p); 1567c8c42864SChris Mason sret = split_node(trans, root, p, level); 1568bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 1569c8c42864SChris Mason 1570c8c42864SChris Mason BUG_ON(sret > 0); 1571c8c42864SChris Mason if (sret) { 1572c8c42864SChris Mason ret = sret; 1573c8c42864SChris Mason goto done; 1574c8c42864SChris Mason } 1575c8c42864SChris Mason b = p->nodes[level]; 1576c8c42864SChris Mason } else if (ins_len < 0 && btrfs_header_nritems(b) < 1577cfbb9308SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { 1578c8c42864SChris Mason int sret; 1579c8c42864SChris Mason 1580bd681513SChris Mason if (*write_lock_level < level + 1) { 1581bd681513SChris Mason *write_lock_level = level + 1; 1582bd681513SChris Mason btrfs_release_path(p); 1583bd681513SChris Mason goto again; 1584bd681513SChris Mason } 1585bd681513SChris Mason 1586c8c42864SChris Mason sret = reada_for_balance(root, p, level); 1587c8c42864SChris Mason if (sret) 1588c8c42864SChris Mason goto again; 1589c8c42864SChris Mason 1590c8c42864SChris Mason btrfs_set_path_blocking(p); 1591c8c42864SChris Mason sret = balance_level(trans, root, p, level); 1592bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 1593c8c42864SChris Mason 1594c8c42864SChris Mason if (sret) { 1595c8c42864SChris Mason ret = sret; 1596c8c42864SChris Mason goto done; 1597c8c42864SChris Mason } 1598c8c42864SChris Mason b = p->nodes[level]; 1599c8c42864SChris Mason if (!b) { 1600b3b4aa74SDavid Sterba btrfs_release_path(p); 1601c8c42864SChris Mason goto again; 1602c8c42864SChris Mason } 1603c8c42864SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 1604c8c42864SChris Mason } 1605c8c42864SChris Mason return 0; 1606c8c42864SChris Mason 1607c8c42864SChris Mason again: 1608c8c42864SChris Mason ret = -EAGAIN; 1609c8c42864SChris Mason done: 1610c8c42864SChris Mason return ret; 1611c8c42864SChris Mason } 1612c8c42864SChris Mason 1613c8c42864SChris Mason /* 161474123bd7SChris Mason * look for key in the tree. path is filled in with nodes along the way 161574123bd7SChris Mason * if key is found, we return zero and you can find the item in the leaf 161674123bd7SChris Mason * level of the path (level 0) 161774123bd7SChris Mason * 161874123bd7SChris Mason * If the key isn't found, the path points to the slot where it should 1619aa5d6bedSChris Mason * be inserted, and 1 is returned. If there are other errors during the 1620aa5d6bedSChris Mason * search a negative error number is returned. 162197571fd0SChris Mason * 162297571fd0SChris Mason * if ins_len > 0, nodes and leaves will be split as we walk down the 162397571fd0SChris Mason * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if 162497571fd0SChris Mason * possible) 162574123bd7SChris Mason */ 1626e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root 1627e089f05cSChris Mason *root, struct btrfs_key *key, struct btrfs_path *p, int 1628e089f05cSChris Mason ins_len, int cow) 1629be0e5c09SChris Mason { 16305f39d397SChris Mason struct extent_buffer *b; 1631be0e5c09SChris Mason int slot; 1632be0e5c09SChris Mason int ret; 163333c66f43SYan Zheng int err; 1634be0e5c09SChris Mason int level; 1635925baeddSChris Mason int lowest_unlock = 1; 1636bd681513SChris Mason int root_lock; 1637bd681513SChris Mason /* everything at write_lock_level or lower must be write locked */ 1638bd681513SChris Mason int write_lock_level = 0; 16399f3a7427SChris Mason u8 lowest_level = 0; 16409f3a7427SChris Mason 16416702ed49SChris Mason lowest_level = p->lowest_level; 1642323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 164322b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 164425179201SJosef Bacik 1645bd681513SChris Mason if (ins_len < 0) { 1646925baeddSChris Mason lowest_unlock = 2; 164765b51a00SChris Mason 1648bd681513SChris Mason /* when we are removing items, we might have to go up to level 1649bd681513SChris Mason * two as we update tree pointers Make sure we keep write 1650bd681513SChris Mason * for those levels as well 1651bd681513SChris Mason */ 1652bd681513SChris Mason write_lock_level = 2; 1653bd681513SChris Mason } else if (ins_len > 0) { 1654bd681513SChris Mason /* 1655bd681513SChris Mason * for inserting items, make sure we have a write lock on 1656bd681513SChris Mason * level 1 so we can update keys 1657bd681513SChris Mason */ 1658bd681513SChris Mason write_lock_level = 1; 1659bd681513SChris Mason } 1660bd681513SChris Mason 1661bd681513SChris Mason if (!cow) 1662bd681513SChris Mason write_lock_level = -1; 1663bd681513SChris Mason 1664bd681513SChris Mason if (cow && (p->keep_locks || p->lowest_level)) 1665bd681513SChris Mason write_lock_level = BTRFS_MAX_LEVEL; 1666bd681513SChris Mason 1667bb803951SChris Mason again: 1668bd681513SChris Mason /* 1669bd681513SChris Mason * we try very hard to do read locks on the root 1670bd681513SChris Mason */ 1671bd681513SChris Mason root_lock = BTRFS_READ_LOCK; 1672bd681513SChris Mason level = 0; 16735d4f98a2SYan Zheng if (p->search_commit_root) { 1674bd681513SChris Mason /* 1675bd681513SChris Mason * the commit roots are read only 1676bd681513SChris Mason * so we always do read locks 1677bd681513SChris Mason */ 16785d4f98a2SYan Zheng b = root->commit_root; 16795d4f98a2SYan Zheng extent_buffer_get(b); 1680bd681513SChris Mason level = btrfs_header_level(b); 16815d4f98a2SYan Zheng if (!p->skip_locking) 1682bd681513SChris Mason btrfs_tree_read_lock(b); 16835d4f98a2SYan Zheng } else { 1684bd681513SChris Mason if (p->skip_locking) { 16855cd57b2cSChris Mason b = btrfs_root_node(root); 1686bd681513SChris Mason level = btrfs_header_level(b); 1687bd681513SChris Mason } else { 1688bd681513SChris Mason /* we don't know the level of the root node 1689bd681513SChris Mason * until we actually have it read locked 1690bd681513SChris Mason */ 1691bd681513SChris Mason b = btrfs_read_lock_root_node(root); 1692bd681513SChris Mason level = btrfs_header_level(b); 1693bd681513SChris Mason if (level <= write_lock_level) { 1694bd681513SChris Mason /* whoops, must trade for write lock */ 1695bd681513SChris Mason btrfs_tree_read_unlock(b); 1696bd681513SChris Mason free_extent_buffer(b); 1697925baeddSChris Mason b = btrfs_lock_root_node(root); 1698bd681513SChris Mason root_lock = BTRFS_WRITE_LOCK; 1699bd681513SChris Mason 1700bd681513SChris Mason /* the level might have changed, check again */ 1701bd681513SChris Mason level = btrfs_header_level(b); 17025d4f98a2SYan Zheng } 1703bd681513SChris Mason } 1704bd681513SChris Mason } 1705bd681513SChris Mason p->nodes[level] = b; 1706bd681513SChris Mason if (!p->skip_locking) 1707bd681513SChris Mason p->locks[level] = root_lock; 1708925baeddSChris Mason 1709eb60ceacSChris Mason while (b) { 17105f39d397SChris Mason level = btrfs_header_level(b); 171165b51a00SChris Mason 171265b51a00SChris Mason /* 171365b51a00SChris Mason * setup the path here so we can release it under lock 171465b51a00SChris Mason * contention with the cow code 171565b51a00SChris Mason */ 171602217ed2SChris Mason if (cow) { 1717c8c42864SChris Mason /* 1718c8c42864SChris Mason * if we don't really need to cow this block 1719c8c42864SChris Mason * then we don't want to set the path blocking, 1720c8c42864SChris Mason * so we test it here 1721c8c42864SChris Mason */ 17225d4f98a2SYan Zheng if (!should_cow_block(trans, root, b)) 172365b51a00SChris Mason goto cow_done; 17245d4f98a2SYan Zheng 1725b4ce94deSChris Mason btrfs_set_path_blocking(p); 1726b4ce94deSChris Mason 1727bd681513SChris Mason /* 1728bd681513SChris Mason * must have write locks on this node and the 1729bd681513SChris Mason * parent 1730bd681513SChris Mason */ 1731bd681513SChris Mason if (level + 1 > write_lock_level) { 1732bd681513SChris Mason write_lock_level = level + 1; 1733bd681513SChris Mason btrfs_release_path(p); 1734bd681513SChris Mason goto again; 1735bd681513SChris Mason } 1736bd681513SChris Mason 173733c66f43SYan Zheng err = btrfs_cow_block(trans, root, b, 1738e20d96d6SChris Mason p->nodes[level + 1], 17399fa8cfe7SChris Mason p->slots[level + 1], &b); 174033c66f43SYan Zheng if (err) { 174133c66f43SYan Zheng ret = err; 174265b51a00SChris Mason goto done; 174354aa1f4dSChris Mason } 174402217ed2SChris Mason } 174565b51a00SChris Mason cow_done: 174602217ed2SChris Mason BUG_ON(!cow && ins_len); 174765b51a00SChris Mason 1748eb60ceacSChris Mason p->nodes[level] = b; 1749bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 1750b4ce94deSChris Mason 1751b4ce94deSChris Mason /* 1752b4ce94deSChris Mason * we have a lock on b and as long as we aren't changing 1753b4ce94deSChris Mason * the tree, there is no way to for the items in b to change. 1754b4ce94deSChris Mason * It is safe to drop the lock on our parent before we 1755b4ce94deSChris Mason * go through the expensive btree search on b. 1756b4ce94deSChris Mason * 1757b4ce94deSChris Mason * If cow is true, then we might be changing slot zero, 1758b4ce94deSChris Mason * which may require changing the parent. So, we can't 1759b4ce94deSChris Mason * drop the lock until after we know which slot we're 1760b4ce94deSChris Mason * operating on. 1761b4ce94deSChris Mason */ 1762b4ce94deSChris Mason if (!cow) 1763b4ce94deSChris Mason btrfs_unlock_up_safe(p, level + 1); 1764b4ce94deSChris Mason 17655f39d397SChris Mason ret = bin_search(b, key, level, &slot); 1766b4ce94deSChris Mason 17675f39d397SChris Mason if (level != 0) { 176833c66f43SYan Zheng int dec = 0; 176933c66f43SYan Zheng if (ret && slot > 0) { 177033c66f43SYan Zheng dec = 1; 1771be0e5c09SChris Mason slot -= 1; 177233c66f43SYan Zheng } 1773be0e5c09SChris Mason p->slots[level] = slot; 177433c66f43SYan Zheng err = setup_nodes_for_search(trans, root, p, b, level, 1775bd681513SChris Mason ins_len, &write_lock_level); 177633c66f43SYan Zheng if (err == -EAGAIN) 1777b4ce94deSChris Mason goto again; 177833c66f43SYan Zheng if (err) { 177933c66f43SYan Zheng ret = err; 178065b51a00SChris Mason goto done; 178133c66f43SYan Zheng } 17825c680ed6SChris Mason b = p->nodes[level]; 17835c680ed6SChris Mason slot = p->slots[level]; 1784b4ce94deSChris Mason 1785bd681513SChris Mason /* 1786bd681513SChris Mason * slot 0 is special, if we change the key 1787bd681513SChris Mason * we have to update the parent pointer 1788bd681513SChris Mason * which means we must have a write lock 1789bd681513SChris Mason * on the parent 1790bd681513SChris Mason */ 1791bd681513SChris Mason if (slot == 0 && cow && 1792bd681513SChris Mason write_lock_level < level + 1) { 1793bd681513SChris Mason write_lock_level = level + 1; 1794bd681513SChris Mason btrfs_release_path(p); 1795bd681513SChris Mason goto again; 1796bd681513SChris Mason } 1797bd681513SChris Mason 1798f9efa9c7SChris Mason unlock_up(p, level, lowest_unlock); 1799f9efa9c7SChris Mason 1800925baeddSChris Mason if (level == lowest_level) { 180133c66f43SYan Zheng if (dec) 180233c66f43SYan Zheng p->slots[level]++; 18035b21f2edSZheng Yan goto done; 1804925baeddSChris Mason } 1805ca7a79adSChris Mason 180633c66f43SYan Zheng err = read_block_for_search(trans, root, p, 1807c8c42864SChris Mason &b, level, slot, key); 180833c66f43SYan Zheng if (err == -EAGAIN) 1809051e1b9fSChris Mason goto again; 181033c66f43SYan Zheng if (err) { 181133c66f43SYan Zheng ret = err; 181276a05b35SChris Mason goto done; 181333c66f43SYan Zheng } 181476a05b35SChris Mason 1815b4ce94deSChris Mason if (!p->skip_locking) { 1816bd681513SChris Mason level = btrfs_header_level(b); 1817bd681513SChris Mason if (level <= write_lock_level) { 1818bd681513SChris Mason err = btrfs_try_tree_write_lock(b); 181933c66f43SYan Zheng if (!err) { 1820b4ce94deSChris Mason btrfs_set_path_blocking(p); 1821925baeddSChris Mason btrfs_tree_lock(b); 1822bd681513SChris Mason btrfs_clear_path_blocking(p, b, 1823bd681513SChris Mason BTRFS_WRITE_LOCK); 1824b4ce94deSChris Mason } 1825bd681513SChris Mason p->locks[level] = BTRFS_WRITE_LOCK; 1826bd681513SChris Mason } else { 1827bd681513SChris Mason err = btrfs_try_tree_read_lock(b); 1828bd681513SChris Mason if (!err) { 1829bd681513SChris Mason btrfs_set_path_blocking(p); 1830bd681513SChris Mason btrfs_tree_read_lock(b); 1831bd681513SChris Mason btrfs_clear_path_blocking(p, b, 1832bd681513SChris Mason BTRFS_READ_LOCK); 1833bd681513SChris Mason } 1834bd681513SChris Mason p->locks[level] = BTRFS_READ_LOCK; 1835bd681513SChris Mason } 1836bd681513SChris Mason p->nodes[level] = b; 1837b4ce94deSChris Mason } 1838be0e5c09SChris Mason } else { 1839be0e5c09SChris Mason p->slots[level] = slot; 184087b29b20SYan Zheng if (ins_len > 0 && 184187b29b20SYan Zheng btrfs_leaf_free_space(root, b) < ins_len) { 1842bd681513SChris Mason if (write_lock_level < 1) { 1843bd681513SChris Mason write_lock_level = 1; 1844bd681513SChris Mason btrfs_release_path(p); 1845bd681513SChris Mason goto again; 1846bd681513SChris Mason } 1847bd681513SChris Mason 1848b4ce94deSChris Mason btrfs_set_path_blocking(p); 184933c66f43SYan Zheng err = split_leaf(trans, root, key, 1850cc0c5538SChris Mason p, ins_len, ret == 0); 1851bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 1852b4ce94deSChris Mason 185333c66f43SYan Zheng BUG_ON(err > 0); 185433c66f43SYan Zheng if (err) { 185533c66f43SYan Zheng ret = err; 185665b51a00SChris Mason goto done; 185765b51a00SChris Mason } 18585c680ed6SChris Mason } 1859459931ecSChris Mason if (!p->search_for_split) 1860925baeddSChris Mason unlock_up(p, level, lowest_unlock); 186165b51a00SChris Mason goto done; 186265b51a00SChris Mason } 186365b51a00SChris Mason } 186465b51a00SChris Mason ret = 1; 186565b51a00SChris Mason done: 1866b4ce94deSChris Mason /* 1867b4ce94deSChris Mason * we don't really know what they plan on doing with the path 1868b4ce94deSChris Mason * from here on, so for now just mark it as blocking 1869b4ce94deSChris Mason */ 1870b9473439SChris Mason if (!p->leave_spinning) 1871b4ce94deSChris Mason btrfs_set_path_blocking(p); 187276a05b35SChris Mason if (ret < 0) 1873b3b4aa74SDavid Sterba btrfs_release_path(p); 1874be0e5c09SChris Mason return ret; 1875be0e5c09SChris Mason } 1876be0e5c09SChris Mason 187774123bd7SChris Mason /* 187874123bd7SChris Mason * adjust the pointers going up the tree, starting at level 187974123bd7SChris Mason * making sure the right key of each node is points to 'key'. 188074123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 188174123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 188274123bd7SChris Mason * higher levels 1883aa5d6bedSChris Mason * 1884aa5d6bedSChris Mason * If this fails to write a tree block, it returns -1, but continues 1885aa5d6bedSChris Mason * fixing up the blocks in ram so the tree is consistent. 188674123bd7SChris Mason */ 18875f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans, 18885f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 18895f39d397SChris Mason struct btrfs_disk_key *key, int level) 1890be0e5c09SChris Mason { 1891be0e5c09SChris Mason int i; 1892aa5d6bedSChris Mason int ret = 0; 18935f39d397SChris Mason struct extent_buffer *t; 18945f39d397SChris Mason 1895234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1896be0e5c09SChris Mason int tslot = path->slots[i]; 1897eb60ceacSChris Mason if (!path->nodes[i]) 1898be0e5c09SChris Mason break; 18995f39d397SChris Mason t = path->nodes[i]; 19005f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 1901d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 1902be0e5c09SChris Mason if (tslot != 0) 1903be0e5c09SChris Mason break; 1904be0e5c09SChris Mason } 1905aa5d6bedSChris Mason return ret; 1906be0e5c09SChris Mason } 1907be0e5c09SChris Mason 190874123bd7SChris Mason /* 190931840ae1SZheng Yan * update item key. 191031840ae1SZheng Yan * 191131840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 191231840ae1SZheng Yan * that the new key won't break the order 191331840ae1SZheng Yan */ 191431840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans, 191531840ae1SZheng Yan struct btrfs_root *root, struct btrfs_path *path, 191631840ae1SZheng Yan struct btrfs_key *new_key) 191731840ae1SZheng Yan { 191831840ae1SZheng Yan struct btrfs_disk_key disk_key; 191931840ae1SZheng Yan struct extent_buffer *eb; 192031840ae1SZheng Yan int slot; 192131840ae1SZheng Yan 192231840ae1SZheng Yan eb = path->nodes[0]; 192331840ae1SZheng Yan slot = path->slots[0]; 192431840ae1SZheng Yan if (slot > 0) { 192531840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 192631840ae1SZheng Yan if (comp_keys(&disk_key, new_key) >= 0) 192731840ae1SZheng Yan return -1; 192831840ae1SZheng Yan } 192931840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 193031840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 193131840ae1SZheng Yan if (comp_keys(&disk_key, new_key) <= 0) 193231840ae1SZheng Yan return -1; 193331840ae1SZheng Yan } 193431840ae1SZheng Yan 193531840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 193631840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 193731840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 193831840ae1SZheng Yan if (slot == 0) 193931840ae1SZheng Yan fixup_low_keys(trans, root, path, &disk_key, 1); 194031840ae1SZheng Yan return 0; 194131840ae1SZheng Yan } 194231840ae1SZheng Yan 194331840ae1SZheng Yan /* 194474123bd7SChris Mason * try to push data from one node into the next node left in the 194579f95c82SChris Mason * tree. 1946aa5d6bedSChris Mason * 1947aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 1948aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 194974123bd7SChris Mason */ 195098ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 195198ed5174SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 1952971a1f66SChris Mason struct extent_buffer *src, int empty) 1953be0e5c09SChris Mason { 1954be0e5c09SChris Mason int push_items = 0; 1955bb803951SChris Mason int src_nritems; 1956bb803951SChris Mason int dst_nritems; 1957aa5d6bedSChris Mason int ret = 0; 1958be0e5c09SChris Mason 19595f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 19605f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1961123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 19627bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 19637bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 196454aa1f4dSChris Mason 1965bce4eae9SChris Mason if (!empty && src_nritems <= 8) 1966971a1f66SChris Mason return 1; 1967971a1f66SChris Mason 1968d397712bSChris Mason if (push_items <= 0) 1969be0e5c09SChris Mason return 1; 1970be0e5c09SChris Mason 1971bce4eae9SChris Mason if (empty) { 1972971a1f66SChris Mason push_items = min(src_nritems, push_items); 1973bce4eae9SChris Mason if (push_items < src_nritems) { 1974bce4eae9SChris Mason /* leave at least 8 pointers in the node if 1975bce4eae9SChris Mason * we aren't going to empty it 1976bce4eae9SChris Mason */ 1977bce4eae9SChris Mason if (src_nritems - push_items < 8) { 1978bce4eae9SChris Mason if (push_items <= 8) 1979bce4eae9SChris Mason return 1; 1980bce4eae9SChris Mason push_items -= 8; 1981bce4eae9SChris Mason } 1982bce4eae9SChris Mason } 1983bce4eae9SChris Mason } else 1984bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 198579f95c82SChris Mason 19865f39d397SChris Mason copy_extent_buffer(dst, src, 19875f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 19885f39d397SChris Mason btrfs_node_key_ptr_offset(0), 1989123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 19905f39d397SChris Mason 1991bb803951SChris Mason if (push_items < src_nritems) { 19925f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 19935f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 1994e2fa7227SChris Mason (src_nritems - push_items) * 1995123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 1996bb803951SChris Mason } 19975f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 19985f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 19995f39d397SChris Mason btrfs_mark_buffer_dirty(src); 20005f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 200131840ae1SZheng Yan 2002bb803951SChris Mason return ret; 2003be0e5c09SChris Mason } 2004be0e5c09SChris Mason 200597571fd0SChris Mason /* 200679f95c82SChris Mason * try to push data from one node into the next node right in the 200779f95c82SChris Mason * tree. 200879f95c82SChris Mason * 200979f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 201079f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 201179f95c82SChris Mason * 201279f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 201379f95c82SChris Mason */ 20145f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 20155f39d397SChris Mason struct btrfs_root *root, 20165f39d397SChris Mason struct extent_buffer *dst, 20175f39d397SChris Mason struct extent_buffer *src) 201879f95c82SChris Mason { 201979f95c82SChris Mason int push_items = 0; 202079f95c82SChris Mason int max_push; 202179f95c82SChris Mason int src_nritems; 202279f95c82SChris Mason int dst_nritems; 202379f95c82SChris Mason int ret = 0; 202479f95c82SChris Mason 20257bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 20267bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 20277bb86316SChris Mason 20285f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 20295f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 2030123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 2031d397712bSChris Mason if (push_items <= 0) 203279f95c82SChris Mason return 1; 2033bce4eae9SChris Mason 2034d397712bSChris Mason if (src_nritems < 4) 2035bce4eae9SChris Mason return 1; 203679f95c82SChris Mason 203779f95c82SChris Mason max_push = src_nritems / 2 + 1; 203879f95c82SChris Mason /* don't try to empty the node */ 2039d397712bSChris Mason if (max_push >= src_nritems) 204079f95c82SChris Mason return 1; 2041252c38f0SYan 204279f95c82SChris Mason if (max_push < push_items) 204379f95c82SChris Mason push_items = max_push; 204479f95c82SChris Mason 20455f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 20465f39d397SChris Mason btrfs_node_key_ptr_offset(0), 20475f39d397SChris Mason (dst_nritems) * 20485f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 2049d6025579SChris Mason 20505f39d397SChris Mason copy_extent_buffer(dst, src, 20515f39d397SChris Mason btrfs_node_key_ptr_offset(0), 20525f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 2053123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 205479f95c82SChris Mason 20555f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 20565f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 205779f95c82SChris Mason 20585f39d397SChris Mason btrfs_mark_buffer_dirty(src); 20595f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 206031840ae1SZheng Yan 206179f95c82SChris Mason return ret; 206279f95c82SChris Mason } 206379f95c82SChris Mason 206479f95c82SChris Mason /* 206597571fd0SChris Mason * helper function to insert a new root level in the tree. 206697571fd0SChris Mason * A new node is allocated, and a single item is inserted to 206797571fd0SChris Mason * point to the existing root 2068aa5d6bedSChris Mason * 2069aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 207097571fd0SChris Mason */ 2071d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans, 20725f39d397SChris Mason struct btrfs_root *root, 20735f39d397SChris Mason struct btrfs_path *path, int level) 207474123bd7SChris Mason { 20757bb86316SChris Mason u64 lower_gen; 20765f39d397SChris Mason struct extent_buffer *lower; 20775f39d397SChris Mason struct extent_buffer *c; 2078925baeddSChris Mason struct extent_buffer *old; 20795f39d397SChris Mason struct btrfs_disk_key lower_key; 20805c680ed6SChris Mason 20815c680ed6SChris Mason BUG_ON(path->nodes[level]); 20825c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 20835c680ed6SChris Mason 20847bb86316SChris Mason lower = path->nodes[level-1]; 20857bb86316SChris Mason if (level == 1) 20867bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 20877bb86316SChris Mason else 20887bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 20897bb86316SChris Mason 209031840ae1SZheng Yan c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 20915d4f98a2SYan Zheng root->root_key.objectid, &lower_key, 2092ad3d81baSChristoph Hellwig level, root->node->start, 0); 20935f39d397SChris Mason if (IS_ERR(c)) 20945f39d397SChris Mason return PTR_ERR(c); 2095925baeddSChris Mason 2096f0486c68SYan, Zheng root_add_used(root, root->nodesize); 2097f0486c68SYan, Zheng 20985d4f98a2SYan Zheng memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); 20995f39d397SChris Mason btrfs_set_header_nritems(c, 1); 21005f39d397SChris Mason btrfs_set_header_level(c, level); 2101db94535dSChris Mason btrfs_set_header_bytenr(c, c->start); 21025f39d397SChris Mason btrfs_set_header_generation(c, trans->transid); 21035d4f98a2SYan Zheng btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); 21045f39d397SChris Mason btrfs_set_header_owner(c, root->root_key.objectid); 2105d5719762SChris Mason 21065f39d397SChris Mason write_extent_buffer(c, root->fs_info->fsid, 21075f39d397SChris Mason (unsigned long)btrfs_header_fsid(c), 21085f39d397SChris Mason BTRFS_FSID_SIZE); 2109e17cade2SChris Mason 2110e17cade2SChris Mason write_extent_buffer(c, root->fs_info->chunk_tree_uuid, 2111e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(c), 2112e17cade2SChris Mason BTRFS_UUID_SIZE); 2113e17cade2SChris Mason 21145f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 2115db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 21167bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 211731840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 21187bb86316SChris Mason 21197bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 21205f39d397SChris Mason 21215f39d397SChris Mason btrfs_mark_buffer_dirty(c); 2122d5719762SChris Mason 2123925baeddSChris Mason old = root->node; 2124240f62c8SChris Mason rcu_assign_pointer(root->node, c); 2125925baeddSChris Mason 2126925baeddSChris Mason /* the super has an extra ref to root->node */ 2127925baeddSChris Mason free_extent_buffer(old); 2128925baeddSChris Mason 21290b86a832SChris Mason add_root_to_dirty_list(root); 21305f39d397SChris Mason extent_buffer_get(c); 21315f39d397SChris Mason path->nodes[level] = c; 2132bd681513SChris Mason path->locks[level] = BTRFS_WRITE_LOCK; 213374123bd7SChris Mason path->slots[level] = 0; 213474123bd7SChris Mason return 0; 213574123bd7SChris Mason } 21365c680ed6SChris Mason 21375c680ed6SChris Mason /* 21385c680ed6SChris Mason * worker function to insert a single pointer in a node. 21395c680ed6SChris Mason * the node should have enough room for the pointer already 214097571fd0SChris Mason * 21415c680ed6SChris Mason * slot and level indicate where you want the key to go, and 21425c680ed6SChris Mason * blocknr is the block the key points to. 2143aa5d6bedSChris Mason * 2144aa5d6bedSChris Mason * returns zero on success and < 0 on any error 21455c680ed6SChris Mason */ 2146e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root 2147e089f05cSChris Mason *root, struct btrfs_path *path, struct btrfs_disk_key 2148db94535dSChris Mason *key, u64 bytenr, int slot, int level) 21495c680ed6SChris Mason { 21505f39d397SChris Mason struct extent_buffer *lower; 21515c680ed6SChris Mason int nritems; 21525c680ed6SChris Mason 21535c680ed6SChris Mason BUG_ON(!path->nodes[level]); 2154f0486c68SYan, Zheng btrfs_assert_tree_locked(path->nodes[level]); 21555f39d397SChris Mason lower = path->nodes[level]; 21565f39d397SChris Mason nritems = btrfs_header_nritems(lower); 2157c293498bSStoyan Gaydarov BUG_ON(slot > nritems); 2158123abc88SChris Mason if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) 215974123bd7SChris Mason BUG(); 216074123bd7SChris Mason if (slot != nritems) { 21615f39d397SChris Mason memmove_extent_buffer(lower, 21625f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 21635f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 2164123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 216574123bd7SChris Mason } 21665f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 2167db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 216874493f7aSChris Mason WARN_ON(trans->transid == 0); 216974493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 21705f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 21715f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 217274123bd7SChris Mason return 0; 217374123bd7SChris Mason } 217474123bd7SChris Mason 217597571fd0SChris Mason /* 217697571fd0SChris Mason * split the node at the specified level in path in two. 217797571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 217897571fd0SChris Mason * 217997571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 218097571fd0SChris Mason * left and right, if either one works, it returns right away. 2181aa5d6bedSChris Mason * 2182aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 218397571fd0SChris Mason */ 2184e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 2185e02119d5SChris Mason struct btrfs_root *root, 2186e02119d5SChris Mason struct btrfs_path *path, int level) 2187be0e5c09SChris Mason { 21885f39d397SChris Mason struct extent_buffer *c; 21895f39d397SChris Mason struct extent_buffer *split; 21905f39d397SChris Mason struct btrfs_disk_key disk_key; 2191be0e5c09SChris Mason int mid; 21925c680ed6SChris Mason int ret; 2193aa5d6bedSChris Mason int wret; 21947518a238SChris Mason u32 c_nritems; 2195be0e5c09SChris Mason 21965f39d397SChris Mason c = path->nodes[level]; 21977bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 21985f39d397SChris Mason if (c == root->node) { 21995c680ed6SChris Mason /* trying to split the root, lets make a new one */ 2200e089f05cSChris Mason ret = insert_new_root(trans, root, path, level + 1); 22015c680ed6SChris Mason if (ret) 22025c680ed6SChris Mason return ret; 2203b3612421SChris Mason } else { 2204e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 22055f39d397SChris Mason c = path->nodes[level]; 22065f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 2207c448acf0SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) 2208e66f709bSChris Mason return 0; 220954aa1f4dSChris Mason if (ret < 0) 221054aa1f4dSChris Mason return ret; 22115c680ed6SChris Mason } 2212e66f709bSChris Mason 22135f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 22145d4f98a2SYan Zheng mid = (c_nritems + 1) / 2; 22155d4f98a2SYan Zheng btrfs_node_key(c, &disk_key, mid); 22167bb86316SChris Mason 22175d4f98a2SYan Zheng split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 22187bb86316SChris Mason root->root_key.objectid, 22195d4f98a2SYan Zheng &disk_key, level, c->start, 0); 22205f39d397SChris Mason if (IS_ERR(split)) 22215f39d397SChris Mason return PTR_ERR(split); 222254aa1f4dSChris Mason 2223f0486c68SYan, Zheng root_add_used(root, root->nodesize); 2224f0486c68SYan, Zheng 22255d4f98a2SYan Zheng memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); 22265f39d397SChris Mason btrfs_set_header_level(split, btrfs_header_level(c)); 2227db94535dSChris Mason btrfs_set_header_bytenr(split, split->start); 22285f39d397SChris Mason btrfs_set_header_generation(split, trans->transid); 22295d4f98a2SYan Zheng btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); 22305f39d397SChris Mason btrfs_set_header_owner(split, root->root_key.objectid); 22315f39d397SChris Mason write_extent_buffer(split, root->fs_info->fsid, 22325f39d397SChris Mason (unsigned long)btrfs_header_fsid(split), 22335f39d397SChris Mason BTRFS_FSID_SIZE); 2234e17cade2SChris Mason write_extent_buffer(split, root->fs_info->chunk_tree_uuid, 2235e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(split), 2236e17cade2SChris Mason BTRFS_UUID_SIZE); 22375f39d397SChris Mason 22385f39d397SChris Mason 22395f39d397SChris Mason copy_extent_buffer(split, c, 22405f39d397SChris Mason btrfs_node_key_ptr_offset(0), 22415f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 2242123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 22435f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 22445f39d397SChris Mason btrfs_set_header_nritems(c, mid); 2245aa5d6bedSChris Mason ret = 0; 2246aa5d6bedSChris Mason 22475f39d397SChris Mason btrfs_mark_buffer_dirty(c); 22485f39d397SChris Mason btrfs_mark_buffer_dirty(split); 22495f39d397SChris Mason 2250db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, split->start, 22515f39d397SChris Mason path->slots[level + 1] + 1, 2252123abc88SChris Mason level + 1); 2253aa5d6bedSChris Mason if (wret) 2254aa5d6bedSChris Mason ret = wret; 2255aa5d6bedSChris Mason 22565de08d7dSChris Mason if (path->slots[level] >= mid) { 22575c680ed6SChris Mason path->slots[level] -= mid; 2258925baeddSChris Mason btrfs_tree_unlock(c); 22595f39d397SChris Mason free_extent_buffer(c); 22605f39d397SChris Mason path->nodes[level] = split; 22615c680ed6SChris Mason path->slots[level + 1] += 1; 2262eb60ceacSChris Mason } else { 2263925baeddSChris Mason btrfs_tree_unlock(split); 22645f39d397SChris Mason free_extent_buffer(split); 2265be0e5c09SChris Mason } 2266aa5d6bedSChris Mason return ret; 2267be0e5c09SChris Mason } 2268be0e5c09SChris Mason 226974123bd7SChris Mason /* 227074123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 227174123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 227274123bd7SChris Mason * space used both by the item structs and the item data 227374123bd7SChris Mason */ 22745f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 2275be0e5c09SChris Mason { 2276be0e5c09SChris Mason int data_len; 22775f39d397SChris Mason int nritems = btrfs_header_nritems(l); 2278d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 2279be0e5c09SChris Mason 2280be0e5c09SChris Mason if (!nr) 2281be0e5c09SChris Mason return 0; 22825f39d397SChris Mason data_len = btrfs_item_end_nr(l, start); 22835f39d397SChris Mason data_len = data_len - btrfs_item_offset_nr(l, end); 22840783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 2285d4dbff95SChris Mason WARN_ON(data_len < 0); 2286be0e5c09SChris Mason return data_len; 2287be0e5c09SChris Mason } 2288be0e5c09SChris Mason 228974123bd7SChris Mason /* 2290d4dbff95SChris Mason * The space between the end of the leaf items and 2291d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 2292d4dbff95SChris Mason * the leaf has left for both items and data 2293d4dbff95SChris Mason */ 2294d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root, 2295e02119d5SChris Mason struct extent_buffer *leaf) 2296d4dbff95SChris Mason { 22975f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 22985f39d397SChris Mason int ret; 22995f39d397SChris Mason ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); 23005f39d397SChris Mason if (ret < 0) { 2301d397712bSChris Mason printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " 2302d397712bSChris Mason "used %d nritems %d\n", 2303ae2f5411SJens Axboe ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), 23045f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 23055f39d397SChris Mason } 23065f39d397SChris Mason return ret; 2307d4dbff95SChris Mason } 2308d4dbff95SChris Mason 230999d8f83cSChris Mason /* 231099d8f83cSChris Mason * min slot controls the lowest index we're willing to push to the 231199d8f83cSChris Mason * right. We'll push up to and including min_slot, but no lower 231299d8f83cSChris Mason */ 231344871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, 231444871b1bSChris Mason struct btrfs_root *root, 231544871b1bSChris Mason struct btrfs_path *path, 231644871b1bSChris Mason int data_size, int empty, 231744871b1bSChris Mason struct extent_buffer *right, 231899d8f83cSChris Mason int free_space, u32 left_nritems, 231999d8f83cSChris Mason u32 min_slot) 232000ec4c51SChris Mason { 23215f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 232244871b1bSChris Mason struct extent_buffer *upper = path->nodes[1]; 23235f39d397SChris Mason struct btrfs_disk_key disk_key; 232400ec4c51SChris Mason int slot; 232534a38218SChris Mason u32 i; 232600ec4c51SChris Mason int push_space = 0; 232700ec4c51SChris Mason int push_items = 0; 23280783fcfcSChris Mason struct btrfs_item *item; 232934a38218SChris Mason u32 nr; 23307518a238SChris Mason u32 right_nritems; 23315f39d397SChris Mason u32 data_end; 2332db94535dSChris Mason u32 this_item_size; 233300ec4c51SChris Mason 233434a38218SChris Mason if (empty) 233534a38218SChris Mason nr = 0; 233634a38218SChris Mason else 233799d8f83cSChris Mason nr = max_t(u32, 1, min_slot); 233834a38218SChris Mason 233931840ae1SZheng Yan if (path->slots[0] >= left_nritems) 234087b29b20SYan Zheng push_space += data_size; 234131840ae1SZheng Yan 234244871b1bSChris Mason slot = path->slots[1]; 234334a38218SChris Mason i = left_nritems - 1; 234434a38218SChris Mason while (i >= nr) { 23455f39d397SChris Mason item = btrfs_item_nr(left, i); 2346db94535dSChris Mason 234731840ae1SZheng Yan if (!empty && push_items > 0) { 234831840ae1SZheng Yan if (path->slots[0] > i) 234931840ae1SZheng Yan break; 235031840ae1SZheng Yan if (path->slots[0] == i) { 235131840ae1SZheng Yan int space = btrfs_leaf_free_space(root, left); 235231840ae1SZheng Yan if (space + push_space * 2 > free_space) 235331840ae1SZheng Yan break; 235431840ae1SZheng Yan } 235531840ae1SZheng Yan } 235631840ae1SZheng Yan 235700ec4c51SChris Mason if (path->slots[0] == i) 235887b29b20SYan Zheng push_space += data_size; 2359db94535dSChris Mason 2360db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 2361db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 236200ec4c51SChris Mason break; 236331840ae1SZheng Yan 236400ec4c51SChris Mason push_items++; 2365db94535dSChris Mason push_space += this_item_size + sizeof(*item); 236634a38218SChris Mason if (i == 0) 236734a38218SChris Mason break; 236834a38218SChris Mason i--; 2369db94535dSChris Mason } 23705f39d397SChris Mason 2371925baeddSChris Mason if (push_items == 0) 2372925baeddSChris Mason goto out_unlock; 23735f39d397SChris Mason 237434a38218SChris Mason if (!empty && push_items == left_nritems) 2375a429e513SChris Mason WARN_ON(1); 23765f39d397SChris Mason 237700ec4c51SChris Mason /* push left to right */ 23785f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 237934a38218SChris Mason 23805f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 2381123abc88SChris Mason push_space -= leaf_data_end(root, left); 23825f39d397SChris Mason 238300ec4c51SChris Mason /* make room in the right data area */ 23845f39d397SChris Mason data_end = leaf_data_end(root, right); 23855f39d397SChris Mason memmove_extent_buffer(right, 23865f39d397SChris Mason btrfs_leaf_data(right) + data_end - push_space, 23875f39d397SChris Mason btrfs_leaf_data(right) + data_end, 23885f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root) - data_end); 23895f39d397SChris Mason 239000ec4c51SChris Mason /* copy from the left data area */ 23915f39d397SChris Mason copy_extent_buffer(right, left, btrfs_leaf_data(right) + 2392d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2393d6025579SChris Mason btrfs_leaf_data(left) + leaf_data_end(root, left), 2394d6025579SChris Mason push_space); 23955f39d397SChris Mason 23965f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 23975f39d397SChris Mason btrfs_item_nr_offset(0), 23980783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 23995f39d397SChris Mason 240000ec4c51SChris Mason /* copy the items from left to right */ 24015f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 24025f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 24030783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 240400ec4c51SChris Mason 240500ec4c51SChris Mason /* update the item pointers */ 24067518a238SChris Mason right_nritems += push_items; 24075f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 2408123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 24097518a238SChris Mason for (i = 0; i < right_nritems; i++) { 24105f39d397SChris Mason item = btrfs_item_nr(right, i); 2411db94535dSChris Mason push_space -= btrfs_item_size(right, item); 2412db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2413db94535dSChris Mason } 2414db94535dSChris Mason 24157518a238SChris Mason left_nritems -= push_items; 24165f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 241700ec4c51SChris Mason 241834a38218SChris Mason if (left_nritems) 24195f39d397SChris Mason btrfs_mark_buffer_dirty(left); 2420f0486c68SYan, Zheng else 2421f0486c68SYan, Zheng clean_tree_block(trans, root, left); 2422f0486c68SYan, Zheng 24235f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2424a429e513SChris Mason 24255f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 24265f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 2427d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 242802217ed2SChris Mason 242900ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 24307518a238SChris Mason if (path->slots[0] >= left_nritems) { 24317518a238SChris Mason path->slots[0] -= left_nritems; 2432925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 2433925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 2434925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 24355f39d397SChris Mason free_extent_buffer(path->nodes[0]); 24365f39d397SChris Mason path->nodes[0] = right; 243700ec4c51SChris Mason path->slots[1] += 1; 243800ec4c51SChris Mason } else { 2439925baeddSChris Mason btrfs_tree_unlock(right); 24405f39d397SChris Mason free_extent_buffer(right); 244100ec4c51SChris Mason } 244200ec4c51SChris Mason return 0; 2443925baeddSChris Mason 2444925baeddSChris Mason out_unlock: 2445925baeddSChris Mason btrfs_tree_unlock(right); 2446925baeddSChris Mason free_extent_buffer(right); 2447925baeddSChris Mason return 1; 244800ec4c51SChris Mason } 2449925baeddSChris Mason 245000ec4c51SChris Mason /* 245144871b1bSChris Mason * push some data in the path leaf to the right, trying to free up at 245274123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 245344871b1bSChris Mason * 245444871b1bSChris Mason * returns 1 if the push failed because the other node didn't have enough 245544871b1bSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 245699d8f83cSChris Mason * 245799d8f83cSChris Mason * this will push starting from min_slot to the end of the leaf. It won't 245899d8f83cSChris Mason * push any slot lower than min_slot 245974123bd7SChris Mason */ 246044871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 246199d8f83cSChris Mason *root, struct btrfs_path *path, 246299d8f83cSChris Mason int min_data_size, int data_size, 246399d8f83cSChris Mason int empty, u32 min_slot) 2464be0e5c09SChris Mason { 246544871b1bSChris Mason struct extent_buffer *left = path->nodes[0]; 246644871b1bSChris Mason struct extent_buffer *right; 246744871b1bSChris Mason struct extent_buffer *upper; 246844871b1bSChris Mason int slot; 246944871b1bSChris Mason int free_space; 247044871b1bSChris Mason u32 left_nritems; 247144871b1bSChris Mason int ret; 247244871b1bSChris Mason 247344871b1bSChris Mason if (!path->nodes[1]) 247444871b1bSChris Mason return 1; 247544871b1bSChris Mason 247644871b1bSChris Mason slot = path->slots[1]; 247744871b1bSChris Mason upper = path->nodes[1]; 247844871b1bSChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 247944871b1bSChris Mason return 1; 248044871b1bSChris Mason 248144871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 248244871b1bSChris Mason 248344871b1bSChris Mason right = read_node_slot(root, upper, slot + 1); 248491ca338dSTsutomu Itoh if (right == NULL) 248591ca338dSTsutomu Itoh return 1; 248691ca338dSTsutomu Itoh 248744871b1bSChris Mason btrfs_tree_lock(right); 248844871b1bSChris Mason btrfs_set_lock_blocking(right); 248944871b1bSChris Mason 249044871b1bSChris Mason free_space = btrfs_leaf_free_space(root, right); 249144871b1bSChris Mason if (free_space < data_size) 249244871b1bSChris Mason goto out_unlock; 249344871b1bSChris Mason 249444871b1bSChris Mason /* cow and double check */ 249544871b1bSChris Mason ret = btrfs_cow_block(trans, root, right, upper, 249644871b1bSChris Mason slot + 1, &right); 249744871b1bSChris Mason if (ret) 249844871b1bSChris Mason goto out_unlock; 249944871b1bSChris Mason 250044871b1bSChris Mason free_space = btrfs_leaf_free_space(root, right); 250144871b1bSChris Mason if (free_space < data_size) 250244871b1bSChris Mason goto out_unlock; 250344871b1bSChris Mason 250444871b1bSChris Mason left_nritems = btrfs_header_nritems(left); 250544871b1bSChris Mason if (left_nritems == 0) 250644871b1bSChris Mason goto out_unlock; 250744871b1bSChris Mason 250899d8f83cSChris Mason return __push_leaf_right(trans, root, path, min_data_size, empty, 250999d8f83cSChris Mason right, free_space, left_nritems, min_slot); 251044871b1bSChris Mason out_unlock: 251144871b1bSChris Mason btrfs_tree_unlock(right); 251244871b1bSChris Mason free_extent_buffer(right); 251344871b1bSChris Mason return 1; 251444871b1bSChris Mason } 251544871b1bSChris Mason 251644871b1bSChris Mason /* 251744871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 251844871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 251999d8f83cSChris Mason * 252099d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 252199d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the 252299d8f83cSChris Mason * items 252344871b1bSChris Mason */ 252444871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, 252544871b1bSChris Mason struct btrfs_root *root, 252644871b1bSChris Mason struct btrfs_path *path, int data_size, 252744871b1bSChris Mason int empty, struct extent_buffer *left, 252899d8f83cSChris Mason int free_space, u32 right_nritems, 252999d8f83cSChris Mason u32 max_slot) 253044871b1bSChris Mason { 25315f39d397SChris Mason struct btrfs_disk_key disk_key; 25325f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 2533be0e5c09SChris Mason int i; 2534be0e5c09SChris Mason int push_space = 0; 2535be0e5c09SChris Mason int push_items = 0; 25360783fcfcSChris Mason struct btrfs_item *item; 25377518a238SChris Mason u32 old_left_nritems; 253834a38218SChris Mason u32 nr; 2539aa5d6bedSChris Mason int ret = 0; 2540aa5d6bedSChris Mason int wret; 2541db94535dSChris Mason u32 this_item_size; 2542db94535dSChris Mason u32 old_left_item_size; 2543be0e5c09SChris Mason 254434a38218SChris Mason if (empty) 254599d8f83cSChris Mason nr = min(right_nritems, max_slot); 254634a38218SChris Mason else 254799d8f83cSChris Mason nr = min(right_nritems - 1, max_slot); 254834a38218SChris Mason 254934a38218SChris Mason for (i = 0; i < nr; i++) { 25505f39d397SChris Mason item = btrfs_item_nr(right, i); 2551db94535dSChris Mason 255231840ae1SZheng Yan if (!empty && push_items > 0) { 255331840ae1SZheng Yan if (path->slots[0] < i) 255431840ae1SZheng Yan break; 255531840ae1SZheng Yan if (path->slots[0] == i) { 255631840ae1SZheng Yan int space = btrfs_leaf_free_space(root, right); 255731840ae1SZheng Yan if (space + push_space * 2 > free_space) 255831840ae1SZheng Yan break; 255931840ae1SZheng Yan } 256031840ae1SZheng Yan } 256131840ae1SZheng Yan 2562be0e5c09SChris Mason if (path->slots[0] == i) 256387b29b20SYan Zheng push_space += data_size; 2564db94535dSChris Mason 2565db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 2566db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 2567be0e5c09SChris Mason break; 2568db94535dSChris Mason 2569be0e5c09SChris Mason push_items++; 2570db94535dSChris Mason push_space += this_item_size + sizeof(*item); 2571be0e5c09SChris Mason } 2572db94535dSChris Mason 2573be0e5c09SChris Mason if (push_items == 0) { 2574925baeddSChris Mason ret = 1; 2575925baeddSChris Mason goto out; 2576be0e5c09SChris Mason } 257734a38218SChris Mason if (!empty && push_items == btrfs_header_nritems(right)) 2578a429e513SChris Mason WARN_ON(1); 25795f39d397SChris Mason 2580be0e5c09SChris Mason /* push data from right to left */ 25815f39d397SChris Mason copy_extent_buffer(left, right, 25825f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 25835f39d397SChris Mason btrfs_item_nr_offset(0), 25845f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 25855f39d397SChris Mason 2586123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root) - 25875f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1); 25885f39d397SChris Mason 25895f39d397SChris Mason copy_extent_buffer(left, right, btrfs_leaf_data(left) + 2590d6025579SChris Mason leaf_data_end(root, left) - push_space, 2591123abc88SChris Mason btrfs_leaf_data(right) + 25925f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 2593be0e5c09SChris Mason push_space); 25945f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 259587b29b20SYan Zheng BUG_ON(old_left_nritems <= 0); 2596eb60ceacSChris Mason 2597db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 2598be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 25995f39d397SChris Mason u32 ioff; 2600db94535dSChris Mason 26015f39d397SChris Mason item = btrfs_item_nr(left, i); 2602db94535dSChris Mason 26035f39d397SChris Mason ioff = btrfs_item_offset(left, item); 26045f39d397SChris Mason btrfs_set_item_offset(left, item, 2605db94535dSChris Mason ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); 2606be0e5c09SChris Mason } 26075f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 2608be0e5c09SChris Mason 2609be0e5c09SChris Mason /* fixup right node */ 261034a38218SChris Mason if (push_items > right_nritems) { 2611d397712bSChris Mason printk(KERN_CRIT "push items %d nr %u\n", push_items, 2612d397712bSChris Mason right_nritems); 261334a38218SChris Mason WARN_ON(1); 261434a38218SChris Mason } 261534a38218SChris Mason 261634a38218SChris Mason if (push_items < right_nritems) { 26175f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 2618123abc88SChris Mason leaf_data_end(root, right); 26195f39d397SChris Mason memmove_extent_buffer(right, btrfs_leaf_data(right) + 2620d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2621d6025579SChris Mason btrfs_leaf_data(right) + 2622123abc88SChris Mason leaf_data_end(root, right), push_space); 26235f39d397SChris Mason 26245f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 26255f39d397SChris Mason btrfs_item_nr_offset(push_items), 26265f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 26270783fcfcSChris Mason sizeof(struct btrfs_item)); 262834a38218SChris Mason } 2629eef1c494SYan right_nritems -= push_items; 2630eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 2631123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 26325f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 26335f39d397SChris Mason item = btrfs_item_nr(right, i); 2634db94535dSChris Mason 2635db94535dSChris Mason push_space = push_space - btrfs_item_size(right, item); 2636db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2637db94535dSChris Mason } 2638eb60ceacSChris Mason 26395f39d397SChris Mason btrfs_mark_buffer_dirty(left); 264034a38218SChris Mason if (right_nritems) 26415f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2642f0486c68SYan, Zheng else 2643f0486c68SYan, Zheng clean_tree_block(trans, root, right); 2644098f59c2SChris Mason 26455f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 26465f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, 1); 2647aa5d6bedSChris Mason if (wret) 2648aa5d6bedSChris Mason ret = wret; 2649be0e5c09SChris Mason 2650be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 2651be0e5c09SChris Mason if (path->slots[0] < push_items) { 2652be0e5c09SChris Mason path->slots[0] += old_left_nritems; 2653925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 26545f39d397SChris Mason free_extent_buffer(path->nodes[0]); 26555f39d397SChris Mason path->nodes[0] = left; 2656be0e5c09SChris Mason path->slots[1] -= 1; 2657be0e5c09SChris Mason } else { 2658925baeddSChris Mason btrfs_tree_unlock(left); 26595f39d397SChris Mason free_extent_buffer(left); 2660be0e5c09SChris Mason path->slots[0] -= push_items; 2661be0e5c09SChris Mason } 2662eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2663aa5d6bedSChris Mason return ret; 2664925baeddSChris Mason out: 2665925baeddSChris Mason btrfs_tree_unlock(left); 2666925baeddSChris Mason free_extent_buffer(left); 2667925baeddSChris Mason return ret; 2668be0e5c09SChris Mason } 2669be0e5c09SChris Mason 267074123bd7SChris Mason /* 267144871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 267244871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 267399d8f83cSChris Mason * 267499d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 267599d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the 267699d8f83cSChris Mason * items 267744871b1bSChris Mason */ 267844871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 267999d8f83cSChris Mason *root, struct btrfs_path *path, int min_data_size, 268099d8f83cSChris Mason int data_size, int empty, u32 max_slot) 268144871b1bSChris Mason { 268244871b1bSChris Mason struct extent_buffer *right = path->nodes[0]; 268344871b1bSChris Mason struct extent_buffer *left; 268444871b1bSChris Mason int slot; 268544871b1bSChris Mason int free_space; 268644871b1bSChris Mason u32 right_nritems; 268744871b1bSChris Mason int ret = 0; 268844871b1bSChris Mason 268944871b1bSChris Mason slot = path->slots[1]; 269044871b1bSChris Mason if (slot == 0) 269144871b1bSChris Mason return 1; 269244871b1bSChris Mason if (!path->nodes[1]) 269344871b1bSChris Mason return 1; 269444871b1bSChris Mason 269544871b1bSChris Mason right_nritems = btrfs_header_nritems(right); 269644871b1bSChris Mason if (right_nritems == 0) 269744871b1bSChris Mason return 1; 269844871b1bSChris Mason 269944871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 270044871b1bSChris Mason 270144871b1bSChris Mason left = read_node_slot(root, path->nodes[1], slot - 1); 270291ca338dSTsutomu Itoh if (left == NULL) 270391ca338dSTsutomu Itoh return 1; 270491ca338dSTsutomu Itoh 270544871b1bSChris Mason btrfs_tree_lock(left); 270644871b1bSChris Mason btrfs_set_lock_blocking(left); 270744871b1bSChris Mason 270844871b1bSChris Mason free_space = btrfs_leaf_free_space(root, left); 270944871b1bSChris Mason if (free_space < data_size) { 271044871b1bSChris Mason ret = 1; 271144871b1bSChris Mason goto out; 271244871b1bSChris Mason } 271344871b1bSChris Mason 271444871b1bSChris Mason /* cow and double check */ 271544871b1bSChris Mason ret = btrfs_cow_block(trans, root, left, 271644871b1bSChris Mason path->nodes[1], slot - 1, &left); 271744871b1bSChris Mason if (ret) { 271844871b1bSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 271944871b1bSChris Mason ret = 1; 272044871b1bSChris Mason goto out; 272144871b1bSChris Mason } 272244871b1bSChris Mason 272344871b1bSChris Mason free_space = btrfs_leaf_free_space(root, left); 272444871b1bSChris Mason if (free_space < data_size) { 272544871b1bSChris Mason ret = 1; 272644871b1bSChris Mason goto out; 272744871b1bSChris Mason } 272844871b1bSChris Mason 272999d8f83cSChris Mason return __push_leaf_left(trans, root, path, min_data_size, 273099d8f83cSChris Mason empty, left, free_space, right_nritems, 273199d8f83cSChris Mason max_slot); 273244871b1bSChris Mason out: 273344871b1bSChris Mason btrfs_tree_unlock(left); 273444871b1bSChris Mason free_extent_buffer(left); 273544871b1bSChris Mason return ret; 273644871b1bSChris Mason } 273744871b1bSChris Mason 273844871b1bSChris Mason /* 273974123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 274074123bd7SChris Mason * available for the resulting leaf level of the path. 2741aa5d6bedSChris Mason * 2742aa5d6bedSChris Mason * returns 0 if all went well and < 0 on failure. 274374123bd7SChris Mason */ 274444871b1bSChris Mason static noinline int copy_for_split(struct btrfs_trans_handle *trans, 2745e02119d5SChris Mason struct btrfs_root *root, 274644871b1bSChris Mason struct btrfs_path *path, 274744871b1bSChris Mason struct extent_buffer *l, 274844871b1bSChris Mason struct extent_buffer *right, 274944871b1bSChris Mason int slot, int mid, int nritems) 2750be0e5c09SChris Mason { 2751be0e5c09SChris Mason int data_copy_size; 2752be0e5c09SChris Mason int rt_data_off; 2753be0e5c09SChris Mason int i; 2754d4dbff95SChris Mason int ret = 0; 2755aa5d6bedSChris Mason int wret; 2756d4dbff95SChris Mason struct btrfs_disk_key disk_key; 2757be0e5c09SChris Mason 27585f39d397SChris Mason nritems = nritems - mid; 27595f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 27605f39d397SChris Mason data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 27615f39d397SChris Mason 27625f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 27635f39d397SChris Mason btrfs_item_nr_offset(mid), 27645f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 27655f39d397SChris Mason 27665f39d397SChris Mason copy_extent_buffer(right, l, 2767d6025579SChris Mason btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - 2768123abc88SChris Mason data_copy_size, btrfs_leaf_data(l) + 2769123abc88SChris Mason leaf_data_end(root, l), data_copy_size); 277074123bd7SChris Mason 27715f39d397SChris Mason rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - 27725f39d397SChris Mason btrfs_item_end_nr(l, mid); 27735f39d397SChris Mason 27745f39d397SChris Mason for (i = 0; i < nritems; i++) { 27755f39d397SChris Mason struct btrfs_item *item = btrfs_item_nr(right, i); 2776db94535dSChris Mason u32 ioff; 2777db94535dSChris Mason 2778db94535dSChris Mason ioff = btrfs_item_offset(right, item); 27795f39d397SChris Mason btrfs_set_item_offset(right, item, ioff + rt_data_off); 27800783fcfcSChris Mason } 278174123bd7SChris Mason 27825f39d397SChris Mason btrfs_set_header_nritems(l, mid); 2783aa5d6bedSChris Mason ret = 0; 27845f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 2785db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, right->start, 2786db94535dSChris Mason path->slots[1] + 1, 1); 2787aa5d6bedSChris Mason if (wret) 2788aa5d6bedSChris Mason ret = wret; 27895f39d397SChris Mason 27905f39d397SChris Mason btrfs_mark_buffer_dirty(right); 27915f39d397SChris Mason btrfs_mark_buffer_dirty(l); 2792eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 27935f39d397SChris Mason 2794be0e5c09SChris Mason if (mid <= slot) { 2795925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 27965f39d397SChris Mason free_extent_buffer(path->nodes[0]); 27975f39d397SChris Mason path->nodes[0] = right; 2798be0e5c09SChris Mason path->slots[0] -= mid; 2799be0e5c09SChris Mason path->slots[1] += 1; 2800925baeddSChris Mason } else { 2801925baeddSChris Mason btrfs_tree_unlock(right); 28025f39d397SChris Mason free_extent_buffer(right); 2803925baeddSChris Mason } 28045f39d397SChris Mason 2805eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2806d4dbff95SChris Mason 280744871b1bSChris Mason return ret; 280844871b1bSChris Mason } 280944871b1bSChris Mason 281044871b1bSChris Mason /* 281199d8f83cSChris Mason * double splits happen when we need to insert a big item in the middle 281299d8f83cSChris Mason * of a leaf. A double split can leave us with 3 mostly empty leaves: 281399d8f83cSChris Mason * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] 281499d8f83cSChris Mason * A B C 281599d8f83cSChris Mason * 281699d8f83cSChris Mason * We avoid this by trying to push the items on either side of our target 281799d8f83cSChris Mason * into the adjacent leaves. If all goes well we can avoid the double split 281899d8f83cSChris Mason * completely. 281999d8f83cSChris Mason */ 282099d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans, 282199d8f83cSChris Mason struct btrfs_root *root, 282299d8f83cSChris Mason struct btrfs_path *path, 282399d8f83cSChris Mason int data_size) 282499d8f83cSChris Mason { 282599d8f83cSChris Mason int ret; 282699d8f83cSChris Mason int progress = 0; 282799d8f83cSChris Mason int slot; 282899d8f83cSChris Mason u32 nritems; 282999d8f83cSChris Mason 283099d8f83cSChris Mason slot = path->slots[0]; 283199d8f83cSChris Mason 283299d8f83cSChris Mason /* 283399d8f83cSChris Mason * try to push all the items after our slot into the 283499d8f83cSChris Mason * right leaf 283599d8f83cSChris Mason */ 283699d8f83cSChris Mason ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); 283799d8f83cSChris Mason if (ret < 0) 283899d8f83cSChris Mason return ret; 283999d8f83cSChris Mason 284099d8f83cSChris Mason if (ret == 0) 284199d8f83cSChris Mason progress++; 284299d8f83cSChris Mason 284399d8f83cSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 284499d8f83cSChris Mason /* 284599d8f83cSChris Mason * our goal is to get our slot at the start or end of a leaf. If 284699d8f83cSChris Mason * we've done so we're done 284799d8f83cSChris Mason */ 284899d8f83cSChris Mason if (path->slots[0] == 0 || path->slots[0] == nritems) 284999d8f83cSChris Mason return 0; 285099d8f83cSChris Mason 285199d8f83cSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 285299d8f83cSChris Mason return 0; 285399d8f83cSChris Mason 285499d8f83cSChris Mason /* try to push all the items before our slot into the next leaf */ 285599d8f83cSChris Mason slot = path->slots[0]; 285699d8f83cSChris Mason ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); 285799d8f83cSChris Mason if (ret < 0) 285899d8f83cSChris Mason return ret; 285999d8f83cSChris Mason 286099d8f83cSChris Mason if (ret == 0) 286199d8f83cSChris Mason progress++; 286299d8f83cSChris Mason 286399d8f83cSChris Mason if (progress) 286499d8f83cSChris Mason return 0; 286599d8f83cSChris Mason return 1; 286699d8f83cSChris Mason } 286799d8f83cSChris Mason 286899d8f83cSChris Mason /* 286944871b1bSChris Mason * split the path's leaf in two, making sure there is at least data_size 287044871b1bSChris Mason * available for the resulting leaf level of the path. 287144871b1bSChris Mason * 287244871b1bSChris Mason * returns 0 if all went well and < 0 on failure. 287344871b1bSChris Mason */ 287444871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 287544871b1bSChris Mason struct btrfs_root *root, 287644871b1bSChris Mason struct btrfs_key *ins_key, 287744871b1bSChris Mason struct btrfs_path *path, int data_size, 287844871b1bSChris Mason int extend) 287944871b1bSChris Mason { 28805d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 288144871b1bSChris Mason struct extent_buffer *l; 288244871b1bSChris Mason u32 nritems; 288344871b1bSChris Mason int mid; 288444871b1bSChris Mason int slot; 288544871b1bSChris Mason struct extent_buffer *right; 288644871b1bSChris Mason int ret = 0; 288744871b1bSChris Mason int wret; 28885d4f98a2SYan Zheng int split; 288944871b1bSChris Mason int num_doubles = 0; 289099d8f83cSChris Mason int tried_avoid_double = 0; 289144871b1bSChris Mason 2892a5719521SYan, Zheng l = path->nodes[0]; 2893a5719521SYan, Zheng slot = path->slots[0]; 2894a5719521SYan, Zheng if (extend && data_size + btrfs_item_size_nr(l, slot) + 2895a5719521SYan, Zheng sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) 2896a5719521SYan, Zheng return -EOVERFLOW; 2897a5719521SYan, Zheng 289844871b1bSChris Mason /* first try to make some room by pushing left and right */ 289999d8f83cSChris Mason if (data_size) { 290099d8f83cSChris Mason wret = push_leaf_right(trans, root, path, data_size, 290199d8f83cSChris Mason data_size, 0, 0); 290244871b1bSChris Mason if (wret < 0) 290344871b1bSChris Mason return wret; 290444871b1bSChris Mason if (wret) { 290599d8f83cSChris Mason wret = push_leaf_left(trans, root, path, data_size, 290699d8f83cSChris Mason data_size, 0, (u32)-1); 290744871b1bSChris Mason if (wret < 0) 290844871b1bSChris Mason return wret; 290944871b1bSChris Mason } 291044871b1bSChris Mason l = path->nodes[0]; 291144871b1bSChris Mason 291244871b1bSChris Mason /* did the pushes work? */ 291344871b1bSChris Mason if (btrfs_leaf_free_space(root, l) >= data_size) 291444871b1bSChris Mason return 0; 291544871b1bSChris Mason } 291644871b1bSChris Mason 291744871b1bSChris Mason if (!path->nodes[1]) { 291844871b1bSChris Mason ret = insert_new_root(trans, root, path, 1); 291944871b1bSChris Mason if (ret) 292044871b1bSChris Mason return ret; 292144871b1bSChris Mason } 292244871b1bSChris Mason again: 29235d4f98a2SYan Zheng split = 1; 292444871b1bSChris Mason l = path->nodes[0]; 292544871b1bSChris Mason slot = path->slots[0]; 292644871b1bSChris Mason nritems = btrfs_header_nritems(l); 292744871b1bSChris Mason mid = (nritems + 1) / 2; 292844871b1bSChris Mason 29295d4f98a2SYan Zheng if (mid <= slot) { 29305d4f98a2SYan Zheng if (nritems == 1 || 29315d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + data_size > 29325d4f98a2SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 29335d4f98a2SYan Zheng if (slot >= nritems) { 29345d4f98a2SYan Zheng split = 0; 29355d4f98a2SYan Zheng } else { 29365d4f98a2SYan Zheng mid = slot; 29375d4f98a2SYan Zheng if (mid != nritems && 29385d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 29395d4f98a2SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 294099d8f83cSChris Mason if (data_size && !tried_avoid_double) 294199d8f83cSChris Mason goto push_for_double; 29425d4f98a2SYan Zheng split = 2; 29435d4f98a2SYan Zheng } 29445d4f98a2SYan Zheng } 29455d4f98a2SYan Zheng } 29465d4f98a2SYan Zheng } else { 29475d4f98a2SYan Zheng if (leaf_space_used(l, 0, mid) + data_size > 29485d4f98a2SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 29495d4f98a2SYan Zheng if (!extend && data_size && slot == 0) { 29505d4f98a2SYan Zheng split = 0; 29515d4f98a2SYan Zheng } else if ((extend || !data_size) && slot == 0) { 29525d4f98a2SYan Zheng mid = 1; 29535d4f98a2SYan Zheng } else { 29545d4f98a2SYan Zheng mid = slot; 29555d4f98a2SYan Zheng if (mid != nritems && 29565d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 29575d4f98a2SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 295899d8f83cSChris Mason if (data_size && !tried_avoid_double) 295999d8f83cSChris Mason goto push_for_double; 29605d4f98a2SYan Zheng split = 2 ; 29615d4f98a2SYan Zheng } 29625d4f98a2SYan Zheng } 29635d4f98a2SYan Zheng } 29645d4f98a2SYan Zheng } 29655d4f98a2SYan Zheng 29665d4f98a2SYan Zheng if (split == 0) 29675d4f98a2SYan Zheng btrfs_cpu_key_to_disk(&disk_key, ins_key); 29685d4f98a2SYan Zheng else 29695d4f98a2SYan Zheng btrfs_item_key(l, &disk_key, mid); 29705d4f98a2SYan Zheng 29715d4f98a2SYan Zheng right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 297244871b1bSChris Mason root->root_key.objectid, 29735d4f98a2SYan Zheng &disk_key, 0, l->start, 0); 2974f0486c68SYan, Zheng if (IS_ERR(right)) 297544871b1bSChris Mason return PTR_ERR(right); 2976f0486c68SYan, Zheng 2977f0486c68SYan, Zheng root_add_used(root, root->leafsize); 297844871b1bSChris Mason 297944871b1bSChris Mason memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); 298044871b1bSChris Mason btrfs_set_header_bytenr(right, right->start); 298144871b1bSChris Mason btrfs_set_header_generation(right, trans->transid); 29825d4f98a2SYan Zheng btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); 298344871b1bSChris Mason btrfs_set_header_owner(right, root->root_key.objectid); 298444871b1bSChris Mason btrfs_set_header_level(right, 0); 298544871b1bSChris Mason write_extent_buffer(right, root->fs_info->fsid, 298644871b1bSChris Mason (unsigned long)btrfs_header_fsid(right), 298744871b1bSChris Mason BTRFS_FSID_SIZE); 298844871b1bSChris Mason 298944871b1bSChris Mason write_extent_buffer(right, root->fs_info->chunk_tree_uuid, 299044871b1bSChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(right), 299144871b1bSChris Mason BTRFS_UUID_SIZE); 299244871b1bSChris Mason 29935d4f98a2SYan Zheng if (split == 0) { 299444871b1bSChris Mason if (mid <= slot) { 299544871b1bSChris Mason btrfs_set_header_nritems(right, 0); 299644871b1bSChris Mason wret = insert_ptr(trans, root, path, 299744871b1bSChris Mason &disk_key, right->start, 299844871b1bSChris Mason path->slots[1] + 1, 1); 299944871b1bSChris Mason if (wret) 300044871b1bSChris Mason ret = wret; 300144871b1bSChris Mason 300244871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 300344871b1bSChris Mason free_extent_buffer(path->nodes[0]); 300444871b1bSChris Mason path->nodes[0] = right; 300544871b1bSChris Mason path->slots[0] = 0; 300644871b1bSChris Mason path->slots[1] += 1; 300744871b1bSChris Mason } else { 300844871b1bSChris Mason btrfs_set_header_nritems(right, 0); 300944871b1bSChris Mason wret = insert_ptr(trans, root, path, 301044871b1bSChris Mason &disk_key, 301144871b1bSChris Mason right->start, 301244871b1bSChris Mason path->slots[1], 1); 301344871b1bSChris Mason if (wret) 301444871b1bSChris Mason ret = wret; 301544871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 301644871b1bSChris Mason free_extent_buffer(path->nodes[0]); 301744871b1bSChris Mason path->nodes[0] = right; 301844871b1bSChris Mason path->slots[0] = 0; 301944871b1bSChris Mason if (path->slots[1] == 0) { 302044871b1bSChris Mason wret = fixup_low_keys(trans, root, 302144871b1bSChris Mason path, &disk_key, 1); 302244871b1bSChris Mason if (wret) 302344871b1bSChris Mason ret = wret; 302444871b1bSChris Mason } 30255d4f98a2SYan Zheng } 302644871b1bSChris Mason btrfs_mark_buffer_dirty(right); 302744871b1bSChris Mason return ret; 302844871b1bSChris Mason } 302944871b1bSChris Mason 303044871b1bSChris Mason ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems); 303144871b1bSChris Mason BUG_ON(ret); 303244871b1bSChris Mason 30335d4f98a2SYan Zheng if (split == 2) { 3034cc0c5538SChris Mason BUG_ON(num_doubles != 0); 3035cc0c5538SChris Mason num_doubles++; 3036cc0c5538SChris Mason goto again; 30373326d1b0SChris Mason } 303844871b1bSChris Mason 3039be0e5c09SChris Mason return ret; 304099d8f83cSChris Mason 304199d8f83cSChris Mason push_for_double: 304299d8f83cSChris Mason push_for_double_split(trans, root, path, data_size); 304399d8f83cSChris Mason tried_avoid_double = 1; 304499d8f83cSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 304599d8f83cSChris Mason return 0; 304699d8f83cSChris Mason goto again; 3047be0e5c09SChris Mason } 3048be0e5c09SChris Mason 3049ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, 3050ad48fd75SYan, Zheng struct btrfs_root *root, 3051ad48fd75SYan, Zheng struct btrfs_path *path, int ins_len) 3052ad48fd75SYan, Zheng { 3053ad48fd75SYan, Zheng struct btrfs_key key; 3054ad48fd75SYan, Zheng struct extent_buffer *leaf; 3055ad48fd75SYan, Zheng struct btrfs_file_extent_item *fi; 3056ad48fd75SYan, Zheng u64 extent_len = 0; 3057ad48fd75SYan, Zheng u32 item_size; 3058ad48fd75SYan, Zheng int ret; 3059ad48fd75SYan, Zheng 3060ad48fd75SYan, Zheng leaf = path->nodes[0]; 3061ad48fd75SYan, Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3062ad48fd75SYan, Zheng 3063ad48fd75SYan, Zheng BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 3064ad48fd75SYan, Zheng key.type != BTRFS_EXTENT_CSUM_KEY); 3065ad48fd75SYan, Zheng 3066ad48fd75SYan, Zheng if (btrfs_leaf_free_space(root, leaf) >= ins_len) 3067ad48fd75SYan, Zheng return 0; 3068ad48fd75SYan, Zheng 3069ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 3070ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 3071ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 3072ad48fd75SYan, Zheng struct btrfs_file_extent_item); 3073ad48fd75SYan, Zheng extent_len = btrfs_file_extent_num_bytes(leaf, fi); 3074ad48fd75SYan, Zheng } 3075b3b4aa74SDavid Sterba btrfs_release_path(path); 3076ad48fd75SYan, Zheng 3077ad48fd75SYan, Zheng path->keep_locks = 1; 3078ad48fd75SYan, Zheng path->search_for_split = 1; 3079ad48fd75SYan, Zheng ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 3080ad48fd75SYan, Zheng path->search_for_split = 0; 3081ad48fd75SYan, Zheng if (ret < 0) 3082ad48fd75SYan, Zheng goto err; 3083ad48fd75SYan, Zheng 3084ad48fd75SYan, Zheng ret = -EAGAIN; 3085ad48fd75SYan, Zheng leaf = path->nodes[0]; 3086ad48fd75SYan, Zheng /* if our item isn't there or got smaller, return now */ 3087ad48fd75SYan, Zheng if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) 3088ad48fd75SYan, Zheng goto err; 3089ad48fd75SYan, Zheng 3090109f6aefSChris Mason /* the leaf has changed, it now has room. return now */ 3091109f6aefSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) 3092109f6aefSChris Mason goto err; 3093109f6aefSChris Mason 3094ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 3095ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 3096ad48fd75SYan, Zheng struct btrfs_file_extent_item); 3097ad48fd75SYan, Zheng if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) 3098ad48fd75SYan, Zheng goto err; 3099ad48fd75SYan, Zheng } 3100ad48fd75SYan, Zheng 3101ad48fd75SYan, Zheng btrfs_set_path_blocking(path); 3102ad48fd75SYan, Zheng ret = split_leaf(trans, root, &key, path, ins_len, 1); 3103f0486c68SYan, Zheng if (ret) 3104f0486c68SYan, Zheng goto err; 3105ad48fd75SYan, Zheng 3106ad48fd75SYan, Zheng path->keep_locks = 0; 3107ad48fd75SYan, Zheng btrfs_unlock_up_safe(path, 1); 3108ad48fd75SYan, Zheng return 0; 3109ad48fd75SYan, Zheng err: 3110ad48fd75SYan, Zheng path->keep_locks = 0; 3111ad48fd75SYan, Zheng return ret; 3112ad48fd75SYan, Zheng } 3113ad48fd75SYan, Zheng 3114ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans, 3115459931ecSChris Mason struct btrfs_root *root, 3116459931ecSChris Mason struct btrfs_path *path, 3117459931ecSChris Mason struct btrfs_key *new_key, 3118459931ecSChris Mason unsigned long split_offset) 3119459931ecSChris Mason { 3120459931ecSChris Mason struct extent_buffer *leaf; 3121459931ecSChris Mason struct btrfs_item *item; 3122459931ecSChris Mason struct btrfs_item *new_item; 3123459931ecSChris Mason int slot; 3124ad48fd75SYan, Zheng char *buf; 3125459931ecSChris Mason u32 nritems; 3126ad48fd75SYan, Zheng u32 item_size; 3127459931ecSChris Mason u32 orig_offset; 3128459931ecSChris Mason struct btrfs_disk_key disk_key; 3129459931ecSChris Mason 3130459931ecSChris Mason leaf = path->nodes[0]; 3131b9473439SChris Mason BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); 3132b9473439SChris Mason 3133b4ce94deSChris Mason btrfs_set_path_blocking(path); 3134b4ce94deSChris Mason 3135459931ecSChris Mason item = btrfs_item_nr(leaf, path->slots[0]); 3136459931ecSChris Mason orig_offset = btrfs_item_offset(leaf, item); 3137459931ecSChris Mason item_size = btrfs_item_size(leaf, item); 3138459931ecSChris Mason 3139459931ecSChris Mason buf = kmalloc(item_size, GFP_NOFS); 3140ad48fd75SYan, Zheng if (!buf) 3141ad48fd75SYan, Zheng return -ENOMEM; 3142ad48fd75SYan, Zheng 3143459931ecSChris Mason read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, 3144459931ecSChris Mason path->slots[0]), item_size); 3145ad48fd75SYan, Zheng 3146459931ecSChris Mason slot = path->slots[0] + 1; 3147459931ecSChris Mason nritems = btrfs_header_nritems(leaf); 3148459931ecSChris Mason if (slot != nritems) { 3149459931ecSChris Mason /* shift the items */ 3150459931ecSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), 3151459931ecSChris Mason btrfs_item_nr_offset(slot), 3152459931ecSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 3153459931ecSChris Mason } 3154459931ecSChris Mason 3155459931ecSChris Mason btrfs_cpu_key_to_disk(&disk_key, new_key); 3156459931ecSChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 3157459931ecSChris Mason 3158459931ecSChris Mason new_item = btrfs_item_nr(leaf, slot); 3159459931ecSChris Mason 3160459931ecSChris Mason btrfs_set_item_offset(leaf, new_item, orig_offset); 3161459931ecSChris Mason btrfs_set_item_size(leaf, new_item, item_size - split_offset); 3162459931ecSChris Mason 3163459931ecSChris Mason btrfs_set_item_offset(leaf, item, 3164459931ecSChris Mason orig_offset + item_size - split_offset); 3165459931ecSChris Mason btrfs_set_item_size(leaf, item, split_offset); 3166459931ecSChris Mason 3167459931ecSChris Mason btrfs_set_header_nritems(leaf, nritems + 1); 3168459931ecSChris Mason 3169459931ecSChris Mason /* write the data for the start of the original item */ 3170459931ecSChris Mason write_extent_buffer(leaf, buf, 3171459931ecSChris Mason btrfs_item_ptr_offset(leaf, path->slots[0]), 3172459931ecSChris Mason split_offset); 3173459931ecSChris Mason 3174459931ecSChris Mason /* write the data for the new item */ 3175459931ecSChris Mason write_extent_buffer(leaf, buf + split_offset, 3176459931ecSChris Mason btrfs_item_ptr_offset(leaf, slot), 3177459931ecSChris Mason item_size - split_offset); 3178459931ecSChris Mason btrfs_mark_buffer_dirty(leaf); 3179459931ecSChris Mason 3180ad48fd75SYan, Zheng BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); 3181459931ecSChris Mason kfree(buf); 3182ad48fd75SYan, Zheng return 0; 3183ad48fd75SYan, Zheng } 3184ad48fd75SYan, Zheng 3185ad48fd75SYan, Zheng /* 3186ad48fd75SYan, Zheng * This function splits a single item into two items, 3187ad48fd75SYan, Zheng * giving 'new_key' to the new item and splitting the 3188ad48fd75SYan, Zheng * old one at split_offset (from the start of the item). 3189ad48fd75SYan, Zheng * 3190ad48fd75SYan, Zheng * The path may be released by this operation. After 3191ad48fd75SYan, Zheng * the split, the path is pointing to the old item. The 3192ad48fd75SYan, Zheng * new item is going to be in the same node as the old one. 3193ad48fd75SYan, Zheng * 3194ad48fd75SYan, Zheng * Note, the item being split must be smaller enough to live alone on 3195ad48fd75SYan, Zheng * a tree block with room for one extra struct btrfs_item 3196ad48fd75SYan, Zheng * 3197ad48fd75SYan, Zheng * This allows us to split the item in place, keeping a lock on the 3198ad48fd75SYan, Zheng * leaf the entire time. 3199ad48fd75SYan, Zheng */ 3200ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans, 3201ad48fd75SYan, Zheng struct btrfs_root *root, 3202ad48fd75SYan, Zheng struct btrfs_path *path, 3203ad48fd75SYan, Zheng struct btrfs_key *new_key, 3204ad48fd75SYan, Zheng unsigned long split_offset) 3205ad48fd75SYan, Zheng { 3206ad48fd75SYan, Zheng int ret; 3207ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 3208ad48fd75SYan, Zheng sizeof(struct btrfs_item)); 3209ad48fd75SYan, Zheng if (ret) 3210459931ecSChris Mason return ret; 3211ad48fd75SYan, Zheng 3212ad48fd75SYan, Zheng ret = split_item(trans, root, path, new_key, split_offset); 3213ad48fd75SYan, Zheng return ret; 3214ad48fd75SYan, Zheng } 3215ad48fd75SYan, Zheng 3216ad48fd75SYan, Zheng /* 3217ad48fd75SYan, Zheng * This function duplicate a item, giving 'new_key' to the new item. 3218ad48fd75SYan, Zheng * It guarantees both items live in the same tree leaf and the new item 3219ad48fd75SYan, Zheng * is contiguous with the original item. 3220ad48fd75SYan, Zheng * 3221ad48fd75SYan, Zheng * This allows us to split file extent in place, keeping a lock on the 3222ad48fd75SYan, Zheng * leaf the entire time. 3223ad48fd75SYan, Zheng */ 3224ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans, 3225ad48fd75SYan, Zheng struct btrfs_root *root, 3226ad48fd75SYan, Zheng struct btrfs_path *path, 3227ad48fd75SYan, Zheng struct btrfs_key *new_key) 3228ad48fd75SYan, Zheng { 3229ad48fd75SYan, Zheng struct extent_buffer *leaf; 3230ad48fd75SYan, Zheng int ret; 3231ad48fd75SYan, Zheng u32 item_size; 3232ad48fd75SYan, Zheng 3233ad48fd75SYan, Zheng leaf = path->nodes[0]; 3234ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 3235ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 3236ad48fd75SYan, Zheng item_size + sizeof(struct btrfs_item)); 3237ad48fd75SYan, Zheng if (ret) 3238ad48fd75SYan, Zheng return ret; 3239ad48fd75SYan, Zheng 3240ad48fd75SYan, Zheng path->slots[0]++; 3241ad48fd75SYan, Zheng ret = setup_items_for_insert(trans, root, path, new_key, &item_size, 3242ad48fd75SYan, Zheng item_size, item_size + 3243ad48fd75SYan, Zheng sizeof(struct btrfs_item), 1); 3244ad48fd75SYan, Zheng BUG_ON(ret); 3245ad48fd75SYan, Zheng 3246ad48fd75SYan, Zheng leaf = path->nodes[0]; 3247ad48fd75SYan, Zheng memcpy_extent_buffer(leaf, 3248ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0]), 3249ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0] - 1), 3250ad48fd75SYan, Zheng item_size); 3251ad48fd75SYan, Zheng return 0; 3252459931ecSChris Mason } 3253459931ecSChris Mason 3254459931ecSChris Mason /* 3255d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 3256d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 3257d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 3258d352ac68SChris Mason * the front. 3259d352ac68SChris Mason */ 3260b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans, 3261b18c6685SChris Mason struct btrfs_root *root, 3262b18c6685SChris Mason struct btrfs_path *path, 3263179e29e4SChris Mason u32 new_size, int from_end) 3264b18c6685SChris Mason { 3265b18c6685SChris Mason int slot; 32665f39d397SChris Mason struct extent_buffer *leaf; 32675f39d397SChris Mason struct btrfs_item *item; 3268b18c6685SChris Mason u32 nritems; 3269b18c6685SChris Mason unsigned int data_end; 3270b18c6685SChris Mason unsigned int old_data_start; 3271b18c6685SChris Mason unsigned int old_size; 3272b18c6685SChris Mason unsigned int size_diff; 3273b18c6685SChris Mason int i; 3274b18c6685SChris Mason 32755f39d397SChris Mason leaf = path->nodes[0]; 3276179e29e4SChris Mason slot = path->slots[0]; 3277179e29e4SChris Mason 3278179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 3279179e29e4SChris Mason if (old_size == new_size) 3280179e29e4SChris Mason return 0; 3281b18c6685SChris Mason 32825f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3283b18c6685SChris Mason data_end = leaf_data_end(root, leaf); 3284b18c6685SChris Mason 32855f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 3286179e29e4SChris Mason 3287b18c6685SChris Mason size_diff = old_size - new_size; 3288b18c6685SChris Mason 3289b18c6685SChris Mason BUG_ON(slot < 0); 3290b18c6685SChris Mason BUG_ON(slot >= nritems); 3291b18c6685SChris Mason 3292b18c6685SChris Mason /* 3293b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 3294b18c6685SChris Mason */ 3295b18c6685SChris Mason /* first correct the data pointers */ 3296b18c6685SChris Mason for (i = slot; i < nritems; i++) { 32975f39d397SChris Mason u32 ioff; 32985f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3299db94535dSChris Mason 33005f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 33015f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + size_diff); 3302b18c6685SChris Mason } 3303db94535dSChris Mason 3304b18c6685SChris Mason /* shift the data */ 3305179e29e4SChris Mason if (from_end) { 33065f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3307b18c6685SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 3308b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 3309179e29e4SChris Mason } else { 3310179e29e4SChris Mason struct btrfs_disk_key disk_key; 3311179e29e4SChris Mason u64 offset; 3312179e29e4SChris Mason 3313179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 3314179e29e4SChris Mason 3315179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 3316179e29e4SChris Mason unsigned long ptr; 3317179e29e4SChris Mason struct btrfs_file_extent_item *fi; 3318179e29e4SChris Mason 3319179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 3320179e29e4SChris Mason struct btrfs_file_extent_item); 3321179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 3322179e29e4SChris Mason (unsigned long)fi - size_diff); 3323179e29e4SChris Mason 3324179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 3325179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 3326179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 3327179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 3328179e29e4SChris Mason (unsigned long)fi, 3329179e29e4SChris Mason offsetof(struct btrfs_file_extent_item, 3330179e29e4SChris Mason disk_bytenr)); 3331179e29e4SChris Mason } 3332179e29e4SChris Mason } 3333179e29e4SChris Mason 3334179e29e4SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3335179e29e4SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 3336179e29e4SChris Mason data_end, old_data_start - data_end); 3337179e29e4SChris Mason 3338179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 3339179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 3340179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 3341179e29e4SChris Mason if (slot == 0) 3342179e29e4SChris Mason fixup_low_keys(trans, root, path, &disk_key, 1); 3343179e29e4SChris Mason } 33445f39d397SChris Mason 33455f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 33465f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 33475f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3348b18c6685SChris Mason 33495f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 33505f39d397SChris Mason btrfs_print_leaf(root, leaf); 3351b18c6685SChris Mason BUG(); 33525f39d397SChris Mason } 33531cd30799STsutomu Itoh return 0; 3354b18c6685SChris Mason } 3355b18c6685SChris Mason 3356d352ac68SChris Mason /* 3357d352ac68SChris Mason * make the item pointed to by the path bigger, data_size is the new size. 3358d352ac68SChris Mason */ 33595f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans, 33605f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 33615f39d397SChris Mason u32 data_size) 33626567e837SChris Mason { 33636567e837SChris Mason int slot; 33645f39d397SChris Mason struct extent_buffer *leaf; 33655f39d397SChris Mason struct btrfs_item *item; 33666567e837SChris Mason u32 nritems; 33676567e837SChris Mason unsigned int data_end; 33686567e837SChris Mason unsigned int old_data; 33696567e837SChris Mason unsigned int old_size; 33706567e837SChris Mason int i; 33716567e837SChris Mason 33725f39d397SChris Mason leaf = path->nodes[0]; 33736567e837SChris Mason 33745f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 33756567e837SChris Mason data_end = leaf_data_end(root, leaf); 33766567e837SChris Mason 33775f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < data_size) { 33785f39d397SChris Mason btrfs_print_leaf(root, leaf); 33796567e837SChris Mason BUG(); 33805f39d397SChris Mason } 33816567e837SChris Mason slot = path->slots[0]; 33825f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 33836567e837SChris Mason 33846567e837SChris Mason BUG_ON(slot < 0); 33853326d1b0SChris Mason if (slot >= nritems) { 33863326d1b0SChris Mason btrfs_print_leaf(root, leaf); 3387d397712bSChris Mason printk(KERN_CRIT "slot %d too large, nritems %d\n", 3388d397712bSChris Mason slot, nritems); 33893326d1b0SChris Mason BUG_ON(1); 33903326d1b0SChris Mason } 33916567e837SChris Mason 33926567e837SChris Mason /* 33936567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 33946567e837SChris Mason */ 33956567e837SChris Mason /* first correct the data pointers */ 33966567e837SChris Mason for (i = slot; i < nritems; i++) { 33975f39d397SChris Mason u32 ioff; 33985f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3399db94535dSChris Mason 34005f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 34015f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff - data_size); 34026567e837SChris Mason } 34035f39d397SChris Mason 34046567e837SChris Mason /* shift the data */ 34055f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 34066567e837SChris Mason data_end - data_size, btrfs_leaf_data(leaf) + 34076567e837SChris Mason data_end, old_data - data_end); 34085f39d397SChris Mason 34096567e837SChris Mason data_end = old_data; 34105f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 34115f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 34125f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 34135f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 34146567e837SChris Mason 34155f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 34165f39d397SChris Mason btrfs_print_leaf(root, leaf); 34176567e837SChris Mason BUG(); 34185f39d397SChris Mason } 34191cd30799STsutomu Itoh return 0; 34206567e837SChris Mason } 34216567e837SChris Mason 342274123bd7SChris Mason /* 3423d352ac68SChris Mason * Given a key and some data, insert items into the tree. 342474123bd7SChris Mason * This does all the path init required, making room in the tree if needed. 3425f3465ca4SJosef Bacik * Returns the number of keys that were inserted. 3426f3465ca4SJosef Bacik */ 3427f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans, 3428f3465ca4SJosef Bacik struct btrfs_root *root, 3429f3465ca4SJosef Bacik struct btrfs_path *path, 3430f3465ca4SJosef Bacik struct btrfs_key *cpu_key, u32 *data_size, 3431f3465ca4SJosef Bacik int nr) 3432f3465ca4SJosef Bacik { 3433f3465ca4SJosef Bacik struct extent_buffer *leaf; 3434f3465ca4SJosef Bacik struct btrfs_item *item; 3435f3465ca4SJosef Bacik int ret = 0; 3436f3465ca4SJosef Bacik int slot; 3437f3465ca4SJosef Bacik int i; 3438f3465ca4SJosef Bacik u32 nritems; 3439f3465ca4SJosef Bacik u32 total_data = 0; 3440f3465ca4SJosef Bacik u32 total_size = 0; 3441f3465ca4SJosef Bacik unsigned int data_end; 3442f3465ca4SJosef Bacik struct btrfs_disk_key disk_key; 3443f3465ca4SJosef Bacik struct btrfs_key found_key; 3444f3465ca4SJosef Bacik 344587b29b20SYan Zheng for (i = 0; i < nr; i++) { 344687b29b20SYan Zheng if (total_size + data_size[i] + sizeof(struct btrfs_item) > 344787b29b20SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 344887b29b20SYan Zheng break; 344987b29b20SYan Zheng nr = i; 345087b29b20SYan Zheng } 3451f3465ca4SJosef Bacik total_data += data_size[i]; 345287b29b20SYan Zheng total_size += data_size[i] + sizeof(struct btrfs_item); 345387b29b20SYan Zheng } 345487b29b20SYan Zheng BUG_ON(nr == 0); 3455f3465ca4SJosef Bacik 3456f3465ca4SJosef Bacik ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 3457f3465ca4SJosef Bacik if (ret == 0) 3458f3465ca4SJosef Bacik return -EEXIST; 3459f3465ca4SJosef Bacik if (ret < 0) 3460f3465ca4SJosef Bacik goto out; 3461f3465ca4SJosef Bacik 3462f3465ca4SJosef Bacik leaf = path->nodes[0]; 3463f3465ca4SJosef Bacik 3464f3465ca4SJosef Bacik nritems = btrfs_header_nritems(leaf); 3465f3465ca4SJosef Bacik data_end = leaf_data_end(root, leaf); 3466f3465ca4SJosef Bacik 3467f3465ca4SJosef Bacik if (btrfs_leaf_free_space(root, leaf) < total_size) { 3468f3465ca4SJosef Bacik for (i = nr; i >= 0; i--) { 3469f3465ca4SJosef Bacik total_data -= data_size[i]; 3470f3465ca4SJosef Bacik total_size -= data_size[i] + sizeof(struct btrfs_item); 3471f3465ca4SJosef Bacik if (total_size < btrfs_leaf_free_space(root, leaf)) 3472f3465ca4SJosef Bacik break; 3473f3465ca4SJosef Bacik } 3474f3465ca4SJosef Bacik nr = i; 3475f3465ca4SJosef Bacik } 3476f3465ca4SJosef Bacik 3477f3465ca4SJosef Bacik slot = path->slots[0]; 3478f3465ca4SJosef Bacik BUG_ON(slot < 0); 3479f3465ca4SJosef Bacik 3480f3465ca4SJosef Bacik if (slot != nritems) { 3481f3465ca4SJosef Bacik unsigned int old_data = btrfs_item_end_nr(leaf, slot); 3482f3465ca4SJosef Bacik 3483f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, slot); 3484f3465ca4SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 3485f3465ca4SJosef Bacik 3486f3465ca4SJosef Bacik /* figure out how many keys we can insert in here */ 3487f3465ca4SJosef Bacik total_data = data_size[0]; 3488f3465ca4SJosef Bacik for (i = 1; i < nr; i++) { 34895d4f98a2SYan Zheng if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0) 3490f3465ca4SJosef Bacik break; 3491f3465ca4SJosef Bacik total_data += data_size[i]; 3492f3465ca4SJosef Bacik } 3493f3465ca4SJosef Bacik nr = i; 3494f3465ca4SJosef Bacik 3495f3465ca4SJosef Bacik if (old_data < data_end) { 3496f3465ca4SJosef Bacik btrfs_print_leaf(root, leaf); 3497d397712bSChris Mason printk(KERN_CRIT "slot %d old_data %d data_end %d\n", 3498f3465ca4SJosef Bacik slot, old_data, data_end); 3499f3465ca4SJosef Bacik BUG_ON(1); 3500f3465ca4SJosef Bacik } 3501f3465ca4SJosef Bacik /* 3502f3465ca4SJosef Bacik * item0..itemN ... dataN.offset..dataN.size .. data0.size 3503f3465ca4SJosef Bacik */ 3504f3465ca4SJosef Bacik /* first correct the data pointers */ 3505f3465ca4SJosef Bacik for (i = slot; i < nritems; i++) { 3506f3465ca4SJosef Bacik u32 ioff; 3507f3465ca4SJosef Bacik 3508f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, i); 3509f3465ca4SJosef Bacik ioff = btrfs_item_offset(leaf, item); 3510f3465ca4SJosef Bacik btrfs_set_item_offset(leaf, item, ioff - total_data); 3511f3465ca4SJosef Bacik } 3512f3465ca4SJosef Bacik /* shift the items */ 3513f3465ca4SJosef Bacik memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 3514f3465ca4SJosef Bacik btrfs_item_nr_offset(slot), 3515f3465ca4SJosef Bacik (nritems - slot) * sizeof(struct btrfs_item)); 3516f3465ca4SJosef Bacik 3517f3465ca4SJosef Bacik /* shift the data */ 3518f3465ca4SJosef Bacik memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3519f3465ca4SJosef Bacik data_end - total_data, btrfs_leaf_data(leaf) + 3520f3465ca4SJosef Bacik data_end, old_data - data_end); 3521f3465ca4SJosef Bacik data_end = old_data; 3522f3465ca4SJosef Bacik } else { 3523f3465ca4SJosef Bacik /* 3524f3465ca4SJosef Bacik * this sucks but it has to be done, if we are inserting at 3525f3465ca4SJosef Bacik * the end of the leaf only insert 1 of the items, since we 3526f3465ca4SJosef Bacik * have no way of knowing whats on the next leaf and we'd have 3527f3465ca4SJosef Bacik * to drop our current locks to figure it out 3528f3465ca4SJosef Bacik */ 3529f3465ca4SJosef Bacik nr = 1; 3530f3465ca4SJosef Bacik } 3531f3465ca4SJosef Bacik 3532f3465ca4SJosef Bacik /* setup the item for the new data */ 3533f3465ca4SJosef Bacik for (i = 0; i < nr; i++) { 3534f3465ca4SJosef Bacik btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 3535f3465ca4SJosef Bacik btrfs_set_item_key(leaf, &disk_key, slot + i); 3536f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, slot + i); 3537f3465ca4SJosef Bacik btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 3538f3465ca4SJosef Bacik data_end -= data_size[i]; 3539f3465ca4SJosef Bacik btrfs_set_item_size(leaf, item, data_size[i]); 3540f3465ca4SJosef Bacik } 3541f3465ca4SJosef Bacik btrfs_set_header_nritems(leaf, nritems + nr); 3542f3465ca4SJosef Bacik btrfs_mark_buffer_dirty(leaf); 3543f3465ca4SJosef Bacik 3544f3465ca4SJosef Bacik ret = 0; 3545f3465ca4SJosef Bacik if (slot == 0) { 3546f3465ca4SJosef Bacik btrfs_cpu_key_to_disk(&disk_key, cpu_key); 3547f3465ca4SJosef Bacik ret = fixup_low_keys(trans, root, path, &disk_key, 1); 3548f3465ca4SJosef Bacik } 3549f3465ca4SJosef Bacik 3550f3465ca4SJosef Bacik if (btrfs_leaf_free_space(root, leaf) < 0) { 3551f3465ca4SJosef Bacik btrfs_print_leaf(root, leaf); 3552f3465ca4SJosef Bacik BUG(); 3553f3465ca4SJosef Bacik } 3554f3465ca4SJosef Bacik out: 3555f3465ca4SJosef Bacik if (!ret) 3556f3465ca4SJosef Bacik ret = nr; 3557f3465ca4SJosef Bacik return ret; 3558f3465ca4SJosef Bacik } 3559f3465ca4SJosef Bacik 3560f3465ca4SJosef Bacik /* 356144871b1bSChris Mason * this is a helper for btrfs_insert_empty_items, the main goal here is 356244871b1bSChris Mason * to save stack depth by doing the bulk of the work in a function 356344871b1bSChris Mason * that doesn't call btrfs_search_slot 356474123bd7SChris Mason */ 356516cdcec7SMiao Xie int setup_items_for_insert(struct btrfs_trans_handle *trans, 356644871b1bSChris Mason struct btrfs_root *root, struct btrfs_path *path, 35679c58309dSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 356844871b1bSChris Mason u32 total_data, u32 total_size, int nr) 3569be0e5c09SChris Mason { 35705f39d397SChris Mason struct btrfs_item *item; 35719c58309dSChris Mason int i; 35727518a238SChris Mason u32 nritems; 3573be0e5c09SChris Mason unsigned int data_end; 3574e2fa7227SChris Mason struct btrfs_disk_key disk_key; 357544871b1bSChris Mason int ret; 357644871b1bSChris Mason struct extent_buffer *leaf; 357744871b1bSChris Mason int slot; 3578e2fa7227SChris Mason 35795f39d397SChris Mason leaf = path->nodes[0]; 358044871b1bSChris Mason slot = path->slots[0]; 358174123bd7SChris Mason 35825f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3583123abc88SChris Mason data_end = leaf_data_end(root, leaf); 3584eb60ceacSChris Mason 3585f25956ccSChris Mason if (btrfs_leaf_free_space(root, leaf) < total_size) { 35863326d1b0SChris Mason btrfs_print_leaf(root, leaf); 3587d397712bSChris Mason printk(KERN_CRIT "not enough freespace need %u have %d\n", 35889c58309dSChris Mason total_size, btrfs_leaf_free_space(root, leaf)); 3589be0e5c09SChris Mason BUG(); 3590d4dbff95SChris Mason } 35915f39d397SChris Mason 3592be0e5c09SChris Mason if (slot != nritems) { 35935f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 3594be0e5c09SChris Mason 35955f39d397SChris Mason if (old_data < data_end) { 35965f39d397SChris Mason btrfs_print_leaf(root, leaf); 3597d397712bSChris Mason printk(KERN_CRIT "slot %d old_data %d data_end %d\n", 35985f39d397SChris Mason slot, old_data, data_end); 35995f39d397SChris Mason BUG_ON(1); 36005f39d397SChris Mason } 3601be0e5c09SChris Mason /* 3602be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 3603be0e5c09SChris Mason */ 3604be0e5c09SChris Mason /* first correct the data pointers */ 36050783fcfcSChris Mason for (i = slot; i < nritems; i++) { 36065f39d397SChris Mason u32 ioff; 3607db94535dSChris Mason 36085f39d397SChris Mason item = btrfs_item_nr(leaf, i); 36095f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 36109c58309dSChris Mason btrfs_set_item_offset(leaf, item, ioff - total_data); 36110783fcfcSChris Mason } 3612be0e5c09SChris Mason /* shift the items */ 36139c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 36145f39d397SChris Mason btrfs_item_nr_offset(slot), 36150783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 3616be0e5c09SChris Mason 3617be0e5c09SChris Mason /* shift the data */ 36185f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 36199c58309dSChris Mason data_end - total_data, btrfs_leaf_data(leaf) + 3620be0e5c09SChris Mason data_end, old_data - data_end); 3621be0e5c09SChris Mason data_end = old_data; 3622be0e5c09SChris Mason } 36235f39d397SChris Mason 362462e2749eSChris Mason /* setup the item for the new data */ 36259c58309dSChris Mason for (i = 0; i < nr; i++) { 36269c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 36279c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 36289c58309dSChris Mason item = btrfs_item_nr(leaf, slot + i); 36299c58309dSChris Mason btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 36309c58309dSChris Mason data_end -= data_size[i]; 36319c58309dSChris Mason btrfs_set_item_size(leaf, item, data_size[i]); 36329c58309dSChris Mason } 363344871b1bSChris Mason 36349c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 3635aa5d6bedSChris Mason 3636aa5d6bedSChris Mason ret = 0; 36375a01a2e3SChris Mason if (slot == 0) { 36385a01a2e3SChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key); 3639e089f05cSChris Mason ret = fixup_low_keys(trans, root, path, &disk_key, 1); 36405a01a2e3SChris Mason } 3641b9473439SChris Mason btrfs_unlock_up_safe(path, 1); 3642b9473439SChris Mason btrfs_mark_buffer_dirty(leaf); 3643aa5d6bedSChris Mason 36445f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 36455f39d397SChris Mason btrfs_print_leaf(root, leaf); 3646be0e5c09SChris Mason BUG(); 36475f39d397SChris Mason } 364844871b1bSChris Mason return ret; 364944871b1bSChris Mason } 365044871b1bSChris Mason 365144871b1bSChris Mason /* 365244871b1bSChris Mason * Given a key and some data, insert items into the tree. 365344871b1bSChris Mason * This does all the path init required, making room in the tree if needed. 365444871b1bSChris Mason */ 365544871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 365644871b1bSChris Mason struct btrfs_root *root, 365744871b1bSChris Mason struct btrfs_path *path, 365844871b1bSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 365944871b1bSChris Mason int nr) 366044871b1bSChris Mason { 366144871b1bSChris Mason int ret = 0; 366244871b1bSChris Mason int slot; 366344871b1bSChris Mason int i; 366444871b1bSChris Mason u32 total_size = 0; 366544871b1bSChris Mason u32 total_data = 0; 366644871b1bSChris Mason 366744871b1bSChris Mason for (i = 0; i < nr; i++) 366844871b1bSChris Mason total_data += data_size[i]; 366944871b1bSChris Mason 367044871b1bSChris Mason total_size = total_data + (nr * sizeof(struct btrfs_item)); 367144871b1bSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 367244871b1bSChris Mason if (ret == 0) 367344871b1bSChris Mason return -EEXIST; 367444871b1bSChris Mason if (ret < 0) 367544871b1bSChris Mason goto out; 367644871b1bSChris Mason 367744871b1bSChris Mason slot = path->slots[0]; 367844871b1bSChris Mason BUG_ON(slot < 0); 367944871b1bSChris Mason 368044871b1bSChris Mason ret = setup_items_for_insert(trans, root, path, cpu_key, data_size, 368144871b1bSChris Mason total_data, total_size, nr); 368244871b1bSChris Mason 3683ed2ff2cbSChris Mason out: 368462e2749eSChris Mason return ret; 368562e2749eSChris Mason } 368662e2749eSChris Mason 368762e2749eSChris Mason /* 368862e2749eSChris Mason * Given a key and some data, insert an item into the tree. 368962e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 369062e2749eSChris Mason */ 3691e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root 3692e089f05cSChris Mason *root, struct btrfs_key *cpu_key, void *data, u32 3693e089f05cSChris Mason data_size) 369462e2749eSChris Mason { 369562e2749eSChris Mason int ret = 0; 36962c90e5d6SChris Mason struct btrfs_path *path; 36975f39d397SChris Mason struct extent_buffer *leaf; 36985f39d397SChris Mason unsigned long ptr; 369962e2749eSChris Mason 37002c90e5d6SChris Mason path = btrfs_alloc_path(); 3701db5b493aSTsutomu Itoh if (!path) 3702db5b493aSTsutomu Itoh return -ENOMEM; 37032c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 370462e2749eSChris Mason if (!ret) { 37055f39d397SChris Mason leaf = path->nodes[0]; 37065f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 37075f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 37085f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 370962e2749eSChris Mason } 37102c90e5d6SChris Mason btrfs_free_path(path); 3711aa5d6bedSChris Mason return ret; 3712be0e5c09SChris Mason } 3713be0e5c09SChris Mason 371474123bd7SChris Mason /* 37155de08d7dSChris Mason * delete the pointer from a given node. 371674123bd7SChris Mason * 3717d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 3718d352ac68SChris Mason * empty a node. 371974123bd7SChris Mason */ 3720e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3721e089f05cSChris Mason struct btrfs_path *path, int level, int slot) 3722be0e5c09SChris Mason { 37235f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 37247518a238SChris Mason u32 nritems; 3725aa5d6bedSChris Mason int ret = 0; 3726bb803951SChris Mason int wret; 3727be0e5c09SChris Mason 37285f39d397SChris Mason nritems = btrfs_header_nritems(parent); 3729be0e5c09SChris Mason if (slot != nritems - 1) { 37305f39d397SChris Mason memmove_extent_buffer(parent, 37315f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 37325f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 3733d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 3734d6025579SChris Mason (nritems - slot - 1)); 3735be0e5c09SChris Mason } 37367518a238SChris Mason nritems--; 37375f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 37387518a238SChris Mason if (nritems == 0 && parent == root->node) { 37395f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 3740eb60ceacSChris Mason /* just turn the root into a leaf and break */ 37415f39d397SChris Mason btrfs_set_header_level(root->node, 0); 3742bb803951SChris Mason } else if (slot == 0) { 37435f39d397SChris Mason struct btrfs_disk_key disk_key; 37445f39d397SChris Mason 37455f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 37465f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); 37470f70abe2SChris Mason if (wret) 37480f70abe2SChris Mason ret = wret; 3749be0e5c09SChris Mason } 3750d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 3751aa5d6bedSChris Mason return ret; 3752be0e5c09SChris Mason } 3753be0e5c09SChris Mason 375474123bd7SChris Mason /* 3755323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 37565d4f98a2SYan Zheng * path->nodes[1]. 3757323ac95bSChris Mason * 3758323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 3759323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 3760323ac95bSChris Mason * 3761323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 3762323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 3763323ac95bSChris Mason */ 37645d4f98a2SYan Zheng static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans, 3765323ac95bSChris Mason struct btrfs_root *root, 37665d4f98a2SYan Zheng struct btrfs_path *path, 37675d4f98a2SYan Zheng struct extent_buffer *leaf) 3768323ac95bSChris Mason { 3769323ac95bSChris Mason int ret; 3770323ac95bSChris Mason 37715d4f98a2SYan Zheng WARN_ON(btrfs_header_generation(leaf) != trans->transid); 3772323ac95bSChris Mason ret = del_ptr(trans, root, path, 1, path->slots[1]); 3773323ac95bSChris Mason if (ret) 3774323ac95bSChris Mason return ret; 3775323ac95bSChris Mason 37764d081c41SChris Mason /* 37774d081c41SChris Mason * btrfs_free_extent is expensive, we want to make sure we 37784d081c41SChris Mason * aren't holding any locks when we call it 37794d081c41SChris Mason */ 37804d081c41SChris Mason btrfs_unlock_up_safe(path, 0); 37814d081c41SChris Mason 3782f0486c68SYan, Zheng root_sub_used(root, leaf->len); 3783f0486c68SYan, Zheng 3784f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, leaf, 0, 1); 3785f0486c68SYan, Zheng return 0; 3786323ac95bSChris Mason } 3787323ac95bSChris Mason /* 378874123bd7SChris Mason * delete the item at the leaf level in path. If that empties 378974123bd7SChris Mason * the leaf, remove it from the tree 379074123bd7SChris Mason */ 379185e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 379285e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 3793be0e5c09SChris Mason { 37945f39d397SChris Mason struct extent_buffer *leaf; 37955f39d397SChris Mason struct btrfs_item *item; 379685e21bacSChris Mason int last_off; 379785e21bacSChris Mason int dsize = 0; 3798aa5d6bedSChris Mason int ret = 0; 3799aa5d6bedSChris Mason int wret; 380085e21bacSChris Mason int i; 38017518a238SChris Mason u32 nritems; 3802be0e5c09SChris Mason 38035f39d397SChris Mason leaf = path->nodes[0]; 380485e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 380585e21bacSChris Mason 380685e21bacSChris Mason for (i = 0; i < nr; i++) 380785e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 380885e21bacSChris Mason 38095f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3810be0e5c09SChris Mason 381185e21bacSChris Mason if (slot + nr != nritems) { 3812123abc88SChris Mason int data_end = leaf_data_end(root, leaf); 38135f39d397SChris Mason 38145f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3815d6025579SChris Mason data_end + dsize, 3816123abc88SChris Mason btrfs_leaf_data(leaf) + data_end, 381785e21bacSChris Mason last_off - data_end); 38185f39d397SChris Mason 381985e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 38205f39d397SChris Mason u32 ioff; 3821db94535dSChris Mason 38225f39d397SChris Mason item = btrfs_item_nr(leaf, i); 38235f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 38245f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + dsize); 38250783fcfcSChris Mason } 3826db94535dSChris Mason 38275f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 382885e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 38290783fcfcSChris Mason sizeof(struct btrfs_item) * 383085e21bacSChris Mason (nritems - slot - nr)); 3831be0e5c09SChris Mason } 383285e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 383385e21bacSChris Mason nritems -= nr; 38345f39d397SChris Mason 383574123bd7SChris Mason /* delete the leaf if we've emptied it */ 38367518a238SChris Mason if (nritems == 0) { 38375f39d397SChris Mason if (leaf == root->node) { 38385f39d397SChris Mason btrfs_set_header_level(leaf, 0); 38399a8dd150SChris Mason } else { 3840f0486c68SYan, Zheng btrfs_set_path_blocking(path); 3841f0486c68SYan, Zheng clean_tree_block(trans, root, leaf); 38425d4f98a2SYan Zheng ret = btrfs_del_leaf(trans, root, path, leaf); 3843323ac95bSChris Mason BUG_ON(ret); 38449a8dd150SChris Mason } 3845be0e5c09SChris Mason } else { 38467518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 3847aa5d6bedSChris Mason if (slot == 0) { 38485f39d397SChris Mason struct btrfs_disk_key disk_key; 38495f39d397SChris Mason 38505f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 3851e089f05cSChris Mason wret = fixup_low_keys(trans, root, path, 38525f39d397SChris Mason &disk_key, 1); 3853aa5d6bedSChris Mason if (wret) 3854aa5d6bedSChris Mason ret = wret; 3855aa5d6bedSChris Mason } 3856aa5d6bedSChris Mason 385774123bd7SChris Mason /* delete the leaf if it is mostly empty */ 3858d717aa1dSYan Zheng if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { 3859be0e5c09SChris Mason /* push_leaf_left fixes the path. 3860be0e5c09SChris Mason * make sure the path still points to our leaf 3861be0e5c09SChris Mason * for possible call to del_ptr below 3862be0e5c09SChris Mason */ 38634920c9acSChris Mason slot = path->slots[1]; 38645f39d397SChris Mason extent_buffer_get(leaf); 38655f39d397SChris Mason 3866b9473439SChris Mason btrfs_set_path_blocking(path); 386799d8f83cSChris Mason wret = push_leaf_left(trans, root, path, 1, 1, 386899d8f83cSChris Mason 1, (u32)-1); 386954aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3870aa5d6bedSChris Mason ret = wret; 38715f39d397SChris Mason 38725f39d397SChris Mason if (path->nodes[0] == leaf && 38735f39d397SChris Mason btrfs_header_nritems(leaf)) { 387499d8f83cSChris Mason wret = push_leaf_right(trans, root, path, 1, 387599d8f83cSChris Mason 1, 1, 0); 387654aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3877aa5d6bedSChris Mason ret = wret; 3878aa5d6bedSChris Mason } 38795f39d397SChris Mason 38805f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 3881323ac95bSChris Mason path->slots[1] = slot; 38825d4f98a2SYan Zheng ret = btrfs_del_leaf(trans, root, path, leaf); 3883323ac95bSChris Mason BUG_ON(ret); 38845f39d397SChris Mason free_extent_buffer(leaf); 38855de08d7dSChris Mason } else { 3886925baeddSChris Mason /* if we're still in the path, make sure 3887925baeddSChris Mason * we're dirty. Otherwise, one of the 3888925baeddSChris Mason * push_leaf functions must have already 3889925baeddSChris Mason * dirtied this buffer 3890925baeddSChris Mason */ 3891925baeddSChris Mason if (path->nodes[0] == leaf) 38925f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 38935f39d397SChris Mason free_extent_buffer(leaf); 3894be0e5c09SChris Mason } 3895d5719762SChris Mason } else { 38965f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3897be0e5c09SChris Mason } 38989a8dd150SChris Mason } 3899aa5d6bedSChris Mason return ret; 39009a8dd150SChris Mason } 39019a8dd150SChris Mason 390297571fd0SChris Mason /* 3903925baeddSChris Mason * search the tree again to find a leaf with lesser keys 39047bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 39057bb86316SChris Mason * returns < 0 on io errors. 3906d352ac68SChris Mason * 3907d352ac68SChris Mason * This may release the path, and so you may lose any locks held at the 3908d352ac68SChris Mason * time you call it. 39097bb86316SChris Mason */ 39107bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 39117bb86316SChris Mason { 3912925baeddSChris Mason struct btrfs_key key; 3913925baeddSChris Mason struct btrfs_disk_key found_key; 3914925baeddSChris Mason int ret; 39157bb86316SChris Mason 3916925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 3917925baeddSChris Mason 3918925baeddSChris Mason if (key.offset > 0) 3919925baeddSChris Mason key.offset--; 3920925baeddSChris Mason else if (key.type > 0) 3921925baeddSChris Mason key.type--; 3922925baeddSChris Mason else if (key.objectid > 0) 3923925baeddSChris Mason key.objectid--; 3924925baeddSChris Mason else 39257bb86316SChris Mason return 1; 39267bb86316SChris Mason 3927b3b4aa74SDavid Sterba btrfs_release_path(path); 3928925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 3929925baeddSChris Mason if (ret < 0) 3930925baeddSChris Mason return ret; 3931925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 3932925baeddSChris Mason ret = comp_keys(&found_key, &key); 3933925baeddSChris Mason if (ret < 0) 39347bb86316SChris Mason return 0; 3935925baeddSChris Mason return 1; 39367bb86316SChris Mason } 39377bb86316SChris Mason 39383f157a2fSChris Mason /* 39393f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 39403f157a2fSChris Mason * for nodes or leaves that are either in cache or have a minimum 3941d352ac68SChris Mason * transaction id. This is used by the btree defrag code, and tree logging 39423f157a2fSChris Mason * 39433f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 39443f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 39453f157a2fSChris Mason * key and get a writable path. 39463f157a2fSChris Mason * 39473f157a2fSChris Mason * This does lock as it descends, and path->keep_locks should be set 39483f157a2fSChris Mason * to 1 by the caller. 39493f157a2fSChris Mason * 39503f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 39513f157a2fSChris Mason * of the tree. 39523f157a2fSChris Mason * 3953d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 3954d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 3955d352ac68SChris Mason * skipped over (without reading them). 3956d352ac68SChris Mason * 39573f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 39583f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 39593f157a2fSChris Mason */ 39603f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 3961e02119d5SChris Mason struct btrfs_key *max_key, 39623f157a2fSChris Mason struct btrfs_path *path, int cache_only, 39633f157a2fSChris Mason u64 min_trans) 39643f157a2fSChris Mason { 39653f157a2fSChris Mason struct extent_buffer *cur; 39663f157a2fSChris Mason struct btrfs_key found_key; 39673f157a2fSChris Mason int slot; 39689652480bSYan int sret; 39693f157a2fSChris Mason u32 nritems; 39703f157a2fSChris Mason int level; 39713f157a2fSChris Mason int ret = 1; 39723f157a2fSChris Mason 3973934d375bSChris Mason WARN_ON(!path->keep_locks); 39743f157a2fSChris Mason again: 3975bd681513SChris Mason cur = btrfs_read_lock_root_node(root); 39763f157a2fSChris Mason level = btrfs_header_level(cur); 3977e02119d5SChris Mason WARN_ON(path->nodes[level]); 39783f157a2fSChris Mason path->nodes[level] = cur; 3979bd681513SChris Mason path->locks[level] = BTRFS_READ_LOCK; 39803f157a2fSChris Mason 39813f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 39823f157a2fSChris Mason ret = 1; 39833f157a2fSChris Mason goto out; 39843f157a2fSChris Mason } 39853f157a2fSChris Mason while (1) { 39863f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 39873f157a2fSChris Mason level = btrfs_header_level(cur); 39889652480bSYan sret = bin_search(cur, min_key, level, &slot); 39893f157a2fSChris Mason 3990323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 3991323ac95bSChris Mason if (level == path->lowest_level) { 3992e02119d5SChris Mason if (slot >= nritems) 3993e02119d5SChris Mason goto find_next_key; 39943f157a2fSChris Mason ret = 0; 39953f157a2fSChris Mason path->slots[level] = slot; 39963f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 39973f157a2fSChris Mason goto out; 39983f157a2fSChris Mason } 39999652480bSYan if (sret && slot > 0) 40009652480bSYan slot--; 40013f157a2fSChris Mason /* 40023f157a2fSChris Mason * check this node pointer against the cache_only and 40033f157a2fSChris Mason * min_trans parameters. If it isn't in cache or is too 40043f157a2fSChris Mason * old, skip to the next one. 40053f157a2fSChris Mason */ 40063f157a2fSChris Mason while (slot < nritems) { 40073f157a2fSChris Mason u64 blockptr; 40083f157a2fSChris Mason u64 gen; 40093f157a2fSChris Mason struct extent_buffer *tmp; 4010e02119d5SChris Mason struct btrfs_disk_key disk_key; 4011e02119d5SChris Mason 40123f157a2fSChris Mason blockptr = btrfs_node_blockptr(cur, slot); 40133f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 40143f157a2fSChris Mason if (gen < min_trans) { 40153f157a2fSChris Mason slot++; 40163f157a2fSChris Mason continue; 40173f157a2fSChris Mason } 40183f157a2fSChris Mason if (!cache_only) 40193f157a2fSChris Mason break; 40203f157a2fSChris Mason 4021e02119d5SChris Mason if (max_key) { 4022e02119d5SChris Mason btrfs_node_key(cur, &disk_key, slot); 4023e02119d5SChris Mason if (comp_keys(&disk_key, max_key) >= 0) { 4024e02119d5SChris Mason ret = 1; 4025e02119d5SChris Mason goto out; 4026e02119d5SChris Mason } 4027e02119d5SChris Mason } 4028e02119d5SChris Mason 40293f157a2fSChris Mason tmp = btrfs_find_tree_block(root, blockptr, 40303f157a2fSChris Mason btrfs_level_size(root, level - 1)); 40313f157a2fSChris Mason 40323f157a2fSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 40333f157a2fSChris Mason free_extent_buffer(tmp); 40343f157a2fSChris Mason break; 40353f157a2fSChris Mason } 40363f157a2fSChris Mason if (tmp) 40373f157a2fSChris Mason free_extent_buffer(tmp); 40383f157a2fSChris Mason slot++; 40393f157a2fSChris Mason } 4040e02119d5SChris Mason find_next_key: 40413f157a2fSChris Mason /* 40423f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 40433f157a2fSChris Mason * and find another one 40443f157a2fSChris Mason */ 40453f157a2fSChris Mason if (slot >= nritems) { 4046e02119d5SChris Mason path->slots[level] = slot; 4047b4ce94deSChris Mason btrfs_set_path_blocking(path); 4048e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 40493f157a2fSChris Mason cache_only, min_trans); 4050e02119d5SChris Mason if (sret == 0) { 4051b3b4aa74SDavid Sterba btrfs_release_path(path); 40523f157a2fSChris Mason goto again; 40533f157a2fSChris Mason } else { 40543f157a2fSChris Mason goto out; 40553f157a2fSChris Mason } 40563f157a2fSChris Mason } 40573f157a2fSChris Mason /* save our key for returning back */ 40583f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 40593f157a2fSChris Mason path->slots[level] = slot; 40603f157a2fSChris Mason if (level == path->lowest_level) { 40613f157a2fSChris Mason ret = 0; 40623f157a2fSChris Mason unlock_up(path, level, 1); 40633f157a2fSChris Mason goto out; 40643f157a2fSChris Mason } 4065b4ce94deSChris Mason btrfs_set_path_blocking(path); 40663f157a2fSChris Mason cur = read_node_slot(root, cur, slot); 406797d9a8a4STsutomu Itoh BUG_ON(!cur); 40683f157a2fSChris Mason 4069bd681513SChris Mason btrfs_tree_read_lock(cur); 4070b4ce94deSChris Mason 4071bd681513SChris Mason path->locks[level - 1] = BTRFS_READ_LOCK; 40723f157a2fSChris Mason path->nodes[level - 1] = cur; 40733f157a2fSChris Mason unlock_up(path, level, 1); 4074bd681513SChris Mason btrfs_clear_path_blocking(path, NULL, 0); 40753f157a2fSChris Mason } 40763f157a2fSChris Mason out: 40773f157a2fSChris Mason if (ret == 0) 40783f157a2fSChris Mason memcpy(min_key, &found_key, sizeof(found_key)); 4079b4ce94deSChris Mason btrfs_set_path_blocking(path); 40803f157a2fSChris Mason return ret; 40813f157a2fSChris Mason } 40823f157a2fSChris Mason 40833f157a2fSChris Mason /* 40843f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 40853f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 40863f157a2fSChris Mason * tree based on the current path and the cache_only and min_trans 40873f157a2fSChris Mason * parameters. 40883f157a2fSChris Mason * 40893f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 40903f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 40913f157a2fSChris Mason * 40923f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 40933f157a2fSChris Mason * calling this function. 40943f157a2fSChris Mason */ 4095e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 409633c66f43SYan Zheng struct btrfs_key *key, int level, 40973f157a2fSChris Mason int cache_only, u64 min_trans) 4098e7a84565SChris Mason { 4099e7a84565SChris Mason int slot; 4100e7a84565SChris Mason struct extent_buffer *c; 4101e7a84565SChris Mason 4102934d375bSChris Mason WARN_ON(!path->keep_locks); 4103e7a84565SChris Mason while (level < BTRFS_MAX_LEVEL) { 4104e7a84565SChris Mason if (!path->nodes[level]) 4105e7a84565SChris Mason return 1; 4106e7a84565SChris Mason 4107e7a84565SChris Mason slot = path->slots[level] + 1; 4108e7a84565SChris Mason c = path->nodes[level]; 41093f157a2fSChris Mason next: 4110e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 411133c66f43SYan Zheng int ret; 411233c66f43SYan Zheng int orig_lowest; 411333c66f43SYan Zheng struct btrfs_key cur_key; 411433c66f43SYan Zheng if (level + 1 >= BTRFS_MAX_LEVEL || 411533c66f43SYan Zheng !path->nodes[level + 1]) 4116e7a84565SChris Mason return 1; 411733c66f43SYan Zheng 411833c66f43SYan Zheng if (path->locks[level + 1]) { 411933c66f43SYan Zheng level++; 4120e7a84565SChris Mason continue; 4121e7a84565SChris Mason } 412233c66f43SYan Zheng 412333c66f43SYan Zheng slot = btrfs_header_nritems(c) - 1; 412433c66f43SYan Zheng if (level == 0) 412533c66f43SYan Zheng btrfs_item_key_to_cpu(c, &cur_key, slot); 412633c66f43SYan Zheng else 412733c66f43SYan Zheng btrfs_node_key_to_cpu(c, &cur_key, slot); 412833c66f43SYan Zheng 412933c66f43SYan Zheng orig_lowest = path->lowest_level; 4130b3b4aa74SDavid Sterba btrfs_release_path(path); 413133c66f43SYan Zheng path->lowest_level = level; 413233c66f43SYan Zheng ret = btrfs_search_slot(NULL, root, &cur_key, path, 413333c66f43SYan Zheng 0, 0); 413433c66f43SYan Zheng path->lowest_level = orig_lowest; 413533c66f43SYan Zheng if (ret < 0) 413633c66f43SYan Zheng return ret; 413733c66f43SYan Zheng 413833c66f43SYan Zheng c = path->nodes[level]; 413933c66f43SYan Zheng slot = path->slots[level]; 414033c66f43SYan Zheng if (ret == 0) 414133c66f43SYan Zheng slot++; 414233c66f43SYan Zheng goto next; 414333c66f43SYan Zheng } 414433c66f43SYan Zheng 4145e7a84565SChris Mason if (level == 0) 4146e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 41473f157a2fSChris Mason else { 41483f157a2fSChris Mason u64 blockptr = btrfs_node_blockptr(c, slot); 41493f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 41503f157a2fSChris Mason 41513f157a2fSChris Mason if (cache_only) { 41523f157a2fSChris Mason struct extent_buffer *cur; 41533f157a2fSChris Mason cur = btrfs_find_tree_block(root, blockptr, 41543f157a2fSChris Mason btrfs_level_size(root, level - 1)); 41553f157a2fSChris Mason if (!cur || !btrfs_buffer_uptodate(cur, gen)) { 41563f157a2fSChris Mason slot++; 41573f157a2fSChris Mason if (cur) 41583f157a2fSChris Mason free_extent_buffer(cur); 41593f157a2fSChris Mason goto next; 41603f157a2fSChris Mason } 41613f157a2fSChris Mason free_extent_buffer(cur); 41623f157a2fSChris Mason } 41633f157a2fSChris Mason if (gen < min_trans) { 41643f157a2fSChris Mason slot++; 41653f157a2fSChris Mason goto next; 41663f157a2fSChris Mason } 4167e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 41683f157a2fSChris Mason } 4169e7a84565SChris Mason return 0; 4170e7a84565SChris Mason } 4171e7a84565SChris Mason return 1; 4172e7a84565SChris Mason } 4173e7a84565SChris Mason 41747bb86316SChris Mason /* 4175925baeddSChris Mason * search the tree again to find a leaf with greater keys 41760f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 41770f70abe2SChris Mason * returns < 0 on io errors. 417897571fd0SChris Mason */ 4179234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 4180d97e63b6SChris Mason { 4181d97e63b6SChris Mason int slot; 41828e73f275SChris Mason int level; 41835f39d397SChris Mason struct extent_buffer *c; 41848e73f275SChris Mason struct extent_buffer *next; 4185925baeddSChris Mason struct btrfs_key key; 4186925baeddSChris Mason u32 nritems; 4187925baeddSChris Mason int ret; 41888e73f275SChris Mason int old_spinning = path->leave_spinning; 4189bd681513SChris Mason int next_rw_lock = 0; 4190925baeddSChris Mason 4191925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4192d397712bSChris Mason if (nritems == 0) 4193925baeddSChris Mason return 1; 4194925baeddSChris Mason 41958e73f275SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 41968e73f275SChris Mason again: 41978e73f275SChris Mason level = 1; 41988e73f275SChris Mason next = NULL; 4199bd681513SChris Mason next_rw_lock = 0; 4200b3b4aa74SDavid Sterba btrfs_release_path(path); 42018e73f275SChris Mason 4202a2135011SChris Mason path->keep_locks = 1; 42038e73f275SChris Mason path->leave_spinning = 1; 42048e73f275SChris Mason 4205925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4206925baeddSChris Mason path->keep_locks = 0; 4207925baeddSChris Mason 4208925baeddSChris Mason if (ret < 0) 4209925baeddSChris Mason return ret; 4210925baeddSChris Mason 4211a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4212168fd7d2SChris Mason /* 4213168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 4214168fd7d2SChris Mason * could have added more items next to the key that used to be 4215168fd7d2SChris Mason * at the very end of the block. So, check again here and 4216168fd7d2SChris Mason * advance the path if there are now more items available. 4217168fd7d2SChris Mason */ 4218a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 4219e457afecSYan Zheng if (ret == 0) 4220168fd7d2SChris Mason path->slots[0]++; 42218e73f275SChris Mason ret = 0; 4222925baeddSChris Mason goto done; 4223925baeddSChris Mason } 4224d97e63b6SChris Mason 4225234b63a0SChris Mason while (level < BTRFS_MAX_LEVEL) { 42268e73f275SChris Mason if (!path->nodes[level]) { 42278e73f275SChris Mason ret = 1; 42288e73f275SChris Mason goto done; 42298e73f275SChris Mason } 42305f39d397SChris Mason 4231d97e63b6SChris Mason slot = path->slots[level] + 1; 4232d97e63b6SChris Mason c = path->nodes[level]; 42335f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 4234d97e63b6SChris Mason level++; 42358e73f275SChris Mason if (level == BTRFS_MAX_LEVEL) { 42368e73f275SChris Mason ret = 1; 42378e73f275SChris Mason goto done; 42388e73f275SChris Mason } 4239d97e63b6SChris Mason continue; 4240d97e63b6SChris Mason } 42415f39d397SChris Mason 4242925baeddSChris Mason if (next) { 4243bd681513SChris Mason btrfs_tree_unlock_rw(next, next_rw_lock); 42445f39d397SChris Mason free_extent_buffer(next); 4245925baeddSChris Mason } 42465f39d397SChris Mason 42478e73f275SChris Mason next = c; 4248bd681513SChris Mason next_rw_lock = path->locks[level]; 42498e73f275SChris Mason ret = read_block_for_search(NULL, root, path, &next, level, 42508e73f275SChris Mason slot, &key); 42518e73f275SChris Mason if (ret == -EAGAIN) 42528e73f275SChris Mason goto again; 42535f39d397SChris Mason 425476a05b35SChris Mason if (ret < 0) { 4255b3b4aa74SDavid Sterba btrfs_release_path(path); 425676a05b35SChris Mason goto done; 425776a05b35SChris Mason } 425876a05b35SChris Mason 42595cd57b2cSChris Mason if (!path->skip_locking) { 4260bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 42618e73f275SChris Mason if (!ret) { 42628e73f275SChris Mason btrfs_set_path_blocking(path); 4263bd681513SChris Mason btrfs_tree_read_lock(next); 4264bd681513SChris Mason btrfs_clear_path_blocking(path, next, 4265bd681513SChris Mason BTRFS_READ_LOCK); 42668e73f275SChris Mason } 4267bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 4268bd681513SChris Mason } 4269d97e63b6SChris Mason break; 4270d97e63b6SChris Mason } 4271d97e63b6SChris Mason path->slots[level] = slot; 4272d97e63b6SChris Mason while (1) { 4273d97e63b6SChris Mason level--; 4274d97e63b6SChris Mason c = path->nodes[level]; 4275925baeddSChris Mason if (path->locks[level]) 4276bd681513SChris Mason btrfs_tree_unlock_rw(c, path->locks[level]); 42778e73f275SChris Mason 42785f39d397SChris Mason free_extent_buffer(c); 4279d97e63b6SChris Mason path->nodes[level] = next; 4280d97e63b6SChris Mason path->slots[level] = 0; 4281a74a4b97SChris Mason if (!path->skip_locking) 4282bd681513SChris Mason path->locks[level] = next_rw_lock; 4283d97e63b6SChris Mason if (!level) 4284d97e63b6SChris Mason break; 4285b4ce94deSChris Mason 42868e73f275SChris Mason ret = read_block_for_search(NULL, root, path, &next, level, 42878e73f275SChris Mason 0, &key); 42888e73f275SChris Mason if (ret == -EAGAIN) 42898e73f275SChris Mason goto again; 42908e73f275SChris Mason 429176a05b35SChris Mason if (ret < 0) { 4292b3b4aa74SDavid Sterba btrfs_release_path(path); 429376a05b35SChris Mason goto done; 429476a05b35SChris Mason } 429576a05b35SChris Mason 42965cd57b2cSChris Mason if (!path->skip_locking) { 4297bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 42988e73f275SChris Mason if (!ret) { 42998e73f275SChris Mason btrfs_set_path_blocking(path); 4300bd681513SChris Mason btrfs_tree_read_lock(next); 4301bd681513SChris Mason btrfs_clear_path_blocking(path, next, 4302bd681513SChris Mason BTRFS_READ_LOCK); 43038e73f275SChris Mason } 4304bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 4305bd681513SChris Mason } 4306d97e63b6SChris Mason } 43078e73f275SChris Mason ret = 0; 4308925baeddSChris Mason done: 4309925baeddSChris Mason unlock_up(path, 0, 1); 43108e73f275SChris Mason path->leave_spinning = old_spinning; 43118e73f275SChris Mason if (!old_spinning) 43128e73f275SChris Mason btrfs_set_path_blocking(path); 43138e73f275SChris Mason 43148e73f275SChris Mason return ret; 4315d97e63b6SChris Mason } 43160b86a832SChris Mason 43173f157a2fSChris Mason /* 43183f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 43193f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 43203f157a2fSChris Mason * 43213f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 43223f157a2fSChris Mason */ 43230b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 43240b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 43250b86a832SChris Mason int type) 43260b86a832SChris Mason { 43270b86a832SChris Mason struct btrfs_key found_key; 43280b86a832SChris Mason struct extent_buffer *leaf; 4329e02119d5SChris Mason u32 nritems; 43300b86a832SChris Mason int ret; 43310b86a832SChris Mason 43320b86a832SChris Mason while (1) { 43330b86a832SChris Mason if (path->slots[0] == 0) { 4334b4ce94deSChris Mason btrfs_set_path_blocking(path); 43350b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 43360b86a832SChris Mason if (ret != 0) 43370b86a832SChris Mason return ret; 43380b86a832SChris Mason } else { 43390b86a832SChris Mason path->slots[0]--; 43400b86a832SChris Mason } 43410b86a832SChris Mason leaf = path->nodes[0]; 4342e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 4343e02119d5SChris Mason if (nritems == 0) 4344e02119d5SChris Mason return 1; 4345e02119d5SChris Mason if (path->slots[0] == nritems) 4346e02119d5SChris Mason path->slots[0]--; 4347e02119d5SChris Mason 43480b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 4349e02119d5SChris Mason if (found_key.objectid < min_objectid) 4350e02119d5SChris Mason break; 43510a4eefbbSYan Zheng if (found_key.type == type) 43520a4eefbbSYan Zheng return 0; 4353e02119d5SChris Mason if (found_key.objectid == min_objectid && 4354e02119d5SChris Mason found_key.type < type) 4355e02119d5SChris Mason break; 43560b86a832SChris Mason } 43570b86a832SChris Mason return 1; 43580b86a832SChris Mason } 4359