16cbd5570SChris Mason /* 2d352ac68SChris Mason * Copyright (C) 2007,2008 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 19a6b6e75eSChris Mason #include <linux/sched.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 21bd989ba3SJan Schmidt #include <linux/rbtree.h> 22eb60ceacSChris Mason #include "ctree.h" 23eb60ceacSChris Mason #include "disk-io.h" 247f5c1516SChris Mason #include "transaction.h" 255f39d397SChris Mason #include "print-tree.h" 26925baeddSChris Mason #include "locking.h" 279a8dd150SChris Mason 28e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 29e089f05cSChris Mason *root, struct btrfs_path *path, int level); 30e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root 31d4dbff95SChris Mason *root, struct btrfs_key *ins_key, 32cc0c5538SChris Mason struct btrfs_path *path, int data_size, int extend); 335f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 345f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 35971a1f66SChris Mason struct extent_buffer *src, int empty); 365f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 375f39d397SChris Mason struct btrfs_root *root, 385f39d397SChris Mason struct extent_buffer *dst_buf, 395f39d397SChris Mason struct extent_buffer *src_buf); 40afe5fea7STsutomu Itoh static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 41afe5fea7STsutomu Itoh int level, int slot); 42f230475eSJan Schmidt static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, 43f230475eSJan Schmidt struct extent_buffer *eb); 4448a3b636SEric Sandeen static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path); 45d97e63b6SChris Mason 462c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 472c90e5d6SChris Mason { 48df24a2b9SChris Mason struct btrfs_path *path; 49e00f7308SJeff Mahoney path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 50df24a2b9SChris Mason return path; 512c90e5d6SChris Mason } 522c90e5d6SChris Mason 53b4ce94deSChris Mason /* 54b4ce94deSChris Mason * set all locked nodes in the path to blocking locks. This should 55b4ce94deSChris Mason * be done before scheduling 56b4ce94deSChris Mason */ 57b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p) 58b4ce94deSChris Mason { 59b4ce94deSChris Mason int i; 60b4ce94deSChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 61bd681513SChris Mason if (!p->nodes[i] || !p->locks[i]) 62bd681513SChris Mason continue; 63bd681513SChris Mason btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]); 64bd681513SChris Mason if (p->locks[i] == BTRFS_READ_LOCK) 65bd681513SChris Mason p->locks[i] = BTRFS_READ_LOCK_BLOCKING; 66bd681513SChris Mason else if (p->locks[i] == BTRFS_WRITE_LOCK) 67bd681513SChris Mason p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; 68b4ce94deSChris Mason } 69b4ce94deSChris Mason } 70b4ce94deSChris Mason 71b4ce94deSChris Mason /* 72b4ce94deSChris Mason * reset all the locked nodes in the patch to spinning locks. 734008c04aSChris Mason * 744008c04aSChris Mason * held is used to keep lockdep happy, when lockdep is enabled 754008c04aSChris Mason * we set held to a blocking lock before we go around and 764008c04aSChris Mason * retake all the spinlocks in the path. You can safely use NULL 774008c04aSChris Mason * for held 78b4ce94deSChris Mason */ 794008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p, 80bd681513SChris Mason struct extent_buffer *held, int held_rw) 81b4ce94deSChris Mason { 82b4ce94deSChris Mason int i; 834008c04aSChris Mason 844008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 854008c04aSChris Mason /* lockdep really cares that we take all of these spinlocks 864008c04aSChris Mason * in the right order. If any of the locks in the path are not 874008c04aSChris Mason * currently blocking, it is going to complain. So, make really 884008c04aSChris Mason * really sure by forcing the path to blocking before we clear 894008c04aSChris Mason * the path blocking. 904008c04aSChris Mason */ 91bd681513SChris Mason if (held) { 92bd681513SChris Mason btrfs_set_lock_blocking_rw(held, held_rw); 93bd681513SChris Mason if (held_rw == BTRFS_WRITE_LOCK) 94bd681513SChris Mason held_rw = BTRFS_WRITE_LOCK_BLOCKING; 95bd681513SChris Mason else if (held_rw == BTRFS_READ_LOCK) 96bd681513SChris Mason held_rw = BTRFS_READ_LOCK_BLOCKING; 97bd681513SChris Mason } 984008c04aSChris Mason btrfs_set_path_blocking(p); 994008c04aSChris Mason #endif 1004008c04aSChris Mason 1014008c04aSChris Mason for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) { 102bd681513SChris Mason if (p->nodes[i] && p->locks[i]) { 103bd681513SChris Mason btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]); 104bd681513SChris Mason if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING) 105bd681513SChris Mason p->locks[i] = BTRFS_WRITE_LOCK; 106bd681513SChris Mason else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING) 107bd681513SChris Mason p->locks[i] = BTRFS_READ_LOCK; 108bd681513SChris Mason } 109b4ce94deSChris Mason } 1104008c04aSChris Mason 1114008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1124008c04aSChris Mason if (held) 113bd681513SChris Mason btrfs_clear_lock_blocking_rw(held, held_rw); 1144008c04aSChris Mason #endif 115b4ce94deSChris Mason } 116b4ce94deSChris Mason 117d352ac68SChris Mason /* this also releases the path */ 1182c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 1192c90e5d6SChris Mason { 120ff175d57SJesper Juhl if (!p) 121ff175d57SJesper Juhl return; 122b3b4aa74SDavid Sterba btrfs_release_path(p); 1232c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 1242c90e5d6SChris Mason } 1252c90e5d6SChris Mason 126d352ac68SChris Mason /* 127d352ac68SChris Mason * path release drops references on the extent buffers in the path 128d352ac68SChris Mason * and it drops any locks held by this path 129d352ac68SChris Mason * 130d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 131d352ac68SChris Mason */ 132b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p) 133eb60ceacSChris Mason { 134eb60ceacSChris Mason int i; 135a2135011SChris Mason 136234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 1373f157a2fSChris Mason p->slots[i] = 0; 138eb60ceacSChris Mason if (!p->nodes[i]) 139925baeddSChris Mason continue; 140925baeddSChris Mason if (p->locks[i]) { 141bd681513SChris Mason btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); 142925baeddSChris Mason p->locks[i] = 0; 143925baeddSChris Mason } 1445f39d397SChris Mason free_extent_buffer(p->nodes[i]); 1453f157a2fSChris Mason p->nodes[i] = NULL; 146eb60ceacSChris Mason } 147eb60ceacSChris Mason } 148eb60ceacSChris Mason 149d352ac68SChris Mason /* 150d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 151d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 152d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 153d352ac68SChris Mason * looping required. 154d352ac68SChris Mason * 155d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 156d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 157d352ac68SChris Mason * at any time because there are no locks held. 158d352ac68SChris Mason */ 159925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 160925baeddSChris Mason { 161925baeddSChris Mason struct extent_buffer *eb; 162240f62c8SChris Mason 1633083ee2eSJosef Bacik while (1) { 164240f62c8SChris Mason rcu_read_lock(); 165240f62c8SChris Mason eb = rcu_dereference(root->node); 1663083ee2eSJosef Bacik 1673083ee2eSJosef Bacik /* 1683083ee2eSJosef Bacik * RCU really hurts here, we could free up the root node because 1693083ee2eSJosef Bacik * it was cow'ed but we may not get the new root node yet so do 1703083ee2eSJosef Bacik * the inc_not_zero dance and if it doesn't work then 1713083ee2eSJosef Bacik * synchronize_rcu and try again. 1723083ee2eSJosef Bacik */ 1733083ee2eSJosef Bacik if (atomic_inc_not_zero(&eb->refs)) { 174240f62c8SChris Mason rcu_read_unlock(); 1753083ee2eSJosef Bacik break; 1763083ee2eSJosef Bacik } 1773083ee2eSJosef Bacik rcu_read_unlock(); 1783083ee2eSJosef Bacik synchronize_rcu(); 1793083ee2eSJosef Bacik } 180925baeddSChris Mason return eb; 181925baeddSChris Mason } 182925baeddSChris Mason 183d352ac68SChris Mason /* loop around taking references on and locking the root node of the 184d352ac68SChris Mason * tree until you end up with a lock on the root. A locked buffer 185d352ac68SChris Mason * is returned, with a reference held. 186d352ac68SChris Mason */ 187925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 188925baeddSChris Mason { 189925baeddSChris Mason struct extent_buffer *eb; 190925baeddSChris Mason 191925baeddSChris Mason while (1) { 192925baeddSChris Mason eb = btrfs_root_node(root); 193925baeddSChris Mason btrfs_tree_lock(eb); 194240f62c8SChris Mason if (eb == root->node) 195925baeddSChris Mason break; 196925baeddSChris Mason btrfs_tree_unlock(eb); 197925baeddSChris Mason free_extent_buffer(eb); 198925baeddSChris Mason } 199925baeddSChris Mason return eb; 200925baeddSChris Mason } 201925baeddSChris Mason 202bd681513SChris Mason /* loop around taking references on and locking the root node of the 203bd681513SChris Mason * tree until you end up with a lock on the root. A locked buffer 204bd681513SChris Mason * is returned, with a reference held. 205bd681513SChris Mason */ 20648a3b636SEric Sandeen static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root) 207bd681513SChris Mason { 208bd681513SChris Mason struct extent_buffer *eb; 209bd681513SChris Mason 210bd681513SChris Mason while (1) { 211bd681513SChris Mason eb = btrfs_root_node(root); 212bd681513SChris Mason btrfs_tree_read_lock(eb); 213bd681513SChris Mason if (eb == root->node) 214bd681513SChris Mason break; 215bd681513SChris Mason btrfs_tree_read_unlock(eb); 216bd681513SChris Mason free_extent_buffer(eb); 217bd681513SChris Mason } 218bd681513SChris Mason return eb; 219bd681513SChris Mason } 220bd681513SChris Mason 221d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get 222d352ac68SChris Mason * put onto a simple dirty list. transaction.c walks this to make sure they 223d352ac68SChris Mason * get properly updated on disk. 224d352ac68SChris Mason */ 2250b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 2260b86a832SChris Mason { 227e5846fc6SChris Mason spin_lock(&root->fs_info->trans_lock); 2280b86a832SChris Mason if (root->track_dirty && list_empty(&root->dirty_list)) { 2290b86a832SChris Mason list_add(&root->dirty_list, 2300b86a832SChris Mason &root->fs_info->dirty_cowonly_roots); 2310b86a832SChris Mason } 232e5846fc6SChris Mason spin_unlock(&root->fs_info->trans_lock); 2330b86a832SChris Mason } 2340b86a832SChris Mason 235d352ac68SChris Mason /* 236d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 237d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 238d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 239d352ac68SChris Mason */ 240be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 241be20aa9dSChris Mason struct btrfs_root *root, 242be20aa9dSChris Mason struct extent_buffer *buf, 243be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 244be20aa9dSChris Mason { 245be20aa9dSChris Mason struct extent_buffer *cow; 246be20aa9dSChris Mason int ret = 0; 247be20aa9dSChris Mason int level; 2485d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 249be20aa9dSChris Mason 250be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != 251be20aa9dSChris Mason root->fs_info->running_transaction->transid); 252be20aa9dSChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 253be20aa9dSChris Mason 254be20aa9dSChris Mason level = btrfs_header_level(buf); 2555d4f98a2SYan Zheng if (level == 0) 2565d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 2575d4f98a2SYan Zheng else 2585d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 25931840ae1SZheng Yan 2605d4f98a2SYan Zheng cow = btrfs_alloc_free_block(trans, root, buf->len, 0, 2615d4f98a2SYan Zheng new_root_objectid, &disk_key, level, 2625581a51aSJan Schmidt buf->start, 0); 2635d4f98a2SYan Zheng if (IS_ERR(cow)) 264be20aa9dSChris Mason return PTR_ERR(cow); 265be20aa9dSChris Mason 266be20aa9dSChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 267be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 268be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 2695d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 2705d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 2715d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 2725d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 2735d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 2745d4f98a2SYan Zheng else 275be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 276be20aa9dSChris Mason 2772b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 2782b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 2792b82032cSYan Zheng BTRFS_FSID_SIZE); 2802b82032cSYan Zheng 281be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 2825d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 28366d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, cow, 1, 1); 2845d4f98a2SYan Zheng else 28566d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, cow, 0, 1); 2864aec2b52SChris Mason 287be20aa9dSChris Mason if (ret) 288be20aa9dSChris Mason return ret; 289be20aa9dSChris Mason 290be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 291be20aa9dSChris Mason *cow_ret = cow; 292be20aa9dSChris Mason return 0; 293be20aa9dSChris Mason } 294be20aa9dSChris Mason 295bd989ba3SJan Schmidt enum mod_log_op { 296bd989ba3SJan Schmidt MOD_LOG_KEY_REPLACE, 297bd989ba3SJan Schmidt MOD_LOG_KEY_ADD, 298bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE, 299bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_FREEING, 300bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_MOVING, 301bd989ba3SJan Schmidt MOD_LOG_MOVE_KEYS, 302bd989ba3SJan Schmidt MOD_LOG_ROOT_REPLACE, 303bd989ba3SJan Schmidt }; 304bd989ba3SJan Schmidt 305bd989ba3SJan Schmidt struct tree_mod_move { 306bd989ba3SJan Schmidt int dst_slot; 307bd989ba3SJan Schmidt int nr_items; 308bd989ba3SJan Schmidt }; 309bd989ba3SJan Schmidt 310bd989ba3SJan Schmidt struct tree_mod_root { 311bd989ba3SJan Schmidt u64 logical; 312bd989ba3SJan Schmidt u8 level; 313bd989ba3SJan Schmidt }; 314bd989ba3SJan Schmidt 315bd989ba3SJan Schmidt struct tree_mod_elem { 316bd989ba3SJan Schmidt struct rb_node node; 317bd989ba3SJan Schmidt u64 index; /* shifted logical */ 318097b8a7cSJan Schmidt u64 seq; 319bd989ba3SJan Schmidt enum mod_log_op op; 320bd989ba3SJan Schmidt 321bd989ba3SJan Schmidt /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ 322bd989ba3SJan Schmidt int slot; 323bd989ba3SJan Schmidt 324bd989ba3SJan Schmidt /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ 325bd989ba3SJan Schmidt u64 generation; 326bd989ba3SJan Schmidt 327bd989ba3SJan Schmidt /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ 328bd989ba3SJan Schmidt struct btrfs_disk_key key; 329bd989ba3SJan Schmidt u64 blockptr; 330bd989ba3SJan Schmidt 331bd989ba3SJan Schmidt /* this is used for op == MOD_LOG_MOVE_KEYS */ 332bd989ba3SJan Schmidt struct tree_mod_move move; 333bd989ba3SJan Schmidt 334bd989ba3SJan Schmidt /* this is used for op == MOD_LOG_ROOT_REPLACE */ 335bd989ba3SJan Schmidt struct tree_mod_root old_root; 336bd989ba3SJan Schmidt }; 337bd989ba3SJan Schmidt 338097b8a7cSJan Schmidt static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info) 339bd989ba3SJan Schmidt { 340097b8a7cSJan Schmidt read_lock(&fs_info->tree_mod_log_lock); 341bd989ba3SJan Schmidt } 342bd989ba3SJan Schmidt 343097b8a7cSJan Schmidt static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info) 344097b8a7cSJan Schmidt { 345097b8a7cSJan Schmidt read_unlock(&fs_info->tree_mod_log_lock); 346097b8a7cSJan Schmidt } 347097b8a7cSJan Schmidt 348097b8a7cSJan Schmidt static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info) 349097b8a7cSJan Schmidt { 350097b8a7cSJan Schmidt write_lock(&fs_info->tree_mod_log_lock); 351097b8a7cSJan Schmidt } 352097b8a7cSJan Schmidt 353097b8a7cSJan Schmidt static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info) 354097b8a7cSJan Schmidt { 355097b8a7cSJan Schmidt write_unlock(&fs_info->tree_mod_log_lock); 356097b8a7cSJan Schmidt } 357097b8a7cSJan Schmidt 358097b8a7cSJan Schmidt /* 359fc36ed7eSJan Schmidt * Increment the upper half of tree_mod_seq, set lower half zero. 360fc36ed7eSJan Schmidt * 361fc36ed7eSJan Schmidt * Must be called with fs_info->tree_mod_seq_lock held. 362fc36ed7eSJan Schmidt */ 363fc36ed7eSJan Schmidt static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info) 364fc36ed7eSJan Schmidt { 365fc36ed7eSJan Schmidt u64 seq = atomic64_read(&fs_info->tree_mod_seq); 366fc36ed7eSJan Schmidt seq &= 0xffffffff00000000ull; 367fc36ed7eSJan Schmidt seq += 1ull << 32; 368fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, seq); 369fc36ed7eSJan Schmidt return seq; 370fc36ed7eSJan Schmidt } 371fc36ed7eSJan Schmidt 372fc36ed7eSJan Schmidt /* 373fc36ed7eSJan Schmidt * Increment the lower half of tree_mod_seq. 374fc36ed7eSJan Schmidt * 375fc36ed7eSJan Schmidt * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers 376fc36ed7eSJan Schmidt * are generated should not technically require a spin lock here. (Rationale: 377fc36ed7eSJan Schmidt * incrementing the minor while incrementing the major seq number is between its 378fc36ed7eSJan Schmidt * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it 379fc36ed7eSJan Schmidt * just returns a unique sequence number as usual.) We have decided to leave 380fc36ed7eSJan Schmidt * that requirement in here and rethink it once we notice it really imposes a 381fc36ed7eSJan Schmidt * problem on some workload. 382fc36ed7eSJan Schmidt */ 383fc36ed7eSJan Schmidt static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info) 384fc36ed7eSJan Schmidt { 385fc36ed7eSJan Schmidt return atomic64_inc_return(&fs_info->tree_mod_seq); 386fc36ed7eSJan Schmidt } 387fc36ed7eSJan Schmidt 388fc36ed7eSJan Schmidt /* 389fc36ed7eSJan Schmidt * return the last minor in the previous major tree_mod_seq number 390fc36ed7eSJan Schmidt */ 391fc36ed7eSJan Schmidt u64 btrfs_tree_mod_seq_prev(u64 seq) 392fc36ed7eSJan Schmidt { 393fc36ed7eSJan Schmidt return (seq & 0xffffffff00000000ull) - 1ull; 394fc36ed7eSJan Schmidt } 395fc36ed7eSJan Schmidt 396fc36ed7eSJan Schmidt /* 397097b8a7cSJan Schmidt * This adds a new blocker to the tree mod log's blocker list if the @elem 398097b8a7cSJan Schmidt * passed does not already have a sequence number set. So when a caller expects 399097b8a7cSJan Schmidt * to record tree modifications, it should ensure to set elem->seq to zero 400097b8a7cSJan Schmidt * before calling btrfs_get_tree_mod_seq. 401097b8a7cSJan Schmidt * Returns a fresh, unused tree log modification sequence number, even if no new 402097b8a7cSJan Schmidt * blocker was added. 403097b8a7cSJan Schmidt */ 404097b8a7cSJan Schmidt u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, 405bd989ba3SJan Schmidt struct seq_list *elem) 406bd989ba3SJan Schmidt { 407097b8a7cSJan Schmidt u64 seq; 408097b8a7cSJan Schmidt 409097b8a7cSJan Schmidt tree_mod_log_write_lock(fs_info); 410bd989ba3SJan Schmidt spin_lock(&fs_info->tree_mod_seq_lock); 411097b8a7cSJan Schmidt if (!elem->seq) { 412fc36ed7eSJan Schmidt elem->seq = btrfs_inc_tree_mod_seq_major(fs_info); 413097b8a7cSJan Schmidt list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); 414097b8a7cSJan Schmidt } 415fc36ed7eSJan Schmidt seq = btrfs_inc_tree_mod_seq_minor(fs_info); 416bd989ba3SJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 417097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 418097b8a7cSJan Schmidt 419097b8a7cSJan Schmidt return seq; 420bd989ba3SJan Schmidt } 421bd989ba3SJan Schmidt 422bd989ba3SJan Schmidt void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, 423bd989ba3SJan Schmidt struct seq_list *elem) 424bd989ba3SJan Schmidt { 425bd989ba3SJan Schmidt struct rb_root *tm_root; 426bd989ba3SJan Schmidt struct rb_node *node; 427bd989ba3SJan Schmidt struct rb_node *next; 428bd989ba3SJan Schmidt struct seq_list *cur_elem; 429bd989ba3SJan Schmidt struct tree_mod_elem *tm; 430bd989ba3SJan Schmidt u64 min_seq = (u64)-1; 431bd989ba3SJan Schmidt u64 seq_putting = elem->seq; 432bd989ba3SJan Schmidt 433bd989ba3SJan Schmidt if (!seq_putting) 434bd989ba3SJan Schmidt return; 435bd989ba3SJan Schmidt 436bd989ba3SJan Schmidt spin_lock(&fs_info->tree_mod_seq_lock); 437bd989ba3SJan Schmidt list_del(&elem->list); 438097b8a7cSJan Schmidt elem->seq = 0; 439bd989ba3SJan Schmidt 440bd989ba3SJan Schmidt list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) { 441097b8a7cSJan Schmidt if (cur_elem->seq < min_seq) { 442bd989ba3SJan Schmidt if (seq_putting > cur_elem->seq) { 443bd989ba3SJan Schmidt /* 444bd989ba3SJan Schmidt * blocker with lower sequence number exists, we 445bd989ba3SJan Schmidt * cannot remove anything from the log 446bd989ba3SJan Schmidt */ 447097b8a7cSJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 448097b8a7cSJan Schmidt return; 449bd989ba3SJan Schmidt } 450bd989ba3SJan Schmidt min_seq = cur_elem->seq; 451bd989ba3SJan Schmidt } 452bd989ba3SJan Schmidt } 453097b8a7cSJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 454097b8a7cSJan Schmidt 455097b8a7cSJan Schmidt /* 456bd989ba3SJan Schmidt * anything that's lower than the lowest existing (read: blocked) 457bd989ba3SJan Schmidt * sequence number can be removed from the tree. 458bd989ba3SJan Schmidt */ 459097b8a7cSJan Schmidt tree_mod_log_write_lock(fs_info); 460bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 461bd989ba3SJan Schmidt for (node = rb_first(tm_root); node; node = next) { 462bd989ba3SJan Schmidt next = rb_next(node); 463bd989ba3SJan Schmidt tm = container_of(node, struct tree_mod_elem, node); 464097b8a7cSJan Schmidt if (tm->seq > min_seq) 465bd989ba3SJan Schmidt continue; 466bd989ba3SJan Schmidt rb_erase(node, tm_root); 467bd989ba3SJan Schmidt kfree(tm); 468bd989ba3SJan Schmidt } 469097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 470bd989ba3SJan Schmidt } 471bd989ba3SJan Schmidt 472bd989ba3SJan Schmidt /* 473bd989ba3SJan Schmidt * key order of the log: 474bd989ba3SJan Schmidt * index -> sequence 475bd989ba3SJan Schmidt * 476bd989ba3SJan Schmidt * the index is the shifted logical of the *new* root node for root replace 477bd989ba3SJan Schmidt * operations, or the shifted logical of the affected block for all other 478bd989ba3SJan Schmidt * operations. 479bd989ba3SJan Schmidt */ 480bd989ba3SJan Schmidt static noinline int 481bd989ba3SJan Schmidt __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) 482bd989ba3SJan Schmidt { 483bd989ba3SJan Schmidt struct rb_root *tm_root; 484bd989ba3SJan Schmidt struct rb_node **new; 485bd989ba3SJan Schmidt struct rb_node *parent = NULL; 486bd989ba3SJan Schmidt struct tree_mod_elem *cur; 487bd989ba3SJan Schmidt 488097b8a7cSJan Schmidt BUG_ON(!tm || !tm->seq); 489bd989ba3SJan Schmidt 490bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 491bd989ba3SJan Schmidt new = &tm_root->rb_node; 492bd989ba3SJan Schmidt while (*new) { 493bd989ba3SJan Schmidt cur = container_of(*new, struct tree_mod_elem, node); 494bd989ba3SJan Schmidt parent = *new; 495bd989ba3SJan Schmidt if (cur->index < tm->index) 496bd989ba3SJan Schmidt new = &((*new)->rb_left); 497bd989ba3SJan Schmidt else if (cur->index > tm->index) 498bd989ba3SJan Schmidt new = &((*new)->rb_right); 499097b8a7cSJan Schmidt else if (cur->seq < tm->seq) 500bd989ba3SJan Schmidt new = &((*new)->rb_left); 501097b8a7cSJan Schmidt else if (cur->seq > tm->seq) 502bd989ba3SJan Schmidt new = &((*new)->rb_right); 503bd989ba3SJan Schmidt else { 504bd989ba3SJan Schmidt kfree(tm); 505097b8a7cSJan Schmidt return -EEXIST; 506bd989ba3SJan Schmidt } 507bd989ba3SJan Schmidt } 508bd989ba3SJan Schmidt 509bd989ba3SJan Schmidt rb_link_node(&tm->node, parent, new); 510bd989ba3SJan Schmidt rb_insert_color(&tm->node, tm_root); 511097b8a7cSJan Schmidt return 0; 512bd989ba3SJan Schmidt } 513bd989ba3SJan Schmidt 514097b8a7cSJan Schmidt /* 515097b8a7cSJan Schmidt * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it 516097b8a7cSJan Schmidt * returns zero with the tree_mod_log_lock acquired. The caller must hold 517097b8a7cSJan Schmidt * this until all tree mod log insertions are recorded in the rb tree and then 518097b8a7cSJan Schmidt * call tree_mod_log_write_unlock() to release. 519097b8a7cSJan Schmidt */ 520e9b7fd4dSJan Schmidt static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, 521e9b7fd4dSJan Schmidt struct extent_buffer *eb) { 522e9b7fd4dSJan Schmidt smp_mb(); 523e9b7fd4dSJan Schmidt if (list_empty(&(fs_info)->tree_mod_seq_list)) 524e9b7fd4dSJan Schmidt return 1; 525097b8a7cSJan Schmidt if (eb && btrfs_header_level(eb) == 0) 526e9b7fd4dSJan Schmidt return 1; 527097b8a7cSJan Schmidt 528097b8a7cSJan Schmidt tree_mod_log_write_lock(fs_info); 529097b8a7cSJan Schmidt if (list_empty(&fs_info->tree_mod_seq_list)) { 530097b8a7cSJan Schmidt /* 531097b8a7cSJan Schmidt * someone emptied the list while we were waiting for the lock. 532097b8a7cSJan Schmidt * we must not add to the list when no blocker exists. 533097b8a7cSJan Schmidt */ 534097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 535097b8a7cSJan Schmidt return 1; 536097b8a7cSJan Schmidt } 537097b8a7cSJan Schmidt 538e9b7fd4dSJan Schmidt return 0; 539e9b7fd4dSJan Schmidt } 540e9b7fd4dSJan Schmidt 5413310c36eSJan Schmidt /* 542097b8a7cSJan Schmidt * This allocates memory and gets a tree modification sequence number. 5433310c36eSJan Schmidt * 544097b8a7cSJan Schmidt * Returns <0 on error. 545097b8a7cSJan Schmidt * Returns >0 (the added sequence number) on success. 5463310c36eSJan Schmidt */ 547926dd8a6SJan Schmidt static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags, 548bd989ba3SJan Schmidt struct tree_mod_elem **tm_ret) 549bd989ba3SJan Schmidt { 550bd989ba3SJan Schmidt struct tree_mod_elem *tm; 551bd989ba3SJan Schmidt 552097b8a7cSJan Schmidt /* 553097b8a7cSJan Schmidt * once we switch from spin locks to something different, we should 554097b8a7cSJan Schmidt * honor the flags parameter here. 555097b8a7cSJan Schmidt */ 556097b8a7cSJan Schmidt tm = *tm_ret = kzalloc(sizeof(*tm), GFP_ATOMIC); 557bd989ba3SJan Schmidt if (!tm) 558bd989ba3SJan Schmidt return -ENOMEM; 559bd989ba3SJan Schmidt 560fc36ed7eSJan Schmidt spin_lock(&fs_info->tree_mod_seq_lock); 561fc36ed7eSJan Schmidt tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info); 562fc36ed7eSJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 563fc36ed7eSJan Schmidt 564097b8a7cSJan Schmidt return tm->seq; 565926dd8a6SJan Schmidt } 566bd989ba3SJan Schmidt 567097b8a7cSJan Schmidt static inline int 568097b8a7cSJan Schmidt __tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, 569bd989ba3SJan Schmidt struct extent_buffer *eb, int slot, 570bd989ba3SJan Schmidt enum mod_log_op op, gfp_t flags) 571bd989ba3SJan Schmidt { 572bd989ba3SJan Schmidt int ret; 573097b8a7cSJan Schmidt struct tree_mod_elem *tm; 574bd989ba3SJan Schmidt 575bd989ba3SJan Schmidt ret = tree_mod_alloc(fs_info, flags, &tm); 576097b8a7cSJan Schmidt if (ret < 0) 577bd989ba3SJan Schmidt return ret; 578bd989ba3SJan Schmidt 579bd989ba3SJan Schmidt tm->index = eb->start >> PAGE_CACHE_SHIFT; 580bd989ba3SJan Schmidt if (op != MOD_LOG_KEY_ADD) { 581bd989ba3SJan Schmidt btrfs_node_key(eb, &tm->key, slot); 582bd989ba3SJan Schmidt tm->blockptr = btrfs_node_blockptr(eb, slot); 583bd989ba3SJan Schmidt } 584bd989ba3SJan Schmidt tm->op = op; 585bd989ba3SJan Schmidt tm->slot = slot; 586bd989ba3SJan Schmidt tm->generation = btrfs_node_ptr_generation(eb, slot); 587bd989ba3SJan Schmidt 588097b8a7cSJan Schmidt return __tree_mod_log_insert(fs_info, tm); 589097b8a7cSJan Schmidt } 590097b8a7cSJan Schmidt 591097b8a7cSJan Schmidt static noinline int 592097b8a7cSJan Schmidt tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info, 593097b8a7cSJan Schmidt struct extent_buffer *eb, int slot, 594097b8a7cSJan Schmidt enum mod_log_op op, gfp_t flags) 595097b8a7cSJan Schmidt { 596097b8a7cSJan Schmidt int ret; 597097b8a7cSJan Schmidt 598097b8a7cSJan Schmidt if (tree_mod_dont_log(fs_info, eb)) 599097b8a7cSJan Schmidt return 0; 600097b8a7cSJan Schmidt 601097b8a7cSJan Schmidt ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags); 602097b8a7cSJan Schmidt 603097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 6043310c36eSJan Schmidt return ret; 605bd989ba3SJan Schmidt } 606bd989ba3SJan Schmidt 607bd989ba3SJan Schmidt static noinline int 608bd989ba3SJan Schmidt tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, 609bd989ba3SJan Schmidt int slot, enum mod_log_op op) 610bd989ba3SJan Schmidt { 611bd989ba3SJan Schmidt return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS); 612bd989ba3SJan Schmidt } 613bd989ba3SJan Schmidt 614bd989ba3SJan Schmidt static noinline int 615097b8a7cSJan Schmidt tree_mod_log_insert_key_locked(struct btrfs_fs_info *fs_info, 616097b8a7cSJan Schmidt struct extent_buffer *eb, int slot, 617097b8a7cSJan Schmidt enum mod_log_op op) 618097b8a7cSJan Schmidt { 619097b8a7cSJan Schmidt return __tree_mod_log_insert_key(fs_info, eb, slot, op, GFP_NOFS); 620097b8a7cSJan Schmidt } 621097b8a7cSJan Schmidt 622097b8a7cSJan Schmidt static noinline int 623bd989ba3SJan Schmidt tree_mod_log_insert_move(struct btrfs_fs_info *fs_info, 624bd989ba3SJan Schmidt struct extent_buffer *eb, int dst_slot, int src_slot, 625bd989ba3SJan Schmidt int nr_items, gfp_t flags) 626bd989ba3SJan Schmidt { 627bd989ba3SJan Schmidt struct tree_mod_elem *tm; 628bd989ba3SJan Schmidt int ret; 629bd989ba3SJan Schmidt int i; 630bd989ba3SJan Schmidt 631f395694cSJan Schmidt if (tree_mod_dont_log(fs_info, eb)) 632f395694cSJan Schmidt return 0; 633bd989ba3SJan Schmidt 63401763a2eSJan Schmidt /* 63501763a2eSJan Schmidt * When we override something during the move, we log these removals. 63601763a2eSJan Schmidt * This can only happen when we move towards the beginning of the 63701763a2eSJan Schmidt * buffer, i.e. dst_slot < src_slot. 63801763a2eSJan Schmidt */ 639bd989ba3SJan Schmidt for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { 640097b8a7cSJan Schmidt ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot, 641bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_MOVING); 642bd989ba3SJan Schmidt BUG_ON(ret < 0); 643bd989ba3SJan Schmidt } 644bd989ba3SJan Schmidt 645f395694cSJan Schmidt ret = tree_mod_alloc(fs_info, flags, &tm); 646097b8a7cSJan Schmidt if (ret < 0) 647097b8a7cSJan Schmidt goto out; 648f395694cSJan Schmidt 649bd989ba3SJan Schmidt tm->index = eb->start >> PAGE_CACHE_SHIFT; 650bd989ba3SJan Schmidt tm->slot = src_slot; 651bd989ba3SJan Schmidt tm->move.dst_slot = dst_slot; 652bd989ba3SJan Schmidt tm->move.nr_items = nr_items; 653bd989ba3SJan Schmidt tm->op = MOD_LOG_MOVE_KEYS; 654bd989ba3SJan Schmidt 6553310c36eSJan Schmidt ret = __tree_mod_log_insert(fs_info, tm); 656097b8a7cSJan Schmidt out: 657097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 6583310c36eSJan Schmidt return ret; 659bd989ba3SJan Schmidt } 660bd989ba3SJan Schmidt 661097b8a7cSJan Schmidt static inline void 662097b8a7cSJan Schmidt __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb) 663097b8a7cSJan Schmidt { 664097b8a7cSJan Schmidt int i; 665097b8a7cSJan Schmidt u32 nritems; 666097b8a7cSJan Schmidt int ret; 667097b8a7cSJan Schmidt 668b12a3b1eSChris Mason if (btrfs_header_level(eb) == 0) 669b12a3b1eSChris Mason return; 670b12a3b1eSChris Mason 671097b8a7cSJan Schmidt nritems = btrfs_header_nritems(eb); 672097b8a7cSJan Schmidt for (i = nritems - 1; i >= 0; i--) { 673097b8a7cSJan Schmidt ret = tree_mod_log_insert_key_locked(fs_info, eb, i, 674097b8a7cSJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_FREEING); 675097b8a7cSJan Schmidt BUG_ON(ret < 0); 676097b8a7cSJan Schmidt } 677097b8a7cSJan Schmidt } 678097b8a7cSJan Schmidt 679bd989ba3SJan Schmidt static noinline int 680bd989ba3SJan Schmidt tree_mod_log_insert_root(struct btrfs_fs_info *fs_info, 681bd989ba3SJan Schmidt struct extent_buffer *old_root, 68290f8d62eSJan Schmidt struct extent_buffer *new_root, gfp_t flags, 68390f8d62eSJan Schmidt int log_removal) 684bd989ba3SJan Schmidt { 685bd989ba3SJan Schmidt struct tree_mod_elem *tm; 686bd989ba3SJan Schmidt int ret; 687bd989ba3SJan Schmidt 688097b8a7cSJan Schmidt if (tree_mod_dont_log(fs_info, NULL)) 689097b8a7cSJan Schmidt return 0; 690097b8a7cSJan Schmidt 69190f8d62eSJan Schmidt if (log_removal) 692d9abbf1cSJan Schmidt __tree_mod_log_free_eb(fs_info, old_root); 693d9abbf1cSJan Schmidt 694bd989ba3SJan Schmidt ret = tree_mod_alloc(fs_info, flags, &tm); 695097b8a7cSJan Schmidt if (ret < 0) 696097b8a7cSJan Schmidt goto out; 697bd989ba3SJan Schmidt 698bd989ba3SJan Schmidt tm->index = new_root->start >> PAGE_CACHE_SHIFT; 699bd989ba3SJan Schmidt tm->old_root.logical = old_root->start; 700bd989ba3SJan Schmidt tm->old_root.level = btrfs_header_level(old_root); 701bd989ba3SJan Schmidt tm->generation = btrfs_header_generation(old_root); 702bd989ba3SJan Schmidt tm->op = MOD_LOG_ROOT_REPLACE; 703bd989ba3SJan Schmidt 7043310c36eSJan Schmidt ret = __tree_mod_log_insert(fs_info, tm); 705097b8a7cSJan Schmidt out: 706097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 7073310c36eSJan Schmidt return ret; 708bd989ba3SJan Schmidt } 709bd989ba3SJan Schmidt 710bd989ba3SJan Schmidt static struct tree_mod_elem * 711bd989ba3SJan Schmidt __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, 712bd989ba3SJan Schmidt int smallest) 713bd989ba3SJan Schmidt { 714bd989ba3SJan Schmidt struct rb_root *tm_root; 715bd989ba3SJan Schmidt struct rb_node *node; 716bd989ba3SJan Schmidt struct tree_mod_elem *cur = NULL; 717bd989ba3SJan Schmidt struct tree_mod_elem *found = NULL; 718bd989ba3SJan Schmidt u64 index = start >> PAGE_CACHE_SHIFT; 719bd989ba3SJan Schmidt 720097b8a7cSJan Schmidt tree_mod_log_read_lock(fs_info); 721bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 722bd989ba3SJan Schmidt node = tm_root->rb_node; 723bd989ba3SJan Schmidt while (node) { 724bd989ba3SJan Schmidt cur = container_of(node, struct tree_mod_elem, node); 725bd989ba3SJan Schmidt if (cur->index < index) { 726bd989ba3SJan Schmidt node = node->rb_left; 727bd989ba3SJan Schmidt } else if (cur->index > index) { 728bd989ba3SJan Schmidt node = node->rb_right; 729097b8a7cSJan Schmidt } else if (cur->seq < min_seq) { 730bd989ba3SJan Schmidt node = node->rb_left; 731bd989ba3SJan Schmidt } else if (!smallest) { 732bd989ba3SJan Schmidt /* we want the node with the highest seq */ 733bd989ba3SJan Schmidt if (found) 734097b8a7cSJan Schmidt BUG_ON(found->seq > cur->seq); 735bd989ba3SJan Schmidt found = cur; 736bd989ba3SJan Schmidt node = node->rb_left; 737097b8a7cSJan Schmidt } else if (cur->seq > min_seq) { 738bd989ba3SJan Schmidt /* we want the node with the smallest seq */ 739bd989ba3SJan Schmidt if (found) 740097b8a7cSJan Schmidt BUG_ON(found->seq < cur->seq); 741bd989ba3SJan Schmidt found = cur; 742bd989ba3SJan Schmidt node = node->rb_right; 743bd989ba3SJan Schmidt } else { 744bd989ba3SJan Schmidt found = cur; 745bd989ba3SJan Schmidt break; 746bd989ba3SJan Schmidt } 747bd989ba3SJan Schmidt } 748097b8a7cSJan Schmidt tree_mod_log_read_unlock(fs_info); 749bd989ba3SJan Schmidt 750bd989ba3SJan Schmidt return found; 751bd989ba3SJan Schmidt } 752bd989ba3SJan Schmidt 753bd989ba3SJan Schmidt /* 754bd989ba3SJan Schmidt * this returns the element from the log with the smallest time sequence 755bd989ba3SJan Schmidt * value that's in the log (the oldest log item). any element with a time 756bd989ba3SJan Schmidt * sequence lower than min_seq will be ignored. 757bd989ba3SJan Schmidt */ 758bd989ba3SJan Schmidt static struct tree_mod_elem * 759bd989ba3SJan Schmidt tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, 760bd989ba3SJan Schmidt u64 min_seq) 761bd989ba3SJan Schmidt { 762bd989ba3SJan Schmidt return __tree_mod_log_search(fs_info, start, min_seq, 1); 763bd989ba3SJan Schmidt } 764bd989ba3SJan Schmidt 765bd989ba3SJan Schmidt /* 766bd989ba3SJan Schmidt * this returns the element from the log with the largest time sequence 767bd989ba3SJan Schmidt * value that's in the log (the most recent log item). any element with 768bd989ba3SJan Schmidt * a time sequence lower than min_seq will be ignored. 769bd989ba3SJan Schmidt */ 770bd989ba3SJan Schmidt static struct tree_mod_elem * 771bd989ba3SJan Schmidt tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) 772bd989ba3SJan Schmidt { 773bd989ba3SJan Schmidt return __tree_mod_log_search(fs_info, start, min_seq, 0); 774bd989ba3SJan Schmidt } 775bd989ba3SJan Schmidt 776097b8a7cSJan Schmidt static noinline void 777bd989ba3SJan Schmidt tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, 778bd989ba3SJan Schmidt struct extent_buffer *src, unsigned long dst_offset, 77990f8d62eSJan Schmidt unsigned long src_offset, int nr_items) 780bd989ba3SJan Schmidt { 781bd989ba3SJan Schmidt int ret; 782bd989ba3SJan Schmidt int i; 783bd989ba3SJan Schmidt 784e9b7fd4dSJan Schmidt if (tree_mod_dont_log(fs_info, NULL)) 785bd989ba3SJan Schmidt return; 786bd989ba3SJan Schmidt 787097b8a7cSJan Schmidt if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) { 788097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 789bd989ba3SJan Schmidt return; 790097b8a7cSJan Schmidt } 791bd989ba3SJan Schmidt 792bd989ba3SJan Schmidt for (i = 0; i < nr_items; i++) { 793097b8a7cSJan Schmidt ret = tree_mod_log_insert_key_locked(fs_info, src, 794097b8a7cSJan Schmidt i + src_offset, 795bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE); 796bd989ba3SJan Schmidt BUG_ON(ret < 0); 797097b8a7cSJan Schmidt ret = tree_mod_log_insert_key_locked(fs_info, dst, 798097b8a7cSJan Schmidt i + dst_offset, 799bd989ba3SJan Schmidt MOD_LOG_KEY_ADD); 800bd989ba3SJan Schmidt BUG_ON(ret < 0); 801bd989ba3SJan Schmidt } 802097b8a7cSJan Schmidt 803097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 804bd989ba3SJan Schmidt } 805bd989ba3SJan Schmidt 806bd989ba3SJan Schmidt static inline void 807bd989ba3SJan Schmidt tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, 808bd989ba3SJan Schmidt int dst_offset, int src_offset, int nr_items) 809bd989ba3SJan Schmidt { 810bd989ba3SJan Schmidt int ret; 811bd989ba3SJan Schmidt ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset, 812bd989ba3SJan Schmidt nr_items, GFP_NOFS); 813bd989ba3SJan Schmidt BUG_ON(ret < 0); 814bd989ba3SJan Schmidt } 815bd989ba3SJan Schmidt 816097b8a7cSJan Schmidt static noinline void 817bd989ba3SJan Schmidt tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info, 81832adf090SLiu Bo struct extent_buffer *eb, int slot, int atomic) 819bd989ba3SJan Schmidt { 820bd989ba3SJan Schmidt int ret; 821bd989ba3SJan Schmidt 822bd989ba3SJan Schmidt ret = tree_mod_log_insert_key_mask(fs_info, eb, slot, 823bd989ba3SJan Schmidt MOD_LOG_KEY_REPLACE, 824bd989ba3SJan Schmidt atomic ? GFP_ATOMIC : GFP_NOFS); 825bd989ba3SJan Schmidt BUG_ON(ret < 0); 826bd989ba3SJan Schmidt } 827bd989ba3SJan Schmidt 828097b8a7cSJan Schmidt static noinline void 829097b8a7cSJan Schmidt tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb) 830bd989ba3SJan Schmidt { 831e9b7fd4dSJan Schmidt if (tree_mod_dont_log(fs_info, eb)) 832bd989ba3SJan Schmidt return; 833bd989ba3SJan Schmidt 834097b8a7cSJan Schmidt __tree_mod_log_free_eb(fs_info, eb); 835097b8a7cSJan Schmidt 836097b8a7cSJan Schmidt tree_mod_log_write_unlock(fs_info); 837bd989ba3SJan Schmidt } 838bd989ba3SJan Schmidt 839097b8a7cSJan Schmidt static noinline void 840bd989ba3SJan Schmidt tree_mod_log_set_root_pointer(struct btrfs_root *root, 84190f8d62eSJan Schmidt struct extent_buffer *new_root_node, 84290f8d62eSJan Schmidt int log_removal) 843bd989ba3SJan Schmidt { 844bd989ba3SJan Schmidt int ret; 845bd989ba3SJan Schmidt ret = tree_mod_log_insert_root(root->fs_info, root->node, 84690f8d62eSJan Schmidt new_root_node, GFP_NOFS, log_removal); 847bd989ba3SJan Schmidt BUG_ON(ret < 0); 848bd989ba3SJan Schmidt } 849bd989ba3SJan Schmidt 850d352ac68SChris Mason /* 8515d4f98a2SYan Zheng * check if the tree block can be shared by multiple trees 8525d4f98a2SYan Zheng */ 8535d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root, 8545d4f98a2SYan Zheng struct extent_buffer *buf) 8555d4f98a2SYan Zheng { 8565d4f98a2SYan Zheng /* 8575d4f98a2SYan Zheng * Tree blocks not in refernece counted trees and tree roots 8585d4f98a2SYan Zheng * are never shared. If a block was allocated after the last 8595d4f98a2SYan Zheng * snapshot and the block was not allocated by tree relocation, 8605d4f98a2SYan Zheng * we know the block is not shared. 8615d4f98a2SYan Zheng */ 8625d4f98a2SYan Zheng if (root->ref_cows && 8635d4f98a2SYan Zheng buf != root->node && buf != root->commit_root && 8645d4f98a2SYan Zheng (btrfs_header_generation(buf) <= 8655d4f98a2SYan Zheng btrfs_root_last_snapshot(&root->root_item) || 8665d4f98a2SYan Zheng btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) 8675d4f98a2SYan Zheng return 1; 8685d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 8695d4f98a2SYan Zheng if (root->ref_cows && 8705d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 8715d4f98a2SYan Zheng return 1; 8725d4f98a2SYan Zheng #endif 8735d4f98a2SYan Zheng return 0; 8745d4f98a2SYan Zheng } 8755d4f98a2SYan Zheng 8765d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, 8775d4f98a2SYan Zheng struct btrfs_root *root, 8785d4f98a2SYan Zheng struct extent_buffer *buf, 879f0486c68SYan, Zheng struct extent_buffer *cow, 880f0486c68SYan, Zheng int *last_ref) 8815d4f98a2SYan Zheng { 8825d4f98a2SYan Zheng u64 refs; 8835d4f98a2SYan Zheng u64 owner; 8845d4f98a2SYan Zheng u64 flags; 8855d4f98a2SYan Zheng u64 new_flags = 0; 8865d4f98a2SYan Zheng int ret; 8875d4f98a2SYan Zheng 8885d4f98a2SYan Zheng /* 8895d4f98a2SYan Zheng * Backrefs update rules: 8905d4f98a2SYan Zheng * 8915d4f98a2SYan Zheng * Always use full backrefs for extent pointers in tree block 8925d4f98a2SYan Zheng * allocated by tree relocation. 8935d4f98a2SYan Zheng * 8945d4f98a2SYan Zheng * If a shared tree block is no longer referenced by its owner 8955d4f98a2SYan Zheng * tree (btrfs_header_owner(buf) == root->root_key.objectid), 8965d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 8975d4f98a2SYan Zheng * 8985d4f98a2SYan Zheng * If a tree block is been relocating 8995d4f98a2SYan Zheng * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), 9005d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 9015d4f98a2SYan Zheng * The reason for this is some operations (such as drop tree) 9025d4f98a2SYan Zheng * are only allowed for blocks use full backrefs. 9035d4f98a2SYan Zheng */ 9045d4f98a2SYan Zheng 9055d4f98a2SYan Zheng if (btrfs_block_can_be_shared(root, buf)) { 9065d4f98a2SYan Zheng ret = btrfs_lookup_extent_info(trans, root, buf->start, 9073173a18fSJosef Bacik btrfs_header_level(buf), 1, 9083173a18fSJosef Bacik &refs, &flags); 909be1a5564SMark Fasheh if (ret) 910be1a5564SMark Fasheh return ret; 911e5df9573SMark Fasheh if (refs == 0) { 912e5df9573SMark Fasheh ret = -EROFS; 913e5df9573SMark Fasheh btrfs_std_error(root->fs_info, ret); 914e5df9573SMark Fasheh return ret; 915e5df9573SMark Fasheh } 9165d4f98a2SYan Zheng } else { 9175d4f98a2SYan Zheng refs = 1; 9185d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 9195d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 9205d4f98a2SYan Zheng flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; 9215d4f98a2SYan Zheng else 9225d4f98a2SYan Zheng flags = 0; 9235d4f98a2SYan Zheng } 9245d4f98a2SYan Zheng 9255d4f98a2SYan Zheng owner = btrfs_header_owner(buf); 9265d4f98a2SYan Zheng BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && 9275d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); 9285d4f98a2SYan Zheng 9295d4f98a2SYan Zheng if (refs > 1) { 9305d4f98a2SYan Zheng if ((owner == root->root_key.objectid || 9315d4f98a2SYan Zheng root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && 9325d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 93366d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, buf, 1, 1); 93479787eaaSJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 9355d4f98a2SYan Zheng 9365d4f98a2SYan Zheng if (root->root_key.objectid == 9375d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 93866d7e7f0SArne Jansen ret = btrfs_dec_ref(trans, root, buf, 0, 1); 93979787eaaSJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 94066d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, cow, 1, 1); 94179787eaaSJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 9425d4f98a2SYan Zheng } 9435d4f98a2SYan Zheng new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; 9445d4f98a2SYan Zheng } else { 9455d4f98a2SYan Zheng 9465d4f98a2SYan Zheng if (root->root_key.objectid == 9475d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 94866d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, cow, 1, 1); 9495d4f98a2SYan Zheng else 95066d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, cow, 0, 1); 95179787eaaSJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 9525d4f98a2SYan Zheng } 9535d4f98a2SYan Zheng if (new_flags != 0) { 954*b1c79e09SJosef Bacik int level = btrfs_header_level(buf); 955*b1c79e09SJosef Bacik 9565d4f98a2SYan Zheng ret = btrfs_set_disk_extent_flags(trans, root, 9575d4f98a2SYan Zheng buf->start, 9585d4f98a2SYan Zheng buf->len, 959*b1c79e09SJosef Bacik new_flags, level, 0); 960be1a5564SMark Fasheh if (ret) 961be1a5564SMark Fasheh return ret; 9625d4f98a2SYan Zheng } 9635d4f98a2SYan Zheng } else { 9645d4f98a2SYan Zheng if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { 9655d4f98a2SYan Zheng if (root->root_key.objectid == 9665d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 96766d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, cow, 1, 1); 9685d4f98a2SYan Zheng else 96966d7e7f0SArne Jansen ret = btrfs_inc_ref(trans, root, cow, 0, 1); 97079787eaaSJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 97166d7e7f0SArne Jansen ret = btrfs_dec_ref(trans, root, buf, 1, 1); 97279787eaaSJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 9735d4f98a2SYan Zheng } 9745d4f98a2SYan Zheng clean_tree_block(trans, root, buf); 975f0486c68SYan, Zheng *last_ref = 1; 9765d4f98a2SYan Zheng } 9775d4f98a2SYan Zheng return 0; 9785d4f98a2SYan Zheng } 9795d4f98a2SYan Zheng 9805d4f98a2SYan Zheng /* 981d397712bSChris Mason * does the dirty work in cow of a single block. The parent block (if 982d397712bSChris Mason * supplied) is updated to point to the new cow copy. The new buffer is marked 983d397712bSChris Mason * dirty and returned locked. If you modify the block it needs to be marked 984d397712bSChris Mason * dirty again. 985d352ac68SChris Mason * 986d352ac68SChris Mason * search_start -- an allocation hint for the new block 987d352ac68SChris Mason * 988d397712bSChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in 989d397712bSChris Mason * bytes the allocator should try to find free next to the block it returns. 990d397712bSChris Mason * This is just a hint and may be ignored by the allocator. 991d352ac68SChris Mason */ 992d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, 9935f39d397SChris Mason struct btrfs_root *root, 9945f39d397SChris Mason struct extent_buffer *buf, 9955f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 9965f39d397SChris Mason struct extent_buffer **cow_ret, 9979fa8cfe7SChris Mason u64 search_start, u64 empty_size) 9986702ed49SChris Mason { 9995d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 10005f39d397SChris Mason struct extent_buffer *cow; 1001be1a5564SMark Fasheh int level, ret; 1002f0486c68SYan, Zheng int last_ref = 0; 1003925baeddSChris Mason int unlock_orig = 0; 10045d4f98a2SYan Zheng u64 parent_start; 10056702ed49SChris Mason 1006925baeddSChris Mason if (*cow_ret == buf) 1007925baeddSChris Mason unlock_orig = 1; 1008925baeddSChris Mason 1009b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1010925baeddSChris Mason 10117bb86316SChris Mason WARN_ON(root->ref_cows && trans->transid != 10127bb86316SChris Mason root->fs_info->running_transaction->transid); 10136702ed49SChris Mason WARN_ON(root->ref_cows && trans->transid != root->last_trans); 10145f39d397SChris Mason 10157bb86316SChris Mason level = btrfs_header_level(buf); 101631840ae1SZheng Yan 10175d4f98a2SYan Zheng if (level == 0) 10185d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 10195d4f98a2SYan Zheng else 10205d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 10215d4f98a2SYan Zheng 10225d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { 10235d4f98a2SYan Zheng if (parent) 10245d4f98a2SYan Zheng parent_start = parent->start; 10255d4f98a2SYan Zheng else 10265d4f98a2SYan Zheng parent_start = 0; 10275d4f98a2SYan Zheng } else 10285d4f98a2SYan Zheng parent_start = 0; 10295d4f98a2SYan Zheng 10305d4f98a2SYan Zheng cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start, 10315d4f98a2SYan Zheng root->root_key.objectid, &disk_key, 10325581a51aSJan Schmidt level, search_start, empty_size); 10336702ed49SChris Mason if (IS_ERR(cow)) 10346702ed49SChris Mason return PTR_ERR(cow); 10356702ed49SChris Mason 1036b4ce94deSChris Mason /* cow is set to blocking by btrfs_init_new_buffer */ 1037b4ce94deSChris Mason 10385f39d397SChris Mason copy_extent_buffer(cow, buf, 0, 0, cow->len); 1039db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 10405f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 10415d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 10425d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 10435d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 10445d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 10455d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 10465d4f98a2SYan Zheng else 10475f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 10486702ed49SChris Mason 10492b82032cSYan Zheng write_extent_buffer(cow, root->fs_info->fsid, 10502b82032cSYan Zheng (unsigned long)btrfs_header_fsid(cow), 10512b82032cSYan Zheng BTRFS_FSID_SIZE); 10522b82032cSYan Zheng 1053be1a5564SMark Fasheh ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); 1054b68dc2a9SMark Fasheh if (ret) { 105579787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 1056b68dc2a9SMark Fasheh return ret; 1057b68dc2a9SMark Fasheh } 10581a40e23bSZheng Yan 10593fd0a558SYan, Zheng if (root->ref_cows) 10603fd0a558SYan, Zheng btrfs_reloc_cow_block(trans, root, buf, cow); 10613fd0a558SYan, Zheng 10626702ed49SChris Mason if (buf == root->node) { 1063925baeddSChris Mason WARN_ON(parent && parent != buf); 10645d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 10655d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 10665d4f98a2SYan Zheng parent_start = buf->start; 10675d4f98a2SYan Zheng else 10685d4f98a2SYan Zheng parent_start = 0; 1069925baeddSChris Mason 10705f39d397SChris Mason extent_buffer_get(cow); 107190f8d62eSJan Schmidt tree_mod_log_set_root_pointer(root, cow, 1); 1072240f62c8SChris Mason rcu_assign_pointer(root->node, cow); 1073925baeddSChris Mason 1074f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 10755581a51aSJan Schmidt last_ref); 10765f39d397SChris Mason free_extent_buffer(buf); 10770b86a832SChris Mason add_root_to_dirty_list(root); 10786702ed49SChris Mason } else { 10795d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 10805d4f98a2SYan Zheng parent_start = parent->start; 10815d4f98a2SYan Zheng else 10825d4f98a2SYan Zheng parent_start = 0; 10835d4f98a2SYan Zheng 10845d4f98a2SYan Zheng WARN_ON(trans->transid != btrfs_header_generation(parent)); 1085f230475eSJan Schmidt tree_mod_log_insert_key(root->fs_info, parent, parent_slot, 1086f230475eSJan Schmidt MOD_LOG_KEY_REPLACE); 10875f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 1088db94535dSChris Mason cow->start); 108974493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 109074493f7aSChris Mason trans->transid); 10916702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 1092d9abbf1cSJan Schmidt tree_mod_log_free_eb(root->fs_info, buf); 1093f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 10945581a51aSJan Schmidt last_ref); 10956702ed49SChris Mason } 1096925baeddSChris Mason if (unlock_orig) 1097925baeddSChris Mason btrfs_tree_unlock(buf); 10983083ee2eSJosef Bacik free_extent_buffer_stale(buf); 10996702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 11006702ed49SChris Mason *cow_ret = cow; 11016702ed49SChris Mason return 0; 11026702ed49SChris Mason } 11036702ed49SChris Mason 11045d9e75c4SJan Schmidt /* 11055d9e75c4SJan Schmidt * returns the logical address of the oldest predecessor of the given root. 11065d9e75c4SJan Schmidt * entries older than time_seq are ignored. 11075d9e75c4SJan Schmidt */ 11085d9e75c4SJan Schmidt static struct tree_mod_elem * 11095d9e75c4SJan Schmidt __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info, 111030b0463aSJan Schmidt struct extent_buffer *eb_root, u64 time_seq) 11115d9e75c4SJan Schmidt { 11125d9e75c4SJan Schmidt struct tree_mod_elem *tm; 11135d9e75c4SJan Schmidt struct tree_mod_elem *found = NULL; 111430b0463aSJan Schmidt u64 root_logical = eb_root->start; 11155d9e75c4SJan Schmidt int looped = 0; 11165d9e75c4SJan Schmidt 11175d9e75c4SJan Schmidt if (!time_seq) 11185d9e75c4SJan Schmidt return 0; 11195d9e75c4SJan Schmidt 11205d9e75c4SJan Schmidt /* 11215d9e75c4SJan Schmidt * the very last operation that's logged for a root is the replacement 11225d9e75c4SJan Schmidt * operation (if it is replaced at all). this has the index of the *new* 11235d9e75c4SJan Schmidt * root, making it the very first operation that's logged for this root. 11245d9e75c4SJan Schmidt */ 11255d9e75c4SJan Schmidt while (1) { 11265d9e75c4SJan Schmidt tm = tree_mod_log_search_oldest(fs_info, root_logical, 11275d9e75c4SJan Schmidt time_seq); 11285d9e75c4SJan Schmidt if (!looped && !tm) 11295d9e75c4SJan Schmidt return 0; 11305d9e75c4SJan Schmidt /* 113128da9fb4SJan Schmidt * if there are no tree operation for the oldest root, we simply 113228da9fb4SJan Schmidt * return it. this should only happen if that (old) root is at 113328da9fb4SJan Schmidt * level 0. 11345d9e75c4SJan Schmidt */ 113528da9fb4SJan Schmidt if (!tm) 113628da9fb4SJan Schmidt break; 11375d9e75c4SJan Schmidt 113828da9fb4SJan Schmidt /* 113928da9fb4SJan Schmidt * if there's an operation that's not a root replacement, we 114028da9fb4SJan Schmidt * found the oldest version of our root. normally, we'll find a 114128da9fb4SJan Schmidt * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. 114228da9fb4SJan Schmidt */ 11435d9e75c4SJan Schmidt if (tm->op != MOD_LOG_ROOT_REPLACE) 11445d9e75c4SJan Schmidt break; 11455d9e75c4SJan Schmidt 11465d9e75c4SJan Schmidt found = tm; 11475d9e75c4SJan Schmidt root_logical = tm->old_root.logical; 11485d9e75c4SJan Schmidt looped = 1; 11495d9e75c4SJan Schmidt } 11505d9e75c4SJan Schmidt 1151a95236d9SJan Schmidt /* if there's no old root to return, return what we found instead */ 1152a95236d9SJan Schmidt if (!found) 1153a95236d9SJan Schmidt found = tm; 1154a95236d9SJan Schmidt 11555d9e75c4SJan Schmidt return found; 11565d9e75c4SJan Schmidt } 11575d9e75c4SJan Schmidt 11585d9e75c4SJan Schmidt /* 11595d9e75c4SJan Schmidt * tm is a pointer to the first operation to rewind within eb. then, all 11605d9e75c4SJan Schmidt * previous operations will be rewinded (until we reach something older than 11615d9e75c4SJan Schmidt * time_seq). 11625d9e75c4SJan Schmidt */ 11635d9e75c4SJan Schmidt static void 11645d9e75c4SJan Schmidt __tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq, 11655d9e75c4SJan Schmidt struct tree_mod_elem *first_tm) 11665d9e75c4SJan Schmidt { 11675d9e75c4SJan Schmidt u32 n; 11685d9e75c4SJan Schmidt struct rb_node *next; 11695d9e75c4SJan Schmidt struct tree_mod_elem *tm = first_tm; 11705d9e75c4SJan Schmidt unsigned long o_dst; 11715d9e75c4SJan Schmidt unsigned long o_src; 11725d9e75c4SJan Schmidt unsigned long p_size = sizeof(struct btrfs_key_ptr); 11735d9e75c4SJan Schmidt 11745d9e75c4SJan Schmidt n = btrfs_header_nritems(eb); 1175097b8a7cSJan Schmidt while (tm && tm->seq >= time_seq) { 11765d9e75c4SJan Schmidt /* 11775d9e75c4SJan Schmidt * all the operations are recorded with the operator used for 11785d9e75c4SJan Schmidt * the modification. as we're going backwards, we do the 11795d9e75c4SJan Schmidt * opposite of each operation here. 11805d9e75c4SJan Schmidt */ 11815d9e75c4SJan Schmidt switch (tm->op) { 11825d9e75c4SJan Schmidt case MOD_LOG_KEY_REMOVE_WHILE_FREEING: 11835d9e75c4SJan Schmidt BUG_ON(tm->slot < n); 11841c697d4aSEric Sandeen /* Fallthrough */ 118595c80bb1SLiu Bo case MOD_LOG_KEY_REMOVE_WHILE_MOVING: 11864c3e6969SChris Mason case MOD_LOG_KEY_REMOVE: 11875d9e75c4SJan Schmidt btrfs_set_node_key(eb, &tm->key, tm->slot); 11885d9e75c4SJan Schmidt btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); 11895d9e75c4SJan Schmidt btrfs_set_node_ptr_generation(eb, tm->slot, 11905d9e75c4SJan Schmidt tm->generation); 11914c3e6969SChris Mason n++; 11925d9e75c4SJan Schmidt break; 11935d9e75c4SJan Schmidt case MOD_LOG_KEY_REPLACE: 11945d9e75c4SJan Schmidt BUG_ON(tm->slot >= n); 11955d9e75c4SJan Schmidt btrfs_set_node_key(eb, &tm->key, tm->slot); 11965d9e75c4SJan Schmidt btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); 11975d9e75c4SJan Schmidt btrfs_set_node_ptr_generation(eb, tm->slot, 11985d9e75c4SJan Schmidt tm->generation); 11995d9e75c4SJan Schmidt break; 12005d9e75c4SJan Schmidt case MOD_LOG_KEY_ADD: 120119956c7eSJan Schmidt /* if a move operation is needed it's in the log */ 12025d9e75c4SJan Schmidt n--; 12035d9e75c4SJan Schmidt break; 12045d9e75c4SJan Schmidt case MOD_LOG_MOVE_KEYS: 1205c3193108SJan Schmidt o_dst = btrfs_node_key_ptr_offset(tm->slot); 1206c3193108SJan Schmidt o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); 1207c3193108SJan Schmidt memmove_extent_buffer(eb, o_dst, o_src, 12085d9e75c4SJan Schmidt tm->move.nr_items * p_size); 12095d9e75c4SJan Schmidt break; 12105d9e75c4SJan Schmidt case MOD_LOG_ROOT_REPLACE: 12115d9e75c4SJan Schmidt /* 12125d9e75c4SJan Schmidt * this operation is special. for roots, this must be 12135d9e75c4SJan Schmidt * handled explicitly before rewinding. 12145d9e75c4SJan Schmidt * for non-roots, this operation may exist if the node 12155d9e75c4SJan Schmidt * was a root: root A -> child B; then A gets empty and 12165d9e75c4SJan Schmidt * B is promoted to the new root. in the mod log, we'll 12175d9e75c4SJan Schmidt * have a root-replace operation for B, a tree block 12185d9e75c4SJan Schmidt * that is no root. we simply ignore that operation. 12195d9e75c4SJan Schmidt */ 12205d9e75c4SJan Schmidt break; 12215d9e75c4SJan Schmidt } 12225d9e75c4SJan Schmidt next = rb_next(&tm->node); 12235d9e75c4SJan Schmidt if (!next) 12245d9e75c4SJan Schmidt break; 12255d9e75c4SJan Schmidt tm = container_of(next, struct tree_mod_elem, node); 12265d9e75c4SJan Schmidt if (tm->index != first_tm->index) 12275d9e75c4SJan Schmidt break; 12285d9e75c4SJan Schmidt } 12295d9e75c4SJan Schmidt btrfs_set_header_nritems(eb, n); 12305d9e75c4SJan Schmidt } 12315d9e75c4SJan Schmidt 123247fb091fSJan Schmidt /* 123347fb091fSJan Schmidt * Called with eb read locked. If the buffer cannot be rewinded, the same buffer 123447fb091fSJan Schmidt * is returned. If rewind operations happen, a fresh buffer is returned. The 123547fb091fSJan Schmidt * returned buffer is always read-locked. If the returned buffer is not the 123647fb091fSJan Schmidt * input buffer, the lock on the input buffer is released and the input buffer 123747fb091fSJan Schmidt * is freed (its refcount is decremented). 123847fb091fSJan Schmidt */ 12395d9e75c4SJan Schmidt static struct extent_buffer * 12405d9e75c4SJan Schmidt tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, 12415d9e75c4SJan Schmidt u64 time_seq) 12425d9e75c4SJan Schmidt { 12435d9e75c4SJan Schmidt struct extent_buffer *eb_rewin; 12445d9e75c4SJan Schmidt struct tree_mod_elem *tm; 12455d9e75c4SJan Schmidt 12465d9e75c4SJan Schmidt if (!time_seq) 12475d9e75c4SJan Schmidt return eb; 12485d9e75c4SJan Schmidt 12495d9e75c4SJan Schmidt if (btrfs_header_level(eb) == 0) 12505d9e75c4SJan Schmidt return eb; 12515d9e75c4SJan Schmidt 12525d9e75c4SJan Schmidt tm = tree_mod_log_search(fs_info, eb->start, time_seq); 12535d9e75c4SJan Schmidt if (!tm) 12545d9e75c4SJan Schmidt return eb; 12555d9e75c4SJan Schmidt 12565d9e75c4SJan Schmidt if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 12575d9e75c4SJan Schmidt BUG_ON(tm->slot != 0); 12585d9e75c4SJan Schmidt eb_rewin = alloc_dummy_extent_buffer(eb->start, 12595d9e75c4SJan Schmidt fs_info->tree_root->nodesize); 12605d9e75c4SJan Schmidt BUG_ON(!eb_rewin); 12615d9e75c4SJan Schmidt btrfs_set_header_bytenr(eb_rewin, eb->start); 12625d9e75c4SJan Schmidt btrfs_set_header_backref_rev(eb_rewin, 12635d9e75c4SJan Schmidt btrfs_header_backref_rev(eb)); 12645d9e75c4SJan Schmidt btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); 1265c3193108SJan Schmidt btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); 12665d9e75c4SJan Schmidt } else { 12675d9e75c4SJan Schmidt eb_rewin = btrfs_clone_extent_buffer(eb); 12685d9e75c4SJan Schmidt BUG_ON(!eb_rewin); 12695d9e75c4SJan Schmidt } 12705d9e75c4SJan Schmidt 12715d9e75c4SJan Schmidt extent_buffer_get(eb_rewin); 127247fb091fSJan Schmidt btrfs_tree_read_unlock(eb); 12735d9e75c4SJan Schmidt free_extent_buffer(eb); 12745d9e75c4SJan Schmidt 127547fb091fSJan Schmidt extent_buffer_get(eb_rewin); 127647fb091fSJan Schmidt btrfs_tree_read_lock(eb_rewin); 12775d9e75c4SJan Schmidt __tree_mod_log_rewind(eb_rewin, time_seq, tm); 127857911b8bSJan Schmidt WARN_ON(btrfs_header_nritems(eb_rewin) > 12792a745b14SArne Jansen BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root)); 12805d9e75c4SJan Schmidt 12815d9e75c4SJan Schmidt return eb_rewin; 12825d9e75c4SJan Schmidt } 12835d9e75c4SJan Schmidt 12848ba97a15SJan Schmidt /* 12858ba97a15SJan Schmidt * get_old_root() rewinds the state of @root's root node to the given @time_seq 12868ba97a15SJan Schmidt * value. If there are no changes, the current root->root_node is returned. If 12878ba97a15SJan Schmidt * anything changed in between, there's a fresh buffer allocated on which the 12888ba97a15SJan Schmidt * rewind operations are done. In any case, the returned buffer is read locked. 12898ba97a15SJan Schmidt * Returns NULL on error (with no locks held). 12908ba97a15SJan Schmidt */ 12915d9e75c4SJan Schmidt static inline struct extent_buffer * 12925d9e75c4SJan Schmidt get_old_root(struct btrfs_root *root, u64 time_seq) 12935d9e75c4SJan Schmidt { 12945d9e75c4SJan Schmidt struct tree_mod_elem *tm; 129530b0463aSJan Schmidt struct extent_buffer *eb = NULL; 129630b0463aSJan Schmidt struct extent_buffer *eb_root; 12977bfdcf7fSLiu Bo struct extent_buffer *old; 1298a95236d9SJan Schmidt struct tree_mod_root *old_root = NULL; 12994325edd0SChris Mason u64 old_generation = 0; 1300a95236d9SJan Schmidt u64 logical; 1301834328a8SJan Schmidt u32 blocksize; 13025d9e75c4SJan Schmidt 130330b0463aSJan Schmidt eb_root = btrfs_read_lock_root_node(root); 130430b0463aSJan Schmidt tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq); 13055d9e75c4SJan Schmidt if (!tm) 130630b0463aSJan Schmidt return eb_root; 13075d9e75c4SJan Schmidt 1308a95236d9SJan Schmidt if (tm->op == MOD_LOG_ROOT_REPLACE) { 13095d9e75c4SJan Schmidt old_root = &tm->old_root; 13105d9e75c4SJan Schmidt old_generation = tm->generation; 1311a95236d9SJan Schmidt logical = old_root->logical; 1312a95236d9SJan Schmidt } else { 131330b0463aSJan Schmidt logical = eb_root->start; 1314a95236d9SJan Schmidt } 13155d9e75c4SJan Schmidt 1316a95236d9SJan Schmidt tm = tree_mod_log_search(root->fs_info, logical, time_seq); 1317834328a8SJan Schmidt if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 131830b0463aSJan Schmidt btrfs_tree_read_unlock(eb_root); 131930b0463aSJan Schmidt free_extent_buffer(eb_root); 1320834328a8SJan Schmidt blocksize = btrfs_level_size(root, old_root->level); 13217bfdcf7fSLiu Bo old = read_tree_block(root, logical, blocksize, 0); 1322416bc658SJosef Bacik if (!old || !extent_buffer_uptodate(old)) { 1323416bc658SJosef Bacik free_extent_buffer(old); 1324834328a8SJan Schmidt pr_warn("btrfs: failed to read tree block %llu from get_old_root\n", 1325834328a8SJan Schmidt logical); 1326834328a8SJan Schmidt WARN_ON(1); 1327834328a8SJan Schmidt } else { 13287bfdcf7fSLiu Bo eb = btrfs_clone_extent_buffer(old); 13297bfdcf7fSLiu Bo free_extent_buffer(old); 1330834328a8SJan Schmidt } 1331834328a8SJan Schmidt } else if (old_root) { 133230b0463aSJan Schmidt btrfs_tree_read_unlock(eb_root); 133330b0463aSJan Schmidt free_extent_buffer(eb_root); 133428da9fb4SJan Schmidt eb = alloc_dummy_extent_buffer(logical, root->nodesize); 1335834328a8SJan Schmidt } else { 133630b0463aSJan Schmidt eb = btrfs_clone_extent_buffer(eb_root); 133730b0463aSJan Schmidt btrfs_tree_read_unlock(eb_root); 133830b0463aSJan Schmidt free_extent_buffer(eb_root); 1339834328a8SJan Schmidt } 1340834328a8SJan Schmidt 13418ba97a15SJan Schmidt if (!eb) 13428ba97a15SJan Schmidt return NULL; 1343d6381084SJan Schmidt extent_buffer_get(eb); 13448ba97a15SJan Schmidt btrfs_tree_read_lock(eb); 1345a95236d9SJan Schmidt if (old_root) { 13465d9e75c4SJan Schmidt btrfs_set_header_bytenr(eb, eb->start); 13475d9e75c4SJan Schmidt btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); 134830b0463aSJan Schmidt btrfs_set_header_owner(eb, btrfs_header_owner(eb_root)); 13495d9e75c4SJan Schmidt btrfs_set_header_level(eb, old_root->level); 13505d9e75c4SJan Schmidt btrfs_set_header_generation(eb, old_generation); 1351a95236d9SJan Schmidt } 135228da9fb4SJan Schmidt if (tm) 13535d9e75c4SJan Schmidt __tree_mod_log_rewind(eb, time_seq, tm); 135428da9fb4SJan Schmidt else 135528da9fb4SJan Schmidt WARN_ON(btrfs_header_level(eb) != 0); 135657911b8bSJan Schmidt WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root)); 13575d9e75c4SJan Schmidt 13585d9e75c4SJan Schmidt return eb; 13595d9e75c4SJan Schmidt } 13605d9e75c4SJan Schmidt 13615b6602e7SJan Schmidt int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) 13625b6602e7SJan Schmidt { 13635b6602e7SJan Schmidt struct tree_mod_elem *tm; 13645b6602e7SJan Schmidt int level; 136530b0463aSJan Schmidt struct extent_buffer *eb_root = btrfs_root_node(root); 13665b6602e7SJan Schmidt 136730b0463aSJan Schmidt tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq); 13685b6602e7SJan Schmidt if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { 13695b6602e7SJan Schmidt level = tm->old_root.level; 13705b6602e7SJan Schmidt } else { 137130b0463aSJan Schmidt level = btrfs_header_level(eb_root); 13725b6602e7SJan Schmidt } 137330b0463aSJan Schmidt free_extent_buffer(eb_root); 13745b6602e7SJan Schmidt 13755b6602e7SJan Schmidt return level; 13765b6602e7SJan Schmidt } 13775b6602e7SJan Schmidt 13785d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans, 13795d4f98a2SYan Zheng struct btrfs_root *root, 13805d4f98a2SYan Zheng struct extent_buffer *buf) 13815d4f98a2SYan Zheng { 1382f1ebcc74SLiu Bo /* ensure we can see the force_cow */ 1383f1ebcc74SLiu Bo smp_rmb(); 1384f1ebcc74SLiu Bo 1385f1ebcc74SLiu Bo /* 1386f1ebcc74SLiu Bo * We do not need to cow a block if 1387f1ebcc74SLiu Bo * 1) this block is not created or changed in this transaction; 1388f1ebcc74SLiu Bo * 2) this block does not belong to TREE_RELOC tree; 1389f1ebcc74SLiu Bo * 3) the root is not forced COW. 1390f1ebcc74SLiu Bo * 1391f1ebcc74SLiu Bo * What is forced COW: 1392f1ebcc74SLiu Bo * when we create snapshot during commiting the transaction, 1393f1ebcc74SLiu Bo * after we've finished coping src root, we must COW the shared 1394f1ebcc74SLiu Bo * block to ensure the metadata consistency. 1395f1ebcc74SLiu Bo */ 13965d4f98a2SYan Zheng if (btrfs_header_generation(buf) == trans->transid && 13975d4f98a2SYan Zheng !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && 13985d4f98a2SYan Zheng !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 1399f1ebcc74SLiu Bo btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && 1400f1ebcc74SLiu Bo !root->force_cow) 14015d4f98a2SYan Zheng return 0; 14025d4f98a2SYan Zheng return 1; 14035d4f98a2SYan Zheng } 14045d4f98a2SYan Zheng 1405d352ac68SChris Mason /* 1406d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 1407d352ac68SChris Mason * This version of it has extra checks so that a block isn't cow'd more than 1408d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 1409d352ac68SChris Mason */ 1410d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, 14115f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 14125f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 14139fa8cfe7SChris Mason struct extent_buffer **cow_ret) 141402217ed2SChris Mason { 14156702ed49SChris Mason u64 search_start; 1416f510cfecSChris Mason int ret; 1417dc17ff8fSChris Mason 141831b1a2bdSJulia Lawall if (trans->transaction != root->fs_info->running_transaction) 141931b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 1420d397712bSChris Mason (unsigned long long)trans->transid, 1421d397712bSChris Mason (unsigned long long) 1422ccd467d6SChris Mason root->fs_info->running_transaction->transid); 142331b1a2bdSJulia Lawall 142431b1a2bdSJulia Lawall if (trans->transid != root->fs_info->generation) 142531b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 1426d397712bSChris Mason (unsigned long long)trans->transid, 1427d397712bSChris Mason (unsigned long long)root->fs_info->generation); 1428dc17ff8fSChris Mason 14295d4f98a2SYan Zheng if (!should_cow_block(trans, root, buf)) { 143002217ed2SChris Mason *cow_ret = buf; 143102217ed2SChris Mason return 0; 143202217ed2SChris Mason } 1433c487685dSChris Mason 14340b86a832SChris Mason search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 1435b4ce94deSChris Mason 1436b4ce94deSChris Mason if (parent) 1437b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 1438b4ce94deSChris Mason btrfs_set_lock_blocking(buf); 1439b4ce94deSChris Mason 1440f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 14419fa8cfe7SChris Mason parent_slot, cow_ret, search_start, 0); 14421abe9b8aSliubo 14431abe9b8aSliubo trace_btrfs_cow_block(root, buf, *cow_ret); 14441abe9b8aSliubo 1445f510cfecSChris Mason return ret; 14462c90e5d6SChris Mason } 14476702ed49SChris Mason 1448d352ac68SChris Mason /* 1449d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 1450d352ac68SChris Mason * node are actually close by 1451d352ac68SChris Mason */ 14526b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 14536702ed49SChris Mason { 14546b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 14556702ed49SChris Mason return 1; 14566b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 14576702ed49SChris Mason return 1; 145802217ed2SChris Mason return 0; 145902217ed2SChris Mason } 146002217ed2SChris Mason 1461081e9573SChris Mason /* 1462081e9573SChris Mason * compare two keys in a memcmp fashion 1463081e9573SChris Mason */ 1464081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) 1465081e9573SChris Mason { 1466081e9573SChris Mason struct btrfs_key k1; 1467081e9573SChris Mason 1468081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 1469081e9573SChris Mason 147020736abaSDiego Calleja return btrfs_comp_cpu_keys(&k1, k2); 1471081e9573SChris Mason } 1472081e9573SChris Mason 1473f3465ca4SJosef Bacik /* 1474f3465ca4SJosef Bacik * same as comp_keys only with two btrfs_key's 1475f3465ca4SJosef Bacik */ 14765d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2) 1477f3465ca4SJosef Bacik { 1478f3465ca4SJosef Bacik if (k1->objectid > k2->objectid) 1479f3465ca4SJosef Bacik return 1; 1480f3465ca4SJosef Bacik if (k1->objectid < k2->objectid) 1481f3465ca4SJosef Bacik return -1; 1482f3465ca4SJosef Bacik if (k1->type > k2->type) 1483f3465ca4SJosef Bacik return 1; 1484f3465ca4SJosef Bacik if (k1->type < k2->type) 1485f3465ca4SJosef Bacik return -1; 1486f3465ca4SJosef Bacik if (k1->offset > k2->offset) 1487f3465ca4SJosef Bacik return 1; 1488f3465ca4SJosef Bacik if (k1->offset < k2->offset) 1489f3465ca4SJosef Bacik return -1; 1490f3465ca4SJosef Bacik return 0; 1491f3465ca4SJosef Bacik } 1492081e9573SChris Mason 1493d352ac68SChris Mason /* 1494d352ac68SChris Mason * this is used by the defrag code to go through all the 1495d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 1496d352ac68SChris Mason * disk order is close to key order 1497d352ac68SChris Mason */ 14986702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 14995f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 1500de78b51aSEric Sandeen int start_slot, u64 *last_ret, 1501a6b6e75eSChris Mason struct btrfs_key *progress) 15026702ed49SChris Mason { 15036b80053dSChris Mason struct extent_buffer *cur; 15046702ed49SChris Mason u64 blocknr; 1505ca7a79adSChris Mason u64 gen; 1506e9d0b13bSChris Mason u64 search_start = *last_ret; 1507e9d0b13bSChris Mason u64 last_block = 0; 15086702ed49SChris Mason u64 other; 15096702ed49SChris Mason u32 parent_nritems; 15106702ed49SChris Mason int end_slot; 15116702ed49SChris Mason int i; 15126702ed49SChris Mason int err = 0; 1513f2183bdeSChris Mason int parent_level; 15146b80053dSChris Mason int uptodate; 15156b80053dSChris Mason u32 blocksize; 1516081e9573SChris Mason int progress_passed = 0; 1517081e9573SChris Mason struct btrfs_disk_key disk_key; 15186702ed49SChris Mason 15195708b959SChris Mason parent_level = btrfs_header_level(parent); 15205708b959SChris Mason 15216c1500f2SJulia Lawall WARN_ON(trans->transaction != root->fs_info->running_transaction); 15226c1500f2SJulia Lawall WARN_ON(trans->transid != root->fs_info->generation); 152386479a04SChris Mason 15246b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 15256b80053dSChris Mason blocksize = btrfs_level_size(root, parent_level - 1); 15266702ed49SChris Mason end_slot = parent_nritems; 15276702ed49SChris Mason 15286702ed49SChris Mason if (parent_nritems == 1) 15296702ed49SChris Mason return 0; 15306702ed49SChris Mason 1531b4ce94deSChris Mason btrfs_set_lock_blocking(parent); 1532b4ce94deSChris Mason 15336702ed49SChris Mason for (i = start_slot; i < end_slot; i++) { 15346702ed49SChris Mason int close = 1; 1535a6b6e75eSChris Mason 1536081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 1537081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 1538081e9573SChris Mason continue; 1539081e9573SChris Mason 1540081e9573SChris Mason progress_passed = 1; 15416b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 1542ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 1543e9d0b13bSChris Mason if (last_block == 0) 1544e9d0b13bSChris Mason last_block = blocknr; 15455708b959SChris Mason 15466702ed49SChris Mason if (i > 0) { 15476b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 15486b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 15496702ed49SChris Mason } 15500ef3e66bSChris Mason if (!close && i < end_slot - 2) { 15516b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 15526b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 15536702ed49SChris Mason } 1554e9d0b13bSChris Mason if (close) { 1555e9d0b13bSChris Mason last_block = blocknr; 15566702ed49SChris Mason continue; 1557e9d0b13bSChris Mason } 15586702ed49SChris Mason 15596b80053dSChris Mason cur = btrfs_find_tree_block(root, blocknr, blocksize); 15606b80053dSChris Mason if (cur) 1561b9fab919SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen, 0); 15626b80053dSChris Mason else 15636b80053dSChris Mason uptodate = 0; 15645708b959SChris Mason if (!cur || !uptodate) { 15656b80053dSChris Mason if (!cur) { 15666b80053dSChris Mason cur = read_tree_block(root, blocknr, 1567ca7a79adSChris Mason blocksize, gen); 1568416bc658SJosef Bacik if (!cur || !extent_buffer_uptodate(cur)) { 1569416bc658SJosef Bacik free_extent_buffer(cur); 157097d9a8a4STsutomu Itoh return -EIO; 1571416bc658SJosef Bacik } 15726b80053dSChris Mason } else if (!uptodate) { 1573018642a1STsutomu Itoh err = btrfs_read_buffer(cur, gen); 1574018642a1STsutomu Itoh if (err) { 1575018642a1STsutomu Itoh free_extent_buffer(cur); 1576018642a1STsutomu Itoh return err; 1577018642a1STsutomu Itoh } 15786702ed49SChris Mason } 1579f2183bdeSChris Mason } 1580e9d0b13bSChris Mason if (search_start == 0) 15816b80053dSChris Mason search_start = last_block; 1582e9d0b13bSChris Mason 1583e7a84565SChris Mason btrfs_tree_lock(cur); 1584b4ce94deSChris Mason btrfs_set_lock_blocking(cur); 15856b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 1586e7a84565SChris Mason &cur, search_start, 15876b80053dSChris Mason min(16 * blocksize, 15889fa8cfe7SChris Mason (end_slot - i) * blocksize)); 1589252c38f0SYan if (err) { 1590e7a84565SChris Mason btrfs_tree_unlock(cur); 15916b80053dSChris Mason free_extent_buffer(cur); 15926702ed49SChris Mason break; 1593252c38f0SYan } 1594e7a84565SChris Mason search_start = cur->start; 1595e7a84565SChris Mason last_block = cur->start; 1596f2183bdeSChris Mason *last_ret = search_start; 1597e7a84565SChris Mason btrfs_tree_unlock(cur); 1598e7a84565SChris Mason free_extent_buffer(cur); 15996702ed49SChris Mason } 16006702ed49SChris Mason return err; 16016702ed49SChris Mason } 16026702ed49SChris Mason 160374123bd7SChris Mason /* 160474123bd7SChris Mason * The leaf data grows from end-to-front in the node. 160574123bd7SChris Mason * this returns the address of the start of the last item, 160674123bd7SChris Mason * which is the stop of the leaf data stack 160774123bd7SChris Mason */ 1608123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root, 16095f39d397SChris Mason struct extent_buffer *leaf) 1610be0e5c09SChris Mason { 16115f39d397SChris Mason u32 nr = btrfs_header_nritems(leaf); 1612be0e5c09SChris Mason if (nr == 0) 1613123abc88SChris Mason return BTRFS_LEAF_DATA_SIZE(root); 16145f39d397SChris Mason return btrfs_item_offset_nr(leaf, nr - 1); 1615be0e5c09SChris Mason } 1616be0e5c09SChris Mason 1617aa5d6bedSChris Mason 161874123bd7SChris Mason /* 16195f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 16205f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 16215f39d397SChris Mason * 162274123bd7SChris Mason * the slot in the array is returned via slot, and it points to 162374123bd7SChris Mason * the place where you would insert key if it is not found in 162474123bd7SChris Mason * the array. 162574123bd7SChris Mason * 162674123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 162774123bd7SChris Mason */ 1628e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb, 1629e02119d5SChris Mason unsigned long p, 16305f39d397SChris Mason int item_size, struct btrfs_key *key, 1631be0e5c09SChris Mason int max, int *slot) 1632be0e5c09SChris Mason { 1633be0e5c09SChris Mason int low = 0; 1634be0e5c09SChris Mason int high = max; 1635be0e5c09SChris Mason int mid; 1636be0e5c09SChris Mason int ret; 1637479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 16385f39d397SChris Mason struct btrfs_disk_key unaligned; 16395f39d397SChris Mason unsigned long offset; 16405f39d397SChris Mason char *kaddr = NULL; 16415f39d397SChris Mason unsigned long map_start = 0; 16425f39d397SChris Mason unsigned long map_len = 0; 1643479965d6SChris Mason int err; 1644be0e5c09SChris Mason 1645be0e5c09SChris Mason while (low < high) { 1646be0e5c09SChris Mason mid = (low + high) / 2; 16475f39d397SChris Mason offset = p + mid * item_size; 16485f39d397SChris Mason 1649a6591715SChris Mason if (!kaddr || offset < map_start || 16505f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 16515f39d397SChris Mason map_start + map_len) { 1652934d375bSChris Mason 1653934d375bSChris Mason err = map_private_extent_buffer(eb, offset, 1654479965d6SChris Mason sizeof(struct btrfs_disk_key), 1655a6591715SChris Mason &kaddr, &map_start, &map_len); 16565f39d397SChris Mason 1657479965d6SChris Mason if (!err) { 1658479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 1659479965d6SChris Mason map_start); 1660479965d6SChris Mason } else { 16615f39d397SChris Mason read_extent_buffer(eb, &unaligned, 16625f39d397SChris Mason offset, sizeof(unaligned)); 16635f39d397SChris Mason tmp = &unaligned; 1664479965d6SChris Mason } 1665479965d6SChris Mason 16665f39d397SChris Mason } else { 16675f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 16685f39d397SChris Mason map_start); 16695f39d397SChris Mason } 1670be0e5c09SChris Mason ret = comp_keys(tmp, key); 1671be0e5c09SChris Mason 1672be0e5c09SChris Mason if (ret < 0) 1673be0e5c09SChris Mason low = mid + 1; 1674be0e5c09SChris Mason else if (ret > 0) 1675be0e5c09SChris Mason high = mid; 1676be0e5c09SChris Mason else { 1677be0e5c09SChris Mason *slot = mid; 1678be0e5c09SChris Mason return 0; 1679be0e5c09SChris Mason } 1680be0e5c09SChris Mason } 1681be0e5c09SChris Mason *slot = low; 1682be0e5c09SChris Mason return 1; 1683be0e5c09SChris Mason } 1684be0e5c09SChris Mason 168597571fd0SChris Mason /* 168697571fd0SChris Mason * simple bin_search frontend that does the right thing for 168797571fd0SChris Mason * leaves vs nodes 168897571fd0SChris Mason */ 16895f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, 16905f39d397SChris Mason int level, int *slot) 1691be0e5c09SChris Mason { 1692f775738fSWang Sheng-Hui if (level == 0) 16935f39d397SChris Mason return generic_bin_search(eb, 16945f39d397SChris Mason offsetof(struct btrfs_leaf, items), 16950783fcfcSChris Mason sizeof(struct btrfs_item), 16965f39d397SChris Mason key, btrfs_header_nritems(eb), 16977518a238SChris Mason slot); 1698f775738fSWang Sheng-Hui else 16995f39d397SChris Mason return generic_bin_search(eb, 17005f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 1701123abc88SChris Mason sizeof(struct btrfs_key_ptr), 17025f39d397SChris Mason key, btrfs_header_nritems(eb), 17037518a238SChris Mason slot); 1704be0e5c09SChris Mason } 1705be0e5c09SChris Mason 17065d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, 17075d4f98a2SYan Zheng int level, int *slot) 17085d4f98a2SYan Zheng { 17095d4f98a2SYan Zheng return bin_search(eb, key, level, slot); 17105d4f98a2SYan Zheng } 17115d4f98a2SYan Zheng 1712f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size) 1713f0486c68SYan, Zheng { 1714f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 1715f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 1716f0486c68SYan, Zheng btrfs_root_used(&root->root_item) + size); 1717f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 1718f0486c68SYan, Zheng } 1719f0486c68SYan, Zheng 1720f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size) 1721f0486c68SYan, Zheng { 1722f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 1723f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 1724f0486c68SYan, Zheng btrfs_root_used(&root->root_item) - size); 1725f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 1726f0486c68SYan, Zheng } 1727f0486c68SYan, Zheng 1728d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 1729d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 1730d352ac68SChris Mason * NULL is returned on error. 1731d352ac68SChris Mason */ 1732e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, 17335f39d397SChris Mason struct extent_buffer *parent, int slot) 1734bb803951SChris Mason { 1735ca7a79adSChris Mason int level = btrfs_header_level(parent); 1736416bc658SJosef Bacik struct extent_buffer *eb; 1737416bc658SJosef Bacik 1738bb803951SChris Mason if (slot < 0) 1739bb803951SChris Mason return NULL; 17405f39d397SChris Mason if (slot >= btrfs_header_nritems(parent)) 1741bb803951SChris Mason return NULL; 1742ca7a79adSChris Mason 1743ca7a79adSChris Mason BUG_ON(level == 0); 1744ca7a79adSChris Mason 1745416bc658SJosef Bacik eb = read_tree_block(root, btrfs_node_blockptr(parent, slot), 1746ca7a79adSChris Mason btrfs_level_size(root, level - 1), 1747ca7a79adSChris Mason btrfs_node_ptr_generation(parent, slot)); 1748416bc658SJosef Bacik if (eb && !extent_buffer_uptodate(eb)) { 1749416bc658SJosef Bacik free_extent_buffer(eb); 1750416bc658SJosef Bacik eb = NULL; 1751416bc658SJosef Bacik } 1752416bc658SJosef Bacik 1753416bc658SJosef Bacik return eb; 1754bb803951SChris Mason } 1755bb803951SChris Mason 1756d352ac68SChris Mason /* 1757d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 1758d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 1759d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 1760d352ac68SChris Mason */ 1761e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 176298ed5174SChris Mason struct btrfs_root *root, 176398ed5174SChris Mason struct btrfs_path *path, int level) 1764bb803951SChris Mason { 17655f39d397SChris Mason struct extent_buffer *right = NULL; 17665f39d397SChris Mason struct extent_buffer *mid; 17675f39d397SChris Mason struct extent_buffer *left = NULL; 17685f39d397SChris Mason struct extent_buffer *parent = NULL; 1769bb803951SChris Mason int ret = 0; 1770bb803951SChris Mason int wret; 1771bb803951SChris Mason int pslot; 1772bb803951SChris Mason int orig_slot = path->slots[level]; 177379f95c82SChris Mason u64 orig_ptr; 1774bb803951SChris Mason 1775bb803951SChris Mason if (level == 0) 1776bb803951SChris Mason return 0; 1777bb803951SChris Mason 17785f39d397SChris Mason mid = path->nodes[level]; 1779b4ce94deSChris Mason 1780bd681513SChris Mason WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && 1781bd681513SChris Mason path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); 17827bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 17837bb86316SChris Mason 17841d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 178579f95c82SChris Mason 1786a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 17875f39d397SChris Mason parent = path->nodes[level + 1]; 1788bb803951SChris Mason pslot = path->slots[level + 1]; 1789a05a9bb1SLi Zefan } 1790bb803951SChris Mason 179140689478SChris Mason /* 179240689478SChris Mason * deal with the case where there is only one pointer in the root 179340689478SChris Mason * by promoting the node below to a root 179440689478SChris Mason */ 17955f39d397SChris Mason if (!parent) { 17965f39d397SChris Mason struct extent_buffer *child; 1797bb803951SChris Mason 17985f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 1799bb803951SChris Mason return 0; 1800bb803951SChris Mason 1801bb803951SChris Mason /* promote the child to a root */ 18025f39d397SChris Mason child = read_node_slot(root, mid, 0); 1803305a26afSMark Fasheh if (!child) { 1804305a26afSMark Fasheh ret = -EROFS; 1805305a26afSMark Fasheh btrfs_std_error(root->fs_info, ret); 1806305a26afSMark Fasheh goto enospc; 1807305a26afSMark Fasheh } 1808305a26afSMark Fasheh 1809925baeddSChris Mason btrfs_tree_lock(child); 1810b4ce94deSChris Mason btrfs_set_lock_blocking(child); 18119fa8cfe7SChris Mason ret = btrfs_cow_block(trans, root, child, mid, 0, &child); 1812f0486c68SYan, Zheng if (ret) { 1813f0486c68SYan, Zheng btrfs_tree_unlock(child); 1814f0486c68SYan, Zheng free_extent_buffer(child); 1815f0486c68SYan, Zheng goto enospc; 1816f0486c68SYan, Zheng } 18172f375ab9SYan 181890f8d62eSJan Schmidt tree_mod_log_set_root_pointer(root, child, 1); 1819240f62c8SChris Mason rcu_assign_pointer(root->node, child); 1820925baeddSChris Mason 18210b86a832SChris Mason add_root_to_dirty_list(root); 1822925baeddSChris Mason btrfs_tree_unlock(child); 1823b4ce94deSChris Mason 1824925baeddSChris Mason path->locks[level] = 0; 1825bb803951SChris Mason path->nodes[level] = NULL; 18265f39d397SChris Mason clean_tree_block(trans, root, mid); 1827925baeddSChris Mason btrfs_tree_unlock(mid); 1828bb803951SChris Mason /* once for the path */ 18295f39d397SChris Mason free_extent_buffer(mid); 1830f0486c68SYan, Zheng 1831f0486c68SYan, Zheng root_sub_used(root, mid->len); 18325581a51aSJan Schmidt btrfs_free_tree_block(trans, root, mid, 0, 1); 1833bb803951SChris Mason /* once for the root ptr */ 18343083ee2eSJosef Bacik free_extent_buffer_stale(mid); 1835f0486c68SYan, Zheng return 0; 1836bb803951SChris Mason } 18375f39d397SChris Mason if (btrfs_header_nritems(mid) > 1838123abc88SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 4) 1839bb803951SChris Mason return 0; 1840bb803951SChris Mason 18415f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 18425f39d397SChris Mason if (left) { 1843925baeddSChris Mason btrfs_tree_lock(left); 1844b4ce94deSChris Mason btrfs_set_lock_blocking(left); 18455f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 18469fa8cfe7SChris Mason parent, pslot - 1, &left); 184754aa1f4dSChris Mason if (wret) { 184854aa1f4dSChris Mason ret = wret; 184954aa1f4dSChris Mason goto enospc; 185054aa1f4dSChris Mason } 18512cc58cf2SChris Mason } 18525f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 18535f39d397SChris Mason if (right) { 1854925baeddSChris Mason btrfs_tree_lock(right); 1855b4ce94deSChris Mason btrfs_set_lock_blocking(right); 18565f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 18579fa8cfe7SChris Mason parent, pslot + 1, &right); 18582cc58cf2SChris Mason if (wret) { 18592cc58cf2SChris Mason ret = wret; 18602cc58cf2SChris Mason goto enospc; 18612cc58cf2SChris Mason } 18622cc58cf2SChris Mason } 18632cc58cf2SChris Mason 18642cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 18655f39d397SChris Mason if (left) { 18665f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 1867bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 186879f95c82SChris Mason if (wret < 0) 186979f95c82SChris Mason ret = wret; 1870bb803951SChris Mason } 187179f95c82SChris Mason 187279f95c82SChris Mason /* 187379f95c82SChris Mason * then try to empty the right most buffer into the middle 187479f95c82SChris Mason */ 18755f39d397SChris Mason if (right) { 1876971a1f66SChris Mason wret = push_node_left(trans, root, mid, right, 1); 187754aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 187879f95c82SChris Mason ret = wret; 18795f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 18805f39d397SChris Mason clean_tree_block(trans, root, right); 1881925baeddSChris Mason btrfs_tree_unlock(right); 1882afe5fea7STsutomu Itoh del_ptr(root, path, level + 1, pslot + 1); 1883f0486c68SYan, Zheng root_sub_used(root, right->len); 18845581a51aSJan Schmidt btrfs_free_tree_block(trans, root, right, 0, 1); 18853083ee2eSJosef Bacik free_extent_buffer_stale(right); 1886f0486c68SYan, Zheng right = NULL; 1887bb803951SChris Mason } else { 18885f39d397SChris Mason struct btrfs_disk_key right_key; 18895f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 1890f230475eSJan Schmidt tree_mod_log_set_node_key(root->fs_info, parent, 189132adf090SLiu Bo pslot + 1, 0); 18925f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 18935f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1894bb803951SChris Mason } 1895bb803951SChris Mason } 18965f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 189779f95c82SChris Mason /* 189879f95c82SChris Mason * we're not allowed to leave a node with one item in the 189979f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 190079f95c82SChris Mason * could try to delete the only pointer in this node. 190179f95c82SChris Mason * So, pull some keys from the left. 190279f95c82SChris Mason * There has to be a left pointer at this point because 190379f95c82SChris Mason * otherwise we would have pulled some pointers from the 190479f95c82SChris Mason * right 190579f95c82SChris Mason */ 1906305a26afSMark Fasheh if (!left) { 1907305a26afSMark Fasheh ret = -EROFS; 1908305a26afSMark Fasheh btrfs_std_error(root->fs_info, ret); 1909305a26afSMark Fasheh goto enospc; 1910305a26afSMark Fasheh } 19115f39d397SChris Mason wret = balance_node_right(trans, root, mid, left); 191254aa1f4dSChris Mason if (wret < 0) { 191379f95c82SChris Mason ret = wret; 191454aa1f4dSChris Mason goto enospc; 191554aa1f4dSChris Mason } 1916bce4eae9SChris Mason if (wret == 1) { 1917bce4eae9SChris Mason wret = push_node_left(trans, root, left, mid, 1); 1918bce4eae9SChris Mason if (wret < 0) 1919bce4eae9SChris Mason ret = wret; 1920bce4eae9SChris Mason } 192179f95c82SChris Mason BUG_ON(wret == 1); 192279f95c82SChris Mason } 19235f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 19245f39d397SChris Mason clean_tree_block(trans, root, mid); 1925925baeddSChris Mason btrfs_tree_unlock(mid); 1926afe5fea7STsutomu Itoh del_ptr(root, path, level + 1, pslot); 1927f0486c68SYan, Zheng root_sub_used(root, mid->len); 19285581a51aSJan Schmidt btrfs_free_tree_block(trans, root, mid, 0, 1); 19293083ee2eSJosef Bacik free_extent_buffer_stale(mid); 1930f0486c68SYan, Zheng mid = NULL; 193179f95c82SChris Mason } else { 193279f95c82SChris Mason /* update the parent key to reflect our changes */ 19335f39d397SChris Mason struct btrfs_disk_key mid_key; 19345f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 193532adf090SLiu Bo tree_mod_log_set_node_key(root->fs_info, parent, 1936f230475eSJan Schmidt pslot, 0); 19375f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 19385f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 193979f95c82SChris Mason } 1940bb803951SChris Mason 194179f95c82SChris Mason /* update the path */ 19425f39d397SChris Mason if (left) { 19435f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 19445f39d397SChris Mason extent_buffer_get(left); 1945925baeddSChris Mason /* left was locked after cow */ 19465f39d397SChris Mason path->nodes[level] = left; 1947bb803951SChris Mason path->slots[level + 1] -= 1; 1948bb803951SChris Mason path->slots[level] = orig_slot; 1949925baeddSChris Mason if (mid) { 1950925baeddSChris Mason btrfs_tree_unlock(mid); 19515f39d397SChris Mason free_extent_buffer(mid); 1952925baeddSChris Mason } 1953bb803951SChris Mason } else { 19545f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 1955bb803951SChris Mason path->slots[level] = orig_slot; 1956bb803951SChris Mason } 1957bb803951SChris Mason } 195879f95c82SChris Mason /* double check we haven't messed things up */ 1959e20d96d6SChris Mason if (orig_ptr != 19605f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 196179f95c82SChris Mason BUG(); 196254aa1f4dSChris Mason enospc: 1963925baeddSChris Mason if (right) { 1964925baeddSChris Mason btrfs_tree_unlock(right); 19655f39d397SChris Mason free_extent_buffer(right); 1966925baeddSChris Mason } 1967925baeddSChris Mason if (left) { 1968925baeddSChris Mason if (path->nodes[level] != left) 1969925baeddSChris Mason btrfs_tree_unlock(left); 19705f39d397SChris Mason free_extent_buffer(left); 1971925baeddSChris Mason } 1972bb803951SChris Mason return ret; 1973bb803951SChris Mason } 1974bb803951SChris Mason 1975d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 1976d352ac68SChris Mason * when they are completely full. This is also done top down, so we 1977d352ac68SChris Mason * have to be pessimistic. 1978d352ac68SChris Mason */ 1979d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, 1980e66f709bSChris Mason struct btrfs_root *root, 1981e66f709bSChris Mason struct btrfs_path *path, int level) 1982e66f709bSChris Mason { 19835f39d397SChris Mason struct extent_buffer *right = NULL; 19845f39d397SChris Mason struct extent_buffer *mid; 19855f39d397SChris Mason struct extent_buffer *left = NULL; 19865f39d397SChris Mason struct extent_buffer *parent = NULL; 1987e66f709bSChris Mason int ret = 0; 1988e66f709bSChris Mason int wret; 1989e66f709bSChris Mason int pslot; 1990e66f709bSChris Mason int orig_slot = path->slots[level]; 1991e66f709bSChris Mason 1992e66f709bSChris Mason if (level == 0) 1993e66f709bSChris Mason return 1; 1994e66f709bSChris Mason 19955f39d397SChris Mason mid = path->nodes[level]; 19967bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 1997e66f709bSChris Mason 1998a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 19995f39d397SChris Mason parent = path->nodes[level + 1]; 2000e66f709bSChris Mason pslot = path->slots[level + 1]; 2001a05a9bb1SLi Zefan } 2002e66f709bSChris Mason 20035f39d397SChris Mason if (!parent) 2004e66f709bSChris Mason return 1; 2005e66f709bSChris Mason 20065f39d397SChris Mason left = read_node_slot(root, parent, pslot - 1); 2007e66f709bSChris Mason 2008e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 20095f39d397SChris Mason if (left) { 2010e66f709bSChris Mason u32 left_nr; 2011925baeddSChris Mason 2012925baeddSChris Mason btrfs_tree_lock(left); 2013b4ce94deSChris Mason btrfs_set_lock_blocking(left); 2014b4ce94deSChris Mason 20155f39d397SChris Mason left_nr = btrfs_header_nritems(left); 201633ade1f8SChris Mason if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 201733ade1f8SChris Mason wret = 1; 201833ade1f8SChris Mason } else { 20195f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 20209fa8cfe7SChris Mason pslot - 1, &left); 202154aa1f4dSChris Mason if (ret) 202254aa1f4dSChris Mason wret = 1; 202354aa1f4dSChris Mason else { 202454aa1f4dSChris Mason wret = push_node_left(trans, root, 2025971a1f66SChris Mason left, mid, 0); 202654aa1f4dSChris Mason } 202733ade1f8SChris Mason } 2028e66f709bSChris Mason if (wret < 0) 2029e66f709bSChris Mason ret = wret; 2030e66f709bSChris Mason if (wret == 0) { 20315f39d397SChris Mason struct btrfs_disk_key disk_key; 2032e66f709bSChris Mason orig_slot += left_nr; 20335f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 2034f230475eSJan Schmidt tree_mod_log_set_node_key(root->fs_info, parent, 203532adf090SLiu Bo pslot, 0); 20365f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 20375f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 20385f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 20395f39d397SChris Mason path->nodes[level] = left; 2040e66f709bSChris Mason path->slots[level + 1] -= 1; 2041e66f709bSChris Mason path->slots[level] = orig_slot; 2042925baeddSChris Mason btrfs_tree_unlock(mid); 20435f39d397SChris Mason free_extent_buffer(mid); 2044e66f709bSChris Mason } else { 2045e66f709bSChris Mason orig_slot -= 20465f39d397SChris Mason btrfs_header_nritems(left); 2047e66f709bSChris Mason path->slots[level] = orig_slot; 2048925baeddSChris Mason btrfs_tree_unlock(left); 20495f39d397SChris Mason free_extent_buffer(left); 2050e66f709bSChris Mason } 2051e66f709bSChris Mason return 0; 2052e66f709bSChris Mason } 2053925baeddSChris Mason btrfs_tree_unlock(left); 20545f39d397SChris Mason free_extent_buffer(left); 2055e66f709bSChris Mason } 20565f39d397SChris Mason right = read_node_slot(root, parent, pslot + 1); 2057e66f709bSChris Mason 2058e66f709bSChris Mason /* 2059e66f709bSChris Mason * then try to empty the right most buffer into the middle 2060e66f709bSChris Mason */ 20615f39d397SChris Mason if (right) { 206233ade1f8SChris Mason u32 right_nr; 2063b4ce94deSChris Mason 2064925baeddSChris Mason btrfs_tree_lock(right); 2065b4ce94deSChris Mason btrfs_set_lock_blocking(right); 2066b4ce94deSChris Mason 20675f39d397SChris Mason right_nr = btrfs_header_nritems(right); 206833ade1f8SChris Mason if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 206933ade1f8SChris Mason wret = 1; 207033ade1f8SChris Mason } else { 20715f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 20725f39d397SChris Mason parent, pslot + 1, 20739fa8cfe7SChris Mason &right); 207454aa1f4dSChris Mason if (ret) 207554aa1f4dSChris Mason wret = 1; 207654aa1f4dSChris Mason else { 207733ade1f8SChris Mason wret = balance_node_right(trans, root, 20785f39d397SChris Mason right, mid); 207933ade1f8SChris Mason } 208054aa1f4dSChris Mason } 2081e66f709bSChris Mason if (wret < 0) 2082e66f709bSChris Mason ret = wret; 2083e66f709bSChris Mason if (wret == 0) { 20845f39d397SChris Mason struct btrfs_disk_key disk_key; 20855f39d397SChris Mason 20865f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 2087f230475eSJan Schmidt tree_mod_log_set_node_key(root->fs_info, parent, 208832adf090SLiu Bo pslot + 1, 0); 20895f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 20905f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 20915f39d397SChris Mason 20925f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 20935f39d397SChris Mason path->nodes[level] = right; 2094e66f709bSChris Mason path->slots[level + 1] += 1; 2095e66f709bSChris Mason path->slots[level] = orig_slot - 20965f39d397SChris Mason btrfs_header_nritems(mid); 2097925baeddSChris Mason btrfs_tree_unlock(mid); 20985f39d397SChris Mason free_extent_buffer(mid); 2099e66f709bSChris Mason } else { 2100925baeddSChris Mason btrfs_tree_unlock(right); 21015f39d397SChris Mason free_extent_buffer(right); 2102e66f709bSChris Mason } 2103e66f709bSChris Mason return 0; 2104e66f709bSChris Mason } 2105925baeddSChris Mason btrfs_tree_unlock(right); 21065f39d397SChris Mason free_extent_buffer(right); 2107e66f709bSChris Mason } 2108e66f709bSChris Mason return 1; 2109e66f709bSChris Mason } 2110e66f709bSChris Mason 211174123bd7SChris Mason /* 2112d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 2113d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 21143c69faecSChris Mason */ 2115c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root, 2116e02119d5SChris Mason struct btrfs_path *path, 211701f46658SChris Mason int level, int slot, u64 objectid) 21183c69faecSChris Mason { 21195f39d397SChris Mason struct extent_buffer *node; 212001f46658SChris Mason struct btrfs_disk_key disk_key; 21213c69faecSChris Mason u32 nritems; 21223c69faecSChris Mason u64 search; 2123a7175319SChris Mason u64 target; 21246b80053dSChris Mason u64 nread = 0; 2125cb25c2eaSJosef Bacik u64 gen; 21263c69faecSChris Mason int direction = path->reada; 21275f39d397SChris Mason struct extent_buffer *eb; 21286b80053dSChris Mason u32 nr; 21296b80053dSChris Mason u32 blocksize; 21306b80053dSChris Mason u32 nscan = 0; 2131db94535dSChris Mason 2132a6b6e75eSChris Mason if (level != 1) 21333c69faecSChris Mason return; 21343c69faecSChris Mason 21356702ed49SChris Mason if (!path->nodes[level]) 21366702ed49SChris Mason return; 21376702ed49SChris Mason 21385f39d397SChris Mason node = path->nodes[level]; 2139925baeddSChris Mason 21403c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 21416b80053dSChris Mason blocksize = btrfs_level_size(root, level - 1); 21426b80053dSChris Mason eb = btrfs_find_tree_block(root, search, blocksize); 21435f39d397SChris Mason if (eb) { 21445f39d397SChris Mason free_extent_buffer(eb); 21453c69faecSChris Mason return; 21463c69faecSChris Mason } 21473c69faecSChris Mason 2148a7175319SChris Mason target = search; 21496b80053dSChris Mason 21505f39d397SChris Mason nritems = btrfs_header_nritems(node); 21516b80053dSChris Mason nr = slot; 215225b8b936SJosef Bacik 21533c69faecSChris Mason while (1) { 21546b80053dSChris Mason if (direction < 0) { 21556b80053dSChris Mason if (nr == 0) 21563c69faecSChris Mason break; 21576b80053dSChris Mason nr--; 21586b80053dSChris Mason } else if (direction > 0) { 21596b80053dSChris Mason nr++; 21606b80053dSChris Mason if (nr >= nritems) 21616b80053dSChris Mason break; 21623c69faecSChris Mason } 216301f46658SChris Mason if (path->reada < 0 && objectid) { 216401f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 216501f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 216601f46658SChris Mason break; 216701f46658SChris Mason } 21686b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 2169a7175319SChris Mason if ((search <= target && target - search <= 65536) || 2170a7175319SChris Mason (search > target && search - target <= 65536)) { 2171cb25c2eaSJosef Bacik gen = btrfs_node_ptr_generation(node, nr); 2172cb25c2eaSJosef Bacik readahead_tree_block(root, search, blocksize, gen); 21736b80053dSChris Mason nread += blocksize; 21743c69faecSChris Mason } 21756b80053dSChris Mason nscan++; 2176a7175319SChris Mason if ((nread > 65536 || nscan > 32)) 21776b80053dSChris Mason break; 21783c69faecSChris Mason } 21793c69faecSChris Mason } 2180925baeddSChris Mason 2181d352ac68SChris Mason /* 2182b4ce94deSChris Mason * returns -EAGAIN if it had to drop the path, or zero if everything was in 2183b4ce94deSChris Mason * cache 2184b4ce94deSChris Mason */ 2185b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root, 2186b4ce94deSChris Mason struct btrfs_path *path, int level) 2187b4ce94deSChris Mason { 2188b4ce94deSChris Mason int slot; 2189b4ce94deSChris Mason int nritems; 2190b4ce94deSChris Mason struct extent_buffer *parent; 2191b4ce94deSChris Mason struct extent_buffer *eb; 2192b4ce94deSChris Mason u64 gen; 2193b4ce94deSChris Mason u64 block1 = 0; 2194b4ce94deSChris Mason u64 block2 = 0; 2195b4ce94deSChris Mason int ret = 0; 2196b4ce94deSChris Mason int blocksize; 2197b4ce94deSChris Mason 21988c594ea8SChris Mason parent = path->nodes[level + 1]; 2199b4ce94deSChris Mason if (!parent) 2200b4ce94deSChris Mason return 0; 2201b4ce94deSChris Mason 2202b4ce94deSChris Mason nritems = btrfs_header_nritems(parent); 22038c594ea8SChris Mason slot = path->slots[level + 1]; 2204b4ce94deSChris Mason blocksize = btrfs_level_size(root, level); 2205b4ce94deSChris Mason 2206b4ce94deSChris Mason if (slot > 0) { 2207b4ce94deSChris Mason block1 = btrfs_node_blockptr(parent, slot - 1); 2208b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot - 1); 2209b4ce94deSChris Mason eb = btrfs_find_tree_block(root, block1, blocksize); 2210b9fab919SChris Mason /* 2211b9fab919SChris Mason * if we get -eagain from btrfs_buffer_uptodate, we 2212b9fab919SChris Mason * don't want to return eagain here. That will loop 2213b9fab919SChris Mason * forever 2214b9fab919SChris Mason */ 2215b9fab919SChris Mason if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) 2216b4ce94deSChris Mason block1 = 0; 2217b4ce94deSChris Mason free_extent_buffer(eb); 2218b4ce94deSChris Mason } 22198c594ea8SChris Mason if (slot + 1 < nritems) { 2220b4ce94deSChris Mason block2 = btrfs_node_blockptr(parent, slot + 1); 2221b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot + 1); 2222b4ce94deSChris Mason eb = btrfs_find_tree_block(root, block2, blocksize); 2223b9fab919SChris Mason if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) 2224b4ce94deSChris Mason block2 = 0; 2225b4ce94deSChris Mason free_extent_buffer(eb); 2226b4ce94deSChris Mason } 2227b4ce94deSChris Mason if (block1 || block2) { 2228b4ce94deSChris Mason ret = -EAGAIN; 22298c594ea8SChris Mason 22308c594ea8SChris Mason /* release the whole path */ 2231b3b4aa74SDavid Sterba btrfs_release_path(path); 22328c594ea8SChris Mason 22338c594ea8SChris Mason /* read the blocks */ 2234b4ce94deSChris Mason if (block1) 2235b4ce94deSChris Mason readahead_tree_block(root, block1, blocksize, 0); 2236b4ce94deSChris Mason if (block2) 2237b4ce94deSChris Mason readahead_tree_block(root, block2, blocksize, 0); 2238b4ce94deSChris Mason 2239b4ce94deSChris Mason if (block1) { 2240b4ce94deSChris Mason eb = read_tree_block(root, block1, blocksize, 0); 2241b4ce94deSChris Mason free_extent_buffer(eb); 2242b4ce94deSChris Mason } 22438c594ea8SChris Mason if (block2) { 2244b4ce94deSChris Mason eb = read_tree_block(root, block2, blocksize, 0); 2245b4ce94deSChris Mason free_extent_buffer(eb); 2246b4ce94deSChris Mason } 2247b4ce94deSChris Mason } 2248b4ce94deSChris Mason return ret; 2249b4ce94deSChris Mason } 2250b4ce94deSChris Mason 2251b4ce94deSChris Mason 2252b4ce94deSChris Mason /* 2253d397712bSChris Mason * when we walk down the tree, it is usually safe to unlock the higher layers 2254d397712bSChris Mason * in the tree. The exceptions are when our path goes through slot 0, because 2255d397712bSChris Mason * operations on the tree might require changing key pointers higher up in the 2256d397712bSChris Mason * tree. 2257d352ac68SChris Mason * 2258d397712bSChris Mason * callers might also have set path->keep_locks, which tells this code to keep 2259d397712bSChris Mason * the lock if the path points to the last slot in the block. This is part of 2260d397712bSChris Mason * walking through the tree, and selecting the next slot in the higher block. 2261d352ac68SChris Mason * 2262d397712bSChris Mason * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so 2263d397712bSChris Mason * if lowest_unlock is 1, level 0 won't be unlocked 2264d352ac68SChris Mason */ 2265e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level, 2266f7c79f30SChris Mason int lowest_unlock, int min_write_lock_level, 2267f7c79f30SChris Mason int *write_lock_level) 2268925baeddSChris Mason { 2269925baeddSChris Mason int i; 2270925baeddSChris Mason int skip_level = level; 2271051e1b9fSChris Mason int no_skips = 0; 2272925baeddSChris Mason struct extent_buffer *t; 2273925baeddSChris Mason 2274925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2275925baeddSChris Mason if (!path->nodes[i]) 2276925baeddSChris Mason break; 2277925baeddSChris Mason if (!path->locks[i]) 2278925baeddSChris Mason break; 2279051e1b9fSChris Mason if (!no_skips && path->slots[i] == 0) { 2280925baeddSChris Mason skip_level = i + 1; 2281925baeddSChris Mason continue; 2282925baeddSChris Mason } 2283051e1b9fSChris Mason if (!no_skips && path->keep_locks) { 2284925baeddSChris Mason u32 nritems; 2285925baeddSChris Mason t = path->nodes[i]; 2286925baeddSChris Mason nritems = btrfs_header_nritems(t); 2287051e1b9fSChris Mason if (nritems < 1 || path->slots[i] >= nritems - 1) { 2288925baeddSChris Mason skip_level = i + 1; 2289925baeddSChris Mason continue; 2290925baeddSChris Mason } 2291925baeddSChris Mason } 2292051e1b9fSChris Mason if (skip_level < i && i >= lowest_unlock) 2293051e1b9fSChris Mason no_skips = 1; 2294051e1b9fSChris Mason 2295925baeddSChris Mason t = path->nodes[i]; 2296925baeddSChris Mason if (i >= lowest_unlock && i > skip_level && path->locks[i]) { 2297bd681513SChris Mason btrfs_tree_unlock_rw(t, path->locks[i]); 2298925baeddSChris Mason path->locks[i] = 0; 2299f7c79f30SChris Mason if (write_lock_level && 2300f7c79f30SChris Mason i > min_write_lock_level && 2301f7c79f30SChris Mason i <= *write_lock_level) { 2302f7c79f30SChris Mason *write_lock_level = i - 1; 2303f7c79f30SChris Mason } 2304925baeddSChris Mason } 2305925baeddSChris Mason } 2306925baeddSChris Mason } 2307925baeddSChris Mason 23083c69faecSChris Mason /* 2309b4ce94deSChris Mason * This releases any locks held in the path starting at level and 2310b4ce94deSChris Mason * going all the way up to the root. 2311b4ce94deSChris Mason * 2312b4ce94deSChris Mason * btrfs_search_slot will keep the lock held on higher nodes in a few 2313b4ce94deSChris Mason * corner cases, such as COW of the block at slot zero in the node. This 2314b4ce94deSChris Mason * ignores those rules, and it should only be called when there are no 2315b4ce94deSChris Mason * more updates to be done higher up in the tree. 2316b4ce94deSChris Mason */ 2317b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) 2318b4ce94deSChris Mason { 2319b4ce94deSChris Mason int i; 2320b4ce94deSChris Mason 232109a2a8f9SJosef Bacik if (path->keep_locks) 2322b4ce94deSChris Mason return; 2323b4ce94deSChris Mason 2324b4ce94deSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2325b4ce94deSChris Mason if (!path->nodes[i]) 232612f4daccSChris Mason continue; 2327b4ce94deSChris Mason if (!path->locks[i]) 232812f4daccSChris Mason continue; 2329bd681513SChris Mason btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); 2330b4ce94deSChris Mason path->locks[i] = 0; 2331b4ce94deSChris Mason } 2332b4ce94deSChris Mason } 2333b4ce94deSChris Mason 2334b4ce94deSChris Mason /* 2335c8c42864SChris Mason * helper function for btrfs_search_slot. The goal is to find a block 2336c8c42864SChris Mason * in cache without setting the path to blocking. If we find the block 2337c8c42864SChris Mason * we return zero and the path is unchanged. 2338c8c42864SChris Mason * 2339c8c42864SChris Mason * If we can't find the block, we set the path blocking and do some 2340c8c42864SChris Mason * reada. -EAGAIN is returned and the search must be repeated. 2341c8c42864SChris Mason */ 2342c8c42864SChris Mason static int 2343c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans, 2344c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 2345c8c42864SChris Mason struct extent_buffer **eb_ret, int level, int slot, 23465d9e75c4SJan Schmidt struct btrfs_key *key, u64 time_seq) 2347c8c42864SChris Mason { 2348c8c42864SChris Mason u64 blocknr; 2349c8c42864SChris Mason u64 gen; 2350c8c42864SChris Mason u32 blocksize; 2351c8c42864SChris Mason struct extent_buffer *b = *eb_ret; 2352c8c42864SChris Mason struct extent_buffer *tmp; 235376a05b35SChris Mason int ret; 2354c8c42864SChris Mason 2355c8c42864SChris Mason blocknr = btrfs_node_blockptr(b, slot); 2356c8c42864SChris Mason gen = btrfs_node_ptr_generation(b, slot); 2357c8c42864SChris Mason blocksize = btrfs_level_size(root, level - 1); 2358c8c42864SChris Mason 2359c8c42864SChris Mason tmp = btrfs_find_tree_block(root, blocknr, blocksize); 2360cb44921aSChris Mason if (tmp) { 2361b9fab919SChris Mason /* first we do an atomic uptodate check */ 2362b9fab919SChris Mason if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) { 2363b9fab919SChris Mason if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { 236476a05b35SChris Mason /* 2365cb44921aSChris Mason * we found an up to date block without 2366cb44921aSChris Mason * sleeping, return 236776a05b35SChris Mason * right away 236876a05b35SChris Mason */ 2369c8c42864SChris Mason *eb_ret = tmp; 2370c8c42864SChris Mason return 0; 2371c8c42864SChris Mason } 2372cb44921aSChris Mason /* the pages were up to date, but we failed 2373cb44921aSChris Mason * the generation number check. Do a full 2374cb44921aSChris Mason * read for the generation number that is correct. 2375cb44921aSChris Mason * We must do this without dropping locks so 2376cb44921aSChris Mason * we can trust our generation number 2377cb44921aSChris Mason */ 2378cb44921aSChris Mason free_extent_buffer(tmp); 2379bd681513SChris Mason btrfs_set_path_blocking(p); 2380bd681513SChris Mason 2381b9fab919SChris Mason /* now we're allowed to do a blocking uptodate check */ 2382cb44921aSChris Mason tmp = read_tree_block(root, blocknr, blocksize, gen); 2383b9fab919SChris Mason if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) { 2384cb44921aSChris Mason *eb_ret = tmp; 2385cb44921aSChris Mason return 0; 2386cb44921aSChris Mason } 2387cb44921aSChris Mason free_extent_buffer(tmp); 2388b3b4aa74SDavid Sterba btrfs_release_path(p); 2389cb44921aSChris Mason return -EIO; 2390cb44921aSChris Mason } 2391cb44921aSChris Mason } 2392c8c42864SChris Mason 2393c8c42864SChris Mason /* 2394c8c42864SChris Mason * reduce lock contention at high levels 2395c8c42864SChris Mason * of the btree by dropping locks before 239676a05b35SChris Mason * we read. Don't release the lock on the current 239776a05b35SChris Mason * level because we need to walk this node to figure 239876a05b35SChris Mason * out which blocks to read. 2399c8c42864SChris Mason */ 24008c594ea8SChris Mason btrfs_unlock_up_safe(p, level + 1); 24018c594ea8SChris Mason btrfs_set_path_blocking(p); 24028c594ea8SChris Mason 2403c8c42864SChris Mason free_extent_buffer(tmp); 2404c8c42864SChris Mason if (p->reada) 2405c8c42864SChris Mason reada_for_search(root, p, level, slot, key->objectid); 2406c8c42864SChris Mason 2407b3b4aa74SDavid Sterba btrfs_release_path(p); 240876a05b35SChris Mason 240976a05b35SChris Mason ret = -EAGAIN; 24105bdd3536SYan, Zheng tmp = read_tree_block(root, blocknr, blocksize, 0); 241176a05b35SChris Mason if (tmp) { 241276a05b35SChris Mason /* 241376a05b35SChris Mason * If the read above didn't mark this buffer up to date, 241476a05b35SChris Mason * it will never end up being up to date. Set ret to EIO now 241576a05b35SChris Mason * and give up so that our caller doesn't loop forever 241676a05b35SChris Mason * on our EAGAINs. 241776a05b35SChris Mason */ 2418b9fab919SChris Mason if (!btrfs_buffer_uptodate(tmp, 0, 0)) 241976a05b35SChris Mason ret = -EIO; 2420c8c42864SChris Mason free_extent_buffer(tmp); 242176a05b35SChris Mason } 242276a05b35SChris Mason return ret; 2423c8c42864SChris Mason } 2424c8c42864SChris Mason 2425c8c42864SChris Mason /* 2426c8c42864SChris Mason * helper function for btrfs_search_slot. This does all of the checks 2427c8c42864SChris Mason * for node-level blocks and does any balancing required based on 2428c8c42864SChris Mason * the ins_len. 2429c8c42864SChris Mason * 2430c8c42864SChris Mason * If no extra work was required, zero is returned. If we had to 2431c8c42864SChris Mason * drop the path, -EAGAIN is returned and btrfs_search_slot must 2432c8c42864SChris Mason * start over 2433c8c42864SChris Mason */ 2434c8c42864SChris Mason static int 2435c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans, 2436c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 2437bd681513SChris Mason struct extent_buffer *b, int level, int ins_len, 2438bd681513SChris Mason int *write_lock_level) 2439c8c42864SChris Mason { 2440c8c42864SChris Mason int ret; 2441c8c42864SChris Mason if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= 2442c8c42864SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 2443c8c42864SChris Mason int sret; 2444c8c42864SChris Mason 2445bd681513SChris Mason if (*write_lock_level < level + 1) { 2446bd681513SChris Mason *write_lock_level = level + 1; 2447bd681513SChris Mason btrfs_release_path(p); 2448bd681513SChris Mason goto again; 2449bd681513SChris Mason } 2450bd681513SChris Mason 2451c8c42864SChris Mason sret = reada_for_balance(root, p, level); 2452c8c42864SChris Mason if (sret) 2453c8c42864SChris Mason goto again; 2454c8c42864SChris Mason 2455c8c42864SChris Mason btrfs_set_path_blocking(p); 2456c8c42864SChris Mason sret = split_node(trans, root, p, level); 2457bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 2458c8c42864SChris Mason 2459c8c42864SChris Mason BUG_ON(sret > 0); 2460c8c42864SChris Mason if (sret) { 2461c8c42864SChris Mason ret = sret; 2462c8c42864SChris Mason goto done; 2463c8c42864SChris Mason } 2464c8c42864SChris Mason b = p->nodes[level]; 2465c8c42864SChris Mason } else if (ins_len < 0 && btrfs_header_nritems(b) < 2466cfbb9308SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { 2467c8c42864SChris Mason int sret; 2468c8c42864SChris Mason 2469bd681513SChris Mason if (*write_lock_level < level + 1) { 2470bd681513SChris Mason *write_lock_level = level + 1; 2471bd681513SChris Mason btrfs_release_path(p); 2472bd681513SChris Mason goto again; 2473bd681513SChris Mason } 2474bd681513SChris Mason 2475c8c42864SChris Mason sret = reada_for_balance(root, p, level); 2476c8c42864SChris Mason if (sret) 2477c8c42864SChris Mason goto again; 2478c8c42864SChris Mason 2479c8c42864SChris Mason btrfs_set_path_blocking(p); 2480c8c42864SChris Mason sret = balance_level(trans, root, p, level); 2481bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 2482c8c42864SChris Mason 2483c8c42864SChris Mason if (sret) { 2484c8c42864SChris Mason ret = sret; 2485c8c42864SChris Mason goto done; 2486c8c42864SChris Mason } 2487c8c42864SChris Mason b = p->nodes[level]; 2488c8c42864SChris Mason if (!b) { 2489b3b4aa74SDavid Sterba btrfs_release_path(p); 2490c8c42864SChris Mason goto again; 2491c8c42864SChris Mason } 2492c8c42864SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 2493c8c42864SChris Mason } 2494c8c42864SChris Mason return 0; 2495c8c42864SChris Mason 2496c8c42864SChris Mason again: 2497c8c42864SChris Mason ret = -EAGAIN; 2498c8c42864SChris Mason done: 2499c8c42864SChris Mason return ret; 2500c8c42864SChris Mason } 2501c8c42864SChris Mason 2502c8c42864SChris Mason /* 250374123bd7SChris Mason * look for key in the tree. path is filled in with nodes along the way 250474123bd7SChris Mason * if key is found, we return zero and you can find the item in the leaf 250574123bd7SChris Mason * level of the path (level 0) 250674123bd7SChris Mason * 250774123bd7SChris Mason * If the key isn't found, the path points to the slot where it should 2508aa5d6bedSChris Mason * be inserted, and 1 is returned. If there are other errors during the 2509aa5d6bedSChris Mason * search a negative error number is returned. 251097571fd0SChris Mason * 251197571fd0SChris Mason * if ins_len > 0, nodes and leaves will be split as we walk down the 251297571fd0SChris Mason * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if 251397571fd0SChris Mason * possible) 251474123bd7SChris Mason */ 2515e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root 2516e089f05cSChris Mason *root, struct btrfs_key *key, struct btrfs_path *p, int 2517e089f05cSChris Mason ins_len, int cow) 2518be0e5c09SChris Mason { 25195f39d397SChris Mason struct extent_buffer *b; 2520be0e5c09SChris Mason int slot; 2521be0e5c09SChris Mason int ret; 252233c66f43SYan Zheng int err; 2523be0e5c09SChris Mason int level; 2524925baeddSChris Mason int lowest_unlock = 1; 2525bd681513SChris Mason int root_lock; 2526bd681513SChris Mason /* everything at write_lock_level or lower must be write locked */ 2527bd681513SChris Mason int write_lock_level = 0; 25289f3a7427SChris Mason u8 lowest_level = 0; 2529f7c79f30SChris Mason int min_write_lock_level; 25309f3a7427SChris Mason 25316702ed49SChris Mason lowest_level = p->lowest_level; 2532323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 253322b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 253425179201SJosef Bacik 2535bd681513SChris Mason if (ins_len < 0) { 2536925baeddSChris Mason lowest_unlock = 2; 253765b51a00SChris Mason 2538bd681513SChris Mason /* when we are removing items, we might have to go up to level 2539bd681513SChris Mason * two as we update tree pointers Make sure we keep write 2540bd681513SChris Mason * for those levels as well 2541bd681513SChris Mason */ 2542bd681513SChris Mason write_lock_level = 2; 2543bd681513SChris Mason } else if (ins_len > 0) { 2544bd681513SChris Mason /* 2545bd681513SChris Mason * for inserting items, make sure we have a write lock on 2546bd681513SChris Mason * level 1 so we can update keys 2547bd681513SChris Mason */ 2548bd681513SChris Mason write_lock_level = 1; 2549bd681513SChris Mason } 2550bd681513SChris Mason 2551bd681513SChris Mason if (!cow) 2552bd681513SChris Mason write_lock_level = -1; 2553bd681513SChris Mason 255409a2a8f9SJosef Bacik if (cow && (p->keep_locks || p->lowest_level)) 2555bd681513SChris Mason write_lock_level = BTRFS_MAX_LEVEL; 2556bd681513SChris Mason 2557f7c79f30SChris Mason min_write_lock_level = write_lock_level; 2558f7c79f30SChris Mason 2559bb803951SChris Mason again: 2560bd681513SChris Mason /* 2561bd681513SChris Mason * we try very hard to do read locks on the root 2562bd681513SChris Mason */ 2563bd681513SChris Mason root_lock = BTRFS_READ_LOCK; 2564bd681513SChris Mason level = 0; 25655d4f98a2SYan Zheng if (p->search_commit_root) { 2566bd681513SChris Mason /* 2567bd681513SChris Mason * the commit roots are read only 2568bd681513SChris Mason * so we always do read locks 2569bd681513SChris Mason */ 25705d4f98a2SYan Zheng b = root->commit_root; 25715d4f98a2SYan Zheng extent_buffer_get(b); 2572bd681513SChris Mason level = btrfs_header_level(b); 25735d4f98a2SYan Zheng if (!p->skip_locking) 2574bd681513SChris Mason btrfs_tree_read_lock(b); 25755d4f98a2SYan Zheng } else { 2576bd681513SChris Mason if (p->skip_locking) { 25775cd57b2cSChris Mason b = btrfs_root_node(root); 2578bd681513SChris Mason level = btrfs_header_level(b); 2579bd681513SChris Mason } else { 2580bd681513SChris Mason /* we don't know the level of the root node 2581bd681513SChris Mason * until we actually have it read locked 2582bd681513SChris Mason */ 2583bd681513SChris Mason b = btrfs_read_lock_root_node(root); 2584bd681513SChris Mason level = btrfs_header_level(b); 2585bd681513SChris Mason if (level <= write_lock_level) { 2586bd681513SChris Mason /* whoops, must trade for write lock */ 2587bd681513SChris Mason btrfs_tree_read_unlock(b); 2588bd681513SChris Mason free_extent_buffer(b); 2589925baeddSChris Mason b = btrfs_lock_root_node(root); 2590bd681513SChris Mason root_lock = BTRFS_WRITE_LOCK; 2591bd681513SChris Mason 2592bd681513SChris Mason /* the level might have changed, check again */ 2593bd681513SChris Mason level = btrfs_header_level(b); 25945d4f98a2SYan Zheng } 2595bd681513SChris Mason } 2596bd681513SChris Mason } 2597bd681513SChris Mason p->nodes[level] = b; 2598bd681513SChris Mason if (!p->skip_locking) 2599bd681513SChris Mason p->locks[level] = root_lock; 2600925baeddSChris Mason 2601eb60ceacSChris Mason while (b) { 26025f39d397SChris Mason level = btrfs_header_level(b); 260365b51a00SChris Mason 260465b51a00SChris Mason /* 260565b51a00SChris Mason * setup the path here so we can release it under lock 260665b51a00SChris Mason * contention with the cow code 260765b51a00SChris Mason */ 260802217ed2SChris Mason if (cow) { 2609c8c42864SChris Mason /* 2610c8c42864SChris Mason * if we don't really need to cow this block 2611c8c42864SChris Mason * then we don't want to set the path blocking, 2612c8c42864SChris Mason * so we test it here 2613c8c42864SChris Mason */ 26145d4f98a2SYan Zheng if (!should_cow_block(trans, root, b)) 261565b51a00SChris Mason goto cow_done; 26165d4f98a2SYan Zheng 2617b4ce94deSChris Mason btrfs_set_path_blocking(p); 2618b4ce94deSChris Mason 2619bd681513SChris Mason /* 2620bd681513SChris Mason * must have write locks on this node and the 2621bd681513SChris Mason * parent 2622bd681513SChris Mason */ 26235124e00eSJosef Bacik if (level > write_lock_level || 26245124e00eSJosef Bacik (level + 1 > write_lock_level && 26255124e00eSJosef Bacik level + 1 < BTRFS_MAX_LEVEL && 26265124e00eSJosef Bacik p->nodes[level + 1])) { 2627bd681513SChris Mason write_lock_level = level + 1; 2628bd681513SChris Mason btrfs_release_path(p); 2629bd681513SChris Mason goto again; 2630bd681513SChris Mason } 2631bd681513SChris Mason 263233c66f43SYan Zheng err = btrfs_cow_block(trans, root, b, 2633e20d96d6SChris Mason p->nodes[level + 1], 26349fa8cfe7SChris Mason p->slots[level + 1], &b); 263533c66f43SYan Zheng if (err) { 263633c66f43SYan Zheng ret = err; 263765b51a00SChris Mason goto done; 263854aa1f4dSChris Mason } 263902217ed2SChris Mason } 264065b51a00SChris Mason cow_done: 264102217ed2SChris Mason BUG_ON(!cow && ins_len); 264265b51a00SChris Mason 2643eb60ceacSChris Mason p->nodes[level] = b; 2644bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 2645b4ce94deSChris Mason 2646b4ce94deSChris Mason /* 2647b4ce94deSChris Mason * we have a lock on b and as long as we aren't changing 2648b4ce94deSChris Mason * the tree, there is no way to for the items in b to change. 2649b4ce94deSChris Mason * It is safe to drop the lock on our parent before we 2650b4ce94deSChris Mason * go through the expensive btree search on b. 2651b4ce94deSChris Mason * 2652b4ce94deSChris Mason * If cow is true, then we might be changing slot zero, 2653b4ce94deSChris Mason * which may require changing the parent. So, we can't 2654b4ce94deSChris Mason * drop the lock until after we know which slot we're 2655b4ce94deSChris Mason * operating on. 2656b4ce94deSChris Mason */ 2657b4ce94deSChris Mason if (!cow) 2658b4ce94deSChris Mason btrfs_unlock_up_safe(p, level + 1); 2659b4ce94deSChris Mason 26605f39d397SChris Mason ret = bin_search(b, key, level, &slot); 2661b4ce94deSChris Mason 26625f39d397SChris Mason if (level != 0) { 266333c66f43SYan Zheng int dec = 0; 266433c66f43SYan Zheng if (ret && slot > 0) { 266533c66f43SYan Zheng dec = 1; 2666be0e5c09SChris Mason slot -= 1; 266733c66f43SYan Zheng } 2668be0e5c09SChris Mason p->slots[level] = slot; 266933c66f43SYan Zheng err = setup_nodes_for_search(trans, root, p, b, level, 2670bd681513SChris Mason ins_len, &write_lock_level); 267133c66f43SYan Zheng if (err == -EAGAIN) 2672b4ce94deSChris Mason goto again; 267333c66f43SYan Zheng if (err) { 267433c66f43SYan Zheng ret = err; 267565b51a00SChris Mason goto done; 267633c66f43SYan Zheng } 26775c680ed6SChris Mason b = p->nodes[level]; 26785c680ed6SChris Mason slot = p->slots[level]; 2679b4ce94deSChris Mason 2680bd681513SChris Mason /* 2681bd681513SChris Mason * slot 0 is special, if we change the key 2682bd681513SChris Mason * we have to update the parent pointer 2683bd681513SChris Mason * which means we must have a write lock 2684bd681513SChris Mason * on the parent 2685bd681513SChris Mason */ 2686bd681513SChris Mason if (slot == 0 && cow && 2687bd681513SChris Mason write_lock_level < level + 1) { 2688bd681513SChris Mason write_lock_level = level + 1; 2689bd681513SChris Mason btrfs_release_path(p); 2690bd681513SChris Mason goto again; 2691bd681513SChris Mason } 2692bd681513SChris Mason 2693f7c79f30SChris Mason unlock_up(p, level, lowest_unlock, 2694f7c79f30SChris Mason min_write_lock_level, &write_lock_level); 2695f9efa9c7SChris Mason 2696925baeddSChris Mason if (level == lowest_level) { 269733c66f43SYan Zheng if (dec) 269833c66f43SYan Zheng p->slots[level]++; 26995b21f2edSZheng Yan goto done; 2700925baeddSChris Mason } 2701ca7a79adSChris Mason 270233c66f43SYan Zheng err = read_block_for_search(trans, root, p, 27035d9e75c4SJan Schmidt &b, level, slot, key, 0); 270433c66f43SYan Zheng if (err == -EAGAIN) 2705051e1b9fSChris Mason goto again; 270633c66f43SYan Zheng if (err) { 270733c66f43SYan Zheng ret = err; 270876a05b35SChris Mason goto done; 270933c66f43SYan Zheng } 271076a05b35SChris Mason 2711b4ce94deSChris Mason if (!p->skip_locking) { 2712bd681513SChris Mason level = btrfs_header_level(b); 2713bd681513SChris Mason if (level <= write_lock_level) { 2714bd681513SChris Mason err = btrfs_try_tree_write_lock(b); 271533c66f43SYan Zheng if (!err) { 2716b4ce94deSChris Mason btrfs_set_path_blocking(p); 2717925baeddSChris Mason btrfs_tree_lock(b); 2718bd681513SChris Mason btrfs_clear_path_blocking(p, b, 2719bd681513SChris Mason BTRFS_WRITE_LOCK); 2720b4ce94deSChris Mason } 2721bd681513SChris Mason p->locks[level] = BTRFS_WRITE_LOCK; 2722bd681513SChris Mason } else { 2723bd681513SChris Mason err = btrfs_try_tree_read_lock(b); 2724bd681513SChris Mason if (!err) { 2725bd681513SChris Mason btrfs_set_path_blocking(p); 2726bd681513SChris Mason btrfs_tree_read_lock(b); 2727bd681513SChris Mason btrfs_clear_path_blocking(p, b, 2728bd681513SChris Mason BTRFS_READ_LOCK); 2729bd681513SChris Mason } 2730bd681513SChris Mason p->locks[level] = BTRFS_READ_LOCK; 2731bd681513SChris Mason } 2732bd681513SChris Mason p->nodes[level] = b; 2733b4ce94deSChris Mason } 2734be0e5c09SChris Mason } else { 2735be0e5c09SChris Mason p->slots[level] = slot; 273687b29b20SYan Zheng if (ins_len > 0 && 273787b29b20SYan Zheng btrfs_leaf_free_space(root, b) < ins_len) { 2738bd681513SChris Mason if (write_lock_level < 1) { 2739bd681513SChris Mason write_lock_level = 1; 2740bd681513SChris Mason btrfs_release_path(p); 2741bd681513SChris Mason goto again; 2742bd681513SChris Mason } 2743bd681513SChris Mason 2744b4ce94deSChris Mason btrfs_set_path_blocking(p); 274533c66f43SYan Zheng err = split_leaf(trans, root, key, 2746cc0c5538SChris Mason p, ins_len, ret == 0); 2747bd681513SChris Mason btrfs_clear_path_blocking(p, NULL, 0); 2748b4ce94deSChris Mason 274933c66f43SYan Zheng BUG_ON(err > 0); 275033c66f43SYan Zheng if (err) { 275133c66f43SYan Zheng ret = err; 275265b51a00SChris Mason goto done; 275365b51a00SChris Mason } 27545c680ed6SChris Mason } 2755459931ecSChris Mason if (!p->search_for_split) 2756f7c79f30SChris Mason unlock_up(p, level, lowest_unlock, 2757f7c79f30SChris Mason min_write_lock_level, &write_lock_level); 275865b51a00SChris Mason goto done; 275965b51a00SChris Mason } 276065b51a00SChris Mason } 276165b51a00SChris Mason ret = 1; 276265b51a00SChris Mason done: 2763b4ce94deSChris Mason /* 2764b4ce94deSChris Mason * we don't really know what they plan on doing with the path 2765b4ce94deSChris Mason * from here on, so for now just mark it as blocking 2766b4ce94deSChris Mason */ 2767b9473439SChris Mason if (!p->leave_spinning) 2768b4ce94deSChris Mason btrfs_set_path_blocking(p); 276976a05b35SChris Mason if (ret < 0) 2770b3b4aa74SDavid Sterba btrfs_release_path(p); 2771be0e5c09SChris Mason return ret; 2772be0e5c09SChris Mason } 2773be0e5c09SChris Mason 277474123bd7SChris Mason /* 27755d9e75c4SJan Schmidt * Like btrfs_search_slot, this looks for a key in the given tree. It uses the 27765d9e75c4SJan Schmidt * current state of the tree together with the operations recorded in the tree 27775d9e75c4SJan Schmidt * modification log to search for the key in a previous version of this tree, as 27785d9e75c4SJan Schmidt * denoted by the time_seq parameter. 27795d9e75c4SJan Schmidt * 27805d9e75c4SJan Schmidt * Naturally, there is no support for insert, delete or cow operations. 27815d9e75c4SJan Schmidt * 27825d9e75c4SJan Schmidt * The resulting path and return value will be set up as if we called 27835d9e75c4SJan Schmidt * btrfs_search_slot at that point in time with ins_len and cow both set to 0. 27845d9e75c4SJan Schmidt */ 27855d9e75c4SJan Schmidt int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key, 27865d9e75c4SJan Schmidt struct btrfs_path *p, u64 time_seq) 27875d9e75c4SJan Schmidt { 27885d9e75c4SJan Schmidt struct extent_buffer *b; 27895d9e75c4SJan Schmidt int slot; 27905d9e75c4SJan Schmidt int ret; 27915d9e75c4SJan Schmidt int err; 27925d9e75c4SJan Schmidt int level; 27935d9e75c4SJan Schmidt int lowest_unlock = 1; 27945d9e75c4SJan Schmidt u8 lowest_level = 0; 27955d9e75c4SJan Schmidt 27965d9e75c4SJan Schmidt lowest_level = p->lowest_level; 27975d9e75c4SJan Schmidt WARN_ON(p->nodes[0] != NULL); 27985d9e75c4SJan Schmidt 27995d9e75c4SJan Schmidt if (p->search_commit_root) { 28005d9e75c4SJan Schmidt BUG_ON(time_seq); 28015d9e75c4SJan Schmidt return btrfs_search_slot(NULL, root, key, p, 0, 0); 28025d9e75c4SJan Schmidt } 28035d9e75c4SJan Schmidt 28045d9e75c4SJan Schmidt again: 28055d9e75c4SJan Schmidt b = get_old_root(root, time_seq); 28065d9e75c4SJan Schmidt level = btrfs_header_level(b); 28075d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 28085d9e75c4SJan Schmidt 28095d9e75c4SJan Schmidt while (b) { 28105d9e75c4SJan Schmidt level = btrfs_header_level(b); 28115d9e75c4SJan Schmidt p->nodes[level] = b; 28125d9e75c4SJan Schmidt btrfs_clear_path_blocking(p, NULL, 0); 28135d9e75c4SJan Schmidt 28145d9e75c4SJan Schmidt /* 28155d9e75c4SJan Schmidt * we have a lock on b and as long as we aren't changing 28165d9e75c4SJan Schmidt * the tree, there is no way to for the items in b to change. 28175d9e75c4SJan Schmidt * It is safe to drop the lock on our parent before we 28185d9e75c4SJan Schmidt * go through the expensive btree search on b. 28195d9e75c4SJan Schmidt */ 28205d9e75c4SJan Schmidt btrfs_unlock_up_safe(p, level + 1); 28215d9e75c4SJan Schmidt 28225d9e75c4SJan Schmidt ret = bin_search(b, key, level, &slot); 28235d9e75c4SJan Schmidt 28245d9e75c4SJan Schmidt if (level != 0) { 28255d9e75c4SJan Schmidt int dec = 0; 28265d9e75c4SJan Schmidt if (ret && slot > 0) { 28275d9e75c4SJan Schmidt dec = 1; 28285d9e75c4SJan Schmidt slot -= 1; 28295d9e75c4SJan Schmidt } 28305d9e75c4SJan Schmidt p->slots[level] = slot; 28315d9e75c4SJan Schmidt unlock_up(p, level, lowest_unlock, 0, NULL); 28325d9e75c4SJan Schmidt 28335d9e75c4SJan Schmidt if (level == lowest_level) { 28345d9e75c4SJan Schmidt if (dec) 28355d9e75c4SJan Schmidt p->slots[level]++; 28365d9e75c4SJan Schmidt goto done; 28375d9e75c4SJan Schmidt } 28385d9e75c4SJan Schmidt 28395d9e75c4SJan Schmidt err = read_block_for_search(NULL, root, p, &b, level, 28405d9e75c4SJan Schmidt slot, key, time_seq); 28415d9e75c4SJan Schmidt if (err == -EAGAIN) 28425d9e75c4SJan Schmidt goto again; 28435d9e75c4SJan Schmidt if (err) { 28445d9e75c4SJan Schmidt ret = err; 28455d9e75c4SJan Schmidt goto done; 28465d9e75c4SJan Schmidt } 28475d9e75c4SJan Schmidt 28485d9e75c4SJan Schmidt level = btrfs_header_level(b); 28495d9e75c4SJan Schmidt err = btrfs_try_tree_read_lock(b); 28505d9e75c4SJan Schmidt if (!err) { 28515d9e75c4SJan Schmidt btrfs_set_path_blocking(p); 28525d9e75c4SJan Schmidt btrfs_tree_read_lock(b); 28535d9e75c4SJan Schmidt btrfs_clear_path_blocking(p, b, 28545d9e75c4SJan Schmidt BTRFS_READ_LOCK); 28555d9e75c4SJan Schmidt } 285647fb091fSJan Schmidt b = tree_mod_log_rewind(root->fs_info, b, time_seq); 28575d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 28585d9e75c4SJan Schmidt p->nodes[level] = b; 28595d9e75c4SJan Schmidt } else { 28605d9e75c4SJan Schmidt p->slots[level] = slot; 28615d9e75c4SJan Schmidt unlock_up(p, level, lowest_unlock, 0, NULL); 28625d9e75c4SJan Schmidt goto done; 28635d9e75c4SJan Schmidt } 28645d9e75c4SJan Schmidt } 28655d9e75c4SJan Schmidt ret = 1; 28665d9e75c4SJan Schmidt done: 28675d9e75c4SJan Schmidt if (!p->leave_spinning) 28685d9e75c4SJan Schmidt btrfs_set_path_blocking(p); 28695d9e75c4SJan Schmidt if (ret < 0) 28705d9e75c4SJan Schmidt btrfs_release_path(p); 28715d9e75c4SJan Schmidt 28725d9e75c4SJan Schmidt return ret; 28735d9e75c4SJan Schmidt } 28745d9e75c4SJan Schmidt 28755d9e75c4SJan Schmidt /* 28762f38b3e1SArne Jansen * helper to use instead of search slot if no exact match is needed but 28772f38b3e1SArne Jansen * instead the next or previous item should be returned. 28782f38b3e1SArne Jansen * When find_higher is true, the next higher item is returned, the next lower 28792f38b3e1SArne Jansen * otherwise. 28802f38b3e1SArne Jansen * When return_any and find_higher are both true, and no higher item is found, 28812f38b3e1SArne Jansen * return the next lower instead. 28822f38b3e1SArne Jansen * When return_any is true and find_higher is false, and no lower item is found, 28832f38b3e1SArne Jansen * return the next higher instead. 28842f38b3e1SArne Jansen * It returns 0 if any item is found, 1 if none is found (tree empty), and 28852f38b3e1SArne Jansen * < 0 on error 28862f38b3e1SArne Jansen */ 28872f38b3e1SArne Jansen int btrfs_search_slot_for_read(struct btrfs_root *root, 28882f38b3e1SArne Jansen struct btrfs_key *key, struct btrfs_path *p, 28892f38b3e1SArne Jansen int find_higher, int return_any) 28902f38b3e1SArne Jansen { 28912f38b3e1SArne Jansen int ret; 28922f38b3e1SArne Jansen struct extent_buffer *leaf; 28932f38b3e1SArne Jansen 28942f38b3e1SArne Jansen again: 28952f38b3e1SArne Jansen ret = btrfs_search_slot(NULL, root, key, p, 0, 0); 28962f38b3e1SArne Jansen if (ret <= 0) 28972f38b3e1SArne Jansen return ret; 28982f38b3e1SArne Jansen /* 28992f38b3e1SArne Jansen * a return value of 1 means the path is at the position where the 29002f38b3e1SArne Jansen * item should be inserted. Normally this is the next bigger item, 29012f38b3e1SArne Jansen * but in case the previous item is the last in a leaf, path points 29022f38b3e1SArne Jansen * to the first free slot in the previous leaf, i.e. at an invalid 29032f38b3e1SArne Jansen * item. 29042f38b3e1SArne Jansen */ 29052f38b3e1SArne Jansen leaf = p->nodes[0]; 29062f38b3e1SArne Jansen 29072f38b3e1SArne Jansen if (find_higher) { 29082f38b3e1SArne Jansen if (p->slots[0] >= btrfs_header_nritems(leaf)) { 29092f38b3e1SArne Jansen ret = btrfs_next_leaf(root, p); 29102f38b3e1SArne Jansen if (ret <= 0) 29112f38b3e1SArne Jansen return ret; 29122f38b3e1SArne Jansen if (!return_any) 29132f38b3e1SArne Jansen return 1; 29142f38b3e1SArne Jansen /* 29152f38b3e1SArne Jansen * no higher item found, return the next 29162f38b3e1SArne Jansen * lower instead 29172f38b3e1SArne Jansen */ 29182f38b3e1SArne Jansen return_any = 0; 29192f38b3e1SArne Jansen find_higher = 0; 29202f38b3e1SArne Jansen btrfs_release_path(p); 29212f38b3e1SArne Jansen goto again; 29222f38b3e1SArne Jansen } 29232f38b3e1SArne Jansen } else { 29242f38b3e1SArne Jansen if (p->slots[0] == 0) { 29252f38b3e1SArne Jansen ret = btrfs_prev_leaf(root, p); 2926e6793769SArne Jansen if (ret < 0) 29272f38b3e1SArne Jansen return ret; 2928e6793769SArne Jansen if (!ret) { 2929e6793769SArne Jansen p->slots[0] = btrfs_header_nritems(leaf) - 1; 2930e6793769SArne Jansen return 0; 2931e6793769SArne Jansen } 29322f38b3e1SArne Jansen if (!return_any) 29332f38b3e1SArne Jansen return 1; 29342f38b3e1SArne Jansen /* 29352f38b3e1SArne Jansen * no lower item found, return the next 29362f38b3e1SArne Jansen * higher instead 29372f38b3e1SArne Jansen */ 29382f38b3e1SArne Jansen return_any = 0; 29392f38b3e1SArne Jansen find_higher = 1; 29402f38b3e1SArne Jansen btrfs_release_path(p); 29412f38b3e1SArne Jansen goto again; 2942e6793769SArne Jansen } else { 29432f38b3e1SArne Jansen --p->slots[0]; 29442f38b3e1SArne Jansen } 29452f38b3e1SArne Jansen } 29462f38b3e1SArne Jansen return 0; 29472f38b3e1SArne Jansen } 29482f38b3e1SArne Jansen 29492f38b3e1SArne Jansen /* 295074123bd7SChris Mason * adjust the pointers going up the tree, starting at level 295174123bd7SChris Mason * making sure the right key of each node is points to 'key'. 295274123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 295374123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 295474123bd7SChris Mason * higher levels 2955aa5d6bedSChris Mason * 295674123bd7SChris Mason */ 2957d6a0a126STsutomu Itoh static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path, 29585f39d397SChris Mason struct btrfs_disk_key *key, int level) 2959be0e5c09SChris Mason { 2960be0e5c09SChris Mason int i; 29615f39d397SChris Mason struct extent_buffer *t; 29625f39d397SChris Mason 2963234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2964be0e5c09SChris Mason int tslot = path->slots[i]; 2965eb60ceacSChris Mason if (!path->nodes[i]) 2966be0e5c09SChris Mason break; 29675f39d397SChris Mason t = path->nodes[i]; 296832adf090SLiu Bo tree_mod_log_set_node_key(root->fs_info, t, tslot, 1); 29695f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 2970d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 2971be0e5c09SChris Mason if (tslot != 0) 2972be0e5c09SChris Mason break; 2973be0e5c09SChris Mason } 2974be0e5c09SChris Mason } 2975be0e5c09SChris Mason 297674123bd7SChris Mason /* 297731840ae1SZheng Yan * update item key. 297831840ae1SZheng Yan * 297931840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 298031840ae1SZheng Yan * that the new key won't break the order 298131840ae1SZheng Yan */ 2982afe5fea7STsutomu Itoh void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path, 298331840ae1SZheng Yan struct btrfs_key *new_key) 298431840ae1SZheng Yan { 298531840ae1SZheng Yan struct btrfs_disk_key disk_key; 298631840ae1SZheng Yan struct extent_buffer *eb; 298731840ae1SZheng Yan int slot; 298831840ae1SZheng Yan 298931840ae1SZheng Yan eb = path->nodes[0]; 299031840ae1SZheng Yan slot = path->slots[0]; 299131840ae1SZheng Yan if (slot > 0) { 299231840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 2993143bede5SJeff Mahoney BUG_ON(comp_keys(&disk_key, new_key) >= 0); 299431840ae1SZheng Yan } 299531840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 299631840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 2997143bede5SJeff Mahoney BUG_ON(comp_keys(&disk_key, new_key) <= 0); 299831840ae1SZheng Yan } 299931840ae1SZheng Yan 300031840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 300131840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 300231840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 300331840ae1SZheng Yan if (slot == 0) 3004d6a0a126STsutomu Itoh fixup_low_keys(root, path, &disk_key, 1); 300531840ae1SZheng Yan } 300631840ae1SZheng Yan 300731840ae1SZheng Yan /* 300874123bd7SChris Mason * try to push data from one node into the next node left in the 300979f95c82SChris Mason * tree. 3010aa5d6bedSChris Mason * 3011aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 3012aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 301374123bd7SChris Mason */ 301498ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 301598ed5174SChris Mason struct btrfs_root *root, struct extent_buffer *dst, 3016971a1f66SChris Mason struct extent_buffer *src, int empty) 3017be0e5c09SChris Mason { 3018be0e5c09SChris Mason int push_items = 0; 3019bb803951SChris Mason int src_nritems; 3020bb803951SChris Mason int dst_nritems; 3021aa5d6bedSChris Mason int ret = 0; 3022be0e5c09SChris Mason 30235f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 30245f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 3025123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 30267bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 30277bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 302854aa1f4dSChris Mason 3029bce4eae9SChris Mason if (!empty && src_nritems <= 8) 3030971a1f66SChris Mason return 1; 3031971a1f66SChris Mason 3032d397712bSChris Mason if (push_items <= 0) 3033be0e5c09SChris Mason return 1; 3034be0e5c09SChris Mason 3035bce4eae9SChris Mason if (empty) { 3036971a1f66SChris Mason push_items = min(src_nritems, push_items); 3037bce4eae9SChris Mason if (push_items < src_nritems) { 3038bce4eae9SChris Mason /* leave at least 8 pointers in the node if 3039bce4eae9SChris Mason * we aren't going to empty it 3040bce4eae9SChris Mason */ 3041bce4eae9SChris Mason if (src_nritems - push_items < 8) { 3042bce4eae9SChris Mason if (push_items <= 8) 3043bce4eae9SChris Mason return 1; 3044bce4eae9SChris Mason push_items -= 8; 3045bce4eae9SChris Mason } 3046bce4eae9SChris Mason } 3047bce4eae9SChris Mason } else 3048bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 304979f95c82SChris Mason 3050f230475eSJan Schmidt tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0, 305190f8d62eSJan Schmidt push_items); 30525f39d397SChris Mason copy_extent_buffer(dst, src, 30535f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 30545f39d397SChris Mason btrfs_node_key_ptr_offset(0), 3055123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 30565f39d397SChris Mason 3057bb803951SChris Mason if (push_items < src_nritems) { 305857911b8bSJan Schmidt /* 305957911b8bSJan Schmidt * don't call tree_mod_log_eb_move here, key removal was already 306057911b8bSJan Schmidt * fully logged by tree_mod_log_eb_copy above. 306157911b8bSJan Schmidt */ 30625f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 30635f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 3064e2fa7227SChris Mason (src_nritems - push_items) * 3065123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 3066bb803951SChris Mason } 30675f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 30685f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 30695f39d397SChris Mason btrfs_mark_buffer_dirty(src); 30705f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 307131840ae1SZheng Yan 3072bb803951SChris Mason return ret; 3073be0e5c09SChris Mason } 3074be0e5c09SChris Mason 307597571fd0SChris Mason /* 307679f95c82SChris Mason * try to push data from one node into the next node right in the 307779f95c82SChris Mason * tree. 307879f95c82SChris Mason * 307979f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 308079f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 308179f95c82SChris Mason * 308279f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 308379f95c82SChris Mason */ 30845f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 30855f39d397SChris Mason struct btrfs_root *root, 30865f39d397SChris Mason struct extent_buffer *dst, 30875f39d397SChris Mason struct extent_buffer *src) 308879f95c82SChris Mason { 308979f95c82SChris Mason int push_items = 0; 309079f95c82SChris Mason int max_push; 309179f95c82SChris Mason int src_nritems; 309279f95c82SChris Mason int dst_nritems; 309379f95c82SChris Mason int ret = 0; 309479f95c82SChris Mason 30957bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 30967bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 30977bb86316SChris Mason 30985f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 30995f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 3100123abc88SChris Mason push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 3101d397712bSChris Mason if (push_items <= 0) 310279f95c82SChris Mason return 1; 3103bce4eae9SChris Mason 3104d397712bSChris Mason if (src_nritems < 4) 3105bce4eae9SChris Mason return 1; 310679f95c82SChris Mason 310779f95c82SChris Mason max_push = src_nritems / 2 + 1; 310879f95c82SChris Mason /* don't try to empty the node */ 3109d397712bSChris Mason if (max_push >= src_nritems) 311079f95c82SChris Mason return 1; 3111252c38f0SYan 311279f95c82SChris Mason if (max_push < push_items) 311379f95c82SChris Mason push_items = max_push; 311479f95c82SChris Mason 3115f230475eSJan Schmidt tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems); 31165f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 31175f39d397SChris Mason btrfs_node_key_ptr_offset(0), 31185f39d397SChris Mason (dst_nritems) * 31195f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 3120d6025579SChris Mason 3121f230475eSJan Schmidt tree_mod_log_eb_copy(root->fs_info, dst, src, 0, 312290f8d62eSJan Schmidt src_nritems - push_items, push_items); 31235f39d397SChris Mason copy_extent_buffer(dst, src, 31245f39d397SChris Mason btrfs_node_key_ptr_offset(0), 31255f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 3126123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 312779f95c82SChris Mason 31285f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 31295f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 313079f95c82SChris Mason 31315f39d397SChris Mason btrfs_mark_buffer_dirty(src); 31325f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 313331840ae1SZheng Yan 313479f95c82SChris Mason return ret; 313579f95c82SChris Mason } 313679f95c82SChris Mason 313779f95c82SChris Mason /* 313897571fd0SChris Mason * helper function to insert a new root level in the tree. 313997571fd0SChris Mason * A new node is allocated, and a single item is inserted to 314097571fd0SChris Mason * point to the existing root 3141aa5d6bedSChris Mason * 3142aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 314397571fd0SChris Mason */ 3144d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans, 31455f39d397SChris Mason struct btrfs_root *root, 314690f8d62eSJan Schmidt struct btrfs_path *path, int level, int log_removal) 314774123bd7SChris Mason { 31487bb86316SChris Mason u64 lower_gen; 31495f39d397SChris Mason struct extent_buffer *lower; 31505f39d397SChris Mason struct extent_buffer *c; 3151925baeddSChris Mason struct extent_buffer *old; 31525f39d397SChris Mason struct btrfs_disk_key lower_key; 31535c680ed6SChris Mason 31545c680ed6SChris Mason BUG_ON(path->nodes[level]); 31555c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 31565c680ed6SChris Mason 31577bb86316SChris Mason lower = path->nodes[level-1]; 31587bb86316SChris Mason if (level == 1) 31597bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 31607bb86316SChris Mason else 31617bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 31627bb86316SChris Mason 316331840ae1SZheng Yan c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 31645d4f98a2SYan Zheng root->root_key.objectid, &lower_key, 31655581a51aSJan Schmidt level, root->node->start, 0); 31665f39d397SChris Mason if (IS_ERR(c)) 31675f39d397SChris Mason return PTR_ERR(c); 3168925baeddSChris Mason 3169f0486c68SYan, Zheng root_add_used(root, root->nodesize); 3170f0486c68SYan, Zheng 31715d4f98a2SYan Zheng memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); 31725f39d397SChris Mason btrfs_set_header_nritems(c, 1); 31735f39d397SChris Mason btrfs_set_header_level(c, level); 3174db94535dSChris Mason btrfs_set_header_bytenr(c, c->start); 31755f39d397SChris Mason btrfs_set_header_generation(c, trans->transid); 31765d4f98a2SYan Zheng btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); 31775f39d397SChris Mason btrfs_set_header_owner(c, root->root_key.objectid); 3178d5719762SChris Mason 31795f39d397SChris Mason write_extent_buffer(c, root->fs_info->fsid, 31805f39d397SChris Mason (unsigned long)btrfs_header_fsid(c), 31815f39d397SChris Mason BTRFS_FSID_SIZE); 3182e17cade2SChris Mason 3183e17cade2SChris Mason write_extent_buffer(c, root->fs_info->chunk_tree_uuid, 3184e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(c), 3185e17cade2SChris Mason BTRFS_UUID_SIZE); 3186e17cade2SChris Mason 31875f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 3188db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 31897bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 319031840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 31917bb86316SChris Mason 31927bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 31935f39d397SChris Mason 31945f39d397SChris Mason btrfs_mark_buffer_dirty(c); 3195d5719762SChris Mason 3196925baeddSChris Mason old = root->node; 319790f8d62eSJan Schmidt tree_mod_log_set_root_pointer(root, c, log_removal); 3198240f62c8SChris Mason rcu_assign_pointer(root->node, c); 3199925baeddSChris Mason 3200925baeddSChris Mason /* the super has an extra ref to root->node */ 3201925baeddSChris Mason free_extent_buffer(old); 3202925baeddSChris Mason 32030b86a832SChris Mason add_root_to_dirty_list(root); 32045f39d397SChris Mason extent_buffer_get(c); 32055f39d397SChris Mason path->nodes[level] = c; 3206bd681513SChris Mason path->locks[level] = BTRFS_WRITE_LOCK; 320774123bd7SChris Mason path->slots[level] = 0; 320874123bd7SChris Mason return 0; 320974123bd7SChris Mason } 32105c680ed6SChris Mason 32115c680ed6SChris Mason /* 32125c680ed6SChris Mason * worker function to insert a single pointer in a node. 32135c680ed6SChris Mason * the node should have enough room for the pointer already 321497571fd0SChris Mason * 32155c680ed6SChris Mason * slot and level indicate where you want the key to go, and 32165c680ed6SChris Mason * blocknr is the block the key points to. 32175c680ed6SChris Mason */ 3218143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans, 3219143bede5SJeff Mahoney struct btrfs_root *root, struct btrfs_path *path, 3220143bede5SJeff Mahoney struct btrfs_disk_key *key, u64 bytenr, 3221c3e06965SJan Schmidt int slot, int level) 32225c680ed6SChris Mason { 32235f39d397SChris Mason struct extent_buffer *lower; 32245c680ed6SChris Mason int nritems; 3225f3ea38daSJan Schmidt int ret; 32265c680ed6SChris Mason 32275c680ed6SChris Mason BUG_ON(!path->nodes[level]); 3228f0486c68SYan, Zheng btrfs_assert_tree_locked(path->nodes[level]); 32295f39d397SChris Mason lower = path->nodes[level]; 32305f39d397SChris Mason nritems = btrfs_header_nritems(lower); 3231c293498bSStoyan Gaydarov BUG_ON(slot > nritems); 3232143bede5SJeff Mahoney BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root)); 323374123bd7SChris Mason if (slot != nritems) { 3234c3e06965SJan Schmidt if (level) 3235f3ea38daSJan Schmidt tree_mod_log_eb_move(root->fs_info, lower, slot + 1, 3236f3ea38daSJan Schmidt slot, nritems - slot); 32375f39d397SChris Mason memmove_extent_buffer(lower, 32385f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 32395f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 3240123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 324174123bd7SChris Mason } 3242c3e06965SJan Schmidt if (level) { 3243f3ea38daSJan Schmidt ret = tree_mod_log_insert_key(root->fs_info, lower, slot, 3244f3ea38daSJan Schmidt MOD_LOG_KEY_ADD); 3245f3ea38daSJan Schmidt BUG_ON(ret < 0); 3246f3ea38daSJan Schmidt } 32475f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 3248db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 324974493f7aSChris Mason WARN_ON(trans->transid == 0); 325074493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 32515f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 32525f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 325374123bd7SChris Mason } 325474123bd7SChris Mason 325597571fd0SChris Mason /* 325697571fd0SChris Mason * split the node at the specified level in path in two. 325797571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 325897571fd0SChris Mason * 325997571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 326097571fd0SChris Mason * left and right, if either one works, it returns right away. 3261aa5d6bedSChris Mason * 3262aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 326397571fd0SChris Mason */ 3264e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 3265e02119d5SChris Mason struct btrfs_root *root, 3266e02119d5SChris Mason struct btrfs_path *path, int level) 3267be0e5c09SChris Mason { 32685f39d397SChris Mason struct extent_buffer *c; 32695f39d397SChris Mason struct extent_buffer *split; 32705f39d397SChris Mason struct btrfs_disk_key disk_key; 3271be0e5c09SChris Mason int mid; 32725c680ed6SChris Mason int ret; 32737518a238SChris Mason u32 c_nritems; 3274be0e5c09SChris Mason 32755f39d397SChris Mason c = path->nodes[level]; 32767bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 32775f39d397SChris Mason if (c == root->node) { 3278d9abbf1cSJan Schmidt /* 327990f8d62eSJan Schmidt * trying to split the root, lets make a new one 328090f8d62eSJan Schmidt * 328190f8d62eSJan Schmidt * tree mod log: We pass 0 as log_removal parameter to 328290f8d62eSJan Schmidt * insert_new_root, because that root buffer will be kept as a 328390f8d62eSJan Schmidt * normal node. We are going to log removal of half of the 328490f8d62eSJan Schmidt * elements below with tree_mod_log_eb_copy. We're holding a 328590f8d62eSJan Schmidt * tree lock on the buffer, which is why we cannot race with 328690f8d62eSJan Schmidt * other tree_mod_log users. 3287d9abbf1cSJan Schmidt */ 328890f8d62eSJan Schmidt ret = insert_new_root(trans, root, path, level + 1, 0); 32895c680ed6SChris Mason if (ret) 32905c680ed6SChris Mason return ret; 3291b3612421SChris Mason } else { 3292e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 32935f39d397SChris Mason c = path->nodes[level]; 32945f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 3295c448acf0SChris Mason BTRFS_NODEPTRS_PER_BLOCK(root) - 3) 3296e66f709bSChris Mason return 0; 329754aa1f4dSChris Mason if (ret < 0) 329854aa1f4dSChris Mason return ret; 32995c680ed6SChris Mason } 3300e66f709bSChris Mason 33015f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 33025d4f98a2SYan Zheng mid = (c_nritems + 1) / 2; 33035d4f98a2SYan Zheng btrfs_node_key(c, &disk_key, mid); 33047bb86316SChris Mason 33055d4f98a2SYan Zheng split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, 33067bb86316SChris Mason root->root_key.objectid, 33075581a51aSJan Schmidt &disk_key, level, c->start, 0); 33085f39d397SChris Mason if (IS_ERR(split)) 33095f39d397SChris Mason return PTR_ERR(split); 331054aa1f4dSChris Mason 3311f0486c68SYan, Zheng root_add_used(root, root->nodesize); 3312f0486c68SYan, Zheng 33135d4f98a2SYan Zheng memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); 33145f39d397SChris Mason btrfs_set_header_level(split, btrfs_header_level(c)); 3315db94535dSChris Mason btrfs_set_header_bytenr(split, split->start); 33165f39d397SChris Mason btrfs_set_header_generation(split, trans->transid); 33175d4f98a2SYan Zheng btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); 33185f39d397SChris Mason btrfs_set_header_owner(split, root->root_key.objectid); 33195f39d397SChris Mason write_extent_buffer(split, root->fs_info->fsid, 33205f39d397SChris Mason (unsigned long)btrfs_header_fsid(split), 33215f39d397SChris Mason BTRFS_FSID_SIZE); 3322e17cade2SChris Mason write_extent_buffer(split, root->fs_info->chunk_tree_uuid, 3323e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(split), 3324e17cade2SChris Mason BTRFS_UUID_SIZE); 33255f39d397SChris Mason 332690f8d62eSJan Schmidt tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid); 33275f39d397SChris Mason copy_extent_buffer(split, c, 33285f39d397SChris Mason btrfs_node_key_ptr_offset(0), 33295f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 3330123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 33315f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 33325f39d397SChris Mason btrfs_set_header_nritems(c, mid); 3333aa5d6bedSChris Mason ret = 0; 3334aa5d6bedSChris Mason 33355f39d397SChris Mason btrfs_mark_buffer_dirty(c); 33365f39d397SChris Mason btrfs_mark_buffer_dirty(split); 33375f39d397SChris Mason 3338143bede5SJeff Mahoney insert_ptr(trans, root, path, &disk_key, split->start, 3339c3e06965SJan Schmidt path->slots[level + 1] + 1, level + 1); 3340aa5d6bedSChris Mason 33415de08d7dSChris Mason if (path->slots[level] >= mid) { 33425c680ed6SChris Mason path->slots[level] -= mid; 3343925baeddSChris Mason btrfs_tree_unlock(c); 33445f39d397SChris Mason free_extent_buffer(c); 33455f39d397SChris Mason path->nodes[level] = split; 33465c680ed6SChris Mason path->slots[level + 1] += 1; 3347eb60ceacSChris Mason } else { 3348925baeddSChris Mason btrfs_tree_unlock(split); 33495f39d397SChris Mason free_extent_buffer(split); 3350be0e5c09SChris Mason } 3351aa5d6bedSChris Mason return ret; 3352be0e5c09SChris Mason } 3353be0e5c09SChris Mason 335474123bd7SChris Mason /* 335574123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 335674123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 335774123bd7SChris Mason * space used both by the item structs and the item data 335874123bd7SChris Mason */ 33595f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 3360be0e5c09SChris Mason { 336141be1f3bSJosef Bacik struct btrfs_item *start_item; 336241be1f3bSJosef Bacik struct btrfs_item *end_item; 336341be1f3bSJosef Bacik struct btrfs_map_token token; 3364be0e5c09SChris Mason int data_len; 33655f39d397SChris Mason int nritems = btrfs_header_nritems(l); 3366d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 3367be0e5c09SChris Mason 3368be0e5c09SChris Mason if (!nr) 3369be0e5c09SChris Mason return 0; 337041be1f3bSJosef Bacik btrfs_init_map_token(&token); 337141be1f3bSJosef Bacik start_item = btrfs_item_nr(l, start); 337241be1f3bSJosef Bacik end_item = btrfs_item_nr(l, end); 337341be1f3bSJosef Bacik data_len = btrfs_token_item_offset(l, start_item, &token) + 337441be1f3bSJosef Bacik btrfs_token_item_size(l, start_item, &token); 337541be1f3bSJosef Bacik data_len = data_len - btrfs_token_item_offset(l, end_item, &token); 33760783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 3377d4dbff95SChris Mason WARN_ON(data_len < 0); 3378be0e5c09SChris Mason return data_len; 3379be0e5c09SChris Mason } 3380be0e5c09SChris Mason 338174123bd7SChris Mason /* 3382d4dbff95SChris Mason * The space between the end of the leaf items and 3383d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 3384d4dbff95SChris Mason * the leaf has left for both items and data 3385d4dbff95SChris Mason */ 3386d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root, 3387e02119d5SChris Mason struct extent_buffer *leaf) 3388d4dbff95SChris Mason { 33895f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 33905f39d397SChris Mason int ret; 33915f39d397SChris Mason ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); 33925f39d397SChris Mason if (ret < 0) { 3393d397712bSChris Mason printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " 3394d397712bSChris Mason "used %d nritems %d\n", 3395ae2f5411SJens Axboe ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), 33965f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 33975f39d397SChris Mason } 33985f39d397SChris Mason return ret; 3399d4dbff95SChris Mason } 3400d4dbff95SChris Mason 340199d8f83cSChris Mason /* 340299d8f83cSChris Mason * min slot controls the lowest index we're willing to push to the 340399d8f83cSChris Mason * right. We'll push up to and including min_slot, but no lower 340499d8f83cSChris Mason */ 340544871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, 340644871b1bSChris Mason struct btrfs_root *root, 340744871b1bSChris Mason struct btrfs_path *path, 340844871b1bSChris Mason int data_size, int empty, 340944871b1bSChris Mason struct extent_buffer *right, 341099d8f83cSChris Mason int free_space, u32 left_nritems, 341199d8f83cSChris Mason u32 min_slot) 341200ec4c51SChris Mason { 34135f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 341444871b1bSChris Mason struct extent_buffer *upper = path->nodes[1]; 3415cfed81a0SChris Mason struct btrfs_map_token token; 34165f39d397SChris Mason struct btrfs_disk_key disk_key; 341700ec4c51SChris Mason int slot; 341834a38218SChris Mason u32 i; 341900ec4c51SChris Mason int push_space = 0; 342000ec4c51SChris Mason int push_items = 0; 34210783fcfcSChris Mason struct btrfs_item *item; 342234a38218SChris Mason u32 nr; 34237518a238SChris Mason u32 right_nritems; 34245f39d397SChris Mason u32 data_end; 3425db94535dSChris Mason u32 this_item_size; 342600ec4c51SChris Mason 3427cfed81a0SChris Mason btrfs_init_map_token(&token); 3428cfed81a0SChris Mason 342934a38218SChris Mason if (empty) 343034a38218SChris Mason nr = 0; 343134a38218SChris Mason else 343299d8f83cSChris Mason nr = max_t(u32, 1, min_slot); 343334a38218SChris Mason 343431840ae1SZheng Yan if (path->slots[0] >= left_nritems) 343587b29b20SYan Zheng push_space += data_size; 343631840ae1SZheng Yan 343744871b1bSChris Mason slot = path->slots[1]; 343834a38218SChris Mason i = left_nritems - 1; 343934a38218SChris Mason while (i >= nr) { 34405f39d397SChris Mason item = btrfs_item_nr(left, i); 3441db94535dSChris Mason 344231840ae1SZheng Yan if (!empty && push_items > 0) { 344331840ae1SZheng Yan if (path->slots[0] > i) 344431840ae1SZheng Yan break; 344531840ae1SZheng Yan if (path->slots[0] == i) { 344631840ae1SZheng Yan int space = btrfs_leaf_free_space(root, left); 344731840ae1SZheng Yan if (space + push_space * 2 > free_space) 344831840ae1SZheng Yan break; 344931840ae1SZheng Yan } 345031840ae1SZheng Yan } 345131840ae1SZheng Yan 345200ec4c51SChris Mason if (path->slots[0] == i) 345387b29b20SYan Zheng push_space += data_size; 3454db94535dSChris Mason 3455db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 3456db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 345700ec4c51SChris Mason break; 345831840ae1SZheng Yan 345900ec4c51SChris Mason push_items++; 3460db94535dSChris Mason push_space += this_item_size + sizeof(*item); 346134a38218SChris Mason if (i == 0) 346234a38218SChris Mason break; 346334a38218SChris Mason i--; 3464db94535dSChris Mason } 34655f39d397SChris Mason 3466925baeddSChris Mason if (push_items == 0) 3467925baeddSChris Mason goto out_unlock; 34685f39d397SChris Mason 34696c1500f2SJulia Lawall WARN_ON(!empty && push_items == left_nritems); 34705f39d397SChris Mason 347100ec4c51SChris Mason /* push left to right */ 34725f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 347334a38218SChris Mason 34745f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 3475123abc88SChris Mason push_space -= leaf_data_end(root, left); 34765f39d397SChris Mason 347700ec4c51SChris Mason /* make room in the right data area */ 34785f39d397SChris Mason data_end = leaf_data_end(root, right); 34795f39d397SChris Mason memmove_extent_buffer(right, 34805f39d397SChris Mason btrfs_leaf_data(right) + data_end - push_space, 34815f39d397SChris Mason btrfs_leaf_data(right) + data_end, 34825f39d397SChris Mason BTRFS_LEAF_DATA_SIZE(root) - data_end); 34835f39d397SChris Mason 348400ec4c51SChris Mason /* copy from the left data area */ 34855f39d397SChris Mason copy_extent_buffer(right, left, btrfs_leaf_data(right) + 3486d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 3487d6025579SChris Mason btrfs_leaf_data(left) + leaf_data_end(root, left), 3488d6025579SChris Mason push_space); 34895f39d397SChris Mason 34905f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 34915f39d397SChris Mason btrfs_item_nr_offset(0), 34920783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 34935f39d397SChris Mason 349400ec4c51SChris Mason /* copy the items from left to right */ 34955f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 34965f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 34970783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 349800ec4c51SChris Mason 349900ec4c51SChris Mason /* update the item pointers */ 35007518a238SChris Mason right_nritems += push_items; 35015f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 3502123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 35037518a238SChris Mason for (i = 0; i < right_nritems; i++) { 35045f39d397SChris Mason item = btrfs_item_nr(right, i); 3505cfed81a0SChris Mason push_space -= btrfs_token_item_size(right, item, &token); 3506cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, push_space, &token); 3507db94535dSChris Mason } 3508db94535dSChris Mason 35097518a238SChris Mason left_nritems -= push_items; 35105f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 351100ec4c51SChris Mason 351234a38218SChris Mason if (left_nritems) 35135f39d397SChris Mason btrfs_mark_buffer_dirty(left); 3514f0486c68SYan, Zheng else 3515f0486c68SYan, Zheng clean_tree_block(trans, root, left); 3516f0486c68SYan, Zheng 35175f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3518a429e513SChris Mason 35195f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 35205f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 3521d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 352202217ed2SChris Mason 352300ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 35247518a238SChris Mason if (path->slots[0] >= left_nritems) { 35257518a238SChris Mason path->slots[0] -= left_nritems; 3526925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 3527925baeddSChris Mason clean_tree_block(trans, root, path->nodes[0]); 3528925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 35295f39d397SChris Mason free_extent_buffer(path->nodes[0]); 35305f39d397SChris Mason path->nodes[0] = right; 353100ec4c51SChris Mason path->slots[1] += 1; 353200ec4c51SChris Mason } else { 3533925baeddSChris Mason btrfs_tree_unlock(right); 35345f39d397SChris Mason free_extent_buffer(right); 353500ec4c51SChris Mason } 353600ec4c51SChris Mason return 0; 3537925baeddSChris Mason 3538925baeddSChris Mason out_unlock: 3539925baeddSChris Mason btrfs_tree_unlock(right); 3540925baeddSChris Mason free_extent_buffer(right); 3541925baeddSChris Mason return 1; 354200ec4c51SChris Mason } 3543925baeddSChris Mason 354400ec4c51SChris Mason /* 354544871b1bSChris Mason * push some data in the path leaf to the right, trying to free up at 354674123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 354744871b1bSChris Mason * 354844871b1bSChris Mason * returns 1 if the push failed because the other node didn't have enough 354944871b1bSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 355099d8f83cSChris Mason * 355199d8f83cSChris Mason * this will push starting from min_slot to the end of the leaf. It won't 355299d8f83cSChris Mason * push any slot lower than min_slot 355374123bd7SChris Mason */ 355444871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 355599d8f83cSChris Mason *root, struct btrfs_path *path, 355699d8f83cSChris Mason int min_data_size, int data_size, 355799d8f83cSChris Mason int empty, u32 min_slot) 3558be0e5c09SChris Mason { 355944871b1bSChris Mason struct extent_buffer *left = path->nodes[0]; 356044871b1bSChris Mason struct extent_buffer *right; 356144871b1bSChris Mason struct extent_buffer *upper; 356244871b1bSChris Mason int slot; 356344871b1bSChris Mason int free_space; 356444871b1bSChris Mason u32 left_nritems; 356544871b1bSChris Mason int ret; 356644871b1bSChris Mason 356744871b1bSChris Mason if (!path->nodes[1]) 356844871b1bSChris Mason return 1; 356944871b1bSChris Mason 357044871b1bSChris Mason slot = path->slots[1]; 357144871b1bSChris Mason upper = path->nodes[1]; 357244871b1bSChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 357344871b1bSChris Mason return 1; 357444871b1bSChris Mason 357544871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 357644871b1bSChris Mason 357744871b1bSChris Mason right = read_node_slot(root, upper, slot + 1); 357891ca338dSTsutomu Itoh if (right == NULL) 357991ca338dSTsutomu Itoh return 1; 358091ca338dSTsutomu Itoh 358144871b1bSChris Mason btrfs_tree_lock(right); 358244871b1bSChris Mason btrfs_set_lock_blocking(right); 358344871b1bSChris Mason 358444871b1bSChris Mason free_space = btrfs_leaf_free_space(root, right); 358544871b1bSChris Mason if (free_space < data_size) 358644871b1bSChris Mason goto out_unlock; 358744871b1bSChris Mason 358844871b1bSChris Mason /* cow and double check */ 358944871b1bSChris Mason ret = btrfs_cow_block(trans, root, right, upper, 359044871b1bSChris Mason slot + 1, &right); 359144871b1bSChris Mason if (ret) 359244871b1bSChris Mason goto out_unlock; 359344871b1bSChris Mason 359444871b1bSChris Mason free_space = btrfs_leaf_free_space(root, right); 359544871b1bSChris Mason if (free_space < data_size) 359644871b1bSChris Mason goto out_unlock; 359744871b1bSChris Mason 359844871b1bSChris Mason left_nritems = btrfs_header_nritems(left); 359944871b1bSChris Mason if (left_nritems == 0) 360044871b1bSChris Mason goto out_unlock; 360144871b1bSChris Mason 360299d8f83cSChris Mason return __push_leaf_right(trans, root, path, min_data_size, empty, 360399d8f83cSChris Mason right, free_space, left_nritems, min_slot); 360444871b1bSChris Mason out_unlock: 360544871b1bSChris Mason btrfs_tree_unlock(right); 360644871b1bSChris Mason free_extent_buffer(right); 360744871b1bSChris Mason return 1; 360844871b1bSChris Mason } 360944871b1bSChris Mason 361044871b1bSChris Mason /* 361144871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 361244871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 361399d8f83cSChris Mason * 361499d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 361599d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the 361699d8f83cSChris Mason * items 361744871b1bSChris Mason */ 361844871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, 361944871b1bSChris Mason struct btrfs_root *root, 362044871b1bSChris Mason struct btrfs_path *path, int data_size, 362144871b1bSChris Mason int empty, struct extent_buffer *left, 362299d8f83cSChris Mason int free_space, u32 right_nritems, 362399d8f83cSChris Mason u32 max_slot) 362444871b1bSChris Mason { 36255f39d397SChris Mason struct btrfs_disk_key disk_key; 36265f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 3627be0e5c09SChris Mason int i; 3628be0e5c09SChris Mason int push_space = 0; 3629be0e5c09SChris Mason int push_items = 0; 36300783fcfcSChris Mason struct btrfs_item *item; 36317518a238SChris Mason u32 old_left_nritems; 363234a38218SChris Mason u32 nr; 3633aa5d6bedSChris Mason int ret = 0; 3634db94535dSChris Mason u32 this_item_size; 3635db94535dSChris Mason u32 old_left_item_size; 3636cfed81a0SChris Mason struct btrfs_map_token token; 3637cfed81a0SChris Mason 3638cfed81a0SChris Mason btrfs_init_map_token(&token); 3639be0e5c09SChris Mason 364034a38218SChris Mason if (empty) 364199d8f83cSChris Mason nr = min(right_nritems, max_slot); 364234a38218SChris Mason else 364399d8f83cSChris Mason nr = min(right_nritems - 1, max_slot); 364434a38218SChris Mason 364534a38218SChris Mason for (i = 0; i < nr; i++) { 36465f39d397SChris Mason item = btrfs_item_nr(right, i); 3647db94535dSChris Mason 364831840ae1SZheng Yan if (!empty && push_items > 0) { 364931840ae1SZheng Yan if (path->slots[0] < i) 365031840ae1SZheng Yan break; 365131840ae1SZheng Yan if (path->slots[0] == i) { 365231840ae1SZheng Yan int space = btrfs_leaf_free_space(root, right); 365331840ae1SZheng Yan if (space + push_space * 2 > free_space) 365431840ae1SZheng Yan break; 365531840ae1SZheng Yan } 365631840ae1SZheng Yan } 365731840ae1SZheng Yan 3658be0e5c09SChris Mason if (path->slots[0] == i) 365987b29b20SYan Zheng push_space += data_size; 3660db94535dSChris Mason 3661db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 3662db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 3663be0e5c09SChris Mason break; 3664db94535dSChris Mason 3665be0e5c09SChris Mason push_items++; 3666db94535dSChris Mason push_space += this_item_size + sizeof(*item); 3667be0e5c09SChris Mason } 3668db94535dSChris Mason 3669be0e5c09SChris Mason if (push_items == 0) { 3670925baeddSChris Mason ret = 1; 3671925baeddSChris Mason goto out; 3672be0e5c09SChris Mason } 367334a38218SChris Mason if (!empty && push_items == btrfs_header_nritems(right)) 3674a429e513SChris Mason WARN_ON(1); 36755f39d397SChris Mason 3676be0e5c09SChris Mason /* push data from right to left */ 36775f39d397SChris Mason copy_extent_buffer(left, right, 36785f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 36795f39d397SChris Mason btrfs_item_nr_offset(0), 36805f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 36815f39d397SChris Mason 3682123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root) - 36835f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1); 36845f39d397SChris Mason 36855f39d397SChris Mason copy_extent_buffer(left, right, btrfs_leaf_data(left) + 3686d6025579SChris Mason leaf_data_end(root, left) - push_space, 3687123abc88SChris Mason btrfs_leaf_data(right) + 36885f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 3689be0e5c09SChris Mason push_space); 36905f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 369187b29b20SYan Zheng BUG_ON(old_left_nritems <= 0); 3692eb60ceacSChris Mason 3693db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 3694be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 36955f39d397SChris Mason u32 ioff; 3696db94535dSChris Mason 36975f39d397SChris Mason item = btrfs_item_nr(left, i); 3698db94535dSChris Mason 3699cfed81a0SChris Mason ioff = btrfs_token_item_offset(left, item, &token); 3700cfed81a0SChris Mason btrfs_set_token_item_offset(left, item, 3701cfed81a0SChris Mason ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size), 3702cfed81a0SChris Mason &token); 3703be0e5c09SChris Mason } 37045f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 3705be0e5c09SChris Mason 3706be0e5c09SChris Mason /* fixup right node */ 370731b1a2bdSJulia Lawall if (push_items > right_nritems) 370831b1a2bdSJulia Lawall WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, 3709d397712bSChris Mason right_nritems); 371034a38218SChris Mason 371134a38218SChris Mason if (push_items < right_nritems) { 37125f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 3713123abc88SChris Mason leaf_data_end(root, right); 37145f39d397SChris Mason memmove_extent_buffer(right, btrfs_leaf_data(right) + 3715d6025579SChris Mason BTRFS_LEAF_DATA_SIZE(root) - push_space, 3716d6025579SChris Mason btrfs_leaf_data(right) + 3717123abc88SChris Mason leaf_data_end(root, right), push_space); 37185f39d397SChris Mason 37195f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 37205f39d397SChris Mason btrfs_item_nr_offset(push_items), 37215f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 37220783fcfcSChris Mason sizeof(struct btrfs_item)); 372334a38218SChris Mason } 3724eef1c494SYan right_nritems -= push_items; 3725eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 3726123abc88SChris Mason push_space = BTRFS_LEAF_DATA_SIZE(root); 37275f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 37285f39d397SChris Mason item = btrfs_item_nr(right, i); 3729db94535dSChris Mason 3730cfed81a0SChris Mason push_space = push_space - btrfs_token_item_size(right, 3731cfed81a0SChris Mason item, &token); 3732cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, push_space, &token); 3733db94535dSChris Mason } 3734eb60ceacSChris Mason 37355f39d397SChris Mason btrfs_mark_buffer_dirty(left); 373634a38218SChris Mason if (right_nritems) 37375f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3738f0486c68SYan, Zheng else 3739f0486c68SYan, Zheng clean_tree_block(trans, root, right); 3740098f59c2SChris Mason 37415f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 3742d6a0a126STsutomu Itoh fixup_low_keys(root, path, &disk_key, 1); 3743be0e5c09SChris Mason 3744be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 3745be0e5c09SChris Mason if (path->slots[0] < push_items) { 3746be0e5c09SChris Mason path->slots[0] += old_left_nritems; 3747925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 37485f39d397SChris Mason free_extent_buffer(path->nodes[0]); 37495f39d397SChris Mason path->nodes[0] = left; 3750be0e5c09SChris Mason path->slots[1] -= 1; 3751be0e5c09SChris Mason } else { 3752925baeddSChris Mason btrfs_tree_unlock(left); 37535f39d397SChris Mason free_extent_buffer(left); 3754be0e5c09SChris Mason path->slots[0] -= push_items; 3755be0e5c09SChris Mason } 3756eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 3757aa5d6bedSChris Mason return ret; 3758925baeddSChris Mason out: 3759925baeddSChris Mason btrfs_tree_unlock(left); 3760925baeddSChris Mason free_extent_buffer(left); 3761925baeddSChris Mason return ret; 3762be0e5c09SChris Mason } 3763be0e5c09SChris Mason 376474123bd7SChris Mason /* 376544871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 376644871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 376799d8f83cSChris Mason * 376899d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 376999d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the 377099d8f83cSChris Mason * items 377144871b1bSChris Mason */ 377244871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 377399d8f83cSChris Mason *root, struct btrfs_path *path, int min_data_size, 377499d8f83cSChris Mason int data_size, int empty, u32 max_slot) 377544871b1bSChris Mason { 377644871b1bSChris Mason struct extent_buffer *right = path->nodes[0]; 377744871b1bSChris Mason struct extent_buffer *left; 377844871b1bSChris Mason int slot; 377944871b1bSChris Mason int free_space; 378044871b1bSChris Mason u32 right_nritems; 378144871b1bSChris Mason int ret = 0; 378244871b1bSChris Mason 378344871b1bSChris Mason slot = path->slots[1]; 378444871b1bSChris Mason if (slot == 0) 378544871b1bSChris Mason return 1; 378644871b1bSChris Mason if (!path->nodes[1]) 378744871b1bSChris Mason return 1; 378844871b1bSChris Mason 378944871b1bSChris Mason right_nritems = btrfs_header_nritems(right); 379044871b1bSChris Mason if (right_nritems == 0) 379144871b1bSChris Mason return 1; 379244871b1bSChris Mason 379344871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 379444871b1bSChris Mason 379544871b1bSChris Mason left = read_node_slot(root, path->nodes[1], slot - 1); 379691ca338dSTsutomu Itoh if (left == NULL) 379791ca338dSTsutomu Itoh return 1; 379891ca338dSTsutomu Itoh 379944871b1bSChris Mason btrfs_tree_lock(left); 380044871b1bSChris Mason btrfs_set_lock_blocking(left); 380144871b1bSChris Mason 380244871b1bSChris Mason free_space = btrfs_leaf_free_space(root, left); 380344871b1bSChris Mason if (free_space < data_size) { 380444871b1bSChris Mason ret = 1; 380544871b1bSChris Mason goto out; 380644871b1bSChris Mason } 380744871b1bSChris Mason 380844871b1bSChris Mason /* cow and double check */ 380944871b1bSChris Mason ret = btrfs_cow_block(trans, root, left, 381044871b1bSChris Mason path->nodes[1], slot - 1, &left); 381144871b1bSChris Mason if (ret) { 381244871b1bSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 381379787eaaSJeff Mahoney if (ret == -ENOSPC) 381444871b1bSChris Mason ret = 1; 381544871b1bSChris Mason goto out; 381644871b1bSChris Mason } 381744871b1bSChris Mason 381844871b1bSChris Mason free_space = btrfs_leaf_free_space(root, left); 381944871b1bSChris Mason if (free_space < data_size) { 382044871b1bSChris Mason ret = 1; 382144871b1bSChris Mason goto out; 382244871b1bSChris Mason } 382344871b1bSChris Mason 382499d8f83cSChris Mason return __push_leaf_left(trans, root, path, min_data_size, 382599d8f83cSChris Mason empty, left, free_space, right_nritems, 382699d8f83cSChris Mason max_slot); 382744871b1bSChris Mason out: 382844871b1bSChris Mason btrfs_tree_unlock(left); 382944871b1bSChris Mason free_extent_buffer(left); 383044871b1bSChris Mason return ret; 383144871b1bSChris Mason } 383244871b1bSChris Mason 383344871b1bSChris Mason /* 383474123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 383574123bd7SChris Mason * available for the resulting leaf level of the path. 383674123bd7SChris Mason */ 3837143bede5SJeff Mahoney static noinline void copy_for_split(struct btrfs_trans_handle *trans, 3838e02119d5SChris Mason struct btrfs_root *root, 383944871b1bSChris Mason struct btrfs_path *path, 384044871b1bSChris Mason struct extent_buffer *l, 384144871b1bSChris Mason struct extent_buffer *right, 384244871b1bSChris Mason int slot, int mid, int nritems) 3843be0e5c09SChris Mason { 3844be0e5c09SChris Mason int data_copy_size; 3845be0e5c09SChris Mason int rt_data_off; 3846be0e5c09SChris Mason int i; 3847d4dbff95SChris Mason struct btrfs_disk_key disk_key; 3848cfed81a0SChris Mason struct btrfs_map_token token; 3849cfed81a0SChris Mason 3850cfed81a0SChris Mason btrfs_init_map_token(&token); 3851be0e5c09SChris Mason 38525f39d397SChris Mason nritems = nritems - mid; 38535f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 38545f39d397SChris Mason data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 38555f39d397SChris Mason 38565f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 38575f39d397SChris Mason btrfs_item_nr_offset(mid), 38585f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 38595f39d397SChris Mason 38605f39d397SChris Mason copy_extent_buffer(right, l, 3861d6025579SChris Mason btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - 3862123abc88SChris Mason data_copy_size, btrfs_leaf_data(l) + 3863123abc88SChris Mason leaf_data_end(root, l), data_copy_size); 386474123bd7SChris Mason 38655f39d397SChris Mason rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - 38665f39d397SChris Mason btrfs_item_end_nr(l, mid); 38675f39d397SChris Mason 38685f39d397SChris Mason for (i = 0; i < nritems; i++) { 38695f39d397SChris Mason struct btrfs_item *item = btrfs_item_nr(right, i); 3870db94535dSChris Mason u32 ioff; 3871db94535dSChris Mason 3872cfed81a0SChris Mason ioff = btrfs_token_item_offset(right, item, &token); 3873cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, 3874cfed81a0SChris Mason ioff + rt_data_off, &token); 38750783fcfcSChris Mason } 387674123bd7SChris Mason 38775f39d397SChris Mason btrfs_set_header_nritems(l, mid); 38785f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 3879143bede5SJeff Mahoney insert_ptr(trans, root, path, &disk_key, right->start, 3880c3e06965SJan Schmidt path->slots[1] + 1, 1); 38815f39d397SChris Mason 38825f39d397SChris Mason btrfs_mark_buffer_dirty(right); 38835f39d397SChris Mason btrfs_mark_buffer_dirty(l); 3884eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 38855f39d397SChris Mason 3886be0e5c09SChris Mason if (mid <= slot) { 3887925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 38885f39d397SChris Mason free_extent_buffer(path->nodes[0]); 38895f39d397SChris Mason path->nodes[0] = right; 3890be0e5c09SChris Mason path->slots[0] -= mid; 3891be0e5c09SChris Mason path->slots[1] += 1; 3892925baeddSChris Mason } else { 3893925baeddSChris Mason btrfs_tree_unlock(right); 38945f39d397SChris Mason free_extent_buffer(right); 3895925baeddSChris Mason } 38965f39d397SChris Mason 3897eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 389844871b1bSChris Mason } 389944871b1bSChris Mason 390044871b1bSChris Mason /* 390199d8f83cSChris Mason * double splits happen when we need to insert a big item in the middle 390299d8f83cSChris Mason * of a leaf. A double split can leave us with 3 mostly empty leaves: 390399d8f83cSChris Mason * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] 390499d8f83cSChris Mason * A B C 390599d8f83cSChris Mason * 390699d8f83cSChris Mason * We avoid this by trying to push the items on either side of our target 390799d8f83cSChris Mason * into the adjacent leaves. If all goes well we can avoid the double split 390899d8f83cSChris Mason * completely. 390999d8f83cSChris Mason */ 391099d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans, 391199d8f83cSChris Mason struct btrfs_root *root, 391299d8f83cSChris Mason struct btrfs_path *path, 391399d8f83cSChris Mason int data_size) 391499d8f83cSChris Mason { 391599d8f83cSChris Mason int ret; 391699d8f83cSChris Mason int progress = 0; 391799d8f83cSChris Mason int slot; 391899d8f83cSChris Mason u32 nritems; 391999d8f83cSChris Mason 392099d8f83cSChris Mason slot = path->slots[0]; 392199d8f83cSChris Mason 392299d8f83cSChris Mason /* 392399d8f83cSChris Mason * try to push all the items after our slot into the 392499d8f83cSChris Mason * right leaf 392599d8f83cSChris Mason */ 392699d8f83cSChris Mason ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); 392799d8f83cSChris Mason if (ret < 0) 392899d8f83cSChris Mason return ret; 392999d8f83cSChris Mason 393099d8f83cSChris Mason if (ret == 0) 393199d8f83cSChris Mason progress++; 393299d8f83cSChris Mason 393399d8f83cSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 393499d8f83cSChris Mason /* 393599d8f83cSChris Mason * our goal is to get our slot at the start or end of a leaf. If 393699d8f83cSChris Mason * we've done so we're done 393799d8f83cSChris Mason */ 393899d8f83cSChris Mason if (path->slots[0] == 0 || path->slots[0] == nritems) 393999d8f83cSChris Mason return 0; 394099d8f83cSChris Mason 394199d8f83cSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 394299d8f83cSChris Mason return 0; 394399d8f83cSChris Mason 394499d8f83cSChris Mason /* try to push all the items before our slot into the next leaf */ 394599d8f83cSChris Mason slot = path->slots[0]; 394699d8f83cSChris Mason ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); 394799d8f83cSChris Mason if (ret < 0) 394899d8f83cSChris Mason return ret; 394999d8f83cSChris Mason 395099d8f83cSChris Mason if (ret == 0) 395199d8f83cSChris Mason progress++; 395299d8f83cSChris Mason 395399d8f83cSChris Mason if (progress) 395499d8f83cSChris Mason return 0; 395599d8f83cSChris Mason return 1; 395699d8f83cSChris Mason } 395799d8f83cSChris Mason 395899d8f83cSChris Mason /* 395944871b1bSChris Mason * split the path's leaf in two, making sure there is at least data_size 396044871b1bSChris Mason * available for the resulting leaf level of the path. 396144871b1bSChris Mason * 396244871b1bSChris Mason * returns 0 if all went well and < 0 on failure. 396344871b1bSChris Mason */ 396444871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 396544871b1bSChris Mason struct btrfs_root *root, 396644871b1bSChris Mason struct btrfs_key *ins_key, 396744871b1bSChris Mason struct btrfs_path *path, int data_size, 396844871b1bSChris Mason int extend) 396944871b1bSChris Mason { 39705d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 397144871b1bSChris Mason struct extent_buffer *l; 397244871b1bSChris Mason u32 nritems; 397344871b1bSChris Mason int mid; 397444871b1bSChris Mason int slot; 397544871b1bSChris Mason struct extent_buffer *right; 397644871b1bSChris Mason int ret = 0; 397744871b1bSChris Mason int wret; 39785d4f98a2SYan Zheng int split; 397944871b1bSChris Mason int num_doubles = 0; 398099d8f83cSChris Mason int tried_avoid_double = 0; 398144871b1bSChris Mason 3982a5719521SYan, Zheng l = path->nodes[0]; 3983a5719521SYan, Zheng slot = path->slots[0]; 3984a5719521SYan, Zheng if (extend && data_size + btrfs_item_size_nr(l, slot) + 3985a5719521SYan, Zheng sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) 3986a5719521SYan, Zheng return -EOVERFLOW; 3987a5719521SYan, Zheng 398844871b1bSChris Mason /* first try to make some room by pushing left and right */ 398999d8f83cSChris Mason if (data_size) { 399099d8f83cSChris Mason wret = push_leaf_right(trans, root, path, data_size, 399199d8f83cSChris Mason data_size, 0, 0); 399244871b1bSChris Mason if (wret < 0) 399344871b1bSChris Mason return wret; 399444871b1bSChris Mason if (wret) { 399599d8f83cSChris Mason wret = push_leaf_left(trans, root, path, data_size, 399699d8f83cSChris Mason data_size, 0, (u32)-1); 399744871b1bSChris Mason if (wret < 0) 399844871b1bSChris Mason return wret; 399944871b1bSChris Mason } 400044871b1bSChris Mason l = path->nodes[0]; 400144871b1bSChris Mason 400244871b1bSChris Mason /* did the pushes work? */ 400344871b1bSChris Mason if (btrfs_leaf_free_space(root, l) >= data_size) 400444871b1bSChris Mason return 0; 400544871b1bSChris Mason } 400644871b1bSChris Mason 400744871b1bSChris Mason if (!path->nodes[1]) { 400890f8d62eSJan Schmidt ret = insert_new_root(trans, root, path, 1, 1); 400944871b1bSChris Mason if (ret) 401044871b1bSChris Mason return ret; 401144871b1bSChris Mason } 401244871b1bSChris Mason again: 40135d4f98a2SYan Zheng split = 1; 401444871b1bSChris Mason l = path->nodes[0]; 401544871b1bSChris Mason slot = path->slots[0]; 401644871b1bSChris Mason nritems = btrfs_header_nritems(l); 401744871b1bSChris Mason mid = (nritems + 1) / 2; 401844871b1bSChris Mason 40195d4f98a2SYan Zheng if (mid <= slot) { 40205d4f98a2SYan Zheng if (nritems == 1 || 40215d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + data_size > 40225d4f98a2SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 40235d4f98a2SYan Zheng if (slot >= nritems) { 40245d4f98a2SYan Zheng split = 0; 40255d4f98a2SYan Zheng } else { 40265d4f98a2SYan Zheng mid = slot; 40275d4f98a2SYan Zheng if (mid != nritems && 40285d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 40295d4f98a2SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 403099d8f83cSChris Mason if (data_size && !tried_avoid_double) 403199d8f83cSChris Mason goto push_for_double; 40325d4f98a2SYan Zheng split = 2; 40335d4f98a2SYan Zheng } 40345d4f98a2SYan Zheng } 40355d4f98a2SYan Zheng } 40365d4f98a2SYan Zheng } else { 40375d4f98a2SYan Zheng if (leaf_space_used(l, 0, mid) + data_size > 40385d4f98a2SYan Zheng BTRFS_LEAF_DATA_SIZE(root)) { 40395d4f98a2SYan Zheng if (!extend && data_size && slot == 0) { 40405d4f98a2SYan Zheng split = 0; 40415d4f98a2SYan Zheng } else if ((extend || !data_size) && slot == 0) { 40425d4f98a2SYan Zheng mid = 1; 40435d4f98a2SYan Zheng } else { 40445d4f98a2SYan Zheng mid = slot; 40455d4f98a2SYan Zheng if (mid != nritems && 40465d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 40475d4f98a2SYan Zheng data_size > BTRFS_LEAF_DATA_SIZE(root)) { 404899d8f83cSChris Mason if (data_size && !tried_avoid_double) 404999d8f83cSChris Mason goto push_for_double; 40505d4f98a2SYan Zheng split = 2 ; 40515d4f98a2SYan Zheng } 40525d4f98a2SYan Zheng } 40535d4f98a2SYan Zheng } 40545d4f98a2SYan Zheng } 40555d4f98a2SYan Zheng 40565d4f98a2SYan Zheng if (split == 0) 40575d4f98a2SYan Zheng btrfs_cpu_key_to_disk(&disk_key, ins_key); 40585d4f98a2SYan Zheng else 40595d4f98a2SYan Zheng btrfs_item_key(l, &disk_key, mid); 40605d4f98a2SYan Zheng 40615d4f98a2SYan Zheng right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 406244871b1bSChris Mason root->root_key.objectid, 40635581a51aSJan Schmidt &disk_key, 0, l->start, 0); 4064f0486c68SYan, Zheng if (IS_ERR(right)) 406544871b1bSChris Mason return PTR_ERR(right); 4066f0486c68SYan, Zheng 4067f0486c68SYan, Zheng root_add_used(root, root->leafsize); 406844871b1bSChris Mason 406944871b1bSChris Mason memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); 407044871b1bSChris Mason btrfs_set_header_bytenr(right, right->start); 407144871b1bSChris Mason btrfs_set_header_generation(right, trans->transid); 40725d4f98a2SYan Zheng btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); 407344871b1bSChris Mason btrfs_set_header_owner(right, root->root_key.objectid); 407444871b1bSChris Mason btrfs_set_header_level(right, 0); 407544871b1bSChris Mason write_extent_buffer(right, root->fs_info->fsid, 407644871b1bSChris Mason (unsigned long)btrfs_header_fsid(right), 407744871b1bSChris Mason BTRFS_FSID_SIZE); 407844871b1bSChris Mason 407944871b1bSChris Mason write_extent_buffer(right, root->fs_info->chunk_tree_uuid, 408044871b1bSChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(right), 408144871b1bSChris Mason BTRFS_UUID_SIZE); 408244871b1bSChris Mason 40835d4f98a2SYan Zheng if (split == 0) { 408444871b1bSChris Mason if (mid <= slot) { 408544871b1bSChris Mason btrfs_set_header_nritems(right, 0); 4086143bede5SJeff Mahoney insert_ptr(trans, root, path, &disk_key, right->start, 4087c3e06965SJan Schmidt path->slots[1] + 1, 1); 408844871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 408944871b1bSChris Mason free_extent_buffer(path->nodes[0]); 409044871b1bSChris Mason path->nodes[0] = right; 409144871b1bSChris Mason path->slots[0] = 0; 409244871b1bSChris Mason path->slots[1] += 1; 409344871b1bSChris Mason } else { 409444871b1bSChris Mason btrfs_set_header_nritems(right, 0); 4095143bede5SJeff Mahoney insert_ptr(trans, root, path, &disk_key, right->start, 4096c3e06965SJan Schmidt path->slots[1], 1); 409744871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 409844871b1bSChris Mason free_extent_buffer(path->nodes[0]); 409944871b1bSChris Mason path->nodes[0] = right; 410044871b1bSChris Mason path->slots[0] = 0; 4101143bede5SJeff Mahoney if (path->slots[1] == 0) 4102d6a0a126STsutomu Itoh fixup_low_keys(root, path, &disk_key, 1); 41035d4f98a2SYan Zheng } 410444871b1bSChris Mason btrfs_mark_buffer_dirty(right); 410544871b1bSChris Mason return ret; 410644871b1bSChris Mason } 410744871b1bSChris Mason 4108143bede5SJeff Mahoney copy_for_split(trans, root, path, l, right, slot, mid, nritems); 410944871b1bSChris Mason 41105d4f98a2SYan Zheng if (split == 2) { 4111cc0c5538SChris Mason BUG_ON(num_doubles != 0); 4112cc0c5538SChris Mason num_doubles++; 4113cc0c5538SChris Mason goto again; 41143326d1b0SChris Mason } 411544871b1bSChris Mason 4116143bede5SJeff Mahoney return 0; 411799d8f83cSChris Mason 411899d8f83cSChris Mason push_for_double: 411999d8f83cSChris Mason push_for_double_split(trans, root, path, data_size); 412099d8f83cSChris Mason tried_avoid_double = 1; 412199d8f83cSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 412299d8f83cSChris Mason return 0; 412399d8f83cSChris Mason goto again; 4124be0e5c09SChris Mason } 4125be0e5c09SChris Mason 4126ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, 4127ad48fd75SYan, Zheng struct btrfs_root *root, 4128ad48fd75SYan, Zheng struct btrfs_path *path, int ins_len) 4129ad48fd75SYan, Zheng { 4130ad48fd75SYan, Zheng struct btrfs_key key; 4131ad48fd75SYan, Zheng struct extent_buffer *leaf; 4132ad48fd75SYan, Zheng struct btrfs_file_extent_item *fi; 4133ad48fd75SYan, Zheng u64 extent_len = 0; 4134ad48fd75SYan, Zheng u32 item_size; 4135ad48fd75SYan, Zheng int ret; 4136ad48fd75SYan, Zheng 4137ad48fd75SYan, Zheng leaf = path->nodes[0]; 4138ad48fd75SYan, Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 4139ad48fd75SYan, Zheng 4140ad48fd75SYan, Zheng BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 4141ad48fd75SYan, Zheng key.type != BTRFS_EXTENT_CSUM_KEY); 4142ad48fd75SYan, Zheng 4143ad48fd75SYan, Zheng if (btrfs_leaf_free_space(root, leaf) >= ins_len) 4144ad48fd75SYan, Zheng return 0; 4145ad48fd75SYan, Zheng 4146ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 4147ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 4148ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 4149ad48fd75SYan, Zheng struct btrfs_file_extent_item); 4150ad48fd75SYan, Zheng extent_len = btrfs_file_extent_num_bytes(leaf, fi); 4151ad48fd75SYan, Zheng } 4152b3b4aa74SDavid Sterba btrfs_release_path(path); 4153ad48fd75SYan, Zheng 4154ad48fd75SYan, Zheng path->keep_locks = 1; 4155ad48fd75SYan, Zheng path->search_for_split = 1; 4156ad48fd75SYan, Zheng ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 4157ad48fd75SYan, Zheng path->search_for_split = 0; 4158ad48fd75SYan, Zheng if (ret < 0) 4159ad48fd75SYan, Zheng goto err; 4160ad48fd75SYan, Zheng 4161ad48fd75SYan, Zheng ret = -EAGAIN; 4162ad48fd75SYan, Zheng leaf = path->nodes[0]; 4163ad48fd75SYan, Zheng /* if our item isn't there or got smaller, return now */ 4164ad48fd75SYan, Zheng if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) 4165ad48fd75SYan, Zheng goto err; 4166ad48fd75SYan, Zheng 4167109f6aefSChris Mason /* the leaf has changed, it now has room. return now */ 4168109f6aefSChris Mason if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) 4169109f6aefSChris Mason goto err; 4170109f6aefSChris Mason 4171ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 4172ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 4173ad48fd75SYan, Zheng struct btrfs_file_extent_item); 4174ad48fd75SYan, Zheng if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) 4175ad48fd75SYan, Zheng goto err; 4176ad48fd75SYan, Zheng } 4177ad48fd75SYan, Zheng 4178ad48fd75SYan, Zheng btrfs_set_path_blocking(path); 4179ad48fd75SYan, Zheng ret = split_leaf(trans, root, &key, path, ins_len, 1); 4180f0486c68SYan, Zheng if (ret) 4181f0486c68SYan, Zheng goto err; 4182ad48fd75SYan, Zheng 4183ad48fd75SYan, Zheng path->keep_locks = 0; 4184ad48fd75SYan, Zheng btrfs_unlock_up_safe(path, 1); 4185ad48fd75SYan, Zheng return 0; 4186ad48fd75SYan, Zheng err: 4187ad48fd75SYan, Zheng path->keep_locks = 0; 4188ad48fd75SYan, Zheng return ret; 4189ad48fd75SYan, Zheng } 4190ad48fd75SYan, Zheng 4191ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans, 4192459931ecSChris Mason struct btrfs_root *root, 4193459931ecSChris Mason struct btrfs_path *path, 4194459931ecSChris Mason struct btrfs_key *new_key, 4195459931ecSChris Mason unsigned long split_offset) 4196459931ecSChris Mason { 4197459931ecSChris Mason struct extent_buffer *leaf; 4198459931ecSChris Mason struct btrfs_item *item; 4199459931ecSChris Mason struct btrfs_item *new_item; 4200459931ecSChris Mason int slot; 4201ad48fd75SYan, Zheng char *buf; 4202459931ecSChris Mason u32 nritems; 4203ad48fd75SYan, Zheng u32 item_size; 4204459931ecSChris Mason u32 orig_offset; 4205459931ecSChris Mason struct btrfs_disk_key disk_key; 4206459931ecSChris Mason 4207459931ecSChris Mason leaf = path->nodes[0]; 4208b9473439SChris Mason BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); 4209b9473439SChris Mason 4210b4ce94deSChris Mason btrfs_set_path_blocking(path); 4211b4ce94deSChris Mason 4212459931ecSChris Mason item = btrfs_item_nr(leaf, path->slots[0]); 4213459931ecSChris Mason orig_offset = btrfs_item_offset(leaf, item); 4214459931ecSChris Mason item_size = btrfs_item_size(leaf, item); 4215459931ecSChris Mason 4216459931ecSChris Mason buf = kmalloc(item_size, GFP_NOFS); 4217ad48fd75SYan, Zheng if (!buf) 4218ad48fd75SYan, Zheng return -ENOMEM; 4219ad48fd75SYan, Zheng 4220459931ecSChris Mason read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, 4221459931ecSChris Mason path->slots[0]), item_size); 4222ad48fd75SYan, Zheng 4223459931ecSChris Mason slot = path->slots[0] + 1; 4224459931ecSChris Mason nritems = btrfs_header_nritems(leaf); 4225459931ecSChris Mason if (slot != nritems) { 4226459931ecSChris Mason /* shift the items */ 4227459931ecSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), 4228459931ecSChris Mason btrfs_item_nr_offset(slot), 4229459931ecSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 4230459931ecSChris Mason } 4231459931ecSChris Mason 4232459931ecSChris Mason btrfs_cpu_key_to_disk(&disk_key, new_key); 4233459931ecSChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4234459931ecSChris Mason 4235459931ecSChris Mason new_item = btrfs_item_nr(leaf, slot); 4236459931ecSChris Mason 4237459931ecSChris Mason btrfs_set_item_offset(leaf, new_item, orig_offset); 4238459931ecSChris Mason btrfs_set_item_size(leaf, new_item, item_size - split_offset); 4239459931ecSChris Mason 4240459931ecSChris Mason btrfs_set_item_offset(leaf, item, 4241459931ecSChris Mason orig_offset + item_size - split_offset); 4242459931ecSChris Mason btrfs_set_item_size(leaf, item, split_offset); 4243459931ecSChris Mason 4244459931ecSChris Mason btrfs_set_header_nritems(leaf, nritems + 1); 4245459931ecSChris Mason 4246459931ecSChris Mason /* write the data for the start of the original item */ 4247459931ecSChris Mason write_extent_buffer(leaf, buf, 4248459931ecSChris Mason btrfs_item_ptr_offset(leaf, path->slots[0]), 4249459931ecSChris Mason split_offset); 4250459931ecSChris Mason 4251459931ecSChris Mason /* write the data for the new item */ 4252459931ecSChris Mason write_extent_buffer(leaf, buf + split_offset, 4253459931ecSChris Mason btrfs_item_ptr_offset(leaf, slot), 4254459931ecSChris Mason item_size - split_offset); 4255459931ecSChris Mason btrfs_mark_buffer_dirty(leaf); 4256459931ecSChris Mason 4257ad48fd75SYan, Zheng BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); 4258459931ecSChris Mason kfree(buf); 4259ad48fd75SYan, Zheng return 0; 4260ad48fd75SYan, Zheng } 4261ad48fd75SYan, Zheng 4262ad48fd75SYan, Zheng /* 4263ad48fd75SYan, Zheng * This function splits a single item into two items, 4264ad48fd75SYan, Zheng * giving 'new_key' to the new item and splitting the 4265ad48fd75SYan, Zheng * old one at split_offset (from the start of the item). 4266ad48fd75SYan, Zheng * 4267ad48fd75SYan, Zheng * The path may be released by this operation. After 4268ad48fd75SYan, Zheng * the split, the path is pointing to the old item. The 4269ad48fd75SYan, Zheng * new item is going to be in the same node as the old one. 4270ad48fd75SYan, Zheng * 4271ad48fd75SYan, Zheng * Note, the item being split must be smaller enough to live alone on 4272ad48fd75SYan, Zheng * a tree block with room for one extra struct btrfs_item 4273ad48fd75SYan, Zheng * 4274ad48fd75SYan, Zheng * This allows us to split the item in place, keeping a lock on the 4275ad48fd75SYan, Zheng * leaf the entire time. 4276ad48fd75SYan, Zheng */ 4277ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans, 4278ad48fd75SYan, Zheng struct btrfs_root *root, 4279ad48fd75SYan, Zheng struct btrfs_path *path, 4280ad48fd75SYan, Zheng struct btrfs_key *new_key, 4281ad48fd75SYan, Zheng unsigned long split_offset) 4282ad48fd75SYan, Zheng { 4283ad48fd75SYan, Zheng int ret; 4284ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 4285ad48fd75SYan, Zheng sizeof(struct btrfs_item)); 4286ad48fd75SYan, Zheng if (ret) 4287459931ecSChris Mason return ret; 4288ad48fd75SYan, Zheng 4289ad48fd75SYan, Zheng ret = split_item(trans, root, path, new_key, split_offset); 4290ad48fd75SYan, Zheng return ret; 4291ad48fd75SYan, Zheng } 4292ad48fd75SYan, Zheng 4293ad48fd75SYan, Zheng /* 4294ad48fd75SYan, Zheng * This function duplicate a item, giving 'new_key' to the new item. 4295ad48fd75SYan, Zheng * It guarantees both items live in the same tree leaf and the new item 4296ad48fd75SYan, Zheng * is contiguous with the original item. 4297ad48fd75SYan, Zheng * 4298ad48fd75SYan, Zheng * This allows us to split file extent in place, keeping a lock on the 4299ad48fd75SYan, Zheng * leaf the entire time. 4300ad48fd75SYan, Zheng */ 4301ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans, 4302ad48fd75SYan, Zheng struct btrfs_root *root, 4303ad48fd75SYan, Zheng struct btrfs_path *path, 4304ad48fd75SYan, Zheng struct btrfs_key *new_key) 4305ad48fd75SYan, Zheng { 4306ad48fd75SYan, Zheng struct extent_buffer *leaf; 4307ad48fd75SYan, Zheng int ret; 4308ad48fd75SYan, Zheng u32 item_size; 4309ad48fd75SYan, Zheng 4310ad48fd75SYan, Zheng leaf = path->nodes[0]; 4311ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 4312ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 4313ad48fd75SYan, Zheng item_size + sizeof(struct btrfs_item)); 4314ad48fd75SYan, Zheng if (ret) 4315ad48fd75SYan, Zheng return ret; 4316ad48fd75SYan, Zheng 4317ad48fd75SYan, Zheng path->slots[0]++; 4318afe5fea7STsutomu Itoh setup_items_for_insert(root, path, new_key, &item_size, 4319ad48fd75SYan, Zheng item_size, item_size + 4320ad48fd75SYan, Zheng sizeof(struct btrfs_item), 1); 4321ad48fd75SYan, Zheng leaf = path->nodes[0]; 4322ad48fd75SYan, Zheng memcpy_extent_buffer(leaf, 4323ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0]), 4324ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0] - 1), 4325ad48fd75SYan, Zheng item_size); 4326ad48fd75SYan, Zheng return 0; 4327459931ecSChris Mason } 4328459931ecSChris Mason 4329459931ecSChris Mason /* 4330d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 4331d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 4332d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 4333d352ac68SChris Mason * the front. 4334d352ac68SChris Mason */ 4335afe5fea7STsutomu Itoh void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path, 4336179e29e4SChris Mason u32 new_size, int from_end) 4337b18c6685SChris Mason { 4338b18c6685SChris Mason int slot; 43395f39d397SChris Mason struct extent_buffer *leaf; 43405f39d397SChris Mason struct btrfs_item *item; 4341b18c6685SChris Mason u32 nritems; 4342b18c6685SChris Mason unsigned int data_end; 4343b18c6685SChris Mason unsigned int old_data_start; 4344b18c6685SChris Mason unsigned int old_size; 4345b18c6685SChris Mason unsigned int size_diff; 4346b18c6685SChris Mason int i; 4347cfed81a0SChris Mason struct btrfs_map_token token; 4348cfed81a0SChris Mason 4349cfed81a0SChris Mason btrfs_init_map_token(&token); 4350b18c6685SChris Mason 43515f39d397SChris Mason leaf = path->nodes[0]; 4352179e29e4SChris Mason slot = path->slots[0]; 4353179e29e4SChris Mason 4354179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 4355179e29e4SChris Mason if (old_size == new_size) 4356143bede5SJeff Mahoney return; 4357b18c6685SChris Mason 43585f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 4359b18c6685SChris Mason data_end = leaf_data_end(root, leaf); 4360b18c6685SChris Mason 43615f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 4362179e29e4SChris Mason 4363b18c6685SChris Mason size_diff = old_size - new_size; 4364b18c6685SChris Mason 4365b18c6685SChris Mason BUG_ON(slot < 0); 4366b18c6685SChris Mason BUG_ON(slot >= nritems); 4367b18c6685SChris Mason 4368b18c6685SChris Mason /* 4369b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4370b18c6685SChris Mason */ 4371b18c6685SChris Mason /* first correct the data pointers */ 4372b18c6685SChris Mason for (i = slot; i < nritems; i++) { 43735f39d397SChris Mason u32 ioff; 43745f39d397SChris Mason item = btrfs_item_nr(leaf, i); 4375db94535dSChris Mason 4376cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4377cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4378cfed81a0SChris Mason ioff + size_diff, &token); 4379b18c6685SChris Mason } 4380db94535dSChris Mason 4381b18c6685SChris Mason /* shift the data */ 4382179e29e4SChris Mason if (from_end) { 43835f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 4384b18c6685SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 4385b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 4386179e29e4SChris Mason } else { 4387179e29e4SChris Mason struct btrfs_disk_key disk_key; 4388179e29e4SChris Mason u64 offset; 4389179e29e4SChris Mason 4390179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 4391179e29e4SChris Mason 4392179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 4393179e29e4SChris Mason unsigned long ptr; 4394179e29e4SChris Mason struct btrfs_file_extent_item *fi; 4395179e29e4SChris Mason 4396179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 4397179e29e4SChris Mason struct btrfs_file_extent_item); 4398179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 4399179e29e4SChris Mason (unsigned long)fi - size_diff); 4400179e29e4SChris Mason 4401179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 4402179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 4403179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 4404179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 4405179e29e4SChris Mason (unsigned long)fi, 4406179e29e4SChris Mason offsetof(struct btrfs_file_extent_item, 4407179e29e4SChris Mason disk_bytenr)); 4408179e29e4SChris Mason } 4409179e29e4SChris Mason } 4410179e29e4SChris Mason 4411179e29e4SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 4412179e29e4SChris Mason data_end + size_diff, btrfs_leaf_data(leaf) + 4413179e29e4SChris Mason data_end, old_data_start - data_end); 4414179e29e4SChris Mason 4415179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 4416179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 4417179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4418179e29e4SChris Mason if (slot == 0) 4419d6a0a126STsutomu Itoh fixup_low_keys(root, path, &disk_key, 1); 4420179e29e4SChris Mason } 44215f39d397SChris Mason 44225f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 44235f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 44245f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 4425b18c6685SChris Mason 44265f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 44275f39d397SChris Mason btrfs_print_leaf(root, leaf); 4428b18c6685SChris Mason BUG(); 44295f39d397SChris Mason } 4430b18c6685SChris Mason } 4431b18c6685SChris Mason 4432d352ac68SChris Mason /* 4433d352ac68SChris Mason * make the item pointed to by the path bigger, data_size is the new size. 4434d352ac68SChris Mason */ 44354b90c680STsutomu Itoh void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path, 44365f39d397SChris Mason u32 data_size) 44376567e837SChris Mason { 44386567e837SChris Mason int slot; 44395f39d397SChris Mason struct extent_buffer *leaf; 44405f39d397SChris Mason struct btrfs_item *item; 44416567e837SChris Mason u32 nritems; 44426567e837SChris Mason unsigned int data_end; 44436567e837SChris Mason unsigned int old_data; 44446567e837SChris Mason unsigned int old_size; 44456567e837SChris Mason int i; 4446cfed81a0SChris Mason struct btrfs_map_token token; 4447cfed81a0SChris Mason 4448cfed81a0SChris Mason btrfs_init_map_token(&token); 44496567e837SChris Mason 44505f39d397SChris Mason leaf = path->nodes[0]; 44516567e837SChris Mason 44525f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 44536567e837SChris Mason data_end = leaf_data_end(root, leaf); 44546567e837SChris Mason 44555f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < data_size) { 44565f39d397SChris Mason btrfs_print_leaf(root, leaf); 44576567e837SChris Mason BUG(); 44585f39d397SChris Mason } 44596567e837SChris Mason slot = path->slots[0]; 44605f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 44616567e837SChris Mason 44626567e837SChris Mason BUG_ON(slot < 0); 44633326d1b0SChris Mason if (slot >= nritems) { 44643326d1b0SChris Mason btrfs_print_leaf(root, leaf); 4465d397712bSChris Mason printk(KERN_CRIT "slot %d too large, nritems %d\n", 4466d397712bSChris Mason slot, nritems); 44673326d1b0SChris Mason BUG_ON(1); 44683326d1b0SChris Mason } 44696567e837SChris Mason 44706567e837SChris Mason /* 44716567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 44726567e837SChris Mason */ 44736567e837SChris Mason /* first correct the data pointers */ 44746567e837SChris Mason for (i = slot; i < nritems; i++) { 44755f39d397SChris Mason u32 ioff; 44765f39d397SChris Mason item = btrfs_item_nr(leaf, i); 4477db94535dSChris Mason 4478cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4479cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4480cfed81a0SChris Mason ioff - data_size, &token); 44816567e837SChris Mason } 44825f39d397SChris Mason 44836567e837SChris Mason /* shift the data */ 44845f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 44856567e837SChris Mason data_end - data_size, btrfs_leaf_data(leaf) + 44866567e837SChris Mason data_end, old_data - data_end); 44875f39d397SChris Mason 44886567e837SChris Mason data_end = old_data; 44895f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 44905f39d397SChris Mason item = btrfs_item_nr(leaf, slot); 44915f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 44925f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 44936567e837SChris Mason 44945f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 44955f39d397SChris Mason btrfs_print_leaf(root, leaf); 44966567e837SChris Mason BUG(); 44975f39d397SChris Mason } 44986567e837SChris Mason } 44996567e837SChris Mason 450074123bd7SChris Mason /* 450144871b1bSChris Mason * this is a helper for btrfs_insert_empty_items, the main goal here is 450244871b1bSChris Mason * to save stack depth by doing the bulk of the work in a function 450344871b1bSChris Mason * that doesn't call btrfs_search_slot 450474123bd7SChris Mason */ 4505afe5fea7STsutomu Itoh void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, 45069c58309dSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 450744871b1bSChris Mason u32 total_data, u32 total_size, int nr) 4508be0e5c09SChris Mason { 45095f39d397SChris Mason struct btrfs_item *item; 45109c58309dSChris Mason int i; 45117518a238SChris Mason u32 nritems; 4512be0e5c09SChris Mason unsigned int data_end; 4513e2fa7227SChris Mason struct btrfs_disk_key disk_key; 451444871b1bSChris Mason struct extent_buffer *leaf; 451544871b1bSChris Mason int slot; 4516cfed81a0SChris Mason struct btrfs_map_token token; 4517cfed81a0SChris Mason 4518cfed81a0SChris Mason btrfs_init_map_token(&token); 4519e2fa7227SChris Mason 45205f39d397SChris Mason leaf = path->nodes[0]; 452144871b1bSChris Mason slot = path->slots[0]; 452274123bd7SChris Mason 45235f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 4524123abc88SChris Mason data_end = leaf_data_end(root, leaf); 4525eb60ceacSChris Mason 4526f25956ccSChris Mason if (btrfs_leaf_free_space(root, leaf) < total_size) { 45273326d1b0SChris Mason btrfs_print_leaf(root, leaf); 4528d397712bSChris Mason printk(KERN_CRIT "not enough freespace need %u have %d\n", 45299c58309dSChris Mason total_size, btrfs_leaf_free_space(root, leaf)); 4530be0e5c09SChris Mason BUG(); 4531d4dbff95SChris Mason } 45325f39d397SChris Mason 4533be0e5c09SChris Mason if (slot != nritems) { 45345f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 4535be0e5c09SChris Mason 45365f39d397SChris Mason if (old_data < data_end) { 45375f39d397SChris Mason btrfs_print_leaf(root, leaf); 4538d397712bSChris Mason printk(KERN_CRIT "slot %d old_data %d data_end %d\n", 45395f39d397SChris Mason slot, old_data, data_end); 45405f39d397SChris Mason BUG_ON(1); 45415f39d397SChris Mason } 4542be0e5c09SChris Mason /* 4543be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4544be0e5c09SChris Mason */ 4545be0e5c09SChris Mason /* first correct the data pointers */ 45460783fcfcSChris Mason for (i = slot; i < nritems; i++) { 45475f39d397SChris Mason u32 ioff; 4548db94535dSChris Mason 45495f39d397SChris Mason item = btrfs_item_nr(leaf, i); 4550cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4551cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4552cfed81a0SChris Mason ioff - total_data, &token); 45530783fcfcSChris Mason } 4554be0e5c09SChris Mason /* shift the items */ 45559c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 45565f39d397SChris Mason btrfs_item_nr_offset(slot), 45570783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 4558be0e5c09SChris Mason 4559be0e5c09SChris Mason /* shift the data */ 45605f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 45619c58309dSChris Mason data_end - total_data, btrfs_leaf_data(leaf) + 4562be0e5c09SChris Mason data_end, old_data - data_end); 4563be0e5c09SChris Mason data_end = old_data; 4564be0e5c09SChris Mason } 45655f39d397SChris Mason 456662e2749eSChris Mason /* setup the item for the new data */ 45679c58309dSChris Mason for (i = 0; i < nr; i++) { 45689c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 45699c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 45709c58309dSChris Mason item = btrfs_item_nr(leaf, slot + i); 4571cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4572cfed81a0SChris Mason data_end - data_size[i], &token); 45739c58309dSChris Mason data_end -= data_size[i]; 4574cfed81a0SChris Mason btrfs_set_token_item_size(leaf, item, data_size[i], &token); 45759c58309dSChris Mason } 457644871b1bSChris Mason 45779c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 4578aa5d6bedSChris Mason 45795a01a2e3SChris Mason if (slot == 0) { 45805a01a2e3SChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key); 4581d6a0a126STsutomu Itoh fixup_low_keys(root, path, &disk_key, 1); 45825a01a2e3SChris Mason } 4583b9473439SChris Mason btrfs_unlock_up_safe(path, 1); 4584b9473439SChris Mason btrfs_mark_buffer_dirty(leaf); 4585aa5d6bedSChris Mason 45865f39d397SChris Mason if (btrfs_leaf_free_space(root, leaf) < 0) { 45875f39d397SChris Mason btrfs_print_leaf(root, leaf); 4588be0e5c09SChris Mason BUG(); 45895f39d397SChris Mason } 459044871b1bSChris Mason } 459144871b1bSChris Mason 459244871b1bSChris Mason /* 459344871b1bSChris Mason * Given a key and some data, insert items into the tree. 459444871b1bSChris Mason * This does all the path init required, making room in the tree if needed. 459544871b1bSChris Mason */ 459644871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 459744871b1bSChris Mason struct btrfs_root *root, 459844871b1bSChris Mason struct btrfs_path *path, 459944871b1bSChris Mason struct btrfs_key *cpu_key, u32 *data_size, 460044871b1bSChris Mason int nr) 460144871b1bSChris Mason { 460244871b1bSChris Mason int ret = 0; 460344871b1bSChris Mason int slot; 460444871b1bSChris Mason int i; 460544871b1bSChris Mason u32 total_size = 0; 460644871b1bSChris Mason u32 total_data = 0; 460744871b1bSChris Mason 460844871b1bSChris Mason for (i = 0; i < nr; i++) 460944871b1bSChris Mason total_data += data_size[i]; 461044871b1bSChris Mason 461144871b1bSChris Mason total_size = total_data + (nr * sizeof(struct btrfs_item)); 461244871b1bSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 461344871b1bSChris Mason if (ret == 0) 461444871b1bSChris Mason return -EEXIST; 461544871b1bSChris Mason if (ret < 0) 4616143bede5SJeff Mahoney return ret; 461744871b1bSChris Mason 461844871b1bSChris Mason slot = path->slots[0]; 461944871b1bSChris Mason BUG_ON(slot < 0); 462044871b1bSChris Mason 4621afe5fea7STsutomu Itoh setup_items_for_insert(root, path, cpu_key, data_size, 462244871b1bSChris Mason total_data, total_size, nr); 4623143bede5SJeff Mahoney return 0; 462462e2749eSChris Mason } 462562e2749eSChris Mason 462662e2749eSChris Mason /* 462762e2749eSChris Mason * Given a key and some data, insert an item into the tree. 462862e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 462962e2749eSChris Mason */ 4630e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root 4631e089f05cSChris Mason *root, struct btrfs_key *cpu_key, void *data, u32 4632e089f05cSChris Mason data_size) 463362e2749eSChris Mason { 463462e2749eSChris Mason int ret = 0; 46352c90e5d6SChris Mason struct btrfs_path *path; 46365f39d397SChris Mason struct extent_buffer *leaf; 46375f39d397SChris Mason unsigned long ptr; 463862e2749eSChris Mason 46392c90e5d6SChris Mason path = btrfs_alloc_path(); 4640db5b493aSTsutomu Itoh if (!path) 4641db5b493aSTsutomu Itoh return -ENOMEM; 46422c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 464362e2749eSChris Mason if (!ret) { 46445f39d397SChris Mason leaf = path->nodes[0]; 46455f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 46465f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 46475f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 464862e2749eSChris Mason } 46492c90e5d6SChris Mason btrfs_free_path(path); 4650aa5d6bedSChris Mason return ret; 4651be0e5c09SChris Mason } 4652be0e5c09SChris Mason 465374123bd7SChris Mason /* 46545de08d7dSChris Mason * delete the pointer from a given node. 465574123bd7SChris Mason * 4656d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 4657d352ac68SChris Mason * empty a node. 465874123bd7SChris Mason */ 4659afe5fea7STsutomu Itoh static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 4660afe5fea7STsutomu Itoh int level, int slot) 4661be0e5c09SChris Mason { 46625f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 46637518a238SChris Mason u32 nritems; 4664f3ea38daSJan Schmidt int ret; 4665be0e5c09SChris Mason 46665f39d397SChris Mason nritems = btrfs_header_nritems(parent); 4667be0e5c09SChris Mason if (slot != nritems - 1) { 46680e411eceSLiu Bo if (level) 4669f3ea38daSJan Schmidt tree_mod_log_eb_move(root->fs_info, parent, slot, 4670f3ea38daSJan Schmidt slot + 1, nritems - slot - 1); 46715f39d397SChris Mason memmove_extent_buffer(parent, 46725f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 46735f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 4674d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 4675d6025579SChris Mason (nritems - slot - 1)); 467657ba86c0SChris Mason } else if (level) { 467757ba86c0SChris Mason ret = tree_mod_log_insert_key(root->fs_info, parent, slot, 467857ba86c0SChris Mason MOD_LOG_KEY_REMOVE); 467957ba86c0SChris Mason BUG_ON(ret < 0); 4680be0e5c09SChris Mason } 4681f3ea38daSJan Schmidt 46827518a238SChris Mason nritems--; 46835f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 46847518a238SChris Mason if (nritems == 0 && parent == root->node) { 46855f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 4686eb60ceacSChris Mason /* just turn the root into a leaf and break */ 46875f39d397SChris Mason btrfs_set_header_level(root->node, 0); 4688bb803951SChris Mason } else if (slot == 0) { 46895f39d397SChris Mason struct btrfs_disk_key disk_key; 46905f39d397SChris Mason 46915f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 4692d6a0a126STsutomu Itoh fixup_low_keys(root, path, &disk_key, level + 1); 4693be0e5c09SChris Mason } 4694d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 4695be0e5c09SChris Mason } 4696be0e5c09SChris Mason 469774123bd7SChris Mason /* 4698323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 46995d4f98a2SYan Zheng * path->nodes[1]. 4700323ac95bSChris Mason * 4701323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 4702323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 4703323ac95bSChris Mason * 4704323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 4705323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 4706323ac95bSChris Mason */ 4707143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, 4708323ac95bSChris Mason struct btrfs_root *root, 47095d4f98a2SYan Zheng struct btrfs_path *path, 47105d4f98a2SYan Zheng struct extent_buffer *leaf) 4711323ac95bSChris Mason { 47125d4f98a2SYan Zheng WARN_ON(btrfs_header_generation(leaf) != trans->transid); 4713afe5fea7STsutomu Itoh del_ptr(root, path, 1, path->slots[1]); 4714323ac95bSChris Mason 47154d081c41SChris Mason /* 47164d081c41SChris Mason * btrfs_free_extent is expensive, we want to make sure we 47174d081c41SChris Mason * aren't holding any locks when we call it 47184d081c41SChris Mason */ 47194d081c41SChris Mason btrfs_unlock_up_safe(path, 0); 47204d081c41SChris Mason 4721f0486c68SYan, Zheng root_sub_used(root, leaf->len); 4722f0486c68SYan, Zheng 47233083ee2eSJosef Bacik extent_buffer_get(leaf); 47245581a51aSJan Schmidt btrfs_free_tree_block(trans, root, leaf, 0, 1); 47253083ee2eSJosef Bacik free_extent_buffer_stale(leaf); 4726323ac95bSChris Mason } 4727323ac95bSChris Mason /* 472874123bd7SChris Mason * delete the item at the leaf level in path. If that empties 472974123bd7SChris Mason * the leaf, remove it from the tree 473074123bd7SChris Mason */ 473185e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 473285e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 4733be0e5c09SChris Mason { 47345f39d397SChris Mason struct extent_buffer *leaf; 47355f39d397SChris Mason struct btrfs_item *item; 473685e21bacSChris Mason int last_off; 473785e21bacSChris Mason int dsize = 0; 4738aa5d6bedSChris Mason int ret = 0; 4739aa5d6bedSChris Mason int wret; 474085e21bacSChris Mason int i; 47417518a238SChris Mason u32 nritems; 4742cfed81a0SChris Mason struct btrfs_map_token token; 4743cfed81a0SChris Mason 4744cfed81a0SChris Mason btrfs_init_map_token(&token); 4745be0e5c09SChris Mason 47465f39d397SChris Mason leaf = path->nodes[0]; 474785e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 474885e21bacSChris Mason 474985e21bacSChris Mason for (i = 0; i < nr; i++) 475085e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 475185e21bacSChris Mason 47525f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 4753be0e5c09SChris Mason 475485e21bacSChris Mason if (slot + nr != nritems) { 4755123abc88SChris Mason int data_end = leaf_data_end(root, leaf); 47565f39d397SChris Mason 47575f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 4758d6025579SChris Mason data_end + dsize, 4759123abc88SChris Mason btrfs_leaf_data(leaf) + data_end, 476085e21bacSChris Mason last_off - data_end); 47615f39d397SChris Mason 476285e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 47635f39d397SChris Mason u32 ioff; 4764db94535dSChris Mason 47655f39d397SChris Mason item = btrfs_item_nr(leaf, i); 4766cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4767cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4768cfed81a0SChris Mason ioff + dsize, &token); 47690783fcfcSChris Mason } 4770db94535dSChris Mason 47715f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 477285e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 47730783fcfcSChris Mason sizeof(struct btrfs_item) * 477485e21bacSChris Mason (nritems - slot - nr)); 4775be0e5c09SChris Mason } 477685e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 477785e21bacSChris Mason nritems -= nr; 47785f39d397SChris Mason 477974123bd7SChris Mason /* delete the leaf if we've emptied it */ 47807518a238SChris Mason if (nritems == 0) { 47815f39d397SChris Mason if (leaf == root->node) { 47825f39d397SChris Mason btrfs_set_header_level(leaf, 0); 47839a8dd150SChris Mason } else { 4784f0486c68SYan, Zheng btrfs_set_path_blocking(path); 4785f0486c68SYan, Zheng clean_tree_block(trans, root, leaf); 4786143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 47879a8dd150SChris Mason } 4788be0e5c09SChris Mason } else { 47897518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 4790aa5d6bedSChris Mason if (slot == 0) { 47915f39d397SChris Mason struct btrfs_disk_key disk_key; 47925f39d397SChris Mason 47935f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 4794d6a0a126STsutomu Itoh fixup_low_keys(root, path, &disk_key, 1); 4795aa5d6bedSChris Mason } 4796aa5d6bedSChris Mason 479774123bd7SChris Mason /* delete the leaf if it is mostly empty */ 4798d717aa1dSYan Zheng if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { 4799be0e5c09SChris Mason /* push_leaf_left fixes the path. 4800be0e5c09SChris Mason * make sure the path still points to our leaf 4801be0e5c09SChris Mason * for possible call to del_ptr below 4802be0e5c09SChris Mason */ 48034920c9acSChris Mason slot = path->slots[1]; 48045f39d397SChris Mason extent_buffer_get(leaf); 48055f39d397SChris Mason 4806b9473439SChris Mason btrfs_set_path_blocking(path); 480799d8f83cSChris Mason wret = push_leaf_left(trans, root, path, 1, 1, 480899d8f83cSChris Mason 1, (u32)-1); 480954aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 4810aa5d6bedSChris Mason ret = wret; 48115f39d397SChris Mason 48125f39d397SChris Mason if (path->nodes[0] == leaf && 48135f39d397SChris Mason btrfs_header_nritems(leaf)) { 481499d8f83cSChris Mason wret = push_leaf_right(trans, root, path, 1, 481599d8f83cSChris Mason 1, 1, 0); 481654aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 4817aa5d6bedSChris Mason ret = wret; 4818aa5d6bedSChris Mason } 48195f39d397SChris Mason 48205f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 4821323ac95bSChris Mason path->slots[1] = slot; 4822143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 48235f39d397SChris Mason free_extent_buffer(leaf); 4824143bede5SJeff Mahoney ret = 0; 48255de08d7dSChris Mason } else { 4826925baeddSChris Mason /* if we're still in the path, make sure 4827925baeddSChris Mason * we're dirty. Otherwise, one of the 4828925baeddSChris Mason * push_leaf functions must have already 4829925baeddSChris Mason * dirtied this buffer 4830925baeddSChris Mason */ 4831925baeddSChris Mason if (path->nodes[0] == leaf) 48325f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 48335f39d397SChris Mason free_extent_buffer(leaf); 4834be0e5c09SChris Mason } 4835d5719762SChris Mason } else { 48365f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 4837be0e5c09SChris Mason } 48389a8dd150SChris Mason } 4839aa5d6bedSChris Mason return ret; 48409a8dd150SChris Mason } 48419a8dd150SChris Mason 484297571fd0SChris Mason /* 4843925baeddSChris Mason * search the tree again to find a leaf with lesser keys 48447bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 48457bb86316SChris Mason * returns < 0 on io errors. 4846d352ac68SChris Mason * 4847d352ac68SChris Mason * This may release the path, and so you may lose any locks held at the 4848d352ac68SChris Mason * time you call it. 48497bb86316SChris Mason */ 48507bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 48517bb86316SChris Mason { 4852925baeddSChris Mason struct btrfs_key key; 4853925baeddSChris Mason struct btrfs_disk_key found_key; 4854925baeddSChris Mason int ret; 48557bb86316SChris Mason 4856925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 4857925baeddSChris Mason 4858925baeddSChris Mason if (key.offset > 0) 4859925baeddSChris Mason key.offset--; 4860925baeddSChris Mason else if (key.type > 0) 4861925baeddSChris Mason key.type--; 4862925baeddSChris Mason else if (key.objectid > 0) 4863925baeddSChris Mason key.objectid--; 4864925baeddSChris Mason else 48657bb86316SChris Mason return 1; 48667bb86316SChris Mason 4867b3b4aa74SDavid Sterba btrfs_release_path(path); 4868925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4869925baeddSChris Mason if (ret < 0) 4870925baeddSChris Mason return ret; 4871925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 4872925baeddSChris Mason ret = comp_keys(&found_key, &key); 4873925baeddSChris Mason if (ret < 0) 48747bb86316SChris Mason return 0; 4875925baeddSChris Mason return 1; 48767bb86316SChris Mason } 48777bb86316SChris Mason 48783f157a2fSChris Mason /* 48793f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 4880de78b51aSEric Sandeen * for nodes or leaves that are have a minimum transaction id. 4881de78b51aSEric Sandeen * This is used by the btree defrag code, and tree logging 48823f157a2fSChris Mason * 48833f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 48843f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 48853f157a2fSChris Mason * key and get a writable path. 48863f157a2fSChris Mason * 48873f157a2fSChris Mason * This does lock as it descends, and path->keep_locks should be set 48883f157a2fSChris Mason * to 1 by the caller. 48893f157a2fSChris Mason * 48903f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 48913f157a2fSChris Mason * of the tree. 48923f157a2fSChris Mason * 4893d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 4894d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 4895d352ac68SChris Mason * skipped over (without reading them). 4896d352ac68SChris Mason * 48973f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 48983f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 48993f157a2fSChris Mason */ 49003f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 4901e02119d5SChris Mason struct btrfs_key *max_key, 4902de78b51aSEric Sandeen struct btrfs_path *path, 49033f157a2fSChris Mason u64 min_trans) 49043f157a2fSChris Mason { 49053f157a2fSChris Mason struct extent_buffer *cur; 49063f157a2fSChris Mason struct btrfs_key found_key; 49073f157a2fSChris Mason int slot; 49089652480bSYan int sret; 49093f157a2fSChris Mason u32 nritems; 49103f157a2fSChris Mason int level; 49113f157a2fSChris Mason int ret = 1; 49123f157a2fSChris Mason 4913934d375bSChris Mason WARN_ON(!path->keep_locks); 49143f157a2fSChris Mason again: 4915bd681513SChris Mason cur = btrfs_read_lock_root_node(root); 49163f157a2fSChris Mason level = btrfs_header_level(cur); 4917e02119d5SChris Mason WARN_ON(path->nodes[level]); 49183f157a2fSChris Mason path->nodes[level] = cur; 4919bd681513SChris Mason path->locks[level] = BTRFS_READ_LOCK; 49203f157a2fSChris Mason 49213f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 49223f157a2fSChris Mason ret = 1; 49233f157a2fSChris Mason goto out; 49243f157a2fSChris Mason } 49253f157a2fSChris Mason while (1) { 49263f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 49273f157a2fSChris Mason level = btrfs_header_level(cur); 49289652480bSYan sret = bin_search(cur, min_key, level, &slot); 49293f157a2fSChris Mason 4930323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 4931323ac95bSChris Mason if (level == path->lowest_level) { 4932e02119d5SChris Mason if (slot >= nritems) 4933e02119d5SChris Mason goto find_next_key; 49343f157a2fSChris Mason ret = 0; 49353f157a2fSChris Mason path->slots[level] = slot; 49363f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 49373f157a2fSChris Mason goto out; 49383f157a2fSChris Mason } 49399652480bSYan if (sret && slot > 0) 49409652480bSYan slot--; 49413f157a2fSChris Mason /* 4942de78b51aSEric Sandeen * check this node pointer against the min_trans parameters. 4943de78b51aSEric Sandeen * If it is too old, old, skip to the next one. 49443f157a2fSChris Mason */ 49453f157a2fSChris Mason while (slot < nritems) { 49463f157a2fSChris Mason u64 blockptr; 49473f157a2fSChris Mason u64 gen; 4948e02119d5SChris Mason 49493f157a2fSChris Mason blockptr = btrfs_node_blockptr(cur, slot); 49503f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 49513f157a2fSChris Mason if (gen < min_trans) { 49523f157a2fSChris Mason slot++; 49533f157a2fSChris Mason continue; 49543f157a2fSChris Mason } 49553f157a2fSChris Mason break; 49563f157a2fSChris Mason } 4957e02119d5SChris Mason find_next_key: 49583f157a2fSChris Mason /* 49593f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 49603f157a2fSChris Mason * and find another one 49613f157a2fSChris Mason */ 49623f157a2fSChris Mason if (slot >= nritems) { 4963e02119d5SChris Mason path->slots[level] = slot; 4964b4ce94deSChris Mason btrfs_set_path_blocking(path); 4965e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 4966de78b51aSEric Sandeen min_trans); 4967e02119d5SChris Mason if (sret == 0) { 4968b3b4aa74SDavid Sterba btrfs_release_path(path); 49693f157a2fSChris Mason goto again; 49703f157a2fSChris Mason } else { 49713f157a2fSChris Mason goto out; 49723f157a2fSChris Mason } 49733f157a2fSChris Mason } 49743f157a2fSChris Mason /* save our key for returning back */ 49753f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 49763f157a2fSChris Mason path->slots[level] = slot; 49773f157a2fSChris Mason if (level == path->lowest_level) { 49783f157a2fSChris Mason ret = 0; 4979f7c79f30SChris Mason unlock_up(path, level, 1, 0, NULL); 49803f157a2fSChris Mason goto out; 49813f157a2fSChris Mason } 4982b4ce94deSChris Mason btrfs_set_path_blocking(path); 49833f157a2fSChris Mason cur = read_node_slot(root, cur, slot); 498479787eaaSJeff Mahoney BUG_ON(!cur); /* -ENOMEM */ 49853f157a2fSChris Mason 4986bd681513SChris Mason btrfs_tree_read_lock(cur); 4987b4ce94deSChris Mason 4988bd681513SChris Mason path->locks[level - 1] = BTRFS_READ_LOCK; 49893f157a2fSChris Mason path->nodes[level - 1] = cur; 4990f7c79f30SChris Mason unlock_up(path, level, 1, 0, NULL); 4991bd681513SChris Mason btrfs_clear_path_blocking(path, NULL, 0); 49923f157a2fSChris Mason } 49933f157a2fSChris Mason out: 49943f157a2fSChris Mason if (ret == 0) 49953f157a2fSChris Mason memcpy(min_key, &found_key, sizeof(found_key)); 4996b4ce94deSChris Mason btrfs_set_path_blocking(path); 49973f157a2fSChris Mason return ret; 49983f157a2fSChris Mason } 49993f157a2fSChris Mason 50007069830aSAlexander Block static void tree_move_down(struct btrfs_root *root, 50017069830aSAlexander Block struct btrfs_path *path, 50027069830aSAlexander Block int *level, int root_level) 50037069830aSAlexander Block { 500474dd17fbSChris Mason BUG_ON(*level == 0); 50057069830aSAlexander Block path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level], 50067069830aSAlexander Block path->slots[*level]); 50077069830aSAlexander Block path->slots[*level - 1] = 0; 50087069830aSAlexander Block (*level)--; 50097069830aSAlexander Block } 50107069830aSAlexander Block 50117069830aSAlexander Block static int tree_move_next_or_upnext(struct btrfs_root *root, 50127069830aSAlexander Block struct btrfs_path *path, 50137069830aSAlexander Block int *level, int root_level) 50147069830aSAlexander Block { 50157069830aSAlexander Block int ret = 0; 50167069830aSAlexander Block int nritems; 50177069830aSAlexander Block nritems = btrfs_header_nritems(path->nodes[*level]); 50187069830aSAlexander Block 50197069830aSAlexander Block path->slots[*level]++; 50207069830aSAlexander Block 502174dd17fbSChris Mason while (path->slots[*level] >= nritems) { 50227069830aSAlexander Block if (*level == root_level) 50237069830aSAlexander Block return -1; 50247069830aSAlexander Block 50257069830aSAlexander Block /* move upnext */ 50267069830aSAlexander Block path->slots[*level] = 0; 50277069830aSAlexander Block free_extent_buffer(path->nodes[*level]); 50287069830aSAlexander Block path->nodes[*level] = NULL; 50297069830aSAlexander Block (*level)++; 50307069830aSAlexander Block path->slots[*level]++; 50317069830aSAlexander Block 50327069830aSAlexander Block nritems = btrfs_header_nritems(path->nodes[*level]); 50337069830aSAlexander Block ret = 1; 50347069830aSAlexander Block } 50357069830aSAlexander Block return ret; 50367069830aSAlexander Block } 50377069830aSAlexander Block 50387069830aSAlexander Block /* 50397069830aSAlexander Block * Returns 1 if it had to move up and next. 0 is returned if it moved only next 50407069830aSAlexander Block * or down. 50417069830aSAlexander Block */ 50427069830aSAlexander Block static int tree_advance(struct btrfs_root *root, 50437069830aSAlexander Block struct btrfs_path *path, 50447069830aSAlexander Block int *level, int root_level, 50457069830aSAlexander Block int allow_down, 50467069830aSAlexander Block struct btrfs_key *key) 50477069830aSAlexander Block { 50487069830aSAlexander Block int ret; 50497069830aSAlexander Block 50507069830aSAlexander Block if (*level == 0 || !allow_down) { 50517069830aSAlexander Block ret = tree_move_next_or_upnext(root, path, level, root_level); 50527069830aSAlexander Block } else { 50537069830aSAlexander Block tree_move_down(root, path, level, root_level); 50547069830aSAlexander Block ret = 0; 50557069830aSAlexander Block } 50567069830aSAlexander Block if (ret >= 0) { 50577069830aSAlexander Block if (*level == 0) 50587069830aSAlexander Block btrfs_item_key_to_cpu(path->nodes[*level], key, 50597069830aSAlexander Block path->slots[*level]); 50607069830aSAlexander Block else 50617069830aSAlexander Block btrfs_node_key_to_cpu(path->nodes[*level], key, 50627069830aSAlexander Block path->slots[*level]); 50637069830aSAlexander Block } 50647069830aSAlexander Block return ret; 50657069830aSAlexander Block } 50667069830aSAlexander Block 50677069830aSAlexander Block static int tree_compare_item(struct btrfs_root *left_root, 50687069830aSAlexander Block struct btrfs_path *left_path, 50697069830aSAlexander Block struct btrfs_path *right_path, 50707069830aSAlexander Block char *tmp_buf) 50717069830aSAlexander Block { 50727069830aSAlexander Block int cmp; 50737069830aSAlexander Block int len1, len2; 50747069830aSAlexander Block unsigned long off1, off2; 50757069830aSAlexander Block 50767069830aSAlexander Block len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); 50777069830aSAlexander Block len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); 50787069830aSAlexander Block if (len1 != len2) 50797069830aSAlexander Block return 1; 50807069830aSAlexander Block 50817069830aSAlexander Block off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); 50827069830aSAlexander Block off2 = btrfs_item_ptr_offset(right_path->nodes[0], 50837069830aSAlexander Block right_path->slots[0]); 50847069830aSAlexander Block 50857069830aSAlexander Block read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); 50867069830aSAlexander Block 50877069830aSAlexander Block cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); 50887069830aSAlexander Block if (cmp) 50897069830aSAlexander Block return 1; 50907069830aSAlexander Block return 0; 50917069830aSAlexander Block } 50927069830aSAlexander Block 50937069830aSAlexander Block #define ADVANCE 1 50947069830aSAlexander Block #define ADVANCE_ONLY_NEXT -1 50957069830aSAlexander Block 50967069830aSAlexander Block /* 50977069830aSAlexander Block * This function compares two trees and calls the provided callback for 50987069830aSAlexander Block * every changed/new/deleted item it finds. 50997069830aSAlexander Block * If shared tree blocks are encountered, whole subtrees are skipped, making 51007069830aSAlexander Block * the compare pretty fast on snapshotted subvolumes. 51017069830aSAlexander Block * 51027069830aSAlexander Block * This currently works on commit roots only. As commit roots are read only, 51037069830aSAlexander Block * we don't do any locking. The commit roots are protected with transactions. 51047069830aSAlexander Block * Transactions are ended and rejoined when a commit is tried in between. 51057069830aSAlexander Block * 51067069830aSAlexander Block * This function checks for modifications done to the trees while comparing. 51077069830aSAlexander Block * If it detects a change, it aborts immediately. 51087069830aSAlexander Block */ 51097069830aSAlexander Block int btrfs_compare_trees(struct btrfs_root *left_root, 51107069830aSAlexander Block struct btrfs_root *right_root, 51117069830aSAlexander Block btrfs_changed_cb_t changed_cb, void *ctx) 51127069830aSAlexander Block { 51137069830aSAlexander Block int ret; 51147069830aSAlexander Block int cmp; 51157069830aSAlexander Block struct btrfs_trans_handle *trans = NULL; 51167069830aSAlexander Block struct btrfs_path *left_path = NULL; 51177069830aSAlexander Block struct btrfs_path *right_path = NULL; 51187069830aSAlexander Block struct btrfs_key left_key; 51197069830aSAlexander Block struct btrfs_key right_key; 51207069830aSAlexander Block char *tmp_buf = NULL; 51217069830aSAlexander Block int left_root_level; 51227069830aSAlexander Block int right_root_level; 51237069830aSAlexander Block int left_level; 51247069830aSAlexander Block int right_level; 51257069830aSAlexander Block int left_end_reached; 51267069830aSAlexander Block int right_end_reached; 51277069830aSAlexander Block int advance_left; 51287069830aSAlexander Block int advance_right; 51297069830aSAlexander Block u64 left_blockptr; 51307069830aSAlexander Block u64 right_blockptr; 51317069830aSAlexander Block u64 left_start_ctransid; 51327069830aSAlexander Block u64 right_start_ctransid; 51337069830aSAlexander Block u64 ctransid; 51347069830aSAlexander Block 51357069830aSAlexander Block left_path = btrfs_alloc_path(); 51367069830aSAlexander Block if (!left_path) { 51377069830aSAlexander Block ret = -ENOMEM; 51387069830aSAlexander Block goto out; 51397069830aSAlexander Block } 51407069830aSAlexander Block right_path = btrfs_alloc_path(); 51417069830aSAlexander Block if (!right_path) { 51427069830aSAlexander Block ret = -ENOMEM; 51437069830aSAlexander Block goto out; 51447069830aSAlexander Block } 51457069830aSAlexander Block 51467069830aSAlexander Block tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS); 51477069830aSAlexander Block if (!tmp_buf) { 51487069830aSAlexander Block ret = -ENOMEM; 51497069830aSAlexander Block goto out; 51507069830aSAlexander Block } 51517069830aSAlexander Block 51527069830aSAlexander Block left_path->search_commit_root = 1; 51537069830aSAlexander Block left_path->skip_locking = 1; 51547069830aSAlexander Block right_path->search_commit_root = 1; 51557069830aSAlexander Block right_path->skip_locking = 1; 51567069830aSAlexander Block 51575f3ab90aSAnand Jain spin_lock(&left_root->root_item_lock); 51587069830aSAlexander Block left_start_ctransid = btrfs_root_ctransid(&left_root->root_item); 51595f3ab90aSAnand Jain spin_unlock(&left_root->root_item_lock); 51607069830aSAlexander Block 51615f3ab90aSAnand Jain spin_lock(&right_root->root_item_lock); 51627069830aSAlexander Block right_start_ctransid = btrfs_root_ctransid(&right_root->root_item); 51635f3ab90aSAnand Jain spin_unlock(&right_root->root_item_lock); 51647069830aSAlexander Block 51657069830aSAlexander Block trans = btrfs_join_transaction(left_root); 51667069830aSAlexander Block if (IS_ERR(trans)) { 51677069830aSAlexander Block ret = PTR_ERR(trans); 51687069830aSAlexander Block trans = NULL; 51697069830aSAlexander Block goto out; 51707069830aSAlexander Block } 51717069830aSAlexander Block 51727069830aSAlexander Block /* 51737069830aSAlexander Block * Strategy: Go to the first items of both trees. Then do 51747069830aSAlexander Block * 51757069830aSAlexander Block * If both trees are at level 0 51767069830aSAlexander Block * Compare keys of current items 51777069830aSAlexander Block * If left < right treat left item as new, advance left tree 51787069830aSAlexander Block * and repeat 51797069830aSAlexander Block * If left > right treat right item as deleted, advance right tree 51807069830aSAlexander Block * and repeat 51817069830aSAlexander Block * If left == right do deep compare of items, treat as changed if 51827069830aSAlexander Block * needed, advance both trees and repeat 51837069830aSAlexander Block * If both trees are at the same level but not at level 0 51847069830aSAlexander Block * Compare keys of current nodes/leafs 51857069830aSAlexander Block * If left < right advance left tree and repeat 51867069830aSAlexander Block * If left > right advance right tree and repeat 51877069830aSAlexander Block * If left == right compare blockptrs of the next nodes/leafs 51887069830aSAlexander Block * If they match advance both trees but stay at the same level 51897069830aSAlexander Block * and repeat 51907069830aSAlexander Block * If they don't match advance both trees while allowing to go 51917069830aSAlexander Block * deeper and repeat 51927069830aSAlexander Block * If tree levels are different 51937069830aSAlexander Block * Advance the tree that needs it and repeat 51947069830aSAlexander Block * 51957069830aSAlexander Block * Advancing a tree means: 51967069830aSAlexander Block * If we are at level 0, try to go to the next slot. If that's not 51977069830aSAlexander Block * possible, go one level up and repeat. Stop when we found a level 51987069830aSAlexander Block * where we could go to the next slot. We may at this point be on a 51997069830aSAlexander Block * node or a leaf. 52007069830aSAlexander Block * 52017069830aSAlexander Block * If we are not at level 0 and not on shared tree blocks, go one 52027069830aSAlexander Block * level deeper. 52037069830aSAlexander Block * 52047069830aSAlexander Block * If we are not at level 0 and on shared tree blocks, go one slot to 52057069830aSAlexander Block * the right if possible or go up and right. 52067069830aSAlexander Block */ 52077069830aSAlexander Block 52087069830aSAlexander Block left_level = btrfs_header_level(left_root->commit_root); 52097069830aSAlexander Block left_root_level = left_level; 52107069830aSAlexander Block left_path->nodes[left_level] = left_root->commit_root; 52117069830aSAlexander Block extent_buffer_get(left_path->nodes[left_level]); 52127069830aSAlexander Block 52137069830aSAlexander Block right_level = btrfs_header_level(right_root->commit_root); 52147069830aSAlexander Block right_root_level = right_level; 52157069830aSAlexander Block right_path->nodes[right_level] = right_root->commit_root; 52167069830aSAlexander Block extent_buffer_get(right_path->nodes[right_level]); 52177069830aSAlexander Block 52187069830aSAlexander Block if (left_level == 0) 52197069830aSAlexander Block btrfs_item_key_to_cpu(left_path->nodes[left_level], 52207069830aSAlexander Block &left_key, left_path->slots[left_level]); 52217069830aSAlexander Block else 52227069830aSAlexander Block btrfs_node_key_to_cpu(left_path->nodes[left_level], 52237069830aSAlexander Block &left_key, left_path->slots[left_level]); 52247069830aSAlexander Block if (right_level == 0) 52257069830aSAlexander Block btrfs_item_key_to_cpu(right_path->nodes[right_level], 52267069830aSAlexander Block &right_key, right_path->slots[right_level]); 52277069830aSAlexander Block else 52287069830aSAlexander Block btrfs_node_key_to_cpu(right_path->nodes[right_level], 52297069830aSAlexander Block &right_key, right_path->slots[right_level]); 52307069830aSAlexander Block 52317069830aSAlexander Block left_end_reached = right_end_reached = 0; 52327069830aSAlexander Block advance_left = advance_right = 0; 52337069830aSAlexander Block 52347069830aSAlexander Block while (1) { 52357069830aSAlexander Block /* 52367069830aSAlexander Block * We need to make sure the transaction does not get committed 52377069830aSAlexander Block * while we do anything on commit roots. This means, we need to 52387069830aSAlexander Block * join and leave transactions for every item that we process. 52397069830aSAlexander Block */ 52407069830aSAlexander Block if (trans && btrfs_should_end_transaction(trans, left_root)) { 52417069830aSAlexander Block btrfs_release_path(left_path); 52427069830aSAlexander Block btrfs_release_path(right_path); 52437069830aSAlexander Block 52447069830aSAlexander Block ret = btrfs_end_transaction(trans, left_root); 52457069830aSAlexander Block trans = NULL; 52467069830aSAlexander Block if (ret < 0) 52477069830aSAlexander Block goto out; 52487069830aSAlexander Block } 52497069830aSAlexander Block /* now rejoin the transaction */ 52507069830aSAlexander Block if (!trans) { 52517069830aSAlexander Block trans = btrfs_join_transaction(left_root); 52527069830aSAlexander Block if (IS_ERR(trans)) { 52537069830aSAlexander Block ret = PTR_ERR(trans); 52547069830aSAlexander Block trans = NULL; 52557069830aSAlexander Block goto out; 52567069830aSAlexander Block } 52577069830aSAlexander Block 52585f3ab90aSAnand Jain spin_lock(&left_root->root_item_lock); 52597069830aSAlexander Block ctransid = btrfs_root_ctransid(&left_root->root_item); 52605f3ab90aSAnand Jain spin_unlock(&left_root->root_item_lock); 52617069830aSAlexander Block if (ctransid != left_start_ctransid) 52627069830aSAlexander Block left_start_ctransid = 0; 52637069830aSAlexander Block 52645f3ab90aSAnand Jain spin_lock(&right_root->root_item_lock); 52657069830aSAlexander Block ctransid = btrfs_root_ctransid(&right_root->root_item); 52665f3ab90aSAnand Jain spin_unlock(&right_root->root_item_lock); 52677069830aSAlexander Block if (ctransid != right_start_ctransid) 52687069830aSAlexander Block right_start_ctransid = 0; 52697069830aSAlexander Block 52707069830aSAlexander Block if (!left_start_ctransid || !right_start_ctransid) { 52717069830aSAlexander Block WARN(1, KERN_WARNING 52727069830aSAlexander Block "btrfs: btrfs_compare_tree detected " 52737069830aSAlexander Block "a change in one of the trees while " 52747069830aSAlexander Block "iterating. This is probably a " 52757069830aSAlexander Block "bug.\n"); 52767069830aSAlexander Block ret = -EIO; 52777069830aSAlexander Block goto out; 52787069830aSAlexander Block } 52797069830aSAlexander Block 52807069830aSAlexander Block /* 52817069830aSAlexander Block * the commit root may have changed, so start again 52827069830aSAlexander Block * where we stopped 52837069830aSAlexander Block */ 52847069830aSAlexander Block left_path->lowest_level = left_level; 52857069830aSAlexander Block right_path->lowest_level = right_level; 52867069830aSAlexander Block ret = btrfs_search_slot(NULL, left_root, 52877069830aSAlexander Block &left_key, left_path, 0, 0); 52887069830aSAlexander Block if (ret < 0) 52897069830aSAlexander Block goto out; 52907069830aSAlexander Block ret = btrfs_search_slot(NULL, right_root, 52917069830aSAlexander Block &right_key, right_path, 0, 0); 52927069830aSAlexander Block if (ret < 0) 52937069830aSAlexander Block goto out; 52947069830aSAlexander Block } 52957069830aSAlexander Block 52967069830aSAlexander Block if (advance_left && !left_end_reached) { 52977069830aSAlexander Block ret = tree_advance(left_root, left_path, &left_level, 52987069830aSAlexander Block left_root_level, 52997069830aSAlexander Block advance_left != ADVANCE_ONLY_NEXT, 53007069830aSAlexander Block &left_key); 53017069830aSAlexander Block if (ret < 0) 53027069830aSAlexander Block left_end_reached = ADVANCE; 53037069830aSAlexander Block advance_left = 0; 53047069830aSAlexander Block } 53057069830aSAlexander Block if (advance_right && !right_end_reached) { 53067069830aSAlexander Block ret = tree_advance(right_root, right_path, &right_level, 53077069830aSAlexander Block right_root_level, 53087069830aSAlexander Block advance_right != ADVANCE_ONLY_NEXT, 53097069830aSAlexander Block &right_key); 53107069830aSAlexander Block if (ret < 0) 53117069830aSAlexander Block right_end_reached = ADVANCE; 53127069830aSAlexander Block advance_right = 0; 53137069830aSAlexander Block } 53147069830aSAlexander Block 53157069830aSAlexander Block if (left_end_reached && right_end_reached) { 53167069830aSAlexander Block ret = 0; 53177069830aSAlexander Block goto out; 53187069830aSAlexander Block } else if (left_end_reached) { 53197069830aSAlexander Block if (right_level == 0) { 53207069830aSAlexander Block ret = changed_cb(left_root, right_root, 53217069830aSAlexander Block left_path, right_path, 53227069830aSAlexander Block &right_key, 53237069830aSAlexander Block BTRFS_COMPARE_TREE_DELETED, 53247069830aSAlexander Block ctx); 53257069830aSAlexander Block if (ret < 0) 53267069830aSAlexander Block goto out; 53277069830aSAlexander Block } 53287069830aSAlexander Block advance_right = ADVANCE; 53297069830aSAlexander Block continue; 53307069830aSAlexander Block } else if (right_end_reached) { 53317069830aSAlexander Block if (left_level == 0) { 53327069830aSAlexander Block ret = changed_cb(left_root, right_root, 53337069830aSAlexander Block left_path, right_path, 53347069830aSAlexander Block &left_key, 53357069830aSAlexander Block BTRFS_COMPARE_TREE_NEW, 53367069830aSAlexander Block ctx); 53377069830aSAlexander Block if (ret < 0) 53387069830aSAlexander Block goto out; 53397069830aSAlexander Block } 53407069830aSAlexander Block advance_left = ADVANCE; 53417069830aSAlexander Block continue; 53427069830aSAlexander Block } 53437069830aSAlexander Block 53447069830aSAlexander Block if (left_level == 0 && right_level == 0) { 53457069830aSAlexander Block cmp = btrfs_comp_cpu_keys(&left_key, &right_key); 53467069830aSAlexander Block if (cmp < 0) { 53477069830aSAlexander Block ret = changed_cb(left_root, right_root, 53487069830aSAlexander Block left_path, right_path, 53497069830aSAlexander Block &left_key, 53507069830aSAlexander Block BTRFS_COMPARE_TREE_NEW, 53517069830aSAlexander Block ctx); 53527069830aSAlexander Block if (ret < 0) 53537069830aSAlexander Block goto out; 53547069830aSAlexander Block advance_left = ADVANCE; 53557069830aSAlexander Block } else if (cmp > 0) { 53567069830aSAlexander Block ret = changed_cb(left_root, right_root, 53577069830aSAlexander Block left_path, right_path, 53587069830aSAlexander Block &right_key, 53597069830aSAlexander Block BTRFS_COMPARE_TREE_DELETED, 53607069830aSAlexander Block ctx); 53617069830aSAlexander Block if (ret < 0) 53627069830aSAlexander Block goto out; 53637069830aSAlexander Block advance_right = ADVANCE; 53647069830aSAlexander Block } else { 536574dd17fbSChris Mason WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); 53667069830aSAlexander Block ret = tree_compare_item(left_root, left_path, 53677069830aSAlexander Block right_path, tmp_buf); 53687069830aSAlexander Block if (ret) { 536974dd17fbSChris Mason WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); 53707069830aSAlexander Block ret = changed_cb(left_root, right_root, 53717069830aSAlexander Block left_path, right_path, 53727069830aSAlexander Block &left_key, 53737069830aSAlexander Block BTRFS_COMPARE_TREE_CHANGED, 53747069830aSAlexander Block ctx); 53757069830aSAlexander Block if (ret < 0) 53767069830aSAlexander Block goto out; 53777069830aSAlexander Block } 53787069830aSAlexander Block advance_left = ADVANCE; 53797069830aSAlexander Block advance_right = ADVANCE; 53807069830aSAlexander Block } 53817069830aSAlexander Block } else if (left_level == right_level) { 53827069830aSAlexander Block cmp = btrfs_comp_cpu_keys(&left_key, &right_key); 53837069830aSAlexander Block if (cmp < 0) { 53847069830aSAlexander Block advance_left = ADVANCE; 53857069830aSAlexander Block } else if (cmp > 0) { 53867069830aSAlexander Block advance_right = ADVANCE; 53877069830aSAlexander Block } else { 53887069830aSAlexander Block left_blockptr = btrfs_node_blockptr( 53897069830aSAlexander Block left_path->nodes[left_level], 53907069830aSAlexander Block left_path->slots[left_level]); 53917069830aSAlexander Block right_blockptr = btrfs_node_blockptr( 53927069830aSAlexander Block right_path->nodes[right_level], 53937069830aSAlexander Block right_path->slots[right_level]); 53947069830aSAlexander Block if (left_blockptr == right_blockptr) { 53957069830aSAlexander Block /* 53967069830aSAlexander Block * As we're on a shared block, don't 53977069830aSAlexander Block * allow to go deeper. 53987069830aSAlexander Block */ 53997069830aSAlexander Block advance_left = ADVANCE_ONLY_NEXT; 54007069830aSAlexander Block advance_right = ADVANCE_ONLY_NEXT; 54017069830aSAlexander Block } else { 54027069830aSAlexander Block advance_left = ADVANCE; 54037069830aSAlexander Block advance_right = ADVANCE; 54047069830aSAlexander Block } 54057069830aSAlexander Block } 54067069830aSAlexander Block } else if (left_level < right_level) { 54077069830aSAlexander Block advance_right = ADVANCE; 54087069830aSAlexander Block } else { 54097069830aSAlexander Block advance_left = ADVANCE; 54107069830aSAlexander Block } 54117069830aSAlexander Block } 54127069830aSAlexander Block 54137069830aSAlexander Block out: 54147069830aSAlexander Block btrfs_free_path(left_path); 54157069830aSAlexander Block btrfs_free_path(right_path); 54167069830aSAlexander Block kfree(tmp_buf); 54177069830aSAlexander Block 54187069830aSAlexander Block if (trans) { 54197069830aSAlexander Block if (!ret) 54207069830aSAlexander Block ret = btrfs_end_transaction(trans, left_root); 54217069830aSAlexander Block else 54227069830aSAlexander Block btrfs_end_transaction(trans, left_root); 54237069830aSAlexander Block } 54247069830aSAlexander Block 54257069830aSAlexander Block return ret; 54267069830aSAlexander Block } 54277069830aSAlexander Block 54283f157a2fSChris Mason /* 54293f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 54303f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 5431de78b51aSEric Sandeen * tree based on the current path and the min_trans parameters. 54323f157a2fSChris Mason * 54333f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 54343f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 54353f157a2fSChris Mason * 54363f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 54373f157a2fSChris Mason * calling this function. 54383f157a2fSChris Mason */ 5439e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 5440de78b51aSEric Sandeen struct btrfs_key *key, int level, u64 min_trans) 5441e7a84565SChris Mason { 5442e7a84565SChris Mason int slot; 5443e7a84565SChris Mason struct extent_buffer *c; 5444e7a84565SChris Mason 5445934d375bSChris Mason WARN_ON(!path->keep_locks); 5446e7a84565SChris Mason while (level < BTRFS_MAX_LEVEL) { 5447e7a84565SChris Mason if (!path->nodes[level]) 5448e7a84565SChris Mason return 1; 5449e7a84565SChris Mason 5450e7a84565SChris Mason slot = path->slots[level] + 1; 5451e7a84565SChris Mason c = path->nodes[level]; 54523f157a2fSChris Mason next: 5453e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 545433c66f43SYan Zheng int ret; 545533c66f43SYan Zheng int orig_lowest; 545633c66f43SYan Zheng struct btrfs_key cur_key; 545733c66f43SYan Zheng if (level + 1 >= BTRFS_MAX_LEVEL || 545833c66f43SYan Zheng !path->nodes[level + 1]) 5459e7a84565SChris Mason return 1; 546033c66f43SYan Zheng 546133c66f43SYan Zheng if (path->locks[level + 1]) { 546233c66f43SYan Zheng level++; 5463e7a84565SChris Mason continue; 5464e7a84565SChris Mason } 546533c66f43SYan Zheng 546633c66f43SYan Zheng slot = btrfs_header_nritems(c) - 1; 546733c66f43SYan Zheng if (level == 0) 546833c66f43SYan Zheng btrfs_item_key_to_cpu(c, &cur_key, slot); 546933c66f43SYan Zheng else 547033c66f43SYan Zheng btrfs_node_key_to_cpu(c, &cur_key, slot); 547133c66f43SYan Zheng 547233c66f43SYan Zheng orig_lowest = path->lowest_level; 5473b3b4aa74SDavid Sterba btrfs_release_path(path); 547433c66f43SYan Zheng path->lowest_level = level; 547533c66f43SYan Zheng ret = btrfs_search_slot(NULL, root, &cur_key, path, 547633c66f43SYan Zheng 0, 0); 547733c66f43SYan Zheng path->lowest_level = orig_lowest; 547833c66f43SYan Zheng if (ret < 0) 547933c66f43SYan Zheng return ret; 548033c66f43SYan Zheng 548133c66f43SYan Zheng c = path->nodes[level]; 548233c66f43SYan Zheng slot = path->slots[level]; 548333c66f43SYan Zheng if (ret == 0) 548433c66f43SYan Zheng slot++; 548533c66f43SYan Zheng goto next; 548633c66f43SYan Zheng } 548733c66f43SYan Zheng 5488e7a84565SChris Mason if (level == 0) 5489e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 54903f157a2fSChris Mason else { 54913f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 54923f157a2fSChris Mason 54933f157a2fSChris Mason if (gen < min_trans) { 54943f157a2fSChris Mason slot++; 54953f157a2fSChris Mason goto next; 54963f157a2fSChris Mason } 5497e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 54983f157a2fSChris Mason } 5499e7a84565SChris Mason return 0; 5500e7a84565SChris Mason } 5501e7a84565SChris Mason return 1; 5502e7a84565SChris Mason } 5503e7a84565SChris Mason 55047bb86316SChris Mason /* 5505925baeddSChris Mason * search the tree again to find a leaf with greater keys 55060f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 55070f70abe2SChris Mason * returns < 0 on io errors. 550897571fd0SChris Mason */ 5509234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 5510d97e63b6SChris Mason { 55113d7806ecSJan Schmidt return btrfs_next_old_leaf(root, path, 0); 55123d7806ecSJan Schmidt } 55133d7806ecSJan Schmidt 55143d7806ecSJan Schmidt int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, 55153d7806ecSJan Schmidt u64 time_seq) 55163d7806ecSJan Schmidt { 5517d97e63b6SChris Mason int slot; 55188e73f275SChris Mason int level; 55195f39d397SChris Mason struct extent_buffer *c; 55208e73f275SChris Mason struct extent_buffer *next; 5521925baeddSChris Mason struct btrfs_key key; 5522925baeddSChris Mason u32 nritems; 5523925baeddSChris Mason int ret; 55248e73f275SChris Mason int old_spinning = path->leave_spinning; 5525bd681513SChris Mason int next_rw_lock = 0; 5526925baeddSChris Mason 5527925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 5528d397712bSChris Mason if (nritems == 0) 5529925baeddSChris Mason return 1; 5530925baeddSChris Mason 55318e73f275SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 55328e73f275SChris Mason again: 55338e73f275SChris Mason level = 1; 55348e73f275SChris Mason next = NULL; 5535bd681513SChris Mason next_rw_lock = 0; 5536b3b4aa74SDavid Sterba btrfs_release_path(path); 55378e73f275SChris Mason 5538a2135011SChris Mason path->keep_locks = 1; 55398e73f275SChris Mason path->leave_spinning = 1; 55408e73f275SChris Mason 55413d7806ecSJan Schmidt if (time_seq) 55423d7806ecSJan Schmidt ret = btrfs_search_old_slot(root, &key, path, time_seq); 55433d7806ecSJan Schmidt else 5544925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5545925baeddSChris Mason path->keep_locks = 0; 5546925baeddSChris Mason 5547925baeddSChris Mason if (ret < 0) 5548925baeddSChris Mason return ret; 5549925baeddSChris Mason 5550a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 5551168fd7d2SChris Mason /* 5552168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 5553168fd7d2SChris Mason * could have added more items next to the key that used to be 5554168fd7d2SChris Mason * at the very end of the block. So, check again here and 5555168fd7d2SChris Mason * advance the path if there are now more items available. 5556168fd7d2SChris Mason */ 5557a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 5558e457afecSYan Zheng if (ret == 0) 5559168fd7d2SChris Mason path->slots[0]++; 55608e73f275SChris Mason ret = 0; 5561925baeddSChris Mason goto done; 5562925baeddSChris Mason } 5563d97e63b6SChris Mason 5564234b63a0SChris Mason while (level < BTRFS_MAX_LEVEL) { 55658e73f275SChris Mason if (!path->nodes[level]) { 55668e73f275SChris Mason ret = 1; 55678e73f275SChris Mason goto done; 55688e73f275SChris Mason } 55695f39d397SChris Mason 5570d97e63b6SChris Mason slot = path->slots[level] + 1; 5571d97e63b6SChris Mason c = path->nodes[level]; 55725f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 5573d97e63b6SChris Mason level++; 55748e73f275SChris Mason if (level == BTRFS_MAX_LEVEL) { 55758e73f275SChris Mason ret = 1; 55768e73f275SChris Mason goto done; 55778e73f275SChris Mason } 5578d97e63b6SChris Mason continue; 5579d97e63b6SChris Mason } 55805f39d397SChris Mason 5581925baeddSChris Mason if (next) { 5582bd681513SChris Mason btrfs_tree_unlock_rw(next, next_rw_lock); 55835f39d397SChris Mason free_extent_buffer(next); 5584925baeddSChris Mason } 55855f39d397SChris Mason 55868e73f275SChris Mason next = c; 5587bd681513SChris Mason next_rw_lock = path->locks[level]; 55888e73f275SChris Mason ret = read_block_for_search(NULL, root, path, &next, level, 55895d9e75c4SJan Schmidt slot, &key, 0); 55908e73f275SChris Mason if (ret == -EAGAIN) 55918e73f275SChris Mason goto again; 55925f39d397SChris Mason 559376a05b35SChris Mason if (ret < 0) { 5594b3b4aa74SDavid Sterba btrfs_release_path(path); 559576a05b35SChris Mason goto done; 559676a05b35SChris Mason } 559776a05b35SChris Mason 55985cd57b2cSChris Mason if (!path->skip_locking) { 5599bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 5600d42244a0SJan Schmidt if (!ret && time_seq) { 5601d42244a0SJan Schmidt /* 5602d42244a0SJan Schmidt * If we don't get the lock, we may be racing 5603d42244a0SJan Schmidt * with push_leaf_left, holding that lock while 5604d42244a0SJan Schmidt * itself waiting for the leaf we've currently 5605d42244a0SJan Schmidt * locked. To solve this situation, we give up 5606d42244a0SJan Schmidt * on our lock and cycle. 5607d42244a0SJan Schmidt */ 5608cf538830SJan Schmidt free_extent_buffer(next); 5609d42244a0SJan Schmidt btrfs_release_path(path); 5610d42244a0SJan Schmidt cond_resched(); 5611d42244a0SJan Schmidt goto again; 5612d42244a0SJan Schmidt } 56138e73f275SChris Mason if (!ret) { 56148e73f275SChris Mason btrfs_set_path_blocking(path); 5615bd681513SChris Mason btrfs_tree_read_lock(next); 5616bd681513SChris Mason btrfs_clear_path_blocking(path, next, 5617bd681513SChris Mason BTRFS_READ_LOCK); 56188e73f275SChris Mason } 5619bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 5620bd681513SChris Mason } 5621d97e63b6SChris Mason break; 5622d97e63b6SChris Mason } 5623d97e63b6SChris Mason path->slots[level] = slot; 5624d97e63b6SChris Mason while (1) { 5625d97e63b6SChris Mason level--; 5626d97e63b6SChris Mason c = path->nodes[level]; 5627925baeddSChris Mason if (path->locks[level]) 5628bd681513SChris Mason btrfs_tree_unlock_rw(c, path->locks[level]); 56298e73f275SChris Mason 56305f39d397SChris Mason free_extent_buffer(c); 5631d97e63b6SChris Mason path->nodes[level] = next; 5632d97e63b6SChris Mason path->slots[level] = 0; 5633a74a4b97SChris Mason if (!path->skip_locking) 5634bd681513SChris Mason path->locks[level] = next_rw_lock; 5635d97e63b6SChris Mason if (!level) 5636d97e63b6SChris Mason break; 5637b4ce94deSChris Mason 56388e73f275SChris Mason ret = read_block_for_search(NULL, root, path, &next, level, 56395d9e75c4SJan Schmidt 0, &key, 0); 56408e73f275SChris Mason if (ret == -EAGAIN) 56418e73f275SChris Mason goto again; 56428e73f275SChris Mason 564376a05b35SChris Mason if (ret < 0) { 5644b3b4aa74SDavid Sterba btrfs_release_path(path); 564576a05b35SChris Mason goto done; 564676a05b35SChris Mason } 564776a05b35SChris Mason 56485cd57b2cSChris Mason if (!path->skip_locking) { 5649bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 56508e73f275SChris Mason if (!ret) { 56518e73f275SChris Mason btrfs_set_path_blocking(path); 5652bd681513SChris Mason btrfs_tree_read_lock(next); 5653bd681513SChris Mason btrfs_clear_path_blocking(path, next, 5654bd681513SChris Mason BTRFS_READ_LOCK); 56558e73f275SChris Mason } 5656bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 5657bd681513SChris Mason } 5658d97e63b6SChris Mason } 56598e73f275SChris Mason ret = 0; 5660925baeddSChris Mason done: 5661f7c79f30SChris Mason unlock_up(path, 0, 1, 0, NULL); 56628e73f275SChris Mason path->leave_spinning = old_spinning; 56638e73f275SChris Mason if (!old_spinning) 56648e73f275SChris Mason btrfs_set_path_blocking(path); 56658e73f275SChris Mason 56668e73f275SChris Mason return ret; 5667d97e63b6SChris Mason } 56680b86a832SChris Mason 56693f157a2fSChris Mason /* 56703f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 56713f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 56723f157a2fSChris Mason * 56733f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 56743f157a2fSChris Mason */ 56750b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 56760b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 56770b86a832SChris Mason int type) 56780b86a832SChris Mason { 56790b86a832SChris Mason struct btrfs_key found_key; 56800b86a832SChris Mason struct extent_buffer *leaf; 5681e02119d5SChris Mason u32 nritems; 56820b86a832SChris Mason int ret; 56830b86a832SChris Mason 56840b86a832SChris Mason while (1) { 56850b86a832SChris Mason if (path->slots[0] == 0) { 5686b4ce94deSChris Mason btrfs_set_path_blocking(path); 56870b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 56880b86a832SChris Mason if (ret != 0) 56890b86a832SChris Mason return ret; 56900b86a832SChris Mason } else { 56910b86a832SChris Mason path->slots[0]--; 56920b86a832SChris Mason } 56930b86a832SChris Mason leaf = path->nodes[0]; 5694e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 5695e02119d5SChris Mason if (nritems == 0) 5696e02119d5SChris Mason return 1; 5697e02119d5SChris Mason if (path->slots[0] == nritems) 5698e02119d5SChris Mason path->slots[0]--; 5699e02119d5SChris Mason 57000b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 5701e02119d5SChris Mason if (found_key.objectid < min_objectid) 5702e02119d5SChris Mason break; 57030a4eefbbSYan Zheng if (found_key.type == type) 57040a4eefbbSYan Zheng return 0; 5705e02119d5SChris Mason if (found_key.objectid == min_objectid && 5706e02119d5SChris Mason found_key.type < type) 5707e02119d5SChris Mason break; 57080b86a832SChris Mason } 57090b86a832SChris Mason return 1; 57100b86a832SChris Mason } 5711