16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 19a6b6e75eSChris Mason #include <linux/sched.h> 20eb60ceacSChris Mason #include "ctree.h" 21eb60ceacSChris Mason #include "disk-io.h" 227f5c1516SChris Mason #include "transaction.h" 235f39d397SChris Mason #include "print-tree.h" 24*925baeddSChris Mason #include "locking.h" 259a8dd150SChris Mason 26e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 27e089f05cSChris Mason *root, struct btrfs_path *path, int level); 28e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root 29d4dbff95SChris Mason *root, struct btrfs_key *ins_key, 30cc0c5538SChris Mason struct btrfs_path *path, int data_size, int extend); 315f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 325f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 33971a1f66SChris Mason struct extent_buffer *src, int empty); 345f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 355f39d397SChris Mason struct btrfs_root *root, 365f39d397SChris Mason struct extent_buffer *dst_buf, 375f39d397SChris Mason struct extent_buffer *src_buf); 38e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 39e089f05cSChris Mason struct btrfs_path *path, int level, int slot); 40d97e63b6SChris Mason 41df24a2b9SChris Mason inline void btrfs_init_path(struct btrfs_path *p) 42df24a2b9SChris Mason { 43df24a2b9SChris Mason memset(p, 0, sizeof(*p)); 44df24a2b9SChris Mason } 45df24a2b9SChris Mason 462c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 472c90e5d6SChris Mason { 48df24a2b9SChris Mason struct btrfs_path *path; 49df24a2b9SChris Mason path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); 502cc58cf2SChris Mason if (path) { 51df24a2b9SChris Mason btrfs_init_path(path); 522cc58cf2SChris Mason path->reada = 1; 532cc58cf2SChris Mason } 54df24a2b9SChris Mason return path; 552c90e5d6SChris Mason } 562c90e5d6SChris Mason 572c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 582c90e5d6SChris Mason { 59df24a2b9SChris Mason btrfs_release_path(NULL, p); 602c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 612c90e5d6SChris Mason } 622c90e5d6SChris Mason 63234b63a0SChris Mason void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) 64eb60ceacSChris Mason { 65eb60ceacSChris Mason int i; 66234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 67eb60ceacSChris Mason if (!p->nodes[i]) 68*925baeddSChris Mason continue; 69*925baeddSChris Mason if (p->locks[i]) { 70*925baeddSChris Mason btrfs_tree_unlock(p->nodes[i]); 71*925baeddSChris Mason p->locks[i] = 0; 72*925baeddSChris Mason } 735f39d397SChris Mason free_extent_buffer(p->nodes[i]); 74eb60ceacSChris Mason } 75aa5d6bedSChris Mason memset(p, 0, sizeof(*p)); 76eb60ceacSChris Mason } 77eb60ceacSChris Mason 78*925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 79*925baeddSChris Mason { 80*925baeddSChris Mason struct extent_buffer *eb; 81*925baeddSChris Mason spin_lock(&root->node_lock); 82*925baeddSChris Mason eb = root->node; 83*925baeddSChris Mason extent_buffer_get(eb); 84*925baeddSChris Mason spin_unlock(&root->node_lock); 85*925baeddSChris Mason return eb; 86*925baeddSChris Mason } 87*925baeddSChris Mason 88*925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 89*925baeddSChris Mason { 90*925baeddSChris Mason struct extent_buffer *eb; 91*925baeddSChris Mason 92*925baeddSChris Mason while(1) { 93*925baeddSChris Mason eb = btrfs_root_node(root); 94*925baeddSChris Mason btrfs_tree_lock(eb); 95*925baeddSChris Mason 96*925baeddSChris Mason spin_lock(&root->node_lock); 97*925baeddSChris Mason if (eb == root->node) { 98*925baeddSChris Mason spin_unlock(&root->node_lock); 99*925baeddSChris Mason break; 100*925baeddSChris Mason } 101*925baeddSChris Mason spin_unlock(&root->node_lock); 102*925baeddSChris Mason 103*925baeddSChris Mason btrfs_tree_unlock(eb); 104*925baeddSChris Mason free_extent_buffer(eb); 105*925baeddSChris Mason } 106*925baeddSChris Mason return eb; 107*925baeddSChris Mason } 108*925baeddSChris Mason 1090b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 1100b86a832SChris Mason { 1110b86a832SChris Mason if (root->track_dirty && list_empty(&root->dirty_list)) { 1120b86a832SChris Mason list_add(&root->dirty_list, 1130b86a832SChris Mason &root->fs_info->dirty_cowonly_roots); 1140b86a832SChris Mason } 1150b86a832SChris Mason } 1160b86a832SChris Mason 117be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 118be20aa9dSChris Mason struct btrfs_root *root, 119be20aa9dSChris Mason struct extent_buffer *buf, 120be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 121be20aa9dSChris Mason { 122be20aa9dSChris Mason struct extent_buffer *cow; 123be20aa9dSChris Mason u32 nritems; 124be20aa9dSChris Mason int ret = 0; 125be20aa9dSChris Mason int level; 126be20aa9dSChris Mason struct btrfs_key first_key; 1274aec2b52SChris Mason struct btrfs_root *new_root; 128be20aa9dSChris Mason 1294aec2b52SChris Mason new_root = kmalloc(sizeof(*new_root), GFP_NOFS); 1304aec2b52SChris Mason if (!new_root) 1314aec2b52SChris Mason return -ENOMEM; 1324aec2b52SChris Mason 1334aec2b52SChris Mason memcpy(new_root, root, sizeof(*new_root)); 1344aec2b52SChris Mason new_root->root_key.objectid = new_root_objectid; 135be20aa9dSChris Mason 136be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != 137be20aa9dSChris Mason root->fs_info->running_transaction->transid); 138be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 139be20aa9dSChris Mason 140be20aa9dSChris Mason level = btrfs_header_level(buf); 141be20aa9dSChris Mason nritems = btrfs_header_nritems(buf); 142be20aa9dSChris Mason if (nritems) { 143be20aa9dSChris Mason if (level == 0) 144be20aa9dSChris Mason btrfs_item_key_to_cpu(buf, &first_key, 0); 145be20aa9dSChris Mason else 146be20aa9dSChris Mason btrfs_node_key_to_cpu(buf, &first_key, 0); 147be20aa9dSChris Mason } else { 148be20aa9dSChris Mason first_key.objectid = 0; 149be20aa9dSChris Mason } 150*925baeddSChris Mason cow = btrfs_alloc_free_block(trans, new_root, buf->len, 151be20aa9dSChris Mason new_root_objectid, 152be20aa9dSChris Mason trans->transid, first_key.objectid, 153be20aa9dSChris Mason level, buf->start, 0); 1544aec2b52SChris Mason if (IS_ERR(cow)) { 1554aec2b52SChris Mason kfree(new_root); 156be20aa9dSChris Mason return PTR_ERR(cow); 1574aec2b52SChris Mason } 158be20aa9dSChris Mason 159be20aa9dSChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 160be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 161be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 162be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 16363b10fc4SChris Mason btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 164be20aa9dSChris Mason 165be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 1664aec2b52SChris Mason ret = btrfs_inc_ref(trans, new_root, buf); 1674aec2b52SChris Mason kfree(new_root); 1684aec2b52SChris Mason 169be20aa9dSChris Mason if (ret) 170be20aa9dSChris Mason return ret; 171be20aa9dSChris Mason 172be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 173be20aa9dSChris Mason *cow_ret = cow; 174be20aa9dSChris Mason return 0; 175be20aa9dSChris Mason } 176be20aa9dSChris Mason 177be20aa9dSChris Mason int __btrfs_cow_block(struct btrfs_trans_handle *trans, 1785f39d397SChris Mason struct btrfs_root *root, 1795f39d397SChris Mason struct extent_buffer *buf, 1805f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 1815f39d397SChris Mason struct extent_buffer **cow_ret, 1825f39d397SChris Mason u64 search_start, u64 empty_size) 1836702ed49SChris Mason { 1847bb86316SChris Mason u64 root_gen; 1855f39d397SChris Mason struct extent_buffer *cow; 1867bb86316SChris Mason u32 nritems; 1876702ed49SChris Mason int ret = 0; 1886702ed49SChris Mason int different_trans = 0; 1897bb86316SChris Mason int level; 190*925baeddSChris Mason int unlock_orig = 0; 1917bb86316SChris Mason struct btrfs_key first_key; 1926702ed49SChris Mason 193*925baeddSChris Mason if (*cow_ret == buf) 194*925baeddSChris Mason unlock_orig = 1; 195*925baeddSChris Mason 196*925baeddSChris Mason WARN_ON(!btrfs_tree_locked(buf)); 197*925baeddSChris Mason 1987bb86316SChris Mason if (root->ref_cows) { 1997bb86316SChris Mason root_gen = trans->transid; 2007bb86316SChris Mason } else { 2017bb86316SChris Mason root_gen = 0; 2027bb86316SChris Mason } 2037bb86316SChris Mason WARN_ON(root->ref_cows && trans->transid != 2047bb86316SChris Mason root->fs_info->running_transaction->transid); 2056702ed49SChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 2065f39d397SChris Mason 2077bb86316SChris Mason level = btrfs_header_level(buf); 2087bb86316SChris Mason nritems = btrfs_header_nritems(buf); 2097bb86316SChris Mason if (nritems) { 2107bb86316SChris Mason if (level == 0) 2117bb86316SChris Mason btrfs_item_key_to_cpu(buf, &first_key, 0); 2127bb86316SChris Mason else 2137bb86316SChris Mason btrfs_node_key_to_cpu(buf, &first_key, 0); 2147bb86316SChris Mason } else { 2157bb86316SChris Mason first_key.objectid = 0; 2167bb86316SChris Mason } 217*925baeddSChris Mason cow = btrfs_alloc_free_block(trans, root, buf->len, 2187bb86316SChris Mason root->root_key.objectid, 2197bb86316SChris Mason root_gen, first_key.objectid, level, 220db94535dSChris Mason search_start, empty_size); 2216702ed49SChris Mason if (IS_ERR(cow)) 2226702ed49SChris Mason return PTR_ERR(cow); 2236702ed49SChris Mason 2245f39d397SChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 225db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 2265f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 2275f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 22863b10fc4SChris Mason btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 2296702ed49SChris Mason 2305f39d397SChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 2315f39d397SChris Mason if (btrfs_header_generation(buf) != trans->transid) { 2326702ed49SChris Mason different_trans = 1; 2336702ed49SChris Mason ret = btrfs_inc_ref(trans, root, buf); 2346702ed49SChris Mason if (ret) 2356702ed49SChris Mason return ret; 2366702ed49SChris Mason } else { 2376702ed49SChris Mason clean_tree_block(trans, root, buf); 2386702ed49SChris Mason } 2396702ed49SChris Mason 2406702ed49SChris Mason if (buf == root->node) { 241*925baeddSChris Mason WARN_ON(parent && parent != buf); 2427bb86316SChris Mason root_gen = btrfs_header_generation(buf); 243*925baeddSChris Mason 244*925baeddSChris Mason spin_lock(&root->node_lock); 2456702ed49SChris Mason root->node = cow; 2465f39d397SChris Mason extent_buffer_get(cow); 247*925baeddSChris Mason spin_unlock(&root->node_lock); 248*925baeddSChris Mason 2496702ed49SChris Mason if (buf != root->commit_root) { 250db94535dSChris Mason btrfs_free_extent(trans, root, buf->start, 2517bb86316SChris Mason buf->len, root->root_key.objectid, 2527bb86316SChris Mason root_gen, 0, 0, 1); 2536702ed49SChris Mason } 2545f39d397SChris Mason free_extent_buffer(buf); 2550b86a832SChris Mason add_root_to_dirty_list(root); 2566702ed49SChris Mason } else { 2577bb86316SChris Mason root_gen = btrfs_header_generation(parent); 2585f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 259db94535dSChris Mason cow->start); 26074493f7aSChris Mason WARN_ON(trans->transid == 0); 26174493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 26274493f7aSChris Mason trans->transid); 2636702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 2645f39d397SChris Mason WARN_ON(btrfs_header_generation(parent) != trans->transid); 2657bb86316SChris Mason btrfs_free_extent(trans, root, buf->start, buf->len, 2667bb86316SChris Mason btrfs_header_owner(parent), root_gen, 2677bb86316SChris Mason 0, 0, 1); 2686702ed49SChris Mason } 269*925baeddSChris Mason if (unlock_orig) 270*925baeddSChris Mason btrfs_tree_unlock(buf); 2715f39d397SChris Mason free_extent_buffer(buf); 2726702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 2736702ed49SChris Mason *cow_ret = cow; 2746702ed49SChris Mason return 0; 2756702ed49SChris Mason } 2766702ed49SChris Mason 2775f39d397SChris Mason int btrfs_cow_block(struct btrfs_trans_handle *trans, 2785f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 2795f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 2805f39d397SChris Mason struct extent_buffer **cow_ret) 28102217ed2SChris Mason { 2826702ed49SChris Mason u64 search_start; 283dc17ff8fSChris Mason u64 header_trans; 284f510cfecSChris Mason int ret; 285dc17ff8fSChris Mason 286ccd467d6SChris Mason if (trans->transaction != root->fs_info->running_transaction) { 287ccd467d6SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 288ccd467d6SChris Mason root->fs_info->running_transaction->transid); 289ccd467d6SChris Mason WARN_ON(1); 290ccd467d6SChris Mason } 291ccd467d6SChris Mason if (trans->transid != root->fs_info->generation) { 292ccd467d6SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 293ccd467d6SChris Mason root->fs_info->generation); 294ccd467d6SChris Mason WARN_ON(1); 295ccd467d6SChris Mason } 296dc17ff8fSChris Mason 297dc17ff8fSChris Mason header_trans = btrfs_header_generation(buf); 29863b10fc4SChris Mason spin_lock(&root->fs_info->hash_lock); 29963b10fc4SChris Mason if (header_trans == trans->transid && 30063b10fc4SChris Mason !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 30102217ed2SChris Mason *cow_ret = buf; 30263b10fc4SChris Mason spin_unlock(&root->fs_info->hash_lock); 30302217ed2SChris Mason return 0; 30402217ed2SChris Mason } 30563b10fc4SChris Mason spin_unlock(&root->fs_info->hash_lock); 3060b86a832SChris Mason search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 307f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 3086702ed49SChris Mason parent_slot, cow_ret, search_start, 0); 309f510cfecSChris Mason return ret; 3102c90e5d6SChris Mason } 3116702ed49SChris Mason 3126b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 3136702ed49SChris Mason { 3146b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 3156702ed49SChris Mason return 1; 3166b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 3176702ed49SChris Mason return 1; 31802217ed2SChris Mason return 0; 31902217ed2SChris Mason } 32002217ed2SChris Mason 321081e9573SChris Mason /* 322081e9573SChris Mason * compare two keys in a memcmp fashion 323081e9573SChris Mason */ 324081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) 325081e9573SChris Mason { 326081e9573SChris Mason struct btrfs_key k1; 327081e9573SChris Mason 328081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 329081e9573SChris Mason 330081e9573SChris Mason if (k1.objectid > k2->objectid) 331081e9573SChris Mason return 1; 332081e9573SChris Mason if (k1.objectid < k2->objectid) 333081e9573SChris Mason return -1; 334081e9573SChris Mason if (k1.type > k2->type) 335081e9573SChris Mason return 1; 336081e9573SChris Mason if (k1.type < k2->type) 337081e9573SChris Mason return -1; 338081e9573SChris Mason if (k1.offset > k2->offset) 339081e9573SChris Mason return 1; 340081e9573SChris Mason if (k1.offset < k2->offset) 341081e9573SChris Mason return -1; 342081e9573SChris Mason return 0; 343081e9573SChris Mason } 344081e9573SChris Mason 345081e9573SChris Mason 3466702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 3475f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 348a6b6e75eSChris Mason int start_slot, int cache_only, u64 *last_ret, 349a6b6e75eSChris Mason struct btrfs_key *progress) 3506702ed49SChris Mason { 3516b80053dSChris Mason struct extent_buffer *cur; 3526b80053dSChris Mason struct extent_buffer *tmp; 3536702ed49SChris Mason u64 blocknr; 354ca7a79adSChris Mason u64 gen; 355e9d0b13bSChris Mason u64 search_start = *last_ret; 356e9d0b13bSChris Mason u64 last_block = 0; 3576702ed49SChris Mason u64 other; 3586702ed49SChris Mason u32 parent_nritems; 3596702ed49SChris Mason int end_slot; 3606702ed49SChris Mason int i; 3616702ed49SChris Mason int err = 0; 362f2183bdeSChris Mason int parent_level; 3636b80053dSChris Mason int uptodate; 3646b80053dSChris Mason u32 blocksize; 365081e9573SChris Mason int progress_passed = 0; 366081e9573SChris Mason struct btrfs_disk_key disk_key; 3676702ed49SChris Mason 368*925baeddSChris Mason /* FIXME this code needs locking */ 369*925baeddSChris Mason return 0; 370*925baeddSChris Mason 3715708b959SChris Mason parent_level = btrfs_header_level(parent); 3725708b959SChris Mason if (cache_only && parent_level != 1) 3735708b959SChris Mason return 0; 3745708b959SChris Mason 3756702ed49SChris Mason if (trans->transaction != root->fs_info->running_transaction) { 3766702ed49SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 3776702ed49SChris Mason root->fs_info->running_transaction->transid); 3786702ed49SChris Mason WARN_ON(1); 3796702ed49SChris Mason } 3806702ed49SChris Mason if (trans->transid != root->fs_info->generation) { 3816702ed49SChris Mason printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 3826702ed49SChris Mason root->fs_info->generation); 3836702ed49SChris Mason WARN_ON(1); 3846702ed49SChris Mason } 38586479a04SChris Mason 3866b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 3876b80053dSChris Mason blocksize = btrfs_level_size(root, parent_level - 1); 3886702ed49SChris Mason end_slot = parent_nritems; 3896702ed49SChris Mason 3906702ed49SChris Mason if (parent_nritems == 1) 3916702ed49SChris Mason return 0; 3926702ed49SChris Mason 3936702ed49SChris Mason for (i = start_slot; i < end_slot; i++) { 3946702ed49SChris Mason int close = 1; 395a6b6e75eSChris Mason 3965708b959SChris Mason if (!parent->map_token) { 3975708b959SChris Mason map_extent_buffer(parent, 3985708b959SChris Mason btrfs_node_key_ptr_offset(i), 3995708b959SChris Mason sizeof(struct btrfs_key_ptr), 4005708b959SChris Mason &parent->map_token, &parent->kaddr, 4015708b959SChris Mason &parent->map_start, &parent->map_len, 4025708b959SChris Mason KM_USER1); 4035708b959SChris Mason } 404081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 405081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 406081e9573SChris Mason continue; 407081e9573SChris Mason 408081e9573SChris Mason progress_passed = 1; 4096b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 410ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 411e9d0b13bSChris Mason if (last_block == 0) 412e9d0b13bSChris Mason last_block = blocknr; 4135708b959SChris Mason 4146702ed49SChris Mason if (i > 0) { 4156b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 4166b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 4176702ed49SChris Mason } 4180ef3e66bSChris Mason if (!close && i < end_slot - 2) { 4196b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 4206b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 4216702ed49SChris Mason } 422e9d0b13bSChris Mason if (close) { 423e9d0b13bSChris Mason last_block = blocknr; 4246702ed49SChris Mason continue; 425e9d0b13bSChris Mason } 4265708b959SChris Mason if (parent->map_token) { 4275708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 4285708b959SChris Mason KM_USER1); 4295708b959SChris Mason parent->map_token = NULL; 4305708b959SChris Mason } 4316702ed49SChris Mason 4326b80053dSChris Mason cur = btrfs_find_tree_block(root, blocknr, blocksize); 4336b80053dSChris Mason if (cur) 4341259ab75SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen); 4356b80053dSChris Mason else 4366b80053dSChris Mason uptodate = 0; 4375708b959SChris Mason if (!cur || !uptodate) { 4386702ed49SChris Mason if (cache_only) { 4396b80053dSChris Mason free_extent_buffer(cur); 4406702ed49SChris Mason continue; 4416702ed49SChris Mason } 4426b80053dSChris Mason if (!cur) { 4436b80053dSChris Mason cur = read_tree_block(root, blocknr, 444ca7a79adSChris Mason blocksize, gen); 4456b80053dSChris Mason } else if (!uptodate) { 446ca7a79adSChris Mason btrfs_read_buffer(cur, gen); 4476702ed49SChris Mason } 448f2183bdeSChris Mason } 449e9d0b13bSChris Mason if (search_start == 0) 4506b80053dSChris Mason search_start = last_block; 451e9d0b13bSChris Mason 4526b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 4536b80053dSChris Mason &tmp, search_start, 4546b80053dSChris Mason min(16 * blocksize, 4556b80053dSChris Mason (end_slot - i) * blocksize)); 456252c38f0SYan if (err) { 4576b80053dSChris Mason free_extent_buffer(cur); 4586702ed49SChris Mason break; 459252c38f0SYan } 4606b80053dSChris Mason search_start = tmp->start; 4615708b959SChris Mason last_block = tmp->start; 462f2183bdeSChris Mason *last_ret = search_start; 463f2183bdeSChris Mason if (parent_level == 1) 4646b80053dSChris Mason btrfs_clear_buffer_defrag(tmp); 4656b80053dSChris Mason free_extent_buffer(tmp); 4666702ed49SChris Mason } 4675708b959SChris Mason if (parent->map_token) { 4685708b959SChris Mason unmap_extent_buffer(parent, parent->map_token, 4695708b959SChris Mason KM_USER1); 4705708b959SChris Mason parent->map_token = NULL; 4715708b959SChris Mason } 4726702ed49SChris Mason return err; 4736702ed49SChris Mason } 4746702ed49SChris Mason 47574123bd7SChris Mason /* 47674123bd7SChris Mason * The leaf data grows from end-to-front in the node. 47774123bd7SChris Mason * this returns the address of the start of the last item, 47874123bd7SChris Mason * which is the stop of the leaf data stack 47974123bd7SChris Mason */ 480123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root, 4815f39d397SChris Mason struct extent_buffer *leaf) 482be0e5c09SChris Mason { 4835f39d397SChris Mason u32 nr = btrfs_header_nritems(leaf); 484be0e5c09SChris Mason if (nr == 0) 485123abc88SChris Mason return BTRFS_LEAF_DATA_SIZE(root); 4865f39d397SChris Mason return btrfs_item_offset_nr(leaf, nr - 1); 487be0e5c09SChris Mason } 488be0e5c09SChris Mason 489123abc88SChris Mason static int check_node(struct btrfs_root *root, struct btrfs_path *path, 490123abc88SChris Mason int level) 491aa5d6bedSChris Mason { 4925f39d397SChris Mason struct extent_buffer *parent = NULL; 4935f39d397SChris Mason struct extent_buffer *node = path->nodes[level]; 4945f39d397SChris Mason struct btrfs_disk_key parent_key; 4955f39d397SChris Mason struct btrfs_disk_key node_key; 496aa5d6bedSChris Mason int parent_slot; 4978d7be552SChris Mason int slot; 4988d7be552SChris Mason struct btrfs_key cpukey; 4995f39d397SChris Mason u32 nritems = btrfs_header_nritems(node); 500aa5d6bedSChris Mason 501aa5d6bedSChris Mason if (path->nodes[level + 1]) 5025f39d397SChris Mason parent = path->nodes[level + 1]; 503a1f39630SAneesh 5048d7be552SChris Mason slot = path->slots[level]; 5057518a238SChris Mason BUG_ON(nritems == 0); 5067518a238SChris Mason if (parent) { 507a1f39630SAneesh parent_slot = path->slots[level + 1]; 5085f39d397SChris Mason btrfs_node_key(parent, &parent_key, parent_slot); 5095f39d397SChris Mason btrfs_node_key(node, &node_key, 0); 5105f39d397SChris Mason BUG_ON(memcmp(&parent_key, &node_key, 511e2fa7227SChris Mason sizeof(struct btrfs_disk_key))); 5121d4f8a0cSChris Mason BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 513db94535dSChris Mason btrfs_header_bytenr(node)); 514aa5d6bedSChris Mason } 515123abc88SChris Mason BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); 5168d7be552SChris Mason if (slot != 0) { 5175f39d397SChris Mason btrfs_node_key_to_cpu(node, &cpukey, slot - 1); 5185f39d397SChris Mason btrfs_node_key(node, &node_key, slot); 5195f39d397SChris Mason BUG_ON(comp_keys(&node_key, &cpukey) <= 0); 5208d7be552SChris Mason } 5218d7be552SChris Mason if (slot < nritems - 1) { 5225f39d397SChris Mason btrfs_node_key_to_cpu(node, &cpukey, slot + 1); 5235f39d397SChris Mason btrfs_node_key(node, &node_key, slot); 5245f39d397SChris Mason BUG_ON(comp_keys(&node_key, &cpukey) >= 0); 525aa5d6bedSChris Mason } 526aa5d6bedSChris Mason return 0; 527aa5d6bedSChris Mason } 528aa5d6bedSChris Mason 529123abc88SChris Mason static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, 530123abc88SChris Mason int level) 531aa5d6bedSChris Mason { 5325f39d397SChris Mason struct extent_buffer *leaf = path->nodes[level]; 5335f39d397SChris Mason struct extent_buffer *parent = NULL; 534aa5d6bedSChris Mason int parent_slot; 5358d7be552SChris Mason struct btrfs_key cpukey; 5365f39d397SChris Mason struct btrfs_disk_key parent_key; 5375f39d397SChris Mason struct btrfs_disk_key leaf_key; 5385f39d397SChris Mason int slot = path->slots[0]; 5398d7be552SChris Mason 5405f39d397SChris Mason u32 nritems = btrfs_header_nritems(leaf); 541aa5d6bedSChris Mason 542aa5d6bedSChris Mason if (path->nodes[level + 1]) 5435f39d397SChris Mason parent = path->nodes[level + 1]; 5447518a238SChris Mason 5457518a238SChris Mason if (nritems == 0) 5467518a238SChris Mason return 0; 5477518a238SChris Mason 5487518a238SChris Mason if (parent) { 549a1f39630SAneesh parent_slot = path->slots[level + 1]; 5505f39d397SChris Mason btrfs_node_key(parent, &parent_key, parent_slot); 5515f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, 0); 5526702ed49SChris Mason 5535f39d397SChris Mason BUG_ON(memcmp(&parent_key, &leaf_key, 554e2fa7227SChris Mason sizeof(struct btrfs_disk_key))); 5551d4f8a0cSChris Mason BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 556db94535dSChris Mason btrfs_header_bytenr(leaf)); 557aa5d6bedSChris Mason } 5585f39d397SChris Mason #if 0 5595f39d397SChris Mason for (i = 0; nritems > 1 && i < nritems - 2; i++) { 5605f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, i + 1); 5615f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, i); 5625f39d397SChris Mason if (comp_keys(&leaf_key, &cpukey) >= 0) { 5635f39d397SChris Mason btrfs_print_leaf(root, leaf); 5645f39d397SChris Mason printk("slot %d offset bad key\n", i); 5655f39d397SChris Mason BUG_ON(1); 5665f39d397SChris Mason } 5675f39d397SChris Mason if (btrfs_item_offset_nr(leaf, i) != 5685f39d397SChris Mason btrfs_item_end_nr(leaf, i + 1)) { 5695f39d397SChris Mason btrfs_print_leaf(root, leaf); 5705f39d397SChris Mason printk("slot %d offset bad\n", i); 5715f39d397SChris Mason BUG_ON(1); 5725f39d397SChris Mason } 5735f39d397SChris Mason if (i == 0) { 5745f39d397SChris Mason if (btrfs_item_offset_nr(leaf, i) + 5755f39d397SChris Mason btrfs_item_size_nr(leaf, i) != 5765f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 5775f39d397SChris Mason btrfs_print_leaf(root, leaf); 5785f39d397SChris Mason printk("slot %d first offset bad\n", i); 5795f39d397SChris Mason BUG_ON(1); 5805f39d397SChris Mason } 5815f39d397SChris Mason } 5825f39d397SChris Mason } 5835f39d397SChris Mason if (nritems > 0) { 5845f39d397SChris Mason if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) { 5855f39d397SChris Mason btrfs_print_leaf(root, leaf); 5865f39d397SChris Mason printk("slot %d bad size \n", nritems - 1); 5875f39d397SChris Mason BUG_ON(1); 5885f39d397SChris Mason } 5895f39d397SChris Mason } 5905f39d397SChris Mason #endif 5915f39d397SChris Mason if (slot != 0 && slot < nritems - 1) { 5925f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, slot); 5935f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1); 5945f39d397SChris Mason if (comp_keys(&leaf_key, &cpukey) <= 0) { 5955f39d397SChris Mason btrfs_print_leaf(root, leaf); 5965f39d397SChris Mason printk("slot %d offset bad key\n", slot); 5975f39d397SChris Mason BUG_ON(1); 5985f39d397SChris Mason } 5995f39d397SChris Mason if (btrfs_item_offset_nr(leaf, slot - 1) != 6005f39d397SChris Mason btrfs_item_end_nr(leaf, slot)) { 6015f39d397SChris Mason btrfs_print_leaf(root, leaf); 6025f39d397SChris Mason printk("slot %d offset bad\n", slot); 6035f39d397SChris Mason BUG_ON(1); 6045f39d397SChris Mason } 605aa5d6bedSChris Mason } 6068d7be552SChris Mason if (slot < nritems - 1) { 6075f39d397SChris Mason btrfs_item_key(leaf, &leaf_key, slot); 6085f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1); 6095f39d397SChris Mason BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0); 6105f39d397SChris Mason if (btrfs_item_offset_nr(leaf, slot) != 6115f39d397SChris Mason btrfs_item_end_nr(leaf, slot + 1)) { 6125f39d397SChris Mason btrfs_print_leaf(root, leaf); 6135f39d397SChris Mason printk("slot %d offset bad\n", slot); 6145f39d397SChris Mason BUG_ON(1); 615aa5d6bedSChris Mason } 6165f39d397SChris Mason } 6175f39d397SChris Mason BUG_ON(btrfs_item_offset_nr(leaf, 0) + 6185f39d397SChris Mason btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root)); 619aa5d6bedSChris Mason return 0; 620aa5d6bedSChris Mason } 621aa5d6bedSChris Mason 62298ed5174SChris Mason static int noinline check_block(struct btrfs_root *root, 62398ed5174SChris Mason struct btrfs_path *path, int level) 624aa5d6bedSChris Mason { 625f188591eSChris Mason u64 found_start; 62685d824c4SChris Mason return 0; 627f188591eSChris Mason if (btrfs_header_level(path->nodes[level]) != level) 628f188591eSChris Mason printk("warning: bad level %Lu wanted %d found %d\n", 629f188591eSChris Mason path->nodes[level]->start, level, 630f188591eSChris Mason btrfs_header_level(path->nodes[level])); 631f188591eSChris Mason found_start = btrfs_header_bytenr(path->nodes[level]); 632f188591eSChris Mason if (found_start != path->nodes[level]->start) { 633f188591eSChris Mason printk("warning: bad bytentr %Lu found %Lu\n", 634f188591eSChris Mason path->nodes[level]->start, found_start); 635f188591eSChris Mason } 636db94535dSChris Mason #if 0 6375f39d397SChris Mason struct extent_buffer *buf = path->nodes[level]; 6385f39d397SChris Mason 639479965d6SChris Mason if (memcmp_extent_buffer(buf, root->fs_info->fsid, 640479965d6SChris Mason (unsigned long)btrfs_header_fsid(buf), 641479965d6SChris Mason BTRFS_FSID_SIZE)) { 6425f39d397SChris Mason printk("warning bad block %Lu\n", buf->start); 643db94535dSChris Mason return 1; 6445f39d397SChris Mason } 645db94535dSChris Mason #endif 646aa5d6bedSChris Mason if (level == 0) 647123abc88SChris Mason return check_leaf(root, path, level); 648123abc88SChris Mason return check_node(root, path, level); 649aa5d6bedSChris Mason } 650aa5d6bedSChris Mason 65174123bd7SChris Mason /* 6525f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 6535f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 6545f39d397SChris Mason * 65574123bd7SChris Mason * the slot in the array is returned via slot, and it points to 65674123bd7SChris Mason * the place where you would insert key if it is not found in 65774123bd7SChris Mason * the array. 65874123bd7SChris Mason * 65974123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 66074123bd7SChris Mason */ 6615f39d397SChris Mason static int generic_bin_search(struct extent_buffer *eb, unsigned long p, 6625f39d397SChris Mason int item_size, struct btrfs_key *key, 663be0e5c09SChris Mason int max, int *slot) 664be0e5c09SChris Mason { 665be0e5c09SChris Mason int low = 0; 666be0e5c09SChris Mason int high = max; 667be0e5c09SChris Mason int mid; 668be0e5c09SChris Mason int ret; 669479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 6705f39d397SChris Mason struct btrfs_disk_key unaligned; 6715f39d397SChris Mason unsigned long offset; 6725f39d397SChris Mason char *map_token = NULL; 6735f39d397SChris Mason char *kaddr = NULL; 6745f39d397SChris Mason unsigned long map_start = 0; 6755f39d397SChris Mason unsigned long map_len = 0; 676479965d6SChris Mason int err; 677be0e5c09SChris Mason 678be0e5c09SChris Mason while(low < high) { 679be0e5c09SChris Mason mid = (low + high) / 2; 6805f39d397SChris Mason offset = p + mid * item_size; 6815f39d397SChris Mason 6825f39d397SChris Mason if (!map_token || offset < map_start || 6835f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 6845f39d397SChris Mason map_start + map_len) { 685479965d6SChris Mason if (map_token) { 6865f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 687479965d6SChris Mason map_token = NULL; 688479965d6SChris Mason } 689479965d6SChris Mason err = map_extent_buffer(eb, offset, 690479965d6SChris Mason sizeof(struct btrfs_disk_key), 691479965d6SChris Mason &map_token, &kaddr, 6925f39d397SChris Mason &map_start, &map_len, KM_USER0); 6935f39d397SChris Mason 694479965d6SChris Mason if (!err) { 695479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 696479965d6SChris Mason map_start); 697479965d6SChris Mason } else { 6985f39d397SChris Mason read_extent_buffer(eb, &unaligned, 6995f39d397SChris Mason offset, sizeof(unaligned)); 7005f39d397SChris Mason tmp = &unaligned; 701479965d6SChris Mason } 702479965d6SChris Mason 7035f39d397SChris Mason } else { 7045f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 7055f39d397SChris Mason map_start); 7065f39d397SChris Mason } 707be0e5c09SChris Mason ret = comp_keys(tmp, key); 708be0e5c09SChris Mason 709be0e5c09SChris Mason if (ret < 0) 710be0e5c09SChris Mason low = mid + 1; 711be0e5c09SChris Mason else if (ret > 0) 712be0e5c09SChris Mason high = mid; 713be0e5c09SChris Mason else { 714be0e5c09SChris Mason *slot = mid; 715479965d6SChris Mason if (map_token) 7165f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 717be0e5c09SChris Mason return 0; 718be0e5c09SChris Mason } 719be0e5c09SChris Mason } 720be0e5c09SChris Mason *slot = low; 7215f39d397SChris Mason if (map_token) 7225f39d397SChris Mason unmap_extent_buffer(eb, map_token, KM_USER0); 723be0e5c09SChris Mason return 1; 724be0e5c09SChris Mason } 725be0e5c09SChris Mason 72697571fd0SChris Mason /* 72797571fd0SChris Mason * simple bin_search frontend that does the right thing for 72897571fd0SChris Mason * leaves vs nodes 72997571fd0SChris Mason */ 7305f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, 7315f39d397SChris Mason int level, int *slot) 732be0e5c09SChris Mason { 7335f39d397SChris Mason if (level == 0) { 7345f39d397SChris Mason return generic_bin_search(eb, 7355f39d397SChris Mason offsetof(struct btrfs_leaf, items), 7360783fcfcSChris Mason sizeof(struct btrfs_item), 7375f39d397SChris Mason key, btrfs_header_nritems(eb), 7387518a238SChris Mason slot); 739be0e5c09SChris Mason } else { 7405f39d397SChris Mason return generic_bin_search(eb, 7415f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 742123abc88SChris Mason sizeof(struct btrfs_key_ptr), 7435f39d397SChris Mason key, btrfs_header_nritems(eb), 7447518a238SChris Mason slot); 745be0e5c09SChris Mason } 746be0e5c09SChris Mason return -1; 747be0e5c09SChris Mason } 748be0e5c09SChris Mason 7495f39d397SChris Mason static struct extent_buffer *read_node_slot(struct btrfs_root *root, 7505f39d397SChris Mason struct extent_buffer *parent, int slot) 751bb803951SChris Mason { 752ca7a79adSChris Mason int level = btrfs_header_level(parent); 753bb803951SChris Mason if (slot < 0) 754bb803951SChris Mason return NULL; 7555f39d397SChris Mason if (slot >= btrfs_header_nritems(parent)) 756bb803951SChris Mason return NULL; 757ca7a79adSChris Mason 758ca7a79adSChris Mason BUG_ON(level == 0); 759ca7a79adSChris Mason 760db94535dSChris Mason return read_tree_block(root, btrfs_node_blockptr(parent, slot), 761ca7a79adSChris Mason btrfs_level_size(root, level - 1), 762ca7a79adSChris Mason btrfs_node_ptr_generation(parent, slot)); 763bb803951SChris Mason } 764bb803951SChris Mason 76598ed5174SChris Mason static int balance_level(struct btrfs_trans_handle *trans, 76698ed5174SChris Mason struct btrfs_root *root, 76798ed5174SChris Mason struct btrfs_path *path, int level) 768bb803951SChris Mason { 7695f39d397SChris Mason struct extent_buffer *right = NULL; 7705f39d397SChris Mason struct extent_buffer *mid; 7715f39d397SChris Mason struct extent_buffer *left = NULL; 7725f39d397SChris Mason struct extent_buffer *parent = NULL; 773bb803951SChris Mason int ret = 0; 774bb803951SChris Mason int wret; 775bb803951SChris Mason int pslot; 776bb803951SChris Mason int orig_slot = path->slots[level]; 77754aa1f4dSChris Mason int err_on_enospc = 0; 77879f95c82SChris Mason u64 orig_ptr; 779bb803951SChris Mason 780bb803951SChris Mason if (level == 0) 781bb803951SChris Mason return 0; 782bb803951SChris Mason 7835f39d397SChris Mason mid = path->nodes[level]; 784*925baeddSChris Mason WARN_ON(!path->locks[level]); 7857bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 7867bb86316SChris Mason 7871d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 78879f95c82SChris Mason 789234b63a0SChris Mason if (level < BTRFS_MAX_LEVEL - 1) 7905f39d397SChris Mason parent = path->nodes[level + 1]; 791bb803951SChris Mason pslot = path->slots[level + 1]; 792bb803951SChris Mason 79340689478SChris Mason /* 79440689478SChris Mason * deal with the case where there is only one pointer in the root 79540689478SChris Mason * by promoting the node below to a root 79640689478SChris Mason */ 7975f39d397SChris Mason if (!parent) { 7985f39d397SChris Mason struct extent_buffer *child; 799bb803951SChris Mason 8005f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 801bb803951SChris Mason return 0; 802bb803951SChris Mason 803bb803951SChris Mason /* promote the child to a root */ 8045f39d397SChris Mason child = read_node_slot(root, mid, 0); 805*925baeddSChris Mason btrfs_tree_lock(child); 806bb803951SChris Mason BUG_ON(!child); 8072f375ab9SYan ret = btrfs_cow_block(trans, root, child, mid, 0, &child); 8082f375ab9SYan BUG_ON(ret); 8092f375ab9SYan 810*925baeddSChris Mason spin_lock(&root->node_lock); 811bb803951SChris Mason root->node = child; 812*925baeddSChris Mason spin_unlock(&root->node_lock); 813*925baeddSChris Mason 8140b86a832SChris Mason add_root_to_dirty_list(root); 815*925baeddSChris Mason btrfs_tree_unlock(child); 816*925baeddSChris Mason path->locks[level] = 0; 817bb803951SChris Mason path->nodes[level] = NULL; 8185f39d397SChris Mason clean_tree_block(trans, root, mid); 819*925baeddSChris Mason btrfs_tree_unlock(mid); 820bb803951SChris Mason /* once for the path */ 8215f39d397SChris Mason free_extent_buffer(mid); 8227bb86316SChris Mason ret = btrfs_free_extent(trans, root, mid->start, mid->len, 8237bb86316SChris Mason root->root_key.objectid, 8247bb86316SChris Mason btrfs_header_generation(mid), 0, 0, 1); 825bb803951SChris Mason /* once for the root ptr */ 8265f39d397SChris Mason free_extent_buffer(mid); 827db94535dSChris Mason return ret; 828bb803951SChris Mason } 8295f39d397SChris Mason if (btrfs_header_nritems(mid) > 830123abc88SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 4) 831bb803951SChris Mason return 0; 832bb803951SChris Mason 8335f39d397SChris Mason if (btrfs_header_nritems(mid) < 2) 83454aa1f4dSChris Mason err_on_enospc = 1; 83554aa1f4dSChris Mason 8365f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 8375f39d397SChris Mason if (left) { 838*925baeddSChris Mason btrfs_tree_lock(left); 8395f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 8405f39d397SChris Mason parent, pslot - 1, &left); 84154aa1f4dSChris Mason if (wret) { 84254aa1f4dSChris Mason ret = wret; 84354aa1f4dSChris Mason goto enospc; 84454aa1f4dSChris Mason } 8452cc58cf2SChris Mason } 8465f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 8475f39d397SChris Mason if (right) { 848*925baeddSChris Mason btrfs_tree_lock(right); 8495f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 8505f39d397SChris Mason parent, pslot + 1, &right); 8512cc58cf2SChris Mason if (wret) { 8522cc58cf2SChris Mason ret = wret; 8532cc58cf2SChris Mason goto enospc; 8542cc58cf2SChris Mason } 8552cc58cf2SChris Mason } 8562cc58cf2SChris Mason 8572cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 8585f39d397SChris Mason if (left) { 8595f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 860bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 86179f95c82SChris Mason if (wret < 0) 86279f95c82SChris Mason ret = wret; 8635f39d397SChris Mason if (btrfs_header_nritems(mid) < 2) 86454aa1f4dSChris Mason err_on_enospc = 1; 865bb803951SChris Mason } 86679f95c82SChris Mason 86779f95c82SChris Mason /* 86879f95c82SChris Mason * then try to empty the right most buffer into the middle 86979f95c82SChris Mason */ 8705f39d397SChris Mason if (right) { 871971a1f66SChris Mason wret = push_node_left(trans, root, mid, right, 1); 87254aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 87379f95c82SChris Mason ret = wret; 8745f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 875db94535dSChris Mason u64 bytenr = right->start; 8767bb86316SChris Mason u64 generation = btrfs_header_generation(parent); 877db94535dSChris Mason u32 blocksize = right->len; 878db94535dSChris Mason 8795f39d397SChris Mason clean_tree_block(trans, root, right); 880*925baeddSChris Mason btrfs_tree_unlock(right); 8815f39d397SChris Mason free_extent_buffer(right); 882bb803951SChris Mason right = NULL; 883e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot + 884e089f05cSChris Mason 1); 885bb803951SChris Mason if (wret) 886bb803951SChris Mason ret = wret; 887db94535dSChris Mason wret = btrfs_free_extent(trans, root, bytenr, 8887bb86316SChris Mason blocksize, 8897bb86316SChris Mason btrfs_header_owner(parent), 8907bb86316SChris Mason generation, 0, 0, 1); 891bb803951SChris Mason if (wret) 892bb803951SChris Mason ret = wret; 893bb803951SChris Mason } else { 8945f39d397SChris Mason struct btrfs_disk_key right_key; 8955f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 8965f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 8975f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 898bb803951SChris Mason } 899bb803951SChris Mason } 9005f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 90179f95c82SChris Mason /* 90279f95c82SChris Mason * we're not allowed to leave a node with one item in the 90379f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 90479f95c82SChris Mason * could try to delete the only pointer in this node. 90579f95c82SChris Mason * So, pull some keys from the left. 90679f95c82SChris Mason * There has to be a left pointer at this point because 90779f95c82SChris Mason * otherwise we would have pulled some pointers from the 90879f95c82SChris Mason * right 90979f95c82SChris Mason */ 9105f39d397SChris Mason BUG_ON(!left); 9115f39d397SChris Mason wret = balance_node_right(trans, root, mid, left); 91254aa1f4dSChris Mason if (wret < 0) { 91379f95c82SChris Mason ret = wret; 91454aa1f4dSChris Mason goto enospc; 91554aa1f4dSChris Mason } 916bce4eae9SChris Mason if (wret == 1) { 917bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 918bce4eae9SChris Mason if (wret < 0) 919bce4eae9SChris Mason ret = wret; 920bce4eae9SChris Mason } 92179f95c82SChris Mason BUG_ON(wret == 1); 92279f95c82SChris Mason } 9235f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 92479f95c82SChris Mason /* we've managed to empty the middle node, drop it */ 9257bb86316SChris Mason u64 root_gen = btrfs_header_generation(parent); 926db94535dSChris Mason u64 bytenr = mid->start; 927db94535dSChris Mason u32 blocksize = mid->len; 928*925baeddSChris Mason 9295f39d397SChris Mason clean_tree_block(trans, root, mid); 930*925baeddSChris Mason btrfs_tree_unlock(mid); 9315f39d397SChris Mason free_extent_buffer(mid); 932bb803951SChris Mason mid = NULL; 933e089f05cSChris Mason wret = del_ptr(trans, root, path, level + 1, pslot); 934bb803951SChris Mason if (wret) 935bb803951SChris Mason ret = wret; 9367bb86316SChris Mason wret = btrfs_free_extent(trans, root, bytenr, blocksize, 9377bb86316SChris Mason btrfs_header_owner(parent), 9387bb86316SChris Mason root_gen, 0, 0, 1); 939bb803951SChris Mason if (wret) 940bb803951SChris Mason ret = wret; 94179f95c82SChris Mason } else { 94279f95c82SChris Mason /* update the parent key to reflect our changes */ 9435f39d397SChris Mason struct btrfs_disk_key mid_key; 9445f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 9455f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 9465f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 94779f95c82SChris Mason } 948bb803951SChris Mason 94979f95c82SChris Mason /* update the path */ 9505f39d397SChris Mason if (left) { 9515f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 9525f39d397SChris Mason extent_buffer_get(left); 953*925baeddSChris Mason /* left was locked after cow */ 9545f39d397SChris Mason path->nodes[level] = left; 955bb803951SChris Mason path->slots[level + 1] -= 1; 956bb803951SChris Mason path->slots[level] = orig_slot; 957*925baeddSChris Mason if (mid) { 958*925baeddSChris Mason btrfs_tree_unlock(mid); 9595f39d397SChris Mason free_extent_buffer(mid); 960*925baeddSChris Mason } 961bb803951SChris Mason } else { 9625f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 963bb803951SChris Mason path->slots[level] = orig_slot; 964bb803951SChris Mason } 965bb803951SChris Mason } 96679f95c82SChris Mason /* double check we haven't messed things up */ 967123abc88SChris Mason check_block(root, path, level); 968e20d96d6SChris Mason if (orig_ptr != 9695f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 97079f95c82SChris Mason BUG(); 97154aa1f4dSChris Mason enospc: 972*925baeddSChris Mason if (right) { 973*925baeddSChris Mason btrfs_tree_unlock(right); 9745f39d397SChris Mason free_extent_buffer(right); 975*925baeddSChris Mason } 976*925baeddSChris Mason if (left) { 977*925baeddSChris Mason if (path->nodes[level] != left) 978*925baeddSChris Mason btrfs_tree_unlock(left); 9795f39d397SChris Mason free_extent_buffer(left); 980*925baeddSChris Mason } 981bb803951SChris Mason return ret; 982bb803951SChris Mason } 983bb803951SChris Mason 984e66f709bSChris Mason /* returns zero if the push worked, non-zero otherwise */ 98598ed5174SChris Mason static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans, 986e66f709bSChris Mason struct btrfs_root *root, 987e66f709bSChris Mason struct btrfs_path *path, int level) 988e66f709bSChris Mason { 9895f39d397SChris Mason struct extent_buffer *right = NULL; 9905f39d397SChris Mason struct extent_buffer *mid; 9915f39d397SChris Mason struct extent_buffer *left = NULL; 9925f39d397SChris Mason struct extent_buffer *parent = NULL; 993e66f709bSChris Mason int ret = 0; 994e66f709bSChris Mason int wret; 995e66f709bSChris Mason int pslot; 996e66f709bSChris Mason int orig_slot = path->slots[level]; 997e66f709bSChris Mason u64 orig_ptr; 998e66f709bSChris Mason 999e66f709bSChris Mason if (level == 0) 1000e66f709bSChris Mason return 1; 1001e66f709bSChris Mason 10025f39d397SChris Mason mid = path->nodes[level]; 10037bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 1004e66f709bSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 1005e66f709bSChris Mason 1006e66f709bSChris Mason if (level < BTRFS_MAX_LEVEL - 1) 10075f39d397SChris Mason parent = path->nodes[level + 1]; 1008e66f709bSChris Mason pslot = path->slots[level + 1]; 1009e66f709bSChris Mason 10105f39d397SChris Mason if (!parent) 1011e66f709bSChris Mason return 1; 1012e66f709bSChris Mason 10135f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 1014e66f709bSChris Mason 1015e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 10165f39d397SChris Mason if (left) { 1017e66f709bSChris Mason u32 left_nr; 1018*925baeddSChris Mason 1019*925baeddSChris Mason btrfs_tree_lock(left); 10205f39d397SChris Mason left_nr = btrfs_header_nritems(left); 102133ade1f8SChris Mason if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 102233ade1f8SChris Mason wret = 1; 102333ade1f8SChris Mason } else { 10245f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 10255f39d397SChris Mason pslot - 1, &left); 102654aa1f4dSChris Mason if (ret) 102754aa1f4dSChris Mason wret = 1; 102854aa1f4dSChris Mason else { 102954aa1f4dSChris Mason wret = push_node_left(trans, root, 1030971a1f66SChris Mason left, mid, 0); 103154aa1f4dSChris Mason } 103233ade1f8SChris Mason } 1033e66f709bSChris Mason if (wret < 0) 1034e66f709bSChris Mason ret = wret; 1035e66f709bSChris Mason if (wret == 0) { 10365f39d397SChris Mason struct btrfs_disk_key disk_key; 1037e66f709bSChris Mason orig_slot += left_nr; 10385f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 10395f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 10405f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 10415f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 10425f39d397SChris Mason path->nodes[level] = left; 1043e66f709bSChris Mason path->slots[level + 1] -= 1; 1044e66f709bSChris Mason path->slots[level] = orig_slot; 1045*925baeddSChris Mason btrfs_tree_unlock(mid); 10465f39d397SChris Mason free_extent_buffer(mid); 1047e66f709bSChris Mason } else { 1048e66f709bSChris Mason orig_slot -= 10495f39d397SChris Mason btrfs_header_nritems(left); 1050e66f709bSChris Mason path->slots[level] = orig_slot; 1051*925baeddSChris Mason btrfs_tree_unlock(left); 10525f39d397SChris Mason free_extent_buffer(left); 1053e66f709bSChris Mason } 1054e66f709bSChris Mason return 0; 1055e66f709bSChris Mason } 1056*925baeddSChris Mason btrfs_tree_unlock(left); 10575f39d397SChris Mason free_extent_buffer(left); 1058e66f709bSChris Mason } 10595f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 1060e66f709bSChris Mason 1061e66f709bSChris Mason /* 1062e66f709bSChris Mason * then try to empty the right most buffer into the middle 1063e66f709bSChris Mason */ 10645f39d397SChris Mason if (right) { 106533ade1f8SChris Mason u32 right_nr; 1066*925baeddSChris Mason btrfs_tree_lock(right); 10675f39d397SChris Mason right_nr = btrfs_header_nritems(right); 106833ade1f8SChris Mason if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 106933ade1f8SChris Mason wret = 1; 107033ade1f8SChris Mason } else { 10715f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 10725f39d397SChris Mason parent, pslot + 1, 10735f39d397SChris Mason &right); 107454aa1f4dSChris Mason if (ret) 107554aa1f4dSChris Mason wret = 1; 107654aa1f4dSChris Mason else { 107733ade1f8SChris Mason wret = balance_node_right(trans, root, 10785f39d397SChris Mason right, mid); 107933ade1f8SChris Mason } 108054aa1f4dSChris Mason } 1081e66f709bSChris Mason if (wret < 0) 1082e66f709bSChris Mason ret = wret; 1083e66f709bSChris Mason if (wret == 0) { 10845f39d397SChris Mason struct btrfs_disk_key disk_key; 10855f39d397SChris Mason 10865f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 10875f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 10885f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 10895f39d397SChris Mason 10905f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 10915f39d397SChris Mason path->nodes[level] = right; 1092e66f709bSChris Mason path->slots[level + 1] += 1; 1093e66f709bSChris Mason path->slots[level] = orig_slot - 10945f39d397SChris Mason btrfs_header_nritems(mid); 1095*925baeddSChris Mason btrfs_tree_unlock(mid); 10965f39d397SChris Mason free_extent_buffer(mid); 1097e66f709bSChris Mason } else { 1098*925baeddSChris Mason btrfs_tree_unlock(right); 10995f39d397SChris Mason free_extent_buffer(right); 1100e66f709bSChris Mason } 1101e66f709bSChris Mason return 0; 1102e66f709bSChris Mason } 1103*925baeddSChris Mason btrfs_tree_unlock(right); 11045f39d397SChris Mason free_extent_buffer(right); 1105e66f709bSChris Mason } 1106e66f709bSChris Mason return 1; 1107e66f709bSChris Mason } 1108e66f709bSChris Mason 110974123bd7SChris Mason /* 11103c69faecSChris Mason * readahead one full node of leaves 11113c69faecSChris Mason */ 11123c69faecSChris Mason static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, 111301f46658SChris Mason int level, int slot, u64 objectid) 11143c69faecSChris Mason { 11155f39d397SChris Mason struct extent_buffer *node; 111601f46658SChris Mason struct btrfs_disk_key disk_key; 11173c69faecSChris Mason u32 nritems; 11183c69faecSChris Mason u64 search; 11196b80053dSChris Mason u64 lowest_read; 11206b80053dSChris Mason u64 highest_read; 11216b80053dSChris Mason u64 nread = 0; 11223c69faecSChris Mason int direction = path->reada; 11235f39d397SChris Mason struct extent_buffer *eb; 11246b80053dSChris Mason u32 nr; 11256b80053dSChris Mason u32 blocksize; 11266b80053dSChris Mason u32 nscan = 0; 1127db94535dSChris Mason 1128a6b6e75eSChris Mason if (level != 1) 11293c69faecSChris Mason return; 11303c69faecSChris Mason 11316702ed49SChris Mason if (!path->nodes[level]) 11326702ed49SChris Mason return; 11336702ed49SChris Mason 11345f39d397SChris Mason node = path->nodes[level]; 1135*925baeddSChris Mason WARN_ON(!path->skip_locking && !btrfs_tree_locked(node)); 1136*925baeddSChris Mason 11373c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 11386b80053dSChris Mason blocksize = btrfs_level_size(root, level - 1); 11396b80053dSChris Mason eb = btrfs_find_tree_block(root, search, blocksize); 11405f39d397SChris Mason if (eb) { 11415f39d397SChris Mason free_extent_buffer(eb); 11423c69faecSChris Mason return; 11433c69faecSChris Mason } 11443c69faecSChris Mason 11456b80053dSChris Mason highest_read = search; 11466b80053dSChris Mason lowest_read = search; 11476b80053dSChris Mason 11485f39d397SChris Mason nritems = btrfs_header_nritems(node); 11496b80053dSChris Mason nr = slot; 11503c69faecSChris Mason while(1) { 11516b80053dSChris Mason if (direction < 0) { 11526b80053dSChris Mason if (nr == 0) 11533c69faecSChris Mason break; 11546b80053dSChris Mason nr--; 11556b80053dSChris Mason } else if (direction > 0) { 11566b80053dSChris Mason nr++; 11576b80053dSChris Mason if (nr >= nritems) 11586b80053dSChris Mason break; 11593c69faecSChris Mason } 116001f46658SChris Mason if (path->reada < 0 && objectid) { 116101f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 116201f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 116301f46658SChris Mason break; 116401f46658SChris Mason } 11656b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 11666b80053dSChris Mason if ((search >= lowest_read && search <= highest_read) || 11676b80053dSChris Mason (search < lowest_read && lowest_read - search <= 32768) || 11686b80053dSChris Mason (search > highest_read && search - highest_read <= 32768)) { 1169ca7a79adSChris Mason readahead_tree_block(root, search, blocksize, 1170ca7a79adSChris Mason btrfs_node_ptr_generation(node, nr)); 11716b80053dSChris Mason nread += blocksize; 11723c69faecSChris Mason } 11736b80053dSChris Mason nscan++; 11746b80053dSChris Mason if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32)) 11756b80053dSChris Mason break; 11766b80053dSChris Mason if(nread > (1024 * 1024) || nscan > 128) 11776b80053dSChris Mason break; 11786b80053dSChris Mason 11796b80053dSChris Mason if (search < lowest_read) 11806b80053dSChris Mason lowest_read = search; 11816b80053dSChris Mason if (search > highest_read) 11826b80053dSChris Mason highest_read = search; 11833c69faecSChris Mason } 11843c69faecSChris Mason } 1185*925baeddSChris Mason 1186*925baeddSChris Mason static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock) 1187*925baeddSChris Mason { 1188*925baeddSChris Mason int i; 1189*925baeddSChris Mason int skip_level = level; 1190*925baeddSChris Mason struct extent_buffer *t; 1191*925baeddSChris Mason 1192*925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1193*925baeddSChris Mason if (!path->nodes[i]) 1194*925baeddSChris Mason break; 1195*925baeddSChris Mason if (!path->locks[i]) 1196*925baeddSChris Mason break; 1197*925baeddSChris Mason if (path->slots[i] == 0) { 1198*925baeddSChris Mason skip_level = i + 1; 1199*925baeddSChris Mason continue; 1200*925baeddSChris Mason } 1201*925baeddSChris Mason if (path->keep_locks) { 1202*925baeddSChris Mason u32 nritems; 1203*925baeddSChris Mason t = path->nodes[i]; 1204*925baeddSChris Mason nritems = btrfs_header_nritems(t); 1205*925baeddSChris Mason if (path->slots[i] >= nritems - 1) { 1206*925baeddSChris Mason skip_level = i + 1; 1207*925baeddSChris Mason continue; 1208*925baeddSChris Mason } 1209*925baeddSChris Mason } 1210*925baeddSChris Mason t = path->nodes[i]; 1211*925baeddSChris Mason if (i >= lowest_unlock && i > skip_level && path->locks[i]) { 1212*925baeddSChris Mason btrfs_tree_unlock(t); 1213*925baeddSChris Mason path->locks[i] = 0; 1214*925baeddSChris Mason } 1215*925baeddSChris Mason } 1216*925baeddSChris Mason } 1217*925baeddSChris Mason 12183c69faecSChris Mason /* 121974123bd7SChris Mason * look for key in the tree. path is filled in with nodes along the way 122074123bd7SChris Mason * if key is found, we return zero and you can find the item in the leaf 122174123bd7SChris Mason * level of the path (level 0) 122274123bd7SChris Mason * 122374123bd7SChris Mason * If the key isn't found, the path points to the slot where it should 1224aa5d6bedSChris Mason * be inserted, and 1 is returned. If there are other errors during the 1225aa5d6bedSChris Mason * search a negative error number is returned. 122697571fd0SChris Mason * 122797571fd0SChris Mason * if ins_len > 0, nodes and leaves will be split as we walk down the 122897571fd0SChris Mason * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if 122997571fd0SChris Mason * possible) 123074123bd7SChris Mason */ 1231e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root 1232e089f05cSChris Mason *root, struct btrfs_key *key, struct btrfs_path *p, int 1233e089f05cSChris Mason ins_len, int cow) 1234be0e5c09SChris Mason { 12355f39d397SChris Mason struct extent_buffer *b; 1236be0e5c09SChris Mason int slot; 1237be0e5c09SChris Mason int ret; 1238be0e5c09SChris Mason int level; 12393c69faecSChris Mason int should_reada = p->reada; 1240*925baeddSChris Mason int lowest_unlock = 1; 12419f3a7427SChris Mason u8 lowest_level = 0; 12429f3a7427SChris Mason 12436702ed49SChris Mason lowest_level = p->lowest_level; 12446702ed49SChris Mason WARN_ON(lowest_level && ins_len); 124522b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 1246*925baeddSChris Mason // WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex)); 1247*925baeddSChris Mason WARN_ON(root == root->fs_info->extent_root && 1248*925baeddSChris Mason !mutex_is_locked(&root->fs_info->alloc_mutex)); 1249*925baeddSChris Mason WARN_ON(root == root->fs_info->chunk_root && 1250*925baeddSChris Mason !mutex_is_locked(&root->fs_info->chunk_mutex)); 1251*925baeddSChris Mason WARN_ON(root == root->fs_info->dev_root && 1252*925baeddSChris Mason !mutex_is_locked(&root->fs_info->chunk_mutex)); 1253*925baeddSChris Mason if (ins_len < 0) 1254*925baeddSChris Mason lowest_unlock = 2; 1255bb803951SChris Mason again: 1256*925baeddSChris Mason if (!p->skip_locking) 1257*925baeddSChris Mason b = btrfs_lock_root_node(root); 1258*925baeddSChris Mason else 1259*925baeddSChris Mason b = btrfs_root_node(root); 1260*925baeddSChris Mason 1261eb60ceacSChris Mason while (b) { 12625f39d397SChris Mason level = btrfs_header_level(b); 126302217ed2SChris Mason if (cow) { 126402217ed2SChris Mason int wret; 1265e20d96d6SChris Mason wret = btrfs_cow_block(trans, root, b, 1266e20d96d6SChris Mason p->nodes[level + 1], 1267e20d96d6SChris Mason p->slots[level + 1], 1268252c38f0SYan &b); 126954aa1f4dSChris Mason if (wret) { 12705f39d397SChris Mason free_extent_buffer(b); 127154aa1f4dSChris Mason return wret; 127254aa1f4dSChris Mason } 127302217ed2SChris Mason } 127402217ed2SChris Mason BUG_ON(!cow && ins_len); 12755f39d397SChris Mason if (level != btrfs_header_level(b)) 12762c90e5d6SChris Mason WARN_ON(1); 12775f39d397SChris Mason level = btrfs_header_level(b); 1278eb60ceacSChris Mason p->nodes[level] = b; 1279*925baeddSChris Mason if (!p->skip_locking) 1280*925baeddSChris Mason p->locks[level] = 1; 1281123abc88SChris Mason ret = check_block(root, p, level); 1282aa5d6bedSChris Mason if (ret) 1283aa5d6bedSChris Mason return -1; 1284*925baeddSChris Mason 12855f39d397SChris Mason ret = bin_search(b, key, level, &slot); 12865f39d397SChris Mason if (level != 0) { 1287be0e5c09SChris Mason if (ret && slot > 0) 1288be0e5c09SChris Mason slot -= 1; 1289be0e5c09SChris Mason p->slots[level] = slot; 12905f39d397SChris Mason if (ins_len > 0 && btrfs_header_nritems(b) >= 12911514794eSChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 1292e089f05cSChris Mason int sret = split_node(trans, root, p, level); 12935c680ed6SChris Mason BUG_ON(sret > 0); 12945c680ed6SChris Mason if (sret) 12955c680ed6SChris Mason return sret; 12965c680ed6SChris Mason b = p->nodes[level]; 12975c680ed6SChris Mason slot = p->slots[level]; 1298bb803951SChris Mason } else if (ins_len < 0) { 1299e089f05cSChris Mason int sret = balance_level(trans, root, p, 1300e089f05cSChris Mason level); 1301bb803951SChris Mason if (sret) 1302bb803951SChris Mason return sret; 1303bb803951SChris Mason b = p->nodes[level]; 1304f510cfecSChris Mason if (!b) { 1305f510cfecSChris Mason btrfs_release_path(NULL, p); 1306bb803951SChris Mason goto again; 1307f510cfecSChris Mason } 1308bb803951SChris Mason slot = p->slots[level]; 13095f39d397SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 13105c680ed6SChris Mason } 13119f3a7427SChris Mason /* this is only true while dropping a snapshot */ 1312*925baeddSChris Mason if (level == lowest_level) { 1313*925baeddSChris Mason unlock_up(p, level, lowest_unlock); 13149f3a7427SChris Mason break; 1315*925baeddSChris Mason } 1316ca7a79adSChris Mason 13176702ed49SChris Mason if (should_reada) 131801f46658SChris Mason reada_for_search(root, p, level, slot, 131901f46658SChris Mason key->objectid); 1320ca7a79adSChris Mason 1321ca7a79adSChris Mason b = read_node_slot(root, b, slot); 1322*925baeddSChris Mason if (!p->skip_locking) 1323*925baeddSChris Mason btrfs_tree_lock(b); 1324*925baeddSChris Mason unlock_up(p, level, lowest_unlock); 1325be0e5c09SChris Mason } else { 1326be0e5c09SChris Mason p->slots[level] = slot; 13275f39d397SChris Mason if (ins_len > 0 && btrfs_leaf_free_space(root, b) < 13280783fcfcSChris Mason sizeof(struct btrfs_item) + ins_len) { 1329d4dbff95SChris Mason int sret = split_leaf(trans, root, key, 1330cc0c5538SChris Mason p, ins_len, ret == 0); 13315c680ed6SChris Mason BUG_ON(sret > 0); 13325c680ed6SChris Mason if (sret) 13335c680ed6SChris Mason return sret; 13345c680ed6SChris Mason } 1335*925baeddSChris Mason unlock_up(p, level, lowest_unlock); 1336be0e5c09SChris Mason return ret; 1337be0e5c09SChris Mason } 1338be0e5c09SChris Mason } 1339aa5d6bedSChris Mason return 1; 1340be0e5c09SChris Mason } 1341be0e5c09SChris Mason 134274123bd7SChris Mason /* 134374123bd7SChris Mason * adjust the pointers going up the tree, starting at level 134474123bd7SChris Mason * making sure the right key of each node is points to 'key'. 134574123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 134674123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 134774123bd7SChris Mason * higher levels 1348aa5d6bedSChris Mason * 1349aa5d6bedSChris Mason * If this fails to write a tree block, it returns -1, but continues 1350aa5d6bedSChris Mason * fixing up the blocks in ram so the tree is consistent. 135174123bd7SChris Mason */ 13525f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans, 13535f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 13545f39d397SChris Mason struct btrfs_disk_key *key, int level) 1355be0e5c09SChris Mason { 1356be0e5c09SChris Mason int i; 1357aa5d6bedSChris Mason int ret = 0; 13585f39d397SChris Mason struct extent_buffer *t; 13595f39d397SChris Mason 1360234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1361be0e5c09SChris Mason int tslot = path->slots[i]; 1362eb60ceacSChris Mason if (!path->nodes[i]) 1363be0e5c09SChris Mason break; 13645f39d397SChris Mason t = path->nodes[i]; 13655f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 1366*925baeddSChris Mason if (!btrfs_tree_locked(path->nodes[i])) { 1367*925baeddSChris Mason int ii; 1368*925baeddSChris Mason printk("fixup without lock on level %d\n", btrfs_header_level(path->nodes[i])); 1369*925baeddSChris Mason for (ii = 0; ii < BTRFS_MAX_LEVEL; ii++) { 1370*925baeddSChris Mason printk("level %d slot %d\n", ii, path->slots[ii]); 1371*925baeddSChris Mason } 1372*925baeddSChris Mason } 1373d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 1374be0e5c09SChris Mason if (tslot != 0) 1375be0e5c09SChris Mason break; 1376be0e5c09SChris Mason } 1377aa5d6bedSChris Mason return ret; 1378be0e5c09SChris Mason } 1379be0e5c09SChris Mason 138074123bd7SChris Mason /* 138174123bd7SChris Mason * try to push data from one node into the next node left in the 138279f95c82SChris Mason * tree. 1383aa5d6bedSChris Mason * 1384aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 1385aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 138674123bd7SChris Mason */ 138798ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 138898ed5174SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 1389971a1f66SChris Mason struct extent_buffer *src, int empty) 1390be0e5c09SChris Mason { 1391be0e5c09SChris Mason int push_items = 0; 1392bb803951SChris Mason int src_nritems; 1393bb803951SChris Mason int dst_nritems; 1394aa5d6bedSChris Mason int ret = 0; 1395be0e5c09SChris Mason 13965f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 13975f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1398123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 13997bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 14007bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 140154aa1f4dSChris Mason 1402bce4eae9SChris Mason if (!empty && src_nritems <= 8) 1403971a1f66SChris Mason return 1; 1404971a1f66SChris Mason 1405eb60ceacSChris Mason if (push_items <= 0) { 1406be0e5c09SChris Mason return 1; 1407eb60ceacSChris Mason } 1408be0e5c09SChris Mason 1409bce4eae9SChris Mason if (empty) { 1410971a1f66SChris Mason push_items = min(src_nritems, push_items); 1411bce4eae9SChris Mason if (push_items < src_nritems) { 1412bce4eae9SChris Mason /* leave at least 8 pointers in the node if 1413bce4eae9SChris Mason * we aren't going to empty it 1414bce4eae9SChris Mason */ 1415bce4eae9SChris Mason if (src_nritems - push_items < 8) { 1416bce4eae9SChris Mason if (push_items <= 8) 1417bce4eae9SChris Mason return 1; 1418bce4eae9SChris Mason push_items -= 8; 1419bce4eae9SChris Mason } 1420bce4eae9SChris Mason } 1421bce4eae9SChris Mason } else 1422bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 142379f95c82SChris Mason 14245f39d397SChris Mason copy_extent_buffer(dst, src, 14255f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 14265f39d397SChris Mason btrfs_node_key_ptr_offset(0), 1427123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 14285f39d397SChris Mason 1429bb803951SChris Mason if (push_items < src_nritems) { 14305f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 14315f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 1432e2fa7227SChris Mason (src_nritems - push_items) * 1433123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 1434bb803951SChris Mason } 14355f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 14365f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 14375f39d397SChris Mason btrfs_mark_buffer_dirty(src); 14385f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 1439bb803951SChris Mason return ret; 1440be0e5c09SChris Mason } 1441be0e5c09SChris Mason 144297571fd0SChris Mason /* 144379f95c82SChris Mason * try to push data from one node into the next node right in the 144479f95c82SChris Mason * tree. 144579f95c82SChris Mason * 144679f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 144779f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 144879f95c82SChris Mason * 144979f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 145079f95c82SChris Mason */ 14515f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 14525f39d397SChris Mason struct btrfs_root *root, 14535f39d397SChris Mason struct extent_buffer *dst, 14545f39d397SChris Mason struct extent_buffer *src) 145579f95c82SChris Mason { 145679f95c82SChris Mason int push_items = 0; 145779f95c82SChris Mason int max_push; 145879f95c82SChris Mason int src_nritems; 145979f95c82SChris Mason int dst_nritems; 146079f95c82SChris Mason int ret = 0; 146179f95c82SChris Mason 14627bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 14637bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 14647bb86316SChris Mason 14655f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 14665f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 1467123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 1468bce4eae9SChris Mason if (push_items <= 0) { 146979f95c82SChris Mason return 1; 1470bce4eae9SChris Mason } 1471bce4eae9SChris Mason 1472bce4eae9SChris Mason if (src_nritems < 4) { 1473bce4eae9SChris Mason return 1; 1474bce4eae9SChris Mason } 147579f95c82SChris Mason 147679f95c82SChris Mason max_push = src_nritems / 2 + 1; 147779f95c82SChris Mason /* don't try to empty the node */ 1478bce4eae9SChris Mason if (max_push >= src_nritems) { 147979f95c82SChris Mason return 1; 1480bce4eae9SChris Mason } 1481252c38f0SYan 148279f95c82SChris Mason if (max_push < push_items) 148379f95c82SChris Mason push_items = max_push; 148479f95c82SChris Mason 14855f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 14865f39d397SChris Mason btrfs_node_key_ptr_offset(0), 14875f39d397SChris Mason (dst_nritems) * 14885f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 1489d6025579SChris Mason 14905f39d397SChris Mason copy_extent_buffer(dst, src, 14915f39d397SChris Mason btrfs_node_key_ptr_offset(0), 14925f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 1493123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 149479f95c82SChris Mason 14955f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 14965f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 149779f95c82SChris Mason 14985f39d397SChris Mason btrfs_mark_buffer_dirty(src); 14995f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 150079f95c82SChris Mason return ret; 150179f95c82SChris Mason } 150279f95c82SChris Mason 150379f95c82SChris Mason /* 150497571fd0SChris Mason * helper function to insert a new root level in the tree. 150597571fd0SChris Mason * A new node is allocated, and a single item is inserted to 150697571fd0SChris Mason * point to the existing root 1507aa5d6bedSChris Mason * 1508aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 150997571fd0SChris Mason */ 151098ed5174SChris Mason static int noinline insert_new_root(struct btrfs_trans_handle *trans, 15115f39d397SChris Mason struct btrfs_root *root, 15125f39d397SChris Mason struct btrfs_path *path, int level) 151374123bd7SChris Mason { 15147bb86316SChris Mason u64 root_gen; 15157bb86316SChris Mason u64 lower_gen; 15165f39d397SChris Mason struct extent_buffer *lower; 15175f39d397SChris Mason struct extent_buffer *c; 1518*925baeddSChris Mason struct extent_buffer *old; 15195f39d397SChris Mason struct btrfs_disk_key lower_key; 15205c680ed6SChris Mason 15215c680ed6SChris Mason BUG_ON(path->nodes[level]); 15225c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 15235c680ed6SChris Mason 15247bb86316SChris Mason if (root->ref_cows) 15257bb86316SChris Mason root_gen = trans->transid; 15267bb86316SChris Mason else 15277bb86316SChris Mason root_gen = 0; 15287bb86316SChris Mason 15297bb86316SChris Mason lower = path->nodes[level-1]; 15307bb86316SChris Mason if (level == 1) 15317bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 15327bb86316SChris Mason else 15337bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 15347bb86316SChris Mason 1535*925baeddSChris Mason c = btrfs_alloc_free_block(trans, root, root->nodesize, 15367bb86316SChris Mason root->root_key.objectid, 15377bb86316SChris Mason root_gen, lower_key.objectid, level, 1538db94535dSChris Mason root->node->start, 0); 15395f39d397SChris Mason if (IS_ERR(c)) 15405f39d397SChris Mason return PTR_ERR(c); 1541*925baeddSChris Mason 15425f39d397SChris Mason memset_extent_buffer(c, 0, 0, root->nodesize); 15435f39d397SChris Mason btrfs_set_header_nritems(c, 1); 15445f39d397SChris Mason btrfs_set_header_level(c, level); 1545db94535dSChris Mason btrfs_set_header_bytenr(c, c->start); 15465f39d397SChris Mason btrfs_set_header_generation(c, trans->transid); 15475f39d397SChris Mason btrfs_set_header_owner(c, root->root_key.objectid); 1548d5719762SChris Mason 15495f39d397SChris Mason write_extent_buffer(c, root->fs_info->fsid, 15505f39d397SChris Mason (unsigned long)btrfs_header_fsid(c), 15515f39d397SChris Mason BTRFS_FSID_SIZE); 1552e17cade2SChris Mason 1553e17cade2SChris Mason write_extent_buffer(c, root->fs_info->chunk_tree_uuid, 1554e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(c), 1555e17cade2SChris Mason BTRFS_UUID_SIZE); 1556e17cade2SChris Mason 15575f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 1558db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 15597bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 15607bb86316SChris Mason WARN_ON(lower_gen == 0); 15617bb86316SChris Mason 15627bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 15635f39d397SChris Mason 15645f39d397SChris Mason btrfs_mark_buffer_dirty(c); 1565d5719762SChris Mason 1566*925baeddSChris Mason spin_lock(&root->node_lock); 1567*925baeddSChris Mason old = root->node; 15685f39d397SChris Mason root->node = c; 1569*925baeddSChris Mason spin_unlock(&root->node_lock); 1570*925baeddSChris Mason 1571*925baeddSChris Mason /* the super has an extra ref to root->node */ 1572*925baeddSChris Mason free_extent_buffer(old); 1573*925baeddSChris Mason 15740b86a832SChris Mason add_root_to_dirty_list(root); 15755f39d397SChris Mason extent_buffer_get(c); 15765f39d397SChris Mason path->nodes[level] = c; 1577*925baeddSChris Mason path->locks[level] = 1; 157874123bd7SChris Mason path->slots[level] = 0; 15797bb86316SChris Mason 15807bb86316SChris Mason if (root->ref_cows && lower_gen != trans->transid) { 15817bb86316SChris Mason struct btrfs_path *back_path = btrfs_alloc_path(); 15827bb86316SChris Mason int ret; 1583*925baeddSChris Mason mutex_lock(&root->fs_info->alloc_mutex); 15847bb86316SChris Mason ret = btrfs_insert_extent_backref(trans, 15857bb86316SChris Mason root->fs_info->extent_root, 15867bb86316SChris Mason path, lower->start, 15877bb86316SChris Mason root->root_key.objectid, 15887bb86316SChris Mason trans->transid, 0, 0); 15897bb86316SChris Mason BUG_ON(ret); 1590*925baeddSChris Mason mutex_unlock(&root->fs_info->alloc_mutex); 15917bb86316SChris Mason btrfs_free_path(back_path); 15927bb86316SChris Mason } 159374123bd7SChris Mason return 0; 159474123bd7SChris Mason } 15955c680ed6SChris Mason 15965c680ed6SChris Mason /* 15975c680ed6SChris Mason * worker function to insert a single pointer in a node. 15985c680ed6SChris Mason * the node should have enough room for the pointer already 159997571fd0SChris Mason * 16005c680ed6SChris Mason * slot and level indicate where you want the key to go, and 16015c680ed6SChris Mason * blocknr is the block the key points to. 1602aa5d6bedSChris Mason * 1603aa5d6bedSChris Mason * returns zero on success and < 0 on any error 16045c680ed6SChris Mason */ 1605e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root 1606e089f05cSChris Mason *root, struct btrfs_path *path, struct btrfs_disk_key 1607db94535dSChris Mason *key, u64 bytenr, int slot, int level) 16085c680ed6SChris Mason { 16095f39d397SChris Mason struct extent_buffer *lower; 16105c680ed6SChris Mason int nritems; 16115c680ed6SChris Mason 16125c680ed6SChris Mason BUG_ON(!path->nodes[level]); 16135f39d397SChris Mason lower = path->nodes[level]; 16145f39d397SChris Mason nritems = btrfs_header_nritems(lower); 161574123bd7SChris Mason if (slot > nritems) 161674123bd7SChris Mason BUG(); 1617123abc88SChris Mason if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) 161874123bd7SChris Mason BUG(); 161974123bd7SChris Mason if (slot != nritems) { 16205f39d397SChris Mason memmove_extent_buffer(lower, 16215f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 16225f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 1623123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 162474123bd7SChris Mason } 16255f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 1626db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 162774493f7aSChris Mason WARN_ON(trans->transid == 0); 162874493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 16295f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 16305f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 163174123bd7SChris Mason return 0; 163274123bd7SChris Mason } 163374123bd7SChris Mason 163497571fd0SChris Mason /* 163597571fd0SChris Mason * split the node at the specified level in path in two. 163697571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 163797571fd0SChris Mason * 163897571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 163997571fd0SChris Mason * left and right, if either one works, it returns right away. 1640aa5d6bedSChris Mason * 1641aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 164297571fd0SChris Mason */ 1643e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 1644e089f05cSChris Mason *root, struct btrfs_path *path, int level) 1645be0e5c09SChris Mason { 16467bb86316SChris Mason u64 root_gen; 16475f39d397SChris Mason struct extent_buffer *c; 16485f39d397SChris Mason struct extent_buffer *split; 16495f39d397SChris Mason struct btrfs_disk_key disk_key; 1650be0e5c09SChris Mason int mid; 16515c680ed6SChris Mason int ret; 1652aa5d6bedSChris Mason int wret; 16537518a238SChris Mason u32 c_nritems; 1654be0e5c09SChris Mason 16555f39d397SChris Mason c = path->nodes[level]; 16567bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 16575f39d397SChris Mason if (c == root->node) { 16585c680ed6SChris Mason /* trying to split the root, lets make a new one */ 1659e089f05cSChris Mason ret = insert_new_root(trans, root, path, level + 1); 16605c680ed6SChris Mason if (ret) 16615c680ed6SChris Mason return ret; 1662e66f709bSChris Mason } else { 1663e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 16645f39d397SChris Mason c = path->nodes[level]; 16655f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 1666c448acf0SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) 1667e66f709bSChris Mason return 0; 166854aa1f4dSChris Mason if (ret < 0) 166954aa1f4dSChris Mason return ret; 16705c680ed6SChris Mason } 1671e66f709bSChris Mason 16725f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 16737bb86316SChris Mason if (root->ref_cows) 16747bb86316SChris Mason root_gen = trans->transid; 16757bb86316SChris Mason else 16767bb86316SChris Mason root_gen = 0; 16777bb86316SChris Mason 16787bb86316SChris Mason btrfs_node_key(c, &disk_key, 0); 1679*925baeddSChris Mason split = btrfs_alloc_free_block(trans, root, root->nodesize, 16807bb86316SChris Mason root->root_key.objectid, 16817bb86316SChris Mason root_gen, 16827bb86316SChris Mason btrfs_disk_key_objectid(&disk_key), 16837bb86316SChris Mason level, c->start, 0); 16845f39d397SChris Mason if (IS_ERR(split)) 16855f39d397SChris Mason return PTR_ERR(split); 168654aa1f4dSChris Mason 16875f39d397SChris Mason btrfs_set_header_flags(split, btrfs_header_flags(c)); 16885f39d397SChris Mason btrfs_set_header_level(split, btrfs_header_level(c)); 1689db94535dSChris Mason btrfs_set_header_bytenr(split, split->start); 16905f39d397SChris Mason btrfs_set_header_generation(split, trans->transid); 16915f39d397SChris Mason btrfs_set_header_owner(split, root->root_key.objectid); 169263b10fc4SChris Mason btrfs_set_header_flags(split, 0); 16935f39d397SChris Mason write_extent_buffer(split, root->fs_info->fsid, 16945f39d397SChris Mason (unsigned long)btrfs_header_fsid(split), 16955f39d397SChris Mason BTRFS_FSID_SIZE); 1696e17cade2SChris Mason write_extent_buffer(split, root->fs_info->chunk_tree_uuid, 1697e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(split), 1698e17cade2SChris Mason BTRFS_UUID_SIZE); 16995f39d397SChris Mason 17007518a238SChris Mason mid = (c_nritems + 1) / 2; 17015f39d397SChris Mason 17025f39d397SChris Mason copy_extent_buffer(split, c, 17035f39d397SChris Mason btrfs_node_key_ptr_offset(0), 17045f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 1705123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 17065f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 17075f39d397SChris Mason btrfs_set_header_nritems(c, mid); 1708aa5d6bedSChris Mason ret = 0; 1709aa5d6bedSChris Mason 17105f39d397SChris Mason btrfs_mark_buffer_dirty(c); 17115f39d397SChris Mason btrfs_mark_buffer_dirty(split); 17125f39d397SChris Mason 17135f39d397SChris Mason btrfs_node_key(split, &disk_key, 0); 1714db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, split->start, 17155f39d397SChris Mason path->slots[level + 1] + 1, 1716123abc88SChris Mason level + 1); 1717aa5d6bedSChris Mason if (wret) 1718aa5d6bedSChris Mason ret = wret; 1719aa5d6bedSChris Mason 17205de08d7dSChris Mason if (path->slots[level] >= mid) { 17215c680ed6SChris Mason path->slots[level] -= mid; 1722*925baeddSChris Mason btrfs_tree_unlock(c); 17235f39d397SChris Mason free_extent_buffer(c); 17245f39d397SChris Mason path->nodes[level] = split; 17255c680ed6SChris Mason path->slots[level + 1] += 1; 1726eb60ceacSChris Mason } else { 1727*925baeddSChris Mason btrfs_tree_unlock(split); 17285f39d397SChris Mason free_extent_buffer(split); 1729be0e5c09SChris Mason } 1730aa5d6bedSChris Mason return ret; 1731be0e5c09SChris Mason } 1732be0e5c09SChris Mason 173374123bd7SChris Mason /* 173474123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 173574123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 173674123bd7SChris Mason * space used both by the item structs and the item data 173774123bd7SChris Mason */ 17385f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 1739be0e5c09SChris Mason { 1740be0e5c09SChris Mason int data_len; 17415f39d397SChris Mason int nritems = btrfs_header_nritems(l); 1742d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 1743be0e5c09SChris Mason 1744be0e5c09SChris Mason if (!nr) 1745be0e5c09SChris Mason return 0; 17465f39d397SChris Mason data_len = btrfs_item_end_nr(l, start); 17475f39d397SChris Mason data_len = data_len - btrfs_item_offset_nr(l, end); 17480783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 1749d4dbff95SChris Mason WARN_ON(data_len < 0); 1750be0e5c09SChris Mason return data_len; 1751be0e5c09SChris Mason } 1752be0e5c09SChris Mason 175374123bd7SChris Mason /* 1754d4dbff95SChris Mason * The space between the end of the leaf items and 1755d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 1756d4dbff95SChris Mason * the leaf has left for both items and data 1757d4dbff95SChris Mason */ 17585f39d397SChris Mason int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf) 1759d4dbff95SChris Mason { 17605f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 17615f39d397SChris Mason int ret; 17625f39d397SChris Mason ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); 17635f39d397SChris Mason if (ret < 0) { 17645f39d397SChris Mason printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n", 1765ae2f5411SJens Axboe ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), 17665f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 17675f39d397SChris Mason } 17685f39d397SChris Mason return ret; 1769d4dbff95SChris Mason } 1770d4dbff95SChris Mason 1771d4dbff95SChris Mason /* 177200ec4c51SChris Mason * push some data in the path leaf to the right, trying to free up at 177300ec4c51SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 1774aa5d6bedSChris Mason * 1775aa5d6bedSChris Mason * returns 1 if the push failed because the other node didn't have enough 1776aa5d6bedSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 177700ec4c51SChris Mason */ 1778e089f05cSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 177934a38218SChris Mason *root, struct btrfs_path *path, int data_size, 178034a38218SChris Mason int empty) 178100ec4c51SChris Mason { 17825f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 17835f39d397SChris Mason struct extent_buffer *right; 17845f39d397SChris Mason struct extent_buffer *upper; 17855f39d397SChris Mason struct btrfs_disk_key disk_key; 178600ec4c51SChris Mason int slot; 178734a38218SChris Mason u32 i; 178800ec4c51SChris Mason int free_space; 178900ec4c51SChris Mason int push_space = 0; 179000ec4c51SChris Mason int push_items = 0; 17910783fcfcSChris Mason struct btrfs_item *item; 17927518a238SChris Mason u32 left_nritems; 179334a38218SChris Mason u32 nr; 17947518a238SChris Mason u32 right_nritems; 17955f39d397SChris Mason u32 data_end; 1796db94535dSChris Mason u32 this_item_size; 179754aa1f4dSChris Mason int ret; 179800ec4c51SChris Mason 179900ec4c51SChris Mason slot = path->slots[1]; 180000ec4c51SChris Mason if (!path->nodes[1]) { 180100ec4c51SChris Mason return 1; 180200ec4c51SChris Mason } 180300ec4c51SChris Mason upper = path->nodes[1]; 18045f39d397SChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 180500ec4c51SChris Mason return 1; 18065f39d397SChris Mason 1807ca7a79adSChris Mason right = read_node_slot(root, upper, slot + 1); 1808*925baeddSChris Mason btrfs_tree_lock(right); 1809123abc88SChris Mason free_space = btrfs_leaf_free_space(root, right); 1810*925baeddSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) 1811*925baeddSChris Mason goto out_unlock; 181202217ed2SChris Mason 18135f39d397SChris Mason /* cow and double check */ 18145f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, upper, 18155f39d397SChris Mason slot + 1, &right); 1816*925baeddSChris Mason if (ret) 1817*925baeddSChris Mason goto out_unlock; 1818*925baeddSChris Mason 18195f39d397SChris Mason free_space = btrfs_leaf_free_space(root, right); 1820*925baeddSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) 1821*925baeddSChris Mason goto out_unlock; 18225f39d397SChris Mason 18235f39d397SChris Mason left_nritems = btrfs_header_nritems(left); 1824*925baeddSChris Mason if (left_nritems == 0) 1825*925baeddSChris Mason goto out_unlock; 18265f39d397SChris Mason 182734a38218SChris Mason if (empty) 182834a38218SChris Mason nr = 0; 182934a38218SChris Mason else 183034a38218SChris Mason nr = 1; 183134a38218SChris Mason 183234a38218SChris Mason i = left_nritems - 1; 183334a38218SChris Mason while (i >= nr) { 18345f39d397SChris Mason item = btrfs_item_nr(left, i); 1835db94535dSChris Mason 183600ec4c51SChris Mason if (path->slots[0] == i) 183700ec4c51SChris Mason push_space += data_size + sizeof(*item); 1838db94535dSChris Mason 1839db94535dSChris Mason if (!left->map_token) { 1840db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 1841db94535dSChris Mason sizeof(struct btrfs_item), 1842db94535dSChris Mason &left->map_token, &left->kaddr, 1843db94535dSChris Mason &left->map_start, &left->map_len, 1844db94535dSChris Mason KM_USER1); 1845db94535dSChris Mason } 1846db94535dSChris Mason 1847db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 1848db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 184900ec4c51SChris Mason break; 185000ec4c51SChris Mason push_items++; 1851db94535dSChris Mason push_space += this_item_size + sizeof(*item); 185234a38218SChris Mason if (i == 0) 185334a38218SChris Mason break; 185434a38218SChris Mason i--; 1855db94535dSChris Mason } 1856db94535dSChris Mason if (left->map_token) { 1857db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 1858db94535dSChris Mason left->map_token = NULL; 185900ec4c51SChris Mason } 18605f39d397SChris Mason 1861*925baeddSChris Mason if (push_items == 0) 1862*925baeddSChris Mason goto out_unlock; 18635f39d397SChris Mason 186434a38218SChris Mason if (!empty && push_items == left_nritems) 1865a429e513SChris Mason WARN_ON(1); 18665f39d397SChris Mason 186700ec4c51SChris Mason /* push left to right */ 18685f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 186934a38218SChris Mason 18705f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 1871123abc88SChris Mason push_space -= leaf_data_end(root, left); 18725f39d397SChris Mason 187300ec4c51SChris Mason /* make room in the right data area */ 18745f39d397SChris Mason data_end = leaf_data_end(root, right); 18755f39d397SChris Mason memmove_extent_buffer(right, 18765f39d397SChris Mason btrfs_leaf_data(right) + data_end - push_space, 18775f39d397SChris Mason btrfs_leaf_data(right) + data_end, 18785f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root) - data_end); 18795f39d397SChris Mason 188000ec4c51SChris Mason /* copy from the left data area */ 18815f39d397SChris Mason copy_extent_buffer(right, left, btrfs_leaf_data(right) + 1882d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 1883d6025579SChris Mason btrfs_leaf_data(left) + leaf_data_end(root, left), 1884d6025579SChris Mason push_space); 18855f39d397SChris Mason 18865f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 18875f39d397SChris Mason btrfs_item_nr_offset(0), 18880783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 18895f39d397SChris Mason 189000ec4c51SChris Mason /* copy the items from left to right */ 18915f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 18925f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 18930783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 189400ec4c51SChris Mason 189500ec4c51SChris Mason /* update the item pointers */ 18967518a238SChris Mason right_nritems += push_items; 18975f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 1898123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 18997518a238SChris Mason for (i = 0; i < right_nritems; i++) { 19005f39d397SChris Mason item = btrfs_item_nr(right, i); 1901db94535dSChris Mason if (!right->map_token) { 1902db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 1903db94535dSChris Mason sizeof(struct btrfs_item), 1904db94535dSChris Mason &right->map_token, &right->kaddr, 1905db94535dSChris Mason &right->map_start, &right->map_len, 1906db94535dSChris Mason KM_USER1); 1907db94535dSChris Mason } 1908db94535dSChris Mason push_space -= btrfs_item_size(right, item); 1909db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 1910db94535dSChris Mason } 1911db94535dSChris Mason 1912db94535dSChris Mason if (right->map_token) { 1913db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 1914db94535dSChris Mason right->map_token = NULL; 191500ec4c51SChris Mason } 19167518a238SChris Mason left_nritems -= push_items; 19175f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 191800ec4c51SChris Mason 191934a38218SChris Mason if (left_nritems) 19205f39d397SChris Mason btrfs_mark_buffer_dirty(left); 19215f39d397SChris Mason btrfs_mark_buffer_dirty(right); 1922a429e513SChris Mason 19235f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 19245f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 1925d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 192602217ed2SChris Mason 192700ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 19287518a238SChris Mason if (path->slots[0] >= left_nritems) { 19297518a238SChris Mason path->slots[0] -= left_nritems; 1930*925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 1931*925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 1932*925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 19335f39d397SChris Mason free_extent_buffer(path->nodes[0]); 19345f39d397SChris Mason path->nodes[0] = right; 193500ec4c51SChris Mason path->slots[1] += 1; 193600ec4c51SChris Mason } else { 1937*925baeddSChris Mason btrfs_tree_unlock(right); 19385f39d397SChris Mason free_extent_buffer(right); 193900ec4c51SChris Mason } 194000ec4c51SChris Mason return 0; 1941*925baeddSChris Mason 1942*925baeddSChris Mason out_unlock: 1943*925baeddSChris Mason btrfs_tree_unlock(right); 1944*925baeddSChris Mason free_extent_buffer(right); 1945*925baeddSChris Mason return 1; 194600ec4c51SChris Mason } 1947*925baeddSChris Mason 194800ec4c51SChris Mason /* 194974123bd7SChris Mason * push some data in the path leaf to the left, trying to free up at 195074123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 195174123bd7SChris Mason */ 1952e089f05cSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 195334a38218SChris Mason *root, struct btrfs_path *path, int data_size, 195434a38218SChris Mason int empty) 1955be0e5c09SChris Mason { 19565f39d397SChris Mason struct btrfs_disk_key disk_key; 19575f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 19585f39d397SChris Mason struct extent_buffer *left; 1959be0e5c09SChris Mason int slot; 1960be0e5c09SChris Mason int i; 1961be0e5c09SChris Mason int free_space; 1962be0e5c09SChris Mason int push_space = 0; 1963be0e5c09SChris Mason int push_items = 0; 19640783fcfcSChris Mason struct btrfs_item *item; 19657518a238SChris Mason u32 old_left_nritems; 19665f39d397SChris Mason u32 right_nritems; 196734a38218SChris Mason u32 nr; 1968aa5d6bedSChris Mason int ret = 0; 1969aa5d6bedSChris Mason int wret; 1970db94535dSChris Mason u32 this_item_size; 1971db94535dSChris Mason u32 old_left_item_size; 1972be0e5c09SChris Mason 1973be0e5c09SChris Mason slot = path->slots[1]; 19745f39d397SChris Mason if (slot == 0) 1975be0e5c09SChris Mason return 1; 19765f39d397SChris Mason if (!path->nodes[1]) 1977be0e5c09SChris Mason return 1; 19785f39d397SChris Mason 19793685f791SChris Mason right_nritems = btrfs_header_nritems(right); 19803685f791SChris Mason if (right_nritems == 0) { 19813685f791SChris Mason return 1; 19823685f791SChris Mason } 19833685f791SChris Mason 1984ca7a79adSChris Mason left = read_node_slot(root, path->nodes[1], slot - 1); 1985*925baeddSChris Mason btrfs_tree_lock(left); 1986123abc88SChris Mason free_space = btrfs_leaf_free_space(root, left); 19870783fcfcSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) { 1988*925baeddSChris Mason ret = 1; 1989*925baeddSChris Mason goto out; 1990be0e5c09SChris Mason } 199102217ed2SChris Mason 199202217ed2SChris Mason /* cow and double check */ 19935f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, 19945f39d397SChris Mason path->nodes[1], slot - 1, &left); 199554aa1f4dSChris Mason if (ret) { 199654aa1f4dSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 1997*925baeddSChris Mason ret = 1; 1998*925baeddSChris Mason goto out; 199954aa1f4dSChris Mason } 20003685f791SChris Mason 2001123abc88SChris Mason free_space = btrfs_leaf_free_space(root, left); 20020783fcfcSChris Mason if (free_space < data_size + sizeof(struct btrfs_item)) { 2003*925baeddSChris Mason ret = 1; 2004*925baeddSChris Mason goto out; 200502217ed2SChris Mason } 200602217ed2SChris Mason 200734a38218SChris Mason if (empty) 200834a38218SChris Mason nr = right_nritems; 200934a38218SChris Mason else 201034a38218SChris Mason nr = right_nritems - 1; 201134a38218SChris Mason 201234a38218SChris Mason for (i = 0; i < nr; i++) { 20135f39d397SChris Mason item = btrfs_item_nr(right, i); 2014db94535dSChris Mason if (!right->map_token) { 2015db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2016db94535dSChris Mason sizeof(struct btrfs_item), 2017db94535dSChris Mason &right->map_token, &right->kaddr, 2018db94535dSChris Mason &right->map_start, &right->map_len, 2019db94535dSChris Mason KM_USER1); 2020db94535dSChris Mason } 2021db94535dSChris Mason 2022be0e5c09SChris Mason if (path->slots[0] == i) 2023be0e5c09SChris Mason push_space += data_size + sizeof(*item); 2024db94535dSChris Mason 2025db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 2026db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 2027be0e5c09SChris Mason break; 2028db94535dSChris Mason 2029be0e5c09SChris Mason push_items++; 2030db94535dSChris Mason push_space += this_item_size + sizeof(*item); 2031be0e5c09SChris Mason } 2032db94535dSChris Mason 2033db94535dSChris Mason if (right->map_token) { 2034db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2035db94535dSChris Mason right->map_token = NULL; 2036db94535dSChris Mason } 2037db94535dSChris Mason 2038be0e5c09SChris Mason if (push_items == 0) { 2039*925baeddSChris Mason ret = 1; 2040*925baeddSChris Mason goto out; 2041be0e5c09SChris Mason } 204234a38218SChris Mason if (!empty && push_items == btrfs_header_nritems(right)) 2043a429e513SChris Mason WARN_ON(1); 20445f39d397SChris Mason 2045be0e5c09SChris Mason /* push data from right to left */ 20465f39d397SChris Mason copy_extent_buffer(left, right, 20475f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 20485f39d397SChris Mason btrfs_item_nr_offset(0), 20495f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 20505f39d397SChris Mason 2051123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root) - 20525f39d397SChris Mason btrfs_item_offset_nr(right, push_items -1); 20535f39d397SChris Mason 20545f39d397SChris Mason copy_extent_buffer(left, right, btrfs_leaf_data(left) + 2055d6025579SChris Mason leaf_data_end(root, left) - push_space, 2056123abc88SChris Mason btrfs_leaf_data(right) + 20575f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 2058be0e5c09SChris Mason push_space); 20595f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 2060eb60ceacSChris Mason BUG_ON(old_left_nritems < 0); 2061eb60ceacSChris Mason 2062db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 2063be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 20645f39d397SChris Mason u32 ioff; 2065db94535dSChris Mason 20665f39d397SChris Mason item = btrfs_item_nr(left, i); 2067db94535dSChris Mason if (!left->map_token) { 2068db94535dSChris Mason map_extent_buffer(left, (unsigned long)item, 2069db94535dSChris Mason sizeof(struct btrfs_item), 2070db94535dSChris Mason &left->map_token, &left->kaddr, 2071db94535dSChris Mason &left->map_start, &left->map_len, 2072db94535dSChris Mason KM_USER1); 2073db94535dSChris Mason } 2074db94535dSChris Mason 20755f39d397SChris Mason ioff = btrfs_item_offset(left, item); 20765f39d397SChris Mason btrfs_set_item_offset(left, item, 2077db94535dSChris Mason ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); 2078be0e5c09SChris Mason } 20795f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 2080db94535dSChris Mason if (left->map_token) { 2081db94535dSChris Mason unmap_extent_buffer(left, left->map_token, KM_USER1); 2082db94535dSChris Mason left->map_token = NULL; 2083db94535dSChris Mason } 2084be0e5c09SChris Mason 2085be0e5c09SChris Mason /* fixup right node */ 208634a38218SChris Mason if (push_items > right_nritems) { 208734a38218SChris Mason printk("push items %d nr %u\n", push_items, right_nritems); 208834a38218SChris Mason WARN_ON(1); 208934a38218SChris Mason } 209034a38218SChris Mason 209134a38218SChris Mason if (push_items < right_nritems) { 20925f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 2093123abc88SChris Mason leaf_data_end(root, right); 20945f39d397SChris Mason memmove_extent_buffer(right, btrfs_leaf_data(right) + 2095d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 2096d6025579SChris Mason btrfs_leaf_data(right) + 2097123abc88SChris Mason leaf_data_end(root, right), push_space); 20985f39d397SChris Mason 20995f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 21005f39d397SChris Mason btrfs_item_nr_offset(push_items), 21015f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 21020783fcfcSChris Mason sizeof(struct btrfs_item)); 210334a38218SChris Mason } 2104eef1c494SYan right_nritems -= push_items; 2105eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 2106123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 21075f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 21085f39d397SChris Mason item = btrfs_item_nr(right, i); 2109db94535dSChris Mason 2110db94535dSChris Mason if (!right->map_token) { 2111db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2112db94535dSChris Mason sizeof(struct btrfs_item), 2113db94535dSChris Mason &right->map_token, &right->kaddr, 2114db94535dSChris Mason &right->map_start, &right->map_len, 2115db94535dSChris Mason KM_USER1); 2116db94535dSChris Mason } 2117db94535dSChris Mason 2118db94535dSChris Mason push_space = push_space - btrfs_item_size(right, item); 2119db94535dSChris Mason btrfs_set_item_offset(right, item, push_space); 2120db94535dSChris Mason } 2121db94535dSChris Mason if (right->map_token) { 2122db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2123db94535dSChris Mason right->map_token = NULL; 2124be0e5c09SChris Mason } 2125eb60ceacSChris Mason 21265f39d397SChris Mason btrfs_mark_buffer_dirty(left); 212734a38218SChris Mason if (right_nritems) 21285f39d397SChris Mason btrfs_mark_buffer_dirty(right); 2129098f59c2SChris Mason 21305f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 21315f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, 1); 2132aa5d6bedSChris Mason if (wret) 2133aa5d6bedSChris Mason ret = wret; 2134be0e5c09SChris Mason 2135be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 2136be0e5c09SChris Mason if (path->slots[0] < push_items) { 2137be0e5c09SChris Mason path->slots[0] += old_left_nritems; 2138*925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 2139*925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 2140*925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 21415f39d397SChris Mason free_extent_buffer(path->nodes[0]); 21425f39d397SChris Mason path->nodes[0] = left; 2143be0e5c09SChris Mason path->slots[1] -= 1; 2144be0e5c09SChris Mason } else { 2145*925baeddSChris Mason btrfs_tree_unlock(left); 21465f39d397SChris Mason free_extent_buffer(left); 2147be0e5c09SChris Mason path->slots[0] -= push_items; 2148be0e5c09SChris Mason } 2149eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2150aa5d6bedSChris Mason return ret; 2151*925baeddSChris Mason out: 2152*925baeddSChris Mason btrfs_tree_unlock(left); 2153*925baeddSChris Mason free_extent_buffer(left); 2154*925baeddSChris Mason return ret; 2155be0e5c09SChris Mason } 2156be0e5c09SChris Mason 215774123bd7SChris Mason /* 215874123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 215974123bd7SChris Mason * available for the resulting leaf level of the path. 2160aa5d6bedSChris Mason * 2161aa5d6bedSChris Mason * returns 0 if all went well and < 0 on failure. 216274123bd7SChris Mason */ 2163e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root 2164d4dbff95SChris Mason *root, struct btrfs_key *ins_key, 2165cc0c5538SChris Mason struct btrfs_path *path, int data_size, int extend) 2166be0e5c09SChris Mason { 21677bb86316SChris Mason u64 root_gen; 21685f39d397SChris Mason struct extent_buffer *l; 21697518a238SChris Mason u32 nritems; 2170eb60ceacSChris Mason int mid; 2171eb60ceacSChris Mason int slot; 21725f39d397SChris Mason struct extent_buffer *right; 21730783fcfcSChris Mason int space_needed = data_size + sizeof(struct btrfs_item); 2174be0e5c09SChris Mason int data_copy_size; 2175be0e5c09SChris Mason int rt_data_off; 2176be0e5c09SChris Mason int i; 2177d4dbff95SChris Mason int ret = 0; 2178aa5d6bedSChris Mason int wret; 2179cc0c5538SChris Mason int double_split; 2180cc0c5538SChris Mason int num_doubles = 0; 2181d4dbff95SChris Mason struct btrfs_disk_key disk_key; 2182be0e5c09SChris Mason 2183cc0c5538SChris Mason if (extend) 2184cc0c5538SChris Mason space_needed = data_size; 2185cc0c5538SChris Mason 21867bb86316SChris Mason if (root->ref_cows) 21877bb86316SChris Mason root_gen = trans->transid; 21887bb86316SChris Mason else 21897bb86316SChris Mason root_gen = 0; 21907bb86316SChris Mason 219140689478SChris Mason /* first try to make some room by pushing left and right */ 21923685f791SChris Mason if (ins_key->type != BTRFS_DIR_ITEM_KEY) { 219334a38218SChris Mason wret = push_leaf_right(trans, root, path, data_size, 0); 21943326d1b0SChris Mason if (wret < 0) { 2195eaee50e8SChris Mason return wret; 21963326d1b0SChris Mason } 2197eaee50e8SChris Mason if (wret) { 219834a38218SChris Mason wret = push_leaf_left(trans, root, path, data_size, 0); 2199eaee50e8SChris Mason if (wret < 0) 2200eaee50e8SChris Mason return wret; 2201eaee50e8SChris Mason } 22025f39d397SChris Mason l = path->nodes[0]; 2203aa5d6bedSChris Mason 2204aa5d6bedSChris Mason /* did the pushes work? */ 2205cc0c5538SChris Mason if (btrfs_leaf_free_space(root, l) >= space_needed) 2206be0e5c09SChris Mason return 0; 22073326d1b0SChris Mason } 2208aa5d6bedSChris Mason 22095c680ed6SChris Mason if (!path->nodes[1]) { 2210e089f05cSChris Mason ret = insert_new_root(trans, root, path, 1); 22115c680ed6SChris Mason if (ret) 22125c680ed6SChris Mason return ret; 22135c680ed6SChris Mason } 2214cc0c5538SChris Mason again: 2215cc0c5538SChris Mason double_split = 0; 2216cc0c5538SChris Mason l = path->nodes[0]; 2217eb60ceacSChris Mason slot = path->slots[0]; 22185f39d397SChris Mason nritems = btrfs_header_nritems(l); 2219eb60ceacSChris Mason mid = (nritems + 1)/ 2; 222054aa1f4dSChris Mason 22217bb86316SChris Mason btrfs_item_key(l, &disk_key, 0); 22227bb86316SChris Mason 2223*925baeddSChris Mason right = btrfs_alloc_free_block(trans, root, root->leafsize, 22247bb86316SChris Mason root->root_key.objectid, 22257bb86316SChris Mason root_gen, disk_key.objectid, 0, 2226db94535dSChris Mason l->start, 0); 2227cea9e445SChris Mason if (IS_ERR(right)) { 2228cea9e445SChris Mason BUG_ON(1); 22295f39d397SChris Mason return PTR_ERR(right); 2230cea9e445SChris Mason } 223154aa1f4dSChris Mason 22325f39d397SChris Mason memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); 2233db94535dSChris Mason btrfs_set_header_bytenr(right, right->start); 22345f39d397SChris Mason btrfs_set_header_generation(right, trans->transid); 22355f39d397SChris Mason btrfs_set_header_owner(right, root->root_key.objectid); 22365f39d397SChris Mason btrfs_set_header_level(right, 0); 22375f39d397SChris Mason write_extent_buffer(right, root->fs_info->fsid, 22385f39d397SChris Mason (unsigned long)btrfs_header_fsid(right), 22395f39d397SChris Mason BTRFS_FSID_SIZE); 2240e17cade2SChris Mason 2241e17cade2SChris Mason write_extent_buffer(right, root->fs_info->chunk_tree_uuid, 2242e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(right), 2243e17cade2SChris Mason BTRFS_UUID_SIZE); 2244d4dbff95SChris Mason if (mid <= slot) { 2245d4dbff95SChris Mason if (nritems == 1 || 2246d4dbff95SChris Mason leaf_space_used(l, mid, nritems - mid) + space_needed > 2247d4dbff95SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 2248d4dbff95SChris Mason if (slot >= nritems) { 2249d4dbff95SChris Mason btrfs_cpu_key_to_disk(&disk_key, ins_key); 22505f39d397SChris Mason btrfs_set_header_nritems(right, 0); 2251d4dbff95SChris Mason wret = insert_ptr(trans, root, path, 2252db94535dSChris Mason &disk_key, right->start, 2253d4dbff95SChris Mason path->slots[1] + 1, 1); 2254d4dbff95SChris Mason if (wret) 2255d4dbff95SChris Mason ret = wret; 2256*925baeddSChris Mason 2257*925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 22585f39d397SChris Mason free_extent_buffer(path->nodes[0]); 22595f39d397SChris Mason path->nodes[0] = right; 2260d4dbff95SChris Mason path->slots[0] = 0; 2261d4dbff95SChris Mason path->slots[1] += 1; 22620ef8b242SChris Mason btrfs_mark_buffer_dirty(right); 2263d4dbff95SChris Mason return ret; 2264d4dbff95SChris Mason } 2265d4dbff95SChris Mason mid = slot; 22663326d1b0SChris Mason if (mid != nritems && 22673326d1b0SChris Mason leaf_space_used(l, mid, nritems - mid) + 22683326d1b0SChris Mason space_needed > BTRFS_LEAF_DATA_SIZE(root)) { 2269d4dbff95SChris Mason double_split = 1; 2270d4dbff95SChris Mason } 22713326d1b0SChris Mason } 2272d4dbff95SChris Mason } else { 2273d4dbff95SChris Mason if (leaf_space_used(l, 0, mid + 1) + space_needed > 2274d4dbff95SChris Mason BTRFS_LEAF_DATA_SIZE(root)) { 2275cc0c5538SChris Mason if (!extend && slot == 0) { 2276d4dbff95SChris Mason btrfs_cpu_key_to_disk(&disk_key, ins_key); 22775f39d397SChris Mason btrfs_set_header_nritems(right, 0); 2278d4dbff95SChris Mason wret = insert_ptr(trans, root, path, 2279d4dbff95SChris Mason &disk_key, 2280db94535dSChris Mason right->start, 2281098f59c2SChris Mason path->slots[1], 1); 2282d4dbff95SChris Mason if (wret) 2283d4dbff95SChris Mason ret = wret; 2284*925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 22855f39d397SChris Mason free_extent_buffer(path->nodes[0]); 22865f39d397SChris Mason path->nodes[0] = right; 2287d4dbff95SChris Mason path->slots[0] = 0; 2288a429e513SChris Mason if (path->slots[1] == 0) { 2289a429e513SChris Mason wret = fixup_low_keys(trans, root, 2290a429e513SChris Mason path, &disk_key, 1); 2291a429e513SChris Mason if (wret) 2292a429e513SChris Mason ret = wret; 2293a429e513SChris Mason } 22940ef8b242SChris Mason btrfs_mark_buffer_dirty(right); 2295d4dbff95SChris Mason return ret; 2296cc0c5538SChris Mason } else if (extend && slot == 0) { 2297cc0c5538SChris Mason mid = 1; 2298cc0c5538SChris Mason } else { 2299d4dbff95SChris Mason mid = slot; 23005ee78ac7SChris Mason if (mid != nritems && 23015ee78ac7SChris Mason leaf_space_used(l, mid, nritems - mid) + 23025ee78ac7SChris Mason space_needed > BTRFS_LEAF_DATA_SIZE(root)) { 2303d4dbff95SChris Mason double_split = 1; 2304d4dbff95SChris Mason } 2305d4dbff95SChris Mason } 23065ee78ac7SChris Mason } 2307cc0c5538SChris Mason } 23085f39d397SChris Mason nritems = nritems - mid; 23095f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 23105f39d397SChris Mason data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 23115f39d397SChris Mason 23125f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 23135f39d397SChris Mason btrfs_item_nr_offset(mid), 23145f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 23155f39d397SChris Mason 23165f39d397SChris Mason copy_extent_buffer(right, l, 2317d6025579SChris Mason btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - 2318123abc88SChris Mason data_copy_size, btrfs_leaf_data(l) + 2319123abc88SChris Mason leaf_data_end(root, l), data_copy_size); 232074123bd7SChris Mason 23215f39d397SChris Mason rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - 23225f39d397SChris Mason btrfs_item_end_nr(l, mid); 23235f39d397SChris Mason 23245f39d397SChris Mason for (i = 0; i < nritems; i++) { 23255f39d397SChris Mason struct btrfs_item *item = btrfs_item_nr(right, i); 2326db94535dSChris Mason u32 ioff; 2327db94535dSChris Mason 2328db94535dSChris Mason if (!right->map_token) { 2329db94535dSChris Mason map_extent_buffer(right, (unsigned long)item, 2330db94535dSChris Mason sizeof(struct btrfs_item), 2331db94535dSChris Mason &right->map_token, &right->kaddr, 2332db94535dSChris Mason &right->map_start, &right->map_len, 2333db94535dSChris Mason KM_USER1); 2334db94535dSChris Mason } 2335db94535dSChris Mason 2336db94535dSChris Mason ioff = btrfs_item_offset(right, item); 23375f39d397SChris Mason btrfs_set_item_offset(right, item, ioff + rt_data_off); 23380783fcfcSChris Mason } 233974123bd7SChris Mason 2340db94535dSChris Mason if (right->map_token) { 2341db94535dSChris Mason unmap_extent_buffer(right, right->map_token, KM_USER1); 2342db94535dSChris Mason right->map_token = NULL; 2343db94535dSChris Mason } 2344db94535dSChris Mason 23455f39d397SChris Mason btrfs_set_header_nritems(l, mid); 2346aa5d6bedSChris Mason ret = 0; 23475f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 2348db94535dSChris Mason wret = insert_ptr(trans, root, path, &disk_key, right->start, 2349db94535dSChris Mason path->slots[1] + 1, 1); 2350aa5d6bedSChris Mason if (wret) 2351aa5d6bedSChris Mason ret = wret; 23525f39d397SChris Mason 23535f39d397SChris Mason btrfs_mark_buffer_dirty(right); 23545f39d397SChris Mason btrfs_mark_buffer_dirty(l); 2355eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 23565f39d397SChris Mason 2357be0e5c09SChris Mason if (mid <= slot) { 2358*925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 23595f39d397SChris Mason free_extent_buffer(path->nodes[0]); 23605f39d397SChris Mason path->nodes[0] = right; 2361be0e5c09SChris Mason path->slots[0] -= mid; 2362be0e5c09SChris Mason path->slots[1] += 1; 2363*925baeddSChris Mason } else { 2364*925baeddSChris Mason btrfs_tree_unlock(right); 23655f39d397SChris Mason free_extent_buffer(right); 2366*925baeddSChris Mason } 23675f39d397SChris Mason 2368eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 2369d4dbff95SChris Mason 2370cc0c5538SChris Mason if (double_split) { 2371cc0c5538SChris Mason BUG_ON(num_doubles != 0); 2372cc0c5538SChris Mason num_doubles++; 2373cc0c5538SChris Mason goto again; 23743326d1b0SChris Mason } 2375be0e5c09SChris Mason return ret; 2376be0e5c09SChris Mason } 2377be0e5c09SChris Mason 2378b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans, 2379b18c6685SChris Mason struct btrfs_root *root, 2380b18c6685SChris Mason struct btrfs_path *path, 2381179e29e4SChris Mason u32 new_size, int from_end) 2382b18c6685SChris Mason { 2383b18c6685SChris Mason int ret = 0; 2384b18c6685SChris Mason int slot; 2385b18c6685SChris Mason int slot_orig; 23865f39d397SChris Mason struct extent_buffer *leaf; 23875f39d397SChris Mason struct btrfs_item *item; 2388b18c6685SChris Mason u32 nritems; 2389b18c6685SChris Mason unsigned int data_end; 2390b18c6685SChris Mason unsigned int old_data_start; 2391b18c6685SChris Mason unsigned int old_size; 2392b18c6685SChris Mason unsigned int size_diff; 2393b18c6685SChris Mason int i; 2394b18c6685SChris Mason 2395b18c6685SChris Mason slot_orig = path->slots[0]; 23965f39d397SChris Mason leaf = path->nodes[0]; 2397179e29e4SChris Mason slot = path->slots[0]; 2398179e29e4SChris Mason 2399179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 2400179e29e4SChris Mason if (old_size == new_size) 2401179e29e4SChris Mason return 0; 2402b18c6685SChris Mason 24035f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 2404b18c6685SChris Mason data_end = leaf_data_end(root, leaf); 2405b18c6685SChris Mason 24065f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 2407179e29e4SChris Mason 2408b18c6685SChris Mason size_diff = old_size - new_size; 2409b18c6685SChris Mason 2410b18c6685SChris Mason BUG_ON(slot < 0); 2411b18c6685SChris Mason BUG_ON(slot >= nritems); 2412b18c6685SChris Mason 2413b18c6685SChris Mason /* 2414b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 2415b18c6685SChris Mason */ 2416b18c6685SChris Mason /* first correct the data pointers */ 2417b18c6685SChris Mason for (i = slot; i < nritems; i++) { 24185f39d397SChris Mason u32 ioff; 24195f39d397SChris Mason item = btrfs_item_nr(leaf, i); 2420db94535dSChris Mason 2421db94535dSChris Mason if (!leaf->map_token) { 2422db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 2423db94535dSChris Mason sizeof(struct btrfs_item), 2424db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 2425db94535dSChris Mason &leaf->map_start, &leaf->map_len, 2426db94535dSChris Mason KM_USER1); 2427db94535dSChris Mason } 2428db94535dSChris Mason 24295f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 24305f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + size_diff); 2431b18c6685SChris Mason } 2432db94535dSChris Mason 2433db94535dSChris Mason if (leaf->map_token) { 2434db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2435db94535dSChris Mason leaf->map_token = NULL; 2436db94535dSChris Mason } 2437db94535dSChris Mason 2438b18c6685SChris Mason /* shift the data */ 2439179e29e4SChris Mason if (from_end) { 24405f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2441b18c6685SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 2442b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 2443179e29e4SChris Mason } else { 2444179e29e4SChris Mason struct btrfs_disk_key disk_key; 2445179e29e4SChris Mason u64 offset; 2446179e29e4SChris Mason 2447179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 2448179e29e4SChris Mason 2449179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 2450179e29e4SChris Mason unsigned long ptr; 2451179e29e4SChris Mason struct btrfs_file_extent_item *fi; 2452179e29e4SChris Mason 2453179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 2454179e29e4SChris Mason struct btrfs_file_extent_item); 2455179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 2456179e29e4SChris Mason (unsigned long)fi - size_diff); 2457179e29e4SChris Mason 2458179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 2459179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 2460179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 2461179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 2462179e29e4SChris Mason (unsigned long)fi, 2463179e29e4SChris Mason offsetof(struct btrfs_file_extent_item, 2464179e29e4SChris Mason disk_bytenr)); 2465179e29e4SChris Mason } 2466179e29e4SChris Mason } 2467179e29e4SChris Mason 2468179e29e4SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2469179e29e4SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 2470179e29e4SChris Mason data_end, old_data_start - data_end); 2471179e29e4SChris Mason 2472179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 2473179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 2474179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 2475179e29e4SChris Mason if (slot == 0) 2476179e29e4SChris Mason fixup_low_keys(trans, root, path, &disk_key, 1); 2477179e29e4SChris Mason } 24785f39d397SChris Mason 24795f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 24805f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 24815f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 2482b18c6685SChris Mason 2483b18c6685SChris Mason ret = 0; 24845f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 24855f39d397SChris Mason btrfs_print_leaf(root, leaf); 2486b18c6685SChris Mason BUG(); 24875f39d397SChris Mason } 2488b18c6685SChris Mason return ret; 2489b18c6685SChris Mason } 2490b18c6685SChris Mason 24915f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans, 24925f39d397SChris Mason struct btrfs_root *root, struct btrfs_path *path, 24935f39d397SChris Mason u32 data_size) 24946567e837SChris Mason { 24956567e837SChris Mason int ret = 0; 24966567e837SChris Mason int slot; 24976567e837SChris Mason int slot_orig; 24985f39d397SChris Mason struct extent_buffer *leaf; 24995f39d397SChris Mason struct btrfs_item *item; 25006567e837SChris Mason u32 nritems; 25016567e837SChris Mason unsigned int data_end; 25026567e837SChris Mason unsigned int old_data; 25036567e837SChris Mason unsigned int old_size; 25046567e837SChris Mason int i; 25056567e837SChris Mason 25066567e837SChris Mason slot_orig = path->slots[0]; 25075f39d397SChris Mason leaf = path->nodes[0]; 25086567e837SChris Mason 25095f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 25106567e837SChris Mason data_end = leaf_data_end(root, leaf); 25116567e837SChris Mason 25125f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < data_size) { 25135f39d397SChris Mason btrfs_print_leaf(root, leaf); 25146567e837SChris Mason BUG(); 25155f39d397SChris Mason } 25166567e837SChris Mason slot = path->slots[0]; 25175f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 25186567e837SChris Mason 25196567e837SChris Mason BUG_ON(slot < 0); 25203326d1b0SChris Mason if (slot >= nritems) { 25213326d1b0SChris Mason btrfs_print_leaf(root, leaf); 25223326d1b0SChris Mason printk("slot %d too large, nritems %d\n", slot, nritems); 25233326d1b0SChris Mason BUG_ON(1); 25243326d1b0SChris Mason } 25256567e837SChris Mason 25266567e837SChris Mason /* 25276567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 25286567e837SChris Mason */ 25296567e837SChris Mason /* first correct the data pointers */ 25306567e837SChris Mason for (i = slot; i < nritems; i++) { 25315f39d397SChris Mason u32 ioff; 25325f39d397SChris Mason item = btrfs_item_nr(leaf, i); 2533db94535dSChris Mason 2534db94535dSChris Mason if (!leaf->map_token) { 2535db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 2536db94535dSChris Mason sizeof(struct btrfs_item), 2537db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 2538db94535dSChris Mason &leaf->map_start, &leaf->map_len, 2539db94535dSChris Mason KM_USER1); 2540db94535dSChris Mason } 25415f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 25425f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff - data_size); 25436567e837SChris Mason } 25445f39d397SChris Mason 2545db94535dSChris Mason if (leaf->map_token) { 2546db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2547db94535dSChris Mason leaf->map_token = NULL; 2548db94535dSChris Mason } 2549db94535dSChris Mason 25506567e837SChris Mason /* shift the data */ 25515f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 25526567e837SChris Mason data_end - data_size, btrfs_leaf_data(leaf) + 25536567e837SChris Mason data_end, old_data - data_end); 25545f39d397SChris Mason 25556567e837SChris Mason data_end = old_data; 25565f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 25575f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 25585f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 25595f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 25606567e837SChris Mason 25616567e837SChris Mason ret = 0; 25625f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 25635f39d397SChris Mason btrfs_print_leaf(root, leaf); 25646567e837SChris Mason BUG(); 25655f39d397SChris Mason } 25666567e837SChris Mason return ret; 25676567e837SChris Mason } 25686567e837SChris Mason 256974123bd7SChris Mason /* 257074123bd7SChris Mason * Given a key and some data, insert an item into the tree. 257174123bd7SChris Mason * This does all the path init required, making room in the tree if needed. 257274123bd7SChris Mason */ 25739c58309dSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 25745f39d397SChris Mason struct btrfs_root *root, 25755f39d397SChris Mason struct btrfs_path *path, 25769c58309dSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 25779c58309dSChris Mason int nr) 2578be0e5c09SChris Mason { 25795f39d397SChris Mason struct extent_buffer *leaf; 25805f39d397SChris Mason struct btrfs_item *item; 2581aa5d6bedSChris Mason int ret = 0; 2582be0e5c09SChris Mason int slot; 2583eb60ceacSChris Mason int slot_orig; 25849c58309dSChris Mason int i; 25857518a238SChris Mason u32 nritems; 25869c58309dSChris Mason u32 total_size = 0; 25879c58309dSChris Mason u32 total_data = 0; 2588be0e5c09SChris Mason unsigned int data_end; 2589e2fa7227SChris Mason struct btrfs_disk_key disk_key; 2590e2fa7227SChris Mason 25919c58309dSChris Mason for (i = 0; i < nr; i++) { 25929c58309dSChris Mason total_data += data_size[i]; 25939c58309dSChris Mason } 2594be0e5c09SChris Mason 25959c58309dSChris Mason total_size = total_data + (nr - 1) * sizeof(struct btrfs_item); 25969c58309dSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 2597eb60ceacSChris Mason if (ret == 0) { 2598f0930a37SChris Mason return -EEXIST; 2599aa5d6bedSChris Mason } 2600ed2ff2cbSChris Mason if (ret < 0) 2601ed2ff2cbSChris Mason goto out; 2602be0e5c09SChris Mason 260362e2749eSChris Mason slot_orig = path->slots[0]; 26045f39d397SChris Mason leaf = path->nodes[0]; 260574123bd7SChris Mason 26065f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 2607123abc88SChris Mason data_end = leaf_data_end(root, leaf); 2608eb60ceacSChris Mason 2609123abc88SChris Mason if (btrfs_leaf_free_space(root, leaf) < 26109c58309dSChris Mason sizeof(struct btrfs_item) + total_size) { 26113326d1b0SChris Mason btrfs_print_leaf(root, leaf); 26123326d1b0SChris Mason printk("not enough freespace need %u have %d\n", 26139c58309dSChris Mason total_size, btrfs_leaf_free_space(root, leaf)); 2614be0e5c09SChris Mason BUG(); 2615d4dbff95SChris Mason } 26165f39d397SChris Mason 261762e2749eSChris Mason slot = path->slots[0]; 2618eb60ceacSChris Mason BUG_ON(slot < 0); 26195f39d397SChris Mason 2620be0e5c09SChris Mason if (slot != nritems) { 2621be0e5c09SChris Mason int i; 26225f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 2623be0e5c09SChris Mason 26245f39d397SChris Mason if (old_data < data_end) { 26255f39d397SChris Mason btrfs_print_leaf(root, leaf); 26265f39d397SChris Mason printk("slot %d old_data %d data_end %d\n", 26275f39d397SChris Mason slot, old_data, data_end); 26285f39d397SChris Mason BUG_ON(1); 26295f39d397SChris Mason } 2630be0e5c09SChris Mason /* 2631be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 2632be0e5c09SChris Mason */ 2633be0e5c09SChris Mason /* first correct the data pointers */ 2634db94535dSChris Mason WARN_ON(leaf->map_token); 26350783fcfcSChris Mason for (i = slot; i < nritems; i++) { 26365f39d397SChris Mason u32 ioff; 2637db94535dSChris Mason 26385f39d397SChris Mason item = btrfs_item_nr(leaf, i); 2639db94535dSChris Mason if (!leaf->map_token) { 2640db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 2641db94535dSChris Mason sizeof(struct btrfs_item), 2642db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 2643db94535dSChris Mason &leaf->map_start, &leaf->map_len, 2644db94535dSChris Mason KM_USER1); 2645db94535dSChris Mason } 2646db94535dSChris Mason 26475f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 26489c58309dSChris Mason btrfs_set_item_offset(leaf, item, ioff - total_data); 26490783fcfcSChris Mason } 2650db94535dSChris Mason if (leaf->map_token) { 2651db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2652db94535dSChris Mason leaf->map_token = NULL; 2653db94535dSChris Mason } 2654be0e5c09SChris Mason 2655be0e5c09SChris Mason /* shift the items */ 26569c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 26575f39d397SChris Mason btrfs_item_nr_offset(slot), 26580783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 2659be0e5c09SChris Mason 2660be0e5c09SChris Mason /* shift the data */ 26615f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 26629c58309dSChris Mason data_end - total_data, btrfs_leaf_data(leaf) + 2663be0e5c09SChris Mason data_end, old_data - data_end); 2664be0e5c09SChris Mason data_end = old_data; 2665be0e5c09SChris Mason } 26665f39d397SChris Mason 266762e2749eSChris Mason /* setup the item for the new data */ 26689c58309dSChris Mason for (i = 0; i < nr; i++) { 26699c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 26709c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 26719c58309dSChris Mason item = btrfs_item_nr(leaf, slot + i); 26729c58309dSChris Mason btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 26739c58309dSChris Mason data_end -= data_size[i]; 26749c58309dSChris Mason btrfs_set_item_size(leaf, item, data_size[i]); 26759c58309dSChris Mason } 26769c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 26775f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 2678aa5d6bedSChris Mason 2679aa5d6bedSChris Mason ret = 0; 26805a01a2e3SChris Mason if (slot == 0) { 26815a01a2e3SChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key); 2682e089f05cSChris Mason ret = fixup_low_keys(trans, root, path, &disk_key, 1); 26835a01a2e3SChris Mason } 2684aa5d6bedSChris Mason 26855f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 26865f39d397SChris Mason btrfs_print_leaf(root, leaf); 2687be0e5c09SChris Mason BUG(); 26885f39d397SChris Mason } 2689ed2ff2cbSChris Mason out: 269062e2749eSChris Mason return ret; 269162e2749eSChris Mason } 269262e2749eSChris Mason 269362e2749eSChris Mason /* 269462e2749eSChris Mason * Given a key and some data, insert an item into the tree. 269562e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 269662e2749eSChris Mason */ 2697e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root 2698e089f05cSChris Mason *root, struct btrfs_key *cpu_key, void *data, u32 2699e089f05cSChris Mason data_size) 270062e2749eSChris Mason { 270162e2749eSChris Mason int ret = 0; 27022c90e5d6SChris Mason struct btrfs_path *path; 27035f39d397SChris Mason struct extent_buffer *leaf; 27045f39d397SChris Mason unsigned long ptr; 270562e2749eSChris Mason 27062c90e5d6SChris Mason path = btrfs_alloc_path(); 27072c90e5d6SChris Mason BUG_ON(!path); 27082c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 270962e2749eSChris Mason if (!ret) { 27105f39d397SChris Mason leaf = path->nodes[0]; 27115f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 27125f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 27135f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 271462e2749eSChris Mason } 27152c90e5d6SChris Mason btrfs_free_path(path); 2716aa5d6bedSChris Mason return ret; 2717be0e5c09SChris Mason } 2718be0e5c09SChris Mason 271974123bd7SChris Mason /* 27205de08d7dSChris Mason * delete the pointer from a given node. 272174123bd7SChris Mason * 272274123bd7SChris Mason * If the delete empties a node, the node is removed from the tree, 272374123bd7SChris Mason * continuing all the way the root if required. The root is converted into 272474123bd7SChris Mason * a leaf if all the nodes are emptied. 272574123bd7SChris Mason */ 2726e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2727e089f05cSChris Mason struct btrfs_path *path, int level, int slot) 2728be0e5c09SChris Mason { 27295f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 27307518a238SChris Mason u32 nritems; 2731aa5d6bedSChris Mason int ret = 0; 2732bb803951SChris Mason int wret; 2733be0e5c09SChris Mason 27345f39d397SChris Mason nritems = btrfs_header_nritems(parent); 2735be0e5c09SChris Mason if (slot != nritems -1) { 27365f39d397SChris Mason memmove_extent_buffer(parent, 27375f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 27385f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 2739d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 2740d6025579SChris Mason (nritems - slot - 1)); 2741be0e5c09SChris Mason } 27427518a238SChris Mason nritems--; 27435f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 27447518a238SChris Mason if (nritems == 0 && parent == root->node) { 27455f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 2746eb60ceacSChris Mason /* just turn the root into a leaf and break */ 27475f39d397SChris Mason btrfs_set_header_level(root->node, 0); 2748bb803951SChris Mason } else if (slot == 0) { 27495f39d397SChris Mason struct btrfs_disk_key disk_key; 27505f39d397SChris Mason 27515f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 27525f39d397SChris Mason wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); 27530f70abe2SChris Mason if (wret) 27540f70abe2SChris Mason ret = wret; 2755be0e5c09SChris Mason } 2756d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 2757aa5d6bedSChris Mason return ret; 2758be0e5c09SChris Mason } 2759be0e5c09SChris Mason 276074123bd7SChris Mason /* 276174123bd7SChris Mason * delete the item at the leaf level in path. If that empties 276274123bd7SChris Mason * the leaf, remove it from the tree 276374123bd7SChris Mason */ 276485e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 276585e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 2766be0e5c09SChris Mason { 27675f39d397SChris Mason struct extent_buffer *leaf; 27685f39d397SChris Mason struct btrfs_item *item; 276985e21bacSChris Mason int last_off; 277085e21bacSChris Mason int dsize = 0; 2771aa5d6bedSChris Mason int ret = 0; 2772aa5d6bedSChris Mason int wret; 277385e21bacSChris Mason int i; 27747518a238SChris Mason u32 nritems; 2775be0e5c09SChris Mason 27765f39d397SChris Mason leaf = path->nodes[0]; 277785e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 277885e21bacSChris Mason 277985e21bacSChris Mason for (i = 0; i < nr; i++) 278085e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 278185e21bacSChris Mason 27825f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 2783be0e5c09SChris Mason 278485e21bacSChris Mason if (slot + nr != nritems) { 2785be0e5c09SChris Mason int i; 2786123abc88SChris Mason int data_end = leaf_data_end(root, leaf); 27875f39d397SChris Mason 27885f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2789d6025579SChris Mason data_end + dsize, 2790123abc88SChris Mason btrfs_leaf_data(leaf) + data_end, 279185e21bacSChris Mason last_off - data_end); 27925f39d397SChris Mason 279385e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 27945f39d397SChris Mason u32 ioff; 2795db94535dSChris Mason 27965f39d397SChris Mason item = btrfs_item_nr(leaf, i); 2797db94535dSChris Mason if (!leaf->map_token) { 2798db94535dSChris Mason map_extent_buffer(leaf, (unsigned long)item, 2799db94535dSChris Mason sizeof(struct btrfs_item), 2800db94535dSChris Mason &leaf->map_token, &leaf->kaddr, 2801db94535dSChris Mason &leaf->map_start, &leaf->map_len, 2802db94535dSChris Mason KM_USER1); 2803db94535dSChris Mason } 28045f39d397SChris Mason ioff = btrfs_item_offset(leaf, item); 28055f39d397SChris Mason btrfs_set_item_offset(leaf, item, ioff + dsize); 28060783fcfcSChris Mason } 2807db94535dSChris Mason 2808db94535dSChris Mason if (leaf->map_token) { 2809db94535dSChris Mason unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2810db94535dSChris Mason leaf->map_token = NULL; 2811db94535dSChris Mason } 2812db94535dSChris Mason 28135f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 281485e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 28150783fcfcSChris Mason sizeof(struct btrfs_item) * 281685e21bacSChris Mason (nritems - slot - nr)); 2817be0e5c09SChris Mason } 281885e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 281985e21bacSChris Mason nritems -= nr; 28205f39d397SChris Mason 282174123bd7SChris Mason /* delete the leaf if we've emptied it */ 28227518a238SChris Mason if (nritems == 0) { 28235f39d397SChris Mason if (leaf == root->node) { 28245f39d397SChris Mason btrfs_set_header_level(leaf, 0); 28259a8dd150SChris Mason } else { 28267bb86316SChris Mason u64 root_gen = btrfs_header_generation(path->nodes[1]); 2827e089f05cSChris Mason wret = del_ptr(trans, root, path, 1, path->slots[1]); 2828aa5d6bedSChris Mason if (wret) 2829aa5d6bedSChris Mason ret = wret; 2830e089f05cSChris Mason wret = btrfs_free_extent(trans, root, 28317bb86316SChris Mason leaf->start, leaf->len, 28327bb86316SChris Mason btrfs_header_owner(path->nodes[1]), 28337bb86316SChris Mason root_gen, 0, 0, 1); 28340f70abe2SChris Mason if (wret) 28350f70abe2SChris Mason ret = wret; 28369a8dd150SChris Mason } 2837be0e5c09SChris Mason } else { 28387518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 2839aa5d6bedSChris Mason if (slot == 0) { 28405f39d397SChris Mason struct btrfs_disk_key disk_key; 28415f39d397SChris Mason 28425f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 2843e089f05cSChris Mason wret = fixup_low_keys(trans, root, path, 28445f39d397SChris Mason &disk_key, 1); 2845aa5d6bedSChris Mason if (wret) 2846aa5d6bedSChris Mason ret = wret; 2847aa5d6bedSChris Mason } 2848aa5d6bedSChris Mason 284974123bd7SChris Mason /* delete the leaf if it is mostly empty */ 285085e21bacSChris Mason if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) { 2851be0e5c09SChris Mason /* push_leaf_left fixes the path. 2852be0e5c09SChris Mason * make sure the path still points to our leaf 2853be0e5c09SChris Mason * for possible call to del_ptr below 2854be0e5c09SChris Mason */ 28554920c9acSChris Mason slot = path->slots[1]; 28565f39d397SChris Mason extent_buffer_get(leaf); 28575f39d397SChris Mason 285885e21bacSChris Mason wret = push_leaf_left(trans, root, path, 1, 1); 285954aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 2860aa5d6bedSChris Mason ret = wret; 28615f39d397SChris Mason 28625f39d397SChris Mason if (path->nodes[0] == leaf && 28635f39d397SChris Mason btrfs_header_nritems(leaf)) { 286485e21bacSChris Mason wret = push_leaf_right(trans, root, path, 1, 1); 286554aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 2866aa5d6bedSChris Mason ret = wret; 2867aa5d6bedSChris Mason } 28685f39d397SChris Mason 28695f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 28707bb86316SChris Mason u64 root_gen; 2871db94535dSChris Mason u64 bytenr = leaf->start; 2872db94535dSChris Mason u32 blocksize = leaf->len; 28735f39d397SChris Mason 28747bb86316SChris Mason root_gen = btrfs_header_generation( 28757bb86316SChris Mason path->nodes[1]); 28767bb86316SChris Mason 2877e089f05cSChris Mason wret = del_ptr(trans, root, path, 1, slot); 2878aa5d6bedSChris Mason if (wret) 2879aa5d6bedSChris Mason ret = wret; 28805f39d397SChris Mason 28815f39d397SChris Mason free_extent_buffer(leaf); 2882db94535dSChris Mason wret = btrfs_free_extent(trans, root, bytenr, 28837bb86316SChris Mason blocksize, 28847bb86316SChris Mason btrfs_header_owner(path->nodes[1]), 28857bb86316SChris Mason root_gen, 0, 0, 1); 28860f70abe2SChris Mason if (wret) 28870f70abe2SChris Mason ret = wret; 28885de08d7dSChris Mason } else { 2889*925baeddSChris Mason /* if we're still in the path, make sure 2890*925baeddSChris Mason * we're dirty. Otherwise, one of the 2891*925baeddSChris Mason * push_leaf functions must have already 2892*925baeddSChris Mason * dirtied this buffer 2893*925baeddSChris Mason */ 2894*925baeddSChris Mason if (path->nodes[0] == leaf) 28955f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 28965f39d397SChris Mason free_extent_buffer(leaf); 2897be0e5c09SChris Mason } 2898d5719762SChris Mason } else { 28995f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 2900be0e5c09SChris Mason } 29019a8dd150SChris Mason } 2902aa5d6bedSChris Mason return ret; 29039a8dd150SChris Mason } 29049a8dd150SChris Mason 290597571fd0SChris Mason /* 2906*925baeddSChris Mason * search the tree again to find a leaf with lesser keys 29077bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 29087bb86316SChris Mason * returns < 0 on io errors. 29097bb86316SChris Mason */ 29107bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 29117bb86316SChris Mason { 2912*925baeddSChris Mason struct btrfs_key key; 2913*925baeddSChris Mason struct btrfs_disk_key found_key; 2914*925baeddSChris Mason int ret; 29157bb86316SChris Mason 2916*925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 2917*925baeddSChris Mason 2918*925baeddSChris Mason if (key.offset > 0) 2919*925baeddSChris Mason key.offset--; 2920*925baeddSChris Mason else if (key.type > 0) 2921*925baeddSChris Mason key.type--; 2922*925baeddSChris Mason else if (key.objectid > 0) 2923*925baeddSChris Mason key.objectid--; 2924*925baeddSChris Mason else 29257bb86316SChris Mason return 1; 29267bb86316SChris Mason 2927*925baeddSChris Mason btrfs_release_path(root, path); 2928*925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2929*925baeddSChris Mason if (ret < 0) 2930*925baeddSChris Mason return ret; 2931*925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 2932*925baeddSChris Mason ret = comp_keys(&found_key, &key); 2933*925baeddSChris Mason if (ret < 0) 29347bb86316SChris Mason return 0; 2935*925baeddSChris Mason return 1; 29367bb86316SChris Mason } 29377bb86316SChris Mason 29387bb86316SChris Mason /* 2939*925baeddSChris Mason * search the tree again to find a leaf with greater keys 29400f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 29410f70abe2SChris Mason * returns < 0 on io errors. 294297571fd0SChris Mason */ 2943234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 2944d97e63b6SChris Mason { 2945d97e63b6SChris Mason int slot; 2946d97e63b6SChris Mason int level = 1; 29475f39d397SChris Mason struct extent_buffer *c; 29485f39d397SChris Mason struct extent_buffer *next = NULL; 2949*925baeddSChris Mason struct btrfs_key key; 2950*925baeddSChris Mason u32 nritems; 2951*925baeddSChris Mason int ret; 2952*925baeddSChris Mason 2953*925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 2954*925baeddSChris Mason if (nritems == 0) { 2955*925baeddSChris Mason return 1; 2956*925baeddSChris Mason } 2957*925baeddSChris Mason 2958*925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 2959*925baeddSChris Mason 2960*925baeddSChris Mason path->keep_locks = 1; 2961*925baeddSChris Mason btrfs_release_path(root, path); 2962*925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2963*925baeddSChris Mason path->keep_locks = 0; 2964*925baeddSChris Mason 2965*925baeddSChris Mason if (ret < 0) 2966*925baeddSChris Mason return ret; 2967*925baeddSChris Mason 2968*925baeddSChris Mason if (path->slots[0] < nritems - 1) { 2969*925baeddSChris Mason goto done; 2970*925baeddSChris Mason } 2971d97e63b6SChris Mason 2972234b63a0SChris Mason while(level < BTRFS_MAX_LEVEL) { 2973d97e63b6SChris Mason if (!path->nodes[level]) 29740f70abe2SChris Mason return 1; 29755f39d397SChris Mason 2976d97e63b6SChris Mason slot = path->slots[level] + 1; 2977d97e63b6SChris Mason c = path->nodes[level]; 29785f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 2979d97e63b6SChris Mason level++; 2980*925baeddSChris Mason if (level == BTRFS_MAX_LEVEL) { 29817bb86316SChris Mason return 1; 2982*925baeddSChris Mason } 2983d97e63b6SChris Mason continue; 2984d97e63b6SChris Mason } 29855f39d397SChris Mason 2986*925baeddSChris Mason if (next) { 2987*925baeddSChris Mason btrfs_tree_unlock(next); 29885f39d397SChris Mason free_extent_buffer(next); 2989*925baeddSChris Mason } 29905f39d397SChris Mason 2991*925baeddSChris Mason if (level == 1 && path->locks[1] && path->reada) 299201f46658SChris Mason reada_for_search(root, path, level, slot, 0); 29935f39d397SChris Mason 2994ca7a79adSChris Mason next = read_node_slot(root, c, slot); 2995*925baeddSChris Mason if (!path->skip_locking) 2996*925baeddSChris Mason btrfs_tree_lock(next); 2997d97e63b6SChris Mason break; 2998d97e63b6SChris Mason } 2999d97e63b6SChris Mason path->slots[level] = slot; 3000d97e63b6SChris Mason while(1) { 3001d97e63b6SChris Mason level--; 3002d97e63b6SChris Mason c = path->nodes[level]; 3003*925baeddSChris Mason if (path->locks[level]) 3004*925baeddSChris Mason btrfs_tree_unlock(c); 30055f39d397SChris Mason free_extent_buffer(c); 3006d97e63b6SChris Mason path->nodes[level] = next; 3007d97e63b6SChris Mason path->slots[level] = 0; 3008*925baeddSChris Mason path->locks[level] = 1; 3009d97e63b6SChris Mason if (!level) 3010d97e63b6SChris Mason break; 3011*925baeddSChris Mason if (level == 1 && path->locks[1] && path->reada) 3012*925baeddSChris Mason reada_for_search(root, path, level, slot, 0); 3013ca7a79adSChris Mason next = read_node_slot(root, next, 0); 3014*925baeddSChris Mason if (!path->skip_locking) 3015*925baeddSChris Mason btrfs_tree_lock(next); 3016d97e63b6SChris Mason } 3017*925baeddSChris Mason done: 3018*925baeddSChris Mason unlock_up(path, 0, 1); 3019d97e63b6SChris Mason return 0; 3020d97e63b6SChris Mason } 30210b86a832SChris Mason 30220b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 30230b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 30240b86a832SChris Mason int type) 30250b86a832SChris Mason { 30260b86a832SChris Mason struct btrfs_key found_key; 30270b86a832SChris Mason struct extent_buffer *leaf; 30280b86a832SChris Mason int ret; 30290b86a832SChris Mason 30300b86a832SChris Mason while(1) { 30310b86a832SChris Mason if (path->slots[0] == 0) { 30320b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 30330b86a832SChris Mason if (ret != 0) 30340b86a832SChris Mason return ret; 30350b86a832SChris Mason } else { 30360b86a832SChris Mason path->slots[0]--; 30370b86a832SChris Mason } 30380b86a832SChris Mason leaf = path->nodes[0]; 30390b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 30400b86a832SChris Mason if (found_key.type == type) 30410b86a832SChris Mason return 0; 30420b86a832SChris Mason } 30430b86a832SChris Mason return 1; 30440b86a832SChris Mason } 30450b86a832SChris Mason 3046