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); 41ad48fd75SYan, Zheng static int setup_items_for_insert(struct btrfs_trans_handle *trans, 42ad48fd75SYan, Zheng struct btrfs_root *root, struct btrfs_path *path, 43ad48fd75SYan, Zheng struct btrfs_key *cpu_key, u32 *data_size, 44ad48fd75SYan, Zheng u32 total_data, u32 total_size, int nr); 45ad48fd75SYan, Zheng 46d97e63b6SChris Mason 472c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 482c90e5d6SChris Mason { 49df24a2b9SChris Mason struct btrfs_path *path; 50e00f7308SJeff Mahoney path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 51e00f7308SJeff Mahoney if (path) 522cc58cf2SChris Mason path->reada = 1; 53df24a2b9SChris Mason return path; 542c90e5d6SChris Mason } 552c90e5d6SChris Mason 56b4ce94deSChris Mason /* 57b4ce94deSChris Mason * set all locked nodes in the path to blocking locks. This should 58b4ce94deSChris Mason * be done before scheduling 59b4ce94deSChris Mason */ 60b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p) 61b4ce94deSChris Mason { 62b4ce94deSChris Mason int i; 63b4ce94deSChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 64b4ce94deSChris Mason if (p->nodes[i] && p->locks[i]) 65b4ce94deSChris Mason btrfs_set_lock_blocking(p->nodes[i]); 66b4ce94deSChris Mason } 67b4ce94deSChris Mason } 68b4ce94deSChris Mason 69b4ce94deSChris Mason /* 70b4ce94deSChris Mason * reset all the locked nodes in the patch to spinning locks. 714008c04aSChris Mason * 724008c04aSChris Mason * held is used to keep lockdep happy, when lockdep is enabled 734008c04aSChris Mason * we set held to a blocking lock before we go around and 744008c04aSChris Mason * retake all the spinlocks in the path. You can safely use NULL 754008c04aSChris Mason * for held 76b4ce94deSChris Mason */ 774008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p, 784008c04aSChris Mason struct extent_buffer *held) 79b4ce94deSChris Mason { 80b4ce94deSChris Mason int i; 814008c04aSChris Mason 824008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 834008c04aSChris Mason /* lockdep really cares that we take all of these spinlocks 844008c04aSChris Mason * in the right order. If any of the locks in the path are not 854008c04aSChris Mason * currently blocking, it is going to complain. So, make really 864008c04aSChris Mason * really sure by forcing the path to blocking before we clear 874008c04aSChris Mason * the path blocking. 884008c04aSChris Mason */ 894008c04aSChris Mason if (held) 904008c04aSChris Mason btrfs_set_lock_blocking(held); 914008c04aSChris Mason btrfs_set_path_blocking(p); 924008c04aSChris Mason #endif 934008c04aSChris Mason 944008c04aSChris Mason for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) { 95b4ce94deSChris Mason if (p->nodes[i] && p->locks[i]) 96b4ce94deSChris Mason btrfs_clear_lock_blocking(p->nodes[i]); 97b4ce94deSChris Mason } 984008c04aSChris Mason 994008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1004008c04aSChris Mason if (held) 1014008c04aSChris Mason btrfs_clear_lock_blocking(held); 1024008c04aSChris Mason #endif 103b4ce94deSChris Mason } 104b4ce94deSChris Mason 105d352ac68SChris Mason /* this also releases the path */ 1062c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 1072c90e5d6SChris Mason { 108ff175d57SJesper Juhl if (!p) 109ff175d57SJesper Juhl return; 110df24a2b9SChris Mason btrfs_release_path(NULL, p); 1112c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 1122c90e5d6SChris Mason } 1132c90e5d6SChris Mason 114d352ac68SChris Mason /* 115d352ac68SChris Mason * path release drops references on the extent buffers in the path 116d352ac68SChris Mason * and it drops any locks held by this path 117d352ac68SChris Mason * 118d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 119d352ac68SChris Mason */ 120d397712bSChris Mason noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) 121eb60ceacSChris Mason { 122eb60ceacSChris Mason int i; 123a2135011SChris Mason 124234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 1253f157a2fSChris Mason p->slots[i] = 0; 126eb60ceacSChris Mason if (!p->nodes[i]) 127925baeddSChris Mason continue; 128925baeddSChris Mason if (p->locks[i]) { 129925baeddSChris Mason btrfs_tree_unlock(p->nodes[i]); 130925baeddSChris Mason p->locks[i] = 0; 131925baeddSChris Mason } 1325f39d397SChris Mason free_extent_buffer(p->nodes[i]); 1333f157a2fSChris Mason p->nodes[i] = NULL; 134eb60ceacSChris Mason } 135eb60ceacSChris Mason } 136eb60ceacSChris Mason 137d352ac68SChris Mason /* 138d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 139d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 140d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 141d352ac68SChris Mason * looping required. 142d352ac68SChris Mason * 143d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 144d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 145d352ac68SChris Mason * at any time because there are no locks held. 146d352ac68SChris Mason */ 147925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 148925baeddSChris Mason { 149925baeddSChris Mason struct extent_buffer *eb; 150240f62c8SChris Mason 151240f62c8SChris Mason rcu_read_lock(); 152240f62c8SChris Mason eb = rcu_dereference(root->node); 153925baeddSChris Mason extent_buffer_get(eb); 154240f62c8SChris Mason rcu_read_unlock(); 155925baeddSChris Mason return eb; 156925baeddSChris Mason } 157925baeddSChris Mason 158d352ac68SChris Mason /* loop around taking references on and locking the root node of the 159d352ac68SChris Mason * tree until you end up with a lock on the root. A locked buffer 160d352ac68SChris Mason * is returned, with a reference held. 161d352ac68SChris Mason */ 162925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 163925baeddSChris Mason { 164925baeddSChris Mason struct extent_buffer *eb; 165925baeddSChris Mason 166925baeddSChris Mason while (1) { 167925baeddSChris Mason eb = btrfs_root_node(root); 168925baeddSChris Mason btrfs_tree_lock(eb); 169240f62c8SChris Mason if (eb == root->node) 170925baeddSChris Mason break; 171925baeddSChris Mason btrfs_tree_unlock(eb); 172925baeddSChris Mason free_extent_buffer(eb); 173925baeddSChris Mason } 174925baeddSChris Mason return eb; 175925baeddSChris Mason } 176925baeddSChris Mason 177d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get 178d352ac68SChris Mason * put onto a simple dirty list. transaction.c walks this to make sure they 179d352ac68SChris Mason * get properly updated on disk. 180d352ac68SChris Mason */ 1810b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 1820b86a832SChris Mason { 1830b86a832SChris Mason if (root->track_dirty && list_empty(&root->dirty_list)) { 1840b86a832SChris Mason list_add(&root->dirty_list, 1850b86a832SChris Mason &root->fs_info->dirty_cowonly_roots); 1860b86a832SChris Mason } 1870b86a832SChris Mason } 1880b86a832SChris Mason 189d352ac68SChris Mason /* 190d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 191d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 192d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 193d352ac68SChris Mason */ 194be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 195be20aa9dSChris Mason struct btrfs_root *root, 196be20aa9dSChris Mason struct extent_buffer *buf, 197be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 198be20aa9dSChris Mason { 199be20aa9dSChris Mason struct extent_buffer *cow; 200be20aa9dSChris Mason int ret = 0; 201be20aa9dSChris Mason int level; 2025d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 203be20aa9dSChris Mason 204be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != 205be20aa9dSChris Mason root->fs_info->running_transaction->transid); 206be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 207be20aa9dSChris Mason 208be20aa9dSChris Mason level = btrfs_header_level(buf); 2095d4f98a2SYan Zheng if (level == 0) 2105d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 2115d4f98a2SYan Zheng else 2125d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 21331840ae1SZheng Yan 2145d4f98a2SYan Zheng cow = btrfs_alloc_free_block(trans, root, buf->len, 0, 2155d4f98a2SYan Zheng new_root_objectid, &disk_key, level, 2165d4f98a2SYan Zheng buf->start, 0); 2175d4f98a2SYan Zheng if (IS_ERR(cow)) 218be20aa9dSChris Mason return PTR_ERR(cow); 219be20aa9dSChris Mason 220be20aa9dSChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 221be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 222be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 2235d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 2245d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 2255d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 2265d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 2275d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 2285d4f98a2SYan Zheng else 229be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 230be20aa9dSChris Mason 2312b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 2322b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 2332b82032cSYan Zheng BTRFS_FSID_SIZE); 2342b82032cSYan Zheng 235be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 2365d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 2375d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 2385d4f98a2SYan Zheng else 2395d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 0); 2404aec2b52SChris Mason 241be20aa9dSChris Mason if (ret) 242be20aa9dSChris Mason return ret; 243be20aa9dSChris Mason 244be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 245be20aa9dSChris Mason *cow_ret = cow; 246be20aa9dSChris Mason return 0; 247be20aa9dSChris Mason } 248be20aa9dSChris Mason 249d352ac68SChris Mason /* 2505d4f98a2SYan Zheng * check if the tree block can be shared by multiple trees 2515d4f98a2SYan Zheng */ 2525d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root, 2535d4f98a2SYan Zheng struct extent_buffer *buf) 2545d4f98a2SYan Zheng { 2555d4f98a2SYan Zheng /* 2565d4f98a2SYan Zheng * Tree blocks not in refernece counted trees and tree roots 2575d4f98a2SYan Zheng * are never shared. If a block was allocated after the last 2585d4f98a2SYan Zheng * snapshot and the block was not allocated by tree relocation, 2595d4f98a2SYan Zheng * we know the block is not shared. 2605d4f98a2SYan Zheng */ 2615d4f98a2SYan Zheng if (root->ref_cows && 2625d4f98a2SYan Zheng buf != root->node && buf != root->commit_root && 2635d4f98a2SYan Zheng (btrfs_header_generation(buf) <= 2645d4f98a2SYan Zheng btrfs_root_last_snapshot(&root->root_item) || 2655d4f98a2SYan Zheng btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) 2665d4f98a2SYan Zheng return 1; 2675d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 2685d4f98a2SYan Zheng if (root->ref_cows && 2695d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 2705d4f98a2SYan Zheng return 1; 2715d4f98a2SYan Zheng #endif 2725d4f98a2SYan Zheng return 0; 2735d4f98a2SYan Zheng } 2745d4f98a2SYan Zheng 2755d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, 2765d4f98a2SYan Zheng struct btrfs_root *root, 2775d4f98a2SYan Zheng struct extent_buffer *buf, 278f0486c68SYan, Zheng struct extent_buffer *cow, 279f0486c68SYan, Zheng int *last_ref) 2805d4f98a2SYan Zheng { 2815d4f98a2SYan Zheng u64 refs; 2825d4f98a2SYan Zheng u64 owner; 2835d4f98a2SYan Zheng u64 flags; 2845d4f98a2SYan Zheng u64 new_flags = 0; 2855d4f98a2SYan Zheng int ret; 2865d4f98a2SYan Zheng 2875d4f98a2SYan Zheng /* 2885d4f98a2SYan Zheng * Backrefs update rules: 2895d4f98a2SYan Zheng * 2905d4f98a2SYan Zheng * Always use full backrefs for extent pointers in tree block 2915d4f98a2SYan Zheng * allocated by tree relocation. 2925d4f98a2SYan Zheng * 2935d4f98a2SYan Zheng * If a shared tree block is no longer referenced by its owner 2945d4f98a2SYan Zheng * tree (btrfs_header_owner(buf) == root->root_key.objectid), 2955d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 2965d4f98a2SYan Zheng * 2975d4f98a2SYan Zheng * If a tree block is been relocating 2985d4f98a2SYan Zheng * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), 2995d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 3005d4f98a2SYan Zheng * The reason for this is some operations (such as drop tree) 3015d4f98a2SYan Zheng * are only allowed for blocks use full backrefs. 3025d4f98a2SYan Zheng */ 3035d4f98a2SYan Zheng 3045d4f98a2SYan Zheng if (btrfs_block_can_be_shared(root, buf)) { 3055d4f98a2SYan Zheng ret = btrfs_lookup_extent_info(trans, root, buf->start, 3065d4f98a2SYan Zheng buf->len, &refs, &flags); 3075d4f98a2SYan Zheng BUG_ON(ret); 3085d4f98a2SYan Zheng BUG_ON(refs == 0); 3095d4f98a2SYan Zheng } else { 3105d4f98a2SYan Zheng refs = 1; 3115d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 3125d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 3135d4f98a2SYan Zheng flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; 3145d4f98a2SYan Zheng else 3155d4f98a2SYan Zheng flags = 0; 3165d4f98a2SYan Zheng } 3175d4f98a2SYan Zheng 3185d4f98a2SYan Zheng owner = btrfs_header_owner(buf); 3195d4f98a2SYan Zheng BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && 3205d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); 3215d4f98a2SYan Zheng 3225d4f98a2SYan Zheng if (refs > 1) { 3235d4f98a2SYan Zheng if ((owner == root->root_key.objectid || 3245d4f98a2SYan Zheng root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && 3255d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 3265d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, buf, 1); 3275d4f98a2SYan Zheng BUG_ON(ret); 3285d4f98a2SYan Zheng 3295d4f98a2SYan Zheng if (root->root_key.objectid == 3305d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 3315d4f98a2SYan Zheng ret = btrfs_dec_ref(trans, root, buf, 0); 3325d4f98a2SYan Zheng BUG_ON(ret); 3335d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 3345d4f98a2SYan Zheng BUG_ON(ret); 3355d4f98a2SYan Zheng } 3365d4f98a2SYan Zheng new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; 3375d4f98a2SYan Zheng } else { 3385d4f98a2SYan Zheng 3395d4f98a2SYan Zheng if (root->root_key.objectid == 3405d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 3415d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 3425d4f98a2SYan Zheng else 3435d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 0); 3445d4f98a2SYan Zheng BUG_ON(ret); 3455d4f98a2SYan Zheng } 3465d4f98a2SYan Zheng if (new_flags != 0) { 3475d4f98a2SYan Zheng ret = btrfs_set_disk_extent_flags(trans, root, 3485d4f98a2SYan Zheng buf->start, 3495d4f98a2SYan Zheng buf->len, 3505d4f98a2SYan Zheng new_flags, 0); 3515d4f98a2SYan Zheng BUG_ON(ret); 3525d4f98a2SYan Zheng } 3535d4f98a2SYan Zheng } else { 3545d4f98a2SYan Zheng if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { 3555d4f98a2SYan Zheng if (root->root_key.objectid == 3565d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 3575d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 1); 3585d4f98a2SYan Zheng else 3595d4f98a2SYan Zheng ret = btrfs_inc_ref(trans, root, cow, 0); 3605d4f98a2SYan Zheng BUG_ON(ret); 3615d4f98a2SYan Zheng ret = btrfs_dec_ref(trans, root, buf, 1); 3625d4f98a2SYan Zheng BUG_ON(ret); 3635d4f98a2SYan Zheng } 3645d4f98a2SYan Zheng clean_tree_block(trans, root, buf); 365f0486c68SYan, Zheng *last_ref = 1; 3665d4f98a2SYan Zheng } 3675d4f98a2SYan Zheng return 0; 3685d4f98a2SYan Zheng } 3695d4f98a2SYan Zheng 3705d4f98a2SYan Zheng /* 371d397712bSChris Mason * does the dirty work in cow of a single block. The parent block (if 372d397712bSChris Mason * supplied) is updated to point to the new cow copy. The new buffer is marked 373d397712bSChris Mason * dirty and returned locked. If you modify the block it needs to be marked 374d397712bSChris Mason * dirty again. 375d352ac68SChris Mason * 376d352ac68SChris Mason * search_start -- an allocation hint for the new block 377d352ac68SChris Mason * 378d397712bSChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in 379d397712bSChris Mason * bytes the allocator should try to find free next to the block it returns. 380d397712bSChris Mason * This is just a hint and may be ignored by the allocator. 381d352ac68SChris Mason */ 382d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, 3835f39d397SChris Mason struct btrfs_root *root, 3845f39d397SChris Mason struct extent_buffer *buf, 3855f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 3865f39d397SChris Mason struct extent_buffer **cow_ret, 3879fa8cfe7SChris Mason u64 search_start, u64 empty_size) 3886702ed49SChris Mason { 3895d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 3905f39d397SChris Mason struct extent_buffer *cow; 3917bb86316SChris Mason int level; 392f0486c68SYan, Zheng int last_ref = 0; 393925baeddSChris Mason int unlock_orig = 0; 3945d4f98a2SYan Zheng u64 parent_start; 3956702ed49SChris Mason 396925baeddSChris Mason if (*cow_ret == buf) 397925baeddSChris Mason unlock_orig = 1; 398925baeddSChris Mason 399b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 400925baeddSChris Mason 4017bb86316SChris Mason WARN_ON(root->ref_cows && trans->transid != 4027bb86316SChris Mason root->fs_info->running_transaction->transid); 4036702ed49SChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 4045f39d397SChris Mason 4057bb86316SChris Mason level = btrfs_header_level(buf); 40631840ae1SZheng Yan 4075d4f98a2SYan Zheng if (level == 0) 4085d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 4095d4f98a2SYan Zheng else 4105d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 4115d4f98a2SYan Zheng 4125d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { 4135d4f98a2SYan Zheng if (parent) 4145d4f98a2SYan Zheng parent_start = parent->start; 4155d4f98a2SYan Zheng else 4165d4f98a2SYan Zheng parent_start = 0; 4175d4f98a2SYan Zheng } else 4185d4f98a2SYan Zheng parent_start = 0; 4195d4f98a2SYan Zheng 4205d4f98a2SYan Zheng cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start, 4215d4f98a2SYan Zheng root->root_key.objectid, &disk_key, 4225d4f98a2SYan Zheng level, search_start, empty_size); 4236702ed49SChris Mason if (IS_ERR(cow)) 4246702ed49SChris Mason return PTR_ERR(cow); 4256702ed49SChris Mason 426b4ce94deSChris Mason /* cow is set to blocking by btrfs_init_new_buffer */ 427b4ce94deSChris Mason 4285f39d397SChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 429db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 4305f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 4315d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 4325d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 4335d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 4345d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 4355d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 4365d4f98a2SYan Zheng else 4375f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 4386702ed49SChris Mason 4392b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 4402b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 4412b82032cSYan Zheng BTRFS_FSID_SIZE); 4422b82032cSYan Zheng 443f0486c68SYan, Zheng update_ref_for_cow(trans, root, buf, cow, &last_ref); 4441a40e23bSZheng Yan 4453fd0a558SYan, Zheng if (root->ref_cows) 4463fd0a558SYan, Zheng btrfs_reloc_cow_block(trans, root, buf, cow); 4473fd0a558SYan, Zheng 4486702ed49SChris Mason if (buf == root->node) { 449925baeddSChris Mason WARN_ON(parent && parent != buf); 4505d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 4515d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 4525d4f98a2SYan Zheng parent_start = buf->start; 4535d4f98a2SYan Zheng else 4545d4f98a2SYan Zheng parent_start = 0; 455925baeddSChris Mason 4565f39d397SChris Mason extent_buffer_get(cow); 457240f62c8SChris Mason rcu_assign_pointer(root->node, cow); 458925baeddSChris Mason 459f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 460f0486c68SYan, Zheng last_ref); 4615f39d397SChris Mason free_extent_buffer(buf); 4620b86a832SChris Mason add_root_to_dirty_list(root); 4636702ed49SChris Mason } else { 4645d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 4655d4f98a2SYan Zheng parent_start = parent->start; 4665d4f98a2SYan Zheng else 4675d4f98a2SYan Zheng parent_start = 0; 4685d4f98a2SYan Zheng 4695d4f98a2SYan Zheng WARN_ON(trans->transid != btrfs_header_generation(parent)); 4705f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 471db94535dSChris Mason cow->start); 47274493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 47374493f7aSChris Mason trans->transid); 4746702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 475f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 476f0486c68SYan, Zheng last_ref); 4776702ed49SChris Mason } 478925baeddSChris Mason if (unlock_orig) 479925baeddSChris Mason btrfs_tree_unlock(buf); 4805f39d397SChris Mason free_extent_buffer(buf); 4816702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 4826702ed49SChris Mason *cow_ret = cow; 4836702ed49SChris Mason return 0; 4846702ed49SChris Mason } 4856702ed49SChris Mason 4865d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans, 4875d4f98a2SYan Zheng struct btrfs_root *root, 4885d4f98a2SYan Zheng struct extent_buffer *buf) 4895d4f98a2SYan Zheng { 4905d4f98a2SYan Zheng if (btrfs_header_generation(buf) == trans->transid && 4915d4f98a2SYan Zheng !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && 4925d4f98a2SYan Zheng !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 4935d4f98a2SYan Zheng btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) 4945d4f98a2SYan Zheng return 0; 4955d4f98a2SYan Zheng return 1; 4965d4f98a2SYan Zheng } 4975d4f98a2SYan Zheng 498d352ac68SChris Mason /* 499d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 500d352ac68SChris Mason * This version of it has extra checks so that a block isn't cow'd more than 501d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 502d352ac68SChris Mason */ 503d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, 5045f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 5055f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 5069fa8cfe7SChris Mason struct extent_buffer **cow_ret) 50702217ed2SChris Mason { 5086702ed49SChris Mason u64 search_start; 509f510cfecSChris Mason int ret; 510dc17ff8fSChris Mason 511ccd467d6SChris Mason if (trans->transaction != root->fs_info->running_transaction) { 512d397712bSChris Mason printk(KERN_CRIT "trans %llu running %llu\n", 513d397712bSChris Mason (unsigned long long)trans->transid, 514d397712bSChris Mason (unsigned long long) 515ccd467d6SChris Mason root->fs_info->running_transaction->transid); 516ccd467d6SChris Mason WARN_ON(1); 517ccd467d6SChris Mason } 518ccd467d6SChris Mason if (trans->transid != root->fs_info->generation) { 519d397712bSChris Mason printk(KERN_CRIT "trans %llu running %llu\n", 520d397712bSChris Mason (unsigned long long)trans->transid, 521d397712bSChris Mason (unsigned long long)root->fs_info->generation); 522ccd467d6SChris Mason WARN_ON(1); 523ccd467d6SChris Mason } 524dc17ff8fSChris Mason 5255d4f98a2SYan Zheng if (!should_cow_block(trans, root, buf)) { 52602217ed2SChris Mason *cow_ret = buf; 52702217ed2SChris Mason return 0; 52802217ed2SChris Mason } 529c487685dSChris Mason 5300b86a832SChris Mason search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 531b4ce94deSChris Mason 532b4ce94deSChris Mason if (parent) 533b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 534b4ce94deSChris Mason btrfs_set_lock_blocking(buf); 535b4ce94deSChris Mason 536f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 5379fa8cfe7SChris Mason parent_slot, cow_ret, search_start, 0); 5381abe9b8aSliubo 5391abe9b8aSliubo trace_btrfs_cow_block(root, buf, *cow_ret); 5401abe9b8aSliubo 541f510cfecSChris Mason return ret; 5422c90e5d6SChris Mason } 5436702ed49SChris Mason 544d352ac68SChris Mason /* 545d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 546d352ac68SChris Mason * node are actually close by 547d352ac68SChris Mason */ 5486b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 5496702ed49SChris Mason { 5506b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 5516702ed49SChris Mason return 1; 5526b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 5536702ed49SChris Mason return 1; 55402217ed2SChris Mason return 0; 55502217ed2SChris Mason } 55602217ed2SChris Mason 557081e9573SChris Mason /* 558081e9573SChris Mason * compare two keys in a memcmp fashion 559081e9573SChris Mason */ 560081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) 561081e9573SChris Mason { 562081e9573SChris Mason struct btrfs_key k1; 563081e9573SChris Mason 564081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 565081e9573SChris Mason 56620736abaSDiego Calleja return btrfs_comp_cpu_keys(&k1, k2); 567081e9573SChris Mason } 568081e9573SChris Mason 569f3465ca4SJosef Bacik /* 570f3465ca4SJosef Bacik * same as comp_keys only with two btrfs_key's 571f3465ca4SJosef Bacik */ 5725d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2) 573f3465ca4SJosef Bacik { 574f3465ca4SJosef Bacik if (k1->objectid > k2->objectid) 575f3465ca4SJosef Bacik return 1; 576f3465ca4SJosef Bacik if (k1->objectid < k2->objectid) 577f3465ca4SJosef Bacik return -1; 578f3465ca4SJosef Bacik if (k1->type > k2->type) 579f3465ca4SJosef Bacik return 1; 580f3465ca4SJosef Bacik if (k1->type < k2->type) 581f3465ca4SJosef Bacik return -1; 582f3465ca4SJosef Bacik if (k1->offset > k2->offset) 583f3465ca4SJosef Bacik return 1; 584f3465ca4SJosef Bacik if (k1->offset < k2->offset) 585f3465ca4SJosef Bacik return -1; 586f3465ca4SJosef Bacik return 0; 587f3465ca4SJosef Bacik } 588081e9573SChris Mason 589d352ac68SChris Mason /* 590d352ac68SChris Mason * this is used by the defrag code to go through all the 591d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 592d352ac68SChris Mason * disk order is close to key order 593d352ac68SChris Mason */ 5946702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 5955f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 596a6b6e75eSChris Mason int start_slot, int cache_only, u64 *last_ret, 597a6b6e75eSChris Mason struct btrfs_key *progress) 5986702ed49SChris Mason { 5996b80053dSChris Mason struct extent_buffer *cur; 6006702ed49SChris Mason u64 blocknr; 601ca7a79adSChris Mason u64 gen; 602e9d0b13bSChris Mason u64 search_start = *last_ret; 603e9d0b13bSChris Mason u64 last_block = 0; 6046702ed49SChris Mason u64 other; 6056702ed49SChris Mason u32 parent_nritems; 6066702ed49SChris Mason int end_slot; 6076702ed49SChris Mason int i; 6086702ed49SChris Mason int err = 0; 609f2183bdeSChris Mason int parent_level; 6106b80053dSChris Mason int uptodate; 6116b80053dSChris Mason u32 blocksize; 612081e9573SChris Mason int progress_passed = 0; 613081e9573SChris Mason struct btrfs_disk_key disk_key; 6146702ed49SChris Mason 6155708b959SChris Mason parent_level = btrfs_header_level(parent); 6165708b959SChris Mason if (cache_only && parent_level != 1) 6175708b959SChris Mason return 0; 6185708b959SChris Mason 619d397712bSChris Mason if (trans->transaction != root->fs_info->running_transaction) 6206702ed49SChris Mason WARN_ON(1); 621d397712bSChris Mason if (trans->transid != root->fs_info->generation) 6226702ed49SChris Mason WARN_ON(1); 62386479a04SChris Mason 6246b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 6256b80053dSChris Mason blocksize = btrfs_level_size(root, parent_level - 1); 6266702ed49SChris Mason end_slot = parent_nritems; 6276702ed49SChris Mason 6286702ed49SChris Mason if (parent_nritems == 1) 6296702ed49SChris Mason return 0; 6306702ed49SChris Mason 631b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 632b4ce94deSChris Mason 6336702ed49SChris Mason for (i = start_slot; i < end_slot; i++) { 6346702ed49SChris Mason int close = 1; 635a6b6e75eSChris Mason 6365708b959SChris Mason if (!parent->map_token) { 6375708b959SChris Mason map_extent_buffer(parent, 6385708b959SChris Mason btrfs_node_key_ptr_offset(i), 6395708b959SChris Mason sizeof(struct btrfs_key_ptr), 6405708b959SChris Mason &parent->map_token, &parent->kaddr, 6415708b959SChris Mason &parent->map_start, &parent->map_len, 6425708b959SChris Mason KM_USER1); 6435708b959SChris Mason } 644081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 645081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 646081e9573SChris Mason continue; 647081e9573SChris Mason 648081e9573SChris Mason progress_passed = 1; 6496b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 650ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 651e9d0b13bSChris Mason if (last_block == 0) 652e9d0b13bSChris Mason last_block = blocknr; 6535708b959SChris Mason 6546702ed49SChris Mason if (i > 0) { 6556b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 6566b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 6576702ed49SChris Mason } 6580ef3e66bSChris Mason if (!close && i < end_slot - 2) { 6596b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 6606b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 6616702ed49SChris Mason } 662e9d0b13bSChris Mason if (close) { 663e9d0b13bSChris Mason last_block = blocknr; 6646702ed49SChris Mason continue; 665e9d0b13bSChris Mason } 6665708b959SChris Mason if (parent->map_token) { 6675708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 6685708b959SChris Mason KM_USER1); 6695708b959SChris Mason parent->map_token = NULL; 6705708b959SChris Mason } 6716702ed49SChris Mason 6726b80053dSChris Mason cur = btrfs_find_tree_block(root, blocknr, blocksize); 6736b80053dSChris Mason if (cur) 6741259ab75SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen); 6756b80053dSChris Mason else 6766b80053dSChris Mason uptodate = 0; 6775708b959SChris Mason if (!cur || !uptodate) { 6786702ed49SChris Mason if (cache_only) { 6796b80053dSChris Mason free_extent_buffer(cur); 6806702ed49SChris Mason continue; 6816702ed49SChris Mason } 6826b80053dSChris Mason if (!cur) { 6836b80053dSChris Mason cur = read_tree_block(root, blocknr, 684ca7a79adSChris Mason blocksize, gen); 68597d9a8a4STsutomu Itoh if (!cur) 68697d9a8a4STsutomu Itoh return -EIO; 6876b80053dSChris Mason } else if (!uptodate) { 688ca7a79adSChris Mason btrfs_read_buffer(cur, gen); 6896702ed49SChris Mason } 690f2183bdeSChris Mason } 691e9d0b13bSChris Mason if (search_start == 0) 6926b80053dSChris Mason search_start = last_block; 693e9d0b13bSChris Mason 694e7a84565SChris Mason btrfs_tree_lock(cur); 695b4ce94deSChris Mason btrfs_set_lock_blocking(cur); 6966b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 697e7a84565SChris Mason &cur, search_start, 6986b80053dSChris Mason min(16 * blocksize, 6999fa8cfe7SChris Mason (end_slot - i) * blocksize)); 700252c38f0SYan if (err) { 701e7a84565SChris Mason btrfs_tree_unlock(cur); 7026b80053dSChris Mason free_extent_buffer(cur); 7036702ed49SChris Mason break; 704252c38f0SYan } 705e7a84565SChris Mason search_start = cur->start; 706e7a84565SChris Mason last_block = cur->start; 707f2183bdeSChris Mason *last_ret = search_start; 708e7a84565SChris Mason btrfs_tree_unlock(cur); 709e7a84565SChris Mason free_extent_buffer(cur); 7106702ed49SChris Mason } 7115708b959SChris Mason if (parent->map_token) { 7125708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 7135708b959SChris Mason KM_USER1); 7145708b959SChris Mason parent->map_token = NULL; 7155708b959SChris Mason } 7166702ed49SChris Mason return err; 7176702ed49SChris Mason } 7186702ed49SChris Mason 71974123bd7SChris Mason /* 72074123bd7SChris Mason * The leaf data grows from end-to-front in the node. 72174123bd7SChris Mason * this returns the address of the start of the last item, 72274123bd7SChris Mason * which is the stop of the leaf data stack 72374123bd7SChris Mason */ 724123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root, 7255f39d397SChris Mason struct extent_buffer *leaf) 726be0e5c09SChris Mason { 7275f39d397SChris Mason u32 nr = btrfs_header_nritems(leaf); 728be0e5c09SChris Mason if (nr == 0) 729123abc88SChris Mason return BTRFS_LEAF_DATA_SIZE(root); 7305f39d397SChris Mason return btrfs_item_offset_nr(leaf, nr - 1); 731be0e5c09SChris Mason } 732be0e5c09SChris Mason 733aa5d6bedSChris Mason 73474123bd7SChris Mason /* 7355f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 7365f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 7375f39d397SChris Mason * 73874123bd7SChris Mason * the slot in the array is returned via slot, and it points to 73974123bd7SChris Mason * the place where you would insert key if it is not found in 74074123bd7SChris Mason * the array. 74174123bd7SChris Mason * 74274123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 74374123bd7SChris Mason */ 744e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb, 745e02119d5SChris Mason unsigned long p, 7465f39d397SChris Mason int item_size, struct btrfs_key *key, 747be0e5c09SChris Mason int max, int *slot) 748be0e5c09SChris Mason { 749be0e5c09SChris Mason int low = 0; 750be0e5c09SChris Mason int high = max; 751be0e5c09SChris Mason int mid; 752be0e5c09SChris Mason int ret; 753479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 7545f39d397SChris Mason struct btrfs_disk_key unaligned; 7555f39d397SChris Mason unsigned long offset; 7565f39d397SChris Mason char *map_token = NULL; 7575f39d397SChris Mason char *kaddr = NULL; 7585f39d397SChris Mason unsigned long map_start = 0; 7595f39d397SChris Mason unsigned long map_len = 0; 760479965d6SChris Mason int err; 761be0e5c09SChris Mason 762be0e5c09SChris Mason while (low < high) { 763be0e5c09SChris Mason mid = (low + high) / 2; 7645f39d397SChris Mason offset = p + mid * item_size; 7655f39d397SChris Mason 7665f39d397SChris Mason if (!map_token || offset < map_start || 7675f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 7685f39d397SChris Mason map_start + map_len) { 769479965d6SChris Mason if (map_token) { 7705f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 771479965d6SChris Mason map_token = NULL; 772479965d6SChris Mason } 773934d375bSChris Mason 774934d375bSChris Mason err = map_private_extent_buffer(eb, offset, 775479965d6SChris Mason sizeof(struct btrfs_disk_key), 776479965d6SChris Mason &map_token, &kaddr, 7775f39d397SChris Mason &map_start, &map_len, KM_USER0); 7785f39d397SChris Mason 779479965d6SChris Mason if (!err) { 780479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 781479965d6SChris Mason map_start); 782479965d6SChris Mason } else { 7835f39d397SChris Mason read_extent_buffer(eb, &unaligned, 7845f39d397SChris Mason offset, sizeof(unaligned)); 7855f39d397SChris Mason tmp = &unaligned; 786479965d6SChris Mason } 787479965d6SChris Mason 7885f39d397SChris Mason } else { 7895f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 7905f39d397SChris Mason map_start); 7915f39d397SChris Mason } 792be0e5c09SChris Mason ret = comp_keys(tmp, key); 793be0e5c09SChris Mason 794be0e5c09SChris Mason if (ret < 0) 795be0e5c09SChris Mason low = mid + 1; 796be0e5c09SChris Mason else if (ret > 0) 797be0e5c09SChris Mason high = mid; 798be0e5c09SChris Mason else { 799be0e5c09SChris Mason *slot = mid; 800479965d6SChris Mason if (map_token) 8015f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 802be0e5c09SChris Mason return 0; 803be0e5c09SChris Mason } 804be0e5c09SChris Mason } 805be0e5c09SChris Mason *slot = low; 8065f39d397SChris Mason if (map_token) 8075f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 808be0e5c09SChris Mason return 1; 809be0e5c09SChris Mason } 810be0e5c09SChris Mason 81197571fd0SChris Mason /* 81297571fd0SChris Mason * simple bin_search frontend that does the right thing for 81397571fd0SChris Mason * leaves vs nodes 81497571fd0SChris Mason */ 8155f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, 8165f39d397SChris Mason int level, int *slot) 817be0e5c09SChris Mason { 8185f39d397SChris Mason if (level == 0) { 8195f39d397SChris Mason return generic_bin_search(eb, 8205f39d397SChris Mason offsetof(struct btrfs_leaf, items), 8210783fcfcSChris Mason sizeof(struct btrfs_item), 8225f39d397SChris Mason key, btrfs_header_nritems(eb), 8237518a238SChris Mason slot); 824be0e5c09SChris Mason } else { 8255f39d397SChris Mason return generic_bin_search(eb, 8265f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 827123abc88SChris Mason sizeof(struct btrfs_key_ptr), 8285f39d397SChris Mason key, btrfs_header_nritems(eb), 8297518a238SChris Mason slot); 830be0e5c09SChris Mason } 831be0e5c09SChris Mason return -1; 832be0e5c09SChris Mason } 833be0e5c09SChris Mason 8345d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, 8355d4f98a2SYan Zheng int level, int *slot) 8365d4f98a2SYan Zheng { 8375d4f98a2SYan Zheng return bin_search(eb, key, level, slot); 8385d4f98a2SYan Zheng } 8395d4f98a2SYan Zheng 840f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size) 841f0486c68SYan, Zheng { 842f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 843f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 844f0486c68SYan, Zheng btrfs_root_used(&root->root_item) + size); 845f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 846f0486c68SYan, Zheng } 847f0486c68SYan, Zheng 848f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size) 849f0486c68SYan, Zheng { 850f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 851f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 852f0486c68SYan, Zheng btrfs_root_used(&root->root_item) - size); 853f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 854f0486c68SYan, Zheng } 855f0486c68SYan, Zheng 856d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 857d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 858d352ac68SChris Mason * NULL is returned on error. 859d352ac68SChris Mason */ 860e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, 8615f39d397SChris Mason struct extent_buffer *parent, int slot) 862bb803951SChris Mason { 863ca7a79adSChris Mason int level = btrfs_header_level(parent); 864bb803951SChris Mason if (slot < 0) 865bb803951SChris Mason return NULL; 8665f39d397SChris Mason if (slot >= btrfs_header_nritems(parent)) 867bb803951SChris Mason return NULL; 868ca7a79adSChris Mason 869ca7a79adSChris Mason BUG_ON(level == 0); 870ca7a79adSChris Mason 871db94535dSChris Mason return read_tree_block(root, btrfs_node_blockptr(parent, slot), 872ca7a79adSChris Mason btrfs_level_size(root, level - 1), 873ca7a79adSChris Mason btrfs_node_ptr_generation(parent, slot)); 874bb803951SChris Mason } 875bb803951SChris Mason 876d352ac68SChris Mason /* 877d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 878d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 879d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 880d352ac68SChris Mason */ 881e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 88298ed5174SChris Mason struct btrfs_root *root, 88398ed5174SChris Mason struct btrfs_path *path, int level) 884bb803951SChris Mason { 8855f39d397SChris Mason struct extent_buffer *right = NULL; 8865f39d397SChris Mason struct extent_buffer *mid; 8875f39d397SChris Mason struct extent_buffer *left = NULL; 8885f39d397SChris Mason struct extent_buffer *parent = NULL; 889bb803951SChris Mason int ret = 0; 890bb803951SChris Mason int wret; 891bb803951SChris Mason int pslot; 892bb803951SChris Mason int orig_slot = path->slots[level]; 89379f95c82SChris Mason u64 orig_ptr; 894bb803951SChris Mason 895bb803951SChris Mason if (level == 0) 896bb803951SChris Mason return 0; 897bb803951SChris Mason 8985f39d397SChris Mason mid = path->nodes[level]; 899b4ce94deSChris Mason 900925baeddSChris Mason WARN_ON(!path->locks[level]); 9017bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 9027bb86316SChris Mason 9031d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 90479f95c82SChris Mason 905234b63a0SChris Mason if (level < BTRFS_MAX_LEVEL - 1) 9065f39d397SChris Mason parent = path->nodes[level + 1]; 907bb803951SChris Mason pslot = path->slots[level + 1]; 908bb803951SChris Mason 90940689478SChris Mason /* 91040689478SChris Mason * deal with the case where there is only one pointer in the root 91140689478SChris Mason * by promoting the node below to a root 91240689478SChris Mason */ 9135f39d397SChris Mason if (!parent) { 9145f39d397SChris Mason struct extent_buffer *child; 915bb803951SChris Mason 9165f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 917bb803951SChris Mason return 0; 918bb803951SChris Mason 919bb803951SChris Mason /* promote the child to a root */ 9205f39d397SChris Mason child = read_node_slot(root, mid, 0); 9217951f3ceSJeff Mahoney BUG_ON(!child); 922925baeddSChris Mason btrfs_tree_lock(child); 923b4ce94deSChris Mason btrfs_set_lock_blocking(child); 9249fa8cfe7SChris Mason ret = btrfs_cow_block(trans, root, child, mid, 0, &child); 925f0486c68SYan, Zheng if (ret) { 926f0486c68SYan, Zheng btrfs_tree_unlock(child); 927f0486c68SYan, Zheng free_extent_buffer(child); 928f0486c68SYan, Zheng goto enospc; 929f0486c68SYan, Zheng } 9302f375ab9SYan 931240f62c8SChris Mason rcu_assign_pointer(root->node, child); 932925baeddSChris Mason 9330b86a832SChris Mason add_root_to_dirty_list(root); 934925baeddSChris Mason btrfs_tree_unlock(child); 935b4ce94deSChris Mason 936925baeddSChris Mason path->locks[level] = 0; 937bb803951SChris Mason path->nodes[level] = NULL; 9385f39d397SChris Mason clean_tree_block(trans, root, mid); 939925baeddSChris Mason btrfs_tree_unlock(mid); 940bb803951SChris Mason /* once for the path */ 9415f39d397SChris Mason free_extent_buffer(mid); 942f0486c68SYan, Zheng 943f0486c68SYan, Zheng root_sub_used(root, mid->len); 944f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, mid, 0, 1); 945bb803951SChris Mason /* once for the root ptr */ 9465f39d397SChris Mason free_extent_buffer(mid); 947f0486c68SYan, Zheng return 0; 948bb803951SChris Mason } 9495f39d397SChris Mason if (btrfs_header_nritems(mid) > 950123abc88SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 4) 951bb803951SChris Mason return 0; 952bb803951SChris Mason 953559af821SAndi Kleen btrfs_header_nritems(mid); 95454aa1f4dSChris Mason 9555f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 9565f39d397SChris Mason if (left) { 957925baeddSChris Mason btrfs_tree_lock(left); 958b4ce94deSChris Mason btrfs_set_lock_blocking(left); 9595f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 9609fa8cfe7SChris Mason parent, pslot - 1, &left); 96154aa1f4dSChris Mason if (wret) { 96254aa1f4dSChris Mason ret = wret; 96354aa1f4dSChris Mason goto enospc; 96454aa1f4dSChris Mason } 9652cc58cf2SChris Mason } 9665f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 9675f39d397SChris Mason if (right) { 968925baeddSChris Mason btrfs_tree_lock(right); 969b4ce94deSChris Mason btrfs_set_lock_blocking(right); 9705f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 9719fa8cfe7SChris Mason parent, pslot + 1, &right); 9722cc58cf2SChris Mason if (wret) { 9732cc58cf2SChris Mason ret = wret; 9742cc58cf2SChris Mason goto enospc; 9752cc58cf2SChris Mason } 9762cc58cf2SChris Mason } 9772cc58cf2SChris Mason 9782cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 9795f39d397SChris Mason if (left) { 9805f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 981bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 98279f95c82SChris Mason if (wret < 0) 98379f95c82SChris Mason ret = wret; 984559af821SAndi Kleen btrfs_header_nritems(mid); 985bb803951SChris Mason } 98679f95c82SChris Mason 98779f95c82SChris Mason /* 98879f95c82SChris Mason * then try to empty the right most buffer into the middle 98979f95c82SChris Mason */ 9905f39d397SChris Mason if (right) { 991971a1f66SChris Mason wret = push_node_left(trans, root, mid, right, 1); 99254aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 99379f95c82SChris Mason ret = wret; 9945f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 9955f39d397SChris Mason clean_tree_block(trans, root, right); 996925baeddSChris Mason btrfs_tree_unlock(right); 997e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot + 998e089f05cSChris Mason 1); 999bb803951SChris Mason if (wret) 1000bb803951SChris Mason ret = wret; 1001f0486c68SYan, Zheng root_sub_used(root, right->len); 1002f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, right, 0, 1); 1003f0486c68SYan, Zheng free_extent_buffer(right); 1004f0486c68SYan, Zheng right = NULL; 1005bb803951SChris Mason } else { 10065f39d397SChris Mason struct btrfs_disk_key right_key; 10075f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 10085f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 10095f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1010bb803951SChris Mason } 1011bb803951SChris Mason } 10125f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 101379f95c82SChris Mason /* 101479f95c82SChris Mason * we're not allowed to leave a node with one item in the 101579f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 101679f95c82SChris Mason * could try to delete the only pointer in this node. 101779f95c82SChris Mason * So, pull some keys from the left. 101879f95c82SChris Mason * There has to be a left pointer at this point because 101979f95c82SChris Mason * otherwise we would have pulled some pointers from the 102079f95c82SChris Mason * right 102179f95c82SChris Mason */ 10225f39d397SChris Mason BUG_ON(!left); 10235f39d397SChris Mason wret = balance_node_right(trans, root, mid, left); 102454aa1f4dSChris Mason if (wret < 0) { 102579f95c82SChris Mason ret = wret; 102654aa1f4dSChris Mason goto enospc; 102754aa1f4dSChris Mason } 1028bce4eae9SChris Mason if (wret == 1) { 1029bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 1030bce4eae9SChris Mason if (wret < 0) 1031bce4eae9SChris Mason ret = wret; 1032bce4eae9SChris Mason } 103379f95c82SChris Mason BUG_ON(wret == 1); 103479f95c82SChris Mason } 10355f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 10365f39d397SChris Mason clean_tree_block(trans, root, mid); 1037925baeddSChris Mason btrfs_tree_unlock(mid); 1038e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot); 1039bb803951SChris Mason if (wret) 1040bb803951SChris Mason ret = wret; 1041f0486c68SYan, Zheng root_sub_used(root, mid->len); 1042f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, mid, 0, 1); 1043f0486c68SYan, Zheng free_extent_buffer(mid); 1044f0486c68SYan, Zheng mid = NULL; 104579f95c82SChris Mason } else { 104679f95c82SChris Mason /* update the parent key to reflect our changes */ 10475f39d397SChris Mason struct btrfs_disk_key mid_key; 10485f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 10495f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 10505f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 105179f95c82SChris Mason } 1052bb803951SChris Mason 105379f95c82SChris Mason /* update the path */ 10545f39d397SChris Mason if (left) { 10555f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 10565f39d397SChris Mason extent_buffer_get(left); 1057925baeddSChris Mason /* left was locked after cow */ 10585f39d397SChris Mason path->nodes[level] = left; 1059bb803951SChris Mason path->slots[level + 1] -= 1; 1060bb803951SChris Mason path->slots[level] = orig_slot; 1061925baeddSChris Mason if (mid) { 1062925baeddSChris Mason btrfs_tree_unlock(mid); 10635f39d397SChris Mason free_extent_buffer(mid); 1064925baeddSChris Mason } 1065bb803951SChris Mason } else { 10665f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 1067bb803951SChris Mason path->slots[level] = orig_slot; 1068bb803951SChris Mason } 1069bb803951SChris Mason } 107079f95c82SChris Mason /* double check we haven't messed things up */ 1071e20d96d6SChris Mason if (orig_ptr != 10725f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 107379f95c82SChris Mason BUG(); 107454aa1f4dSChris Mason enospc: 1075925baeddSChris Mason if (right) { 1076925baeddSChris Mason btrfs_tree_unlock(right); 10775f39d397SChris Mason free_extent_buffer(right); 1078925baeddSChris Mason } 1079925baeddSChris Mason if (left) { 1080925baeddSChris Mason if (path->nodes[level] != left) 1081925baeddSChris Mason btrfs_tree_unlock(left); 10825f39d397SChris Mason free_extent_buffer(left); 1083925baeddSChris Mason } 1084bb803951SChris Mason return ret; 1085bb803951SChris Mason } 1086bb803951SChris Mason 1087d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 1088d352ac68SChris Mason * when they are completely full. This is also done top down, so we 1089d352ac68SChris Mason * have to be pessimistic. 1090d352ac68SChris Mason */ 1091d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, 1092e66f709bSChris Mason struct btrfs_root *root, 1093e66f709bSChris Mason struct btrfs_path *path, int level) 1094e66f709bSChris Mason { 10955f39d397SChris Mason struct extent_buffer *right = NULL; 10965f39d397SChris Mason struct extent_buffer *mid; 10975f39d397SChris Mason struct extent_buffer *left = NULL; 10985f39d397SChris Mason struct extent_buffer *parent = NULL; 1099e66f709bSChris Mason int ret = 0; 1100e66f709bSChris Mason int wret; 1101e66f709bSChris Mason int pslot; 1102e66f709bSChris Mason int orig_slot = path->slots[level]; 1103e66f709bSChris Mason 1104e66f709bSChris Mason if (level == 0) 1105e66f709bSChris Mason return 1; 1106e66f709bSChris Mason 11075f39d397SChris Mason mid = path->nodes[level]; 11087bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 1109e66f709bSChris Mason 1110e66f709bSChris Mason if (level < BTRFS_MAX_LEVEL - 1) 11115f39d397SChris Mason parent = path->nodes[level + 1]; 1112e66f709bSChris Mason pslot = path->slots[level + 1]; 1113e66f709bSChris Mason 11145f39d397SChris Mason if (!parent) 1115e66f709bSChris Mason return 1; 1116e66f709bSChris Mason 11175f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 1118e66f709bSChris Mason 1119e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 11205f39d397SChris Mason if (left) { 1121e66f709bSChris Mason u32 left_nr; 1122925baeddSChris Mason 1123925baeddSChris Mason btrfs_tree_lock(left); 1124b4ce94deSChris Mason btrfs_set_lock_blocking(left); 1125b4ce94deSChris Mason 11265f39d397SChris Mason left_nr = btrfs_header_nritems(left); 112733ade1f8SChris Mason if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 112833ade1f8SChris Mason wret = 1; 112933ade1f8SChris Mason } else { 11305f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 11319fa8cfe7SChris Mason pslot - 1, &left); 113254aa1f4dSChris Mason if (ret) 113354aa1f4dSChris Mason wret = 1; 113454aa1f4dSChris Mason else { 113554aa1f4dSChris Mason wret = push_node_left(trans, root, 1136971a1f66SChris Mason left, mid, 0); 113754aa1f4dSChris Mason } 113833ade1f8SChris Mason } 1139e66f709bSChris Mason if (wret < 0) 1140e66f709bSChris Mason ret = wret; 1141e66f709bSChris Mason if (wret == 0) { 11425f39d397SChris Mason struct btrfs_disk_key disk_key; 1143e66f709bSChris Mason orig_slot += left_nr; 11445f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 11455f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 11465f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 11475f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 11485f39d397SChris Mason path->nodes[level] = left; 1149e66f709bSChris Mason path->slots[level + 1] -= 1; 1150e66f709bSChris Mason path->slots[level] = orig_slot; 1151925baeddSChris Mason btrfs_tree_unlock(mid); 11525f39d397SChris Mason free_extent_buffer(mid); 1153e66f709bSChris Mason } else { 1154e66f709bSChris Mason orig_slot -= 11555f39d397SChris Mason btrfs_header_nritems(left); 1156e66f709bSChris Mason path->slots[level] = orig_slot; 1157925baeddSChris Mason btrfs_tree_unlock(left); 11585f39d397SChris Mason free_extent_buffer(left); 1159e66f709bSChris Mason } 1160e66f709bSChris Mason return 0; 1161e66f709bSChris Mason } 1162925baeddSChris Mason btrfs_tree_unlock(left); 11635f39d397SChris Mason free_extent_buffer(left); 1164e66f709bSChris Mason } 11655f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 1166e66f709bSChris Mason 1167e66f709bSChris Mason /* 1168e66f709bSChris Mason * then try to empty the right most buffer into the middle 1169e66f709bSChris Mason */ 11705f39d397SChris Mason if (right) { 117133ade1f8SChris Mason u32 right_nr; 1172b4ce94deSChris Mason 1173925baeddSChris Mason btrfs_tree_lock(right); 1174b4ce94deSChris Mason btrfs_set_lock_blocking(right); 1175b4ce94deSChris Mason 11765f39d397SChris Mason right_nr = btrfs_header_nritems(right); 117733ade1f8SChris Mason if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 117833ade1f8SChris Mason wret = 1; 117933ade1f8SChris Mason } else { 11805f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 11815f39d397SChris Mason parent, pslot + 1, 11829fa8cfe7SChris Mason &right); 118354aa1f4dSChris Mason if (ret) 118454aa1f4dSChris Mason wret = 1; 118554aa1f4dSChris Mason else { 118633ade1f8SChris Mason wret = balance_node_right(trans, root, 11875f39d397SChris Mason right, mid); 118833ade1f8SChris Mason } 118954aa1f4dSChris Mason } 1190e66f709bSChris Mason if (wret < 0) 1191e66f709bSChris Mason ret = wret; 1192e66f709bSChris Mason if (wret == 0) { 11935f39d397SChris Mason struct btrfs_disk_key disk_key; 11945f39d397SChris Mason 11955f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 11965f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 11975f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 11985f39d397SChris Mason 11995f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 12005f39d397SChris Mason path->nodes[level] = right; 1201e66f709bSChris Mason path->slots[level + 1] += 1; 1202e66f709bSChris Mason path->slots[level] = orig_slot - 12035f39d397SChris Mason btrfs_header_nritems(mid); 1204925baeddSChris Mason btrfs_tree_unlock(mid); 12055f39d397SChris Mason free_extent_buffer(mid); 1206e66f709bSChris Mason } else { 1207925baeddSChris Mason btrfs_tree_unlock(right); 12085f39d397SChris Mason free_extent_buffer(right); 1209e66f709bSChris Mason } 1210e66f709bSChris Mason return 0; 1211e66f709bSChris Mason } 1212925baeddSChris Mason btrfs_tree_unlock(right); 12135f39d397SChris Mason free_extent_buffer(right); 1214e66f709bSChris Mason } 1215e66f709bSChris Mason return 1; 1216e66f709bSChris Mason } 1217e66f709bSChris Mason 121874123bd7SChris Mason /* 1219d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 1220d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 12213c69faecSChris Mason */ 1222c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root, 1223e02119d5SChris Mason struct btrfs_path *path, 122401f46658SChris Mason int level, int slot, u64 objectid) 12253c69faecSChris Mason { 12265f39d397SChris Mason struct extent_buffer *node; 122701f46658SChris Mason struct btrfs_disk_key disk_key; 12283c69faecSChris Mason u32 nritems; 12293c69faecSChris Mason u64 search; 1230a7175319SChris Mason u64 target; 12316b80053dSChris Mason u64 nread = 0; 1232*cb25c2eaSJosef Bacik u64 gen; 12333c69faecSChris Mason int direction = path->reada; 12345f39d397SChris Mason struct extent_buffer *eb; 12356b80053dSChris Mason u32 nr; 12366b80053dSChris Mason u32 blocksize; 12376b80053dSChris Mason u32 nscan = 0; 1238db94535dSChris Mason 1239a6b6e75eSChris Mason if (level != 1) 12403c69faecSChris Mason return; 12413c69faecSChris Mason 12426702ed49SChris Mason if (!path->nodes[level]) 12436702ed49SChris Mason return; 12446702ed49SChris Mason 12455f39d397SChris Mason node = path->nodes[level]; 1246925baeddSChris Mason 12473c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 12486b80053dSChris Mason blocksize = btrfs_level_size(root, level - 1); 12496b80053dSChris Mason eb = btrfs_find_tree_block(root, search, blocksize); 12505f39d397SChris Mason if (eb) { 12515f39d397SChris Mason free_extent_buffer(eb); 12523c69faecSChris Mason return; 12533c69faecSChris Mason } 12543c69faecSChris Mason 1255a7175319SChris Mason target = search; 12566b80053dSChris Mason 12575f39d397SChris Mason nritems = btrfs_header_nritems(node); 12586b80053dSChris Mason nr = slot; 12593c69faecSChris Mason while (1) { 1260*cb25c2eaSJosef Bacik if (!node->map_token) { 1261*cb25c2eaSJosef Bacik unsigned long offset = btrfs_node_key_ptr_offset(nr); 1262*cb25c2eaSJosef Bacik map_private_extent_buffer(node, offset, 1263*cb25c2eaSJosef Bacik sizeof(struct btrfs_key_ptr), 1264*cb25c2eaSJosef Bacik &node->map_token, 1265*cb25c2eaSJosef Bacik &node->kaddr, 1266*cb25c2eaSJosef Bacik &node->map_start, 1267*cb25c2eaSJosef Bacik &node->map_len, KM_USER1); 1268*cb25c2eaSJosef Bacik } 12696b80053dSChris Mason if (direction < 0) { 12706b80053dSChris Mason if (nr == 0) 12713c69faecSChris Mason break; 12726b80053dSChris Mason nr--; 12736b80053dSChris Mason } else if (direction > 0) { 12746b80053dSChris Mason nr++; 12756b80053dSChris Mason if (nr >= nritems) 12766b80053dSChris Mason break; 12773c69faecSChris Mason } 127801f46658SChris Mason if (path->reada < 0 && objectid) { 127901f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 128001f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 128101f46658SChris Mason break; 128201f46658SChris Mason } 12836b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 1284a7175319SChris Mason if ((search <= target && target - search <= 65536) || 1285a7175319SChris Mason (search > target && search - target <= 65536)) { 1286*cb25c2eaSJosef Bacik gen = btrfs_node_ptr_generation(node, nr); 1287*cb25c2eaSJosef Bacik if (node->map_token) { 1288*cb25c2eaSJosef Bacik unmap_extent_buffer(node, node->map_token, 1289*cb25c2eaSJosef Bacik KM_USER1); 1290*cb25c2eaSJosef Bacik node->map_token = NULL; 1291*cb25c2eaSJosef Bacik } 1292*cb25c2eaSJosef Bacik readahead_tree_block(root, search, blocksize, gen); 12936b80053dSChris Mason nread += blocksize; 12943c69faecSChris Mason } 12956b80053dSChris Mason nscan++; 1296a7175319SChris Mason if ((nread > 65536 || nscan > 32)) 12976b80053dSChris Mason break; 12983c69faecSChris Mason } 1299*cb25c2eaSJosef Bacik if (node->map_token) { 1300*cb25c2eaSJosef Bacik unmap_extent_buffer(node, node->map_token, KM_USER1); 1301*cb25c2eaSJosef Bacik node->map_token = NULL; 1302*cb25c2eaSJosef Bacik } 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 */ 1350b4ce94deSChris Mason btrfs_release_path(root, 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]) { 1415925baeddSChris Mason btrfs_tree_unlock(t); 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; 1442b4ce94deSChris Mason btrfs_tree_unlock(path->nodes[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); 1491cb44921aSChris Mason tmp = read_tree_block(root, blocknr, blocksize, gen); 1492cb44921aSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 1493cb44921aSChris Mason *eb_ret = tmp; 1494cb44921aSChris Mason return 0; 1495cb44921aSChris Mason } 1496cb44921aSChris Mason free_extent_buffer(tmp); 1497cb44921aSChris Mason btrfs_release_path(NULL, p); 1498cb44921aSChris Mason return -EIO; 1499cb44921aSChris Mason } 1500cb44921aSChris Mason } 1501c8c42864SChris Mason 1502c8c42864SChris Mason /* 1503c8c42864SChris Mason * reduce lock contention at high levels 1504c8c42864SChris Mason * of the btree by dropping locks before 150576a05b35SChris Mason * we read. Don't release the lock on the current 150676a05b35SChris Mason * level because we need to walk this node to figure 150776a05b35SChris Mason * out which blocks to read. 1508c8c42864SChris Mason */ 15098c594ea8SChris Mason btrfs_unlock_up_safe(p, level + 1); 15108c594ea8SChris Mason btrfs_set_path_blocking(p); 15118c594ea8SChris Mason 1512c8c42864SChris Mason free_extent_buffer(tmp); 1513c8c42864SChris Mason if (p->reada) 1514c8c42864SChris Mason reada_for_search(root, p, level, slot, key->objectid); 1515c8c42864SChris Mason 15168c594ea8SChris Mason btrfs_release_path(NULL, p); 151776a05b35SChris Mason 151876a05b35SChris Mason ret = -EAGAIN; 15195bdd3536SYan, Zheng tmp = read_tree_block(root, blocknr, blocksize, 0); 152076a05b35SChris Mason if (tmp) { 152176a05b35SChris Mason /* 152276a05b35SChris Mason * If the read above didn't mark this buffer up to date, 152376a05b35SChris Mason * it will never end up being up to date. Set ret to EIO now 152476a05b35SChris Mason * and give up so that our caller doesn't loop forever 152576a05b35SChris Mason * on our EAGAINs. 152676a05b35SChris Mason */ 152776a05b35SChris Mason if (!btrfs_buffer_uptodate(tmp, 0)) 152876a05b35SChris Mason ret = -EIO; 1529c8c42864SChris Mason free_extent_buffer(tmp); 153076a05b35SChris Mason } 153176a05b35SChris Mason return ret; 1532c8c42864SChris Mason } 1533c8c42864SChris Mason 1534c8c42864SChris Mason /* 1535c8c42864SChris Mason * helper function for btrfs_search_slot. This does all of the checks 1536c8c42864SChris Mason * for node-level blocks and does any balancing required based on 1537c8c42864SChris Mason * the ins_len. 1538c8c42864SChris Mason * 1539c8c42864SChris Mason * If no extra work was required, zero is returned. If we had to 1540c8c42864SChris Mason * drop the path, -EAGAIN is returned and btrfs_search_slot must 1541c8c42864SChris Mason * start over 1542c8c42864SChris Mason */ 1543c8c42864SChris Mason static int 1544c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans, 1545c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 1546c8c42864SChris Mason struct extent_buffer *b, int level, int ins_len) 1547c8c42864SChris Mason { 1548c8c42864SChris Mason int ret; 1549c8c42864SChris Mason if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= 1550c8c42864SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 1551c8c42864SChris Mason int sret; 1552c8c42864SChris Mason 1553c8c42864SChris Mason sret = reada_for_balance(root, p, level); 1554c8c42864SChris Mason if (sret) 1555c8c42864SChris Mason goto again; 1556c8c42864SChris Mason 1557c8c42864SChris Mason btrfs_set_path_blocking(p); 1558c8c42864SChris Mason sret = split_node(trans, root, p, level); 1559c8c42864SChris Mason btrfs_clear_path_blocking(p, NULL); 1560c8c42864SChris Mason 1561c8c42864SChris Mason BUG_ON(sret > 0); 1562c8c42864SChris Mason if (sret) { 1563c8c42864SChris Mason ret = sret; 1564c8c42864SChris Mason goto done; 1565c8c42864SChris Mason } 1566c8c42864SChris Mason b = p->nodes[level]; 1567c8c42864SChris Mason } else if (ins_len < 0 && btrfs_header_nritems(b) < 1568cfbb9308SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { 1569c8c42864SChris Mason int sret; 1570c8c42864SChris Mason 1571c8c42864SChris Mason sret = reada_for_balance(root, p, level); 1572c8c42864SChris Mason if (sret) 1573c8c42864SChris Mason goto again; 1574c8c42864SChris Mason 1575c8c42864SChris Mason btrfs_set_path_blocking(p); 1576c8c42864SChris Mason sret = balance_level(trans, root, p, level); 1577c8c42864SChris Mason btrfs_clear_path_blocking(p, NULL); 1578c8c42864SChris Mason 1579c8c42864SChris Mason if (sret) { 1580c8c42864SChris Mason ret = sret; 1581c8c42864SChris Mason goto done; 1582c8c42864SChris Mason } 1583c8c42864SChris Mason b = p->nodes[level]; 1584c8c42864SChris Mason if (!b) { 1585c8c42864SChris Mason btrfs_release_path(NULL, p); 1586c8c42864SChris Mason goto again; 1587c8c42864SChris Mason } 1588c8c42864SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 1589c8c42864SChris Mason } 1590c8c42864SChris Mason return 0; 1591c8c42864SChris Mason 1592c8c42864SChris Mason again: 1593c8c42864SChris Mason ret = -EAGAIN; 1594c8c42864SChris Mason done: 1595c8c42864SChris Mason return ret; 1596c8c42864SChris Mason } 1597c8c42864SChris Mason 1598c8c42864SChris Mason /* 159974123bd7SChris Mason * look for key in the tree. path is filled in with nodes along the way 160074123bd7SChris Mason * if key is found, we return zero and you can find the item in the leaf 160174123bd7SChris Mason * level of the path (level 0) 160274123bd7SChris Mason * 160374123bd7SChris Mason * If the key isn't found, the path points to the slot where it should 1604aa5d6bedSChris Mason * be inserted, and 1 is returned. If there are other errors during the 1605aa5d6bedSChris Mason * search a negative error number is returned. 160697571fd0SChris Mason * 160797571fd0SChris Mason * if ins_len > 0, nodes and leaves will be split as we walk down the 160897571fd0SChris Mason * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if 160997571fd0SChris Mason * possible) 161074123bd7SChris Mason */ 1611e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root 1612e089f05cSChris Mason *root, struct btrfs_key *key, struct btrfs_path *p, int 1613e089f05cSChris Mason ins_len, int cow) 1614be0e5c09SChris Mason { 16155f39d397SChris Mason struct extent_buffer *b; 1616be0e5c09SChris Mason int slot; 1617be0e5c09SChris Mason int ret; 161833c66f43SYan Zheng int err; 1619be0e5c09SChris Mason int level; 1620925baeddSChris Mason int lowest_unlock = 1; 16219f3a7427SChris Mason u8 lowest_level = 0; 16229f3a7427SChris Mason 16236702ed49SChris Mason lowest_level = p->lowest_level; 1624323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 162522b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 162625179201SJosef Bacik 1627925baeddSChris Mason if (ins_len < 0) 1628925baeddSChris Mason lowest_unlock = 2; 162965b51a00SChris Mason 1630bb803951SChris Mason again: 16315d4f98a2SYan Zheng if (p->search_commit_root) { 16325d4f98a2SYan Zheng b = root->commit_root; 16335d4f98a2SYan Zheng extent_buffer_get(b); 16345d4f98a2SYan Zheng if (!p->skip_locking) 16355d4f98a2SYan Zheng btrfs_tree_lock(b); 16365d4f98a2SYan Zheng } else { 16375cd57b2cSChris Mason if (p->skip_locking) 16385cd57b2cSChris Mason b = btrfs_root_node(root); 16395cd57b2cSChris Mason else 1640925baeddSChris Mason b = btrfs_lock_root_node(root); 16415d4f98a2SYan Zheng } 1642925baeddSChris Mason 1643eb60ceacSChris Mason while (b) { 16445f39d397SChris Mason level = btrfs_header_level(b); 164565b51a00SChris Mason 164665b51a00SChris Mason /* 164765b51a00SChris Mason * setup the path here so we can release it under lock 164865b51a00SChris Mason * contention with the cow code 164965b51a00SChris Mason */ 165065b51a00SChris Mason p->nodes[level] = b; 165165b51a00SChris Mason if (!p->skip_locking) 165265b51a00SChris Mason p->locks[level] = 1; 165365b51a00SChris Mason 165402217ed2SChris Mason if (cow) { 1655c8c42864SChris Mason /* 1656c8c42864SChris Mason * if we don't really need to cow this block 1657c8c42864SChris Mason * then we don't want to set the path blocking, 1658c8c42864SChris Mason * so we test it here 1659c8c42864SChris Mason */ 16605d4f98a2SYan Zheng if (!should_cow_block(trans, root, b)) 166165b51a00SChris Mason goto cow_done; 16625d4f98a2SYan Zheng 1663b4ce94deSChris Mason btrfs_set_path_blocking(p); 1664b4ce94deSChris Mason 166533c66f43SYan Zheng err = btrfs_cow_block(trans, root, b, 1666e20d96d6SChris Mason p->nodes[level + 1], 16679fa8cfe7SChris Mason p->slots[level + 1], &b); 166833c66f43SYan Zheng if (err) { 166933c66f43SYan Zheng ret = err; 167065b51a00SChris Mason goto done; 167154aa1f4dSChris Mason } 167202217ed2SChris Mason } 167365b51a00SChris Mason cow_done: 167402217ed2SChris Mason BUG_ON(!cow && ins_len); 16755f39d397SChris Mason if (level != btrfs_header_level(b)) 16762c90e5d6SChris Mason WARN_ON(1); 16775f39d397SChris Mason level = btrfs_header_level(b); 167865b51a00SChris Mason 1679eb60ceacSChris Mason p->nodes[level] = b; 16805cd57b2cSChris Mason if (!p->skip_locking) 1681925baeddSChris Mason p->locks[level] = 1; 168265b51a00SChris Mason 16834008c04aSChris Mason btrfs_clear_path_blocking(p, NULL); 1684b4ce94deSChris Mason 1685b4ce94deSChris Mason /* 1686b4ce94deSChris Mason * we have a lock on b and as long as we aren't changing 1687b4ce94deSChris Mason * the tree, there is no way to for the items in b to change. 1688b4ce94deSChris Mason * It is safe to drop the lock on our parent before we 1689b4ce94deSChris Mason * go through the expensive btree search on b. 1690b4ce94deSChris Mason * 1691b4ce94deSChris Mason * If cow is true, then we might be changing slot zero, 1692b4ce94deSChris Mason * which may require changing the parent. So, we can't 1693b4ce94deSChris Mason * drop the lock until after we know which slot we're 1694b4ce94deSChris Mason * operating on. 1695b4ce94deSChris Mason */ 1696b4ce94deSChris Mason if (!cow) 1697b4ce94deSChris Mason btrfs_unlock_up_safe(p, level + 1); 1698b4ce94deSChris Mason 16995f39d397SChris Mason ret = bin_search(b, key, level, &slot); 1700b4ce94deSChris Mason 17015f39d397SChris Mason if (level != 0) { 170233c66f43SYan Zheng int dec = 0; 170333c66f43SYan Zheng if (ret && slot > 0) { 170433c66f43SYan Zheng dec = 1; 1705be0e5c09SChris Mason slot -= 1; 170633c66f43SYan Zheng } 1707be0e5c09SChris Mason p->slots[level] = slot; 170833c66f43SYan Zheng err = setup_nodes_for_search(trans, root, p, b, level, 1709c8c42864SChris Mason ins_len); 171033c66f43SYan Zheng if (err == -EAGAIN) 1711b4ce94deSChris Mason goto again; 171233c66f43SYan Zheng if (err) { 171333c66f43SYan Zheng ret = err; 171465b51a00SChris Mason goto done; 171533c66f43SYan Zheng } 17165c680ed6SChris Mason b = p->nodes[level]; 17175c680ed6SChris Mason slot = p->slots[level]; 1718b4ce94deSChris Mason 1719f9efa9c7SChris Mason unlock_up(p, level, lowest_unlock); 1720f9efa9c7SChris Mason 1721925baeddSChris Mason if (level == lowest_level) { 172233c66f43SYan Zheng if (dec) 172333c66f43SYan Zheng p->slots[level]++; 17245b21f2edSZheng Yan goto done; 1725925baeddSChris Mason } 1726ca7a79adSChris Mason 172733c66f43SYan Zheng err = read_block_for_search(trans, root, p, 1728c8c42864SChris Mason &b, level, slot, key); 172933c66f43SYan Zheng if (err == -EAGAIN) 1730051e1b9fSChris Mason goto again; 173133c66f43SYan Zheng if (err) { 173233c66f43SYan Zheng ret = err; 173376a05b35SChris Mason goto done; 173433c66f43SYan Zheng } 173576a05b35SChris Mason 1736b4ce94deSChris Mason if (!p->skip_locking) { 17374008c04aSChris Mason btrfs_clear_path_blocking(p, NULL); 173833c66f43SYan Zheng err = btrfs_try_spin_lock(b); 1739b4ce94deSChris Mason 174033c66f43SYan Zheng if (!err) { 1741b4ce94deSChris Mason btrfs_set_path_blocking(p); 1742925baeddSChris Mason btrfs_tree_lock(b); 17434008c04aSChris Mason btrfs_clear_path_blocking(p, b); 1744b4ce94deSChris Mason } 1745b4ce94deSChris Mason } 1746be0e5c09SChris Mason } else { 1747be0e5c09SChris Mason p->slots[level] = slot; 174887b29b20SYan Zheng if (ins_len > 0 && 174987b29b20SYan Zheng btrfs_leaf_free_space(root, b) < ins_len) { 1750b4ce94deSChris Mason btrfs_set_path_blocking(p); 175133c66f43SYan Zheng err = split_leaf(trans, root, key, 1752cc0c5538SChris Mason p, ins_len, ret == 0); 17534008c04aSChris Mason btrfs_clear_path_blocking(p, NULL); 1754b4ce94deSChris Mason 175533c66f43SYan Zheng BUG_ON(err > 0); 175633c66f43SYan Zheng if (err) { 175733c66f43SYan Zheng ret = err; 175865b51a00SChris Mason goto done; 175965b51a00SChris Mason } 17605c680ed6SChris Mason } 1761459931ecSChris Mason if (!p->search_for_split) 1762925baeddSChris Mason unlock_up(p, level, lowest_unlock); 176365b51a00SChris Mason goto done; 176465b51a00SChris Mason } 176565b51a00SChris Mason } 176665b51a00SChris Mason ret = 1; 176765b51a00SChris Mason done: 1768b4ce94deSChris Mason /* 1769b4ce94deSChris Mason * we don't really know what they plan on doing with the path 1770b4ce94deSChris Mason * from here on, so for now just mark it as blocking 1771b4ce94deSChris Mason */ 1772b9473439SChris Mason if (!p->leave_spinning) 1773b4ce94deSChris Mason btrfs_set_path_blocking(p); 177476a05b35SChris Mason if (ret < 0) 177576a05b35SChris Mason btrfs_release_path(root, p); 1776be0e5c09SChris Mason return ret; 1777be0e5c09SChris Mason } 1778be0e5c09SChris Mason 177974123bd7SChris Mason /* 178074123bd7SChris Mason * adjust the pointers going up the tree, starting at level 178174123bd7SChris Mason * making sure the right key of each node is points to 'key'. 178274123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 178374123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 178474123bd7SChris Mason * higher levels 1785aa5d6bedSChris Mason * 1786aa5d6bedSChris Mason * If this fails to write a tree block, it returns -1, but continues 1787aa5d6bedSChris Mason * fixing up the blocks in ram so the tree is consistent. 178874123bd7SChris Mason */ 17895f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans, 17905f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 17915f39d397SChris Mason struct btrfs_disk_key *key, int level) 1792be0e5c09SChris Mason { 1793be0e5c09SChris Mason int i; 1794aa5d6bedSChris Mason int ret = 0; 17955f39d397SChris Mason struct extent_buffer *t; 17965f39d397SChris Mason 1797234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1798be0e5c09SChris Mason int tslot = path->slots[i]; 1799eb60ceacSChris Mason if (!path->nodes[i]) 1800be0e5c09SChris Mason break; 18015f39d397SChris Mason t = path->nodes[i]; 18025f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 1803d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 1804be0e5c09SChris Mason if (tslot != 0) 1805be0e5c09SChris Mason break; 1806be0e5c09SChris Mason } 1807aa5d6bedSChris Mason return ret; 1808be0e5c09SChris Mason } 1809be0e5c09SChris Mason 181074123bd7SChris Mason /* 181131840ae1SZheng Yan * update item key. 181231840ae1SZheng Yan * 181331840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 181431840ae1SZheng Yan * that the new key won't break the order 181531840ae1SZheng Yan */ 181631840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans, 181731840ae1SZheng Yan struct btrfs_root *root, struct btrfs_path *path, 181831840ae1SZheng Yan struct btrfs_key *new_key) 181931840ae1SZheng Yan { 182031840ae1SZheng Yan struct btrfs_disk_key disk_key; 182131840ae1SZheng Yan struct extent_buffer *eb; 182231840ae1SZheng Yan int slot; 182331840ae1SZheng Yan 182431840ae1SZheng Yan eb = path->nodes[0]; 182531840ae1SZheng Yan slot = path->slots[0]; 182631840ae1SZheng Yan if (slot > 0) { 182731840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 182831840ae1SZheng Yan if (comp_keys(&disk_key, new_key) >= 0) 182931840ae1SZheng Yan return -1; 183031840ae1SZheng Yan } 183131840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 183231840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 183331840ae1SZheng Yan if (comp_keys(&disk_key, new_key) <= 0) 183431840ae1SZheng Yan return -1; 183531840ae1SZheng Yan } 183631840ae1SZheng Yan 183731840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 183831840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 183931840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 184031840ae1SZheng Yan if (slot == 0) 184131840ae1SZheng Yan fixup_low_keys(trans, root, path, &disk_key, 1); 184231840ae1SZheng Yan return 0; 184331840ae1SZheng Yan } 184431840ae1SZheng Yan 184531840ae1SZheng Yan /* 184674123bd7SChris Mason * try to push data from one node into the next node left in the 184779f95c82SChris Mason * tree. 1848aa5d6bedSChris Mason * 1849aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 1850aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 185174123bd7SChris Mason */ 185298ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 185398ed5174SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 1854971a1f66SChris Mason struct extent_buffer *src, int empty) 1855be0e5c09SChris Mason { 1856be0e5c09SChris Mason int push_items = 0; 1857bb803951SChris Mason int src_nritems; 1858bb803951SChris Mason int dst_nritems; 1859aa5d6bedSChris Mason int ret = 0; 1860be0e5c09SChris Mason 18615f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 18625f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1863123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 18647bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 18657bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 186654aa1f4dSChris Mason 1867bce4eae9SChris Mason if (!empty && src_nritems <= 8) 1868971a1f66SChris Mason return 1; 1869971a1f66SChris Mason 1870d397712bSChris Mason if (push_items <= 0) 1871be0e5c09SChris Mason return 1; 1872be0e5c09SChris Mason 1873bce4eae9SChris Mason if (empty) { 1874971a1f66SChris Mason push_items = min(src_nritems, push_items); 1875bce4eae9SChris Mason if (push_items < src_nritems) { 1876bce4eae9SChris Mason /* leave at least 8 pointers in the node if 1877bce4eae9SChris Mason * we aren't going to empty it 1878bce4eae9SChris Mason */ 1879bce4eae9SChris Mason if (src_nritems - push_items < 8) { 1880bce4eae9SChris Mason if (push_items <= 8) 1881bce4eae9SChris Mason return 1; 1882bce4eae9SChris Mason push_items -= 8; 1883bce4eae9SChris Mason } 1884bce4eae9SChris Mason } 1885bce4eae9SChris Mason } else 1886bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 188779f95c82SChris Mason 18885f39d397SChris Mason copy_extent_buffer(dst, src, 18895f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 18905f39d397SChris Mason btrfs_node_key_ptr_offset(0), 1891123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 18925f39d397SChris Mason 1893bb803951SChris Mason if (push_items < src_nritems) { 18945f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 18955f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 1896e2fa7227SChris Mason (src_nritems - push_items) * 1897123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 1898bb803951SChris Mason } 18995f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 19005f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 19015f39d397SChris Mason btrfs_mark_buffer_dirty(src); 19025f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 190331840ae1SZheng Yan 1904bb803951SChris Mason return ret; 1905be0e5c09SChris Mason } 1906be0e5c09SChris Mason 190797571fd0SChris Mason /* 190879f95c82SChris Mason * try to push data from one node into the next node right in the 190979f95c82SChris Mason * tree. 191079f95c82SChris Mason * 191179f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 191279f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 191379f95c82SChris Mason * 191479f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 191579f95c82SChris Mason */ 19165f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 19175f39d397SChris Mason struct btrfs_root *root, 19185f39d397SChris Mason struct extent_buffer *dst, 19195f39d397SChris Mason struct extent_buffer *src) 192079f95c82SChris Mason { 192179f95c82SChris Mason int push_items = 0; 192279f95c82SChris Mason int max_push; 192379f95c82SChris Mason int src_nritems; 192479f95c82SChris Mason int dst_nritems; 192579f95c82SChris Mason int ret = 0; 192679f95c82SChris Mason 19277bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 19287bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 19297bb86316SChris Mason 19305f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 19315f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1932123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 1933d397712bSChris Mason if (push_items <= 0) 193479f95c82SChris Mason return 1; 1935bce4eae9SChris Mason 1936d397712bSChris Mason if (src_nritems < 4) 1937bce4eae9SChris Mason return 1; 193879f95c82SChris Mason 193979f95c82SChris Mason max_push = src_nritems / 2 + 1; 194079f95c82SChris Mason /* don't try to empty the node */ 1941d397712bSChris Mason if (max_push >= src_nritems) 194279f95c82SChris Mason return 1; 1943252c38f0SYan 194479f95c82SChris Mason if (max_push < push_items) 194579f95c82SChris Mason push_items = max_push; 194679f95c82SChris Mason 19475f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 19485f39d397SChris Mason btrfs_node_key_ptr_offset(0), 19495f39d397SChris Mason (dst_nritems) * 19505f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 1951d6025579SChris Mason 19525f39d397SChris Mason copy_extent_buffer(dst, src, 19535f39d397SChris Mason btrfs_node_key_ptr_offset(0), 19545f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 1955123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 195679f95c82SChris Mason 19575f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 19585f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 195979f95c82SChris Mason 19605f39d397SChris Mason btrfs_mark_buffer_dirty(src); 19615f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 196231840ae1SZheng Yan 196379f95c82SChris Mason return ret; 196479f95c82SChris Mason } 196579f95c82SChris Mason 196679f95c82SChris Mason /* 196797571fd0SChris Mason * helper function to insert a new root level in the tree. 196897571fd0SChris Mason * A new node is allocated, and a single item is inserted to 196997571fd0SChris Mason * point to the existing root 1970aa5d6bedSChris Mason * 1971aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 197297571fd0SChris Mason */ 1973d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans, 19745f39d397SChris Mason struct btrfs_root *root, 19755f39d397SChris Mason struct btrfs_path *path, int level) 197674123bd7SChris Mason { 19777bb86316SChris Mason u64 lower_gen; 19785f39d397SChris Mason struct extent_buffer *lower; 19795f39d397SChris Mason struct extent_buffer *c; 1980925baeddSChris Mason struct extent_buffer *old; 19815f39d397SChris Mason struct btrfs_disk_key lower_key; 19825c680ed6SChris Mason 19835c680ed6SChris Mason BUG_ON(path->nodes[level]); 19845c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 19855c680ed6SChris Mason 19867bb86316SChris Mason lower = path->nodes[level-1]; 19877bb86316SChris Mason if (level == 1) 19887bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 19897bb86316SChris Mason else 19907bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 19917bb86316SChris Mason 199231840ae1SZheng Yan c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 19935d4f98a2SYan Zheng root->root_key.objectid, &lower_key, 1994ad3d81baSChristoph Hellwig level, root->node->start, 0); 19955f39d397SChris Mason if (IS_ERR(c)) 19965f39d397SChris Mason return PTR_ERR(c); 1997925baeddSChris Mason 1998f0486c68SYan, Zheng root_add_used(root, root->nodesize); 1999f0486c68SYan, Zheng 20005d4f98a2SYan Zheng memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); 20015f39d397SChris Mason btrfs_set_header_nritems(c, 1); 20025f39d397SChris Mason btrfs_set_header_level(c, level); 2003db94535dSChris Mason btrfs_set_header_bytenr(c, c->start); 20045f39d397SChris Mason btrfs_set_header_generation(c, trans->transid); 20055d4f98a2SYan Zheng btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); 20065f39d397SChris Mason btrfs_set_header_owner(c, root->root_key.objectid); 2007d5719762SChris Mason 20085f39d397SChris Mason write_extent_buffer(c, root->fs_info->fsid, 20095f39d397SChris Mason (unsigned long)btrfs_header_fsid(c), 20105f39d397SChris Mason BTRFS_FSID_SIZE); 2011e17cade2SChris Mason 2012e17cade2SChris Mason write_extent_buffer(c, root->fs_info->chunk_tree_uuid, 2013e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(c), 2014e17cade2SChris Mason BTRFS_UUID_SIZE); 2015e17cade2SChris Mason 20165f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 2017db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 20187bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 201931840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 20207bb86316SChris Mason 20217bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 20225f39d397SChris Mason 20235f39d397SChris Mason btrfs_mark_buffer_dirty(c); 2024d5719762SChris Mason 2025925baeddSChris Mason old = root->node; 2026240f62c8SChris Mason rcu_assign_pointer(root->node, c); 2027925baeddSChris Mason 2028925baeddSChris Mason /* the super has an extra ref to root->node */ 2029925baeddSChris Mason free_extent_buffer(old); 2030925baeddSChris Mason 20310b86a832SChris Mason add_root_to_dirty_list(root); 20325f39d397SChris Mason extent_buffer_get(c); 20335f39d397SChris Mason path->nodes[level] = c; 2034925baeddSChris Mason path->locks[level] = 1; 203574123bd7SChris Mason path->slots[level] = 0; 203674123bd7SChris Mason return 0; 203774123bd7SChris Mason } 20385c680ed6SChris Mason 20395c680ed6SChris Mason /* 20405c680ed6SChris Mason * worker function to insert a single pointer in a node. 20415c680ed6SChris Mason * the node should have enough room for the pointer already 204297571fd0SChris Mason * 20435c680ed6SChris Mason * slot and level indicate where you want the key to go, and 20445c680ed6SChris Mason * blocknr is the block the key points to. 2045aa5d6bedSChris Mason * 2046aa5d6bedSChris Mason * returns zero on success and < 0 on any error 20475c680ed6SChris Mason */ 2048e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root 2049e089f05cSChris Mason *root, struct btrfs_path *path, struct btrfs_disk_key 2050db94535dSChris Mason *key, u64 bytenr, int slot, int level) 20515c680ed6SChris Mason { 20525f39d397SChris Mason struct extent_buffer *lower; 20535c680ed6SChris Mason int nritems; 20545c680ed6SChris Mason 20555c680ed6SChris Mason BUG_ON(!path->nodes[level]); 2056f0486c68SYan, Zheng btrfs_assert_tree_locked(path->nodes[level]); 20575f39d397SChris Mason lower = path->nodes[level]; 20585f39d397SChris Mason nritems = btrfs_header_nritems(lower); 2059c293498bSStoyan Gaydarov BUG_ON(slot > nritems); 2060123abc88SChris Mason if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) 206174123bd7SChris Mason BUG(); 206274123bd7SChris Mason if (slot != nritems) { 20635f39d397SChris Mason memmove_extent_buffer(lower, 20645f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 20655f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 2066123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 206774123bd7SChris Mason } 20685f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 2069db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 207074493f7aSChris Mason WARN_ON(trans->transid == 0); 207174493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 20725f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 20735f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 207474123bd7SChris Mason return 0; 207574123bd7SChris Mason } 207674123bd7SChris Mason 207797571fd0SChris Mason /* 207897571fd0SChris Mason * split the node at the specified level in path in two. 207997571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 208097571fd0SChris Mason * 208197571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 208297571fd0SChris Mason * left and right, if either one works, it returns right away. 2083aa5d6bedSChris Mason * 2084aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 208597571fd0SChris Mason */ 2086e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 2087e02119d5SChris Mason struct btrfs_root *root, 2088e02119d5SChris Mason struct btrfs_path *path, int level) 2089be0e5c09SChris Mason { 20905f39d397SChris Mason struct extent_buffer *c; 20915f39d397SChris Mason struct extent_buffer *split; 20925f39d397SChris Mason struct btrfs_disk_key disk_key; 2093be0e5c09SChris Mason int mid; 20945c680ed6SChris Mason int ret; 2095aa5d6bedSChris Mason int wret; 20967518a238SChris Mason u32 c_nritems; 2097be0e5c09SChris Mason 20985f39d397SChris Mason c = path->nodes[level]; 20997bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 21005f39d397SChris Mason if (c == root->node) { 21015c680ed6SChris Mason /* trying to split the root, lets make a new one */ 2102e089f05cSChris Mason ret = insert_new_root(trans, root, path, level + 1); 21035c680ed6SChris Mason if (ret) 21045c680ed6SChris Mason return ret; 2105b3612421SChris Mason } else { 2106e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 21075f39d397SChris Mason c = path->nodes[level]; 21085f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 2109c448acf0SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) 2110e66f709bSChris Mason return 0; 211154aa1f4dSChris Mason if (ret < 0) 211254aa1f4dSChris Mason return ret; 21135c680ed6SChris Mason } 2114e66f709bSChris Mason 21155f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 21165d4f98a2SYan Zheng mid = (c_nritems + 1) / 2; 21175d4f98a2SYan Zheng btrfs_node_key(c, &disk_key, mid); 21187bb86316SChris Mason 21195d4f98a2SYan Zheng split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 21207bb86316SChris Mason root->root_key.objectid, 21215d4f98a2SYan Zheng &disk_key, level, c->start, 0); 21225f39d397SChris Mason if (IS_ERR(split)) 21235f39d397SChris Mason return PTR_ERR(split); 212454aa1f4dSChris Mason 2125f0486c68SYan, Zheng root_add_used(root, root->nodesize); 2126f0486c68SYan, Zheng 21275d4f98a2SYan Zheng memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); 21285f39d397SChris Mason btrfs_set_header_level(split, btrfs_header_level(c)); 2129db94535dSChris Mason btrfs_set_header_bytenr(split, split->start); 21305f39d397SChris Mason btrfs_set_header_generation(split, trans->transid); 21315d4f98a2SYan Zheng btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); 21325f39d397SChris Mason btrfs_set_header_owner(split, root->root_key.objectid); 21335f39d397SChris Mason write_extent_buffer(split, root->fs_info->fsid, 21345f39d397SChris Mason (unsigned long)btrfs_header_fsid(split), 21355f39d397SChris Mason BTRFS_FSID_SIZE); 2136e17cade2SChris Mason write_extent_buffer(split, root->fs_info->chunk_tree_uuid, 2137e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(split), 2138e17cade2SChris Mason BTRFS_UUID_SIZE); 21395f39d397SChris Mason 21405f39d397SChris Mason 21415f39d397SChris Mason copy_extent_buffer(split, c, 21425f39d397SChris Mason btrfs_node_key_ptr_offset(0), 21435f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 2144123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 21455f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 21465f39d397SChris Mason btrfs_set_header_nritems(c, mid); 2147aa5d6bedSChris Mason ret = 0; 2148aa5d6bedSChris Mason 21495f39d397SChris Mason btrfs_mark_buffer_dirty(c); 21505f39d397SChris Mason btrfs_mark_buffer_dirty(split); 21515f39d397SChris Mason 2152db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, split->start, 21535f39d397SChris Mason path->slots[level + 1] + 1, 2154123abc88SChris Mason level + 1); 2155aa5d6bedSChris Mason if (wret) 2156aa5d6bedSChris Mason ret = wret; 2157aa5d6bedSChris Mason 21585de08d7dSChris Mason if (path->slots[level] >= mid) { 21595c680ed6SChris Mason path->slots[level] -= mid; 2160925baeddSChris Mason btrfs_tree_unlock(c); 21615f39d397SChris Mason free_extent_buffer(c); 21625f39d397SChris Mason path->nodes[level] = split; 21635c680ed6SChris Mason path->slots[level + 1] += 1; 2164eb60ceacSChris Mason } else { 2165925baeddSChris Mason btrfs_tree_unlock(split); 21665f39d397SChris Mason free_extent_buffer(split); 2167be0e5c09SChris Mason } 2168aa5d6bedSChris Mason return ret; 2169be0e5c09SChris Mason } 2170be0e5c09SChris Mason 217174123bd7SChris Mason /* 217274123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 217374123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 217474123bd7SChris Mason * space used both by the item structs and the item data 217574123bd7SChris Mason */ 21765f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 2177be0e5c09SChris Mason { 2178be0e5c09SChris Mason int data_len; 21795f39d397SChris Mason int nritems = btrfs_header_nritems(l); 2180d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 2181be0e5c09SChris Mason 2182be0e5c09SChris Mason if (!nr) 2183be0e5c09SChris Mason return 0; 21845f39d397SChris Mason data_len = btrfs_item_end_nr(l, start); 21855f39d397SChris Mason data_len = data_len - btrfs_item_offset_nr(l, end); 21860783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 2187d4dbff95SChris Mason WARN_ON(data_len < 0); 2188be0e5c09SChris Mason return data_len; 2189be0e5c09SChris Mason } 2190be0e5c09SChris Mason 219174123bd7SChris Mason /* 2192d4dbff95SChris Mason * The space between the end of the leaf items and 2193d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 2194d4dbff95SChris Mason * the leaf has left for both items and data 2195d4dbff95SChris Mason */ 2196d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root, 2197e02119d5SChris Mason struct extent_buffer *leaf) 2198d4dbff95SChris Mason { 21995f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 22005f39d397SChris Mason int ret; 22015f39d397SChris Mason ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); 22025f39d397SChris Mason if (ret < 0) { 2203d397712bSChris Mason printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " 2204d397712bSChris Mason "used %d nritems %d\n", 2205ae2f5411SJens Axboe ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), 22065f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 22075f39d397SChris Mason } 22085f39d397SChris Mason return ret; 2209d4dbff95SChris Mason } 2210d4dbff95SChris Mason 221199d8f83cSChris Mason /* 221299d8f83cSChris Mason * min slot controls the lowest index we're willing to push to the 221399d8f83cSChris Mason * right. We'll push up to and including min_slot, but no lower 221499d8f83cSChris Mason */ 221544871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, 221644871b1bSChris Mason struct btrfs_root *root, 221744871b1bSChris Mason struct btrfs_path *path, 221844871b1bSChris Mason int data_size, int empty, 221944871b1bSChris Mason struct extent_buffer *right, 222099d8f83cSChris Mason int free_space, u32 left_nritems, 222199d8f83cSChris Mason u32 min_slot) 222200ec4c51SChris Mason { 22235f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 222444871b1bSChris Mason struct extent_buffer *upper = path->nodes[1]; 22255f39d397SChris Mason struct btrfs_disk_key disk_key; 222600ec4c51SChris Mason int slot; 222734a38218SChris Mason u32 i; 222800ec4c51SChris Mason int push_space = 0; 222900ec4c51SChris Mason int push_items = 0; 22300783fcfcSChris Mason struct btrfs_item *item; 223134a38218SChris Mason u32 nr; 22327518a238SChris Mason u32 right_nritems; 22335f39d397SChris Mason u32 data_end; 2234db94535dSChris Mason u32 this_item_size; 223500ec4c51SChris Mason 223634a38218SChris Mason if (empty) 223734a38218SChris Mason nr = 0; 223834a38218SChris Mason else 223999d8f83cSChris Mason nr = max_t(u32, 1, min_slot); 224034a38218SChris Mason 224131840ae1SZheng Yan if (path->slots[0] >= left_nritems) 224287b29b20SYan Zheng push_space += data_size; 224331840ae1SZheng Yan 224444871b1bSChris Mason slot = path->slots[1]; 224534a38218SChris Mason i = left_nritems - 1; 224634a38218SChris Mason while (i >= nr) { 22475f39d397SChris Mason item = btrfs_item_nr(left, i); 2248db94535dSChris Mason 224931840ae1SZheng Yan if (!empty && push_items > 0) { 225031840ae1SZheng Yan if (path->slots[0] > i) 225131840ae1SZheng Yan break; 225231840ae1SZheng Yan if (path->slots[0] == i) { 225331840ae1SZheng Yan int space = btrfs_leaf_free_space(root, left); 225431840ae1SZheng Yan if (space + push_space * 2 > free_space) 225531840ae1SZheng Yan break; 225631840ae1SZheng Yan } 225731840ae1SZheng Yan } 225831840ae1SZheng Yan 225900ec4c51SChris Mason if (path->slots[0] == i) 226087b29b20SYan Zheng push_space += data_size; 2261db94535dSChris Mason 2262db94535dSChris Mason if (!left->map_token) { 2263db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 2264db94535dSChris Mason sizeof(struct btrfs_item), 2265db94535dSChris Mason &left->map_token, &left->kaddr, 2266db94535dSChris Mason &left->map_start, &left->map_len, 2267db94535dSChris Mason KM_USER1); 2268db94535dSChris Mason } 2269db94535dSChris Mason 2270db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 2271db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 227200ec4c51SChris Mason break; 227331840ae1SZheng Yan 227400ec4c51SChris Mason push_items++; 2275db94535dSChris Mason push_space += this_item_size + sizeof(*item); 227634a38218SChris Mason if (i == 0) 227734a38218SChris Mason break; 227834a38218SChris Mason i--; 2279db94535dSChris Mason } 2280db94535dSChris Mason if (left->map_token) { 2281db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 2282db94535dSChris Mason left->map_token = NULL; 228300ec4c51SChris Mason } 22845f39d397SChris Mason 2285925baeddSChris Mason if (push_items == 0) 2286925baeddSChris Mason goto out_unlock; 22875f39d397SChris Mason 228834a38218SChris Mason if (!empty && push_items == left_nritems) 2289a429e513SChris Mason WARN_ON(1); 22905f39d397SChris Mason 229100ec4c51SChris Mason /* push left to right */ 22925f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 229334a38218SChris Mason 22945f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 2295123abc88SChris Mason push_space -= leaf_data_end(root, left); 22965f39d397SChris Mason 229700ec4c51SChris Mason /* make room in the right data area */ 22985f39d397SChris Mason data_end = leaf_data_end(root, right); 22995f39d397SChris Mason memmove_extent_buffer(right, 23005f39d397SChris Mason btrfs_leaf_data(right) + data_end - push_space, 23015f39d397SChris Mason btrfs_leaf_data(right) + data_end, 23025f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root) - data_end); 23035f39d397SChris Mason 230400ec4c51SChris Mason /* copy from the left data area */ 23055f39d397SChris Mason copy_extent_buffer(right, left, btrfs_leaf_data(right) + 2306d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2307d6025579SChris Mason btrfs_leaf_data(left) + leaf_data_end(root, left), 2308d6025579SChris Mason push_space); 23095f39d397SChris Mason 23105f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 23115f39d397SChris Mason btrfs_item_nr_offset(0), 23120783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 23135f39d397SChris Mason 231400ec4c51SChris Mason /* copy the items from left to right */ 23155f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 23165f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 23170783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 231800ec4c51SChris Mason 231900ec4c51SChris Mason /* update the item pointers */ 23207518a238SChris Mason right_nritems += push_items; 23215f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 2322123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 23237518a238SChris Mason for (i = 0; i < right_nritems; i++) { 23245f39d397SChris Mason item = btrfs_item_nr(right, i); 2325db94535dSChris Mason if (!right->map_token) { 2326db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2327db94535dSChris Mason sizeof(struct btrfs_item), 2328db94535dSChris Mason &right->map_token, &right->kaddr, 2329db94535dSChris Mason &right->map_start, &right->map_len, 2330db94535dSChris Mason KM_USER1); 2331db94535dSChris Mason } 2332db94535dSChris Mason push_space -= btrfs_item_size(right, item); 2333db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2334db94535dSChris Mason } 2335db94535dSChris Mason 2336db94535dSChris Mason if (right->map_token) { 2337db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2338db94535dSChris Mason right->map_token = NULL; 233900ec4c51SChris Mason } 23407518a238SChris Mason left_nritems -= push_items; 23415f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 234200ec4c51SChris Mason 234334a38218SChris Mason if (left_nritems) 23445f39d397SChris Mason btrfs_mark_buffer_dirty(left); 2345f0486c68SYan, Zheng else 2346f0486c68SYan, Zheng clean_tree_block(trans, root, left); 2347f0486c68SYan, Zheng 23485f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2349a429e513SChris Mason 23505f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 23515f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 2352d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 235302217ed2SChris Mason 235400ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 23557518a238SChris Mason if (path->slots[0] >= left_nritems) { 23567518a238SChris Mason path->slots[0] -= left_nritems; 2357925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 2358925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 2359925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 23605f39d397SChris Mason free_extent_buffer(path->nodes[0]); 23615f39d397SChris Mason path->nodes[0] = right; 236200ec4c51SChris Mason path->slots[1] += 1; 236300ec4c51SChris Mason } else { 2364925baeddSChris Mason btrfs_tree_unlock(right); 23655f39d397SChris Mason free_extent_buffer(right); 236600ec4c51SChris Mason } 236700ec4c51SChris Mason return 0; 2368925baeddSChris Mason 2369925baeddSChris Mason out_unlock: 2370925baeddSChris Mason btrfs_tree_unlock(right); 2371925baeddSChris Mason free_extent_buffer(right); 2372925baeddSChris Mason return 1; 237300ec4c51SChris Mason } 2374925baeddSChris Mason 237500ec4c51SChris Mason /* 237644871b1bSChris Mason * push some data in the path leaf to the right, trying to free up at 237774123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 237844871b1bSChris Mason * 237944871b1bSChris Mason * returns 1 if the push failed because the other node didn't have enough 238044871b1bSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 238199d8f83cSChris Mason * 238299d8f83cSChris Mason * this will push starting from min_slot to the end of the leaf. It won't 238399d8f83cSChris Mason * push any slot lower than min_slot 238474123bd7SChris Mason */ 238544871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 238699d8f83cSChris Mason *root, struct btrfs_path *path, 238799d8f83cSChris Mason int min_data_size, int data_size, 238899d8f83cSChris Mason int empty, u32 min_slot) 2389be0e5c09SChris Mason { 239044871b1bSChris Mason struct extent_buffer *left = path->nodes[0]; 239144871b1bSChris Mason struct extent_buffer *right; 239244871b1bSChris Mason struct extent_buffer *upper; 239344871b1bSChris Mason int slot; 239444871b1bSChris Mason int free_space; 239544871b1bSChris Mason u32 left_nritems; 239644871b1bSChris Mason int ret; 239744871b1bSChris Mason 239844871b1bSChris Mason if (!path->nodes[1]) 239944871b1bSChris Mason return 1; 240044871b1bSChris Mason 240144871b1bSChris Mason slot = path->slots[1]; 240244871b1bSChris Mason upper = path->nodes[1]; 240344871b1bSChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 240444871b1bSChris Mason return 1; 240544871b1bSChris Mason 240644871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 240744871b1bSChris Mason 240844871b1bSChris Mason right = read_node_slot(root, upper, slot + 1); 240991ca338dSTsutomu Itoh if (right == NULL) 241091ca338dSTsutomu Itoh return 1; 241191ca338dSTsutomu Itoh 241244871b1bSChris Mason btrfs_tree_lock(right); 241344871b1bSChris Mason btrfs_set_lock_blocking(right); 241444871b1bSChris Mason 241544871b1bSChris Mason free_space = btrfs_leaf_free_space(root, right); 241644871b1bSChris Mason if (free_space < data_size) 241744871b1bSChris Mason goto out_unlock; 241844871b1bSChris Mason 241944871b1bSChris Mason /* cow and double check */ 242044871b1bSChris Mason ret = btrfs_cow_block(trans, root, right, upper, 242144871b1bSChris Mason slot + 1, &right); 242244871b1bSChris Mason if (ret) 242344871b1bSChris Mason goto out_unlock; 242444871b1bSChris Mason 242544871b1bSChris Mason free_space = btrfs_leaf_free_space(root, right); 242644871b1bSChris Mason if (free_space < data_size) 242744871b1bSChris Mason goto out_unlock; 242844871b1bSChris Mason 242944871b1bSChris Mason left_nritems = btrfs_header_nritems(left); 243044871b1bSChris Mason if (left_nritems == 0) 243144871b1bSChris Mason goto out_unlock; 243244871b1bSChris Mason 243399d8f83cSChris Mason return __push_leaf_right(trans, root, path, min_data_size, empty, 243499d8f83cSChris Mason right, free_space, left_nritems, min_slot); 243544871b1bSChris Mason out_unlock: 243644871b1bSChris Mason btrfs_tree_unlock(right); 243744871b1bSChris Mason free_extent_buffer(right); 243844871b1bSChris Mason return 1; 243944871b1bSChris Mason } 244044871b1bSChris Mason 244144871b1bSChris Mason /* 244244871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 244344871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 244499d8f83cSChris Mason * 244599d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 244699d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the 244799d8f83cSChris Mason * items 244844871b1bSChris Mason */ 244944871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, 245044871b1bSChris Mason struct btrfs_root *root, 245144871b1bSChris Mason struct btrfs_path *path, int data_size, 245244871b1bSChris Mason int empty, struct extent_buffer *left, 245399d8f83cSChris Mason int free_space, u32 right_nritems, 245499d8f83cSChris Mason u32 max_slot) 245544871b1bSChris Mason { 24565f39d397SChris Mason struct btrfs_disk_key disk_key; 24575f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 2458be0e5c09SChris Mason int i; 2459be0e5c09SChris Mason int push_space = 0; 2460be0e5c09SChris Mason int push_items = 0; 24610783fcfcSChris Mason struct btrfs_item *item; 24627518a238SChris Mason u32 old_left_nritems; 246334a38218SChris Mason u32 nr; 2464aa5d6bedSChris Mason int ret = 0; 2465aa5d6bedSChris Mason int wret; 2466db94535dSChris Mason u32 this_item_size; 2467db94535dSChris Mason u32 old_left_item_size; 2468be0e5c09SChris Mason 246934a38218SChris Mason if (empty) 247099d8f83cSChris Mason nr = min(right_nritems, max_slot); 247134a38218SChris Mason else 247299d8f83cSChris Mason nr = min(right_nritems - 1, max_slot); 247334a38218SChris Mason 247434a38218SChris Mason for (i = 0; i < nr; i++) { 24755f39d397SChris Mason item = btrfs_item_nr(right, i); 2476db94535dSChris Mason if (!right->map_token) { 2477db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2478db94535dSChris Mason sizeof(struct btrfs_item), 2479db94535dSChris Mason &right->map_token, &right->kaddr, 2480db94535dSChris Mason &right->map_start, &right->map_len, 2481db94535dSChris Mason KM_USER1); 2482db94535dSChris Mason } 2483db94535dSChris Mason 248431840ae1SZheng Yan if (!empty && push_items > 0) { 248531840ae1SZheng Yan if (path->slots[0] < i) 248631840ae1SZheng Yan break; 248731840ae1SZheng Yan if (path->slots[0] == i) { 248831840ae1SZheng Yan int space = btrfs_leaf_free_space(root, right); 248931840ae1SZheng Yan if (space + push_space * 2 > free_space) 249031840ae1SZheng Yan break; 249131840ae1SZheng Yan } 249231840ae1SZheng Yan } 249331840ae1SZheng Yan 2494be0e5c09SChris Mason if (path->slots[0] == i) 249587b29b20SYan Zheng push_space += data_size; 2496db94535dSChris Mason 2497db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 2498db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 2499be0e5c09SChris Mason break; 2500db94535dSChris Mason 2501be0e5c09SChris Mason push_items++; 2502db94535dSChris Mason push_space += this_item_size + sizeof(*item); 2503be0e5c09SChris Mason } 2504db94535dSChris Mason 2505db94535dSChris Mason if (right->map_token) { 2506db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2507db94535dSChris Mason right->map_token = NULL; 2508db94535dSChris Mason } 2509db94535dSChris Mason 2510be0e5c09SChris Mason if (push_items == 0) { 2511925baeddSChris Mason ret = 1; 2512925baeddSChris Mason goto out; 2513be0e5c09SChris Mason } 251434a38218SChris Mason if (!empty && push_items == btrfs_header_nritems(right)) 2515a429e513SChris Mason WARN_ON(1); 25165f39d397SChris Mason 2517be0e5c09SChris Mason /* push data from right to left */ 25185f39d397SChris Mason copy_extent_buffer(left, right, 25195f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 25205f39d397SChris Mason btrfs_item_nr_offset(0), 25215f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 25225f39d397SChris Mason 2523123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root) - 25245f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1); 25255f39d397SChris Mason 25265f39d397SChris Mason copy_extent_buffer(left, right, btrfs_leaf_data(left) + 2527d6025579SChris Mason leaf_data_end(root, left) - push_space, 2528123abc88SChris Mason btrfs_leaf_data(right) + 25295f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 2530be0e5c09SChris Mason push_space); 25315f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 253287b29b20SYan Zheng BUG_ON(old_left_nritems <= 0); 2533eb60ceacSChris Mason 2534db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 2535be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 25365f39d397SChris Mason u32 ioff; 2537db94535dSChris Mason 25385f39d397SChris Mason item = btrfs_item_nr(left, i); 2539db94535dSChris Mason if (!left->map_token) { 2540db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 2541db94535dSChris Mason sizeof(struct btrfs_item), 2542db94535dSChris Mason &left->map_token, &left->kaddr, 2543db94535dSChris Mason &left->map_start, &left->map_len, 2544db94535dSChris Mason KM_USER1); 2545db94535dSChris Mason } 2546db94535dSChris Mason 25475f39d397SChris Mason ioff = btrfs_item_offset(left, item); 25485f39d397SChris Mason btrfs_set_item_offset(left, item, 2549db94535dSChris Mason ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); 2550be0e5c09SChris Mason } 25515f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 2552db94535dSChris Mason if (left->map_token) { 2553db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 2554db94535dSChris Mason left->map_token = NULL; 2555db94535dSChris Mason } 2556be0e5c09SChris Mason 2557be0e5c09SChris Mason /* fixup right node */ 255834a38218SChris Mason if (push_items > right_nritems) { 2559d397712bSChris Mason printk(KERN_CRIT "push items %d nr %u\n", push_items, 2560d397712bSChris Mason right_nritems); 256134a38218SChris Mason WARN_ON(1); 256234a38218SChris Mason } 256334a38218SChris Mason 256434a38218SChris Mason if (push_items < right_nritems) { 25655f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 2566123abc88SChris Mason leaf_data_end(root, right); 25675f39d397SChris Mason memmove_extent_buffer(right, btrfs_leaf_data(right) + 2568d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2569d6025579SChris Mason btrfs_leaf_data(right) + 2570123abc88SChris Mason leaf_data_end(root, right), push_space); 25715f39d397SChris Mason 25725f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 25735f39d397SChris Mason btrfs_item_nr_offset(push_items), 25745f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 25750783fcfcSChris Mason sizeof(struct btrfs_item)); 257634a38218SChris Mason } 2577eef1c494SYan right_nritems -= push_items; 2578eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 2579123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 25805f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 25815f39d397SChris Mason item = btrfs_item_nr(right, i); 2582db94535dSChris Mason 2583db94535dSChris Mason if (!right->map_token) { 2584db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2585db94535dSChris Mason sizeof(struct btrfs_item), 2586db94535dSChris Mason &right->map_token, &right->kaddr, 2587db94535dSChris Mason &right->map_start, &right->map_len, 2588db94535dSChris Mason KM_USER1); 2589db94535dSChris Mason } 2590db94535dSChris Mason 2591db94535dSChris Mason push_space = push_space - btrfs_item_size(right, item); 2592db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2593db94535dSChris Mason } 2594db94535dSChris Mason if (right->map_token) { 2595db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2596db94535dSChris Mason right->map_token = NULL; 2597be0e5c09SChris Mason } 2598eb60ceacSChris Mason 25995f39d397SChris Mason btrfs_mark_buffer_dirty(left); 260034a38218SChris Mason if (right_nritems) 26015f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2602f0486c68SYan, Zheng else 2603f0486c68SYan, Zheng clean_tree_block(trans, root, right); 2604098f59c2SChris Mason 26055f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 26065f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, 1); 2607aa5d6bedSChris Mason if (wret) 2608aa5d6bedSChris Mason ret = wret; 2609be0e5c09SChris Mason 2610be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 2611be0e5c09SChris Mason if (path->slots[0] < push_items) { 2612be0e5c09SChris Mason path->slots[0] += old_left_nritems; 2613925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 26145f39d397SChris Mason free_extent_buffer(path->nodes[0]); 26155f39d397SChris Mason path->nodes[0] = left; 2616be0e5c09SChris Mason path->slots[1] -= 1; 2617be0e5c09SChris Mason } else { 2618925baeddSChris Mason btrfs_tree_unlock(left); 26195f39d397SChris Mason free_extent_buffer(left); 2620be0e5c09SChris Mason path->slots[0] -= push_items; 2621be0e5c09SChris Mason } 2622eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2623aa5d6bedSChris Mason return ret; 2624925baeddSChris Mason out: 2625925baeddSChris Mason btrfs_tree_unlock(left); 2626925baeddSChris Mason free_extent_buffer(left); 2627925baeddSChris Mason return ret; 2628be0e5c09SChris Mason } 2629be0e5c09SChris Mason 263074123bd7SChris Mason /* 263144871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 263244871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 263399d8f83cSChris Mason * 263499d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 263599d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the 263699d8f83cSChris Mason * items 263744871b1bSChris Mason */ 263844871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 263999d8f83cSChris Mason *root, struct btrfs_path *path, int min_data_size, 264099d8f83cSChris Mason int data_size, int empty, u32 max_slot) 264144871b1bSChris Mason { 264244871b1bSChris Mason struct extent_buffer *right = path->nodes[0]; 264344871b1bSChris Mason struct extent_buffer *left; 264444871b1bSChris Mason int slot; 264544871b1bSChris Mason int free_space; 264644871b1bSChris Mason u32 right_nritems; 264744871b1bSChris Mason int ret = 0; 264844871b1bSChris Mason 264944871b1bSChris Mason slot = path->slots[1]; 265044871b1bSChris Mason if (slot == 0) 265144871b1bSChris Mason return 1; 265244871b1bSChris Mason if (!path->nodes[1]) 265344871b1bSChris Mason return 1; 265444871b1bSChris Mason 265544871b1bSChris Mason right_nritems = btrfs_header_nritems(right); 265644871b1bSChris Mason if (right_nritems == 0) 265744871b1bSChris Mason return 1; 265844871b1bSChris Mason 265944871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 266044871b1bSChris Mason 266144871b1bSChris Mason left = read_node_slot(root, path->nodes[1], slot - 1); 266291ca338dSTsutomu Itoh if (left == NULL) 266391ca338dSTsutomu Itoh return 1; 266491ca338dSTsutomu Itoh 266544871b1bSChris Mason btrfs_tree_lock(left); 266644871b1bSChris Mason btrfs_set_lock_blocking(left); 266744871b1bSChris Mason 266844871b1bSChris Mason free_space = btrfs_leaf_free_space(root, left); 266944871b1bSChris Mason if (free_space < data_size) { 267044871b1bSChris Mason ret = 1; 267144871b1bSChris Mason goto out; 267244871b1bSChris Mason } 267344871b1bSChris Mason 267444871b1bSChris Mason /* cow and double check */ 267544871b1bSChris Mason ret = btrfs_cow_block(trans, root, left, 267644871b1bSChris Mason path->nodes[1], slot - 1, &left); 267744871b1bSChris Mason if (ret) { 267844871b1bSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 267944871b1bSChris Mason ret = 1; 268044871b1bSChris Mason goto out; 268144871b1bSChris Mason } 268244871b1bSChris Mason 268344871b1bSChris Mason free_space = btrfs_leaf_free_space(root, left); 268444871b1bSChris Mason if (free_space < data_size) { 268544871b1bSChris Mason ret = 1; 268644871b1bSChris Mason goto out; 268744871b1bSChris Mason } 268844871b1bSChris Mason 268999d8f83cSChris Mason return __push_leaf_left(trans, root, path, min_data_size, 269099d8f83cSChris Mason empty, left, free_space, right_nritems, 269199d8f83cSChris Mason max_slot); 269244871b1bSChris Mason out: 269344871b1bSChris Mason btrfs_tree_unlock(left); 269444871b1bSChris Mason free_extent_buffer(left); 269544871b1bSChris Mason return ret; 269644871b1bSChris Mason } 269744871b1bSChris Mason 269844871b1bSChris Mason /* 269974123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 270074123bd7SChris Mason * available for the resulting leaf level of the path. 2701aa5d6bedSChris Mason * 2702aa5d6bedSChris Mason * returns 0 if all went well and < 0 on failure. 270374123bd7SChris Mason */ 270444871b1bSChris Mason static noinline int copy_for_split(struct btrfs_trans_handle *trans, 2705e02119d5SChris Mason struct btrfs_root *root, 270644871b1bSChris Mason struct btrfs_path *path, 270744871b1bSChris Mason struct extent_buffer *l, 270844871b1bSChris Mason struct extent_buffer *right, 270944871b1bSChris Mason int slot, int mid, int nritems) 2710be0e5c09SChris Mason { 2711be0e5c09SChris Mason int data_copy_size; 2712be0e5c09SChris Mason int rt_data_off; 2713be0e5c09SChris Mason int i; 2714d4dbff95SChris Mason int ret = 0; 2715aa5d6bedSChris Mason int wret; 2716d4dbff95SChris Mason struct btrfs_disk_key disk_key; 2717be0e5c09SChris Mason 27185f39d397SChris Mason nritems = nritems - mid; 27195f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 27205f39d397SChris Mason data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 27215f39d397SChris Mason 27225f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 27235f39d397SChris Mason btrfs_item_nr_offset(mid), 27245f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 27255f39d397SChris Mason 27265f39d397SChris Mason copy_extent_buffer(right, l, 2727d6025579SChris Mason btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - 2728123abc88SChris Mason data_copy_size, btrfs_leaf_data(l) + 2729123abc88SChris Mason leaf_data_end(root, l), data_copy_size); 273074123bd7SChris Mason 27315f39d397SChris Mason rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - 27325f39d397SChris Mason btrfs_item_end_nr(l, mid); 27335f39d397SChris Mason 27345f39d397SChris Mason for (i = 0; i < nritems; i++) { 27355f39d397SChris Mason struct btrfs_item *item = btrfs_item_nr(right, i); 2736db94535dSChris Mason u32 ioff; 2737db94535dSChris Mason 2738db94535dSChris Mason if (!right->map_token) { 2739db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2740db94535dSChris Mason sizeof(struct btrfs_item), 2741db94535dSChris Mason &right->map_token, &right->kaddr, 2742db94535dSChris Mason &right->map_start, &right->map_len, 2743db94535dSChris Mason KM_USER1); 2744db94535dSChris Mason } 2745db94535dSChris Mason 2746db94535dSChris Mason ioff = btrfs_item_offset(right, item); 27475f39d397SChris Mason btrfs_set_item_offset(right, item, ioff + rt_data_off); 27480783fcfcSChris Mason } 274974123bd7SChris Mason 2750db94535dSChris Mason if (right->map_token) { 2751db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2752db94535dSChris Mason right->map_token = NULL; 2753db94535dSChris Mason } 2754db94535dSChris Mason 27555f39d397SChris Mason btrfs_set_header_nritems(l, mid); 2756aa5d6bedSChris Mason ret = 0; 27575f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 2758db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, right->start, 2759db94535dSChris Mason path->slots[1] + 1, 1); 2760aa5d6bedSChris Mason if (wret) 2761aa5d6bedSChris Mason ret = wret; 27625f39d397SChris Mason 27635f39d397SChris Mason btrfs_mark_buffer_dirty(right); 27645f39d397SChris Mason btrfs_mark_buffer_dirty(l); 2765eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 27665f39d397SChris Mason 2767be0e5c09SChris Mason if (mid <= slot) { 2768925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 27695f39d397SChris Mason free_extent_buffer(path->nodes[0]); 27705f39d397SChris Mason path->nodes[0] = right; 2771be0e5c09SChris Mason path->slots[0] -= mid; 2772be0e5c09SChris Mason path->slots[1] += 1; 2773925baeddSChris Mason } else { 2774925baeddSChris Mason btrfs_tree_unlock(right); 27755f39d397SChris Mason free_extent_buffer(right); 2776925baeddSChris Mason } 27775f39d397SChris Mason 2778eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2779d4dbff95SChris Mason 278044871b1bSChris Mason return ret; 278144871b1bSChris Mason } 278244871b1bSChris Mason 278344871b1bSChris Mason /* 278499d8f83cSChris Mason * double splits happen when we need to insert a big item in the middle 278599d8f83cSChris Mason * of a leaf. A double split can leave us with 3 mostly empty leaves: 278699d8f83cSChris Mason * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] 278799d8f83cSChris Mason * A B C 278899d8f83cSChris Mason * 278999d8f83cSChris Mason * We avoid this by trying to push the items on either side of our target 279099d8f83cSChris Mason * into the adjacent leaves. If all goes well we can avoid the double split 279199d8f83cSChris Mason * completely. 279299d8f83cSChris Mason */ 279399d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans, 279499d8f83cSChris Mason struct btrfs_root *root, 279599d8f83cSChris Mason struct btrfs_path *path, 279699d8f83cSChris Mason int data_size) 279799d8f83cSChris Mason { 279899d8f83cSChris Mason int ret; 279999d8f83cSChris Mason int progress = 0; 280099d8f83cSChris Mason int slot; 280199d8f83cSChris Mason u32 nritems; 280299d8f83cSChris Mason 280399d8f83cSChris Mason slot = path->slots[0]; 280499d8f83cSChris Mason 280599d8f83cSChris Mason /* 280699d8f83cSChris Mason * try to push all the items after our slot into the 280799d8f83cSChris Mason * right leaf 280899d8f83cSChris Mason */ 280999d8f83cSChris Mason ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); 281099d8f83cSChris Mason if (ret < 0) 281199d8f83cSChris Mason return ret; 281299d8f83cSChris Mason 281399d8f83cSChris Mason if (ret == 0) 281499d8f83cSChris Mason progress++; 281599d8f83cSChris Mason 281699d8f83cSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 281799d8f83cSChris Mason /* 281899d8f83cSChris Mason * our goal is to get our slot at the start or end of a leaf. If 281999d8f83cSChris Mason * we've done so we're done 282099d8f83cSChris Mason */ 282199d8f83cSChris Mason if (path->slots[0] == 0 || path->slots[0] == nritems) 282299d8f83cSChris Mason return 0; 282399d8f83cSChris Mason 282499d8f83cSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 282599d8f83cSChris Mason return 0; 282699d8f83cSChris Mason 282799d8f83cSChris Mason /* try to push all the items before our slot into the next leaf */ 282899d8f83cSChris Mason slot = path->slots[0]; 282999d8f83cSChris Mason ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); 283099d8f83cSChris Mason if (ret < 0) 283199d8f83cSChris Mason return ret; 283299d8f83cSChris Mason 283399d8f83cSChris Mason if (ret == 0) 283499d8f83cSChris Mason progress++; 283599d8f83cSChris Mason 283699d8f83cSChris Mason if (progress) 283799d8f83cSChris Mason return 0; 283899d8f83cSChris Mason return 1; 283999d8f83cSChris Mason } 284099d8f83cSChris Mason 284199d8f83cSChris Mason /* 284244871b1bSChris Mason * split the path's leaf in two, making sure there is at least data_size 284344871b1bSChris Mason * available for the resulting leaf level of the path. 284444871b1bSChris Mason * 284544871b1bSChris Mason * returns 0 if all went well and < 0 on failure. 284644871b1bSChris Mason */ 284744871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 284844871b1bSChris Mason struct btrfs_root *root, 284944871b1bSChris Mason struct btrfs_key *ins_key, 285044871b1bSChris Mason struct btrfs_path *path, int data_size, 285144871b1bSChris Mason int extend) 285244871b1bSChris Mason { 28535d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 285444871b1bSChris Mason struct extent_buffer *l; 285544871b1bSChris Mason u32 nritems; 285644871b1bSChris Mason int mid; 285744871b1bSChris Mason int slot; 285844871b1bSChris Mason struct extent_buffer *right; 285944871b1bSChris Mason int ret = 0; 286044871b1bSChris Mason int wret; 28615d4f98a2SYan Zheng int split; 286244871b1bSChris Mason int num_doubles = 0; 286399d8f83cSChris Mason int tried_avoid_double = 0; 286444871b1bSChris Mason 2865a5719521SYan, Zheng l = path->nodes[0]; 2866a5719521SYan, Zheng slot = path->slots[0]; 2867a5719521SYan, Zheng if (extend && data_size + btrfs_item_size_nr(l, slot) + 2868a5719521SYan, Zheng sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) 2869a5719521SYan, Zheng return -EOVERFLOW; 2870a5719521SYan, Zheng 287144871b1bSChris Mason /* first try to make some room by pushing left and right */ 287299d8f83cSChris Mason if (data_size) { 287399d8f83cSChris Mason wret = push_leaf_right(trans, root, path, data_size, 287499d8f83cSChris Mason data_size, 0, 0); 287544871b1bSChris Mason if (wret < 0) 287644871b1bSChris Mason return wret; 287744871b1bSChris Mason if (wret) { 287899d8f83cSChris Mason wret = push_leaf_left(trans, root, path, data_size, 287999d8f83cSChris Mason data_size, 0, (u32)-1); 288044871b1bSChris Mason if (wret < 0) 288144871b1bSChris Mason return wret; 288244871b1bSChris Mason } 288344871b1bSChris Mason l = path->nodes[0]; 288444871b1bSChris Mason 288544871b1bSChris Mason /* did the pushes work? */ 288644871b1bSChris Mason if (btrfs_leaf_free_space(root, l) >= data_size) 288744871b1bSChris Mason return 0; 288844871b1bSChris Mason } 288944871b1bSChris Mason 289044871b1bSChris Mason if (!path->nodes[1]) { 289144871b1bSChris Mason ret = insert_new_root(trans, root, path, 1); 289244871b1bSChris Mason if (ret) 289344871b1bSChris Mason return ret; 289444871b1bSChris Mason } 289544871b1bSChris Mason again: 28965d4f98a2SYan Zheng split = 1; 289744871b1bSChris Mason l = path->nodes[0]; 289844871b1bSChris Mason slot = path->slots[0]; 289944871b1bSChris Mason nritems = btrfs_header_nritems(l); 290044871b1bSChris Mason mid = (nritems + 1) / 2; 290144871b1bSChris Mason 29025d4f98a2SYan Zheng if (mid <= slot) { 29035d4f98a2SYan Zheng if (nritems == 1 || 29045d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + data_size > 29055d4f98a2SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 29065d4f98a2SYan Zheng if (slot >= nritems) { 29075d4f98a2SYan Zheng split = 0; 29085d4f98a2SYan Zheng } else { 29095d4f98a2SYan Zheng mid = slot; 29105d4f98a2SYan Zheng if (mid != nritems && 29115d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 29125d4f98a2SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 291399d8f83cSChris Mason if (data_size && !tried_avoid_double) 291499d8f83cSChris Mason goto push_for_double; 29155d4f98a2SYan Zheng split = 2; 29165d4f98a2SYan Zheng } 29175d4f98a2SYan Zheng } 29185d4f98a2SYan Zheng } 29195d4f98a2SYan Zheng } else { 29205d4f98a2SYan Zheng if (leaf_space_used(l, 0, mid) + data_size > 29215d4f98a2SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 29225d4f98a2SYan Zheng if (!extend && data_size && slot == 0) { 29235d4f98a2SYan Zheng split = 0; 29245d4f98a2SYan Zheng } else if ((extend || !data_size) && slot == 0) { 29255d4f98a2SYan Zheng mid = 1; 29265d4f98a2SYan Zheng } else { 29275d4f98a2SYan Zheng mid = slot; 29285d4f98a2SYan Zheng if (mid != nritems && 29295d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 29305d4f98a2SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 293199d8f83cSChris Mason if (data_size && !tried_avoid_double) 293299d8f83cSChris Mason goto push_for_double; 29335d4f98a2SYan Zheng split = 2 ; 29345d4f98a2SYan Zheng } 29355d4f98a2SYan Zheng } 29365d4f98a2SYan Zheng } 29375d4f98a2SYan Zheng } 29385d4f98a2SYan Zheng 29395d4f98a2SYan Zheng if (split == 0) 29405d4f98a2SYan Zheng btrfs_cpu_key_to_disk(&disk_key, ins_key); 29415d4f98a2SYan Zheng else 29425d4f98a2SYan Zheng btrfs_item_key(l, &disk_key, mid); 29435d4f98a2SYan Zheng 29445d4f98a2SYan Zheng right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 294544871b1bSChris Mason root->root_key.objectid, 29465d4f98a2SYan Zheng &disk_key, 0, l->start, 0); 2947f0486c68SYan, Zheng if (IS_ERR(right)) 294844871b1bSChris Mason return PTR_ERR(right); 2949f0486c68SYan, Zheng 2950f0486c68SYan, Zheng root_add_used(root, root->leafsize); 295144871b1bSChris Mason 295244871b1bSChris Mason memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); 295344871b1bSChris Mason btrfs_set_header_bytenr(right, right->start); 295444871b1bSChris Mason btrfs_set_header_generation(right, trans->transid); 29555d4f98a2SYan Zheng btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); 295644871b1bSChris Mason btrfs_set_header_owner(right, root->root_key.objectid); 295744871b1bSChris Mason btrfs_set_header_level(right, 0); 295844871b1bSChris Mason write_extent_buffer(right, root->fs_info->fsid, 295944871b1bSChris Mason (unsigned long)btrfs_header_fsid(right), 296044871b1bSChris Mason BTRFS_FSID_SIZE); 296144871b1bSChris Mason 296244871b1bSChris Mason write_extent_buffer(right, root->fs_info->chunk_tree_uuid, 296344871b1bSChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(right), 296444871b1bSChris Mason BTRFS_UUID_SIZE); 296544871b1bSChris Mason 29665d4f98a2SYan Zheng if (split == 0) { 296744871b1bSChris Mason if (mid <= slot) { 296844871b1bSChris Mason btrfs_set_header_nritems(right, 0); 296944871b1bSChris Mason wret = insert_ptr(trans, root, path, 297044871b1bSChris Mason &disk_key, right->start, 297144871b1bSChris Mason path->slots[1] + 1, 1); 297244871b1bSChris Mason if (wret) 297344871b1bSChris Mason ret = wret; 297444871b1bSChris Mason 297544871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 297644871b1bSChris Mason free_extent_buffer(path->nodes[0]); 297744871b1bSChris Mason path->nodes[0] = right; 297844871b1bSChris Mason path->slots[0] = 0; 297944871b1bSChris Mason path->slots[1] += 1; 298044871b1bSChris Mason } else { 298144871b1bSChris Mason btrfs_set_header_nritems(right, 0); 298244871b1bSChris Mason wret = insert_ptr(trans, root, path, 298344871b1bSChris Mason &disk_key, 298444871b1bSChris Mason right->start, 298544871b1bSChris Mason path->slots[1], 1); 298644871b1bSChris Mason if (wret) 298744871b1bSChris Mason ret = wret; 298844871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 298944871b1bSChris Mason free_extent_buffer(path->nodes[0]); 299044871b1bSChris Mason path->nodes[0] = right; 299144871b1bSChris Mason path->slots[0] = 0; 299244871b1bSChris Mason if (path->slots[1] == 0) { 299344871b1bSChris Mason wret = fixup_low_keys(trans, root, 299444871b1bSChris Mason path, &disk_key, 1); 299544871b1bSChris Mason if (wret) 299644871b1bSChris Mason ret = wret; 299744871b1bSChris Mason } 29985d4f98a2SYan Zheng } 299944871b1bSChris Mason btrfs_mark_buffer_dirty(right); 300044871b1bSChris Mason return ret; 300144871b1bSChris Mason } 300244871b1bSChris Mason 300344871b1bSChris Mason ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems); 300444871b1bSChris Mason BUG_ON(ret); 300544871b1bSChris Mason 30065d4f98a2SYan Zheng if (split == 2) { 3007cc0c5538SChris Mason BUG_ON(num_doubles != 0); 3008cc0c5538SChris Mason num_doubles++; 3009cc0c5538SChris Mason goto again; 30103326d1b0SChris Mason } 301144871b1bSChris Mason 3012be0e5c09SChris Mason return ret; 301399d8f83cSChris Mason 301499d8f83cSChris Mason push_for_double: 301599d8f83cSChris Mason push_for_double_split(trans, root, path, data_size); 301699d8f83cSChris Mason tried_avoid_double = 1; 301799d8f83cSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 301899d8f83cSChris Mason return 0; 301999d8f83cSChris Mason goto again; 3020be0e5c09SChris Mason } 3021be0e5c09SChris Mason 3022ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, 3023ad48fd75SYan, Zheng struct btrfs_root *root, 3024ad48fd75SYan, Zheng struct btrfs_path *path, int ins_len) 3025ad48fd75SYan, Zheng { 3026ad48fd75SYan, Zheng struct btrfs_key key; 3027ad48fd75SYan, Zheng struct extent_buffer *leaf; 3028ad48fd75SYan, Zheng struct btrfs_file_extent_item *fi; 3029ad48fd75SYan, Zheng u64 extent_len = 0; 3030ad48fd75SYan, Zheng u32 item_size; 3031ad48fd75SYan, Zheng int ret; 3032ad48fd75SYan, Zheng 3033ad48fd75SYan, Zheng leaf = path->nodes[0]; 3034ad48fd75SYan, Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3035ad48fd75SYan, Zheng 3036ad48fd75SYan, Zheng BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 3037ad48fd75SYan, Zheng key.type != BTRFS_EXTENT_CSUM_KEY); 3038ad48fd75SYan, Zheng 3039ad48fd75SYan, Zheng if (btrfs_leaf_free_space(root, leaf) >= ins_len) 3040ad48fd75SYan, Zheng return 0; 3041ad48fd75SYan, Zheng 3042ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 3043ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 3044ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 3045ad48fd75SYan, Zheng struct btrfs_file_extent_item); 3046ad48fd75SYan, Zheng extent_len = btrfs_file_extent_num_bytes(leaf, fi); 3047ad48fd75SYan, Zheng } 3048ad48fd75SYan, Zheng btrfs_release_path(root, path); 3049ad48fd75SYan, Zheng 3050ad48fd75SYan, Zheng path->keep_locks = 1; 3051ad48fd75SYan, Zheng path->search_for_split = 1; 3052ad48fd75SYan, Zheng ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 3053ad48fd75SYan, Zheng path->search_for_split = 0; 3054ad48fd75SYan, Zheng if (ret < 0) 3055ad48fd75SYan, Zheng goto err; 3056ad48fd75SYan, Zheng 3057ad48fd75SYan, Zheng ret = -EAGAIN; 3058ad48fd75SYan, Zheng leaf = path->nodes[0]; 3059ad48fd75SYan, Zheng /* if our item isn't there or got smaller, return now */ 3060ad48fd75SYan, Zheng if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) 3061ad48fd75SYan, Zheng goto err; 3062ad48fd75SYan, Zheng 3063109f6aefSChris Mason /* the leaf has changed, it now has room. return now */ 3064109f6aefSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) 3065109f6aefSChris Mason goto err; 3066109f6aefSChris Mason 3067ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 3068ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 3069ad48fd75SYan, Zheng struct btrfs_file_extent_item); 3070ad48fd75SYan, Zheng if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) 3071ad48fd75SYan, Zheng goto err; 3072ad48fd75SYan, Zheng } 3073ad48fd75SYan, Zheng 3074ad48fd75SYan, Zheng btrfs_set_path_blocking(path); 3075ad48fd75SYan, Zheng ret = split_leaf(trans, root, &key, path, ins_len, 1); 3076f0486c68SYan, Zheng if (ret) 3077f0486c68SYan, Zheng goto err; 3078ad48fd75SYan, Zheng 3079ad48fd75SYan, Zheng path->keep_locks = 0; 3080ad48fd75SYan, Zheng btrfs_unlock_up_safe(path, 1); 3081ad48fd75SYan, Zheng return 0; 3082ad48fd75SYan, Zheng err: 3083ad48fd75SYan, Zheng path->keep_locks = 0; 3084ad48fd75SYan, Zheng return ret; 3085ad48fd75SYan, Zheng } 3086ad48fd75SYan, Zheng 3087ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans, 3088459931ecSChris Mason struct btrfs_root *root, 3089459931ecSChris Mason struct btrfs_path *path, 3090459931ecSChris Mason struct btrfs_key *new_key, 3091459931ecSChris Mason unsigned long split_offset) 3092459931ecSChris Mason { 3093459931ecSChris Mason struct extent_buffer *leaf; 3094459931ecSChris Mason struct btrfs_item *item; 3095459931ecSChris Mason struct btrfs_item *new_item; 3096459931ecSChris Mason int slot; 3097ad48fd75SYan, Zheng char *buf; 3098459931ecSChris Mason u32 nritems; 3099ad48fd75SYan, Zheng u32 item_size; 3100459931ecSChris Mason u32 orig_offset; 3101459931ecSChris Mason struct btrfs_disk_key disk_key; 3102459931ecSChris Mason 3103459931ecSChris Mason leaf = path->nodes[0]; 3104b9473439SChris Mason BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); 3105b9473439SChris Mason 3106b4ce94deSChris Mason btrfs_set_path_blocking(path); 3107b4ce94deSChris Mason 3108459931ecSChris Mason item = btrfs_item_nr(leaf, path->slots[0]); 3109459931ecSChris Mason orig_offset = btrfs_item_offset(leaf, item); 3110459931ecSChris Mason item_size = btrfs_item_size(leaf, item); 3111459931ecSChris Mason 3112459931ecSChris Mason buf = kmalloc(item_size, GFP_NOFS); 3113ad48fd75SYan, Zheng if (!buf) 3114ad48fd75SYan, Zheng return -ENOMEM; 3115ad48fd75SYan, Zheng 3116459931ecSChris Mason read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, 3117459931ecSChris Mason path->slots[0]), item_size); 3118ad48fd75SYan, Zheng 3119459931ecSChris Mason slot = path->slots[0] + 1; 3120459931ecSChris Mason nritems = btrfs_header_nritems(leaf); 3121459931ecSChris Mason if (slot != nritems) { 3122459931ecSChris Mason /* shift the items */ 3123459931ecSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), 3124459931ecSChris Mason btrfs_item_nr_offset(slot), 3125459931ecSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 3126459931ecSChris Mason } 3127459931ecSChris Mason 3128459931ecSChris Mason btrfs_cpu_key_to_disk(&disk_key, new_key); 3129459931ecSChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 3130459931ecSChris Mason 3131459931ecSChris Mason new_item = btrfs_item_nr(leaf, slot); 3132459931ecSChris Mason 3133459931ecSChris Mason btrfs_set_item_offset(leaf, new_item, orig_offset); 3134459931ecSChris Mason btrfs_set_item_size(leaf, new_item, item_size - split_offset); 3135459931ecSChris Mason 3136459931ecSChris Mason btrfs_set_item_offset(leaf, item, 3137459931ecSChris Mason orig_offset + item_size - split_offset); 3138459931ecSChris Mason btrfs_set_item_size(leaf, item, split_offset); 3139459931ecSChris Mason 3140459931ecSChris Mason btrfs_set_header_nritems(leaf, nritems + 1); 3141459931ecSChris Mason 3142459931ecSChris Mason /* write the data for the start of the original item */ 3143459931ecSChris Mason write_extent_buffer(leaf, buf, 3144459931ecSChris Mason btrfs_item_ptr_offset(leaf, path->slots[0]), 3145459931ecSChris Mason split_offset); 3146459931ecSChris Mason 3147459931ecSChris Mason /* write the data for the new item */ 3148459931ecSChris Mason write_extent_buffer(leaf, buf + split_offset, 3149459931ecSChris Mason btrfs_item_ptr_offset(leaf, slot), 3150459931ecSChris Mason item_size - split_offset); 3151459931ecSChris Mason btrfs_mark_buffer_dirty(leaf); 3152459931ecSChris Mason 3153ad48fd75SYan, Zheng BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); 3154459931ecSChris Mason kfree(buf); 3155ad48fd75SYan, Zheng return 0; 3156ad48fd75SYan, Zheng } 3157ad48fd75SYan, Zheng 3158ad48fd75SYan, Zheng /* 3159ad48fd75SYan, Zheng * This function splits a single item into two items, 3160ad48fd75SYan, Zheng * giving 'new_key' to the new item and splitting the 3161ad48fd75SYan, Zheng * old one at split_offset (from the start of the item). 3162ad48fd75SYan, Zheng * 3163ad48fd75SYan, Zheng * The path may be released by this operation. After 3164ad48fd75SYan, Zheng * the split, the path is pointing to the old item. The 3165ad48fd75SYan, Zheng * new item is going to be in the same node as the old one. 3166ad48fd75SYan, Zheng * 3167ad48fd75SYan, Zheng * Note, the item being split must be smaller enough to live alone on 3168ad48fd75SYan, Zheng * a tree block with room for one extra struct btrfs_item 3169ad48fd75SYan, Zheng * 3170ad48fd75SYan, Zheng * This allows us to split the item in place, keeping a lock on the 3171ad48fd75SYan, Zheng * leaf the entire time. 3172ad48fd75SYan, Zheng */ 3173ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans, 3174ad48fd75SYan, Zheng struct btrfs_root *root, 3175ad48fd75SYan, Zheng struct btrfs_path *path, 3176ad48fd75SYan, Zheng struct btrfs_key *new_key, 3177ad48fd75SYan, Zheng unsigned long split_offset) 3178ad48fd75SYan, Zheng { 3179ad48fd75SYan, Zheng int ret; 3180ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 3181ad48fd75SYan, Zheng sizeof(struct btrfs_item)); 3182ad48fd75SYan, Zheng if (ret) 3183459931ecSChris Mason return ret; 3184ad48fd75SYan, Zheng 3185ad48fd75SYan, Zheng ret = split_item(trans, root, path, new_key, split_offset); 3186ad48fd75SYan, Zheng return ret; 3187ad48fd75SYan, Zheng } 3188ad48fd75SYan, Zheng 3189ad48fd75SYan, Zheng /* 3190ad48fd75SYan, Zheng * This function duplicate a item, giving 'new_key' to the new item. 3191ad48fd75SYan, Zheng * It guarantees both items live in the same tree leaf and the new item 3192ad48fd75SYan, Zheng * is contiguous with the original item. 3193ad48fd75SYan, Zheng * 3194ad48fd75SYan, Zheng * This allows us to split file extent in place, keeping a lock on the 3195ad48fd75SYan, Zheng * leaf the entire time. 3196ad48fd75SYan, Zheng */ 3197ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans, 3198ad48fd75SYan, Zheng struct btrfs_root *root, 3199ad48fd75SYan, Zheng struct btrfs_path *path, 3200ad48fd75SYan, Zheng struct btrfs_key *new_key) 3201ad48fd75SYan, Zheng { 3202ad48fd75SYan, Zheng struct extent_buffer *leaf; 3203ad48fd75SYan, Zheng int ret; 3204ad48fd75SYan, Zheng u32 item_size; 3205ad48fd75SYan, Zheng 3206ad48fd75SYan, Zheng leaf = path->nodes[0]; 3207ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 3208ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 3209ad48fd75SYan, Zheng item_size + sizeof(struct btrfs_item)); 3210ad48fd75SYan, Zheng if (ret) 3211ad48fd75SYan, Zheng return ret; 3212ad48fd75SYan, Zheng 3213ad48fd75SYan, Zheng path->slots[0]++; 3214ad48fd75SYan, Zheng ret = setup_items_for_insert(trans, root, path, new_key, &item_size, 3215ad48fd75SYan, Zheng item_size, item_size + 3216ad48fd75SYan, Zheng sizeof(struct btrfs_item), 1); 3217ad48fd75SYan, Zheng BUG_ON(ret); 3218ad48fd75SYan, Zheng 3219ad48fd75SYan, Zheng leaf = path->nodes[0]; 3220ad48fd75SYan, Zheng memcpy_extent_buffer(leaf, 3221ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0]), 3222ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0] - 1), 3223ad48fd75SYan, Zheng item_size); 3224ad48fd75SYan, Zheng return 0; 3225459931ecSChris Mason } 3226459931ecSChris Mason 3227459931ecSChris Mason /* 3228d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 3229d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 3230d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 3231d352ac68SChris Mason * the front. 3232d352ac68SChris Mason */ 3233b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans, 3234b18c6685SChris Mason struct btrfs_root *root, 3235b18c6685SChris Mason struct btrfs_path *path, 3236179e29e4SChris Mason u32 new_size, int from_end) 3237b18c6685SChris Mason { 3238b18c6685SChris Mason int ret = 0; 3239b18c6685SChris Mason int slot; 32405f39d397SChris Mason struct extent_buffer *leaf; 32415f39d397SChris Mason struct btrfs_item *item; 3242b18c6685SChris Mason u32 nritems; 3243b18c6685SChris Mason unsigned int data_end; 3244b18c6685SChris Mason unsigned int old_data_start; 3245b18c6685SChris Mason unsigned int old_size; 3246b18c6685SChris Mason unsigned int size_diff; 3247b18c6685SChris Mason int i; 3248b18c6685SChris Mason 32495f39d397SChris Mason leaf = path->nodes[0]; 3250179e29e4SChris Mason slot = path->slots[0]; 3251179e29e4SChris Mason 3252179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 3253179e29e4SChris Mason if (old_size == new_size) 3254179e29e4SChris Mason return 0; 3255b18c6685SChris Mason 32565f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3257b18c6685SChris Mason data_end = leaf_data_end(root, leaf); 3258b18c6685SChris Mason 32595f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 3260179e29e4SChris Mason 3261b18c6685SChris Mason size_diff = old_size - new_size; 3262b18c6685SChris Mason 3263b18c6685SChris Mason BUG_ON(slot < 0); 3264b18c6685SChris Mason BUG_ON(slot >= nritems); 3265b18c6685SChris Mason 3266b18c6685SChris Mason /* 3267b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 3268b18c6685SChris Mason */ 3269b18c6685SChris Mason /* first correct the data pointers */ 3270b18c6685SChris Mason for (i = slot; i < nritems; i++) { 32715f39d397SChris Mason u32 ioff; 32725f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3273db94535dSChris Mason 3274db94535dSChris Mason if (!leaf->map_token) { 3275db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3276db94535dSChris Mason sizeof(struct btrfs_item), 3277db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3278db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3279db94535dSChris Mason KM_USER1); 3280db94535dSChris Mason } 3281db94535dSChris Mason 32825f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 32835f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + size_diff); 3284b18c6685SChris Mason } 3285db94535dSChris Mason 3286db94535dSChris Mason if (leaf->map_token) { 3287db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3288db94535dSChris Mason leaf->map_token = NULL; 3289db94535dSChris Mason } 3290db94535dSChris Mason 3291b18c6685SChris Mason /* shift the data */ 3292179e29e4SChris Mason if (from_end) { 32935f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3294b18c6685SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 3295b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 3296179e29e4SChris Mason } else { 3297179e29e4SChris Mason struct btrfs_disk_key disk_key; 3298179e29e4SChris Mason u64 offset; 3299179e29e4SChris Mason 3300179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 3301179e29e4SChris Mason 3302179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 3303179e29e4SChris Mason unsigned long ptr; 3304179e29e4SChris Mason struct btrfs_file_extent_item *fi; 3305179e29e4SChris Mason 3306179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 3307179e29e4SChris Mason struct btrfs_file_extent_item); 3308179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 3309179e29e4SChris Mason (unsigned long)fi - size_diff); 3310179e29e4SChris Mason 3311179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 3312179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 3313179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 3314179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 3315179e29e4SChris Mason (unsigned long)fi, 3316179e29e4SChris Mason offsetof(struct btrfs_file_extent_item, 3317179e29e4SChris Mason disk_bytenr)); 3318179e29e4SChris Mason } 3319179e29e4SChris Mason } 3320179e29e4SChris Mason 3321179e29e4SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3322179e29e4SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 3323179e29e4SChris Mason data_end, old_data_start - data_end); 3324179e29e4SChris Mason 3325179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 3326179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 3327179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 3328179e29e4SChris Mason if (slot == 0) 3329179e29e4SChris Mason fixup_low_keys(trans, root, path, &disk_key, 1); 3330179e29e4SChris Mason } 33315f39d397SChris Mason 33325f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 33335f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 33345f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3335b18c6685SChris Mason 3336b18c6685SChris Mason ret = 0; 33375f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 33385f39d397SChris Mason btrfs_print_leaf(root, leaf); 3339b18c6685SChris Mason BUG(); 33405f39d397SChris Mason } 3341b18c6685SChris Mason return ret; 3342b18c6685SChris Mason } 3343b18c6685SChris Mason 3344d352ac68SChris Mason /* 3345d352ac68SChris Mason * make the item pointed to by the path bigger, data_size is the new size. 3346d352ac68SChris Mason */ 33475f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans, 33485f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 33495f39d397SChris Mason u32 data_size) 33506567e837SChris Mason { 33516567e837SChris Mason int ret = 0; 33526567e837SChris Mason int slot; 33535f39d397SChris Mason struct extent_buffer *leaf; 33545f39d397SChris Mason struct btrfs_item *item; 33556567e837SChris Mason u32 nritems; 33566567e837SChris Mason unsigned int data_end; 33576567e837SChris Mason unsigned int old_data; 33586567e837SChris Mason unsigned int old_size; 33596567e837SChris Mason int i; 33606567e837SChris Mason 33615f39d397SChris Mason leaf = path->nodes[0]; 33626567e837SChris Mason 33635f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 33646567e837SChris Mason data_end = leaf_data_end(root, leaf); 33656567e837SChris Mason 33665f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < data_size) { 33675f39d397SChris Mason btrfs_print_leaf(root, leaf); 33686567e837SChris Mason BUG(); 33695f39d397SChris Mason } 33706567e837SChris Mason slot = path->slots[0]; 33715f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 33726567e837SChris Mason 33736567e837SChris Mason BUG_ON(slot < 0); 33743326d1b0SChris Mason if (slot >= nritems) { 33753326d1b0SChris Mason btrfs_print_leaf(root, leaf); 3376d397712bSChris Mason printk(KERN_CRIT "slot %d too large, nritems %d\n", 3377d397712bSChris Mason slot, nritems); 33783326d1b0SChris Mason BUG_ON(1); 33793326d1b0SChris Mason } 33806567e837SChris Mason 33816567e837SChris Mason /* 33826567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 33836567e837SChris Mason */ 33846567e837SChris Mason /* first correct the data pointers */ 33856567e837SChris Mason for (i = slot; i < nritems; i++) { 33865f39d397SChris Mason u32 ioff; 33875f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3388db94535dSChris Mason 3389db94535dSChris Mason if (!leaf->map_token) { 3390db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3391db94535dSChris Mason sizeof(struct btrfs_item), 3392db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3393db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3394db94535dSChris Mason KM_USER1); 3395db94535dSChris Mason } 33965f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 33975f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff - data_size); 33986567e837SChris Mason } 33995f39d397SChris Mason 3400db94535dSChris Mason if (leaf->map_token) { 3401db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3402db94535dSChris Mason leaf->map_token = NULL; 3403db94535dSChris Mason } 3404db94535dSChris Mason 34056567e837SChris Mason /* shift the data */ 34065f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 34076567e837SChris Mason data_end - data_size, btrfs_leaf_data(leaf) + 34086567e837SChris Mason data_end, old_data - data_end); 34095f39d397SChris Mason 34106567e837SChris Mason data_end = old_data; 34115f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 34125f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 34135f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 34145f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 34156567e837SChris Mason 34166567e837SChris Mason ret = 0; 34175f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 34185f39d397SChris Mason btrfs_print_leaf(root, leaf); 34196567e837SChris Mason BUG(); 34205f39d397SChris Mason } 34216567e837SChris Mason return ret; 34226567e837SChris Mason } 34236567e837SChris Mason 342474123bd7SChris Mason /* 3425d352ac68SChris Mason * Given a key and some data, insert items into the tree. 342674123bd7SChris Mason * This does all the path init required, making room in the tree if needed. 3427f3465ca4SJosef Bacik * Returns the number of keys that were inserted. 3428f3465ca4SJosef Bacik */ 3429f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans, 3430f3465ca4SJosef Bacik struct btrfs_root *root, 3431f3465ca4SJosef Bacik struct btrfs_path *path, 3432f3465ca4SJosef Bacik struct btrfs_key *cpu_key, u32 *data_size, 3433f3465ca4SJosef Bacik int nr) 3434f3465ca4SJosef Bacik { 3435f3465ca4SJosef Bacik struct extent_buffer *leaf; 3436f3465ca4SJosef Bacik struct btrfs_item *item; 3437f3465ca4SJosef Bacik int ret = 0; 3438f3465ca4SJosef Bacik int slot; 3439f3465ca4SJosef Bacik int i; 3440f3465ca4SJosef Bacik u32 nritems; 3441f3465ca4SJosef Bacik u32 total_data = 0; 3442f3465ca4SJosef Bacik u32 total_size = 0; 3443f3465ca4SJosef Bacik unsigned int data_end; 3444f3465ca4SJosef Bacik struct btrfs_disk_key disk_key; 3445f3465ca4SJosef Bacik struct btrfs_key found_key; 3446f3465ca4SJosef Bacik 344787b29b20SYan Zheng for (i = 0; i < nr; i++) { 344887b29b20SYan Zheng if (total_size + data_size[i] + sizeof(struct btrfs_item) > 344987b29b20SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 345087b29b20SYan Zheng break; 345187b29b20SYan Zheng nr = i; 345287b29b20SYan Zheng } 3453f3465ca4SJosef Bacik total_data += data_size[i]; 345487b29b20SYan Zheng total_size += data_size[i] + sizeof(struct btrfs_item); 345587b29b20SYan Zheng } 345687b29b20SYan Zheng BUG_ON(nr == 0); 3457f3465ca4SJosef Bacik 3458f3465ca4SJosef Bacik ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 3459f3465ca4SJosef Bacik if (ret == 0) 3460f3465ca4SJosef Bacik return -EEXIST; 3461f3465ca4SJosef Bacik if (ret < 0) 3462f3465ca4SJosef Bacik goto out; 3463f3465ca4SJosef Bacik 3464f3465ca4SJosef Bacik leaf = path->nodes[0]; 3465f3465ca4SJosef Bacik 3466f3465ca4SJosef Bacik nritems = btrfs_header_nritems(leaf); 3467f3465ca4SJosef Bacik data_end = leaf_data_end(root, leaf); 3468f3465ca4SJosef Bacik 3469f3465ca4SJosef Bacik if (btrfs_leaf_free_space(root, leaf) < total_size) { 3470f3465ca4SJosef Bacik for (i = nr; i >= 0; i--) { 3471f3465ca4SJosef Bacik total_data -= data_size[i]; 3472f3465ca4SJosef Bacik total_size -= data_size[i] + sizeof(struct btrfs_item); 3473f3465ca4SJosef Bacik if (total_size < btrfs_leaf_free_space(root, leaf)) 3474f3465ca4SJosef Bacik break; 3475f3465ca4SJosef Bacik } 3476f3465ca4SJosef Bacik nr = i; 3477f3465ca4SJosef Bacik } 3478f3465ca4SJosef Bacik 3479f3465ca4SJosef Bacik slot = path->slots[0]; 3480f3465ca4SJosef Bacik BUG_ON(slot < 0); 3481f3465ca4SJosef Bacik 3482f3465ca4SJosef Bacik if (slot != nritems) { 3483f3465ca4SJosef Bacik unsigned int old_data = btrfs_item_end_nr(leaf, slot); 3484f3465ca4SJosef Bacik 3485f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, slot); 3486f3465ca4SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 3487f3465ca4SJosef Bacik 3488f3465ca4SJosef Bacik /* figure out how many keys we can insert in here */ 3489f3465ca4SJosef Bacik total_data = data_size[0]; 3490f3465ca4SJosef Bacik for (i = 1; i < nr; i++) { 34915d4f98a2SYan Zheng if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0) 3492f3465ca4SJosef Bacik break; 3493f3465ca4SJosef Bacik total_data += data_size[i]; 3494f3465ca4SJosef Bacik } 3495f3465ca4SJosef Bacik nr = i; 3496f3465ca4SJosef Bacik 3497f3465ca4SJosef Bacik if (old_data < data_end) { 3498f3465ca4SJosef Bacik btrfs_print_leaf(root, leaf); 3499d397712bSChris Mason printk(KERN_CRIT "slot %d old_data %d data_end %d\n", 3500f3465ca4SJosef Bacik slot, old_data, data_end); 3501f3465ca4SJosef Bacik BUG_ON(1); 3502f3465ca4SJosef Bacik } 3503f3465ca4SJosef Bacik /* 3504f3465ca4SJosef Bacik * item0..itemN ... dataN.offset..dataN.size .. data0.size 3505f3465ca4SJosef Bacik */ 3506f3465ca4SJosef Bacik /* first correct the data pointers */ 3507f3465ca4SJosef Bacik WARN_ON(leaf->map_token); 3508f3465ca4SJosef Bacik for (i = slot; i < nritems; i++) { 3509f3465ca4SJosef Bacik u32 ioff; 3510f3465ca4SJosef Bacik 3511f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, i); 3512f3465ca4SJosef Bacik if (!leaf->map_token) { 3513f3465ca4SJosef Bacik map_extent_buffer(leaf, (unsigned long)item, 3514f3465ca4SJosef Bacik sizeof(struct btrfs_item), 3515f3465ca4SJosef Bacik &leaf->map_token, &leaf->kaddr, 3516f3465ca4SJosef Bacik &leaf->map_start, &leaf->map_len, 3517f3465ca4SJosef Bacik KM_USER1); 3518f3465ca4SJosef Bacik } 3519f3465ca4SJosef Bacik 3520f3465ca4SJosef Bacik ioff = btrfs_item_offset(leaf, item); 3521f3465ca4SJosef Bacik btrfs_set_item_offset(leaf, item, ioff - total_data); 3522f3465ca4SJosef Bacik } 3523f3465ca4SJosef Bacik if (leaf->map_token) { 3524f3465ca4SJosef Bacik unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3525f3465ca4SJosef Bacik leaf->map_token = NULL; 3526f3465ca4SJosef Bacik } 3527f3465ca4SJosef Bacik 3528f3465ca4SJosef Bacik /* shift the items */ 3529f3465ca4SJosef Bacik memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 3530f3465ca4SJosef Bacik btrfs_item_nr_offset(slot), 3531f3465ca4SJosef Bacik (nritems - slot) * sizeof(struct btrfs_item)); 3532f3465ca4SJosef Bacik 3533f3465ca4SJosef Bacik /* shift the data */ 3534f3465ca4SJosef Bacik memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3535f3465ca4SJosef Bacik data_end - total_data, btrfs_leaf_data(leaf) + 3536f3465ca4SJosef Bacik data_end, old_data - data_end); 3537f3465ca4SJosef Bacik data_end = old_data; 3538f3465ca4SJosef Bacik } else { 3539f3465ca4SJosef Bacik /* 3540f3465ca4SJosef Bacik * this sucks but it has to be done, if we are inserting at 3541f3465ca4SJosef Bacik * the end of the leaf only insert 1 of the items, since we 3542f3465ca4SJosef Bacik * have no way of knowing whats on the next leaf and we'd have 3543f3465ca4SJosef Bacik * to drop our current locks to figure it out 3544f3465ca4SJosef Bacik */ 3545f3465ca4SJosef Bacik nr = 1; 3546f3465ca4SJosef Bacik } 3547f3465ca4SJosef Bacik 3548f3465ca4SJosef Bacik /* setup the item for the new data */ 3549f3465ca4SJosef Bacik for (i = 0; i < nr; i++) { 3550f3465ca4SJosef Bacik btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 3551f3465ca4SJosef Bacik btrfs_set_item_key(leaf, &disk_key, slot + i); 3552f3465ca4SJosef Bacik item = btrfs_item_nr(leaf, slot + i); 3553f3465ca4SJosef Bacik btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 3554f3465ca4SJosef Bacik data_end -= data_size[i]; 3555f3465ca4SJosef Bacik btrfs_set_item_size(leaf, item, data_size[i]); 3556f3465ca4SJosef Bacik } 3557f3465ca4SJosef Bacik btrfs_set_header_nritems(leaf, nritems + nr); 3558f3465ca4SJosef Bacik btrfs_mark_buffer_dirty(leaf); 3559f3465ca4SJosef Bacik 3560f3465ca4SJosef Bacik ret = 0; 3561f3465ca4SJosef Bacik if (slot == 0) { 3562f3465ca4SJosef Bacik btrfs_cpu_key_to_disk(&disk_key, cpu_key); 3563f3465ca4SJosef Bacik ret = fixup_low_keys(trans, root, path, &disk_key, 1); 3564f3465ca4SJosef Bacik } 3565f3465ca4SJosef Bacik 3566f3465ca4SJosef Bacik if (btrfs_leaf_free_space(root, leaf) < 0) { 3567f3465ca4SJosef Bacik btrfs_print_leaf(root, leaf); 3568f3465ca4SJosef Bacik BUG(); 3569f3465ca4SJosef Bacik } 3570f3465ca4SJosef Bacik out: 3571f3465ca4SJosef Bacik if (!ret) 3572f3465ca4SJosef Bacik ret = nr; 3573f3465ca4SJosef Bacik return ret; 3574f3465ca4SJosef Bacik } 3575f3465ca4SJosef Bacik 3576f3465ca4SJosef Bacik /* 357744871b1bSChris Mason * this is a helper for btrfs_insert_empty_items, the main goal here is 357844871b1bSChris Mason * to save stack depth by doing the bulk of the work in a function 357944871b1bSChris Mason * that doesn't call btrfs_search_slot 358074123bd7SChris Mason */ 358144871b1bSChris Mason static noinline_for_stack int 358244871b1bSChris Mason setup_items_for_insert(struct btrfs_trans_handle *trans, 358344871b1bSChris Mason struct btrfs_root *root, struct btrfs_path *path, 35849c58309dSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 358544871b1bSChris Mason u32 total_data, u32 total_size, int nr) 3586be0e5c09SChris Mason { 35875f39d397SChris Mason struct btrfs_item *item; 35889c58309dSChris Mason int i; 35897518a238SChris Mason u32 nritems; 3590be0e5c09SChris Mason unsigned int data_end; 3591e2fa7227SChris Mason struct btrfs_disk_key disk_key; 359244871b1bSChris Mason int ret; 359344871b1bSChris Mason struct extent_buffer *leaf; 359444871b1bSChris Mason int slot; 3595e2fa7227SChris Mason 35965f39d397SChris Mason leaf = path->nodes[0]; 359744871b1bSChris Mason slot = path->slots[0]; 359874123bd7SChris Mason 35995f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3600123abc88SChris Mason data_end = leaf_data_end(root, leaf); 3601eb60ceacSChris Mason 3602f25956ccSChris Mason if (btrfs_leaf_free_space(root, leaf) < total_size) { 36033326d1b0SChris Mason btrfs_print_leaf(root, leaf); 3604d397712bSChris Mason printk(KERN_CRIT "not enough freespace need %u have %d\n", 36059c58309dSChris Mason total_size, btrfs_leaf_free_space(root, leaf)); 3606be0e5c09SChris Mason BUG(); 3607d4dbff95SChris Mason } 36085f39d397SChris Mason 3609be0e5c09SChris Mason if (slot != nritems) { 36105f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 3611be0e5c09SChris Mason 36125f39d397SChris Mason if (old_data < data_end) { 36135f39d397SChris Mason btrfs_print_leaf(root, leaf); 3614d397712bSChris Mason printk(KERN_CRIT "slot %d old_data %d data_end %d\n", 36155f39d397SChris Mason slot, old_data, data_end); 36165f39d397SChris Mason BUG_ON(1); 36175f39d397SChris Mason } 3618be0e5c09SChris Mason /* 3619be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 3620be0e5c09SChris Mason */ 3621be0e5c09SChris Mason /* first correct the data pointers */ 3622db94535dSChris Mason WARN_ON(leaf->map_token); 36230783fcfcSChris Mason for (i = slot; i < nritems; i++) { 36245f39d397SChris Mason u32 ioff; 3625db94535dSChris Mason 36265f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3627db94535dSChris Mason if (!leaf->map_token) { 3628db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3629db94535dSChris Mason sizeof(struct btrfs_item), 3630db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3631db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3632db94535dSChris Mason KM_USER1); 3633db94535dSChris Mason } 3634db94535dSChris Mason 36355f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 36369c58309dSChris Mason btrfs_set_item_offset(leaf, item, ioff - total_data); 36370783fcfcSChris Mason } 3638db94535dSChris Mason if (leaf->map_token) { 3639db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3640db94535dSChris Mason leaf->map_token = NULL; 3641db94535dSChris Mason } 3642be0e5c09SChris Mason 3643be0e5c09SChris Mason /* shift the items */ 36449c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 36455f39d397SChris Mason btrfs_item_nr_offset(slot), 36460783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 3647be0e5c09SChris Mason 3648be0e5c09SChris Mason /* shift the data */ 36495f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 36509c58309dSChris Mason data_end - total_data, btrfs_leaf_data(leaf) + 3651be0e5c09SChris Mason data_end, old_data - data_end); 3652be0e5c09SChris Mason data_end = old_data; 3653be0e5c09SChris Mason } 36545f39d397SChris Mason 365562e2749eSChris Mason /* setup the item for the new data */ 36569c58309dSChris Mason for (i = 0; i < nr; i++) { 36579c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 36589c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 36599c58309dSChris Mason item = btrfs_item_nr(leaf, slot + i); 36609c58309dSChris Mason btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 36619c58309dSChris Mason data_end -= data_size[i]; 36629c58309dSChris Mason btrfs_set_item_size(leaf, item, data_size[i]); 36639c58309dSChris Mason } 366444871b1bSChris Mason 36659c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 3666aa5d6bedSChris Mason 3667aa5d6bedSChris Mason ret = 0; 36685a01a2e3SChris Mason if (slot == 0) { 366944871b1bSChris Mason struct btrfs_disk_key disk_key; 36705a01a2e3SChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key); 3671e089f05cSChris Mason ret = fixup_low_keys(trans, root, path, &disk_key, 1); 36725a01a2e3SChris Mason } 3673b9473439SChris Mason btrfs_unlock_up_safe(path, 1); 3674b9473439SChris Mason btrfs_mark_buffer_dirty(leaf); 3675aa5d6bedSChris Mason 36765f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 36775f39d397SChris Mason btrfs_print_leaf(root, leaf); 3678be0e5c09SChris Mason BUG(); 36795f39d397SChris Mason } 368044871b1bSChris Mason return ret; 368144871b1bSChris Mason } 368244871b1bSChris Mason 368344871b1bSChris Mason /* 368444871b1bSChris Mason * Given a key and some data, insert items into the tree. 368544871b1bSChris Mason * This does all the path init required, making room in the tree if needed. 368644871b1bSChris Mason */ 368744871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 368844871b1bSChris Mason struct btrfs_root *root, 368944871b1bSChris Mason struct btrfs_path *path, 369044871b1bSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 369144871b1bSChris Mason int nr) 369244871b1bSChris Mason { 369344871b1bSChris Mason int ret = 0; 369444871b1bSChris Mason int slot; 369544871b1bSChris Mason int i; 369644871b1bSChris Mason u32 total_size = 0; 369744871b1bSChris Mason u32 total_data = 0; 369844871b1bSChris Mason 369944871b1bSChris Mason for (i = 0; i < nr; i++) 370044871b1bSChris Mason total_data += data_size[i]; 370144871b1bSChris Mason 370244871b1bSChris Mason total_size = total_data + (nr * sizeof(struct btrfs_item)); 370344871b1bSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 370444871b1bSChris Mason if (ret == 0) 370544871b1bSChris Mason return -EEXIST; 370644871b1bSChris Mason if (ret < 0) 370744871b1bSChris Mason goto out; 370844871b1bSChris Mason 370944871b1bSChris Mason slot = path->slots[0]; 371044871b1bSChris Mason BUG_ON(slot < 0); 371144871b1bSChris Mason 371244871b1bSChris Mason ret = setup_items_for_insert(trans, root, path, cpu_key, data_size, 371344871b1bSChris Mason total_data, total_size, nr); 371444871b1bSChris Mason 3715ed2ff2cbSChris Mason out: 371662e2749eSChris Mason return ret; 371762e2749eSChris Mason } 371862e2749eSChris Mason 371962e2749eSChris Mason /* 372062e2749eSChris Mason * Given a key and some data, insert an item into the tree. 372162e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 372262e2749eSChris Mason */ 3723e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root 3724e089f05cSChris Mason *root, struct btrfs_key *cpu_key, void *data, u32 3725e089f05cSChris Mason data_size) 372662e2749eSChris Mason { 372762e2749eSChris Mason int ret = 0; 37282c90e5d6SChris Mason struct btrfs_path *path; 37295f39d397SChris Mason struct extent_buffer *leaf; 37305f39d397SChris Mason unsigned long ptr; 373162e2749eSChris Mason 37322c90e5d6SChris Mason path = btrfs_alloc_path(); 3733db5b493aSTsutomu Itoh if (!path) 3734db5b493aSTsutomu Itoh return -ENOMEM; 37352c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 373662e2749eSChris Mason if (!ret) { 37375f39d397SChris Mason leaf = path->nodes[0]; 37385f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 37395f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 37405f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 374162e2749eSChris Mason } 37422c90e5d6SChris Mason btrfs_free_path(path); 3743aa5d6bedSChris Mason return ret; 3744be0e5c09SChris Mason } 3745be0e5c09SChris Mason 374674123bd7SChris Mason /* 37475de08d7dSChris Mason * delete the pointer from a given node. 374874123bd7SChris Mason * 3749d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 3750d352ac68SChris Mason * empty a node. 375174123bd7SChris Mason */ 3752e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3753e089f05cSChris Mason struct btrfs_path *path, int level, int slot) 3754be0e5c09SChris Mason { 37555f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 37567518a238SChris Mason u32 nritems; 3757aa5d6bedSChris Mason int ret = 0; 3758bb803951SChris Mason int wret; 3759be0e5c09SChris Mason 37605f39d397SChris Mason nritems = btrfs_header_nritems(parent); 3761be0e5c09SChris Mason if (slot != nritems - 1) { 37625f39d397SChris Mason memmove_extent_buffer(parent, 37635f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 37645f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 3765d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 3766d6025579SChris Mason (nritems - slot - 1)); 3767be0e5c09SChris Mason } 37687518a238SChris Mason nritems--; 37695f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 37707518a238SChris Mason if (nritems == 0 && parent == root->node) { 37715f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 3772eb60ceacSChris Mason /* just turn the root into a leaf and break */ 37735f39d397SChris Mason btrfs_set_header_level(root->node, 0); 3774bb803951SChris Mason } else if (slot == 0) { 37755f39d397SChris Mason struct btrfs_disk_key disk_key; 37765f39d397SChris Mason 37775f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 37785f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); 37790f70abe2SChris Mason if (wret) 37800f70abe2SChris Mason ret = wret; 3781be0e5c09SChris Mason } 3782d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 3783aa5d6bedSChris Mason return ret; 3784be0e5c09SChris Mason } 3785be0e5c09SChris Mason 378674123bd7SChris Mason /* 3787323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 37885d4f98a2SYan Zheng * path->nodes[1]. 3789323ac95bSChris Mason * 3790323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 3791323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 3792323ac95bSChris Mason * 3793323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 3794323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 3795323ac95bSChris Mason */ 37965d4f98a2SYan Zheng static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans, 3797323ac95bSChris Mason struct btrfs_root *root, 37985d4f98a2SYan Zheng struct btrfs_path *path, 37995d4f98a2SYan Zheng struct extent_buffer *leaf) 3800323ac95bSChris Mason { 3801323ac95bSChris Mason int ret; 3802323ac95bSChris Mason 38035d4f98a2SYan Zheng WARN_ON(btrfs_header_generation(leaf) != trans->transid); 3804323ac95bSChris Mason ret = del_ptr(trans, root, path, 1, path->slots[1]); 3805323ac95bSChris Mason if (ret) 3806323ac95bSChris Mason return ret; 3807323ac95bSChris Mason 38084d081c41SChris Mason /* 38094d081c41SChris Mason * btrfs_free_extent is expensive, we want to make sure we 38104d081c41SChris Mason * aren't holding any locks when we call it 38114d081c41SChris Mason */ 38124d081c41SChris Mason btrfs_unlock_up_safe(path, 0); 38134d081c41SChris Mason 3814f0486c68SYan, Zheng root_sub_used(root, leaf->len); 3815f0486c68SYan, Zheng 3816f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, leaf, 0, 1); 3817f0486c68SYan, Zheng return 0; 3818323ac95bSChris Mason } 3819323ac95bSChris Mason /* 382074123bd7SChris Mason * delete the item at the leaf level in path. If that empties 382174123bd7SChris Mason * the leaf, remove it from the tree 382274123bd7SChris Mason */ 382385e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 382485e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 3825be0e5c09SChris Mason { 38265f39d397SChris Mason struct extent_buffer *leaf; 38275f39d397SChris Mason struct btrfs_item *item; 382885e21bacSChris Mason int last_off; 382985e21bacSChris Mason int dsize = 0; 3830aa5d6bedSChris Mason int ret = 0; 3831aa5d6bedSChris Mason int wret; 383285e21bacSChris Mason int i; 38337518a238SChris Mason u32 nritems; 3834be0e5c09SChris Mason 38355f39d397SChris Mason leaf = path->nodes[0]; 383685e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 383785e21bacSChris Mason 383885e21bacSChris Mason for (i = 0; i < nr; i++) 383985e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 384085e21bacSChris Mason 38415f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 3842be0e5c09SChris Mason 384385e21bacSChris Mason if (slot + nr != nritems) { 3844123abc88SChris Mason int data_end = leaf_data_end(root, leaf); 38455f39d397SChris Mason 38465f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 3847d6025579SChris Mason data_end + dsize, 3848123abc88SChris Mason btrfs_leaf_data(leaf) + data_end, 384985e21bacSChris Mason last_off - data_end); 38505f39d397SChris Mason 385185e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 38525f39d397SChris Mason u32 ioff; 3853db94535dSChris Mason 38545f39d397SChris Mason item = btrfs_item_nr(leaf, i); 3855db94535dSChris Mason if (!leaf->map_token) { 3856db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 3857db94535dSChris Mason sizeof(struct btrfs_item), 3858db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 3859db94535dSChris Mason &leaf->map_start, &leaf->map_len, 3860db94535dSChris Mason KM_USER1); 3861db94535dSChris Mason } 38625f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 38635f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + dsize); 38640783fcfcSChris Mason } 3865db94535dSChris Mason 3866db94535dSChris Mason if (leaf->map_token) { 3867db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 3868db94535dSChris Mason leaf->map_token = NULL; 3869db94535dSChris Mason } 3870db94535dSChris Mason 38715f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 387285e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 38730783fcfcSChris Mason sizeof(struct btrfs_item) * 387485e21bacSChris Mason (nritems - slot - nr)); 3875be0e5c09SChris Mason } 387685e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 387785e21bacSChris Mason nritems -= nr; 38785f39d397SChris Mason 387974123bd7SChris Mason /* delete the leaf if we've emptied it */ 38807518a238SChris Mason if (nritems == 0) { 38815f39d397SChris Mason if (leaf == root->node) { 38825f39d397SChris Mason btrfs_set_header_level(leaf, 0); 38839a8dd150SChris Mason } else { 3884f0486c68SYan, Zheng btrfs_set_path_blocking(path); 3885f0486c68SYan, Zheng clean_tree_block(trans, root, leaf); 38865d4f98a2SYan Zheng ret = btrfs_del_leaf(trans, root, path, leaf); 3887323ac95bSChris Mason BUG_ON(ret); 38889a8dd150SChris Mason } 3889be0e5c09SChris Mason } else { 38907518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 3891aa5d6bedSChris Mason if (slot == 0) { 38925f39d397SChris Mason struct btrfs_disk_key disk_key; 38935f39d397SChris Mason 38945f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 3895e089f05cSChris Mason wret = fixup_low_keys(trans, root, path, 38965f39d397SChris Mason &disk_key, 1); 3897aa5d6bedSChris Mason if (wret) 3898aa5d6bedSChris Mason ret = wret; 3899aa5d6bedSChris Mason } 3900aa5d6bedSChris Mason 390174123bd7SChris Mason /* delete the leaf if it is mostly empty */ 3902d717aa1dSYan Zheng if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { 3903be0e5c09SChris Mason /* push_leaf_left fixes the path. 3904be0e5c09SChris Mason * make sure the path still points to our leaf 3905be0e5c09SChris Mason * for possible call to del_ptr below 3906be0e5c09SChris Mason */ 39074920c9acSChris Mason slot = path->slots[1]; 39085f39d397SChris Mason extent_buffer_get(leaf); 39095f39d397SChris Mason 3910b9473439SChris Mason btrfs_set_path_blocking(path); 391199d8f83cSChris Mason wret = push_leaf_left(trans, root, path, 1, 1, 391299d8f83cSChris Mason 1, (u32)-1); 391354aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3914aa5d6bedSChris Mason ret = wret; 39155f39d397SChris Mason 39165f39d397SChris Mason if (path->nodes[0] == leaf && 39175f39d397SChris Mason btrfs_header_nritems(leaf)) { 391899d8f83cSChris Mason wret = push_leaf_right(trans, root, path, 1, 391999d8f83cSChris Mason 1, 1, 0); 392054aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 3921aa5d6bedSChris Mason ret = wret; 3922aa5d6bedSChris Mason } 39235f39d397SChris Mason 39245f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 3925323ac95bSChris Mason path->slots[1] = slot; 39265d4f98a2SYan Zheng ret = btrfs_del_leaf(trans, root, path, leaf); 3927323ac95bSChris Mason BUG_ON(ret); 39285f39d397SChris Mason free_extent_buffer(leaf); 39295de08d7dSChris Mason } else { 3930925baeddSChris Mason /* if we're still in the path, make sure 3931925baeddSChris Mason * we're dirty. Otherwise, one of the 3932925baeddSChris Mason * push_leaf functions must have already 3933925baeddSChris Mason * dirtied this buffer 3934925baeddSChris Mason */ 3935925baeddSChris Mason if (path->nodes[0] == leaf) 39365f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 39375f39d397SChris Mason free_extent_buffer(leaf); 3938be0e5c09SChris Mason } 3939d5719762SChris Mason } else { 39405f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 3941be0e5c09SChris Mason } 39429a8dd150SChris Mason } 3943aa5d6bedSChris Mason return ret; 39449a8dd150SChris Mason } 39459a8dd150SChris Mason 394697571fd0SChris Mason /* 3947925baeddSChris Mason * search the tree again to find a leaf with lesser keys 39487bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 39497bb86316SChris Mason * returns < 0 on io errors. 3950d352ac68SChris Mason * 3951d352ac68SChris Mason * This may release the path, and so you may lose any locks held at the 3952d352ac68SChris Mason * time you call it. 39537bb86316SChris Mason */ 39547bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 39557bb86316SChris Mason { 3956925baeddSChris Mason struct btrfs_key key; 3957925baeddSChris Mason struct btrfs_disk_key found_key; 3958925baeddSChris Mason int ret; 39597bb86316SChris Mason 3960925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 3961925baeddSChris Mason 3962925baeddSChris Mason if (key.offset > 0) 3963925baeddSChris Mason key.offset--; 3964925baeddSChris Mason else if (key.type > 0) 3965925baeddSChris Mason key.type--; 3966925baeddSChris Mason else if (key.objectid > 0) 3967925baeddSChris Mason key.objectid--; 3968925baeddSChris Mason else 39697bb86316SChris Mason return 1; 39707bb86316SChris Mason 3971925baeddSChris Mason btrfs_release_path(root, path); 3972925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 3973925baeddSChris Mason if (ret < 0) 3974925baeddSChris Mason return ret; 3975925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 3976925baeddSChris Mason ret = comp_keys(&found_key, &key); 3977925baeddSChris Mason if (ret < 0) 39787bb86316SChris Mason return 0; 3979925baeddSChris Mason return 1; 39807bb86316SChris Mason } 39817bb86316SChris Mason 39823f157a2fSChris Mason /* 39833f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 39843f157a2fSChris Mason * for nodes or leaves that are either in cache or have a minimum 3985d352ac68SChris Mason * transaction id. This is used by the btree defrag code, and tree logging 39863f157a2fSChris Mason * 39873f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 39883f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 39893f157a2fSChris Mason * key and get a writable path. 39903f157a2fSChris Mason * 39913f157a2fSChris Mason * This does lock as it descends, and path->keep_locks should be set 39923f157a2fSChris Mason * to 1 by the caller. 39933f157a2fSChris Mason * 39943f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 39953f157a2fSChris Mason * of the tree. 39963f157a2fSChris Mason * 3997d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 3998d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 3999d352ac68SChris Mason * skipped over (without reading them). 4000d352ac68SChris Mason * 40013f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 40023f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 40033f157a2fSChris Mason */ 40043f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 4005e02119d5SChris Mason struct btrfs_key *max_key, 40063f157a2fSChris Mason struct btrfs_path *path, int cache_only, 40073f157a2fSChris Mason u64 min_trans) 40083f157a2fSChris Mason { 40093f157a2fSChris Mason struct extent_buffer *cur; 40103f157a2fSChris Mason struct btrfs_key found_key; 40113f157a2fSChris Mason int slot; 40129652480bSYan int sret; 40133f157a2fSChris Mason u32 nritems; 40143f157a2fSChris Mason int level; 40153f157a2fSChris Mason int ret = 1; 40163f157a2fSChris Mason 4017934d375bSChris Mason WARN_ON(!path->keep_locks); 40183f157a2fSChris Mason again: 40193f157a2fSChris Mason cur = btrfs_lock_root_node(root); 40203f157a2fSChris Mason level = btrfs_header_level(cur); 4021e02119d5SChris Mason WARN_ON(path->nodes[level]); 40223f157a2fSChris Mason path->nodes[level] = cur; 40233f157a2fSChris Mason path->locks[level] = 1; 40243f157a2fSChris Mason 40253f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 40263f157a2fSChris Mason ret = 1; 40273f157a2fSChris Mason goto out; 40283f157a2fSChris Mason } 40293f157a2fSChris Mason while (1) { 40303f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 40313f157a2fSChris Mason level = btrfs_header_level(cur); 40329652480bSYan sret = bin_search(cur, min_key, level, &slot); 40333f157a2fSChris Mason 4034323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 4035323ac95bSChris Mason if (level == path->lowest_level) { 4036e02119d5SChris Mason if (slot >= nritems) 4037e02119d5SChris Mason goto find_next_key; 40383f157a2fSChris Mason ret = 0; 40393f157a2fSChris Mason path->slots[level] = slot; 40403f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 40413f157a2fSChris Mason goto out; 40423f157a2fSChris Mason } 40439652480bSYan if (sret && slot > 0) 40449652480bSYan slot--; 40453f157a2fSChris Mason /* 40463f157a2fSChris Mason * check this node pointer against the cache_only and 40473f157a2fSChris Mason * min_trans parameters. If it isn't in cache or is too 40483f157a2fSChris Mason * old, skip to the next one. 40493f157a2fSChris Mason */ 40503f157a2fSChris Mason while (slot < nritems) { 40513f157a2fSChris Mason u64 blockptr; 40523f157a2fSChris Mason u64 gen; 40533f157a2fSChris Mason struct extent_buffer *tmp; 4054e02119d5SChris Mason struct btrfs_disk_key disk_key; 4055e02119d5SChris Mason 40563f157a2fSChris Mason blockptr = btrfs_node_blockptr(cur, slot); 40573f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 40583f157a2fSChris Mason if (gen < min_trans) { 40593f157a2fSChris Mason slot++; 40603f157a2fSChris Mason continue; 40613f157a2fSChris Mason } 40623f157a2fSChris Mason if (!cache_only) 40633f157a2fSChris Mason break; 40643f157a2fSChris Mason 4065e02119d5SChris Mason if (max_key) { 4066e02119d5SChris Mason btrfs_node_key(cur, &disk_key, slot); 4067e02119d5SChris Mason if (comp_keys(&disk_key, max_key) >= 0) { 4068e02119d5SChris Mason ret = 1; 4069e02119d5SChris Mason goto out; 4070e02119d5SChris Mason } 4071e02119d5SChris Mason } 4072e02119d5SChris Mason 40733f157a2fSChris Mason tmp = btrfs_find_tree_block(root, blockptr, 40743f157a2fSChris Mason btrfs_level_size(root, level - 1)); 40753f157a2fSChris Mason 40763f157a2fSChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen)) { 40773f157a2fSChris Mason free_extent_buffer(tmp); 40783f157a2fSChris Mason break; 40793f157a2fSChris Mason } 40803f157a2fSChris Mason if (tmp) 40813f157a2fSChris Mason free_extent_buffer(tmp); 40823f157a2fSChris Mason slot++; 40833f157a2fSChris Mason } 4084e02119d5SChris Mason find_next_key: 40853f157a2fSChris Mason /* 40863f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 40873f157a2fSChris Mason * and find another one 40883f157a2fSChris Mason */ 40893f157a2fSChris Mason if (slot >= nritems) { 4090e02119d5SChris Mason path->slots[level] = slot; 4091b4ce94deSChris Mason btrfs_set_path_blocking(path); 4092e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 40933f157a2fSChris Mason cache_only, min_trans); 4094e02119d5SChris Mason if (sret == 0) { 40953f157a2fSChris Mason btrfs_release_path(root, path); 40963f157a2fSChris Mason goto again; 40973f157a2fSChris Mason } else { 40983f157a2fSChris Mason goto out; 40993f157a2fSChris Mason } 41003f157a2fSChris Mason } 41013f157a2fSChris Mason /* save our key for returning back */ 41023f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 41033f157a2fSChris Mason path->slots[level] = slot; 41043f157a2fSChris Mason if (level == path->lowest_level) { 41053f157a2fSChris Mason ret = 0; 41063f157a2fSChris Mason unlock_up(path, level, 1); 41073f157a2fSChris Mason goto out; 41083f157a2fSChris Mason } 4109b4ce94deSChris Mason btrfs_set_path_blocking(path); 41103f157a2fSChris Mason cur = read_node_slot(root, cur, slot); 411197d9a8a4STsutomu Itoh BUG_ON(!cur); 41123f157a2fSChris Mason 41133f157a2fSChris Mason btrfs_tree_lock(cur); 4114b4ce94deSChris Mason 41153f157a2fSChris Mason path->locks[level - 1] = 1; 41163f157a2fSChris Mason path->nodes[level - 1] = cur; 41173f157a2fSChris Mason unlock_up(path, level, 1); 41184008c04aSChris Mason btrfs_clear_path_blocking(path, NULL); 41193f157a2fSChris Mason } 41203f157a2fSChris Mason out: 41213f157a2fSChris Mason if (ret == 0) 41223f157a2fSChris Mason memcpy(min_key, &found_key, sizeof(found_key)); 4123b4ce94deSChris Mason btrfs_set_path_blocking(path); 41243f157a2fSChris Mason return ret; 41253f157a2fSChris Mason } 41263f157a2fSChris Mason 41273f157a2fSChris Mason /* 41283f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 41293f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 41303f157a2fSChris Mason * tree based on the current path and the cache_only and min_trans 41313f157a2fSChris Mason * parameters. 41323f157a2fSChris Mason * 41333f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 41343f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 41353f157a2fSChris Mason * 41363f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 41373f157a2fSChris Mason * calling this function. 41383f157a2fSChris Mason */ 4139e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 414033c66f43SYan Zheng struct btrfs_key *key, int level, 41413f157a2fSChris Mason int cache_only, u64 min_trans) 4142e7a84565SChris Mason { 4143e7a84565SChris Mason int slot; 4144e7a84565SChris Mason struct extent_buffer *c; 4145e7a84565SChris Mason 4146934d375bSChris Mason WARN_ON(!path->keep_locks); 4147e7a84565SChris Mason while (level < BTRFS_MAX_LEVEL) { 4148e7a84565SChris Mason if (!path->nodes[level]) 4149e7a84565SChris Mason return 1; 4150e7a84565SChris Mason 4151e7a84565SChris Mason slot = path->slots[level] + 1; 4152e7a84565SChris Mason c = path->nodes[level]; 41533f157a2fSChris Mason next: 4154e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 415533c66f43SYan Zheng int ret; 415633c66f43SYan Zheng int orig_lowest; 415733c66f43SYan Zheng struct btrfs_key cur_key; 415833c66f43SYan Zheng if (level + 1 >= BTRFS_MAX_LEVEL || 415933c66f43SYan Zheng !path->nodes[level + 1]) 4160e7a84565SChris Mason return 1; 416133c66f43SYan Zheng 416233c66f43SYan Zheng if (path->locks[level + 1]) { 416333c66f43SYan Zheng level++; 4164e7a84565SChris Mason continue; 4165e7a84565SChris Mason } 416633c66f43SYan Zheng 416733c66f43SYan Zheng slot = btrfs_header_nritems(c) - 1; 416833c66f43SYan Zheng if (level == 0) 416933c66f43SYan Zheng btrfs_item_key_to_cpu(c, &cur_key, slot); 417033c66f43SYan Zheng else 417133c66f43SYan Zheng btrfs_node_key_to_cpu(c, &cur_key, slot); 417233c66f43SYan Zheng 417333c66f43SYan Zheng orig_lowest = path->lowest_level; 417433c66f43SYan Zheng btrfs_release_path(root, path); 417533c66f43SYan Zheng path->lowest_level = level; 417633c66f43SYan Zheng ret = btrfs_search_slot(NULL, root, &cur_key, path, 417733c66f43SYan Zheng 0, 0); 417833c66f43SYan Zheng path->lowest_level = orig_lowest; 417933c66f43SYan Zheng if (ret < 0) 418033c66f43SYan Zheng return ret; 418133c66f43SYan Zheng 418233c66f43SYan Zheng c = path->nodes[level]; 418333c66f43SYan Zheng slot = path->slots[level]; 418433c66f43SYan Zheng if (ret == 0) 418533c66f43SYan Zheng slot++; 418633c66f43SYan Zheng goto next; 418733c66f43SYan Zheng } 418833c66f43SYan Zheng 4189e7a84565SChris Mason if (level == 0) 4190e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 41913f157a2fSChris Mason else { 41923f157a2fSChris Mason u64 blockptr = btrfs_node_blockptr(c, slot); 41933f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 41943f157a2fSChris Mason 41953f157a2fSChris Mason if (cache_only) { 41963f157a2fSChris Mason struct extent_buffer *cur; 41973f157a2fSChris Mason cur = btrfs_find_tree_block(root, blockptr, 41983f157a2fSChris Mason btrfs_level_size(root, level - 1)); 41993f157a2fSChris Mason if (!cur || !btrfs_buffer_uptodate(cur, gen)) { 42003f157a2fSChris Mason slot++; 42013f157a2fSChris Mason if (cur) 42023f157a2fSChris Mason free_extent_buffer(cur); 42033f157a2fSChris Mason goto next; 42043f157a2fSChris Mason } 42053f157a2fSChris Mason free_extent_buffer(cur); 42063f157a2fSChris Mason } 42073f157a2fSChris Mason if (gen < min_trans) { 42083f157a2fSChris Mason slot++; 42093f157a2fSChris Mason goto next; 42103f157a2fSChris Mason } 4211e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 42123f157a2fSChris Mason } 4213e7a84565SChris Mason return 0; 4214e7a84565SChris Mason } 4215e7a84565SChris Mason return 1; 4216e7a84565SChris Mason } 4217e7a84565SChris Mason 42187bb86316SChris Mason /* 4219925baeddSChris Mason * search the tree again to find a leaf with greater keys 42200f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 42210f70abe2SChris Mason * returns < 0 on io errors. 422297571fd0SChris Mason */ 4223234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 4224d97e63b6SChris Mason { 4225d97e63b6SChris Mason int slot; 42268e73f275SChris Mason int level; 42275f39d397SChris Mason struct extent_buffer *c; 42288e73f275SChris Mason struct extent_buffer *next; 4229925baeddSChris Mason struct btrfs_key key; 4230925baeddSChris Mason u32 nritems; 4231925baeddSChris Mason int ret; 42328e73f275SChris Mason int old_spinning = path->leave_spinning; 42338e73f275SChris Mason int force_blocking = 0; 4234925baeddSChris Mason 4235925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4236d397712bSChris Mason if (nritems == 0) 4237925baeddSChris Mason return 1; 4238925baeddSChris Mason 42398e73f275SChris Mason /* 42408e73f275SChris Mason * we take the blocks in an order that upsets lockdep. Using 42418e73f275SChris Mason * blocking mode is the only way around it. 42428e73f275SChris Mason */ 42438e73f275SChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 42448e73f275SChris Mason force_blocking = 1; 42458e73f275SChris Mason #endif 4246925baeddSChris Mason 42478e73f275SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 42488e73f275SChris Mason again: 42498e73f275SChris Mason level = 1; 42508e73f275SChris Mason next = NULL; 4251925baeddSChris Mason btrfs_release_path(root, path); 42528e73f275SChris Mason 4253a2135011SChris Mason path->keep_locks = 1; 42548e73f275SChris Mason 42558e73f275SChris Mason if (!force_blocking) 42568e73f275SChris Mason path->leave_spinning = 1; 42578e73f275SChris Mason 4258925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4259925baeddSChris Mason path->keep_locks = 0; 4260925baeddSChris Mason 4261925baeddSChris Mason if (ret < 0) 4262925baeddSChris Mason return ret; 4263925baeddSChris Mason 4264a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4265168fd7d2SChris Mason /* 4266168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 4267168fd7d2SChris Mason * could have added more items next to the key that used to be 4268168fd7d2SChris Mason * at the very end of the block. So, check again here and 4269168fd7d2SChris Mason * advance the path if there are now more items available. 4270168fd7d2SChris Mason */ 4271a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 4272e457afecSYan Zheng if (ret == 0) 4273168fd7d2SChris Mason path->slots[0]++; 42748e73f275SChris Mason ret = 0; 4275925baeddSChris Mason goto done; 4276925baeddSChris Mason } 4277d97e63b6SChris Mason 4278234b63a0SChris Mason while (level < BTRFS_MAX_LEVEL) { 42798e73f275SChris Mason if (!path->nodes[level]) { 42808e73f275SChris Mason ret = 1; 42818e73f275SChris Mason goto done; 42828e73f275SChris Mason } 42835f39d397SChris Mason 4284d97e63b6SChris Mason slot = path->slots[level] + 1; 4285d97e63b6SChris Mason c = path->nodes[level]; 42865f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 4287d97e63b6SChris Mason level++; 42888e73f275SChris Mason if (level == BTRFS_MAX_LEVEL) { 42898e73f275SChris Mason ret = 1; 42908e73f275SChris Mason goto done; 42918e73f275SChris Mason } 4292d97e63b6SChris Mason continue; 4293d97e63b6SChris Mason } 42945f39d397SChris Mason 4295925baeddSChris Mason if (next) { 4296925baeddSChris Mason btrfs_tree_unlock(next); 42975f39d397SChris Mason free_extent_buffer(next); 4298925baeddSChris Mason } 42995f39d397SChris Mason 43008e73f275SChris Mason next = c; 43018e73f275SChris Mason ret = read_block_for_search(NULL, root, path, &next, level, 43028e73f275SChris Mason slot, &key); 43038e73f275SChris Mason if (ret == -EAGAIN) 43048e73f275SChris Mason goto again; 43055f39d397SChris Mason 430676a05b35SChris Mason if (ret < 0) { 430776a05b35SChris Mason btrfs_release_path(root, path); 430876a05b35SChris Mason goto done; 430976a05b35SChris Mason } 431076a05b35SChris Mason 43115cd57b2cSChris Mason if (!path->skip_locking) { 43128e73f275SChris Mason ret = btrfs_try_spin_lock(next); 43138e73f275SChris Mason if (!ret) { 43148e73f275SChris Mason btrfs_set_path_blocking(path); 4315925baeddSChris Mason btrfs_tree_lock(next); 43168e73f275SChris Mason if (!force_blocking) 43178e73f275SChris Mason btrfs_clear_path_blocking(path, next); 43188e73f275SChris Mason } 43198e73f275SChris Mason if (force_blocking) 4320b4ce94deSChris Mason btrfs_set_lock_blocking(next); 43215cd57b2cSChris Mason } 4322d97e63b6SChris Mason break; 4323d97e63b6SChris Mason } 4324d97e63b6SChris Mason path->slots[level] = slot; 4325d97e63b6SChris Mason while (1) { 4326d97e63b6SChris Mason level--; 4327d97e63b6SChris Mason c = path->nodes[level]; 4328925baeddSChris Mason if (path->locks[level]) 4329925baeddSChris Mason btrfs_tree_unlock(c); 43308e73f275SChris Mason 43315f39d397SChris Mason free_extent_buffer(c); 4332d97e63b6SChris Mason path->nodes[level] = next; 4333d97e63b6SChris Mason path->slots[level] = 0; 4334a74a4b97SChris Mason if (!path->skip_locking) 4335925baeddSChris Mason path->locks[level] = 1; 43368e73f275SChris Mason 4337d97e63b6SChris Mason if (!level) 4338d97e63b6SChris Mason break; 4339b4ce94deSChris Mason 43408e73f275SChris Mason ret = read_block_for_search(NULL, root, path, &next, level, 43418e73f275SChris Mason 0, &key); 43428e73f275SChris Mason if (ret == -EAGAIN) 43438e73f275SChris Mason goto again; 43448e73f275SChris Mason 434576a05b35SChris Mason if (ret < 0) { 434676a05b35SChris Mason btrfs_release_path(root, path); 434776a05b35SChris Mason goto done; 434876a05b35SChris Mason } 434976a05b35SChris Mason 43505cd57b2cSChris Mason if (!path->skip_locking) { 4351b9447ef8SChris Mason btrfs_assert_tree_locked(path->nodes[level]); 43528e73f275SChris Mason ret = btrfs_try_spin_lock(next); 43538e73f275SChris Mason if (!ret) { 43548e73f275SChris Mason btrfs_set_path_blocking(path); 4355925baeddSChris Mason btrfs_tree_lock(next); 43568e73f275SChris Mason if (!force_blocking) 43578e73f275SChris Mason btrfs_clear_path_blocking(path, next); 43588e73f275SChris Mason } 43598e73f275SChris Mason if (force_blocking) 4360b4ce94deSChris Mason btrfs_set_lock_blocking(next); 4361d97e63b6SChris Mason } 43625cd57b2cSChris Mason } 43638e73f275SChris Mason ret = 0; 4364925baeddSChris Mason done: 4365925baeddSChris Mason unlock_up(path, 0, 1); 43668e73f275SChris Mason path->leave_spinning = old_spinning; 43678e73f275SChris Mason if (!old_spinning) 43688e73f275SChris Mason btrfs_set_path_blocking(path); 43698e73f275SChris Mason 43708e73f275SChris Mason return ret; 4371d97e63b6SChris Mason } 43720b86a832SChris Mason 43733f157a2fSChris Mason /* 43743f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 43753f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 43763f157a2fSChris Mason * 43773f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 43783f157a2fSChris Mason */ 43790b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 43800b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 43810b86a832SChris Mason int type) 43820b86a832SChris Mason { 43830b86a832SChris Mason struct btrfs_key found_key; 43840b86a832SChris Mason struct extent_buffer *leaf; 4385e02119d5SChris Mason u32 nritems; 43860b86a832SChris Mason int ret; 43870b86a832SChris Mason 43880b86a832SChris Mason while (1) { 43890b86a832SChris Mason if (path->slots[0] == 0) { 4390b4ce94deSChris Mason btrfs_set_path_blocking(path); 43910b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 43920b86a832SChris Mason if (ret != 0) 43930b86a832SChris Mason return ret; 43940b86a832SChris Mason } else { 43950b86a832SChris Mason path->slots[0]--; 43960b86a832SChris Mason } 43970b86a832SChris Mason leaf = path->nodes[0]; 4398e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 4399e02119d5SChris Mason if (nritems == 0) 4400e02119d5SChris Mason return 1; 4401e02119d5SChris Mason if (path->slots[0] == nritems) 4402e02119d5SChris Mason path->slots[0]--; 4403e02119d5SChris Mason 44040b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 4405e02119d5SChris Mason if (found_key.objectid < min_objectid) 4406e02119d5SChris Mason break; 44070a4eefbbSYan Zheng if (found_key.type == type) 44080a4eefbbSYan Zheng return 0; 4409e02119d5SChris Mason if (found_key.objectid == min_objectid && 4410e02119d5SChris Mason found_key.type < type) 4411e02119d5SChris Mason break; 44120b86a832SChris Mason } 44130b86a832SChris Mason return 1; 44140b86a832SChris Mason } 4415