1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 3d352ac68SChris Mason * Copyright (C) 2007,2008 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6a6b6e75eSChris Mason #include <linux/sched.h> 75a0e3ad6STejun Heo #include <linux/slab.h> 8bd989ba3SJan Schmidt #include <linux/rbtree.h> 9adf02123SDavid Sterba #include <linux/mm.h> 10eb60ceacSChris Mason #include "ctree.h" 11eb60ceacSChris Mason #include "disk-io.h" 127f5c1516SChris Mason #include "transaction.h" 135f39d397SChris Mason #include "print-tree.h" 14925baeddSChris Mason #include "locking.h" 15de37aa51SNikolay Borisov #include "volumes.h" 16f616f5cdSQu Wenruo #include "qgroup.h" 179a8dd150SChris Mason 18e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 19e089f05cSChris Mason *root, struct btrfs_path *path, int level); 20310712b2SOmar Sandoval static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root, 21310712b2SOmar Sandoval const struct btrfs_key *ins_key, struct btrfs_path *path, 22310712b2SOmar Sandoval int data_size, int extend); 235f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 242ff7e61eSJeff Mahoney struct extent_buffer *dst, 25971a1f66SChris Mason struct extent_buffer *src, int empty); 265f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 275f39d397SChris Mason struct extent_buffer *dst_buf, 285f39d397SChris Mason struct extent_buffer *src_buf); 29afe5fea7STsutomu Itoh static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 30afe5fea7STsutomu Itoh int level, int slot); 31d97e63b6SChris Mason 322c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 332c90e5d6SChris Mason { 34e2c89907SMasahiro Yamada return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 352c90e5d6SChris Mason } 362c90e5d6SChris Mason 37b4ce94deSChris Mason /* 38b4ce94deSChris Mason * set all locked nodes in the path to blocking locks. This should 39b4ce94deSChris Mason * be done before scheduling 40b4ce94deSChris Mason */ 41b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p) 42b4ce94deSChris Mason { 43b4ce94deSChris Mason int i; 44b4ce94deSChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 45bd681513SChris Mason if (!p->nodes[i] || !p->locks[i]) 46bd681513SChris Mason continue; 47766ece54SDavid Sterba /* 48766ece54SDavid Sterba * If we currently have a spinning reader or writer lock this 49766ece54SDavid Sterba * will bump the count of blocking holders and drop the 50766ece54SDavid Sterba * spinlock. 51766ece54SDavid Sterba */ 52766ece54SDavid Sterba if (p->locks[i] == BTRFS_READ_LOCK) { 53766ece54SDavid Sterba btrfs_set_lock_blocking_read(p->nodes[i]); 54bd681513SChris Mason p->locks[i] = BTRFS_READ_LOCK_BLOCKING; 55766ece54SDavid Sterba } else if (p->locks[i] == BTRFS_WRITE_LOCK) { 56766ece54SDavid Sterba btrfs_set_lock_blocking_write(p->nodes[i]); 57bd681513SChris Mason p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; 58b4ce94deSChris Mason } 59b4ce94deSChris Mason } 60766ece54SDavid Sterba } 61b4ce94deSChris Mason 62d352ac68SChris Mason /* this also releases the path */ 632c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 642c90e5d6SChris Mason { 65ff175d57SJesper Juhl if (!p) 66ff175d57SJesper Juhl return; 67b3b4aa74SDavid Sterba btrfs_release_path(p); 682c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 692c90e5d6SChris Mason } 702c90e5d6SChris Mason 71d352ac68SChris Mason /* 72d352ac68SChris Mason * path release drops references on the extent buffers in the path 73d352ac68SChris Mason * and it drops any locks held by this path 74d352ac68SChris Mason * 75d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 76d352ac68SChris Mason */ 77b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p) 78eb60ceacSChris Mason { 79eb60ceacSChris Mason int i; 80a2135011SChris Mason 81234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 823f157a2fSChris Mason p->slots[i] = 0; 83eb60ceacSChris Mason if (!p->nodes[i]) 84925baeddSChris Mason continue; 85925baeddSChris Mason if (p->locks[i]) { 86bd681513SChris Mason btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); 87925baeddSChris Mason p->locks[i] = 0; 88925baeddSChris Mason } 895f39d397SChris Mason free_extent_buffer(p->nodes[i]); 903f157a2fSChris Mason p->nodes[i] = NULL; 91eb60ceacSChris Mason } 92eb60ceacSChris Mason } 93eb60ceacSChris Mason 94d352ac68SChris Mason /* 95d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 96d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 97d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 98d352ac68SChris Mason * looping required. 99d352ac68SChris Mason * 100d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 101d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 102d352ac68SChris Mason * at any time because there are no locks held. 103d352ac68SChris Mason */ 104925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 105925baeddSChris Mason { 106925baeddSChris Mason struct extent_buffer *eb; 107240f62c8SChris Mason 1083083ee2eSJosef Bacik while (1) { 109240f62c8SChris Mason rcu_read_lock(); 110240f62c8SChris Mason eb = rcu_dereference(root->node); 1113083ee2eSJosef Bacik 1123083ee2eSJosef Bacik /* 1133083ee2eSJosef Bacik * RCU really hurts here, we could free up the root node because 11401327610SNicholas D Steeves * it was COWed but we may not get the new root node yet so do 1153083ee2eSJosef Bacik * the inc_not_zero dance and if it doesn't work then 1163083ee2eSJosef Bacik * synchronize_rcu and try again. 1173083ee2eSJosef Bacik */ 1183083ee2eSJosef Bacik if (atomic_inc_not_zero(&eb->refs)) { 119240f62c8SChris Mason rcu_read_unlock(); 1203083ee2eSJosef Bacik break; 1213083ee2eSJosef Bacik } 1223083ee2eSJosef Bacik rcu_read_unlock(); 1233083ee2eSJosef Bacik synchronize_rcu(); 1243083ee2eSJosef Bacik } 125925baeddSChris Mason return eb; 126925baeddSChris Mason } 127925baeddSChris Mason 128d352ac68SChris Mason /* loop around taking references on and locking the root node of the 129d352ac68SChris Mason * tree until you end up with a lock on the root. A locked buffer 130d352ac68SChris Mason * is returned, with a reference held. 131d352ac68SChris Mason */ 132925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 133925baeddSChris Mason { 134925baeddSChris Mason struct extent_buffer *eb; 135925baeddSChris Mason 136925baeddSChris Mason while (1) { 137925baeddSChris Mason eb = btrfs_root_node(root); 138925baeddSChris Mason btrfs_tree_lock(eb); 139240f62c8SChris Mason if (eb == root->node) 140925baeddSChris Mason break; 141925baeddSChris Mason btrfs_tree_unlock(eb); 142925baeddSChris Mason free_extent_buffer(eb); 143925baeddSChris Mason } 144925baeddSChris Mason return eb; 145925baeddSChris Mason } 146925baeddSChris Mason 147bd681513SChris Mason /* loop around taking references on and locking the root node of the 148bd681513SChris Mason * tree until you end up with a lock on the root. A locked buffer 149bd681513SChris Mason * is returned, with a reference held. 150bd681513SChris Mason */ 15184f7d8e6SJosef Bacik struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root) 152bd681513SChris Mason { 153bd681513SChris Mason struct extent_buffer *eb; 154bd681513SChris Mason 155bd681513SChris Mason while (1) { 156bd681513SChris Mason eb = btrfs_root_node(root); 157bd681513SChris Mason btrfs_tree_read_lock(eb); 158bd681513SChris Mason if (eb == root->node) 159bd681513SChris Mason break; 160bd681513SChris Mason btrfs_tree_read_unlock(eb); 161bd681513SChris Mason free_extent_buffer(eb); 162bd681513SChris Mason } 163bd681513SChris Mason return eb; 164bd681513SChris Mason } 165bd681513SChris Mason 166d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get 167d352ac68SChris Mason * put onto a simple dirty list. transaction.c walks this to make sure they 168d352ac68SChris Mason * get properly updated on disk. 169d352ac68SChris Mason */ 1700b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 1710b86a832SChris Mason { 1720b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1730b246afaSJeff Mahoney 174e7070be1SJosef Bacik if (test_bit(BTRFS_ROOT_DIRTY, &root->state) || 175e7070be1SJosef Bacik !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state)) 176e7070be1SJosef Bacik return; 177e7070be1SJosef Bacik 1780b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 179e7070be1SJosef Bacik if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) { 180e7070be1SJosef Bacik /* Want the extent tree to be the last on the list */ 1814fd786e6SMisono Tomohiro if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID) 182e7070be1SJosef Bacik list_move_tail(&root->dirty_list, 1830b246afaSJeff Mahoney &fs_info->dirty_cowonly_roots); 184e7070be1SJosef Bacik else 185e7070be1SJosef Bacik list_move(&root->dirty_list, 1860b246afaSJeff Mahoney &fs_info->dirty_cowonly_roots); 1870b86a832SChris Mason } 1880b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1890b86a832SChris Mason } 1900b86a832SChris Mason 191d352ac68SChris Mason /* 192d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 193d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 194d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 195d352ac68SChris Mason */ 196be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 197be20aa9dSChris Mason struct btrfs_root *root, 198be20aa9dSChris Mason struct extent_buffer *buf, 199be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 200be20aa9dSChris Mason { 2010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 202be20aa9dSChris Mason struct extent_buffer *cow; 203be20aa9dSChris Mason int ret = 0; 204be20aa9dSChris Mason int level; 2055d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 206be20aa9dSChris Mason 20727cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 2080b246afaSJeff Mahoney trans->transid != fs_info->running_transaction->transid); 20927cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 21027cdeb70SMiao Xie trans->transid != root->last_trans); 211be20aa9dSChris Mason 212be20aa9dSChris Mason level = btrfs_header_level(buf); 2135d4f98a2SYan Zheng if (level == 0) 2145d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 2155d4f98a2SYan Zheng else 2165d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 21731840ae1SZheng Yan 2184d75f8a9SDavid Sterba cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, 2194d75f8a9SDavid Sterba &disk_key, level, buf->start, 0); 2205d4f98a2SYan Zheng if (IS_ERR(cow)) 221be20aa9dSChris Mason return PTR_ERR(cow); 222be20aa9dSChris Mason 22358e8012cSDavid Sterba copy_extent_buffer_full(cow, buf); 224be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 225be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 2265d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 2275d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 2285d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 2295d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 2305d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 2315d4f98a2SYan Zheng else 232be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 233be20aa9dSChris Mason 234de37aa51SNikolay Borisov write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); 2352b82032cSYan Zheng 236be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 2375d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 238e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 2395d4f98a2SYan Zheng else 240e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 2414aec2b52SChris Mason 242be20aa9dSChris Mason if (ret) 243be20aa9dSChris Mason return ret; 244be20aa9dSChris Mason 245be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 246be20aa9dSChris Mason *cow_ret = cow; 247be20aa9dSChris Mason return 0; 248be20aa9dSChris Mason } 249be20aa9dSChris Mason 250bd989ba3SJan Schmidt enum mod_log_op { 251bd989ba3SJan Schmidt MOD_LOG_KEY_REPLACE, 252bd989ba3SJan Schmidt MOD_LOG_KEY_ADD, 253bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE, 254bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_FREEING, 255bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_MOVING, 256bd989ba3SJan Schmidt MOD_LOG_MOVE_KEYS, 257bd989ba3SJan Schmidt MOD_LOG_ROOT_REPLACE, 258bd989ba3SJan Schmidt }; 259bd989ba3SJan Schmidt 260bd989ba3SJan Schmidt struct tree_mod_root { 261bd989ba3SJan Schmidt u64 logical; 262bd989ba3SJan Schmidt u8 level; 263bd989ba3SJan Schmidt }; 264bd989ba3SJan Schmidt 265bd989ba3SJan Schmidt struct tree_mod_elem { 266bd989ba3SJan Schmidt struct rb_node node; 267298cfd36SChandan Rajendra u64 logical; 268097b8a7cSJan Schmidt u64 seq; 269bd989ba3SJan Schmidt enum mod_log_op op; 270bd989ba3SJan Schmidt 271bd989ba3SJan Schmidt /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ 272bd989ba3SJan Schmidt int slot; 273bd989ba3SJan Schmidt 274bd989ba3SJan Schmidt /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ 275bd989ba3SJan Schmidt u64 generation; 276bd989ba3SJan Schmidt 277bd989ba3SJan Schmidt /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ 278bd989ba3SJan Schmidt struct btrfs_disk_key key; 279bd989ba3SJan Schmidt u64 blockptr; 280bd989ba3SJan Schmidt 281bd989ba3SJan Schmidt /* this is used for op == MOD_LOG_MOVE_KEYS */ 282b6dfa35bSDavid Sterba struct { 283b6dfa35bSDavid Sterba int dst_slot; 284b6dfa35bSDavid Sterba int nr_items; 285b6dfa35bSDavid Sterba } move; 286bd989ba3SJan Schmidt 287bd989ba3SJan Schmidt /* this is used for op == MOD_LOG_ROOT_REPLACE */ 288bd989ba3SJan Schmidt struct tree_mod_root old_root; 289bd989ba3SJan Schmidt }; 290bd989ba3SJan Schmidt 291097b8a7cSJan Schmidt /* 292fcebe456SJosef Bacik * Pull a new tree mod seq number for our operation. 293fc36ed7eSJan Schmidt */ 294fcebe456SJosef Bacik static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info) 295fc36ed7eSJan Schmidt { 296fc36ed7eSJan Schmidt return atomic64_inc_return(&fs_info->tree_mod_seq); 297fc36ed7eSJan Schmidt } 298fc36ed7eSJan Schmidt 299fc36ed7eSJan Schmidt /* 300097b8a7cSJan Schmidt * This adds a new blocker to the tree mod log's blocker list if the @elem 301097b8a7cSJan Schmidt * passed does not already have a sequence number set. So when a caller expects 302097b8a7cSJan Schmidt * to record tree modifications, it should ensure to set elem->seq to zero 303097b8a7cSJan Schmidt * before calling btrfs_get_tree_mod_seq. 304097b8a7cSJan Schmidt * Returns a fresh, unused tree log modification sequence number, even if no new 305097b8a7cSJan Schmidt * blocker was added. 306097b8a7cSJan Schmidt */ 307097b8a7cSJan Schmidt u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, 308bd989ba3SJan Schmidt struct seq_list *elem) 309bd989ba3SJan Schmidt { 310b1a09f1eSDavid Sterba write_lock(&fs_info->tree_mod_log_lock); 311bd989ba3SJan Schmidt spin_lock(&fs_info->tree_mod_seq_lock); 312097b8a7cSJan Schmidt if (!elem->seq) { 313fcebe456SJosef Bacik elem->seq = btrfs_inc_tree_mod_seq(fs_info); 314097b8a7cSJan Schmidt list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); 315097b8a7cSJan Schmidt } 316bd989ba3SJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 317b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 318097b8a7cSJan Schmidt 319fcebe456SJosef Bacik return elem->seq; 320bd989ba3SJan Schmidt } 321bd989ba3SJan Schmidt 322bd989ba3SJan Schmidt void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, 323bd989ba3SJan Schmidt struct seq_list *elem) 324bd989ba3SJan Schmidt { 325bd989ba3SJan Schmidt struct rb_root *tm_root; 326bd989ba3SJan Schmidt struct rb_node *node; 327bd989ba3SJan Schmidt struct rb_node *next; 328bd989ba3SJan Schmidt struct seq_list *cur_elem; 329bd989ba3SJan Schmidt struct tree_mod_elem *tm; 330bd989ba3SJan Schmidt u64 min_seq = (u64)-1; 331bd989ba3SJan Schmidt u64 seq_putting = elem->seq; 332bd989ba3SJan Schmidt 333bd989ba3SJan Schmidt if (!seq_putting) 334bd989ba3SJan Schmidt return; 335bd989ba3SJan Schmidt 336bd989ba3SJan Schmidt spin_lock(&fs_info->tree_mod_seq_lock); 337bd989ba3SJan Schmidt list_del(&elem->list); 338097b8a7cSJan Schmidt elem->seq = 0; 339bd989ba3SJan Schmidt 340bd989ba3SJan Schmidt list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) { 341097b8a7cSJan Schmidt if (cur_elem->seq < min_seq) { 342bd989ba3SJan Schmidt if (seq_putting > cur_elem->seq) { 343bd989ba3SJan Schmidt /* 344bd989ba3SJan Schmidt * blocker with lower sequence number exists, we 345bd989ba3SJan Schmidt * cannot remove anything from the log 346bd989ba3SJan Schmidt */ 347097b8a7cSJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 348097b8a7cSJan Schmidt return; 349bd989ba3SJan Schmidt } 350bd989ba3SJan Schmidt min_seq = cur_elem->seq; 351bd989ba3SJan Schmidt } 352bd989ba3SJan Schmidt } 353097b8a7cSJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 354097b8a7cSJan Schmidt 355097b8a7cSJan Schmidt /* 356bd989ba3SJan Schmidt * anything that's lower than the lowest existing (read: blocked) 357bd989ba3SJan Schmidt * sequence number can be removed from the tree. 358bd989ba3SJan Schmidt */ 359b1a09f1eSDavid Sterba write_lock(&fs_info->tree_mod_log_lock); 360bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 361bd989ba3SJan Schmidt for (node = rb_first(tm_root); node; node = next) { 362bd989ba3SJan Schmidt next = rb_next(node); 3636b4df8b6SGeliang Tang tm = rb_entry(node, struct tree_mod_elem, node); 364097b8a7cSJan Schmidt if (tm->seq > min_seq) 365bd989ba3SJan Schmidt continue; 366bd989ba3SJan Schmidt rb_erase(node, tm_root); 367bd989ba3SJan Schmidt kfree(tm); 368bd989ba3SJan Schmidt } 369b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 370bd989ba3SJan Schmidt } 371bd989ba3SJan Schmidt 372bd989ba3SJan Schmidt /* 373bd989ba3SJan Schmidt * key order of the log: 374298cfd36SChandan Rajendra * node/leaf start address -> sequence 375bd989ba3SJan Schmidt * 376298cfd36SChandan Rajendra * The 'start address' is the logical address of the *new* root node 377298cfd36SChandan Rajendra * for root replace operations, or the logical address of the affected 378298cfd36SChandan Rajendra * block for all other operations. 379bd989ba3SJan Schmidt */ 380bd989ba3SJan Schmidt static noinline int 381bd989ba3SJan Schmidt __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) 382bd989ba3SJan Schmidt { 383bd989ba3SJan Schmidt struct rb_root *tm_root; 384bd989ba3SJan Schmidt struct rb_node **new; 385bd989ba3SJan Schmidt struct rb_node *parent = NULL; 386bd989ba3SJan Schmidt struct tree_mod_elem *cur; 387bd989ba3SJan Schmidt 388*73e82fe4SDavid Sterba lockdep_assert_held_write(&fs_info->tree_mod_log_lock); 389*73e82fe4SDavid Sterba 390fcebe456SJosef Bacik tm->seq = btrfs_inc_tree_mod_seq(fs_info); 391bd989ba3SJan Schmidt 392bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 393bd989ba3SJan Schmidt new = &tm_root->rb_node; 394bd989ba3SJan Schmidt while (*new) { 3956b4df8b6SGeliang Tang cur = rb_entry(*new, struct tree_mod_elem, node); 396bd989ba3SJan Schmidt parent = *new; 397298cfd36SChandan Rajendra if (cur->logical < tm->logical) 398bd989ba3SJan Schmidt new = &((*new)->rb_left); 399298cfd36SChandan Rajendra else if (cur->logical > tm->logical) 400bd989ba3SJan Schmidt new = &((*new)->rb_right); 401097b8a7cSJan Schmidt else if (cur->seq < tm->seq) 402bd989ba3SJan Schmidt new = &((*new)->rb_left); 403097b8a7cSJan Schmidt else if (cur->seq > tm->seq) 404bd989ba3SJan Schmidt new = &((*new)->rb_right); 4055de865eeSFilipe David Borba Manana else 4065de865eeSFilipe David Borba Manana return -EEXIST; 407bd989ba3SJan Schmidt } 408bd989ba3SJan Schmidt 409bd989ba3SJan Schmidt rb_link_node(&tm->node, parent, new); 410bd989ba3SJan Schmidt rb_insert_color(&tm->node, tm_root); 4115de865eeSFilipe David Borba Manana return 0; 412bd989ba3SJan Schmidt } 413bd989ba3SJan Schmidt 414097b8a7cSJan Schmidt /* 415097b8a7cSJan Schmidt * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it 416097b8a7cSJan Schmidt * returns zero with the tree_mod_log_lock acquired. The caller must hold 417097b8a7cSJan Schmidt * this until all tree mod log insertions are recorded in the rb tree and then 418b1a09f1eSDavid Sterba * write unlock fs_info::tree_mod_log_lock. 419097b8a7cSJan Schmidt */ 420e9b7fd4dSJan Schmidt static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, 421e9b7fd4dSJan Schmidt struct extent_buffer *eb) { 422e9b7fd4dSJan Schmidt smp_mb(); 423e9b7fd4dSJan Schmidt if (list_empty(&(fs_info)->tree_mod_seq_list)) 424e9b7fd4dSJan Schmidt return 1; 425097b8a7cSJan Schmidt if (eb && btrfs_header_level(eb) == 0) 426e9b7fd4dSJan Schmidt return 1; 4275de865eeSFilipe David Borba Manana 428b1a09f1eSDavid Sterba write_lock(&fs_info->tree_mod_log_lock); 4295de865eeSFilipe David Borba Manana if (list_empty(&(fs_info)->tree_mod_seq_list)) { 430b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 4315de865eeSFilipe David Borba Manana return 1; 4325de865eeSFilipe David Borba Manana } 4335de865eeSFilipe David Borba Manana 434e9b7fd4dSJan Schmidt return 0; 435e9b7fd4dSJan Schmidt } 436e9b7fd4dSJan Schmidt 4375de865eeSFilipe David Borba Manana /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */ 4385de865eeSFilipe David Borba Manana static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info, 4395de865eeSFilipe David Borba Manana struct extent_buffer *eb) 4405de865eeSFilipe David Borba Manana { 4415de865eeSFilipe David Borba Manana smp_mb(); 4425de865eeSFilipe David Borba Manana if (list_empty(&(fs_info)->tree_mod_seq_list)) 4435de865eeSFilipe David Borba Manana return 0; 4445de865eeSFilipe David Borba Manana if (eb && btrfs_header_level(eb) == 0) 4455de865eeSFilipe David Borba Manana return 0; 4465de865eeSFilipe David Borba Manana 4475de865eeSFilipe David Borba Manana return 1; 4485de865eeSFilipe David Borba Manana } 4495de865eeSFilipe David Borba Manana 4505de865eeSFilipe David Borba Manana static struct tree_mod_elem * 4515de865eeSFilipe David Borba Manana alloc_tree_mod_elem(struct extent_buffer *eb, int slot, 452bd989ba3SJan Schmidt enum mod_log_op op, gfp_t flags) 453bd989ba3SJan Schmidt { 454097b8a7cSJan Schmidt struct tree_mod_elem *tm; 455bd989ba3SJan Schmidt 456c8cc6341SJosef Bacik tm = kzalloc(sizeof(*tm), flags); 457c8cc6341SJosef Bacik if (!tm) 4585de865eeSFilipe David Borba Manana return NULL; 459bd989ba3SJan Schmidt 460298cfd36SChandan Rajendra tm->logical = eb->start; 461bd989ba3SJan Schmidt if (op != MOD_LOG_KEY_ADD) { 462bd989ba3SJan Schmidt btrfs_node_key(eb, &tm->key, slot); 463bd989ba3SJan Schmidt tm->blockptr = btrfs_node_blockptr(eb, slot); 464bd989ba3SJan Schmidt } 465bd989ba3SJan Schmidt tm->op = op; 466bd989ba3SJan Schmidt tm->slot = slot; 467bd989ba3SJan Schmidt tm->generation = btrfs_node_ptr_generation(eb, slot); 4685de865eeSFilipe David Borba Manana RB_CLEAR_NODE(&tm->node); 469bd989ba3SJan Schmidt 4705de865eeSFilipe David Borba Manana return tm; 471097b8a7cSJan Schmidt } 472097b8a7cSJan Schmidt 473e09c2efeSDavid Sterba static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot, 474097b8a7cSJan Schmidt enum mod_log_op op, gfp_t flags) 475097b8a7cSJan Schmidt { 4765de865eeSFilipe David Borba Manana struct tree_mod_elem *tm; 4775de865eeSFilipe David Borba Manana int ret; 4785de865eeSFilipe David Borba Manana 479e09c2efeSDavid Sterba if (!tree_mod_need_log(eb->fs_info, eb)) 480097b8a7cSJan Schmidt return 0; 481097b8a7cSJan Schmidt 4825de865eeSFilipe David Borba Manana tm = alloc_tree_mod_elem(eb, slot, op, flags); 4835de865eeSFilipe David Borba Manana if (!tm) 4845de865eeSFilipe David Borba Manana return -ENOMEM; 4855de865eeSFilipe David Borba Manana 486e09c2efeSDavid Sterba if (tree_mod_dont_log(eb->fs_info, eb)) { 4875de865eeSFilipe David Borba Manana kfree(tm); 4885de865eeSFilipe David Borba Manana return 0; 4895de865eeSFilipe David Borba Manana } 4905de865eeSFilipe David Borba Manana 491e09c2efeSDavid Sterba ret = __tree_mod_log_insert(eb->fs_info, tm); 492b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 4935de865eeSFilipe David Borba Manana if (ret) 4945de865eeSFilipe David Borba Manana kfree(tm); 4955de865eeSFilipe David Borba Manana 4965de865eeSFilipe David Borba Manana return ret; 497097b8a7cSJan Schmidt } 498097b8a7cSJan Schmidt 4996074d45fSDavid Sterba static noinline int tree_mod_log_insert_move(struct extent_buffer *eb, 5006074d45fSDavid Sterba int dst_slot, int src_slot, int nr_items) 501bd989ba3SJan Schmidt { 5025de865eeSFilipe David Borba Manana struct tree_mod_elem *tm = NULL; 5035de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 5045de865eeSFilipe David Borba Manana int ret = 0; 505bd989ba3SJan Schmidt int i; 5065de865eeSFilipe David Borba Manana int locked = 0; 507bd989ba3SJan Schmidt 5086074d45fSDavid Sterba if (!tree_mod_need_log(eb->fs_info, eb)) 509f395694cSJan Schmidt return 0; 510bd989ba3SJan Schmidt 511176ef8f5SDavid Sterba tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS); 5125de865eeSFilipe David Borba Manana if (!tm_list) 5135de865eeSFilipe David Borba Manana return -ENOMEM; 514bd989ba3SJan Schmidt 515176ef8f5SDavid Sterba tm = kzalloc(sizeof(*tm), GFP_NOFS); 5165de865eeSFilipe David Borba Manana if (!tm) { 5175de865eeSFilipe David Borba Manana ret = -ENOMEM; 5185de865eeSFilipe David Borba Manana goto free_tms; 5195de865eeSFilipe David Borba Manana } 520f395694cSJan Schmidt 521298cfd36SChandan Rajendra tm->logical = eb->start; 522bd989ba3SJan Schmidt tm->slot = src_slot; 523bd989ba3SJan Schmidt tm->move.dst_slot = dst_slot; 524bd989ba3SJan Schmidt tm->move.nr_items = nr_items; 525bd989ba3SJan Schmidt tm->op = MOD_LOG_MOVE_KEYS; 526bd989ba3SJan Schmidt 5275de865eeSFilipe David Borba Manana for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { 5285de865eeSFilipe David Borba Manana tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot, 529176ef8f5SDavid Sterba MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); 5305de865eeSFilipe David Borba Manana if (!tm_list[i]) { 5315de865eeSFilipe David Borba Manana ret = -ENOMEM; 5325de865eeSFilipe David Borba Manana goto free_tms; 5335de865eeSFilipe David Borba Manana } 534bd989ba3SJan Schmidt } 535bd989ba3SJan Schmidt 5366074d45fSDavid Sterba if (tree_mod_dont_log(eb->fs_info, eb)) 5375de865eeSFilipe David Borba Manana goto free_tms; 5385de865eeSFilipe David Borba Manana locked = 1; 5395de865eeSFilipe David Borba Manana 5405de865eeSFilipe David Borba Manana /* 5415de865eeSFilipe David Borba Manana * When we override something during the move, we log these removals. 5425de865eeSFilipe David Borba Manana * This can only happen when we move towards the beginning of the 5435de865eeSFilipe David Borba Manana * buffer, i.e. dst_slot < src_slot. 5445de865eeSFilipe David Borba Manana */ 5455de865eeSFilipe David Borba Manana for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { 5466074d45fSDavid Sterba ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]); 5475de865eeSFilipe David Borba Manana if (ret) 5485de865eeSFilipe David Borba Manana goto free_tms; 5495de865eeSFilipe David Borba Manana } 5505de865eeSFilipe David Borba Manana 5516074d45fSDavid Sterba ret = __tree_mod_log_insert(eb->fs_info, tm); 5525de865eeSFilipe David Borba Manana if (ret) 5535de865eeSFilipe David Borba Manana goto free_tms; 554b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 5555de865eeSFilipe David Borba Manana kfree(tm_list); 5565de865eeSFilipe David Borba Manana 5575de865eeSFilipe David Borba Manana return 0; 5585de865eeSFilipe David Borba Manana free_tms: 5595de865eeSFilipe David Borba Manana for (i = 0; i < nr_items; i++) { 5605de865eeSFilipe David Borba Manana if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) 5616074d45fSDavid Sterba rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log); 5625de865eeSFilipe David Borba Manana kfree(tm_list[i]); 5635de865eeSFilipe David Borba Manana } 5645de865eeSFilipe David Borba Manana if (locked) 565b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 5665de865eeSFilipe David Borba Manana kfree(tm_list); 5675de865eeSFilipe David Borba Manana kfree(tm); 5685de865eeSFilipe David Borba Manana 5695de865eeSFilipe David Borba Manana return ret; 5705de865eeSFilipe David Borba Manana } 5715de865eeSFilipe David Borba Manana 5725de865eeSFilipe David Borba Manana static inline int 5735de865eeSFilipe David Borba Manana __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, 5745de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list, 5755de865eeSFilipe David Borba Manana int nritems) 576097b8a7cSJan Schmidt { 5775de865eeSFilipe David Borba Manana int i, j; 578097b8a7cSJan Schmidt int ret; 579097b8a7cSJan Schmidt 580097b8a7cSJan Schmidt for (i = nritems - 1; i >= 0; i--) { 5815de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm_list[i]); 5825de865eeSFilipe David Borba Manana if (ret) { 5835de865eeSFilipe David Borba Manana for (j = nritems - 1; j > i; j--) 5845de865eeSFilipe David Borba Manana rb_erase(&tm_list[j]->node, 5855de865eeSFilipe David Borba Manana &fs_info->tree_mod_log); 5865de865eeSFilipe David Borba Manana return ret; 587097b8a7cSJan Schmidt } 588097b8a7cSJan Schmidt } 589097b8a7cSJan Schmidt 5905de865eeSFilipe David Borba Manana return 0; 5915de865eeSFilipe David Borba Manana } 5925de865eeSFilipe David Borba Manana 59395b757c1SDavid Sterba static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root, 59495b757c1SDavid Sterba struct extent_buffer *new_root, int log_removal) 595bd989ba3SJan Schmidt { 59695b757c1SDavid Sterba struct btrfs_fs_info *fs_info = old_root->fs_info; 5975de865eeSFilipe David Borba Manana struct tree_mod_elem *tm = NULL; 5985de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 5995de865eeSFilipe David Borba Manana int nritems = 0; 6005de865eeSFilipe David Borba Manana int ret = 0; 6015de865eeSFilipe David Borba Manana int i; 602bd989ba3SJan Schmidt 6035de865eeSFilipe David Borba Manana if (!tree_mod_need_log(fs_info, NULL)) 604097b8a7cSJan Schmidt return 0; 605097b8a7cSJan Schmidt 6065de865eeSFilipe David Borba Manana if (log_removal && btrfs_header_level(old_root) > 0) { 6075de865eeSFilipe David Borba Manana nritems = btrfs_header_nritems(old_root); 60831e818feSDavid Sterba tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), 609bcc8e07fSDavid Sterba GFP_NOFS); 6105de865eeSFilipe David Borba Manana if (!tm_list) { 6115de865eeSFilipe David Borba Manana ret = -ENOMEM; 6125de865eeSFilipe David Borba Manana goto free_tms; 6135de865eeSFilipe David Borba Manana } 6145de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) { 6155de865eeSFilipe David Borba Manana tm_list[i] = alloc_tree_mod_elem(old_root, i, 616bcc8e07fSDavid Sterba MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); 6175de865eeSFilipe David Borba Manana if (!tm_list[i]) { 6185de865eeSFilipe David Borba Manana ret = -ENOMEM; 6195de865eeSFilipe David Borba Manana goto free_tms; 6205de865eeSFilipe David Borba Manana } 6215de865eeSFilipe David Borba Manana } 6225de865eeSFilipe David Borba Manana } 623d9abbf1cSJan Schmidt 624bcc8e07fSDavid Sterba tm = kzalloc(sizeof(*tm), GFP_NOFS); 6255de865eeSFilipe David Borba Manana if (!tm) { 6265de865eeSFilipe David Borba Manana ret = -ENOMEM; 6275de865eeSFilipe David Borba Manana goto free_tms; 6285de865eeSFilipe David Borba Manana } 629bd989ba3SJan Schmidt 630298cfd36SChandan Rajendra tm->logical = new_root->start; 631bd989ba3SJan Schmidt tm->old_root.logical = old_root->start; 632bd989ba3SJan Schmidt tm->old_root.level = btrfs_header_level(old_root); 633bd989ba3SJan Schmidt tm->generation = btrfs_header_generation(old_root); 634bd989ba3SJan Schmidt tm->op = MOD_LOG_ROOT_REPLACE; 635bd989ba3SJan Schmidt 6365de865eeSFilipe David Borba Manana if (tree_mod_dont_log(fs_info, NULL)) 6375de865eeSFilipe David Borba Manana goto free_tms; 6385de865eeSFilipe David Borba Manana 6395de865eeSFilipe David Borba Manana if (tm_list) 6405de865eeSFilipe David Borba Manana ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems); 6415de865eeSFilipe David Borba Manana if (!ret) 6425de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm); 6435de865eeSFilipe David Borba Manana 644b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 6455de865eeSFilipe David Borba Manana if (ret) 6465de865eeSFilipe David Borba Manana goto free_tms; 6475de865eeSFilipe David Borba Manana kfree(tm_list); 6485de865eeSFilipe David Borba Manana 6495de865eeSFilipe David Borba Manana return ret; 6505de865eeSFilipe David Borba Manana 6515de865eeSFilipe David Borba Manana free_tms: 6525de865eeSFilipe David Borba Manana if (tm_list) { 6535de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) 6545de865eeSFilipe David Borba Manana kfree(tm_list[i]); 6555de865eeSFilipe David Borba Manana kfree(tm_list); 6565de865eeSFilipe David Borba Manana } 6575de865eeSFilipe David Borba Manana kfree(tm); 6585de865eeSFilipe David Borba Manana 6595de865eeSFilipe David Borba Manana return ret; 660bd989ba3SJan Schmidt } 661bd989ba3SJan Schmidt 662bd989ba3SJan Schmidt static struct tree_mod_elem * 663bd989ba3SJan Schmidt __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, 664bd989ba3SJan Schmidt int smallest) 665bd989ba3SJan Schmidt { 666bd989ba3SJan Schmidt struct rb_root *tm_root; 667bd989ba3SJan Schmidt struct rb_node *node; 668bd989ba3SJan Schmidt struct tree_mod_elem *cur = NULL; 669bd989ba3SJan Schmidt struct tree_mod_elem *found = NULL; 670bd989ba3SJan Schmidt 671b1a09f1eSDavid Sterba read_lock(&fs_info->tree_mod_log_lock); 672bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 673bd989ba3SJan Schmidt node = tm_root->rb_node; 674bd989ba3SJan Schmidt while (node) { 6756b4df8b6SGeliang Tang cur = rb_entry(node, struct tree_mod_elem, node); 676298cfd36SChandan Rajendra if (cur->logical < start) { 677bd989ba3SJan Schmidt node = node->rb_left; 678298cfd36SChandan Rajendra } else if (cur->logical > start) { 679bd989ba3SJan Schmidt node = node->rb_right; 680097b8a7cSJan Schmidt } else if (cur->seq < min_seq) { 681bd989ba3SJan Schmidt node = node->rb_left; 682bd989ba3SJan Schmidt } else if (!smallest) { 683bd989ba3SJan Schmidt /* we want the node with the highest seq */ 684bd989ba3SJan Schmidt if (found) 685097b8a7cSJan Schmidt BUG_ON(found->seq > cur->seq); 686bd989ba3SJan Schmidt found = cur; 687bd989ba3SJan Schmidt node = node->rb_left; 688097b8a7cSJan Schmidt } else if (cur->seq > min_seq) { 689bd989ba3SJan Schmidt /* we want the node with the smallest seq */ 690bd989ba3SJan Schmidt if (found) 691097b8a7cSJan Schmidt BUG_ON(found->seq < cur->seq); 692bd989ba3SJan Schmidt found = cur; 693bd989ba3SJan Schmidt node = node->rb_right; 694bd989ba3SJan Schmidt } else { 695bd989ba3SJan Schmidt found = cur; 696bd989ba3SJan Schmidt break; 697bd989ba3SJan Schmidt } 698bd989ba3SJan Schmidt } 699b1a09f1eSDavid Sterba read_unlock(&fs_info->tree_mod_log_lock); 700bd989ba3SJan Schmidt 701bd989ba3SJan Schmidt return found; 702bd989ba3SJan Schmidt } 703bd989ba3SJan Schmidt 704bd989ba3SJan Schmidt /* 705bd989ba3SJan Schmidt * this returns the element from the log with the smallest time sequence 706bd989ba3SJan Schmidt * value that's in the log (the oldest log item). any element with a time 707bd989ba3SJan Schmidt * sequence lower than min_seq will be ignored. 708bd989ba3SJan Schmidt */ 709bd989ba3SJan Schmidt static struct tree_mod_elem * 710bd989ba3SJan Schmidt tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, 711bd989ba3SJan Schmidt u64 min_seq) 712bd989ba3SJan Schmidt { 713bd989ba3SJan Schmidt return __tree_mod_log_search(fs_info, start, min_seq, 1); 714bd989ba3SJan Schmidt } 715bd989ba3SJan Schmidt 716bd989ba3SJan Schmidt /* 717bd989ba3SJan Schmidt * this returns the element from the log with the largest time sequence 718bd989ba3SJan Schmidt * value that's in the log (the most recent log item). any element with 719bd989ba3SJan Schmidt * a time sequence lower than min_seq will be ignored. 720bd989ba3SJan Schmidt */ 721bd989ba3SJan Schmidt static struct tree_mod_elem * 722bd989ba3SJan Schmidt tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) 723bd989ba3SJan Schmidt { 724bd989ba3SJan Schmidt return __tree_mod_log_search(fs_info, start, min_seq, 0); 725bd989ba3SJan Schmidt } 726bd989ba3SJan Schmidt 727ed874f0dSDavid Sterba static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst, 728bd989ba3SJan Schmidt struct extent_buffer *src, unsigned long dst_offset, 72990f8d62eSJan Schmidt unsigned long src_offset, int nr_items) 730bd989ba3SJan Schmidt { 731ed874f0dSDavid Sterba struct btrfs_fs_info *fs_info = dst->fs_info; 7325de865eeSFilipe David Borba Manana int ret = 0; 7335de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 7345de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list_add, **tm_list_rem; 735bd989ba3SJan Schmidt int i; 7365de865eeSFilipe David Borba Manana int locked = 0; 737bd989ba3SJan Schmidt 7385de865eeSFilipe David Borba Manana if (!tree_mod_need_log(fs_info, NULL)) 7395de865eeSFilipe David Borba Manana return 0; 740bd989ba3SJan Schmidt 741c8cc6341SJosef Bacik if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) 7425de865eeSFilipe David Borba Manana return 0; 7435de865eeSFilipe David Borba Manana 74431e818feSDavid Sterba tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *), 7455de865eeSFilipe David Borba Manana GFP_NOFS); 7465de865eeSFilipe David Borba Manana if (!tm_list) 7475de865eeSFilipe David Borba Manana return -ENOMEM; 7485de865eeSFilipe David Borba Manana 7495de865eeSFilipe David Borba Manana tm_list_add = tm_list; 7505de865eeSFilipe David Borba Manana tm_list_rem = tm_list + nr_items; 7515de865eeSFilipe David Borba Manana for (i = 0; i < nr_items; i++) { 7525de865eeSFilipe David Borba Manana tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset, 7535de865eeSFilipe David Borba Manana MOD_LOG_KEY_REMOVE, GFP_NOFS); 7545de865eeSFilipe David Borba Manana if (!tm_list_rem[i]) { 7555de865eeSFilipe David Borba Manana ret = -ENOMEM; 7565de865eeSFilipe David Borba Manana goto free_tms; 7575de865eeSFilipe David Borba Manana } 7585de865eeSFilipe David Borba Manana 7595de865eeSFilipe David Borba Manana tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset, 7605de865eeSFilipe David Borba Manana MOD_LOG_KEY_ADD, GFP_NOFS); 7615de865eeSFilipe David Borba Manana if (!tm_list_add[i]) { 7625de865eeSFilipe David Borba Manana ret = -ENOMEM; 7635de865eeSFilipe David Borba Manana goto free_tms; 7645de865eeSFilipe David Borba Manana } 7655de865eeSFilipe David Borba Manana } 7665de865eeSFilipe David Borba Manana 7675de865eeSFilipe David Borba Manana if (tree_mod_dont_log(fs_info, NULL)) 7685de865eeSFilipe David Borba Manana goto free_tms; 7695de865eeSFilipe David Borba Manana locked = 1; 770bd989ba3SJan Schmidt 771bd989ba3SJan Schmidt for (i = 0; i < nr_items; i++) { 7725de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]); 7735de865eeSFilipe David Borba Manana if (ret) 7745de865eeSFilipe David Borba Manana goto free_tms; 7755de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm_list_add[i]); 7765de865eeSFilipe David Borba Manana if (ret) 7775de865eeSFilipe David Borba Manana goto free_tms; 778bd989ba3SJan Schmidt } 7795de865eeSFilipe David Borba Manana 780b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 7815de865eeSFilipe David Borba Manana kfree(tm_list); 7825de865eeSFilipe David Borba Manana 7835de865eeSFilipe David Borba Manana return 0; 7845de865eeSFilipe David Borba Manana 7855de865eeSFilipe David Borba Manana free_tms: 7865de865eeSFilipe David Borba Manana for (i = 0; i < nr_items * 2; i++) { 7875de865eeSFilipe David Borba Manana if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) 7885de865eeSFilipe David Borba Manana rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log); 7895de865eeSFilipe David Borba Manana kfree(tm_list[i]); 7905de865eeSFilipe David Borba Manana } 7915de865eeSFilipe David Borba Manana if (locked) 792b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 7935de865eeSFilipe David Borba Manana kfree(tm_list); 7945de865eeSFilipe David Borba Manana 7955de865eeSFilipe David Borba Manana return ret; 796bd989ba3SJan Schmidt } 797bd989ba3SJan Schmidt 798db7279a2SDavid Sterba static noinline int tree_mod_log_free_eb(struct extent_buffer *eb) 799bd989ba3SJan Schmidt { 8005de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 8015de865eeSFilipe David Borba Manana int nritems = 0; 8025de865eeSFilipe David Borba Manana int i; 8035de865eeSFilipe David Borba Manana int ret = 0; 8045de865eeSFilipe David Borba Manana 8055de865eeSFilipe David Borba Manana if (btrfs_header_level(eb) == 0) 8065de865eeSFilipe David Borba Manana return 0; 8075de865eeSFilipe David Borba Manana 808db7279a2SDavid Sterba if (!tree_mod_need_log(eb->fs_info, NULL)) 8095de865eeSFilipe David Borba Manana return 0; 8105de865eeSFilipe David Borba Manana 8115de865eeSFilipe David Borba Manana nritems = btrfs_header_nritems(eb); 81231e818feSDavid Sterba tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS); 8135de865eeSFilipe David Borba Manana if (!tm_list) 8145de865eeSFilipe David Borba Manana return -ENOMEM; 8155de865eeSFilipe David Borba Manana 8165de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) { 8175de865eeSFilipe David Borba Manana tm_list[i] = alloc_tree_mod_elem(eb, i, 8185de865eeSFilipe David Borba Manana MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); 8195de865eeSFilipe David Borba Manana if (!tm_list[i]) { 8205de865eeSFilipe David Borba Manana ret = -ENOMEM; 8215de865eeSFilipe David Borba Manana goto free_tms; 8225de865eeSFilipe David Borba Manana } 8235de865eeSFilipe David Borba Manana } 8245de865eeSFilipe David Borba Manana 825db7279a2SDavid Sterba if (tree_mod_dont_log(eb->fs_info, eb)) 8265de865eeSFilipe David Borba Manana goto free_tms; 8275de865eeSFilipe David Borba Manana 828db7279a2SDavid Sterba ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems); 829b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 8305de865eeSFilipe David Borba Manana if (ret) 8315de865eeSFilipe David Borba Manana goto free_tms; 8325de865eeSFilipe David Borba Manana kfree(tm_list); 8335de865eeSFilipe David Borba Manana 8345de865eeSFilipe David Borba Manana return 0; 8355de865eeSFilipe David Borba Manana 8365de865eeSFilipe David Borba Manana free_tms: 8375de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) 8385de865eeSFilipe David Borba Manana kfree(tm_list[i]); 8395de865eeSFilipe David Borba Manana kfree(tm_list); 8405de865eeSFilipe David Borba Manana 8415de865eeSFilipe David Borba Manana return ret; 842bd989ba3SJan Schmidt } 843bd989ba3SJan Schmidt 844d352ac68SChris Mason /* 8455d4f98a2SYan Zheng * check if the tree block can be shared by multiple trees 8465d4f98a2SYan Zheng */ 8475d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root, 8485d4f98a2SYan Zheng struct extent_buffer *buf) 8495d4f98a2SYan Zheng { 8505d4f98a2SYan Zheng /* 85101327610SNicholas D Steeves * Tree blocks not in reference counted trees and tree roots 8525d4f98a2SYan Zheng * are never shared. If a block was allocated after the last 8535d4f98a2SYan Zheng * snapshot and the block was not allocated by tree relocation, 8545d4f98a2SYan Zheng * we know the block is not shared. 8555d4f98a2SYan Zheng */ 85627cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 8575d4f98a2SYan Zheng buf != root->node && buf != root->commit_root && 8585d4f98a2SYan Zheng (btrfs_header_generation(buf) <= 8595d4f98a2SYan Zheng btrfs_root_last_snapshot(&root->root_item) || 8605d4f98a2SYan Zheng btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) 8615d4f98a2SYan Zheng return 1; 862a79865c6SNikolay Borisov 8635d4f98a2SYan Zheng return 0; 8645d4f98a2SYan Zheng } 8655d4f98a2SYan Zheng 8665d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, 8675d4f98a2SYan Zheng struct btrfs_root *root, 8685d4f98a2SYan Zheng struct extent_buffer *buf, 869f0486c68SYan, Zheng struct extent_buffer *cow, 870f0486c68SYan, Zheng int *last_ref) 8715d4f98a2SYan Zheng { 8720b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 8735d4f98a2SYan Zheng u64 refs; 8745d4f98a2SYan Zheng u64 owner; 8755d4f98a2SYan Zheng u64 flags; 8765d4f98a2SYan Zheng u64 new_flags = 0; 8775d4f98a2SYan Zheng int ret; 8785d4f98a2SYan Zheng 8795d4f98a2SYan Zheng /* 8805d4f98a2SYan Zheng * Backrefs update rules: 8815d4f98a2SYan Zheng * 8825d4f98a2SYan Zheng * Always use full backrefs for extent pointers in tree block 8835d4f98a2SYan Zheng * allocated by tree relocation. 8845d4f98a2SYan Zheng * 8855d4f98a2SYan Zheng * If a shared tree block is no longer referenced by its owner 8865d4f98a2SYan Zheng * tree (btrfs_header_owner(buf) == root->root_key.objectid), 8875d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 8885d4f98a2SYan Zheng * 8895d4f98a2SYan Zheng * If a tree block is been relocating 8905d4f98a2SYan Zheng * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), 8915d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 8925d4f98a2SYan Zheng * The reason for this is some operations (such as drop tree) 8935d4f98a2SYan Zheng * are only allowed for blocks use full backrefs. 8945d4f98a2SYan Zheng */ 8955d4f98a2SYan Zheng 8965d4f98a2SYan Zheng if (btrfs_block_can_be_shared(root, buf)) { 8972ff7e61eSJeff Mahoney ret = btrfs_lookup_extent_info(trans, fs_info, buf->start, 8983173a18fSJosef Bacik btrfs_header_level(buf), 1, 8993173a18fSJosef Bacik &refs, &flags); 900be1a5564SMark Fasheh if (ret) 901be1a5564SMark Fasheh return ret; 902e5df9573SMark Fasheh if (refs == 0) { 903e5df9573SMark Fasheh ret = -EROFS; 9040b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, NULL); 905e5df9573SMark Fasheh return ret; 906e5df9573SMark Fasheh } 9075d4f98a2SYan Zheng } else { 9085d4f98a2SYan Zheng refs = 1; 9095d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 9105d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 9115d4f98a2SYan Zheng flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; 9125d4f98a2SYan Zheng else 9135d4f98a2SYan Zheng flags = 0; 9145d4f98a2SYan Zheng } 9155d4f98a2SYan Zheng 9165d4f98a2SYan Zheng owner = btrfs_header_owner(buf); 9175d4f98a2SYan Zheng BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && 9185d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); 9195d4f98a2SYan Zheng 9205d4f98a2SYan Zheng if (refs > 1) { 9215d4f98a2SYan Zheng if ((owner == root->root_key.objectid || 9225d4f98a2SYan Zheng root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && 9235d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 924e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, buf, 1); 925692826b2SJeff Mahoney if (ret) 926692826b2SJeff Mahoney return ret; 9275d4f98a2SYan Zheng 9285d4f98a2SYan Zheng if (root->root_key.objectid == 9295d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 930e339a6b0SJosef Bacik ret = btrfs_dec_ref(trans, root, buf, 0); 931692826b2SJeff Mahoney if (ret) 932692826b2SJeff Mahoney return ret; 933e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 934692826b2SJeff Mahoney if (ret) 935692826b2SJeff Mahoney return ret; 9365d4f98a2SYan Zheng } 9375d4f98a2SYan Zheng new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; 9385d4f98a2SYan Zheng } else { 9395d4f98a2SYan Zheng 9405d4f98a2SYan Zheng if (root->root_key.objectid == 9415d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 942e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 9435d4f98a2SYan Zheng else 944e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 945692826b2SJeff Mahoney if (ret) 946692826b2SJeff Mahoney return ret; 9475d4f98a2SYan Zheng } 9485d4f98a2SYan Zheng if (new_flags != 0) { 949b1c79e09SJosef Bacik int level = btrfs_header_level(buf); 950b1c79e09SJosef Bacik 951f5c8daa5SDavid Sterba ret = btrfs_set_disk_extent_flags(trans, 9525d4f98a2SYan Zheng buf->start, 9535d4f98a2SYan Zheng buf->len, 954b1c79e09SJosef Bacik new_flags, level, 0); 955be1a5564SMark Fasheh if (ret) 956be1a5564SMark Fasheh return ret; 9575d4f98a2SYan Zheng } 9585d4f98a2SYan Zheng } else { 9595d4f98a2SYan Zheng if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { 9605d4f98a2SYan Zheng if (root->root_key.objectid == 9615d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 962e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 9635d4f98a2SYan Zheng else 964e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 965692826b2SJeff Mahoney if (ret) 966692826b2SJeff Mahoney return ret; 967e339a6b0SJosef Bacik ret = btrfs_dec_ref(trans, root, buf, 1); 968692826b2SJeff Mahoney if (ret) 969692826b2SJeff Mahoney return ret; 9705d4f98a2SYan Zheng } 9716a884d7dSDavid Sterba btrfs_clean_tree_block(buf); 972f0486c68SYan, Zheng *last_ref = 1; 9735d4f98a2SYan Zheng } 9745d4f98a2SYan Zheng return 0; 9755d4f98a2SYan Zheng } 9765d4f98a2SYan Zheng 977a6279470SFilipe Manana static struct extent_buffer *alloc_tree_block_no_bg_flush( 978a6279470SFilipe Manana struct btrfs_trans_handle *trans, 979a6279470SFilipe Manana struct btrfs_root *root, 980a6279470SFilipe Manana u64 parent_start, 981a6279470SFilipe Manana const struct btrfs_disk_key *disk_key, 982a6279470SFilipe Manana int level, 983a6279470SFilipe Manana u64 hint, 984a6279470SFilipe Manana u64 empty_size) 985a6279470SFilipe Manana { 986a6279470SFilipe Manana struct btrfs_fs_info *fs_info = root->fs_info; 987a6279470SFilipe Manana struct extent_buffer *ret; 988a6279470SFilipe Manana 989a6279470SFilipe Manana /* 990a6279470SFilipe Manana * If we are COWing a node/leaf from the extent, chunk, device or free 991a6279470SFilipe Manana * space trees, make sure that we do not finish block group creation of 992a6279470SFilipe Manana * pending block groups. We do this to avoid a deadlock. 993a6279470SFilipe Manana * COWing can result in allocation of a new chunk, and flushing pending 994a6279470SFilipe Manana * block groups (btrfs_create_pending_block_groups()) can be triggered 995a6279470SFilipe Manana * when finishing allocation of a new chunk. Creation of a pending block 996a6279470SFilipe Manana * group modifies the extent, chunk, device and free space trees, 997a6279470SFilipe Manana * therefore we could deadlock with ourselves since we are holding a 998a6279470SFilipe Manana * lock on an extent buffer that btrfs_create_pending_block_groups() may 999a6279470SFilipe Manana * try to COW later. 1000a6279470SFilipe Manana * For similar reasons, we also need to delay flushing pending block 1001a6279470SFilipe Manana * groups when splitting a leaf or node, from one of those trees, since 1002a6279470SFilipe Manana * we are holding a write lock on it and its parent or when inserting a 1003a6279470SFilipe Manana * new root node for one of those trees. 1004a6279470SFilipe Manana */ 1005a6279470SFilipe Manana if (root == fs_info->extent_root || 1006a6279470SFilipe Manana root == fs_info->chunk_root || 1007a6279470SFilipe Manana root == fs_info->dev_root || 1008a6279470SFilipe Manana root == fs_info->free_space_root) 1009a6279470SFilipe Manana trans->can_flush_pending_bgs = false; 1010a6279470SFilipe Manana 1011a6279470SFilipe Manana ret = btrfs_alloc_tree_block(trans, root, parent_start, 1012a6279470SFilipe Manana root->root_key.objectid, disk_key, level, 1013a6279470SFilipe Manana hint, empty_size); 1014a6279470SFilipe Manana trans->can_flush_pending_bgs = true; 1015a6279470SFilipe Manana 1016a6279470SFilipe Manana return ret; 1017a6279470SFilipe Manana } 1018a6279470SFilipe Manana 10195d4f98a2SYan Zheng /* 1020d397712bSChris Mason * does the dirty work in cow of a single block. The parent block (if 1021d397712bSChris Mason * supplied) is updated to point to the new cow copy. The new buffer is marked 1022d397712bSChris Mason * dirty and returned locked. If you modify the block it needs to be marked 1023d397712bSChris Mason * dirty again. 1024d352ac68SChris Mason * 1025d352ac68SChris Mason * search_start -- an allocation hint for the new block 1026d352ac68SChris Mason * 1027d397712bSChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in 1028d397712bSChris Mason * bytes the allocator should try to find free next to the block it returns. 1029d397712bSChris Mason * This is just a hint and may be ignored by the allocator. 1030d352ac68SChris Mason */ 1031d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, 10325f39d397SChris Mason struct btrfs_root *root, 10335f39d397SChris Mason struct extent_buffer *buf, 10345f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 10355f39d397SChris Mason struct extent_buffer **cow_ret, 10369fa8cfe7SChris Mason u64 search_start, u64 empty_size) 10376702ed49SChris Mason { 10380b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10395d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 10405f39d397SChris Mason struct extent_buffer *cow; 1041be1a5564SMark Fasheh int level, ret; 1042f0486c68SYan, Zheng int last_ref = 0; 1043925baeddSChris Mason int unlock_orig = 0; 10440f5053ebSGoldwyn Rodrigues u64 parent_start = 0; 10456702ed49SChris Mason 1046925baeddSChris Mason if (*cow_ret == buf) 1047925baeddSChris Mason unlock_orig = 1; 1048925baeddSChris Mason 1049b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1050925baeddSChris Mason 105127cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 10520b246afaSJeff Mahoney trans->transid != fs_info->running_transaction->transid); 105327cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 105427cdeb70SMiao Xie trans->transid != root->last_trans); 10555f39d397SChris Mason 10567bb86316SChris Mason level = btrfs_header_level(buf); 105731840ae1SZheng Yan 10585d4f98a2SYan Zheng if (level == 0) 10595d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 10605d4f98a2SYan Zheng else 10615d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 10625d4f98a2SYan Zheng 10630f5053ebSGoldwyn Rodrigues if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent) 10645d4f98a2SYan Zheng parent_start = parent->start; 10655d4f98a2SYan Zheng 1066a6279470SFilipe Manana cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key, 1067a6279470SFilipe Manana level, search_start, empty_size); 10686702ed49SChris Mason if (IS_ERR(cow)) 10696702ed49SChris Mason return PTR_ERR(cow); 10706702ed49SChris Mason 1071b4ce94deSChris Mason /* cow is set to blocking by btrfs_init_new_buffer */ 1072b4ce94deSChris Mason 107358e8012cSDavid Sterba copy_extent_buffer_full(cow, buf); 1074db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 10755f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 10765d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 10775d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 10785d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 10795d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 10805d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 10815d4f98a2SYan Zheng else 10825f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 10836702ed49SChris Mason 1084de37aa51SNikolay Borisov write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); 10852b82032cSYan Zheng 1086be1a5564SMark Fasheh ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); 1087b68dc2a9SMark Fasheh if (ret) { 108866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1089b68dc2a9SMark Fasheh return ret; 1090b68dc2a9SMark Fasheh } 10911a40e23bSZheng Yan 109227cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) { 109383d4cfd4SJosef Bacik ret = btrfs_reloc_cow_block(trans, root, buf, cow); 109493314e3bSZhaolei if (ret) { 109566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 109683d4cfd4SJosef Bacik return ret; 109783d4cfd4SJosef Bacik } 109893314e3bSZhaolei } 10993fd0a558SYan, Zheng 11006702ed49SChris Mason if (buf == root->node) { 1101925baeddSChris Mason WARN_ON(parent && parent != buf); 11025d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 11035d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 11045d4f98a2SYan Zheng parent_start = buf->start; 1105925baeddSChris Mason 11065f39d397SChris Mason extent_buffer_get(cow); 1107d9d19a01SDavid Sterba ret = tree_mod_log_insert_root(root->node, cow, 1); 1108d9d19a01SDavid Sterba BUG_ON(ret < 0); 1109240f62c8SChris Mason rcu_assign_pointer(root->node, cow); 1110925baeddSChris Mason 1111f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 11125581a51aSJan Schmidt last_ref); 11135f39d397SChris Mason free_extent_buffer(buf); 11140b86a832SChris Mason add_root_to_dirty_list(root); 11156702ed49SChris Mason } else { 11165d4f98a2SYan Zheng WARN_ON(trans->transid != btrfs_header_generation(parent)); 1117e09c2efeSDavid Sterba tree_mod_log_insert_key(parent, parent_slot, 1118c8cc6341SJosef Bacik MOD_LOG_KEY_REPLACE, GFP_NOFS); 11195f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 1120db94535dSChris Mason cow->start); 112174493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 112274493f7aSChris Mason trans->transid); 11236702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 11245de865eeSFilipe David Borba Manana if (last_ref) { 1125db7279a2SDavid Sterba ret = tree_mod_log_free_eb(buf); 11265de865eeSFilipe David Borba Manana if (ret) { 112766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 11285de865eeSFilipe David Borba Manana return ret; 11295de865eeSFilipe David Borba Manana } 11305de865eeSFilipe David Borba Manana } 1131f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 11325581a51aSJan Schmidt last_ref); 11336702ed49SChris Mason } 1134925baeddSChris Mason if (unlock_orig) 1135925baeddSChris Mason btrfs_tree_unlock(buf); 11363083ee2eSJosef Bacik free_extent_buffer_stale(buf); 11376702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 11386702ed49SChris Mason *cow_ret = cow; 11396702ed49SChris Mason return 0; 11406702ed49SChris Mason } 11416702ed49SChris Mason 11425d9e75c4SJan Schmidt /* 11435d9e75c4SJan Schmidt * returns the logical address of the oldest predecessor of the given root. 11445d9e75c4SJan Schmidt * entries older than time_seq are ignored. 11455d9e75c4SJan Schmidt */ 1146bcd24dabSDavid Sterba static struct tree_mod_elem *__tree_mod_log_oldest_root( 114730b0463aSJan Schmidt struct extent_buffer *eb_root, u64 time_seq) 11485d9e75c4SJan Schmidt { 11495d9e75c4SJan Schmidt struct tree_mod_elem *tm; 11505d9e75c4SJan Schmidt struct tree_mod_elem *found = NULL; 115130b0463aSJan Schmidt u64 root_logical = eb_root->start; 11525d9e75c4SJan Schmidt int looped = 0; 11535d9e75c4SJan Schmidt 11545d9e75c4SJan Schmidt if (!time_seq) 115535a3621bSStefan Behrens return NULL; 11565d9e75c4SJan Schmidt 11575d9e75c4SJan Schmidt /* 1158298cfd36SChandan Rajendra * the very last operation that's logged for a root is the 1159298cfd36SChandan Rajendra * replacement operation (if it is replaced at all). this has 1160298cfd36SChandan Rajendra * the logical address of the *new* root, making it the very 1161298cfd36SChandan Rajendra * first operation that's logged for this root. 11625d9e75c4SJan Schmidt */ 11635d9e75c4SJan Schmidt while (1) { 1164bcd24dabSDavid Sterba tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical, 11655d9e75c4SJan Schmidt time_seq); 11665d9e75c4SJan Schmidt if (!looped && !tm) 116735a3621bSStefan Behrens return NULL; 11685d9e75c4SJan Schmidt /* 116928da9fb4SJan Schmidt * if there are no tree operation for the oldest root, we simply 117028da9fb4SJan Schmidt * return it. this should only happen if that (old) root is at 117128da9fb4SJan Schmidt * level 0. 11725d9e75c4SJan Schmidt */ 117328da9fb4SJan Schmidt if (!tm) 117428da9fb4SJan Schmidt break; 11755d9e75c4SJan Schmidt 117628da9fb4SJan Schmidt /* 117728da9fb4SJan Schmidt * if there's an operation that's not a root replacement, we 117828da9fb4SJan Schmidt * found the oldest version of our root. normally, we'll find a 117928da9fb4SJan Schmidt * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. 118028da9fb4SJan Schmidt */ 11815d9e75c4SJan Schmidt if (tm->op != MOD_LOG_ROOT_REPLACE) 11825d9e75c4SJan Schmidt break; 11835d9e75c4SJan Schmidt 11845d9e75c4SJan Schmidt found = tm; 11855d9e75c4SJan Schmidt root_logical = tm->old_root.logical; 11865d9e75c4SJan Schmidt looped = 1; 11875d9e75c4SJan Schmidt } 11885d9e75c4SJan Schmidt 1189a95236d9SJan Schmidt /* if there's no old root to return, return what we found instead */ 1190a95236d9SJan Schmidt if (!found) 1191a95236d9SJan Schmidt found = tm; 1192a95236d9SJan Schmidt 11935d9e75c4SJan Schmidt return found; 11945d9e75c4SJan Schmidt } 11955d9e75c4SJan Schmidt 11965d9e75c4SJan Schmidt /* 11975d9e75c4SJan Schmidt * tm is a pointer to the first operation to rewind within eb. then, all 119801327610SNicholas D Steeves * previous operations will be rewound (until we reach something older than 11995d9e75c4SJan Schmidt * time_seq). 12005d9e75c4SJan Schmidt */ 12015d9e75c4SJan Schmidt static void 1202f1ca7e98SJosef Bacik __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, 1203f1ca7e98SJosef Bacik u64 time_seq, struct tree_mod_elem *first_tm) 12045d9e75c4SJan Schmidt { 12055d9e75c4SJan Schmidt u32 n; 12065d9e75c4SJan Schmidt struct rb_node *next; 12075d9e75c4SJan Schmidt struct tree_mod_elem *tm = first_tm; 12085d9e75c4SJan Schmidt unsigned long o_dst; 12095d9e75c4SJan Schmidt unsigned long o_src; 12105d9e75c4SJan Schmidt unsigned long p_size = sizeof(struct btrfs_key_ptr); 12115d9e75c4SJan Schmidt 12125d9e75c4SJan Schmidt n = btrfs_header_nritems(eb); 1213b1a09f1eSDavid Sterba read_lock(&fs_info->tree_mod_log_lock); 1214097b8a7cSJan Schmidt while (tm && tm->seq >= time_seq) { 12155d9e75c4SJan Schmidt /* 12165d9e75c4SJan Schmidt * all the operations are recorded with the operator used for 12175d9e75c4SJan Schmidt * the modification. as we're going backwards, we do the 12185d9e75c4SJan Schmidt * opposite of each operation here. 12195d9e75c4SJan Schmidt */ 12205d9e75c4SJan Schmidt switch (tm->op) { 12215d9e75c4SJan Schmidt case MOD_LOG_KEY_REMOVE_WHILE_FREEING: 12225d9e75c4SJan Schmidt BUG_ON(tm->slot < n); 12231c697d4aSEric Sandeen /* Fallthrough */ 122495c80bb1SLiu Bo case MOD_LOG_KEY_REMOVE_WHILE_MOVING: 12254c3e6969SChris Mason case MOD_LOG_KEY_REMOVE: 12265d9e75c4SJan Schmidt btrfs_set_node_key(eb, &tm->key, tm->slot); 12275d9e75c4SJan Schmidt btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); 12285d9e75c4SJan Schmidt btrfs_set_node_ptr_generation(eb, tm->slot, 12295d9e75c4SJan Schmidt tm->generation); 12304c3e6969SChris Mason n++; 12315d9e75c4SJan Schmidt break; 12325d9e75c4SJan Schmidt case MOD_LOG_KEY_REPLACE: 12335d9e75c4SJan Schmidt BUG_ON(tm->slot >= n); 12345d9e75c4SJan Schmidt btrfs_set_node_key(eb, &tm->key, tm->slot); 12355d9e75c4SJan Schmidt btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); 12365d9e75c4SJan Schmidt btrfs_set_node_ptr_generation(eb, tm->slot, 12375d9e75c4SJan Schmidt tm->generation); 12385d9e75c4SJan Schmidt break; 12395d9e75c4SJan Schmidt case MOD_LOG_KEY_ADD: 124019956c7eSJan Schmidt /* if a move operation is needed it's in the log */ 12415d9e75c4SJan Schmidt n--; 12425d9e75c4SJan Schmidt break; 12435d9e75c4SJan Schmidt case MOD_LOG_MOVE_KEYS: 1244c3193108SJan Schmidt o_dst = btrfs_node_key_ptr_offset(tm->slot); 1245c3193108SJan Schmidt o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); 1246c3193108SJan Schmidt memmove_extent_buffer(eb, o_dst, o_src, 12475d9e75c4SJan Schmidt tm->move.nr_items * p_size); 12485d9e75c4SJan Schmidt break; 12495d9e75c4SJan Schmidt case MOD_LOG_ROOT_REPLACE: 12505d9e75c4SJan Schmidt /* 12515d9e75c4SJan Schmidt * this operation is special. for roots, this must be 12525d9e75c4SJan Schmidt * handled explicitly before rewinding. 12535d9e75c4SJan Schmidt * for non-roots, this operation may exist if the node 12545d9e75c4SJan Schmidt * was a root: root A -> child B; then A gets empty and 12555d9e75c4SJan Schmidt * B is promoted to the new root. in the mod log, we'll 12565d9e75c4SJan Schmidt * have a root-replace operation for B, a tree block 12575d9e75c4SJan Schmidt * that is no root. we simply ignore that operation. 12585d9e75c4SJan Schmidt */ 12595d9e75c4SJan Schmidt break; 12605d9e75c4SJan Schmidt } 12615d9e75c4SJan Schmidt next = rb_next(&tm->node); 12625d9e75c4SJan Schmidt if (!next) 12635d9e75c4SJan Schmidt break; 12646b4df8b6SGeliang Tang tm = rb_entry(next, struct tree_mod_elem, node); 1265298cfd36SChandan Rajendra if (tm->logical != first_tm->logical) 12665d9e75c4SJan Schmidt break; 12675d9e75c4SJan Schmidt } 1268b1a09f1eSDavid Sterba read_unlock(&fs_info->tree_mod_log_lock); 12695d9e75c4SJan Schmidt btrfs_set_header_nritems(eb, n); 12705d9e75c4SJan Schmidt } 12715d9e75c4SJan Schmidt 127247fb091fSJan Schmidt /* 127301327610SNicholas D Steeves * Called with eb read locked. If the buffer cannot be rewound, the same buffer 127447fb091fSJan Schmidt * is returned. If rewind operations happen, a fresh buffer is returned. The 127547fb091fSJan Schmidt * returned buffer is always read-locked. If the returned buffer is not the 127647fb091fSJan Schmidt * input buffer, the lock on the input buffer is released and the input buffer 127747fb091fSJan Schmidt * is freed (its refcount is decremented). 127847fb091fSJan Schmidt */ 12795d9e75c4SJan Schmidt static struct extent_buffer * 12809ec72677SJosef Bacik tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, 12819ec72677SJosef Bacik struct extent_buffer *eb, u64 time_seq) 12825d9e75c4SJan Schmidt { 12835d9e75c4SJan Schmidt struct extent_buffer *eb_rewin; 12845d9e75c4SJan Schmidt struct tree_mod_elem *tm; 12855d9e75c4SJan Schmidt 12865d9e75c4SJan Schmidt if (!time_seq) 12875d9e75c4SJan Schmidt return eb; 12885d9e75c4SJan Schmidt 12895d9e75c4SJan Schmidt if (btrfs_header_level(eb) == 0) 12905d9e75c4SJan Schmidt return eb; 12915d9e75c4SJan Schmidt 12925d9e75c4SJan Schmidt tm = tree_mod_log_search(fs_info, eb->start, time_seq); 12935d9e75c4SJan Schmidt if (!tm) 12945d9e75c4SJan Schmidt return eb; 12955d9e75c4SJan Schmidt 12969ec72677SJosef Bacik btrfs_set_path_blocking(path); 1297300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 12989ec72677SJosef Bacik 12995d9e75c4SJan Schmidt if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 13005d9e75c4SJan Schmidt BUG_ON(tm->slot != 0); 1301da17066cSJeff Mahoney eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start); 1302db7f3436SJosef Bacik if (!eb_rewin) { 13039ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb); 1304db7f3436SJosef Bacik free_extent_buffer(eb); 1305db7f3436SJosef Bacik return NULL; 1306db7f3436SJosef Bacik } 13075d9e75c4SJan Schmidt btrfs_set_header_bytenr(eb_rewin, eb->start); 13085d9e75c4SJan Schmidt btrfs_set_header_backref_rev(eb_rewin, 13095d9e75c4SJan Schmidt btrfs_header_backref_rev(eb)); 13105d9e75c4SJan Schmidt btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); 1311c3193108SJan Schmidt btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); 13125d9e75c4SJan Schmidt } else { 13135d9e75c4SJan Schmidt eb_rewin = btrfs_clone_extent_buffer(eb); 1314db7f3436SJosef Bacik if (!eb_rewin) { 13159ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb); 1316db7f3436SJosef Bacik free_extent_buffer(eb); 1317db7f3436SJosef Bacik return NULL; 1318db7f3436SJosef Bacik } 13195d9e75c4SJan Schmidt } 13205d9e75c4SJan Schmidt 13219ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb); 13225d9e75c4SJan Schmidt free_extent_buffer(eb); 13235d9e75c4SJan Schmidt 132447fb091fSJan Schmidt btrfs_tree_read_lock(eb_rewin); 1325f1ca7e98SJosef Bacik __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); 132657911b8bSJan Schmidt WARN_ON(btrfs_header_nritems(eb_rewin) > 1327da17066cSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 13285d9e75c4SJan Schmidt 13295d9e75c4SJan Schmidt return eb_rewin; 13305d9e75c4SJan Schmidt } 13315d9e75c4SJan Schmidt 13328ba97a15SJan Schmidt /* 13338ba97a15SJan Schmidt * get_old_root() rewinds the state of @root's root node to the given @time_seq 13348ba97a15SJan Schmidt * value. If there are no changes, the current root->root_node is returned. If 13358ba97a15SJan Schmidt * anything changed in between, there's a fresh buffer allocated on which the 13368ba97a15SJan Schmidt * rewind operations are done. In any case, the returned buffer is read locked. 13378ba97a15SJan Schmidt * Returns NULL on error (with no locks held). 13388ba97a15SJan Schmidt */ 13395d9e75c4SJan Schmidt static inline struct extent_buffer * 13405d9e75c4SJan Schmidt get_old_root(struct btrfs_root *root, u64 time_seq) 13415d9e75c4SJan Schmidt { 13420b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13435d9e75c4SJan Schmidt struct tree_mod_elem *tm; 134430b0463aSJan Schmidt struct extent_buffer *eb = NULL; 134530b0463aSJan Schmidt struct extent_buffer *eb_root; 13467bfdcf7fSLiu Bo struct extent_buffer *old; 1347a95236d9SJan Schmidt struct tree_mod_root *old_root = NULL; 13484325edd0SChris Mason u64 old_generation = 0; 1349a95236d9SJan Schmidt u64 logical; 1350581c1760SQu Wenruo int level; 13515d9e75c4SJan Schmidt 135230b0463aSJan Schmidt eb_root = btrfs_read_lock_root_node(root); 1353bcd24dabSDavid Sterba tm = __tree_mod_log_oldest_root(eb_root, time_seq); 13545d9e75c4SJan Schmidt if (!tm) 135530b0463aSJan Schmidt return eb_root; 13565d9e75c4SJan Schmidt 1357a95236d9SJan Schmidt if (tm->op == MOD_LOG_ROOT_REPLACE) { 13585d9e75c4SJan Schmidt old_root = &tm->old_root; 13595d9e75c4SJan Schmidt old_generation = tm->generation; 1360a95236d9SJan Schmidt logical = old_root->logical; 1361581c1760SQu Wenruo level = old_root->level; 1362a95236d9SJan Schmidt } else { 136330b0463aSJan Schmidt logical = eb_root->start; 1364581c1760SQu Wenruo level = btrfs_header_level(eb_root); 1365a95236d9SJan Schmidt } 13665d9e75c4SJan Schmidt 13670b246afaSJeff Mahoney tm = tree_mod_log_search(fs_info, logical, time_seq); 1368834328a8SJan Schmidt if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 136930b0463aSJan Schmidt btrfs_tree_read_unlock(eb_root); 137030b0463aSJan Schmidt free_extent_buffer(eb_root); 1371581c1760SQu Wenruo old = read_tree_block(fs_info, logical, 0, level, NULL); 137264c043deSLiu Bo if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) { 137364c043deSLiu Bo if (!IS_ERR(old)) 1374416bc658SJosef Bacik free_extent_buffer(old); 13750b246afaSJeff Mahoney btrfs_warn(fs_info, 13760b246afaSJeff Mahoney "failed to read tree block %llu from get_old_root", 13770b246afaSJeff Mahoney logical); 1378834328a8SJan Schmidt } else { 13797bfdcf7fSLiu Bo eb = btrfs_clone_extent_buffer(old); 13807bfdcf7fSLiu Bo free_extent_buffer(old); 1381834328a8SJan Schmidt } 1382834328a8SJan Schmidt } else if (old_root) { 138330b0463aSJan Schmidt btrfs_tree_read_unlock(eb_root); 138430b0463aSJan Schmidt free_extent_buffer(eb_root); 13850b246afaSJeff Mahoney eb = alloc_dummy_extent_buffer(fs_info, logical); 1386834328a8SJan Schmidt } else { 1387300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb_root); 138830b0463aSJan Schmidt eb = btrfs_clone_extent_buffer(eb_root); 13899ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb_root); 139030b0463aSJan Schmidt free_extent_buffer(eb_root); 1391834328a8SJan Schmidt } 1392834328a8SJan Schmidt 13938ba97a15SJan Schmidt if (!eb) 13948ba97a15SJan Schmidt return NULL; 13958ba97a15SJan Schmidt btrfs_tree_read_lock(eb); 1396a95236d9SJan Schmidt if (old_root) { 13975d9e75c4SJan Schmidt btrfs_set_header_bytenr(eb, eb->start); 13985d9e75c4SJan Schmidt btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); 139930b0463aSJan Schmidt btrfs_set_header_owner(eb, btrfs_header_owner(eb_root)); 14005d9e75c4SJan Schmidt btrfs_set_header_level(eb, old_root->level); 14015d9e75c4SJan Schmidt btrfs_set_header_generation(eb, old_generation); 1402a95236d9SJan Schmidt } 140328da9fb4SJan Schmidt if (tm) 14040b246afaSJeff Mahoney __tree_mod_log_rewind(fs_info, eb, time_seq, tm); 140528da9fb4SJan Schmidt else 140628da9fb4SJan Schmidt WARN_ON(btrfs_header_level(eb) != 0); 14070b246afaSJeff Mahoney WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 14085d9e75c4SJan Schmidt 14095d9e75c4SJan Schmidt return eb; 14105d9e75c4SJan Schmidt } 14115d9e75c4SJan Schmidt 14125b6602e7SJan Schmidt int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) 14135b6602e7SJan Schmidt { 14145b6602e7SJan Schmidt struct tree_mod_elem *tm; 14155b6602e7SJan Schmidt int level; 141630b0463aSJan Schmidt struct extent_buffer *eb_root = btrfs_root_node(root); 14175b6602e7SJan Schmidt 1418bcd24dabSDavid Sterba tm = __tree_mod_log_oldest_root(eb_root, time_seq); 14195b6602e7SJan Schmidt if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { 14205b6602e7SJan Schmidt level = tm->old_root.level; 14215b6602e7SJan Schmidt } else { 142230b0463aSJan Schmidt level = btrfs_header_level(eb_root); 14235b6602e7SJan Schmidt } 142430b0463aSJan Schmidt free_extent_buffer(eb_root); 14255b6602e7SJan Schmidt 14265b6602e7SJan Schmidt return level; 14275b6602e7SJan Schmidt } 14285b6602e7SJan Schmidt 14295d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans, 14305d4f98a2SYan Zheng struct btrfs_root *root, 14315d4f98a2SYan Zheng struct extent_buffer *buf) 14325d4f98a2SYan Zheng { 1433f5ee5c9aSJeff Mahoney if (btrfs_is_testing(root->fs_info)) 1434faa2dbf0SJosef Bacik return 0; 1435fccb84c9SDavid Sterba 1436d1980131SDavid Sterba /* Ensure we can see the FORCE_COW bit */ 1437d1980131SDavid Sterba smp_mb__before_atomic(); 1438f1ebcc74SLiu Bo 1439f1ebcc74SLiu Bo /* 1440f1ebcc74SLiu Bo * We do not need to cow a block if 1441f1ebcc74SLiu Bo * 1) this block is not created or changed in this transaction; 1442f1ebcc74SLiu Bo * 2) this block does not belong to TREE_RELOC tree; 1443f1ebcc74SLiu Bo * 3) the root is not forced COW. 1444f1ebcc74SLiu Bo * 1445f1ebcc74SLiu Bo * What is forced COW: 144601327610SNicholas D Steeves * when we create snapshot during committing the transaction, 144752042d8eSAndrea Gelmini * after we've finished copying src root, we must COW the shared 1448f1ebcc74SLiu Bo * block to ensure the metadata consistency. 1449f1ebcc74SLiu Bo */ 14505d4f98a2SYan Zheng if (btrfs_header_generation(buf) == trans->transid && 14515d4f98a2SYan Zheng !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && 14525d4f98a2SYan Zheng !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 1453f1ebcc74SLiu Bo btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && 145427cdeb70SMiao Xie !test_bit(BTRFS_ROOT_FORCE_COW, &root->state)) 14555d4f98a2SYan Zheng return 0; 14565d4f98a2SYan Zheng return 1; 14575d4f98a2SYan Zheng } 14585d4f98a2SYan Zheng 1459d352ac68SChris Mason /* 1460d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 146101327610SNicholas D Steeves * This version of it has extra checks so that a block isn't COWed more than 1462d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 1463d352ac68SChris Mason */ 1464d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, 14655f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 14665f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 14679fa8cfe7SChris Mason struct extent_buffer **cow_ret) 146802217ed2SChris Mason { 14690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14706702ed49SChris Mason u64 search_start; 1471f510cfecSChris Mason int ret; 1472dc17ff8fSChris Mason 147383354f07SJosef Bacik if (test_bit(BTRFS_ROOT_DELETING, &root->state)) 147483354f07SJosef Bacik btrfs_err(fs_info, 147583354f07SJosef Bacik "COW'ing blocks on a fs root that's being dropped"); 147683354f07SJosef Bacik 14770b246afaSJeff Mahoney if (trans->transaction != fs_info->running_transaction) 147831b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 1479c1c9ff7cSGeert Uytterhoeven trans->transid, 14800b246afaSJeff Mahoney fs_info->running_transaction->transid); 148131b1a2bdSJulia Lawall 14820b246afaSJeff Mahoney if (trans->transid != fs_info->generation) 148331b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 14840b246afaSJeff Mahoney trans->transid, fs_info->generation); 1485dc17ff8fSChris Mason 14865d4f98a2SYan Zheng if (!should_cow_block(trans, root, buf)) { 148764c12921SJeff Mahoney trans->dirty = true; 148802217ed2SChris Mason *cow_ret = buf; 148902217ed2SChris Mason return 0; 149002217ed2SChris Mason } 1491c487685dSChris Mason 1492ee22184bSByongho Lee search_start = buf->start & ~((u64)SZ_1G - 1); 1493b4ce94deSChris Mason 1494b4ce94deSChris Mason if (parent) 14958bead258SDavid Sterba btrfs_set_lock_blocking_write(parent); 14968bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 1497b4ce94deSChris Mason 1498f616f5cdSQu Wenruo /* 1499f616f5cdSQu Wenruo * Before CoWing this block for later modification, check if it's 1500f616f5cdSQu Wenruo * the subtree root and do the delayed subtree trace if needed. 1501f616f5cdSQu Wenruo * 1502f616f5cdSQu Wenruo * Also We don't care about the error, as it's handled internally. 1503f616f5cdSQu Wenruo */ 1504f616f5cdSQu Wenruo btrfs_qgroup_trace_subtree_after_cow(trans, root, buf); 1505f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 15069fa8cfe7SChris Mason parent_slot, cow_ret, search_start, 0); 15071abe9b8aSliubo 15081abe9b8aSliubo trace_btrfs_cow_block(root, buf, *cow_ret); 15091abe9b8aSliubo 1510f510cfecSChris Mason return ret; 15112c90e5d6SChris Mason } 15126702ed49SChris Mason 1513d352ac68SChris Mason /* 1514d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 1515d352ac68SChris Mason * node are actually close by 1516d352ac68SChris Mason */ 15176b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 15186702ed49SChris Mason { 15196b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 15206702ed49SChris Mason return 1; 15216b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 15226702ed49SChris Mason return 1; 152302217ed2SChris Mason return 0; 152402217ed2SChris Mason } 152502217ed2SChris Mason 1526081e9573SChris Mason /* 1527081e9573SChris Mason * compare two keys in a memcmp fashion 1528081e9573SChris Mason */ 1529310712b2SOmar Sandoval static int comp_keys(const struct btrfs_disk_key *disk, 1530310712b2SOmar Sandoval const struct btrfs_key *k2) 1531081e9573SChris Mason { 1532081e9573SChris Mason struct btrfs_key k1; 1533081e9573SChris Mason 1534081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 1535081e9573SChris Mason 153620736abaSDiego Calleja return btrfs_comp_cpu_keys(&k1, k2); 1537081e9573SChris Mason } 1538081e9573SChris Mason 1539f3465ca4SJosef Bacik /* 1540f3465ca4SJosef Bacik * same as comp_keys only with two btrfs_key's 1541f3465ca4SJosef Bacik */ 1542310712b2SOmar Sandoval int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2) 1543f3465ca4SJosef Bacik { 1544f3465ca4SJosef Bacik if (k1->objectid > k2->objectid) 1545f3465ca4SJosef Bacik return 1; 1546f3465ca4SJosef Bacik if (k1->objectid < k2->objectid) 1547f3465ca4SJosef Bacik return -1; 1548f3465ca4SJosef Bacik if (k1->type > k2->type) 1549f3465ca4SJosef Bacik return 1; 1550f3465ca4SJosef Bacik if (k1->type < k2->type) 1551f3465ca4SJosef Bacik return -1; 1552f3465ca4SJosef Bacik if (k1->offset > k2->offset) 1553f3465ca4SJosef Bacik return 1; 1554f3465ca4SJosef Bacik if (k1->offset < k2->offset) 1555f3465ca4SJosef Bacik return -1; 1556f3465ca4SJosef Bacik return 0; 1557f3465ca4SJosef Bacik } 1558081e9573SChris Mason 1559d352ac68SChris Mason /* 1560d352ac68SChris Mason * this is used by the defrag code to go through all the 1561d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 1562d352ac68SChris Mason * disk order is close to key order 1563d352ac68SChris Mason */ 15646702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 15655f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 1566de78b51aSEric Sandeen int start_slot, u64 *last_ret, 1567a6b6e75eSChris Mason struct btrfs_key *progress) 15686702ed49SChris Mason { 15690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 15706b80053dSChris Mason struct extent_buffer *cur; 15716702ed49SChris Mason u64 blocknr; 1572ca7a79adSChris Mason u64 gen; 1573e9d0b13bSChris Mason u64 search_start = *last_ret; 1574e9d0b13bSChris Mason u64 last_block = 0; 15756702ed49SChris Mason u64 other; 15766702ed49SChris Mason u32 parent_nritems; 15776702ed49SChris Mason int end_slot; 15786702ed49SChris Mason int i; 15796702ed49SChris Mason int err = 0; 1580f2183bdeSChris Mason int parent_level; 15816b80053dSChris Mason int uptodate; 15826b80053dSChris Mason u32 blocksize; 1583081e9573SChris Mason int progress_passed = 0; 1584081e9573SChris Mason struct btrfs_disk_key disk_key; 15856702ed49SChris Mason 15865708b959SChris Mason parent_level = btrfs_header_level(parent); 15875708b959SChris Mason 15880b246afaSJeff Mahoney WARN_ON(trans->transaction != fs_info->running_transaction); 15890b246afaSJeff Mahoney WARN_ON(trans->transid != fs_info->generation); 159086479a04SChris Mason 15916b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 15920b246afaSJeff Mahoney blocksize = fs_info->nodesize; 15935dfe2be7SFilipe Manana end_slot = parent_nritems - 1; 15946702ed49SChris Mason 15955dfe2be7SFilipe Manana if (parent_nritems <= 1) 15966702ed49SChris Mason return 0; 15976702ed49SChris Mason 15988bead258SDavid Sterba btrfs_set_lock_blocking_write(parent); 1599b4ce94deSChris Mason 16005dfe2be7SFilipe Manana for (i = start_slot; i <= end_slot; i++) { 1601581c1760SQu Wenruo struct btrfs_key first_key; 16026702ed49SChris Mason int close = 1; 1603a6b6e75eSChris Mason 1604081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 1605081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 1606081e9573SChris Mason continue; 1607081e9573SChris Mason 1608081e9573SChris Mason progress_passed = 1; 16096b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 1610ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 1611581c1760SQu Wenruo btrfs_node_key_to_cpu(parent, &first_key, i); 1612e9d0b13bSChris Mason if (last_block == 0) 1613e9d0b13bSChris Mason last_block = blocknr; 16145708b959SChris Mason 16156702ed49SChris Mason if (i > 0) { 16166b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 16176b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 16186702ed49SChris Mason } 16195dfe2be7SFilipe Manana if (!close && i < end_slot) { 16206b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 16216b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 16226702ed49SChris Mason } 1623e9d0b13bSChris Mason if (close) { 1624e9d0b13bSChris Mason last_block = blocknr; 16256702ed49SChris Mason continue; 1626e9d0b13bSChris Mason } 16276702ed49SChris Mason 16280b246afaSJeff Mahoney cur = find_extent_buffer(fs_info, blocknr); 16296b80053dSChris Mason if (cur) 1630b9fab919SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen, 0); 16316b80053dSChris Mason else 16326b80053dSChris Mason uptodate = 0; 16335708b959SChris Mason if (!cur || !uptodate) { 16346b80053dSChris Mason if (!cur) { 1635581c1760SQu Wenruo cur = read_tree_block(fs_info, blocknr, gen, 1636581c1760SQu Wenruo parent_level - 1, 1637581c1760SQu Wenruo &first_key); 163864c043deSLiu Bo if (IS_ERR(cur)) { 163964c043deSLiu Bo return PTR_ERR(cur); 164064c043deSLiu Bo } else if (!extent_buffer_uptodate(cur)) { 1641416bc658SJosef Bacik free_extent_buffer(cur); 164297d9a8a4STsutomu Itoh return -EIO; 1643416bc658SJosef Bacik } 16446b80053dSChris Mason } else if (!uptodate) { 1645581c1760SQu Wenruo err = btrfs_read_buffer(cur, gen, 1646581c1760SQu Wenruo parent_level - 1,&first_key); 1647018642a1STsutomu Itoh if (err) { 1648018642a1STsutomu Itoh free_extent_buffer(cur); 1649018642a1STsutomu Itoh return err; 1650018642a1STsutomu Itoh } 16516702ed49SChris Mason } 1652f2183bdeSChris Mason } 1653e9d0b13bSChris Mason if (search_start == 0) 16546b80053dSChris Mason search_start = last_block; 1655e9d0b13bSChris Mason 1656e7a84565SChris Mason btrfs_tree_lock(cur); 16578bead258SDavid Sterba btrfs_set_lock_blocking_write(cur); 16586b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 1659e7a84565SChris Mason &cur, search_start, 16606b80053dSChris Mason min(16 * blocksize, 16619fa8cfe7SChris Mason (end_slot - i) * blocksize)); 1662252c38f0SYan if (err) { 1663e7a84565SChris Mason btrfs_tree_unlock(cur); 16646b80053dSChris Mason free_extent_buffer(cur); 16656702ed49SChris Mason break; 1666252c38f0SYan } 1667e7a84565SChris Mason search_start = cur->start; 1668e7a84565SChris Mason last_block = cur->start; 1669f2183bdeSChris Mason *last_ret = search_start; 1670e7a84565SChris Mason btrfs_tree_unlock(cur); 1671e7a84565SChris Mason free_extent_buffer(cur); 16726702ed49SChris Mason } 16736702ed49SChris Mason return err; 16746702ed49SChris Mason } 16756702ed49SChris Mason 167674123bd7SChris Mason /* 16775f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 16785f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 16795f39d397SChris Mason * 168074123bd7SChris Mason * the slot in the array is returned via slot, and it points to 168174123bd7SChris Mason * the place where you would insert key if it is not found in 168274123bd7SChris Mason * the array. 168374123bd7SChris Mason * 168474123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 168574123bd7SChris Mason */ 1686e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb, 1687310712b2SOmar Sandoval unsigned long p, int item_size, 1688310712b2SOmar Sandoval const struct btrfs_key *key, 1689be0e5c09SChris Mason int max, int *slot) 1690be0e5c09SChris Mason { 1691be0e5c09SChris Mason int low = 0; 1692be0e5c09SChris Mason int high = max; 1693be0e5c09SChris Mason int mid; 1694be0e5c09SChris Mason int ret; 1695479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 16965f39d397SChris Mason struct btrfs_disk_key unaligned; 16975f39d397SChris Mason unsigned long offset; 16985f39d397SChris Mason char *kaddr = NULL; 16995f39d397SChris Mason unsigned long map_start = 0; 17005f39d397SChris Mason unsigned long map_len = 0; 1701479965d6SChris Mason int err; 1702be0e5c09SChris Mason 17035e24e9afSLiu Bo if (low > high) { 17045e24e9afSLiu Bo btrfs_err(eb->fs_info, 17055e24e9afSLiu Bo "%s: low (%d) > high (%d) eb %llu owner %llu level %d", 17065e24e9afSLiu Bo __func__, low, high, eb->start, 17075e24e9afSLiu Bo btrfs_header_owner(eb), btrfs_header_level(eb)); 17085e24e9afSLiu Bo return -EINVAL; 17095e24e9afSLiu Bo } 17105e24e9afSLiu Bo 1711be0e5c09SChris Mason while (low < high) { 1712be0e5c09SChris Mason mid = (low + high) / 2; 17135f39d397SChris Mason offset = p + mid * item_size; 17145f39d397SChris Mason 1715a6591715SChris Mason if (!kaddr || offset < map_start || 17165f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 17175f39d397SChris Mason map_start + map_len) { 1718934d375bSChris Mason 1719934d375bSChris Mason err = map_private_extent_buffer(eb, offset, 1720479965d6SChris Mason sizeof(struct btrfs_disk_key), 1721a6591715SChris Mason &kaddr, &map_start, &map_len); 17225f39d397SChris Mason 1723479965d6SChris Mason if (!err) { 1724479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 1725479965d6SChris Mason map_start); 1726415b35a5SLiu Bo } else if (err == 1) { 17275f39d397SChris Mason read_extent_buffer(eb, &unaligned, 17285f39d397SChris Mason offset, sizeof(unaligned)); 17295f39d397SChris Mason tmp = &unaligned; 1730415b35a5SLiu Bo } else { 1731415b35a5SLiu Bo return err; 1732479965d6SChris Mason } 1733479965d6SChris Mason 17345f39d397SChris Mason } else { 17355f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 17365f39d397SChris Mason map_start); 17375f39d397SChris Mason } 1738be0e5c09SChris Mason ret = comp_keys(tmp, key); 1739be0e5c09SChris Mason 1740be0e5c09SChris Mason if (ret < 0) 1741be0e5c09SChris Mason low = mid + 1; 1742be0e5c09SChris Mason else if (ret > 0) 1743be0e5c09SChris Mason high = mid; 1744be0e5c09SChris Mason else { 1745be0e5c09SChris Mason *slot = mid; 1746be0e5c09SChris Mason return 0; 1747be0e5c09SChris Mason } 1748be0e5c09SChris Mason } 1749be0e5c09SChris Mason *slot = low; 1750be0e5c09SChris Mason return 1; 1751be0e5c09SChris Mason } 1752be0e5c09SChris Mason 175397571fd0SChris Mason /* 175497571fd0SChris Mason * simple bin_search frontend that does the right thing for 175597571fd0SChris Mason * leaves vs nodes 175697571fd0SChris Mason */ 1757a74b35ecSNikolay Borisov int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key, 17585f39d397SChris Mason int level, int *slot) 1759be0e5c09SChris Mason { 1760f775738fSWang Sheng-Hui if (level == 0) 17615f39d397SChris Mason return generic_bin_search(eb, 17625f39d397SChris Mason offsetof(struct btrfs_leaf, items), 17630783fcfcSChris Mason sizeof(struct btrfs_item), 17645f39d397SChris Mason key, btrfs_header_nritems(eb), 17657518a238SChris Mason slot); 1766f775738fSWang Sheng-Hui else 17675f39d397SChris Mason return generic_bin_search(eb, 17685f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 1769123abc88SChris Mason sizeof(struct btrfs_key_ptr), 17705f39d397SChris Mason key, btrfs_header_nritems(eb), 17717518a238SChris Mason slot); 1772be0e5c09SChris Mason } 1773be0e5c09SChris Mason 1774f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size) 1775f0486c68SYan, Zheng { 1776f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 1777f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 1778f0486c68SYan, Zheng btrfs_root_used(&root->root_item) + size); 1779f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 1780f0486c68SYan, Zheng } 1781f0486c68SYan, Zheng 1782f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size) 1783f0486c68SYan, Zheng { 1784f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 1785f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 1786f0486c68SYan, Zheng btrfs_root_used(&root->root_item) - size); 1787f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 1788f0486c68SYan, Zheng } 1789f0486c68SYan, Zheng 1790d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 1791d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 1792d352ac68SChris Mason */ 1793d0d20b0fSDavid Sterba static noinline struct extent_buffer *read_node_slot( 1794d0d20b0fSDavid Sterba struct extent_buffer *parent, int slot) 1795bb803951SChris Mason { 1796ca7a79adSChris Mason int level = btrfs_header_level(parent); 1797416bc658SJosef Bacik struct extent_buffer *eb; 1798581c1760SQu Wenruo struct btrfs_key first_key; 1799416bc658SJosef Bacik 1800fb770ae4SLiu Bo if (slot < 0 || slot >= btrfs_header_nritems(parent)) 1801fb770ae4SLiu Bo return ERR_PTR(-ENOENT); 1802ca7a79adSChris Mason 1803ca7a79adSChris Mason BUG_ON(level == 0); 1804ca7a79adSChris Mason 1805581c1760SQu Wenruo btrfs_node_key_to_cpu(parent, &first_key, slot); 1806d0d20b0fSDavid Sterba eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot), 1807581c1760SQu Wenruo btrfs_node_ptr_generation(parent, slot), 1808581c1760SQu Wenruo level - 1, &first_key); 1809fb770ae4SLiu Bo if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { 1810416bc658SJosef Bacik free_extent_buffer(eb); 1811fb770ae4SLiu Bo eb = ERR_PTR(-EIO); 1812416bc658SJosef Bacik } 1813416bc658SJosef Bacik 1814416bc658SJosef Bacik return eb; 1815bb803951SChris Mason } 1816bb803951SChris Mason 1817d352ac68SChris Mason /* 1818d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 1819d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 1820d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 1821d352ac68SChris Mason */ 1822e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 182398ed5174SChris Mason struct btrfs_root *root, 182498ed5174SChris Mason struct btrfs_path *path, int level) 1825bb803951SChris Mason { 18260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 18275f39d397SChris Mason struct extent_buffer *right = NULL; 18285f39d397SChris Mason struct extent_buffer *mid; 18295f39d397SChris Mason struct extent_buffer *left = NULL; 18305f39d397SChris Mason struct extent_buffer *parent = NULL; 1831bb803951SChris Mason int ret = 0; 1832bb803951SChris Mason int wret; 1833bb803951SChris Mason int pslot; 1834bb803951SChris Mason int orig_slot = path->slots[level]; 183579f95c82SChris Mason u64 orig_ptr; 1836bb803951SChris Mason 183798e6b1ebSLiu Bo ASSERT(level > 0); 1838bb803951SChris Mason 18395f39d397SChris Mason mid = path->nodes[level]; 1840b4ce94deSChris Mason 1841bd681513SChris Mason WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && 1842bd681513SChris Mason path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); 18437bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 18447bb86316SChris Mason 18451d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 184679f95c82SChris Mason 1847a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 18485f39d397SChris Mason parent = path->nodes[level + 1]; 1849bb803951SChris Mason pslot = path->slots[level + 1]; 1850a05a9bb1SLi Zefan } 1851bb803951SChris Mason 185240689478SChris Mason /* 185340689478SChris Mason * deal with the case where there is only one pointer in the root 185440689478SChris Mason * by promoting the node below to a root 185540689478SChris Mason */ 18565f39d397SChris Mason if (!parent) { 18575f39d397SChris Mason struct extent_buffer *child; 1858bb803951SChris Mason 18595f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 1860bb803951SChris Mason return 0; 1861bb803951SChris Mason 1862bb803951SChris Mason /* promote the child to a root */ 1863d0d20b0fSDavid Sterba child = read_node_slot(mid, 0); 1864fb770ae4SLiu Bo if (IS_ERR(child)) { 1865fb770ae4SLiu Bo ret = PTR_ERR(child); 18660b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, NULL); 1867305a26afSMark Fasheh goto enospc; 1868305a26afSMark Fasheh } 1869305a26afSMark Fasheh 1870925baeddSChris Mason btrfs_tree_lock(child); 18718bead258SDavid Sterba btrfs_set_lock_blocking_write(child); 18729fa8cfe7SChris Mason ret = btrfs_cow_block(trans, root, child, mid, 0, &child); 1873f0486c68SYan, Zheng if (ret) { 1874f0486c68SYan, Zheng btrfs_tree_unlock(child); 1875f0486c68SYan, Zheng free_extent_buffer(child); 1876f0486c68SYan, Zheng goto enospc; 1877f0486c68SYan, Zheng } 18782f375ab9SYan 1879d9d19a01SDavid Sterba ret = tree_mod_log_insert_root(root->node, child, 1); 1880d9d19a01SDavid Sterba BUG_ON(ret < 0); 1881240f62c8SChris Mason rcu_assign_pointer(root->node, child); 1882925baeddSChris Mason 18830b86a832SChris Mason add_root_to_dirty_list(root); 1884925baeddSChris Mason btrfs_tree_unlock(child); 1885b4ce94deSChris Mason 1886925baeddSChris Mason path->locks[level] = 0; 1887bb803951SChris Mason path->nodes[level] = NULL; 18886a884d7dSDavid Sterba btrfs_clean_tree_block(mid); 1889925baeddSChris Mason btrfs_tree_unlock(mid); 1890bb803951SChris Mason /* once for the path */ 18915f39d397SChris Mason free_extent_buffer(mid); 1892f0486c68SYan, Zheng 1893f0486c68SYan, Zheng root_sub_used(root, mid->len); 18945581a51aSJan Schmidt btrfs_free_tree_block(trans, root, mid, 0, 1); 1895bb803951SChris Mason /* once for the root ptr */ 18963083ee2eSJosef Bacik free_extent_buffer_stale(mid); 1897f0486c68SYan, Zheng return 0; 1898bb803951SChris Mason } 18995f39d397SChris Mason if (btrfs_header_nritems(mid) > 19000b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) 1901bb803951SChris Mason return 0; 1902bb803951SChris Mason 1903d0d20b0fSDavid Sterba left = read_node_slot(parent, pslot - 1); 1904fb770ae4SLiu Bo if (IS_ERR(left)) 1905fb770ae4SLiu Bo left = NULL; 1906fb770ae4SLiu Bo 19075f39d397SChris Mason if (left) { 1908925baeddSChris Mason btrfs_tree_lock(left); 19098bead258SDavid Sterba btrfs_set_lock_blocking_write(left); 19105f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 19119fa8cfe7SChris Mason parent, pslot - 1, &left); 191254aa1f4dSChris Mason if (wret) { 191354aa1f4dSChris Mason ret = wret; 191454aa1f4dSChris Mason goto enospc; 191554aa1f4dSChris Mason } 19162cc58cf2SChris Mason } 1917fb770ae4SLiu Bo 1918d0d20b0fSDavid Sterba right = read_node_slot(parent, pslot + 1); 1919fb770ae4SLiu Bo if (IS_ERR(right)) 1920fb770ae4SLiu Bo right = NULL; 1921fb770ae4SLiu Bo 19225f39d397SChris Mason if (right) { 1923925baeddSChris Mason btrfs_tree_lock(right); 19248bead258SDavid Sterba btrfs_set_lock_blocking_write(right); 19255f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 19269fa8cfe7SChris Mason parent, pslot + 1, &right); 19272cc58cf2SChris Mason if (wret) { 19282cc58cf2SChris Mason ret = wret; 19292cc58cf2SChris Mason goto enospc; 19302cc58cf2SChris Mason } 19312cc58cf2SChris Mason } 19322cc58cf2SChris Mason 19332cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 19345f39d397SChris Mason if (left) { 19355f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 1936d30a668fSDavid Sterba wret = push_node_left(trans, left, mid, 1); 193779f95c82SChris Mason if (wret < 0) 193879f95c82SChris Mason ret = wret; 1939bb803951SChris Mason } 194079f95c82SChris Mason 194179f95c82SChris Mason /* 194279f95c82SChris Mason * then try to empty the right most buffer into the middle 194379f95c82SChris Mason */ 19445f39d397SChris Mason if (right) { 1945d30a668fSDavid Sterba wret = push_node_left(trans, mid, right, 1); 194654aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 194779f95c82SChris Mason ret = wret; 19485f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 19496a884d7dSDavid Sterba btrfs_clean_tree_block(right); 1950925baeddSChris Mason btrfs_tree_unlock(right); 1951afe5fea7STsutomu Itoh del_ptr(root, path, level + 1, pslot + 1); 1952f0486c68SYan, Zheng root_sub_used(root, right->len); 19535581a51aSJan Schmidt btrfs_free_tree_block(trans, root, right, 0, 1); 19543083ee2eSJosef Bacik free_extent_buffer_stale(right); 1955f0486c68SYan, Zheng right = NULL; 1956bb803951SChris Mason } else { 19575f39d397SChris Mason struct btrfs_disk_key right_key; 19585f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 19590e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot + 1, 19600e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 19610e82bcfeSDavid Sterba BUG_ON(ret < 0); 19625f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 19635f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1964bb803951SChris Mason } 1965bb803951SChris Mason } 19665f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 196779f95c82SChris Mason /* 196879f95c82SChris Mason * we're not allowed to leave a node with one item in the 196979f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 197079f95c82SChris Mason * could try to delete the only pointer in this node. 197179f95c82SChris Mason * So, pull some keys from the left. 197279f95c82SChris Mason * There has to be a left pointer at this point because 197379f95c82SChris Mason * otherwise we would have pulled some pointers from the 197479f95c82SChris Mason * right 197579f95c82SChris Mason */ 1976305a26afSMark Fasheh if (!left) { 1977305a26afSMark Fasheh ret = -EROFS; 19780b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, NULL); 1979305a26afSMark Fasheh goto enospc; 1980305a26afSMark Fasheh } 198155d32ed8SDavid Sterba wret = balance_node_right(trans, mid, left); 198254aa1f4dSChris Mason if (wret < 0) { 198379f95c82SChris Mason ret = wret; 198454aa1f4dSChris Mason goto enospc; 198554aa1f4dSChris Mason } 1986bce4eae9SChris Mason if (wret == 1) { 1987d30a668fSDavid Sterba wret = push_node_left(trans, left, mid, 1); 1988bce4eae9SChris Mason if (wret < 0) 1989bce4eae9SChris Mason ret = wret; 1990bce4eae9SChris Mason } 199179f95c82SChris Mason BUG_ON(wret == 1); 199279f95c82SChris Mason } 19935f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 19946a884d7dSDavid Sterba btrfs_clean_tree_block(mid); 1995925baeddSChris Mason btrfs_tree_unlock(mid); 1996afe5fea7STsutomu Itoh del_ptr(root, path, level + 1, pslot); 1997f0486c68SYan, Zheng root_sub_used(root, mid->len); 19985581a51aSJan Schmidt btrfs_free_tree_block(trans, root, mid, 0, 1); 19993083ee2eSJosef Bacik free_extent_buffer_stale(mid); 2000f0486c68SYan, Zheng mid = NULL; 200179f95c82SChris Mason } else { 200279f95c82SChris Mason /* update the parent key to reflect our changes */ 20035f39d397SChris Mason struct btrfs_disk_key mid_key; 20045f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 20050e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot, 20060e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 20070e82bcfeSDavid Sterba BUG_ON(ret < 0); 20085f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 20095f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 201079f95c82SChris Mason } 2011bb803951SChris Mason 201279f95c82SChris Mason /* update the path */ 20135f39d397SChris Mason if (left) { 20145f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 20155f39d397SChris Mason extent_buffer_get(left); 2016925baeddSChris Mason /* left was locked after cow */ 20175f39d397SChris Mason path->nodes[level] = left; 2018bb803951SChris Mason path->slots[level + 1] -= 1; 2019bb803951SChris Mason path->slots[level] = orig_slot; 2020925baeddSChris Mason if (mid) { 2021925baeddSChris Mason btrfs_tree_unlock(mid); 20225f39d397SChris Mason free_extent_buffer(mid); 2023925baeddSChris Mason } 2024bb803951SChris Mason } else { 20255f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 2026bb803951SChris Mason path->slots[level] = orig_slot; 2027bb803951SChris Mason } 2028bb803951SChris Mason } 202979f95c82SChris Mason /* double check we haven't messed things up */ 2030e20d96d6SChris Mason if (orig_ptr != 20315f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 203279f95c82SChris Mason BUG(); 203354aa1f4dSChris Mason enospc: 2034925baeddSChris Mason if (right) { 2035925baeddSChris Mason btrfs_tree_unlock(right); 20365f39d397SChris Mason free_extent_buffer(right); 2037925baeddSChris Mason } 2038925baeddSChris Mason if (left) { 2039925baeddSChris Mason if (path->nodes[level] != left) 2040925baeddSChris Mason btrfs_tree_unlock(left); 20415f39d397SChris Mason free_extent_buffer(left); 2042925baeddSChris Mason } 2043bb803951SChris Mason return ret; 2044bb803951SChris Mason } 2045bb803951SChris Mason 2046d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 2047d352ac68SChris Mason * when they are completely full. This is also done top down, so we 2048d352ac68SChris Mason * have to be pessimistic. 2049d352ac68SChris Mason */ 2050d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, 2051e66f709bSChris Mason struct btrfs_root *root, 2052e66f709bSChris Mason struct btrfs_path *path, int level) 2053e66f709bSChris Mason { 20540b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 20555f39d397SChris Mason struct extent_buffer *right = NULL; 20565f39d397SChris Mason struct extent_buffer *mid; 20575f39d397SChris Mason struct extent_buffer *left = NULL; 20585f39d397SChris Mason struct extent_buffer *parent = NULL; 2059e66f709bSChris Mason int ret = 0; 2060e66f709bSChris Mason int wret; 2061e66f709bSChris Mason int pslot; 2062e66f709bSChris Mason int orig_slot = path->slots[level]; 2063e66f709bSChris Mason 2064e66f709bSChris Mason if (level == 0) 2065e66f709bSChris Mason return 1; 2066e66f709bSChris Mason 20675f39d397SChris Mason mid = path->nodes[level]; 20687bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 2069e66f709bSChris Mason 2070a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 20715f39d397SChris Mason parent = path->nodes[level + 1]; 2072e66f709bSChris Mason pslot = path->slots[level + 1]; 2073a05a9bb1SLi Zefan } 2074e66f709bSChris Mason 20755f39d397SChris Mason if (!parent) 2076e66f709bSChris Mason return 1; 2077e66f709bSChris Mason 2078d0d20b0fSDavid Sterba left = read_node_slot(parent, pslot - 1); 2079fb770ae4SLiu Bo if (IS_ERR(left)) 2080fb770ae4SLiu Bo left = NULL; 2081e66f709bSChris Mason 2082e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 20835f39d397SChris Mason if (left) { 2084e66f709bSChris Mason u32 left_nr; 2085925baeddSChris Mason 2086925baeddSChris Mason btrfs_tree_lock(left); 20878bead258SDavid Sterba btrfs_set_lock_blocking_write(left); 2088b4ce94deSChris Mason 20895f39d397SChris Mason left_nr = btrfs_header_nritems(left); 20900b246afaSJeff Mahoney if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { 209133ade1f8SChris Mason wret = 1; 209233ade1f8SChris Mason } else { 20935f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 20949fa8cfe7SChris Mason pslot - 1, &left); 209554aa1f4dSChris Mason if (ret) 209654aa1f4dSChris Mason wret = 1; 209754aa1f4dSChris Mason else { 2098d30a668fSDavid Sterba wret = push_node_left(trans, left, mid, 0); 209954aa1f4dSChris Mason } 210033ade1f8SChris Mason } 2101e66f709bSChris Mason if (wret < 0) 2102e66f709bSChris Mason ret = wret; 2103e66f709bSChris Mason if (wret == 0) { 21045f39d397SChris Mason struct btrfs_disk_key disk_key; 2105e66f709bSChris Mason orig_slot += left_nr; 21065f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 21070e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot, 21080e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 21090e82bcfeSDavid Sterba BUG_ON(ret < 0); 21105f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 21115f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 21125f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 21135f39d397SChris Mason path->nodes[level] = left; 2114e66f709bSChris Mason path->slots[level + 1] -= 1; 2115e66f709bSChris Mason path->slots[level] = orig_slot; 2116925baeddSChris Mason btrfs_tree_unlock(mid); 21175f39d397SChris Mason free_extent_buffer(mid); 2118e66f709bSChris Mason } else { 2119e66f709bSChris Mason orig_slot -= 21205f39d397SChris Mason btrfs_header_nritems(left); 2121e66f709bSChris Mason path->slots[level] = orig_slot; 2122925baeddSChris Mason btrfs_tree_unlock(left); 21235f39d397SChris Mason free_extent_buffer(left); 2124e66f709bSChris Mason } 2125e66f709bSChris Mason return 0; 2126e66f709bSChris Mason } 2127925baeddSChris Mason btrfs_tree_unlock(left); 21285f39d397SChris Mason free_extent_buffer(left); 2129e66f709bSChris Mason } 2130d0d20b0fSDavid Sterba right = read_node_slot(parent, pslot + 1); 2131fb770ae4SLiu Bo if (IS_ERR(right)) 2132fb770ae4SLiu Bo right = NULL; 2133e66f709bSChris Mason 2134e66f709bSChris Mason /* 2135e66f709bSChris Mason * then try to empty the right most buffer into the middle 2136e66f709bSChris Mason */ 21375f39d397SChris Mason if (right) { 213833ade1f8SChris Mason u32 right_nr; 2139b4ce94deSChris Mason 2140925baeddSChris Mason btrfs_tree_lock(right); 21418bead258SDavid Sterba btrfs_set_lock_blocking_write(right); 2142b4ce94deSChris Mason 21435f39d397SChris Mason right_nr = btrfs_header_nritems(right); 21440b246afaSJeff Mahoney if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { 214533ade1f8SChris Mason wret = 1; 214633ade1f8SChris Mason } else { 21475f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 21485f39d397SChris Mason parent, pslot + 1, 21499fa8cfe7SChris Mason &right); 215054aa1f4dSChris Mason if (ret) 215154aa1f4dSChris Mason wret = 1; 215254aa1f4dSChris Mason else { 215355d32ed8SDavid Sterba wret = balance_node_right(trans, right, mid); 215433ade1f8SChris Mason } 215554aa1f4dSChris Mason } 2156e66f709bSChris Mason if (wret < 0) 2157e66f709bSChris Mason ret = wret; 2158e66f709bSChris Mason if (wret == 0) { 21595f39d397SChris Mason struct btrfs_disk_key disk_key; 21605f39d397SChris Mason 21615f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 21620e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot + 1, 21630e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 21640e82bcfeSDavid Sterba BUG_ON(ret < 0); 21655f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 21665f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 21675f39d397SChris Mason 21685f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 21695f39d397SChris Mason path->nodes[level] = right; 2170e66f709bSChris Mason path->slots[level + 1] += 1; 2171e66f709bSChris Mason path->slots[level] = orig_slot - 21725f39d397SChris Mason btrfs_header_nritems(mid); 2173925baeddSChris Mason btrfs_tree_unlock(mid); 21745f39d397SChris Mason free_extent_buffer(mid); 2175e66f709bSChris Mason } else { 2176925baeddSChris Mason btrfs_tree_unlock(right); 21775f39d397SChris Mason free_extent_buffer(right); 2178e66f709bSChris Mason } 2179e66f709bSChris Mason return 0; 2180e66f709bSChris Mason } 2181925baeddSChris Mason btrfs_tree_unlock(right); 21825f39d397SChris Mason free_extent_buffer(right); 2183e66f709bSChris Mason } 2184e66f709bSChris Mason return 1; 2185e66f709bSChris Mason } 2186e66f709bSChris Mason 218774123bd7SChris Mason /* 2188d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 2189d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 21903c69faecSChris Mason */ 21912ff7e61eSJeff Mahoney static void reada_for_search(struct btrfs_fs_info *fs_info, 2192e02119d5SChris Mason struct btrfs_path *path, 219301f46658SChris Mason int level, int slot, u64 objectid) 21943c69faecSChris Mason { 21955f39d397SChris Mason struct extent_buffer *node; 219601f46658SChris Mason struct btrfs_disk_key disk_key; 21973c69faecSChris Mason u32 nritems; 21983c69faecSChris Mason u64 search; 2199a7175319SChris Mason u64 target; 22006b80053dSChris Mason u64 nread = 0; 22015f39d397SChris Mason struct extent_buffer *eb; 22026b80053dSChris Mason u32 nr; 22036b80053dSChris Mason u32 blocksize; 22046b80053dSChris Mason u32 nscan = 0; 2205db94535dSChris Mason 2206a6b6e75eSChris Mason if (level != 1) 22073c69faecSChris Mason return; 22083c69faecSChris Mason 22096702ed49SChris Mason if (!path->nodes[level]) 22106702ed49SChris Mason return; 22116702ed49SChris Mason 22125f39d397SChris Mason node = path->nodes[level]; 2213925baeddSChris Mason 22143c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 22150b246afaSJeff Mahoney blocksize = fs_info->nodesize; 22160b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, search); 22175f39d397SChris Mason if (eb) { 22185f39d397SChris Mason free_extent_buffer(eb); 22193c69faecSChris Mason return; 22203c69faecSChris Mason } 22213c69faecSChris Mason 2222a7175319SChris Mason target = search; 22236b80053dSChris Mason 22245f39d397SChris Mason nritems = btrfs_header_nritems(node); 22256b80053dSChris Mason nr = slot; 222625b8b936SJosef Bacik 22273c69faecSChris Mason while (1) { 2228e4058b54SDavid Sterba if (path->reada == READA_BACK) { 22296b80053dSChris Mason if (nr == 0) 22303c69faecSChris Mason break; 22316b80053dSChris Mason nr--; 2232e4058b54SDavid Sterba } else if (path->reada == READA_FORWARD) { 22336b80053dSChris Mason nr++; 22346b80053dSChris Mason if (nr >= nritems) 22356b80053dSChris Mason break; 22363c69faecSChris Mason } 2237e4058b54SDavid Sterba if (path->reada == READA_BACK && objectid) { 223801f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 223901f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 224001f46658SChris Mason break; 224101f46658SChris Mason } 22426b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 2243a7175319SChris Mason if ((search <= target && target - search <= 65536) || 2244a7175319SChris Mason (search > target && search - target <= 65536)) { 22452ff7e61eSJeff Mahoney readahead_tree_block(fs_info, search); 22466b80053dSChris Mason nread += blocksize; 22473c69faecSChris Mason } 22486b80053dSChris Mason nscan++; 2249a7175319SChris Mason if ((nread > 65536 || nscan > 32)) 22506b80053dSChris Mason break; 22513c69faecSChris Mason } 22523c69faecSChris Mason } 2253925baeddSChris Mason 22542ff7e61eSJeff Mahoney static noinline void reada_for_balance(struct btrfs_fs_info *fs_info, 2255b4ce94deSChris Mason struct btrfs_path *path, int level) 2256b4ce94deSChris Mason { 2257b4ce94deSChris Mason int slot; 2258b4ce94deSChris Mason int nritems; 2259b4ce94deSChris Mason struct extent_buffer *parent; 2260b4ce94deSChris Mason struct extent_buffer *eb; 2261b4ce94deSChris Mason u64 gen; 2262b4ce94deSChris Mason u64 block1 = 0; 2263b4ce94deSChris Mason u64 block2 = 0; 2264b4ce94deSChris Mason 22658c594ea8SChris Mason parent = path->nodes[level + 1]; 2266b4ce94deSChris Mason if (!parent) 22670b08851fSJosef Bacik return; 2268b4ce94deSChris Mason 2269b4ce94deSChris Mason nritems = btrfs_header_nritems(parent); 22708c594ea8SChris Mason slot = path->slots[level + 1]; 2271b4ce94deSChris Mason 2272b4ce94deSChris Mason if (slot > 0) { 2273b4ce94deSChris Mason block1 = btrfs_node_blockptr(parent, slot - 1); 2274b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot - 1); 22750b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, block1); 2276b9fab919SChris Mason /* 2277b9fab919SChris Mason * if we get -eagain from btrfs_buffer_uptodate, we 2278b9fab919SChris Mason * don't want to return eagain here. That will loop 2279b9fab919SChris Mason * forever 2280b9fab919SChris Mason */ 2281b9fab919SChris Mason if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) 2282b4ce94deSChris Mason block1 = 0; 2283b4ce94deSChris Mason free_extent_buffer(eb); 2284b4ce94deSChris Mason } 22858c594ea8SChris Mason if (slot + 1 < nritems) { 2286b4ce94deSChris Mason block2 = btrfs_node_blockptr(parent, slot + 1); 2287b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot + 1); 22880b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, block2); 2289b9fab919SChris Mason if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) 2290b4ce94deSChris Mason block2 = 0; 2291b4ce94deSChris Mason free_extent_buffer(eb); 2292b4ce94deSChris Mason } 22938c594ea8SChris Mason 2294b4ce94deSChris Mason if (block1) 22952ff7e61eSJeff Mahoney readahead_tree_block(fs_info, block1); 2296b4ce94deSChris Mason if (block2) 22972ff7e61eSJeff Mahoney readahead_tree_block(fs_info, block2); 2298b4ce94deSChris Mason } 2299b4ce94deSChris Mason 2300b4ce94deSChris Mason 2301b4ce94deSChris Mason /* 2302d397712bSChris Mason * when we walk down the tree, it is usually safe to unlock the higher layers 2303d397712bSChris Mason * in the tree. The exceptions are when our path goes through slot 0, because 2304d397712bSChris Mason * operations on the tree might require changing key pointers higher up in the 2305d397712bSChris Mason * tree. 2306d352ac68SChris Mason * 2307d397712bSChris Mason * callers might also have set path->keep_locks, which tells this code to keep 2308d397712bSChris Mason * the lock if the path points to the last slot in the block. This is part of 2309d397712bSChris Mason * walking through the tree, and selecting the next slot in the higher block. 2310d352ac68SChris Mason * 2311d397712bSChris Mason * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so 2312d397712bSChris Mason * if lowest_unlock is 1, level 0 won't be unlocked 2313d352ac68SChris Mason */ 2314e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level, 2315f7c79f30SChris Mason int lowest_unlock, int min_write_lock_level, 2316f7c79f30SChris Mason int *write_lock_level) 2317925baeddSChris Mason { 2318925baeddSChris Mason int i; 2319925baeddSChris Mason int skip_level = level; 2320051e1b9fSChris Mason int no_skips = 0; 2321925baeddSChris Mason struct extent_buffer *t; 2322925baeddSChris Mason 2323925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2324925baeddSChris Mason if (!path->nodes[i]) 2325925baeddSChris Mason break; 2326925baeddSChris Mason if (!path->locks[i]) 2327925baeddSChris Mason break; 2328051e1b9fSChris Mason if (!no_skips && path->slots[i] == 0) { 2329925baeddSChris Mason skip_level = i + 1; 2330925baeddSChris Mason continue; 2331925baeddSChris Mason } 2332051e1b9fSChris Mason if (!no_skips && path->keep_locks) { 2333925baeddSChris Mason u32 nritems; 2334925baeddSChris Mason t = path->nodes[i]; 2335925baeddSChris Mason nritems = btrfs_header_nritems(t); 2336051e1b9fSChris Mason if (nritems < 1 || path->slots[i] >= nritems - 1) { 2337925baeddSChris Mason skip_level = i + 1; 2338925baeddSChris Mason continue; 2339925baeddSChris Mason } 2340925baeddSChris Mason } 2341051e1b9fSChris Mason if (skip_level < i && i >= lowest_unlock) 2342051e1b9fSChris Mason no_skips = 1; 2343051e1b9fSChris Mason 2344925baeddSChris Mason t = path->nodes[i]; 2345d80bb3f9SLiu Bo if (i >= lowest_unlock && i > skip_level) { 2346bd681513SChris Mason btrfs_tree_unlock_rw(t, path->locks[i]); 2347925baeddSChris Mason path->locks[i] = 0; 2348f7c79f30SChris Mason if (write_lock_level && 2349f7c79f30SChris Mason i > min_write_lock_level && 2350f7c79f30SChris Mason i <= *write_lock_level) { 2351f7c79f30SChris Mason *write_lock_level = i - 1; 2352f7c79f30SChris Mason } 2353925baeddSChris Mason } 2354925baeddSChris Mason } 2355925baeddSChris Mason } 2356925baeddSChris Mason 23573c69faecSChris Mason /* 2358b4ce94deSChris Mason * This releases any locks held in the path starting at level and 2359b4ce94deSChris Mason * going all the way up to the root. 2360b4ce94deSChris Mason * 2361b4ce94deSChris Mason * btrfs_search_slot will keep the lock held on higher nodes in a few 2362b4ce94deSChris Mason * corner cases, such as COW of the block at slot zero in the node. This 2363b4ce94deSChris Mason * ignores those rules, and it should only be called when there are no 2364b4ce94deSChris Mason * more updates to be done higher up in the tree. 2365b4ce94deSChris Mason */ 2366b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) 2367b4ce94deSChris Mason { 2368b4ce94deSChris Mason int i; 2369b4ce94deSChris Mason 237009a2a8f9SJosef Bacik if (path->keep_locks) 2371b4ce94deSChris Mason return; 2372b4ce94deSChris Mason 2373b4ce94deSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2374b4ce94deSChris Mason if (!path->nodes[i]) 237512f4daccSChris Mason continue; 2376b4ce94deSChris Mason if (!path->locks[i]) 237712f4daccSChris Mason continue; 2378bd681513SChris Mason btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); 2379b4ce94deSChris Mason path->locks[i] = 0; 2380b4ce94deSChris Mason } 2381b4ce94deSChris Mason } 2382b4ce94deSChris Mason 2383b4ce94deSChris Mason /* 2384c8c42864SChris Mason * helper function for btrfs_search_slot. The goal is to find a block 2385c8c42864SChris Mason * in cache without setting the path to blocking. If we find the block 2386c8c42864SChris Mason * we return zero and the path is unchanged. 2387c8c42864SChris Mason * 2388c8c42864SChris Mason * If we can't find the block, we set the path blocking and do some 2389c8c42864SChris Mason * reada. -EAGAIN is returned and the search must be repeated. 2390c8c42864SChris Mason */ 2391c8c42864SChris Mason static int 2392d07b8528SLiu Bo read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, 2393c8c42864SChris Mason struct extent_buffer **eb_ret, int level, int slot, 2394cda79c54SDavid Sterba const struct btrfs_key *key) 2395c8c42864SChris Mason { 23960b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2397c8c42864SChris Mason u64 blocknr; 2398c8c42864SChris Mason u64 gen; 2399c8c42864SChris Mason struct extent_buffer *b = *eb_ret; 2400c8c42864SChris Mason struct extent_buffer *tmp; 2401581c1760SQu Wenruo struct btrfs_key first_key; 240276a05b35SChris Mason int ret; 2403581c1760SQu Wenruo int parent_level; 2404c8c42864SChris Mason 2405c8c42864SChris Mason blocknr = btrfs_node_blockptr(b, slot); 2406c8c42864SChris Mason gen = btrfs_node_ptr_generation(b, slot); 2407581c1760SQu Wenruo parent_level = btrfs_header_level(b); 2408581c1760SQu Wenruo btrfs_node_key_to_cpu(b, &first_key, slot); 2409c8c42864SChris Mason 24100b246afaSJeff Mahoney tmp = find_extent_buffer(fs_info, blocknr); 2411cb44921aSChris Mason if (tmp) { 2412b9fab919SChris Mason /* first we do an atomic uptodate check */ 2413b9fab919SChris Mason if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { 2414448de471SQu Wenruo /* 2415448de471SQu Wenruo * Do extra check for first_key, eb can be stale due to 2416448de471SQu Wenruo * being cached, read from scrub, or have multiple 2417448de471SQu Wenruo * parents (shared tree blocks). 2418448de471SQu Wenruo */ 2419e064d5e9SDavid Sterba if (btrfs_verify_level_key(tmp, 2420448de471SQu Wenruo parent_level - 1, &first_key, gen)) { 2421448de471SQu Wenruo free_extent_buffer(tmp); 2422448de471SQu Wenruo return -EUCLEAN; 2423448de471SQu Wenruo } 2424c8c42864SChris Mason *eb_ret = tmp; 2425c8c42864SChris Mason return 0; 2426c8c42864SChris Mason } 2427bdf7c00eSJosef Bacik 2428cb44921aSChris Mason /* the pages were up to date, but we failed 2429cb44921aSChris Mason * the generation number check. Do a full 2430cb44921aSChris Mason * read for the generation number that is correct. 2431cb44921aSChris Mason * We must do this without dropping locks so 2432cb44921aSChris Mason * we can trust our generation number 2433cb44921aSChris Mason */ 2434bd681513SChris Mason btrfs_set_path_blocking(p); 2435bd681513SChris Mason 2436b9fab919SChris Mason /* now we're allowed to do a blocking uptodate check */ 2437581c1760SQu Wenruo ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key); 2438bdf7c00eSJosef Bacik if (!ret) { 2439cb44921aSChris Mason *eb_ret = tmp; 2440cb44921aSChris Mason return 0; 2441cb44921aSChris Mason } 2442cb44921aSChris Mason free_extent_buffer(tmp); 2443b3b4aa74SDavid Sterba btrfs_release_path(p); 2444cb44921aSChris Mason return -EIO; 2445cb44921aSChris Mason } 2446c8c42864SChris Mason 2447c8c42864SChris Mason /* 2448c8c42864SChris Mason * reduce lock contention at high levels 2449c8c42864SChris Mason * of the btree by dropping locks before 245076a05b35SChris Mason * we read. Don't release the lock on the current 245176a05b35SChris Mason * level because we need to walk this node to figure 245276a05b35SChris Mason * out which blocks to read. 2453c8c42864SChris Mason */ 24548c594ea8SChris Mason btrfs_unlock_up_safe(p, level + 1); 24558c594ea8SChris Mason btrfs_set_path_blocking(p); 24568c594ea8SChris Mason 2457e4058b54SDavid Sterba if (p->reada != READA_NONE) 24582ff7e61eSJeff Mahoney reada_for_search(fs_info, p, level, slot, key->objectid); 2459c8c42864SChris Mason 246076a05b35SChris Mason ret = -EAGAIN; 246102a3307aSLiu Bo tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1, 2462581c1760SQu Wenruo &first_key); 246364c043deSLiu Bo if (!IS_ERR(tmp)) { 246476a05b35SChris Mason /* 246576a05b35SChris Mason * If the read above didn't mark this buffer up to date, 246676a05b35SChris Mason * it will never end up being up to date. Set ret to EIO now 246776a05b35SChris Mason * and give up so that our caller doesn't loop forever 246876a05b35SChris Mason * on our EAGAINs. 246976a05b35SChris Mason */ 2470e6a1d6fdSLiu Bo if (!extent_buffer_uptodate(tmp)) 247176a05b35SChris Mason ret = -EIO; 2472c8c42864SChris Mason free_extent_buffer(tmp); 2473c871b0f2SLiu Bo } else { 2474c871b0f2SLiu Bo ret = PTR_ERR(tmp); 247576a05b35SChris Mason } 247602a3307aSLiu Bo 247702a3307aSLiu Bo btrfs_release_path(p); 247876a05b35SChris Mason return ret; 2479c8c42864SChris Mason } 2480c8c42864SChris Mason 2481c8c42864SChris Mason /* 2482c8c42864SChris Mason * helper function for btrfs_search_slot. This does all of the checks 2483c8c42864SChris Mason * for node-level blocks and does any balancing required based on 2484c8c42864SChris Mason * the ins_len. 2485c8c42864SChris Mason * 2486c8c42864SChris Mason * If no extra work was required, zero is returned. If we had to 2487c8c42864SChris Mason * drop the path, -EAGAIN is returned and btrfs_search_slot must 2488c8c42864SChris Mason * start over 2489c8c42864SChris Mason */ 2490c8c42864SChris Mason static int 2491c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans, 2492c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 2493bd681513SChris Mason struct extent_buffer *b, int level, int ins_len, 2494bd681513SChris Mason int *write_lock_level) 2495c8c42864SChris Mason { 24960b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2497c8c42864SChris Mason int ret; 24980b246afaSJeff Mahoney 2499c8c42864SChris Mason if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= 25000b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) { 2501c8c42864SChris Mason int sret; 2502c8c42864SChris Mason 2503bd681513SChris Mason if (*write_lock_level < level + 1) { 2504bd681513SChris Mason *write_lock_level = level + 1; 2505bd681513SChris Mason btrfs_release_path(p); 2506bd681513SChris Mason goto again; 2507bd681513SChris Mason } 2508bd681513SChris Mason 2509c8c42864SChris Mason btrfs_set_path_blocking(p); 25102ff7e61eSJeff Mahoney reada_for_balance(fs_info, p, level); 2511c8c42864SChris Mason sret = split_node(trans, root, p, level); 2512c8c42864SChris Mason 2513c8c42864SChris Mason BUG_ON(sret > 0); 2514c8c42864SChris Mason if (sret) { 2515c8c42864SChris Mason ret = sret; 2516c8c42864SChris Mason goto done; 2517c8c42864SChris Mason } 2518c8c42864SChris Mason b = p->nodes[level]; 2519c8c42864SChris Mason } else if (ins_len < 0 && btrfs_header_nritems(b) < 25200b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) { 2521c8c42864SChris Mason int sret; 2522c8c42864SChris Mason 2523bd681513SChris Mason if (*write_lock_level < level + 1) { 2524bd681513SChris Mason *write_lock_level = level + 1; 2525bd681513SChris Mason btrfs_release_path(p); 2526bd681513SChris Mason goto again; 2527bd681513SChris Mason } 2528bd681513SChris Mason 2529c8c42864SChris Mason btrfs_set_path_blocking(p); 25302ff7e61eSJeff Mahoney reada_for_balance(fs_info, p, level); 2531c8c42864SChris Mason sret = balance_level(trans, root, p, level); 2532c8c42864SChris Mason 2533c8c42864SChris Mason if (sret) { 2534c8c42864SChris Mason ret = sret; 2535c8c42864SChris Mason goto done; 2536c8c42864SChris Mason } 2537c8c42864SChris Mason b = p->nodes[level]; 2538c8c42864SChris Mason if (!b) { 2539b3b4aa74SDavid Sterba btrfs_release_path(p); 2540c8c42864SChris Mason goto again; 2541c8c42864SChris Mason } 2542c8c42864SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 2543c8c42864SChris Mason } 2544c8c42864SChris Mason return 0; 2545c8c42864SChris Mason 2546c8c42864SChris Mason again: 2547c8c42864SChris Mason ret = -EAGAIN; 2548c8c42864SChris Mason done: 2549c8c42864SChris Mason return ret; 2550c8c42864SChris Mason } 2551c8c42864SChris Mason 2552310712b2SOmar Sandoval static int key_search(struct extent_buffer *b, const struct btrfs_key *key, 2553d7396f07SFilipe David Borba Manana int level, int *prev_cmp, int *slot) 2554d7396f07SFilipe David Borba Manana { 2555d7396f07SFilipe David Borba Manana if (*prev_cmp != 0) { 2556a74b35ecSNikolay Borisov *prev_cmp = btrfs_bin_search(b, key, level, slot); 2557d7396f07SFilipe David Borba Manana return *prev_cmp; 2558d7396f07SFilipe David Borba Manana } 2559d7396f07SFilipe David Borba Manana 2560d7396f07SFilipe David Borba Manana *slot = 0; 2561d7396f07SFilipe David Borba Manana 2562d7396f07SFilipe David Borba Manana return 0; 2563d7396f07SFilipe David Borba Manana } 2564d7396f07SFilipe David Borba Manana 2565381cf658SDavid Sterba int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, 2566e33d5c3dSKelley Nielsen u64 iobjectid, u64 ioff, u8 key_type, 2567e33d5c3dSKelley Nielsen struct btrfs_key *found_key) 2568e33d5c3dSKelley Nielsen { 2569e33d5c3dSKelley Nielsen int ret; 2570e33d5c3dSKelley Nielsen struct btrfs_key key; 2571e33d5c3dSKelley Nielsen struct extent_buffer *eb; 2572381cf658SDavid Sterba 2573381cf658SDavid Sterba ASSERT(path); 25741d4c08e0SDavid Sterba ASSERT(found_key); 2575e33d5c3dSKelley Nielsen 2576e33d5c3dSKelley Nielsen key.type = key_type; 2577e33d5c3dSKelley Nielsen key.objectid = iobjectid; 2578e33d5c3dSKelley Nielsen key.offset = ioff; 2579e33d5c3dSKelley Nielsen 2580e33d5c3dSKelley Nielsen ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 25811d4c08e0SDavid Sterba if (ret < 0) 2582e33d5c3dSKelley Nielsen return ret; 2583e33d5c3dSKelley Nielsen 2584e33d5c3dSKelley Nielsen eb = path->nodes[0]; 2585e33d5c3dSKelley Nielsen if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { 2586e33d5c3dSKelley Nielsen ret = btrfs_next_leaf(fs_root, path); 2587e33d5c3dSKelley Nielsen if (ret) 2588e33d5c3dSKelley Nielsen return ret; 2589e33d5c3dSKelley Nielsen eb = path->nodes[0]; 2590e33d5c3dSKelley Nielsen } 2591e33d5c3dSKelley Nielsen 2592e33d5c3dSKelley Nielsen btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); 2593e33d5c3dSKelley Nielsen if (found_key->type != key.type || 2594e33d5c3dSKelley Nielsen found_key->objectid != key.objectid) 2595e33d5c3dSKelley Nielsen return 1; 2596e33d5c3dSKelley Nielsen 2597e33d5c3dSKelley Nielsen return 0; 2598e33d5c3dSKelley Nielsen } 2599e33d5c3dSKelley Nielsen 26001fc28d8eSLiu Bo static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root, 26011fc28d8eSLiu Bo struct btrfs_path *p, 26021fc28d8eSLiu Bo int write_lock_level) 26031fc28d8eSLiu Bo { 26041fc28d8eSLiu Bo struct btrfs_fs_info *fs_info = root->fs_info; 26051fc28d8eSLiu Bo struct extent_buffer *b; 26061fc28d8eSLiu Bo int root_lock; 26071fc28d8eSLiu Bo int level = 0; 26081fc28d8eSLiu Bo 26091fc28d8eSLiu Bo /* We try very hard to do read locks on the root */ 26101fc28d8eSLiu Bo root_lock = BTRFS_READ_LOCK; 26111fc28d8eSLiu Bo 26121fc28d8eSLiu Bo if (p->search_commit_root) { 2613be6821f8SFilipe Manana /* 2614be6821f8SFilipe Manana * The commit roots are read only so we always do read locks, 2615be6821f8SFilipe Manana * and we always must hold the commit_root_sem when doing 2616be6821f8SFilipe Manana * searches on them, the only exception is send where we don't 2617be6821f8SFilipe Manana * want to block transaction commits for a long time, so 2618be6821f8SFilipe Manana * we need to clone the commit root in order to avoid races 2619be6821f8SFilipe Manana * with transaction commits that create a snapshot of one of 2620be6821f8SFilipe Manana * the roots used by a send operation. 2621be6821f8SFilipe Manana */ 2622be6821f8SFilipe Manana if (p->need_commit_sem) { 26231fc28d8eSLiu Bo down_read(&fs_info->commit_root_sem); 2624be6821f8SFilipe Manana b = btrfs_clone_extent_buffer(root->commit_root); 2625be6821f8SFilipe Manana up_read(&fs_info->commit_root_sem); 2626be6821f8SFilipe Manana if (!b) 2627be6821f8SFilipe Manana return ERR_PTR(-ENOMEM); 2628be6821f8SFilipe Manana 2629be6821f8SFilipe Manana } else { 26301fc28d8eSLiu Bo b = root->commit_root; 26311fc28d8eSLiu Bo extent_buffer_get(b); 2632be6821f8SFilipe Manana } 26331fc28d8eSLiu Bo level = btrfs_header_level(b); 2634f9ddfd05SLiu Bo /* 2635f9ddfd05SLiu Bo * Ensure that all callers have set skip_locking when 2636f9ddfd05SLiu Bo * p->search_commit_root = 1. 2637f9ddfd05SLiu Bo */ 2638f9ddfd05SLiu Bo ASSERT(p->skip_locking == 1); 26391fc28d8eSLiu Bo 26401fc28d8eSLiu Bo goto out; 26411fc28d8eSLiu Bo } 26421fc28d8eSLiu Bo 26431fc28d8eSLiu Bo if (p->skip_locking) { 26441fc28d8eSLiu Bo b = btrfs_root_node(root); 26451fc28d8eSLiu Bo level = btrfs_header_level(b); 26461fc28d8eSLiu Bo goto out; 26471fc28d8eSLiu Bo } 26481fc28d8eSLiu Bo 26491fc28d8eSLiu Bo /* 2650662c653bSLiu Bo * If the level is set to maximum, we can skip trying to get the read 2651662c653bSLiu Bo * lock. 2652662c653bSLiu Bo */ 2653662c653bSLiu Bo if (write_lock_level < BTRFS_MAX_LEVEL) { 2654662c653bSLiu Bo /* 2655662c653bSLiu Bo * We don't know the level of the root node until we actually 2656662c653bSLiu Bo * have it read locked 26571fc28d8eSLiu Bo */ 26581fc28d8eSLiu Bo b = btrfs_read_lock_root_node(root); 26591fc28d8eSLiu Bo level = btrfs_header_level(b); 26601fc28d8eSLiu Bo if (level > write_lock_level) 26611fc28d8eSLiu Bo goto out; 26621fc28d8eSLiu Bo 2663662c653bSLiu Bo /* Whoops, must trade for write lock */ 26641fc28d8eSLiu Bo btrfs_tree_read_unlock(b); 26651fc28d8eSLiu Bo free_extent_buffer(b); 2666662c653bSLiu Bo } 2667662c653bSLiu Bo 26681fc28d8eSLiu Bo b = btrfs_lock_root_node(root); 26691fc28d8eSLiu Bo root_lock = BTRFS_WRITE_LOCK; 26701fc28d8eSLiu Bo 26711fc28d8eSLiu Bo /* The level might have changed, check again */ 26721fc28d8eSLiu Bo level = btrfs_header_level(b); 26731fc28d8eSLiu Bo 26741fc28d8eSLiu Bo out: 26751fc28d8eSLiu Bo p->nodes[level] = b; 26761fc28d8eSLiu Bo if (!p->skip_locking) 26771fc28d8eSLiu Bo p->locks[level] = root_lock; 26781fc28d8eSLiu Bo /* 26791fc28d8eSLiu Bo * Callers are responsible for dropping b's references. 26801fc28d8eSLiu Bo */ 26811fc28d8eSLiu Bo return b; 26821fc28d8eSLiu Bo } 26831fc28d8eSLiu Bo 26841fc28d8eSLiu Bo 2685c8c42864SChris Mason /* 26864271eceaSNikolay Borisov * btrfs_search_slot - look for a key in a tree and perform necessary 26874271eceaSNikolay Borisov * modifications to preserve tree invariants. 268874123bd7SChris Mason * 26894271eceaSNikolay Borisov * @trans: Handle of transaction, used when modifying the tree 26904271eceaSNikolay Borisov * @p: Holds all btree nodes along the search path 26914271eceaSNikolay Borisov * @root: The root node of the tree 26924271eceaSNikolay Borisov * @key: The key we are looking for 26934271eceaSNikolay Borisov * @ins_len: Indicates purpose of search, for inserts it is 1, for 26944271eceaSNikolay Borisov * deletions it's -1. 0 for plain searches 26954271eceaSNikolay Borisov * @cow: boolean should CoW operations be performed. Must always be 1 26964271eceaSNikolay Borisov * when modifying the tree. 269797571fd0SChris Mason * 26984271eceaSNikolay Borisov * If @ins_len > 0, nodes and leaves will be split as we walk down the tree. 26994271eceaSNikolay Borisov * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible) 27004271eceaSNikolay Borisov * 27014271eceaSNikolay Borisov * If @key is found, 0 is returned and you can find the item in the leaf level 27024271eceaSNikolay Borisov * of the path (level 0) 27034271eceaSNikolay Borisov * 27044271eceaSNikolay Borisov * If @key isn't found, 1 is returned and the leaf level of the path (level 0) 27054271eceaSNikolay Borisov * points to the slot where it should be inserted 27064271eceaSNikolay Borisov * 27074271eceaSNikolay Borisov * If an error is encountered while searching the tree a negative error number 27084271eceaSNikolay Borisov * is returned 270974123bd7SChris Mason */ 2710310712b2SOmar Sandoval int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2711310712b2SOmar Sandoval const struct btrfs_key *key, struct btrfs_path *p, 2712310712b2SOmar Sandoval int ins_len, int cow) 2713be0e5c09SChris Mason { 27145f39d397SChris Mason struct extent_buffer *b; 2715be0e5c09SChris Mason int slot; 2716be0e5c09SChris Mason int ret; 271733c66f43SYan Zheng int err; 2718be0e5c09SChris Mason int level; 2719925baeddSChris Mason int lowest_unlock = 1; 2720bd681513SChris Mason /* everything at write_lock_level or lower must be write locked */ 2721bd681513SChris Mason int write_lock_level = 0; 27229f3a7427SChris Mason u8 lowest_level = 0; 2723f7c79f30SChris Mason int min_write_lock_level; 2724d7396f07SFilipe David Borba Manana int prev_cmp; 27259f3a7427SChris Mason 27266702ed49SChris Mason lowest_level = p->lowest_level; 2727323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 272822b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 2729eb653de1SFilipe David Borba Manana BUG_ON(!cow && ins_len); 273025179201SJosef Bacik 2731bd681513SChris Mason if (ins_len < 0) { 2732925baeddSChris Mason lowest_unlock = 2; 273365b51a00SChris Mason 2734bd681513SChris Mason /* when we are removing items, we might have to go up to level 2735bd681513SChris Mason * two as we update tree pointers Make sure we keep write 2736bd681513SChris Mason * for those levels as well 2737bd681513SChris Mason */ 2738bd681513SChris Mason write_lock_level = 2; 2739bd681513SChris Mason } else if (ins_len > 0) { 2740bd681513SChris Mason /* 2741bd681513SChris Mason * for inserting items, make sure we have a write lock on 2742bd681513SChris Mason * level 1 so we can update keys 2743bd681513SChris Mason */ 2744bd681513SChris Mason write_lock_level = 1; 2745bd681513SChris Mason } 2746bd681513SChris Mason 2747bd681513SChris Mason if (!cow) 2748bd681513SChris Mason write_lock_level = -1; 2749bd681513SChris Mason 275009a2a8f9SJosef Bacik if (cow && (p->keep_locks || p->lowest_level)) 2751bd681513SChris Mason write_lock_level = BTRFS_MAX_LEVEL; 2752bd681513SChris Mason 2753f7c79f30SChris Mason min_write_lock_level = write_lock_level; 2754f7c79f30SChris Mason 2755bb803951SChris Mason again: 2756d7396f07SFilipe David Borba Manana prev_cmp = -1; 27571fc28d8eSLiu Bo b = btrfs_search_slot_get_root(root, p, write_lock_level); 2758be6821f8SFilipe Manana if (IS_ERR(b)) { 2759be6821f8SFilipe Manana ret = PTR_ERR(b); 2760be6821f8SFilipe Manana goto done; 2761be6821f8SFilipe Manana } 2762925baeddSChris Mason 2763eb60ceacSChris Mason while (b) { 27645f39d397SChris Mason level = btrfs_header_level(b); 276565b51a00SChris Mason 276665b51a00SChris Mason /* 276765b51a00SChris Mason * setup the path here so we can release it under lock 276865b51a00SChris Mason * contention with the cow code 276965b51a00SChris Mason */ 277002217ed2SChris Mason if (cow) { 27719ea2c7c9SNikolay Borisov bool last_level = (level == (BTRFS_MAX_LEVEL - 1)); 27729ea2c7c9SNikolay Borisov 2773c8c42864SChris Mason /* 2774c8c42864SChris Mason * if we don't really need to cow this block 2775c8c42864SChris Mason * then we don't want to set the path blocking, 2776c8c42864SChris Mason * so we test it here 2777c8c42864SChris Mason */ 277864c12921SJeff Mahoney if (!should_cow_block(trans, root, b)) { 277964c12921SJeff Mahoney trans->dirty = true; 278065b51a00SChris Mason goto cow_done; 278164c12921SJeff Mahoney } 27825d4f98a2SYan Zheng 2783bd681513SChris Mason /* 2784bd681513SChris Mason * must have write locks on this node and the 2785bd681513SChris Mason * parent 2786bd681513SChris Mason */ 27875124e00eSJosef Bacik if (level > write_lock_level || 27885124e00eSJosef Bacik (level + 1 > write_lock_level && 27895124e00eSJosef Bacik level + 1 < BTRFS_MAX_LEVEL && 27905124e00eSJosef Bacik p->nodes[level + 1])) { 2791bd681513SChris Mason write_lock_level = level + 1; 2792bd681513SChris Mason btrfs_release_path(p); 2793bd681513SChris Mason goto again; 2794bd681513SChris Mason } 2795bd681513SChris Mason 2796160f4089SFilipe Manana btrfs_set_path_blocking(p); 27979ea2c7c9SNikolay Borisov if (last_level) 27989ea2c7c9SNikolay Borisov err = btrfs_cow_block(trans, root, b, NULL, 0, 27999ea2c7c9SNikolay Borisov &b); 28009ea2c7c9SNikolay Borisov else 280133c66f43SYan Zheng err = btrfs_cow_block(trans, root, b, 2802e20d96d6SChris Mason p->nodes[level + 1], 28039fa8cfe7SChris Mason p->slots[level + 1], &b); 280433c66f43SYan Zheng if (err) { 280533c66f43SYan Zheng ret = err; 280665b51a00SChris Mason goto done; 280754aa1f4dSChris Mason } 280802217ed2SChris Mason } 280965b51a00SChris Mason cow_done: 2810eb60ceacSChris Mason p->nodes[level] = b; 281152398340SLiu Bo /* 281252398340SLiu Bo * Leave path with blocking locks to avoid massive 281352398340SLiu Bo * lock context switch, this is made on purpose. 281452398340SLiu Bo */ 2815b4ce94deSChris Mason 2816b4ce94deSChris Mason /* 2817b4ce94deSChris Mason * we have a lock on b and as long as we aren't changing 2818b4ce94deSChris Mason * the tree, there is no way to for the items in b to change. 2819b4ce94deSChris Mason * It is safe to drop the lock on our parent before we 2820b4ce94deSChris Mason * go through the expensive btree search on b. 2821b4ce94deSChris Mason * 2822eb653de1SFilipe David Borba Manana * If we're inserting or deleting (ins_len != 0), then we might 2823eb653de1SFilipe David Borba Manana * be changing slot zero, which may require changing the parent. 2824eb653de1SFilipe David Borba Manana * So, we can't drop the lock until after we know which slot 2825eb653de1SFilipe David Borba Manana * we're operating on. 2826b4ce94deSChris Mason */ 2827eb653de1SFilipe David Borba Manana if (!ins_len && !p->keep_locks) { 2828eb653de1SFilipe David Borba Manana int u = level + 1; 2829eb653de1SFilipe David Borba Manana 2830eb653de1SFilipe David Borba Manana if (u < BTRFS_MAX_LEVEL && p->locks[u]) { 2831eb653de1SFilipe David Borba Manana btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]); 2832eb653de1SFilipe David Borba Manana p->locks[u] = 0; 2833eb653de1SFilipe David Borba Manana } 2834eb653de1SFilipe David Borba Manana } 2835b4ce94deSChris Mason 2836d7396f07SFilipe David Borba Manana ret = key_search(b, key, level, &prev_cmp, &slot); 2837415b35a5SLiu Bo if (ret < 0) 2838415b35a5SLiu Bo goto done; 2839b4ce94deSChris Mason 28405f39d397SChris Mason if (level != 0) { 284133c66f43SYan Zheng int dec = 0; 284233c66f43SYan Zheng if (ret && slot > 0) { 284333c66f43SYan Zheng dec = 1; 2844be0e5c09SChris Mason slot -= 1; 284533c66f43SYan Zheng } 2846be0e5c09SChris Mason p->slots[level] = slot; 284733c66f43SYan Zheng err = setup_nodes_for_search(trans, root, p, b, level, 2848bd681513SChris Mason ins_len, &write_lock_level); 284933c66f43SYan Zheng if (err == -EAGAIN) 2850b4ce94deSChris Mason goto again; 285133c66f43SYan Zheng if (err) { 285233c66f43SYan Zheng ret = err; 285365b51a00SChris Mason goto done; 285433c66f43SYan Zheng } 28555c680ed6SChris Mason b = p->nodes[level]; 28565c680ed6SChris Mason slot = p->slots[level]; 2857b4ce94deSChris Mason 2858bd681513SChris Mason /* 2859bd681513SChris Mason * slot 0 is special, if we change the key 2860bd681513SChris Mason * we have to update the parent pointer 2861bd681513SChris Mason * which means we must have a write lock 2862bd681513SChris Mason * on the parent 2863bd681513SChris Mason */ 2864eb653de1SFilipe David Borba Manana if (slot == 0 && ins_len && 2865bd681513SChris Mason write_lock_level < level + 1) { 2866bd681513SChris Mason write_lock_level = level + 1; 2867bd681513SChris Mason btrfs_release_path(p); 2868bd681513SChris Mason goto again; 2869bd681513SChris Mason } 2870bd681513SChris Mason 2871f7c79f30SChris Mason unlock_up(p, level, lowest_unlock, 2872f7c79f30SChris Mason min_write_lock_level, &write_lock_level); 2873f9efa9c7SChris Mason 2874925baeddSChris Mason if (level == lowest_level) { 287533c66f43SYan Zheng if (dec) 287633c66f43SYan Zheng p->slots[level]++; 28775b21f2edSZheng Yan goto done; 2878925baeddSChris Mason } 2879ca7a79adSChris Mason 2880d07b8528SLiu Bo err = read_block_for_search(root, p, &b, level, 2881cda79c54SDavid Sterba slot, key); 288233c66f43SYan Zheng if (err == -EAGAIN) 2883051e1b9fSChris Mason goto again; 288433c66f43SYan Zheng if (err) { 288533c66f43SYan Zheng ret = err; 288676a05b35SChris Mason goto done; 288733c66f43SYan Zheng } 288876a05b35SChris Mason 2889b4ce94deSChris Mason if (!p->skip_locking) { 2890bd681513SChris Mason level = btrfs_header_level(b); 2891bd681513SChris Mason if (level <= write_lock_level) { 2892bd681513SChris Mason err = btrfs_try_tree_write_lock(b); 289333c66f43SYan Zheng if (!err) { 2894b4ce94deSChris Mason btrfs_set_path_blocking(p); 2895925baeddSChris Mason btrfs_tree_lock(b); 2896b4ce94deSChris Mason } 2897bd681513SChris Mason p->locks[level] = BTRFS_WRITE_LOCK; 2898bd681513SChris Mason } else { 2899f82c458aSChris Mason err = btrfs_tree_read_lock_atomic(b); 2900bd681513SChris Mason if (!err) { 2901bd681513SChris Mason btrfs_set_path_blocking(p); 2902bd681513SChris Mason btrfs_tree_read_lock(b); 2903bd681513SChris Mason } 2904bd681513SChris Mason p->locks[level] = BTRFS_READ_LOCK; 2905bd681513SChris Mason } 2906bd681513SChris Mason p->nodes[level] = b; 2907b4ce94deSChris Mason } 2908be0e5c09SChris Mason } else { 2909be0e5c09SChris Mason p->slots[level] = slot; 291087b29b20SYan Zheng if (ins_len > 0 && 2911e902baacSDavid Sterba btrfs_leaf_free_space(b) < ins_len) { 2912bd681513SChris Mason if (write_lock_level < 1) { 2913bd681513SChris Mason write_lock_level = 1; 2914bd681513SChris Mason btrfs_release_path(p); 2915bd681513SChris Mason goto again; 2916bd681513SChris Mason } 2917bd681513SChris Mason 2918b4ce94deSChris Mason btrfs_set_path_blocking(p); 291933c66f43SYan Zheng err = split_leaf(trans, root, key, 2920cc0c5538SChris Mason p, ins_len, ret == 0); 2921b4ce94deSChris Mason 292233c66f43SYan Zheng BUG_ON(err > 0); 292333c66f43SYan Zheng if (err) { 292433c66f43SYan Zheng ret = err; 292565b51a00SChris Mason goto done; 292665b51a00SChris Mason } 29275c680ed6SChris Mason } 2928459931ecSChris Mason if (!p->search_for_split) 2929f7c79f30SChris Mason unlock_up(p, level, lowest_unlock, 29304b6f8e96SLiu Bo min_write_lock_level, NULL); 293165b51a00SChris Mason goto done; 293265b51a00SChris Mason } 293365b51a00SChris Mason } 293465b51a00SChris Mason ret = 1; 293565b51a00SChris Mason done: 2936b4ce94deSChris Mason /* 2937b4ce94deSChris Mason * we don't really know what they plan on doing with the path 2938b4ce94deSChris Mason * from here on, so for now just mark it as blocking 2939b4ce94deSChris Mason */ 2940b9473439SChris Mason if (!p->leave_spinning) 2941b4ce94deSChris Mason btrfs_set_path_blocking(p); 29425f5bc6b1SFilipe Manana if (ret < 0 && !p->skip_release_on_error) 2943b3b4aa74SDavid Sterba btrfs_release_path(p); 2944be0e5c09SChris Mason return ret; 2945be0e5c09SChris Mason } 2946be0e5c09SChris Mason 294774123bd7SChris Mason /* 29485d9e75c4SJan Schmidt * Like btrfs_search_slot, this looks for a key in the given tree. It uses the 29495d9e75c4SJan Schmidt * current state of the tree together with the operations recorded in the tree 29505d9e75c4SJan Schmidt * modification log to search for the key in a previous version of this tree, as 29515d9e75c4SJan Schmidt * denoted by the time_seq parameter. 29525d9e75c4SJan Schmidt * 29535d9e75c4SJan Schmidt * Naturally, there is no support for insert, delete or cow operations. 29545d9e75c4SJan Schmidt * 29555d9e75c4SJan Schmidt * The resulting path and return value will be set up as if we called 29565d9e75c4SJan Schmidt * btrfs_search_slot at that point in time with ins_len and cow both set to 0. 29575d9e75c4SJan Schmidt */ 2958310712b2SOmar Sandoval int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key, 29595d9e75c4SJan Schmidt struct btrfs_path *p, u64 time_seq) 29605d9e75c4SJan Schmidt { 29610b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 29625d9e75c4SJan Schmidt struct extent_buffer *b; 29635d9e75c4SJan Schmidt int slot; 29645d9e75c4SJan Schmidt int ret; 29655d9e75c4SJan Schmidt int err; 29665d9e75c4SJan Schmidt int level; 29675d9e75c4SJan Schmidt int lowest_unlock = 1; 29685d9e75c4SJan Schmidt u8 lowest_level = 0; 2969d4b4087cSJosef Bacik int prev_cmp = -1; 29705d9e75c4SJan Schmidt 29715d9e75c4SJan Schmidt lowest_level = p->lowest_level; 29725d9e75c4SJan Schmidt WARN_ON(p->nodes[0] != NULL); 29735d9e75c4SJan Schmidt 29745d9e75c4SJan Schmidt if (p->search_commit_root) { 29755d9e75c4SJan Schmidt BUG_ON(time_seq); 29765d9e75c4SJan Schmidt return btrfs_search_slot(NULL, root, key, p, 0, 0); 29775d9e75c4SJan Schmidt } 29785d9e75c4SJan Schmidt 29795d9e75c4SJan Schmidt again: 29805d9e75c4SJan Schmidt b = get_old_root(root, time_seq); 2981315bed43SNikolay Borisov if (!b) { 2982315bed43SNikolay Borisov ret = -EIO; 2983315bed43SNikolay Borisov goto done; 2984315bed43SNikolay Borisov } 29855d9e75c4SJan Schmidt level = btrfs_header_level(b); 29865d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 29875d9e75c4SJan Schmidt 29885d9e75c4SJan Schmidt while (b) { 29895d9e75c4SJan Schmidt level = btrfs_header_level(b); 29905d9e75c4SJan Schmidt p->nodes[level] = b; 29915d9e75c4SJan Schmidt 29925d9e75c4SJan Schmidt /* 29935d9e75c4SJan Schmidt * we have a lock on b and as long as we aren't changing 29945d9e75c4SJan Schmidt * the tree, there is no way to for the items in b to change. 29955d9e75c4SJan Schmidt * It is safe to drop the lock on our parent before we 29965d9e75c4SJan Schmidt * go through the expensive btree search on b. 29975d9e75c4SJan Schmidt */ 29985d9e75c4SJan Schmidt btrfs_unlock_up_safe(p, level + 1); 29995d9e75c4SJan Schmidt 3000d4b4087cSJosef Bacik /* 300101327610SNicholas D Steeves * Since we can unwind ebs we want to do a real search every 3002d4b4087cSJosef Bacik * time. 3003d4b4087cSJosef Bacik */ 3004d4b4087cSJosef Bacik prev_cmp = -1; 3005d7396f07SFilipe David Borba Manana ret = key_search(b, key, level, &prev_cmp, &slot); 3006cbca7d59SFilipe Manana if (ret < 0) 3007cbca7d59SFilipe Manana goto done; 30085d9e75c4SJan Schmidt 30095d9e75c4SJan Schmidt if (level != 0) { 30105d9e75c4SJan Schmidt int dec = 0; 30115d9e75c4SJan Schmidt if (ret && slot > 0) { 30125d9e75c4SJan Schmidt dec = 1; 30135d9e75c4SJan Schmidt slot -= 1; 30145d9e75c4SJan Schmidt } 30155d9e75c4SJan Schmidt p->slots[level] = slot; 30165d9e75c4SJan Schmidt unlock_up(p, level, lowest_unlock, 0, NULL); 30175d9e75c4SJan Schmidt 30185d9e75c4SJan Schmidt if (level == lowest_level) { 30195d9e75c4SJan Schmidt if (dec) 30205d9e75c4SJan Schmidt p->slots[level]++; 30215d9e75c4SJan Schmidt goto done; 30225d9e75c4SJan Schmidt } 30235d9e75c4SJan Schmidt 3024d07b8528SLiu Bo err = read_block_for_search(root, p, &b, level, 3025cda79c54SDavid Sterba slot, key); 30265d9e75c4SJan Schmidt if (err == -EAGAIN) 30275d9e75c4SJan Schmidt goto again; 30285d9e75c4SJan Schmidt if (err) { 30295d9e75c4SJan Schmidt ret = err; 30305d9e75c4SJan Schmidt goto done; 30315d9e75c4SJan Schmidt } 30325d9e75c4SJan Schmidt 30335d9e75c4SJan Schmidt level = btrfs_header_level(b); 3034f82c458aSChris Mason err = btrfs_tree_read_lock_atomic(b); 30355d9e75c4SJan Schmidt if (!err) { 30365d9e75c4SJan Schmidt btrfs_set_path_blocking(p); 30375d9e75c4SJan Schmidt btrfs_tree_read_lock(b); 30385d9e75c4SJan Schmidt } 30390b246afaSJeff Mahoney b = tree_mod_log_rewind(fs_info, p, b, time_seq); 3040db7f3436SJosef Bacik if (!b) { 3041db7f3436SJosef Bacik ret = -ENOMEM; 3042db7f3436SJosef Bacik goto done; 3043db7f3436SJosef Bacik } 30445d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 30455d9e75c4SJan Schmidt p->nodes[level] = b; 30465d9e75c4SJan Schmidt } else { 30475d9e75c4SJan Schmidt p->slots[level] = slot; 30485d9e75c4SJan Schmidt unlock_up(p, level, lowest_unlock, 0, NULL); 30495d9e75c4SJan Schmidt goto done; 30505d9e75c4SJan Schmidt } 30515d9e75c4SJan Schmidt } 30525d9e75c4SJan Schmidt ret = 1; 30535d9e75c4SJan Schmidt done: 30545d9e75c4SJan Schmidt if (!p->leave_spinning) 30555d9e75c4SJan Schmidt btrfs_set_path_blocking(p); 30565d9e75c4SJan Schmidt if (ret < 0) 30575d9e75c4SJan Schmidt btrfs_release_path(p); 30585d9e75c4SJan Schmidt 30595d9e75c4SJan Schmidt return ret; 30605d9e75c4SJan Schmidt } 30615d9e75c4SJan Schmidt 30625d9e75c4SJan Schmidt /* 30632f38b3e1SArne Jansen * helper to use instead of search slot if no exact match is needed but 30642f38b3e1SArne Jansen * instead the next or previous item should be returned. 30652f38b3e1SArne Jansen * When find_higher is true, the next higher item is returned, the next lower 30662f38b3e1SArne Jansen * otherwise. 30672f38b3e1SArne Jansen * When return_any and find_higher are both true, and no higher item is found, 30682f38b3e1SArne Jansen * return the next lower instead. 30692f38b3e1SArne Jansen * When return_any is true and find_higher is false, and no lower item is found, 30702f38b3e1SArne Jansen * return the next higher instead. 30712f38b3e1SArne Jansen * It returns 0 if any item is found, 1 if none is found (tree empty), and 30722f38b3e1SArne Jansen * < 0 on error 30732f38b3e1SArne Jansen */ 30742f38b3e1SArne Jansen int btrfs_search_slot_for_read(struct btrfs_root *root, 3075310712b2SOmar Sandoval const struct btrfs_key *key, 3076310712b2SOmar Sandoval struct btrfs_path *p, int find_higher, 3077310712b2SOmar Sandoval int return_any) 30782f38b3e1SArne Jansen { 30792f38b3e1SArne Jansen int ret; 30802f38b3e1SArne Jansen struct extent_buffer *leaf; 30812f38b3e1SArne Jansen 30822f38b3e1SArne Jansen again: 30832f38b3e1SArne Jansen ret = btrfs_search_slot(NULL, root, key, p, 0, 0); 30842f38b3e1SArne Jansen if (ret <= 0) 30852f38b3e1SArne Jansen return ret; 30862f38b3e1SArne Jansen /* 30872f38b3e1SArne Jansen * a return value of 1 means the path is at the position where the 30882f38b3e1SArne Jansen * item should be inserted. Normally this is the next bigger item, 30892f38b3e1SArne Jansen * but in case the previous item is the last in a leaf, path points 30902f38b3e1SArne Jansen * to the first free slot in the previous leaf, i.e. at an invalid 30912f38b3e1SArne Jansen * item. 30922f38b3e1SArne Jansen */ 30932f38b3e1SArne Jansen leaf = p->nodes[0]; 30942f38b3e1SArne Jansen 30952f38b3e1SArne Jansen if (find_higher) { 30962f38b3e1SArne Jansen if (p->slots[0] >= btrfs_header_nritems(leaf)) { 30972f38b3e1SArne Jansen ret = btrfs_next_leaf(root, p); 30982f38b3e1SArne Jansen if (ret <= 0) 30992f38b3e1SArne Jansen return ret; 31002f38b3e1SArne Jansen if (!return_any) 31012f38b3e1SArne Jansen return 1; 31022f38b3e1SArne Jansen /* 31032f38b3e1SArne Jansen * no higher item found, return the next 31042f38b3e1SArne Jansen * lower instead 31052f38b3e1SArne Jansen */ 31062f38b3e1SArne Jansen return_any = 0; 31072f38b3e1SArne Jansen find_higher = 0; 31082f38b3e1SArne Jansen btrfs_release_path(p); 31092f38b3e1SArne Jansen goto again; 31102f38b3e1SArne Jansen } 31112f38b3e1SArne Jansen } else { 31122f38b3e1SArne Jansen if (p->slots[0] == 0) { 31132f38b3e1SArne Jansen ret = btrfs_prev_leaf(root, p); 3114e6793769SArne Jansen if (ret < 0) 31152f38b3e1SArne Jansen return ret; 3116e6793769SArne Jansen if (!ret) { 311723c6bf6aSFilipe David Borba Manana leaf = p->nodes[0]; 311823c6bf6aSFilipe David Borba Manana if (p->slots[0] == btrfs_header_nritems(leaf)) 311923c6bf6aSFilipe David Borba Manana p->slots[0]--; 3120e6793769SArne Jansen return 0; 3121e6793769SArne Jansen } 31222f38b3e1SArne Jansen if (!return_any) 31232f38b3e1SArne Jansen return 1; 31242f38b3e1SArne Jansen /* 31252f38b3e1SArne Jansen * no lower item found, return the next 31262f38b3e1SArne Jansen * higher instead 31272f38b3e1SArne Jansen */ 31282f38b3e1SArne Jansen return_any = 0; 31292f38b3e1SArne Jansen find_higher = 1; 31302f38b3e1SArne Jansen btrfs_release_path(p); 31312f38b3e1SArne Jansen goto again; 3132e6793769SArne Jansen } else { 31332f38b3e1SArne Jansen --p->slots[0]; 31342f38b3e1SArne Jansen } 31352f38b3e1SArne Jansen } 31362f38b3e1SArne Jansen return 0; 31372f38b3e1SArne Jansen } 31382f38b3e1SArne Jansen 31392f38b3e1SArne Jansen /* 314074123bd7SChris Mason * adjust the pointers going up the tree, starting at level 314174123bd7SChris Mason * making sure the right key of each node is points to 'key'. 314274123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 314374123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 314474123bd7SChris Mason * higher levels 3145aa5d6bedSChris Mason * 314674123bd7SChris Mason */ 3147b167fa91SNikolay Borisov static void fixup_low_keys(struct btrfs_path *path, 31485f39d397SChris Mason struct btrfs_disk_key *key, int level) 3149be0e5c09SChris Mason { 3150be0e5c09SChris Mason int i; 31515f39d397SChris Mason struct extent_buffer *t; 31520e82bcfeSDavid Sterba int ret; 31535f39d397SChris Mason 3154234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 3155be0e5c09SChris Mason int tslot = path->slots[i]; 31560e82bcfeSDavid Sterba 3157eb60ceacSChris Mason if (!path->nodes[i]) 3158be0e5c09SChris Mason break; 31595f39d397SChris Mason t = path->nodes[i]; 31600e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE, 31610e82bcfeSDavid Sterba GFP_ATOMIC); 31620e82bcfeSDavid Sterba BUG_ON(ret < 0); 31635f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 3164d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 3165be0e5c09SChris Mason if (tslot != 0) 3166be0e5c09SChris Mason break; 3167be0e5c09SChris Mason } 3168be0e5c09SChris Mason } 3169be0e5c09SChris Mason 317074123bd7SChris Mason /* 317131840ae1SZheng Yan * update item key. 317231840ae1SZheng Yan * 317331840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 317431840ae1SZheng Yan * that the new key won't break the order 317531840ae1SZheng Yan */ 3176b7a0365eSDaniel Dressler void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, 3177b7a0365eSDaniel Dressler struct btrfs_path *path, 3178310712b2SOmar Sandoval const struct btrfs_key *new_key) 317931840ae1SZheng Yan { 318031840ae1SZheng Yan struct btrfs_disk_key disk_key; 318131840ae1SZheng Yan struct extent_buffer *eb; 318231840ae1SZheng Yan int slot; 318331840ae1SZheng Yan 318431840ae1SZheng Yan eb = path->nodes[0]; 318531840ae1SZheng Yan slot = path->slots[0]; 318631840ae1SZheng Yan if (slot > 0) { 318731840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 31887c15d410SQu Wenruo if (unlikely(comp_keys(&disk_key, new_key) >= 0)) { 31897c15d410SQu Wenruo btrfs_crit(fs_info, 31907c15d410SQu Wenruo "slot %u key (%llu %u %llu) new key (%llu %u %llu)", 31917c15d410SQu Wenruo slot, btrfs_disk_key_objectid(&disk_key), 31927c15d410SQu Wenruo btrfs_disk_key_type(&disk_key), 31937c15d410SQu Wenruo btrfs_disk_key_offset(&disk_key), 31947c15d410SQu Wenruo new_key->objectid, new_key->type, 31957c15d410SQu Wenruo new_key->offset); 31967c15d410SQu Wenruo btrfs_print_leaf(eb); 31977c15d410SQu Wenruo BUG(); 31987c15d410SQu Wenruo } 319931840ae1SZheng Yan } 320031840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 320131840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 32027c15d410SQu Wenruo if (unlikely(comp_keys(&disk_key, new_key) <= 0)) { 32037c15d410SQu Wenruo btrfs_crit(fs_info, 32047c15d410SQu Wenruo "slot %u key (%llu %u %llu) new key (%llu %u %llu)", 32057c15d410SQu Wenruo slot, btrfs_disk_key_objectid(&disk_key), 32067c15d410SQu Wenruo btrfs_disk_key_type(&disk_key), 32077c15d410SQu Wenruo btrfs_disk_key_offset(&disk_key), 32087c15d410SQu Wenruo new_key->objectid, new_key->type, 32097c15d410SQu Wenruo new_key->offset); 32107c15d410SQu Wenruo btrfs_print_leaf(eb); 32117c15d410SQu Wenruo BUG(); 32127c15d410SQu Wenruo } 321331840ae1SZheng Yan } 321431840ae1SZheng Yan 321531840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 321631840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 321731840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 321831840ae1SZheng Yan if (slot == 0) 3219b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 322031840ae1SZheng Yan } 322131840ae1SZheng Yan 322231840ae1SZheng Yan /* 322374123bd7SChris Mason * try to push data from one node into the next node left in the 322479f95c82SChris Mason * tree. 3225aa5d6bedSChris Mason * 3226aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 3227aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 322874123bd7SChris Mason */ 322998ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 32302ff7e61eSJeff Mahoney struct extent_buffer *dst, 3231971a1f66SChris Mason struct extent_buffer *src, int empty) 3232be0e5c09SChris Mason { 3233d30a668fSDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 3234be0e5c09SChris Mason int push_items = 0; 3235bb803951SChris Mason int src_nritems; 3236bb803951SChris Mason int dst_nritems; 3237aa5d6bedSChris Mason int ret = 0; 3238be0e5c09SChris Mason 32395f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 32405f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 32410b246afaSJeff Mahoney push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; 32427bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 32437bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 324454aa1f4dSChris Mason 3245bce4eae9SChris Mason if (!empty && src_nritems <= 8) 3246971a1f66SChris Mason return 1; 3247971a1f66SChris Mason 3248d397712bSChris Mason if (push_items <= 0) 3249be0e5c09SChris Mason return 1; 3250be0e5c09SChris Mason 3251bce4eae9SChris Mason if (empty) { 3252971a1f66SChris Mason push_items = min(src_nritems, push_items); 3253bce4eae9SChris Mason if (push_items < src_nritems) { 3254bce4eae9SChris Mason /* leave at least 8 pointers in the node if 3255bce4eae9SChris Mason * we aren't going to empty it 3256bce4eae9SChris Mason */ 3257bce4eae9SChris Mason if (src_nritems - push_items < 8) { 3258bce4eae9SChris Mason if (push_items <= 8) 3259bce4eae9SChris Mason return 1; 3260bce4eae9SChris Mason push_items -= 8; 3261bce4eae9SChris Mason } 3262bce4eae9SChris Mason } 3263bce4eae9SChris Mason } else 3264bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 326579f95c82SChris Mason 3266ed874f0dSDavid Sterba ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items); 32675de865eeSFilipe David Borba Manana if (ret) { 326866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 32695de865eeSFilipe David Borba Manana return ret; 32705de865eeSFilipe David Borba Manana } 32715f39d397SChris Mason copy_extent_buffer(dst, src, 32725f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 32735f39d397SChris Mason btrfs_node_key_ptr_offset(0), 3274123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 32755f39d397SChris Mason 3276bb803951SChris Mason if (push_items < src_nritems) { 327757911b8bSJan Schmidt /* 3278bf1d3425SDavid Sterba * Don't call tree_mod_log_insert_move here, key removal was 3279bf1d3425SDavid Sterba * already fully logged by tree_mod_log_eb_copy above. 328057911b8bSJan Schmidt */ 32815f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 32825f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 3283e2fa7227SChris Mason (src_nritems - push_items) * 3284123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 3285bb803951SChris Mason } 32865f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 32875f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 32885f39d397SChris Mason btrfs_mark_buffer_dirty(src); 32895f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 329031840ae1SZheng Yan 3291bb803951SChris Mason return ret; 3292be0e5c09SChris Mason } 3293be0e5c09SChris Mason 329497571fd0SChris Mason /* 329579f95c82SChris Mason * try to push data from one node into the next node right in the 329679f95c82SChris Mason * tree. 329779f95c82SChris Mason * 329879f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 329979f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 330079f95c82SChris Mason * 330179f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 330279f95c82SChris Mason */ 33035f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 33045f39d397SChris Mason struct extent_buffer *dst, 33055f39d397SChris Mason struct extent_buffer *src) 330679f95c82SChris Mason { 330755d32ed8SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 330879f95c82SChris Mason int push_items = 0; 330979f95c82SChris Mason int max_push; 331079f95c82SChris Mason int src_nritems; 331179f95c82SChris Mason int dst_nritems; 331279f95c82SChris Mason int ret = 0; 331379f95c82SChris Mason 33147bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 33157bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 33167bb86316SChris Mason 33175f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 33185f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 33190b246afaSJeff Mahoney push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; 3320d397712bSChris Mason if (push_items <= 0) 332179f95c82SChris Mason return 1; 3322bce4eae9SChris Mason 3323d397712bSChris Mason if (src_nritems < 4) 3324bce4eae9SChris Mason return 1; 332579f95c82SChris Mason 332679f95c82SChris Mason max_push = src_nritems / 2 + 1; 332779f95c82SChris Mason /* don't try to empty the node */ 3328d397712bSChris Mason if (max_push >= src_nritems) 332979f95c82SChris Mason return 1; 3330252c38f0SYan 333179f95c82SChris Mason if (max_push < push_items) 333279f95c82SChris Mason push_items = max_push; 333379f95c82SChris Mason 3334bf1d3425SDavid Sterba ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems); 3335bf1d3425SDavid Sterba BUG_ON(ret < 0); 33365f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 33375f39d397SChris Mason btrfs_node_key_ptr_offset(0), 33385f39d397SChris Mason (dst_nritems) * 33395f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 3340d6025579SChris Mason 3341ed874f0dSDavid Sterba ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items, 3342ed874f0dSDavid Sterba push_items); 33435de865eeSFilipe David Borba Manana if (ret) { 334466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 33455de865eeSFilipe David Borba Manana return ret; 33465de865eeSFilipe David Borba Manana } 33475f39d397SChris Mason copy_extent_buffer(dst, src, 33485f39d397SChris Mason btrfs_node_key_ptr_offset(0), 33495f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 3350123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 335179f95c82SChris Mason 33525f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 33535f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 335479f95c82SChris Mason 33555f39d397SChris Mason btrfs_mark_buffer_dirty(src); 33565f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 335731840ae1SZheng Yan 335879f95c82SChris Mason return ret; 335979f95c82SChris Mason } 336079f95c82SChris Mason 336179f95c82SChris Mason /* 336297571fd0SChris Mason * helper function to insert a new root level in the tree. 336397571fd0SChris Mason * A new node is allocated, and a single item is inserted to 336497571fd0SChris Mason * point to the existing root 3365aa5d6bedSChris Mason * 3366aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 336797571fd0SChris Mason */ 3368d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans, 33695f39d397SChris Mason struct btrfs_root *root, 3370fdd99c72SLiu Bo struct btrfs_path *path, int level) 337174123bd7SChris Mason { 33720b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 33737bb86316SChris Mason u64 lower_gen; 33745f39d397SChris Mason struct extent_buffer *lower; 33755f39d397SChris Mason struct extent_buffer *c; 3376925baeddSChris Mason struct extent_buffer *old; 33775f39d397SChris Mason struct btrfs_disk_key lower_key; 3378d9d19a01SDavid Sterba int ret; 33795c680ed6SChris Mason 33805c680ed6SChris Mason BUG_ON(path->nodes[level]); 33815c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 33825c680ed6SChris Mason 33837bb86316SChris Mason lower = path->nodes[level-1]; 33847bb86316SChris Mason if (level == 1) 33857bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 33867bb86316SChris Mason else 33877bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 33887bb86316SChris Mason 3389a6279470SFilipe Manana c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level, 3390a6279470SFilipe Manana root->node->start, 0); 33915f39d397SChris Mason if (IS_ERR(c)) 33925f39d397SChris Mason return PTR_ERR(c); 3393925baeddSChris Mason 33940b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 3395f0486c68SYan, Zheng 33965f39d397SChris Mason btrfs_set_header_nritems(c, 1); 33975f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 3398db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 33997bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 340031840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 34017bb86316SChris Mason 34027bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 34035f39d397SChris Mason 34045f39d397SChris Mason btrfs_mark_buffer_dirty(c); 3405d5719762SChris Mason 3406925baeddSChris Mason old = root->node; 3407d9d19a01SDavid Sterba ret = tree_mod_log_insert_root(root->node, c, 0); 3408d9d19a01SDavid Sterba BUG_ON(ret < 0); 3409240f62c8SChris Mason rcu_assign_pointer(root->node, c); 3410925baeddSChris Mason 3411925baeddSChris Mason /* the super has an extra ref to root->node */ 3412925baeddSChris Mason free_extent_buffer(old); 3413925baeddSChris Mason 34140b86a832SChris Mason add_root_to_dirty_list(root); 34155f39d397SChris Mason extent_buffer_get(c); 34165f39d397SChris Mason path->nodes[level] = c; 341795449a16Schandan path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; 341874123bd7SChris Mason path->slots[level] = 0; 341974123bd7SChris Mason return 0; 342074123bd7SChris Mason } 34215c680ed6SChris Mason 34225c680ed6SChris Mason /* 34235c680ed6SChris Mason * worker function to insert a single pointer in a node. 34245c680ed6SChris Mason * the node should have enough room for the pointer already 342597571fd0SChris Mason * 34265c680ed6SChris Mason * slot and level indicate where you want the key to go, and 34275c680ed6SChris Mason * blocknr is the block the key points to. 34285c680ed6SChris Mason */ 3429143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans, 34306ad3cf6dSDavid Sterba struct btrfs_path *path, 3431143bede5SJeff Mahoney struct btrfs_disk_key *key, u64 bytenr, 3432c3e06965SJan Schmidt int slot, int level) 34335c680ed6SChris Mason { 34345f39d397SChris Mason struct extent_buffer *lower; 34355c680ed6SChris Mason int nritems; 3436f3ea38daSJan Schmidt int ret; 34375c680ed6SChris Mason 34385c680ed6SChris Mason BUG_ON(!path->nodes[level]); 3439f0486c68SYan, Zheng btrfs_assert_tree_locked(path->nodes[level]); 34405f39d397SChris Mason lower = path->nodes[level]; 34415f39d397SChris Mason nritems = btrfs_header_nritems(lower); 3442c293498bSStoyan Gaydarov BUG_ON(slot > nritems); 34436ad3cf6dSDavid Sterba BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info)); 344474123bd7SChris Mason if (slot != nritems) { 3445bf1d3425SDavid Sterba if (level) { 3446bf1d3425SDavid Sterba ret = tree_mod_log_insert_move(lower, slot + 1, slot, 3447a446a979SDavid Sterba nritems - slot); 3448bf1d3425SDavid Sterba BUG_ON(ret < 0); 3449bf1d3425SDavid Sterba } 34505f39d397SChris Mason memmove_extent_buffer(lower, 34515f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 34525f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 3453123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 345474123bd7SChris Mason } 3455c3e06965SJan Schmidt if (level) { 3456e09c2efeSDavid Sterba ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD, 3457e09c2efeSDavid Sterba GFP_NOFS); 3458f3ea38daSJan Schmidt BUG_ON(ret < 0); 3459f3ea38daSJan Schmidt } 34605f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 3461db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 346274493f7aSChris Mason WARN_ON(trans->transid == 0); 346374493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 34645f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 34655f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 346674123bd7SChris Mason } 346774123bd7SChris Mason 346897571fd0SChris Mason /* 346997571fd0SChris Mason * split the node at the specified level in path in two. 347097571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 347197571fd0SChris Mason * 347297571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 347397571fd0SChris Mason * left and right, if either one works, it returns right away. 3474aa5d6bedSChris Mason * 3475aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 347697571fd0SChris Mason */ 3477e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 3478e02119d5SChris Mason struct btrfs_root *root, 3479e02119d5SChris Mason struct btrfs_path *path, int level) 3480be0e5c09SChris Mason { 34810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 34825f39d397SChris Mason struct extent_buffer *c; 34835f39d397SChris Mason struct extent_buffer *split; 34845f39d397SChris Mason struct btrfs_disk_key disk_key; 3485be0e5c09SChris Mason int mid; 34865c680ed6SChris Mason int ret; 34877518a238SChris Mason u32 c_nritems; 3488be0e5c09SChris Mason 34895f39d397SChris Mason c = path->nodes[level]; 34907bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 34915f39d397SChris Mason if (c == root->node) { 3492d9abbf1cSJan Schmidt /* 349390f8d62eSJan Schmidt * trying to split the root, lets make a new one 349490f8d62eSJan Schmidt * 3495fdd99c72SLiu Bo * tree mod log: We don't log_removal old root in 349690f8d62eSJan Schmidt * insert_new_root, because that root buffer will be kept as a 349790f8d62eSJan Schmidt * normal node. We are going to log removal of half of the 349890f8d62eSJan Schmidt * elements below with tree_mod_log_eb_copy. We're holding a 349990f8d62eSJan Schmidt * tree lock on the buffer, which is why we cannot race with 350090f8d62eSJan Schmidt * other tree_mod_log users. 3501d9abbf1cSJan Schmidt */ 3502fdd99c72SLiu Bo ret = insert_new_root(trans, root, path, level + 1); 35035c680ed6SChris Mason if (ret) 35045c680ed6SChris Mason return ret; 3505b3612421SChris Mason } else { 3506e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 35075f39d397SChris Mason c = path->nodes[level]; 35085f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 35090b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) 3510e66f709bSChris Mason return 0; 351154aa1f4dSChris Mason if (ret < 0) 351254aa1f4dSChris Mason return ret; 35135c680ed6SChris Mason } 3514e66f709bSChris Mason 35155f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 35165d4f98a2SYan Zheng mid = (c_nritems + 1) / 2; 35175d4f98a2SYan Zheng btrfs_node_key(c, &disk_key, mid); 35187bb86316SChris Mason 3519a6279470SFilipe Manana split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level, 3520a6279470SFilipe Manana c->start, 0); 35215f39d397SChris Mason if (IS_ERR(split)) 35225f39d397SChris Mason return PTR_ERR(split); 352354aa1f4dSChris Mason 35240b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 3525bc877d28SNikolay Borisov ASSERT(btrfs_header_level(c) == level); 35265f39d397SChris Mason 3527ed874f0dSDavid Sterba ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid); 35285de865eeSFilipe David Borba Manana if (ret) { 352966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 35305de865eeSFilipe David Borba Manana return ret; 35315de865eeSFilipe David Borba Manana } 35325f39d397SChris Mason copy_extent_buffer(split, c, 35335f39d397SChris Mason btrfs_node_key_ptr_offset(0), 35345f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 3535123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 35365f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 35375f39d397SChris Mason btrfs_set_header_nritems(c, mid); 3538aa5d6bedSChris Mason ret = 0; 3539aa5d6bedSChris Mason 35405f39d397SChris Mason btrfs_mark_buffer_dirty(c); 35415f39d397SChris Mason btrfs_mark_buffer_dirty(split); 35425f39d397SChris Mason 35436ad3cf6dSDavid Sterba insert_ptr(trans, path, &disk_key, split->start, 3544c3e06965SJan Schmidt path->slots[level + 1] + 1, level + 1); 3545aa5d6bedSChris Mason 35465de08d7dSChris Mason if (path->slots[level] >= mid) { 35475c680ed6SChris Mason path->slots[level] -= mid; 3548925baeddSChris Mason btrfs_tree_unlock(c); 35495f39d397SChris Mason free_extent_buffer(c); 35505f39d397SChris Mason path->nodes[level] = split; 35515c680ed6SChris Mason path->slots[level + 1] += 1; 3552eb60ceacSChris Mason } else { 3553925baeddSChris Mason btrfs_tree_unlock(split); 35545f39d397SChris Mason free_extent_buffer(split); 3555be0e5c09SChris Mason } 3556aa5d6bedSChris Mason return ret; 3557be0e5c09SChris Mason } 3558be0e5c09SChris Mason 355974123bd7SChris Mason /* 356074123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 356174123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 356274123bd7SChris Mason * space used both by the item structs and the item data 356374123bd7SChris Mason */ 35645f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 3565be0e5c09SChris Mason { 356641be1f3bSJosef Bacik struct btrfs_item *start_item; 356741be1f3bSJosef Bacik struct btrfs_item *end_item; 356841be1f3bSJosef Bacik struct btrfs_map_token token; 3569be0e5c09SChris Mason int data_len; 35705f39d397SChris Mason int nritems = btrfs_header_nritems(l); 3571d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 3572be0e5c09SChris Mason 3573be0e5c09SChris Mason if (!nr) 3574be0e5c09SChris Mason return 0; 357541be1f3bSJosef Bacik btrfs_init_map_token(&token); 3576dd3cc16bSRoss Kirk start_item = btrfs_item_nr(start); 3577dd3cc16bSRoss Kirk end_item = btrfs_item_nr(end); 357841be1f3bSJosef Bacik data_len = btrfs_token_item_offset(l, start_item, &token) + 357941be1f3bSJosef Bacik btrfs_token_item_size(l, start_item, &token); 358041be1f3bSJosef Bacik data_len = data_len - btrfs_token_item_offset(l, end_item, &token); 35810783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 3582d4dbff95SChris Mason WARN_ON(data_len < 0); 3583be0e5c09SChris Mason return data_len; 3584be0e5c09SChris Mason } 3585be0e5c09SChris Mason 358674123bd7SChris Mason /* 3587d4dbff95SChris Mason * The space between the end of the leaf items and 3588d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 3589d4dbff95SChris Mason * the leaf has left for both items and data 3590d4dbff95SChris Mason */ 3591e902baacSDavid Sterba noinline int btrfs_leaf_free_space(struct extent_buffer *leaf) 3592d4dbff95SChris Mason { 3593e902baacSDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 35945f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 35955f39d397SChris Mason int ret; 35960b246afaSJeff Mahoney 35970b246afaSJeff Mahoney ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems); 35985f39d397SChris Mason if (ret < 0) { 35990b246afaSJeff Mahoney btrfs_crit(fs_info, 3600efe120a0SFrank Holton "leaf free space ret %d, leaf data size %lu, used %d nritems %d", 3601da17066cSJeff Mahoney ret, 36020b246afaSJeff Mahoney (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info), 36035f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 36045f39d397SChris Mason } 36055f39d397SChris Mason return ret; 3606d4dbff95SChris Mason } 3607d4dbff95SChris Mason 360899d8f83cSChris Mason /* 360999d8f83cSChris Mason * min slot controls the lowest index we're willing to push to the 361099d8f83cSChris Mason * right. We'll push up to and including min_slot, but no lower 361199d8f83cSChris Mason */ 3612f72f0010SDavid Sterba static noinline int __push_leaf_right(struct btrfs_path *path, 361344871b1bSChris Mason int data_size, int empty, 361444871b1bSChris Mason struct extent_buffer *right, 361599d8f83cSChris Mason int free_space, u32 left_nritems, 361699d8f83cSChris Mason u32 min_slot) 361700ec4c51SChris Mason { 3618f72f0010SDavid Sterba struct btrfs_fs_info *fs_info = right->fs_info; 36195f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 362044871b1bSChris Mason struct extent_buffer *upper = path->nodes[1]; 3621cfed81a0SChris Mason struct btrfs_map_token token; 36225f39d397SChris Mason struct btrfs_disk_key disk_key; 362300ec4c51SChris Mason int slot; 362434a38218SChris Mason u32 i; 362500ec4c51SChris Mason int push_space = 0; 362600ec4c51SChris Mason int push_items = 0; 36270783fcfcSChris Mason struct btrfs_item *item; 362834a38218SChris Mason u32 nr; 36297518a238SChris Mason u32 right_nritems; 36305f39d397SChris Mason u32 data_end; 3631db94535dSChris Mason u32 this_item_size; 363200ec4c51SChris Mason 3633cfed81a0SChris Mason btrfs_init_map_token(&token); 3634cfed81a0SChris Mason 363534a38218SChris Mason if (empty) 363634a38218SChris Mason nr = 0; 363734a38218SChris Mason else 363899d8f83cSChris Mason nr = max_t(u32, 1, min_slot); 363934a38218SChris Mason 364031840ae1SZheng Yan if (path->slots[0] >= left_nritems) 364187b29b20SYan Zheng push_space += data_size; 364231840ae1SZheng Yan 364344871b1bSChris Mason slot = path->slots[1]; 364434a38218SChris Mason i = left_nritems - 1; 364534a38218SChris Mason while (i >= nr) { 3646dd3cc16bSRoss Kirk item = btrfs_item_nr(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) { 3652e902baacSDavid Sterba int space = btrfs_leaf_free_space(left); 3653e902baacSDavid Sterba 365431840ae1SZheng Yan if (space + push_space * 2 > free_space) 365531840ae1SZheng Yan break; 365631840ae1SZheng Yan } 365731840ae1SZheng Yan } 365831840ae1SZheng Yan 365900ec4c51SChris Mason if (path->slots[0] == i) 366087b29b20SYan Zheng push_space += data_size; 3661db94535dSChris Mason 3662db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 3663db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 366400ec4c51SChris Mason break; 366531840ae1SZheng Yan 366600ec4c51SChris Mason push_items++; 3667db94535dSChris Mason push_space += this_item_size + sizeof(*item); 366834a38218SChris Mason if (i == 0) 366934a38218SChris Mason break; 367034a38218SChris Mason i--; 3671db94535dSChris Mason } 36725f39d397SChris Mason 3673925baeddSChris Mason if (push_items == 0) 3674925baeddSChris Mason goto out_unlock; 36755f39d397SChris Mason 36766c1500f2SJulia Lawall WARN_ON(!empty && push_items == left_nritems); 36775f39d397SChris Mason 367800ec4c51SChris Mason /* push left to right */ 36795f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 368034a38218SChris Mason 36815f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 36828f881e8cSDavid Sterba push_space -= leaf_data_end(left); 36835f39d397SChris Mason 368400ec4c51SChris Mason /* make room in the right data area */ 36858f881e8cSDavid Sterba data_end = leaf_data_end(right); 36865f39d397SChris Mason memmove_extent_buffer(right, 36873d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + data_end - push_space, 36883d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + data_end, 36890b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - data_end); 36905f39d397SChris Mason 369100ec4c51SChris Mason /* copy from the left data area */ 36923d9ec8c4SNikolay Borisov copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET + 36930b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 36948f881e8cSDavid Sterba BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left), 3695d6025579SChris Mason push_space); 36965f39d397SChris Mason 36975f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 36985f39d397SChris Mason btrfs_item_nr_offset(0), 36990783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 37005f39d397SChris Mason 370100ec4c51SChris Mason /* copy the items from left to right */ 37025f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 37035f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 37040783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 370500ec4c51SChris Mason 370600ec4c51SChris Mason /* update the item pointers */ 37077518a238SChris Mason right_nritems += push_items; 37085f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 37090b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info); 37107518a238SChris Mason for (i = 0; i < right_nritems; i++) { 3711dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3712cfed81a0SChris Mason push_space -= btrfs_token_item_size(right, item, &token); 3713cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, push_space, &token); 3714db94535dSChris Mason } 3715db94535dSChris Mason 37167518a238SChris Mason left_nritems -= push_items; 37175f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 371800ec4c51SChris Mason 371934a38218SChris Mason if (left_nritems) 37205f39d397SChris Mason btrfs_mark_buffer_dirty(left); 3721f0486c68SYan, Zheng else 37226a884d7dSDavid Sterba btrfs_clean_tree_block(left); 3723f0486c68SYan, Zheng 37245f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3725a429e513SChris Mason 37265f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 37275f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 3728d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 372902217ed2SChris Mason 373000ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 37317518a238SChris Mason if (path->slots[0] >= left_nritems) { 37327518a238SChris Mason path->slots[0] -= left_nritems; 3733925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 37346a884d7dSDavid Sterba btrfs_clean_tree_block(path->nodes[0]); 3735925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 37365f39d397SChris Mason free_extent_buffer(path->nodes[0]); 37375f39d397SChris Mason path->nodes[0] = right; 373800ec4c51SChris Mason path->slots[1] += 1; 373900ec4c51SChris Mason } else { 3740925baeddSChris Mason btrfs_tree_unlock(right); 37415f39d397SChris Mason free_extent_buffer(right); 374200ec4c51SChris Mason } 374300ec4c51SChris Mason return 0; 3744925baeddSChris Mason 3745925baeddSChris Mason out_unlock: 3746925baeddSChris Mason btrfs_tree_unlock(right); 3747925baeddSChris Mason free_extent_buffer(right); 3748925baeddSChris Mason return 1; 374900ec4c51SChris Mason } 3750925baeddSChris Mason 375100ec4c51SChris Mason /* 375244871b1bSChris Mason * push some data in the path leaf to the right, trying to free up at 375374123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 375444871b1bSChris Mason * 375544871b1bSChris Mason * returns 1 if the push failed because the other node didn't have enough 375644871b1bSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 375799d8f83cSChris Mason * 375899d8f83cSChris Mason * this will push starting from min_slot to the end of the leaf. It won't 375999d8f83cSChris Mason * push any slot lower than min_slot 376074123bd7SChris Mason */ 376144871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 376299d8f83cSChris Mason *root, struct btrfs_path *path, 376399d8f83cSChris Mason int min_data_size, int data_size, 376499d8f83cSChris Mason int empty, u32 min_slot) 3765be0e5c09SChris Mason { 376644871b1bSChris Mason struct extent_buffer *left = path->nodes[0]; 376744871b1bSChris Mason struct extent_buffer *right; 376844871b1bSChris Mason struct extent_buffer *upper; 376944871b1bSChris Mason int slot; 377044871b1bSChris Mason int free_space; 377144871b1bSChris Mason u32 left_nritems; 377244871b1bSChris Mason int ret; 377344871b1bSChris Mason 377444871b1bSChris Mason if (!path->nodes[1]) 377544871b1bSChris Mason return 1; 377644871b1bSChris Mason 377744871b1bSChris Mason slot = path->slots[1]; 377844871b1bSChris Mason upper = path->nodes[1]; 377944871b1bSChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 378044871b1bSChris Mason return 1; 378144871b1bSChris Mason 378244871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 378344871b1bSChris Mason 3784d0d20b0fSDavid Sterba right = read_node_slot(upper, slot + 1); 3785fb770ae4SLiu Bo /* 3786fb770ae4SLiu Bo * slot + 1 is not valid or we fail to read the right node, 3787fb770ae4SLiu Bo * no big deal, just return. 3788fb770ae4SLiu Bo */ 3789fb770ae4SLiu Bo if (IS_ERR(right)) 379091ca338dSTsutomu Itoh return 1; 379191ca338dSTsutomu Itoh 379244871b1bSChris Mason btrfs_tree_lock(right); 37938bead258SDavid Sterba btrfs_set_lock_blocking_write(right); 379444871b1bSChris Mason 3795e902baacSDavid Sterba free_space = btrfs_leaf_free_space(right); 379644871b1bSChris Mason if (free_space < data_size) 379744871b1bSChris Mason goto out_unlock; 379844871b1bSChris Mason 379944871b1bSChris Mason /* cow and double check */ 380044871b1bSChris Mason ret = btrfs_cow_block(trans, root, right, upper, 380144871b1bSChris Mason slot + 1, &right); 380244871b1bSChris Mason if (ret) 380344871b1bSChris Mason goto out_unlock; 380444871b1bSChris Mason 3805e902baacSDavid Sterba free_space = btrfs_leaf_free_space(right); 380644871b1bSChris Mason if (free_space < data_size) 380744871b1bSChris Mason goto out_unlock; 380844871b1bSChris Mason 380944871b1bSChris Mason left_nritems = btrfs_header_nritems(left); 381044871b1bSChris Mason if (left_nritems == 0) 381144871b1bSChris Mason goto out_unlock; 381244871b1bSChris Mason 38132ef1fed2SFilipe David Borba Manana if (path->slots[0] == left_nritems && !empty) { 38142ef1fed2SFilipe David Borba Manana /* Key greater than all keys in the leaf, right neighbor has 38152ef1fed2SFilipe David Borba Manana * enough room for it and we're not emptying our leaf to delete 38162ef1fed2SFilipe David Borba Manana * it, therefore use right neighbor to insert the new item and 381752042d8eSAndrea Gelmini * no need to touch/dirty our left leaf. */ 38182ef1fed2SFilipe David Borba Manana btrfs_tree_unlock(left); 38192ef1fed2SFilipe David Borba Manana free_extent_buffer(left); 38202ef1fed2SFilipe David Borba Manana path->nodes[0] = right; 38212ef1fed2SFilipe David Borba Manana path->slots[0] = 0; 38222ef1fed2SFilipe David Borba Manana path->slots[1]++; 38232ef1fed2SFilipe David Borba Manana return 0; 38242ef1fed2SFilipe David Borba Manana } 38252ef1fed2SFilipe David Borba Manana 3826f72f0010SDavid Sterba return __push_leaf_right(path, min_data_size, empty, 382799d8f83cSChris Mason right, free_space, left_nritems, min_slot); 382844871b1bSChris Mason out_unlock: 382944871b1bSChris Mason btrfs_tree_unlock(right); 383044871b1bSChris Mason free_extent_buffer(right); 383144871b1bSChris Mason return 1; 383244871b1bSChris Mason } 383344871b1bSChris Mason 383444871b1bSChris Mason /* 383544871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 383644871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 383799d8f83cSChris Mason * 383899d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 383999d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the 384099d8f83cSChris Mason * items 384144871b1bSChris Mason */ 38428087c193SDavid Sterba static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, 384344871b1bSChris Mason int empty, struct extent_buffer *left, 384499d8f83cSChris Mason int free_space, u32 right_nritems, 384599d8f83cSChris Mason u32 max_slot) 384644871b1bSChris Mason { 38478087c193SDavid Sterba struct btrfs_fs_info *fs_info = left->fs_info; 38485f39d397SChris Mason struct btrfs_disk_key disk_key; 38495f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 3850be0e5c09SChris Mason int i; 3851be0e5c09SChris Mason int push_space = 0; 3852be0e5c09SChris Mason int push_items = 0; 38530783fcfcSChris Mason struct btrfs_item *item; 38547518a238SChris Mason u32 old_left_nritems; 385534a38218SChris Mason u32 nr; 3856aa5d6bedSChris Mason int ret = 0; 3857db94535dSChris Mason u32 this_item_size; 3858db94535dSChris Mason u32 old_left_item_size; 3859cfed81a0SChris Mason struct btrfs_map_token token; 3860cfed81a0SChris Mason 3861cfed81a0SChris Mason btrfs_init_map_token(&token); 3862be0e5c09SChris Mason 386334a38218SChris Mason if (empty) 386499d8f83cSChris Mason nr = min(right_nritems, max_slot); 386534a38218SChris Mason else 386699d8f83cSChris Mason nr = min(right_nritems - 1, max_slot); 386734a38218SChris Mason 386834a38218SChris Mason for (i = 0; i < nr; i++) { 3869dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3870db94535dSChris Mason 387131840ae1SZheng Yan if (!empty && push_items > 0) { 387231840ae1SZheng Yan if (path->slots[0] < i) 387331840ae1SZheng Yan break; 387431840ae1SZheng Yan if (path->slots[0] == i) { 3875e902baacSDavid Sterba int space = btrfs_leaf_free_space(right); 3876e902baacSDavid Sterba 387731840ae1SZheng Yan if (space + push_space * 2 > free_space) 387831840ae1SZheng Yan break; 387931840ae1SZheng Yan } 388031840ae1SZheng Yan } 388131840ae1SZheng Yan 3882be0e5c09SChris Mason if (path->slots[0] == i) 388387b29b20SYan Zheng push_space += data_size; 3884db94535dSChris Mason 3885db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 3886db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 3887be0e5c09SChris Mason break; 3888db94535dSChris Mason 3889be0e5c09SChris Mason push_items++; 3890db94535dSChris Mason push_space += this_item_size + sizeof(*item); 3891be0e5c09SChris Mason } 3892db94535dSChris Mason 3893be0e5c09SChris Mason if (push_items == 0) { 3894925baeddSChris Mason ret = 1; 3895925baeddSChris Mason goto out; 3896be0e5c09SChris Mason } 3897fae7f21cSDulshani Gunawardhana WARN_ON(!empty && push_items == btrfs_header_nritems(right)); 38985f39d397SChris Mason 3899be0e5c09SChris Mason /* push data from right to left */ 39005f39d397SChris Mason copy_extent_buffer(left, right, 39015f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 39025f39d397SChris Mason btrfs_item_nr_offset(0), 39035f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 39045f39d397SChris Mason 39050b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info) - 39065f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1); 39075f39d397SChris Mason 39083d9ec8c4SNikolay Borisov copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET + 39098f881e8cSDavid Sterba leaf_data_end(left) - push_space, 39103d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + 39115f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 3912be0e5c09SChris Mason push_space); 39135f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 391487b29b20SYan Zheng BUG_ON(old_left_nritems <= 0); 3915eb60ceacSChris Mason 3916db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 3917be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 39185f39d397SChris Mason u32 ioff; 3919db94535dSChris Mason 3920dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3921db94535dSChris Mason 3922cfed81a0SChris Mason ioff = btrfs_token_item_offset(left, item, &token); 3923cfed81a0SChris Mason btrfs_set_token_item_offset(left, item, 39240b246afaSJeff Mahoney ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size), 3925cfed81a0SChris Mason &token); 3926be0e5c09SChris Mason } 39275f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 3928be0e5c09SChris Mason 3929be0e5c09SChris Mason /* fixup right node */ 393031b1a2bdSJulia Lawall if (push_items > right_nritems) 393131b1a2bdSJulia Lawall WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, 3932d397712bSChris Mason right_nritems); 393334a38218SChris Mason 393434a38218SChris Mason if (push_items < right_nritems) { 39355f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 39368f881e8cSDavid Sterba leaf_data_end(right); 39373d9ec8c4SNikolay Borisov memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET + 39380b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 39393d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + 39408f881e8cSDavid Sterba leaf_data_end(right), push_space); 39415f39d397SChris Mason 39425f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 39435f39d397SChris Mason btrfs_item_nr_offset(push_items), 39445f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 39450783fcfcSChris Mason sizeof(struct btrfs_item)); 394634a38218SChris Mason } 3947eef1c494SYan right_nritems -= push_items; 3948eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 39490b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info); 39505f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 3951dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3952db94535dSChris Mason 3953cfed81a0SChris Mason push_space = push_space - btrfs_token_item_size(right, 3954cfed81a0SChris Mason item, &token); 3955cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, push_space, &token); 3956db94535dSChris Mason } 3957eb60ceacSChris Mason 39585f39d397SChris Mason btrfs_mark_buffer_dirty(left); 395934a38218SChris Mason if (right_nritems) 39605f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3961f0486c68SYan, Zheng else 39626a884d7dSDavid Sterba btrfs_clean_tree_block(right); 3963098f59c2SChris Mason 39645f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 3965b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 3966be0e5c09SChris Mason 3967be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 3968be0e5c09SChris Mason if (path->slots[0] < push_items) { 3969be0e5c09SChris Mason path->slots[0] += old_left_nritems; 3970925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 39715f39d397SChris Mason free_extent_buffer(path->nodes[0]); 39725f39d397SChris Mason path->nodes[0] = left; 3973be0e5c09SChris Mason path->slots[1] -= 1; 3974be0e5c09SChris Mason } else { 3975925baeddSChris Mason btrfs_tree_unlock(left); 39765f39d397SChris Mason free_extent_buffer(left); 3977be0e5c09SChris Mason path->slots[0] -= push_items; 3978be0e5c09SChris Mason } 3979eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 3980aa5d6bedSChris Mason return ret; 3981925baeddSChris Mason out: 3982925baeddSChris Mason btrfs_tree_unlock(left); 3983925baeddSChris Mason free_extent_buffer(left); 3984925baeddSChris Mason return ret; 3985be0e5c09SChris Mason } 3986be0e5c09SChris Mason 398774123bd7SChris Mason /* 398844871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 398944871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 399099d8f83cSChris Mason * 399199d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 399299d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the 399399d8f83cSChris Mason * items 399444871b1bSChris Mason */ 399544871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 399699d8f83cSChris Mason *root, struct btrfs_path *path, int min_data_size, 399799d8f83cSChris Mason int data_size, int empty, u32 max_slot) 399844871b1bSChris Mason { 399944871b1bSChris Mason struct extent_buffer *right = path->nodes[0]; 400044871b1bSChris Mason struct extent_buffer *left; 400144871b1bSChris Mason int slot; 400244871b1bSChris Mason int free_space; 400344871b1bSChris Mason u32 right_nritems; 400444871b1bSChris Mason int ret = 0; 400544871b1bSChris Mason 400644871b1bSChris Mason slot = path->slots[1]; 400744871b1bSChris Mason if (slot == 0) 400844871b1bSChris Mason return 1; 400944871b1bSChris Mason if (!path->nodes[1]) 401044871b1bSChris Mason return 1; 401144871b1bSChris Mason 401244871b1bSChris Mason right_nritems = btrfs_header_nritems(right); 401344871b1bSChris Mason if (right_nritems == 0) 401444871b1bSChris Mason return 1; 401544871b1bSChris Mason 401644871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 401744871b1bSChris Mason 4018d0d20b0fSDavid Sterba left = read_node_slot(path->nodes[1], slot - 1); 4019fb770ae4SLiu Bo /* 4020fb770ae4SLiu Bo * slot - 1 is not valid or we fail to read the left node, 4021fb770ae4SLiu Bo * no big deal, just return. 4022fb770ae4SLiu Bo */ 4023fb770ae4SLiu Bo if (IS_ERR(left)) 402491ca338dSTsutomu Itoh return 1; 402591ca338dSTsutomu Itoh 402644871b1bSChris Mason btrfs_tree_lock(left); 40278bead258SDavid Sterba btrfs_set_lock_blocking_write(left); 402844871b1bSChris Mason 4029e902baacSDavid Sterba free_space = btrfs_leaf_free_space(left); 403044871b1bSChris Mason if (free_space < data_size) { 403144871b1bSChris Mason ret = 1; 403244871b1bSChris Mason goto out; 403344871b1bSChris Mason } 403444871b1bSChris Mason 403544871b1bSChris Mason /* cow and double check */ 403644871b1bSChris Mason ret = btrfs_cow_block(trans, root, left, 403744871b1bSChris Mason path->nodes[1], slot - 1, &left); 403844871b1bSChris Mason if (ret) { 403944871b1bSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 404079787eaaSJeff Mahoney if (ret == -ENOSPC) 404144871b1bSChris Mason ret = 1; 404244871b1bSChris Mason goto out; 404344871b1bSChris Mason } 404444871b1bSChris Mason 4045e902baacSDavid Sterba free_space = btrfs_leaf_free_space(left); 404644871b1bSChris Mason if (free_space < data_size) { 404744871b1bSChris Mason ret = 1; 404844871b1bSChris Mason goto out; 404944871b1bSChris Mason } 405044871b1bSChris Mason 40518087c193SDavid Sterba return __push_leaf_left(path, min_data_size, 405299d8f83cSChris Mason empty, left, free_space, right_nritems, 405399d8f83cSChris Mason max_slot); 405444871b1bSChris Mason out: 405544871b1bSChris Mason btrfs_tree_unlock(left); 405644871b1bSChris Mason free_extent_buffer(left); 405744871b1bSChris Mason return ret; 405844871b1bSChris Mason } 405944871b1bSChris Mason 406044871b1bSChris Mason /* 406174123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 406274123bd7SChris Mason * available for the resulting leaf level of the path. 406374123bd7SChris Mason */ 4064143bede5SJeff Mahoney static noinline void copy_for_split(struct btrfs_trans_handle *trans, 406544871b1bSChris Mason struct btrfs_path *path, 406644871b1bSChris Mason struct extent_buffer *l, 406744871b1bSChris Mason struct extent_buffer *right, 406844871b1bSChris Mason int slot, int mid, int nritems) 4069be0e5c09SChris Mason { 407094f94ad9SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 4071be0e5c09SChris Mason int data_copy_size; 4072be0e5c09SChris Mason int rt_data_off; 4073be0e5c09SChris Mason int i; 4074d4dbff95SChris Mason struct btrfs_disk_key disk_key; 4075cfed81a0SChris Mason struct btrfs_map_token token; 4076cfed81a0SChris Mason 4077cfed81a0SChris Mason btrfs_init_map_token(&token); 4078be0e5c09SChris Mason 40795f39d397SChris Mason nritems = nritems - mid; 40805f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 40818f881e8cSDavid Sterba data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l); 40825f39d397SChris Mason 40835f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 40845f39d397SChris Mason btrfs_item_nr_offset(mid), 40855f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 40865f39d397SChris Mason 40875f39d397SChris Mason copy_extent_buffer(right, l, 40883d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) - 40893d9ec8c4SNikolay Borisov data_copy_size, BTRFS_LEAF_DATA_OFFSET + 40908f881e8cSDavid Sterba leaf_data_end(l), data_copy_size); 409174123bd7SChris Mason 40920b246afaSJeff Mahoney rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid); 40935f39d397SChris Mason 40945f39d397SChris Mason for (i = 0; i < nritems; i++) { 4095dd3cc16bSRoss Kirk struct btrfs_item *item = btrfs_item_nr(i); 4096db94535dSChris Mason u32 ioff; 4097db94535dSChris Mason 4098cfed81a0SChris Mason ioff = btrfs_token_item_offset(right, item, &token); 4099cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, 4100cfed81a0SChris Mason ioff + rt_data_off, &token); 41010783fcfcSChris Mason } 410274123bd7SChris Mason 41035f39d397SChris Mason btrfs_set_header_nritems(l, mid); 41045f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 41056ad3cf6dSDavid Sterba insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1); 41065f39d397SChris Mason 41075f39d397SChris Mason btrfs_mark_buffer_dirty(right); 41085f39d397SChris Mason btrfs_mark_buffer_dirty(l); 4109eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 41105f39d397SChris Mason 4111be0e5c09SChris Mason if (mid <= slot) { 4112925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 41135f39d397SChris Mason free_extent_buffer(path->nodes[0]); 41145f39d397SChris Mason path->nodes[0] = right; 4115be0e5c09SChris Mason path->slots[0] -= mid; 4116be0e5c09SChris Mason path->slots[1] += 1; 4117925baeddSChris Mason } else { 4118925baeddSChris Mason btrfs_tree_unlock(right); 41195f39d397SChris Mason free_extent_buffer(right); 4120925baeddSChris Mason } 41215f39d397SChris Mason 4122eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 412344871b1bSChris Mason } 412444871b1bSChris Mason 412544871b1bSChris Mason /* 412699d8f83cSChris Mason * double splits happen when we need to insert a big item in the middle 412799d8f83cSChris Mason * of a leaf. A double split can leave us with 3 mostly empty leaves: 412899d8f83cSChris Mason * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] 412999d8f83cSChris Mason * A B C 413099d8f83cSChris Mason * 413199d8f83cSChris Mason * We avoid this by trying to push the items on either side of our target 413299d8f83cSChris Mason * into the adjacent leaves. If all goes well we can avoid the double split 413399d8f83cSChris Mason * completely. 413499d8f83cSChris Mason */ 413599d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans, 413699d8f83cSChris Mason struct btrfs_root *root, 413799d8f83cSChris Mason struct btrfs_path *path, 413899d8f83cSChris Mason int data_size) 413999d8f83cSChris Mason { 414099d8f83cSChris Mason int ret; 414199d8f83cSChris Mason int progress = 0; 414299d8f83cSChris Mason int slot; 414399d8f83cSChris Mason u32 nritems; 41445a4267caSFilipe David Borba Manana int space_needed = data_size; 414599d8f83cSChris Mason 414699d8f83cSChris Mason slot = path->slots[0]; 41475a4267caSFilipe David Borba Manana if (slot < btrfs_header_nritems(path->nodes[0])) 4148e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(path->nodes[0]); 414999d8f83cSChris Mason 415099d8f83cSChris Mason /* 415199d8f83cSChris Mason * try to push all the items after our slot into the 415299d8f83cSChris Mason * right leaf 415399d8f83cSChris Mason */ 41545a4267caSFilipe David Borba Manana ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot); 415599d8f83cSChris Mason if (ret < 0) 415699d8f83cSChris Mason return ret; 415799d8f83cSChris Mason 415899d8f83cSChris Mason if (ret == 0) 415999d8f83cSChris Mason progress++; 416099d8f83cSChris Mason 416199d8f83cSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 416299d8f83cSChris Mason /* 416399d8f83cSChris Mason * our goal is to get our slot at the start or end of a leaf. If 416499d8f83cSChris Mason * we've done so we're done 416599d8f83cSChris Mason */ 416699d8f83cSChris Mason if (path->slots[0] == 0 || path->slots[0] == nritems) 416799d8f83cSChris Mason return 0; 416899d8f83cSChris Mason 4169e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) 417099d8f83cSChris Mason return 0; 417199d8f83cSChris Mason 417299d8f83cSChris Mason /* try to push all the items before our slot into the next leaf */ 417399d8f83cSChris Mason slot = path->slots[0]; 4174263d3995SFilipe Manana space_needed = data_size; 4175263d3995SFilipe Manana if (slot > 0) 4176e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(path->nodes[0]); 41775a4267caSFilipe David Borba Manana ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot); 417899d8f83cSChris Mason if (ret < 0) 417999d8f83cSChris Mason return ret; 418099d8f83cSChris Mason 418199d8f83cSChris Mason if (ret == 0) 418299d8f83cSChris Mason progress++; 418399d8f83cSChris Mason 418499d8f83cSChris Mason if (progress) 418599d8f83cSChris Mason return 0; 418699d8f83cSChris Mason return 1; 418799d8f83cSChris Mason } 418899d8f83cSChris Mason 418999d8f83cSChris Mason /* 419044871b1bSChris Mason * split the path's leaf in two, making sure there is at least data_size 419144871b1bSChris Mason * available for the resulting leaf level of the path. 419244871b1bSChris Mason * 419344871b1bSChris Mason * returns 0 if all went well and < 0 on failure. 419444871b1bSChris Mason */ 419544871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 419644871b1bSChris Mason struct btrfs_root *root, 4197310712b2SOmar Sandoval const struct btrfs_key *ins_key, 419844871b1bSChris Mason struct btrfs_path *path, int data_size, 419944871b1bSChris Mason int extend) 420044871b1bSChris Mason { 42015d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 420244871b1bSChris Mason struct extent_buffer *l; 420344871b1bSChris Mason u32 nritems; 420444871b1bSChris Mason int mid; 420544871b1bSChris Mason int slot; 420644871b1bSChris Mason struct extent_buffer *right; 4207b7a0365eSDaniel Dressler struct btrfs_fs_info *fs_info = root->fs_info; 420844871b1bSChris Mason int ret = 0; 420944871b1bSChris Mason int wret; 42105d4f98a2SYan Zheng int split; 421144871b1bSChris Mason int num_doubles = 0; 421299d8f83cSChris Mason int tried_avoid_double = 0; 421344871b1bSChris Mason 4214a5719521SYan, Zheng l = path->nodes[0]; 4215a5719521SYan, Zheng slot = path->slots[0]; 4216a5719521SYan, Zheng if (extend && data_size + btrfs_item_size_nr(l, slot) + 42170b246afaSJeff Mahoney sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info)) 4218a5719521SYan, Zheng return -EOVERFLOW; 4219a5719521SYan, Zheng 422044871b1bSChris Mason /* first try to make some room by pushing left and right */ 422133157e05SLiu Bo if (data_size && path->nodes[1]) { 42225a4267caSFilipe David Borba Manana int space_needed = data_size; 42235a4267caSFilipe David Borba Manana 42245a4267caSFilipe David Borba Manana if (slot < btrfs_header_nritems(l)) 4225e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(l); 42265a4267caSFilipe David Borba Manana 42275a4267caSFilipe David Borba Manana wret = push_leaf_right(trans, root, path, space_needed, 42285a4267caSFilipe David Borba Manana space_needed, 0, 0); 422944871b1bSChris Mason if (wret < 0) 423044871b1bSChris Mason return wret; 423144871b1bSChris Mason if (wret) { 4232263d3995SFilipe Manana space_needed = data_size; 4233263d3995SFilipe Manana if (slot > 0) 4234e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(l); 42355a4267caSFilipe David Borba Manana wret = push_leaf_left(trans, root, path, space_needed, 42365a4267caSFilipe David Borba Manana space_needed, 0, (u32)-1); 423744871b1bSChris Mason if (wret < 0) 423844871b1bSChris Mason return wret; 423944871b1bSChris Mason } 424044871b1bSChris Mason l = path->nodes[0]; 424144871b1bSChris Mason 424244871b1bSChris Mason /* did the pushes work? */ 4243e902baacSDavid Sterba if (btrfs_leaf_free_space(l) >= data_size) 424444871b1bSChris Mason return 0; 424544871b1bSChris Mason } 424644871b1bSChris Mason 424744871b1bSChris Mason if (!path->nodes[1]) { 4248fdd99c72SLiu Bo ret = insert_new_root(trans, root, path, 1); 424944871b1bSChris Mason if (ret) 425044871b1bSChris Mason return ret; 425144871b1bSChris Mason } 425244871b1bSChris Mason again: 42535d4f98a2SYan Zheng split = 1; 425444871b1bSChris Mason l = path->nodes[0]; 425544871b1bSChris Mason slot = path->slots[0]; 425644871b1bSChris Mason nritems = btrfs_header_nritems(l); 425744871b1bSChris Mason mid = (nritems + 1) / 2; 425844871b1bSChris Mason 42595d4f98a2SYan Zheng if (mid <= slot) { 42605d4f98a2SYan Zheng if (nritems == 1 || 42615d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + data_size > 42620b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 42635d4f98a2SYan Zheng if (slot >= nritems) { 42645d4f98a2SYan Zheng split = 0; 42655d4f98a2SYan Zheng } else { 42665d4f98a2SYan Zheng mid = slot; 42675d4f98a2SYan Zheng if (mid != nritems && 42685d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 42690b246afaSJeff Mahoney data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { 427099d8f83cSChris Mason if (data_size && !tried_avoid_double) 427199d8f83cSChris Mason goto push_for_double; 42725d4f98a2SYan Zheng split = 2; 42735d4f98a2SYan Zheng } 42745d4f98a2SYan Zheng } 42755d4f98a2SYan Zheng } 42765d4f98a2SYan Zheng } else { 42775d4f98a2SYan Zheng if (leaf_space_used(l, 0, mid) + data_size > 42780b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 42795d4f98a2SYan Zheng if (!extend && data_size && slot == 0) { 42805d4f98a2SYan Zheng split = 0; 42815d4f98a2SYan Zheng } else if ((extend || !data_size) && slot == 0) { 42825d4f98a2SYan Zheng mid = 1; 42835d4f98a2SYan Zheng } else { 42845d4f98a2SYan Zheng mid = slot; 42855d4f98a2SYan Zheng if (mid != nritems && 42865d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 42870b246afaSJeff Mahoney data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { 428899d8f83cSChris Mason if (data_size && !tried_avoid_double) 428999d8f83cSChris Mason goto push_for_double; 42905d4f98a2SYan Zheng split = 2; 42915d4f98a2SYan Zheng } 42925d4f98a2SYan Zheng } 42935d4f98a2SYan Zheng } 42945d4f98a2SYan Zheng } 42955d4f98a2SYan Zheng 42965d4f98a2SYan Zheng if (split == 0) 42975d4f98a2SYan Zheng btrfs_cpu_key_to_disk(&disk_key, ins_key); 42985d4f98a2SYan Zheng else 42995d4f98a2SYan Zheng btrfs_item_key(l, &disk_key, mid); 43005d4f98a2SYan Zheng 4301a6279470SFilipe Manana right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0, 4302a6279470SFilipe Manana l->start, 0); 4303f0486c68SYan, Zheng if (IS_ERR(right)) 430444871b1bSChris Mason return PTR_ERR(right); 4305f0486c68SYan, Zheng 43060b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 430744871b1bSChris Mason 43085d4f98a2SYan Zheng if (split == 0) { 430944871b1bSChris Mason if (mid <= slot) { 431044871b1bSChris Mason btrfs_set_header_nritems(right, 0); 43116ad3cf6dSDavid Sterba insert_ptr(trans, path, &disk_key, 43122ff7e61eSJeff Mahoney right->start, path->slots[1] + 1, 1); 431344871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 431444871b1bSChris Mason free_extent_buffer(path->nodes[0]); 431544871b1bSChris Mason path->nodes[0] = right; 431644871b1bSChris Mason path->slots[0] = 0; 431744871b1bSChris Mason path->slots[1] += 1; 431844871b1bSChris Mason } else { 431944871b1bSChris Mason btrfs_set_header_nritems(right, 0); 43206ad3cf6dSDavid Sterba insert_ptr(trans, path, &disk_key, 43212ff7e61eSJeff Mahoney right->start, path->slots[1], 1); 432244871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 432344871b1bSChris Mason free_extent_buffer(path->nodes[0]); 432444871b1bSChris Mason path->nodes[0] = right; 432544871b1bSChris Mason path->slots[0] = 0; 4326143bede5SJeff Mahoney if (path->slots[1] == 0) 4327b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 43285d4f98a2SYan Zheng } 4329196e0249SLiu Bo /* 4330196e0249SLiu Bo * We create a new leaf 'right' for the required ins_len and 4331196e0249SLiu Bo * we'll do btrfs_mark_buffer_dirty() on this leaf after copying 4332196e0249SLiu Bo * the content of ins_len to 'right'. 4333196e0249SLiu Bo */ 433444871b1bSChris Mason return ret; 433544871b1bSChris Mason } 433644871b1bSChris Mason 433794f94ad9SDavid Sterba copy_for_split(trans, path, l, right, slot, mid, nritems); 433844871b1bSChris Mason 43395d4f98a2SYan Zheng if (split == 2) { 4340cc0c5538SChris Mason BUG_ON(num_doubles != 0); 4341cc0c5538SChris Mason num_doubles++; 4342cc0c5538SChris Mason goto again; 43433326d1b0SChris Mason } 434444871b1bSChris Mason 4345143bede5SJeff Mahoney return 0; 434699d8f83cSChris Mason 434799d8f83cSChris Mason push_for_double: 434899d8f83cSChris Mason push_for_double_split(trans, root, path, data_size); 434999d8f83cSChris Mason tried_avoid_double = 1; 4350e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) 435199d8f83cSChris Mason return 0; 435299d8f83cSChris Mason goto again; 4353be0e5c09SChris Mason } 4354be0e5c09SChris Mason 4355ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, 4356ad48fd75SYan, Zheng struct btrfs_root *root, 4357ad48fd75SYan, Zheng struct btrfs_path *path, int ins_len) 4358ad48fd75SYan, Zheng { 4359ad48fd75SYan, Zheng struct btrfs_key key; 4360ad48fd75SYan, Zheng struct extent_buffer *leaf; 4361ad48fd75SYan, Zheng struct btrfs_file_extent_item *fi; 4362ad48fd75SYan, Zheng u64 extent_len = 0; 4363ad48fd75SYan, Zheng u32 item_size; 4364ad48fd75SYan, Zheng int ret; 4365ad48fd75SYan, Zheng 4366ad48fd75SYan, Zheng leaf = path->nodes[0]; 4367ad48fd75SYan, Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 4368ad48fd75SYan, Zheng 4369ad48fd75SYan, Zheng BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 4370ad48fd75SYan, Zheng key.type != BTRFS_EXTENT_CSUM_KEY); 4371ad48fd75SYan, Zheng 4372e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) >= ins_len) 4373ad48fd75SYan, Zheng return 0; 4374ad48fd75SYan, Zheng 4375ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 4376ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 4377ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 4378ad48fd75SYan, Zheng struct btrfs_file_extent_item); 4379ad48fd75SYan, Zheng extent_len = btrfs_file_extent_num_bytes(leaf, fi); 4380ad48fd75SYan, Zheng } 4381b3b4aa74SDavid Sterba btrfs_release_path(path); 4382ad48fd75SYan, Zheng 4383ad48fd75SYan, Zheng path->keep_locks = 1; 4384ad48fd75SYan, Zheng path->search_for_split = 1; 4385ad48fd75SYan, Zheng ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 4386ad48fd75SYan, Zheng path->search_for_split = 0; 4387a8df6fe6SFilipe Manana if (ret > 0) 4388a8df6fe6SFilipe Manana ret = -EAGAIN; 4389ad48fd75SYan, Zheng if (ret < 0) 4390ad48fd75SYan, Zheng goto err; 4391ad48fd75SYan, Zheng 4392ad48fd75SYan, Zheng ret = -EAGAIN; 4393ad48fd75SYan, Zheng leaf = path->nodes[0]; 4394a8df6fe6SFilipe Manana /* if our item isn't there, return now */ 4395a8df6fe6SFilipe Manana if (item_size != btrfs_item_size_nr(leaf, path->slots[0])) 4396ad48fd75SYan, Zheng goto err; 4397ad48fd75SYan, Zheng 4398109f6aefSChris Mason /* the leaf has changed, it now has room. return now */ 4399e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len) 4400109f6aefSChris Mason goto err; 4401109f6aefSChris Mason 4402ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 4403ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 4404ad48fd75SYan, Zheng struct btrfs_file_extent_item); 4405ad48fd75SYan, Zheng if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) 4406ad48fd75SYan, Zheng goto err; 4407ad48fd75SYan, Zheng } 4408ad48fd75SYan, Zheng 4409ad48fd75SYan, Zheng btrfs_set_path_blocking(path); 4410ad48fd75SYan, Zheng ret = split_leaf(trans, root, &key, path, ins_len, 1); 4411f0486c68SYan, Zheng if (ret) 4412f0486c68SYan, Zheng goto err; 4413ad48fd75SYan, Zheng 4414ad48fd75SYan, Zheng path->keep_locks = 0; 4415ad48fd75SYan, Zheng btrfs_unlock_up_safe(path, 1); 4416ad48fd75SYan, Zheng return 0; 4417ad48fd75SYan, Zheng err: 4418ad48fd75SYan, Zheng path->keep_locks = 0; 4419ad48fd75SYan, Zheng return ret; 4420ad48fd75SYan, Zheng } 4421ad48fd75SYan, Zheng 442225263cd7SDavid Sterba static noinline int split_item(struct btrfs_path *path, 4423310712b2SOmar Sandoval const struct btrfs_key *new_key, 4424459931ecSChris Mason unsigned long split_offset) 4425459931ecSChris Mason { 4426459931ecSChris Mason struct extent_buffer *leaf; 4427459931ecSChris Mason struct btrfs_item *item; 4428459931ecSChris Mason struct btrfs_item *new_item; 4429459931ecSChris Mason int slot; 4430ad48fd75SYan, Zheng char *buf; 4431459931ecSChris Mason u32 nritems; 4432ad48fd75SYan, Zheng u32 item_size; 4433459931ecSChris Mason u32 orig_offset; 4434459931ecSChris Mason struct btrfs_disk_key disk_key; 4435459931ecSChris Mason 4436459931ecSChris Mason leaf = path->nodes[0]; 4437e902baacSDavid Sterba BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)); 4438b9473439SChris Mason 4439b4ce94deSChris Mason btrfs_set_path_blocking(path); 4440b4ce94deSChris Mason 4441dd3cc16bSRoss Kirk item = btrfs_item_nr(path->slots[0]); 4442459931ecSChris Mason orig_offset = btrfs_item_offset(leaf, item); 4443459931ecSChris Mason item_size = btrfs_item_size(leaf, item); 4444459931ecSChris Mason 4445459931ecSChris Mason buf = kmalloc(item_size, GFP_NOFS); 4446ad48fd75SYan, Zheng if (!buf) 4447ad48fd75SYan, Zheng return -ENOMEM; 4448ad48fd75SYan, Zheng 4449459931ecSChris Mason read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, 4450459931ecSChris Mason path->slots[0]), item_size); 4451ad48fd75SYan, Zheng 4452459931ecSChris Mason slot = path->slots[0] + 1; 4453459931ecSChris Mason nritems = btrfs_header_nritems(leaf); 4454459931ecSChris Mason if (slot != nritems) { 4455459931ecSChris Mason /* shift the items */ 4456459931ecSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), 4457459931ecSChris Mason btrfs_item_nr_offset(slot), 4458459931ecSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 4459459931ecSChris Mason } 4460459931ecSChris Mason 4461459931ecSChris Mason btrfs_cpu_key_to_disk(&disk_key, new_key); 4462459931ecSChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4463459931ecSChris Mason 4464dd3cc16bSRoss Kirk new_item = btrfs_item_nr(slot); 4465459931ecSChris Mason 4466459931ecSChris Mason btrfs_set_item_offset(leaf, new_item, orig_offset); 4467459931ecSChris Mason btrfs_set_item_size(leaf, new_item, item_size - split_offset); 4468459931ecSChris Mason 4469459931ecSChris Mason btrfs_set_item_offset(leaf, item, 4470459931ecSChris Mason orig_offset + item_size - split_offset); 4471459931ecSChris Mason btrfs_set_item_size(leaf, item, split_offset); 4472459931ecSChris Mason 4473459931ecSChris Mason btrfs_set_header_nritems(leaf, nritems + 1); 4474459931ecSChris Mason 4475459931ecSChris Mason /* write the data for the start of the original item */ 4476459931ecSChris Mason write_extent_buffer(leaf, buf, 4477459931ecSChris Mason btrfs_item_ptr_offset(leaf, path->slots[0]), 4478459931ecSChris Mason split_offset); 4479459931ecSChris Mason 4480459931ecSChris Mason /* write the data for the new item */ 4481459931ecSChris Mason write_extent_buffer(leaf, buf + split_offset, 4482459931ecSChris Mason btrfs_item_ptr_offset(leaf, slot), 4483459931ecSChris Mason item_size - split_offset); 4484459931ecSChris Mason btrfs_mark_buffer_dirty(leaf); 4485459931ecSChris Mason 4486e902baacSDavid Sterba BUG_ON(btrfs_leaf_free_space(leaf) < 0); 4487459931ecSChris Mason kfree(buf); 4488ad48fd75SYan, Zheng return 0; 4489ad48fd75SYan, Zheng } 4490ad48fd75SYan, Zheng 4491ad48fd75SYan, Zheng /* 4492ad48fd75SYan, Zheng * This function splits a single item into two items, 4493ad48fd75SYan, Zheng * giving 'new_key' to the new item and splitting the 4494ad48fd75SYan, Zheng * old one at split_offset (from the start of the item). 4495ad48fd75SYan, Zheng * 4496ad48fd75SYan, Zheng * The path may be released by this operation. After 4497ad48fd75SYan, Zheng * the split, the path is pointing to the old item. The 4498ad48fd75SYan, Zheng * new item is going to be in the same node as the old one. 4499ad48fd75SYan, Zheng * 4500ad48fd75SYan, Zheng * Note, the item being split must be smaller enough to live alone on 4501ad48fd75SYan, Zheng * a tree block with room for one extra struct btrfs_item 4502ad48fd75SYan, Zheng * 4503ad48fd75SYan, Zheng * This allows us to split the item in place, keeping a lock on the 4504ad48fd75SYan, Zheng * leaf the entire time. 4505ad48fd75SYan, Zheng */ 4506ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans, 4507ad48fd75SYan, Zheng struct btrfs_root *root, 4508ad48fd75SYan, Zheng struct btrfs_path *path, 4509310712b2SOmar Sandoval const struct btrfs_key *new_key, 4510ad48fd75SYan, Zheng unsigned long split_offset) 4511ad48fd75SYan, Zheng { 4512ad48fd75SYan, Zheng int ret; 4513ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 4514ad48fd75SYan, Zheng sizeof(struct btrfs_item)); 4515ad48fd75SYan, Zheng if (ret) 4516459931ecSChris Mason return ret; 4517ad48fd75SYan, Zheng 451825263cd7SDavid Sterba ret = split_item(path, new_key, split_offset); 4519ad48fd75SYan, Zheng return ret; 4520ad48fd75SYan, Zheng } 4521ad48fd75SYan, Zheng 4522ad48fd75SYan, Zheng /* 4523ad48fd75SYan, Zheng * This function duplicate a item, giving 'new_key' to the new item. 4524ad48fd75SYan, Zheng * It guarantees both items live in the same tree leaf and the new item 4525ad48fd75SYan, Zheng * is contiguous with the original item. 4526ad48fd75SYan, Zheng * 4527ad48fd75SYan, Zheng * This allows us to split file extent in place, keeping a lock on the 4528ad48fd75SYan, Zheng * leaf the entire time. 4529ad48fd75SYan, Zheng */ 4530ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans, 4531ad48fd75SYan, Zheng struct btrfs_root *root, 4532ad48fd75SYan, Zheng struct btrfs_path *path, 4533310712b2SOmar Sandoval const struct btrfs_key *new_key) 4534ad48fd75SYan, Zheng { 4535ad48fd75SYan, Zheng struct extent_buffer *leaf; 4536ad48fd75SYan, Zheng int ret; 4537ad48fd75SYan, Zheng u32 item_size; 4538ad48fd75SYan, Zheng 4539ad48fd75SYan, Zheng leaf = path->nodes[0]; 4540ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 4541ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 4542ad48fd75SYan, Zheng item_size + sizeof(struct btrfs_item)); 4543ad48fd75SYan, Zheng if (ret) 4544ad48fd75SYan, Zheng return ret; 4545ad48fd75SYan, Zheng 4546ad48fd75SYan, Zheng path->slots[0]++; 4547afe5fea7STsutomu Itoh setup_items_for_insert(root, path, new_key, &item_size, 4548ad48fd75SYan, Zheng item_size, item_size + 4549ad48fd75SYan, Zheng sizeof(struct btrfs_item), 1); 4550ad48fd75SYan, Zheng leaf = path->nodes[0]; 4551ad48fd75SYan, Zheng memcpy_extent_buffer(leaf, 4552ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0]), 4553ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0] - 1), 4554ad48fd75SYan, Zheng item_size); 4555ad48fd75SYan, Zheng return 0; 4556459931ecSChris Mason } 4557459931ecSChris Mason 4558459931ecSChris Mason /* 4559d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 4560d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 4561d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 4562d352ac68SChris Mason * the front. 4563d352ac68SChris Mason */ 456478ac4f9eSDavid Sterba void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) 4565b18c6685SChris Mason { 4566b18c6685SChris Mason int slot; 45675f39d397SChris Mason struct extent_buffer *leaf; 45685f39d397SChris Mason struct btrfs_item *item; 4569b18c6685SChris Mason u32 nritems; 4570b18c6685SChris Mason unsigned int data_end; 4571b18c6685SChris Mason unsigned int old_data_start; 4572b18c6685SChris Mason unsigned int old_size; 4573b18c6685SChris Mason unsigned int size_diff; 4574b18c6685SChris Mason int i; 4575cfed81a0SChris Mason struct btrfs_map_token token; 4576cfed81a0SChris Mason 4577cfed81a0SChris Mason btrfs_init_map_token(&token); 4578b18c6685SChris Mason 45795f39d397SChris Mason leaf = path->nodes[0]; 4580179e29e4SChris Mason slot = path->slots[0]; 4581179e29e4SChris Mason 4582179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 4583179e29e4SChris Mason if (old_size == new_size) 4584143bede5SJeff Mahoney return; 4585b18c6685SChris Mason 45865f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 45878f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 4588b18c6685SChris Mason 45895f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 4590179e29e4SChris Mason 4591b18c6685SChris Mason size_diff = old_size - new_size; 4592b18c6685SChris Mason 4593b18c6685SChris Mason BUG_ON(slot < 0); 4594b18c6685SChris Mason BUG_ON(slot >= nritems); 4595b18c6685SChris Mason 4596b18c6685SChris Mason /* 4597b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4598b18c6685SChris Mason */ 4599b18c6685SChris Mason /* first correct the data pointers */ 4600b18c6685SChris Mason for (i = slot; i < nritems; i++) { 46015f39d397SChris Mason u32 ioff; 4602dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4603db94535dSChris Mason 4604cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4605cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4606cfed81a0SChris Mason ioff + size_diff, &token); 4607b18c6685SChris Mason } 4608db94535dSChris Mason 4609b18c6685SChris Mason /* shift the data */ 4610179e29e4SChris Mason if (from_end) { 46113d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 46123d9ec8c4SNikolay Borisov data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + 4613b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 4614179e29e4SChris Mason } else { 4615179e29e4SChris Mason struct btrfs_disk_key disk_key; 4616179e29e4SChris Mason u64 offset; 4617179e29e4SChris Mason 4618179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 4619179e29e4SChris Mason 4620179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 4621179e29e4SChris Mason unsigned long ptr; 4622179e29e4SChris Mason struct btrfs_file_extent_item *fi; 4623179e29e4SChris Mason 4624179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 4625179e29e4SChris Mason struct btrfs_file_extent_item); 4626179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 4627179e29e4SChris Mason (unsigned long)fi - size_diff); 4628179e29e4SChris Mason 4629179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 4630179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 4631179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 4632179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 4633179e29e4SChris Mason (unsigned long)fi, 46347ec20afbSDavid Sterba BTRFS_FILE_EXTENT_INLINE_DATA_START); 4635179e29e4SChris Mason } 4636179e29e4SChris Mason } 4637179e29e4SChris Mason 46383d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 46393d9ec8c4SNikolay Borisov data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + 4640179e29e4SChris Mason data_end, old_data_start - data_end); 4641179e29e4SChris Mason 4642179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 4643179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 4644179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4645179e29e4SChris Mason if (slot == 0) 4646b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 4647179e29e4SChris Mason } 46485f39d397SChris Mason 4649dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 46505f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 46515f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 4652b18c6685SChris Mason 4653e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4654a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4655b18c6685SChris Mason BUG(); 46565f39d397SChris Mason } 4657b18c6685SChris Mason } 4658b18c6685SChris Mason 4659d352ac68SChris Mason /* 46608f69dbd2SStefan Behrens * make the item pointed to by the path bigger, data_size is the added size. 4661d352ac68SChris Mason */ 4662c71dd880SDavid Sterba void btrfs_extend_item(struct btrfs_path *path, u32 data_size) 46636567e837SChris Mason { 46646567e837SChris Mason int slot; 46655f39d397SChris Mason struct extent_buffer *leaf; 46665f39d397SChris Mason struct btrfs_item *item; 46676567e837SChris Mason u32 nritems; 46686567e837SChris Mason unsigned int data_end; 46696567e837SChris Mason unsigned int old_data; 46706567e837SChris Mason unsigned int old_size; 46716567e837SChris Mason int i; 4672cfed81a0SChris Mason struct btrfs_map_token token; 4673cfed81a0SChris Mason 4674cfed81a0SChris Mason btrfs_init_map_token(&token); 46756567e837SChris Mason 46765f39d397SChris Mason leaf = path->nodes[0]; 46776567e837SChris Mason 46785f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 46798f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 46806567e837SChris Mason 4681e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < data_size) { 4682a4f78750SDavid Sterba btrfs_print_leaf(leaf); 46836567e837SChris Mason BUG(); 46845f39d397SChris Mason } 46856567e837SChris Mason slot = path->slots[0]; 46865f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 46876567e837SChris Mason 46886567e837SChris Mason BUG_ON(slot < 0); 46893326d1b0SChris Mason if (slot >= nritems) { 4690a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4691c71dd880SDavid Sterba btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d", 4692d397712bSChris Mason slot, nritems); 4693290342f6SArnd Bergmann BUG(); 46943326d1b0SChris Mason } 46956567e837SChris Mason 46966567e837SChris Mason /* 46976567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 46986567e837SChris Mason */ 46996567e837SChris Mason /* first correct the data pointers */ 47006567e837SChris Mason for (i = slot; i < nritems; i++) { 47015f39d397SChris Mason u32 ioff; 4702dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4703db94535dSChris Mason 4704cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4705cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4706cfed81a0SChris Mason ioff - data_size, &token); 47076567e837SChris Mason } 47085f39d397SChris Mason 47096567e837SChris Mason /* shift the data */ 47103d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 47113d9ec8c4SNikolay Borisov data_end - data_size, BTRFS_LEAF_DATA_OFFSET + 47126567e837SChris Mason data_end, old_data - data_end); 47135f39d397SChris Mason 47146567e837SChris Mason data_end = old_data; 47155f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 4716dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 47175f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 47185f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 47196567e837SChris Mason 4720e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4721a4f78750SDavid Sterba btrfs_print_leaf(leaf); 47226567e837SChris Mason BUG(); 47235f39d397SChris Mason } 47246567e837SChris Mason } 47256567e837SChris Mason 472674123bd7SChris Mason /* 472744871b1bSChris Mason * this is a helper for btrfs_insert_empty_items, the main goal here is 472844871b1bSChris Mason * to save stack depth by doing the bulk of the work in a function 472944871b1bSChris Mason * that doesn't call btrfs_search_slot 473074123bd7SChris Mason */ 4731afe5fea7STsutomu Itoh void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, 4732310712b2SOmar Sandoval const struct btrfs_key *cpu_key, u32 *data_size, 473344871b1bSChris Mason u32 total_data, u32 total_size, int nr) 4734be0e5c09SChris Mason { 47350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 47365f39d397SChris Mason struct btrfs_item *item; 47379c58309dSChris Mason int i; 47387518a238SChris Mason u32 nritems; 4739be0e5c09SChris Mason unsigned int data_end; 4740e2fa7227SChris Mason struct btrfs_disk_key disk_key; 474144871b1bSChris Mason struct extent_buffer *leaf; 474244871b1bSChris Mason int slot; 4743cfed81a0SChris Mason struct btrfs_map_token token; 4744cfed81a0SChris Mason 474524cdc847SFilipe Manana if (path->slots[0] == 0) { 474624cdc847SFilipe Manana btrfs_cpu_key_to_disk(&disk_key, cpu_key); 4747b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 474824cdc847SFilipe Manana } 474924cdc847SFilipe Manana btrfs_unlock_up_safe(path, 1); 475024cdc847SFilipe Manana 4751cfed81a0SChris Mason btrfs_init_map_token(&token); 4752e2fa7227SChris Mason 47535f39d397SChris Mason leaf = path->nodes[0]; 475444871b1bSChris Mason slot = path->slots[0]; 475574123bd7SChris Mason 47565f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 47578f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 4758eb60ceacSChris Mason 4759e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < total_size) { 4760a4f78750SDavid Sterba btrfs_print_leaf(leaf); 47610b246afaSJeff Mahoney btrfs_crit(fs_info, "not enough freespace need %u have %d", 4762e902baacSDavid Sterba total_size, btrfs_leaf_free_space(leaf)); 4763be0e5c09SChris Mason BUG(); 4764d4dbff95SChris Mason } 47655f39d397SChris Mason 4766be0e5c09SChris Mason if (slot != nritems) { 47675f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 4768be0e5c09SChris Mason 47695f39d397SChris Mason if (old_data < data_end) { 4770a4f78750SDavid Sterba btrfs_print_leaf(leaf); 47710b246afaSJeff Mahoney btrfs_crit(fs_info, "slot %d old_data %d data_end %d", 47725f39d397SChris Mason slot, old_data, data_end); 4773290342f6SArnd Bergmann BUG(); 47745f39d397SChris Mason } 4775be0e5c09SChris Mason /* 4776be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4777be0e5c09SChris Mason */ 4778be0e5c09SChris Mason /* first correct the data pointers */ 47790783fcfcSChris Mason for (i = slot; i < nritems; i++) { 47805f39d397SChris Mason u32 ioff; 4781db94535dSChris Mason 4782dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4783cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4784cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4785cfed81a0SChris Mason ioff - total_data, &token); 47860783fcfcSChris Mason } 4787be0e5c09SChris Mason /* shift the items */ 47889c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 47895f39d397SChris Mason btrfs_item_nr_offset(slot), 47900783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 4791be0e5c09SChris Mason 4792be0e5c09SChris Mason /* shift the data */ 47933d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 47943d9ec8c4SNikolay Borisov data_end - total_data, BTRFS_LEAF_DATA_OFFSET + 4795be0e5c09SChris Mason data_end, old_data - data_end); 4796be0e5c09SChris Mason data_end = old_data; 4797be0e5c09SChris Mason } 47985f39d397SChris Mason 479962e2749eSChris Mason /* setup the item for the new data */ 48009c58309dSChris Mason for (i = 0; i < nr; i++) { 48019c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 48029c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 4803dd3cc16bSRoss Kirk item = btrfs_item_nr(slot + i); 4804cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4805cfed81a0SChris Mason data_end - data_size[i], &token); 48069c58309dSChris Mason data_end -= data_size[i]; 4807cfed81a0SChris Mason btrfs_set_token_item_size(leaf, item, data_size[i], &token); 48089c58309dSChris Mason } 480944871b1bSChris Mason 48109c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 4811b9473439SChris Mason btrfs_mark_buffer_dirty(leaf); 4812aa5d6bedSChris Mason 4813e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4814a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4815be0e5c09SChris Mason BUG(); 48165f39d397SChris Mason } 481744871b1bSChris Mason } 481844871b1bSChris Mason 481944871b1bSChris Mason /* 482044871b1bSChris Mason * Given a key and some data, insert items into the tree. 482144871b1bSChris Mason * This does all the path init required, making room in the tree if needed. 482244871b1bSChris Mason */ 482344871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 482444871b1bSChris Mason struct btrfs_root *root, 482544871b1bSChris Mason struct btrfs_path *path, 4826310712b2SOmar Sandoval const struct btrfs_key *cpu_key, u32 *data_size, 482744871b1bSChris Mason int nr) 482844871b1bSChris Mason { 482944871b1bSChris Mason int ret = 0; 483044871b1bSChris Mason int slot; 483144871b1bSChris Mason int i; 483244871b1bSChris Mason u32 total_size = 0; 483344871b1bSChris Mason u32 total_data = 0; 483444871b1bSChris Mason 483544871b1bSChris Mason for (i = 0; i < nr; i++) 483644871b1bSChris Mason total_data += data_size[i]; 483744871b1bSChris Mason 483844871b1bSChris Mason total_size = total_data + (nr * sizeof(struct btrfs_item)); 483944871b1bSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 484044871b1bSChris Mason if (ret == 0) 484144871b1bSChris Mason return -EEXIST; 484244871b1bSChris Mason if (ret < 0) 4843143bede5SJeff Mahoney return ret; 484444871b1bSChris Mason 484544871b1bSChris Mason slot = path->slots[0]; 484644871b1bSChris Mason BUG_ON(slot < 0); 484744871b1bSChris Mason 4848afe5fea7STsutomu Itoh setup_items_for_insert(root, path, cpu_key, data_size, 484944871b1bSChris Mason total_data, total_size, nr); 4850143bede5SJeff Mahoney return 0; 485162e2749eSChris Mason } 485262e2749eSChris Mason 485362e2749eSChris Mason /* 485462e2749eSChris Mason * Given a key and some data, insert an item into the tree. 485562e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 485662e2749eSChris Mason */ 4857310712b2SOmar Sandoval int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, 4858310712b2SOmar Sandoval const struct btrfs_key *cpu_key, void *data, 4859310712b2SOmar Sandoval u32 data_size) 486062e2749eSChris Mason { 486162e2749eSChris Mason int ret = 0; 48622c90e5d6SChris Mason struct btrfs_path *path; 48635f39d397SChris Mason struct extent_buffer *leaf; 48645f39d397SChris Mason unsigned long ptr; 486562e2749eSChris Mason 48662c90e5d6SChris Mason path = btrfs_alloc_path(); 4867db5b493aSTsutomu Itoh if (!path) 4868db5b493aSTsutomu Itoh return -ENOMEM; 48692c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 487062e2749eSChris Mason if (!ret) { 48715f39d397SChris Mason leaf = path->nodes[0]; 48725f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 48735f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 48745f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 487562e2749eSChris Mason } 48762c90e5d6SChris Mason btrfs_free_path(path); 4877aa5d6bedSChris Mason return ret; 4878be0e5c09SChris Mason } 4879be0e5c09SChris Mason 488074123bd7SChris Mason /* 48815de08d7dSChris Mason * delete the pointer from a given node. 488274123bd7SChris Mason * 4883d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 4884d352ac68SChris Mason * empty a node. 488574123bd7SChris Mason */ 4886afe5fea7STsutomu Itoh static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 4887afe5fea7STsutomu Itoh int level, int slot) 4888be0e5c09SChris Mason { 48895f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 48907518a238SChris Mason u32 nritems; 4891f3ea38daSJan Schmidt int ret; 4892be0e5c09SChris Mason 48935f39d397SChris Mason nritems = btrfs_header_nritems(parent); 4894be0e5c09SChris Mason if (slot != nritems - 1) { 4895bf1d3425SDavid Sterba if (level) { 4896bf1d3425SDavid Sterba ret = tree_mod_log_insert_move(parent, slot, slot + 1, 4897a446a979SDavid Sterba nritems - slot - 1); 4898bf1d3425SDavid Sterba BUG_ON(ret < 0); 4899bf1d3425SDavid Sterba } 49005f39d397SChris Mason memmove_extent_buffer(parent, 49015f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 49025f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 4903d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 4904d6025579SChris Mason (nritems - slot - 1)); 490557ba86c0SChris Mason } else if (level) { 4906e09c2efeSDavid Sterba ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE, 4907e09c2efeSDavid Sterba GFP_NOFS); 490857ba86c0SChris Mason BUG_ON(ret < 0); 4909be0e5c09SChris Mason } 4910f3ea38daSJan Schmidt 49117518a238SChris Mason nritems--; 49125f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 49137518a238SChris Mason if (nritems == 0 && parent == root->node) { 49145f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 4915eb60ceacSChris Mason /* just turn the root into a leaf and break */ 49165f39d397SChris Mason btrfs_set_header_level(root->node, 0); 4917bb803951SChris Mason } else if (slot == 0) { 49185f39d397SChris Mason struct btrfs_disk_key disk_key; 49195f39d397SChris Mason 49205f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 4921b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, level + 1); 4922be0e5c09SChris Mason } 4923d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 4924be0e5c09SChris Mason } 4925be0e5c09SChris Mason 492674123bd7SChris Mason /* 4927323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 49285d4f98a2SYan Zheng * path->nodes[1]. 4929323ac95bSChris Mason * 4930323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 4931323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 4932323ac95bSChris Mason * 4933323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 4934323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 4935323ac95bSChris Mason */ 4936143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, 4937323ac95bSChris Mason struct btrfs_root *root, 49385d4f98a2SYan Zheng struct btrfs_path *path, 49395d4f98a2SYan Zheng struct extent_buffer *leaf) 4940323ac95bSChris Mason { 49415d4f98a2SYan Zheng WARN_ON(btrfs_header_generation(leaf) != trans->transid); 4942afe5fea7STsutomu Itoh del_ptr(root, path, 1, path->slots[1]); 4943323ac95bSChris Mason 49444d081c41SChris Mason /* 49454d081c41SChris Mason * btrfs_free_extent is expensive, we want to make sure we 49464d081c41SChris Mason * aren't holding any locks when we call it 49474d081c41SChris Mason */ 49484d081c41SChris Mason btrfs_unlock_up_safe(path, 0); 49494d081c41SChris Mason 4950f0486c68SYan, Zheng root_sub_used(root, leaf->len); 4951f0486c68SYan, Zheng 49523083ee2eSJosef Bacik extent_buffer_get(leaf); 49535581a51aSJan Schmidt btrfs_free_tree_block(trans, root, leaf, 0, 1); 49543083ee2eSJosef Bacik free_extent_buffer_stale(leaf); 4955323ac95bSChris Mason } 4956323ac95bSChris Mason /* 495774123bd7SChris Mason * delete the item at the leaf level in path. If that empties 495874123bd7SChris Mason * the leaf, remove it from the tree 495974123bd7SChris Mason */ 496085e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 496185e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 4962be0e5c09SChris Mason { 49630b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 49645f39d397SChris Mason struct extent_buffer *leaf; 49655f39d397SChris Mason struct btrfs_item *item; 4966ce0eac2aSAlexandru Moise u32 last_off; 4967ce0eac2aSAlexandru Moise u32 dsize = 0; 4968aa5d6bedSChris Mason int ret = 0; 4969aa5d6bedSChris Mason int wret; 497085e21bacSChris Mason int i; 49717518a238SChris Mason u32 nritems; 4972cfed81a0SChris Mason struct btrfs_map_token token; 4973cfed81a0SChris Mason 4974cfed81a0SChris Mason btrfs_init_map_token(&token); 4975be0e5c09SChris Mason 49765f39d397SChris Mason leaf = path->nodes[0]; 497785e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 497885e21bacSChris Mason 497985e21bacSChris Mason for (i = 0; i < nr; i++) 498085e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 498185e21bacSChris Mason 49825f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 4983be0e5c09SChris Mason 498485e21bacSChris Mason if (slot + nr != nritems) { 49858f881e8cSDavid Sterba int data_end = leaf_data_end(leaf); 49865f39d397SChris Mason 49873d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 4988d6025579SChris Mason data_end + dsize, 49893d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + data_end, 499085e21bacSChris Mason last_off - data_end); 49915f39d397SChris Mason 499285e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 49935f39d397SChris Mason u32 ioff; 4994db94535dSChris Mason 4995dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4996cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4997cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4998cfed81a0SChris Mason ioff + dsize, &token); 49990783fcfcSChris Mason } 5000db94535dSChris Mason 50015f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 500285e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 50030783fcfcSChris Mason sizeof(struct btrfs_item) * 500485e21bacSChris Mason (nritems - slot - nr)); 5005be0e5c09SChris Mason } 500685e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 500785e21bacSChris Mason nritems -= nr; 50085f39d397SChris Mason 500974123bd7SChris Mason /* delete the leaf if we've emptied it */ 50107518a238SChris Mason if (nritems == 0) { 50115f39d397SChris Mason if (leaf == root->node) { 50125f39d397SChris Mason btrfs_set_header_level(leaf, 0); 50139a8dd150SChris Mason } else { 5014f0486c68SYan, Zheng btrfs_set_path_blocking(path); 50156a884d7dSDavid Sterba btrfs_clean_tree_block(leaf); 5016143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 50179a8dd150SChris Mason } 5018be0e5c09SChris Mason } else { 50197518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 5020aa5d6bedSChris Mason if (slot == 0) { 50215f39d397SChris Mason struct btrfs_disk_key disk_key; 50225f39d397SChris Mason 50235f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 5024b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 5025aa5d6bedSChris Mason } 5026aa5d6bedSChris Mason 502774123bd7SChris Mason /* delete the leaf if it is mostly empty */ 50280b246afaSJeff Mahoney if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) { 5029be0e5c09SChris Mason /* push_leaf_left fixes the path. 5030be0e5c09SChris Mason * make sure the path still points to our leaf 5031be0e5c09SChris Mason * for possible call to del_ptr below 5032be0e5c09SChris Mason */ 50334920c9acSChris Mason slot = path->slots[1]; 50345f39d397SChris Mason extent_buffer_get(leaf); 50355f39d397SChris Mason 5036b9473439SChris Mason btrfs_set_path_blocking(path); 503799d8f83cSChris Mason wret = push_leaf_left(trans, root, path, 1, 1, 503899d8f83cSChris Mason 1, (u32)-1); 503954aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 5040aa5d6bedSChris Mason ret = wret; 50415f39d397SChris Mason 50425f39d397SChris Mason if (path->nodes[0] == leaf && 50435f39d397SChris Mason btrfs_header_nritems(leaf)) { 504499d8f83cSChris Mason wret = push_leaf_right(trans, root, path, 1, 504599d8f83cSChris Mason 1, 1, 0); 504654aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 5047aa5d6bedSChris Mason ret = wret; 5048aa5d6bedSChris Mason } 50495f39d397SChris Mason 50505f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 5051323ac95bSChris Mason path->slots[1] = slot; 5052143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 50535f39d397SChris Mason free_extent_buffer(leaf); 5054143bede5SJeff Mahoney ret = 0; 50555de08d7dSChris Mason } else { 5056925baeddSChris Mason /* if we're still in the path, make sure 5057925baeddSChris Mason * we're dirty. Otherwise, one of the 5058925baeddSChris Mason * push_leaf functions must have already 5059925baeddSChris Mason * dirtied this buffer 5060925baeddSChris Mason */ 5061925baeddSChris Mason if (path->nodes[0] == leaf) 50625f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 50635f39d397SChris Mason free_extent_buffer(leaf); 5064be0e5c09SChris Mason } 5065d5719762SChris Mason } else { 50665f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 5067be0e5c09SChris Mason } 50689a8dd150SChris Mason } 5069aa5d6bedSChris Mason return ret; 50709a8dd150SChris Mason } 50719a8dd150SChris Mason 507297571fd0SChris Mason /* 5073925baeddSChris Mason * search the tree again to find a leaf with lesser keys 50747bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 50757bb86316SChris Mason * returns < 0 on io errors. 5076d352ac68SChris Mason * 5077d352ac68SChris Mason * This may release the path, and so you may lose any locks held at the 5078d352ac68SChris Mason * time you call it. 50797bb86316SChris Mason */ 508016e7549fSJosef Bacik int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 50817bb86316SChris Mason { 5082925baeddSChris Mason struct btrfs_key key; 5083925baeddSChris Mason struct btrfs_disk_key found_key; 5084925baeddSChris Mason int ret; 50857bb86316SChris Mason 5086925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 5087925baeddSChris Mason 5088e8b0d724SFilipe David Borba Manana if (key.offset > 0) { 5089925baeddSChris Mason key.offset--; 5090e8b0d724SFilipe David Borba Manana } else if (key.type > 0) { 5091925baeddSChris Mason key.type--; 5092e8b0d724SFilipe David Borba Manana key.offset = (u64)-1; 5093e8b0d724SFilipe David Borba Manana } else if (key.objectid > 0) { 5094925baeddSChris Mason key.objectid--; 5095e8b0d724SFilipe David Borba Manana key.type = (u8)-1; 5096e8b0d724SFilipe David Borba Manana key.offset = (u64)-1; 5097e8b0d724SFilipe David Borba Manana } else { 50987bb86316SChris Mason return 1; 5099e8b0d724SFilipe David Borba Manana } 51007bb86316SChris Mason 5101b3b4aa74SDavid Sterba btrfs_release_path(path); 5102925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5103925baeddSChris Mason if (ret < 0) 5104925baeddSChris Mason return ret; 5105925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 5106925baeddSChris Mason ret = comp_keys(&found_key, &key); 5107337c6f68SFilipe Manana /* 5108337c6f68SFilipe Manana * We might have had an item with the previous key in the tree right 5109337c6f68SFilipe Manana * before we released our path. And after we released our path, that 5110337c6f68SFilipe Manana * item might have been pushed to the first slot (0) of the leaf we 5111337c6f68SFilipe Manana * were holding due to a tree balance. Alternatively, an item with the 5112337c6f68SFilipe Manana * previous key can exist as the only element of a leaf (big fat item). 5113337c6f68SFilipe Manana * Therefore account for these 2 cases, so that our callers (like 5114337c6f68SFilipe Manana * btrfs_previous_item) don't miss an existing item with a key matching 5115337c6f68SFilipe Manana * the previous key we computed above. 5116337c6f68SFilipe Manana */ 5117337c6f68SFilipe Manana if (ret <= 0) 51187bb86316SChris Mason return 0; 5119925baeddSChris Mason return 1; 51207bb86316SChris Mason } 51217bb86316SChris Mason 51223f157a2fSChris Mason /* 51233f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 5124de78b51aSEric Sandeen * for nodes or leaves that are have a minimum transaction id. 5125de78b51aSEric Sandeen * This is used by the btree defrag code, and tree logging 51263f157a2fSChris Mason * 51273f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 51283f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 51293f157a2fSChris Mason * key and get a writable path. 51303f157a2fSChris Mason * 51313f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 51323f157a2fSChris Mason * of the tree. 51333f157a2fSChris Mason * 5134d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 5135d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 5136d352ac68SChris Mason * skipped over (without reading them). 5137d352ac68SChris Mason * 51383f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 51393f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 51403f157a2fSChris Mason */ 51413f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 5142de78b51aSEric Sandeen struct btrfs_path *path, 51433f157a2fSChris Mason u64 min_trans) 51443f157a2fSChris Mason { 51453f157a2fSChris Mason struct extent_buffer *cur; 51463f157a2fSChris Mason struct btrfs_key found_key; 51473f157a2fSChris Mason int slot; 51489652480bSYan int sret; 51493f157a2fSChris Mason u32 nritems; 51503f157a2fSChris Mason int level; 51513f157a2fSChris Mason int ret = 1; 5152f98de9b9SFilipe Manana int keep_locks = path->keep_locks; 51533f157a2fSChris Mason 5154f98de9b9SFilipe Manana path->keep_locks = 1; 51553f157a2fSChris Mason again: 5156bd681513SChris Mason cur = btrfs_read_lock_root_node(root); 51573f157a2fSChris Mason level = btrfs_header_level(cur); 5158e02119d5SChris Mason WARN_ON(path->nodes[level]); 51593f157a2fSChris Mason path->nodes[level] = cur; 5160bd681513SChris Mason path->locks[level] = BTRFS_READ_LOCK; 51613f157a2fSChris Mason 51623f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 51633f157a2fSChris Mason ret = 1; 51643f157a2fSChris Mason goto out; 51653f157a2fSChris Mason } 51663f157a2fSChris Mason while (1) { 51673f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 51683f157a2fSChris Mason level = btrfs_header_level(cur); 5169a74b35ecSNikolay Borisov sret = btrfs_bin_search(cur, min_key, level, &slot); 5170cbca7d59SFilipe Manana if (sret < 0) { 5171cbca7d59SFilipe Manana ret = sret; 5172cbca7d59SFilipe Manana goto out; 5173cbca7d59SFilipe Manana } 51743f157a2fSChris Mason 5175323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 5176323ac95bSChris Mason if (level == path->lowest_level) { 5177e02119d5SChris Mason if (slot >= nritems) 5178e02119d5SChris Mason goto find_next_key; 51793f157a2fSChris Mason ret = 0; 51803f157a2fSChris Mason path->slots[level] = slot; 51813f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 51823f157a2fSChris Mason goto out; 51833f157a2fSChris Mason } 51849652480bSYan if (sret && slot > 0) 51859652480bSYan slot--; 51863f157a2fSChris Mason /* 5187de78b51aSEric Sandeen * check this node pointer against the min_trans parameters. 5188de78b51aSEric Sandeen * If it is too old, old, skip to the next one. 51893f157a2fSChris Mason */ 51903f157a2fSChris Mason while (slot < nritems) { 51913f157a2fSChris Mason u64 gen; 5192e02119d5SChris Mason 51933f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 51943f157a2fSChris Mason if (gen < min_trans) { 51953f157a2fSChris Mason slot++; 51963f157a2fSChris Mason continue; 51973f157a2fSChris Mason } 51983f157a2fSChris Mason break; 51993f157a2fSChris Mason } 5200e02119d5SChris Mason find_next_key: 52013f157a2fSChris Mason /* 52023f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 52033f157a2fSChris Mason * and find another one 52043f157a2fSChris Mason */ 52053f157a2fSChris Mason if (slot >= nritems) { 5206e02119d5SChris Mason path->slots[level] = slot; 5207b4ce94deSChris Mason btrfs_set_path_blocking(path); 5208e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 5209de78b51aSEric Sandeen min_trans); 5210e02119d5SChris Mason if (sret == 0) { 5211b3b4aa74SDavid Sterba btrfs_release_path(path); 52123f157a2fSChris Mason goto again; 52133f157a2fSChris Mason } else { 52143f157a2fSChris Mason goto out; 52153f157a2fSChris Mason } 52163f157a2fSChris Mason } 52173f157a2fSChris Mason /* save our key for returning back */ 52183f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 52193f157a2fSChris Mason path->slots[level] = slot; 52203f157a2fSChris Mason if (level == path->lowest_level) { 52213f157a2fSChris Mason ret = 0; 52223f157a2fSChris Mason goto out; 52233f157a2fSChris Mason } 5224b4ce94deSChris Mason btrfs_set_path_blocking(path); 5225d0d20b0fSDavid Sterba cur = read_node_slot(cur, slot); 5226fb770ae4SLiu Bo if (IS_ERR(cur)) { 5227fb770ae4SLiu Bo ret = PTR_ERR(cur); 5228fb770ae4SLiu Bo goto out; 5229fb770ae4SLiu Bo } 52303f157a2fSChris Mason 5231bd681513SChris Mason btrfs_tree_read_lock(cur); 5232b4ce94deSChris Mason 5233bd681513SChris Mason path->locks[level - 1] = BTRFS_READ_LOCK; 52343f157a2fSChris Mason path->nodes[level - 1] = cur; 5235f7c79f30SChris Mason unlock_up(path, level, 1, 0, NULL); 52363f157a2fSChris Mason } 52373f157a2fSChris Mason out: 5238f98de9b9SFilipe Manana path->keep_locks = keep_locks; 5239f98de9b9SFilipe Manana if (ret == 0) { 5240f98de9b9SFilipe Manana btrfs_unlock_up_safe(path, path->lowest_level + 1); 5241b4ce94deSChris Mason btrfs_set_path_blocking(path); 5242f98de9b9SFilipe Manana memcpy(min_key, &found_key, sizeof(found_key)); 5243f98de9b9SFilipe Manana } 52443f157a2fSChris Mason return ret; 52453f157a2fSChris Mason } 52463f157a2fSChris Mason 5247c7da9597SDavid Sterba static int tree_move_down(struct btrfs_path *path, int *level) 52487069830aSAlexander Block { 5249fb770ae4SLiu Bo struct extent_buffer *eb; 5250fb770ae4SLiu Bo 525174dd17fbSChris Mason BUG_ON(*level == 0); 5252d0d20b0fSDavid Sterba eb = read_node_slot(path->nodes[*level], path->slots[*level]); 5253fb770ae4SLiu Bo if (IS_ERR(eb)) 5254fb770ae4SLiu Bo return PTR_ERR(eb); 5255fb770ae4SLiu Bo 5256fb770ae4SLiu Bo path->nodes[*level - 1] = eb; 52577069830aSAlexander Block path->slots[*level - 1] = 0; 52587069830aSAlexander Block (*level)--; 5259fb770ae4SLiu Bo return 0; 52607069830aSAlexander Block } 52617069830aSAlexander Block 5262f1e30261SDavid Sterba static int tree_move_next_or_upnext(struct btrfs_path *path, 52637069830aSAlexander Block int *level, int root_level) 52647069830aSAlexander Block { 52657069830aSAlexander Block int ret = 0; 52667069830aSAlexander Block int nritems; 52677069830aSAlexander Block nritems = btrfs_header_nritems(path->nodes[*level]); 52687069830aSAlexander Block 52697069830aSAlexander Block path->slots[*level]++; 52707069830aSAlexander Block 527174dd17fbSChris Mason while (path->slots[*level] >= nritems) { 52727069830aSAlexander Block if (*level == root_level) 52737069830aSAlexander Block return -1; 52747069830aSAlexander Block 52757069830aSAlexander Block /* move upnext */ 52767069830aSAlexander Block path->slots[*level] = 0; 52777069830aSAlexander Block free_extent_buffer(path->nodes[*level]); 52787069830aSAlexander Block path->nodes[*level] = NULL; 52797069830aSAlexander Block (*level)++; 52807069830aSAlexander Block path->slots[*level]++; 52817069830aSAlexander Block 52827069830aSAlexander Block nritems = btrfs_header_nritems(path->nodes[*level]); 52837069830aSAlexander Block ret = 1; 52847069830aSAlexander Block } 52857069830aSAlexander Block return ret; 52867069830aSAlexander Block } 52877069830aSAlexander Block 52887069830aSAlexander Block /* 52897069830aSAlexander Block * Returns 1 if it had to move up and next. 0 is returned if it moved only next 52907069830aSAlexander Block * or down. 52917069830aSAlexander Block */ 5292179d1e6aSDavid Sterba static int tree_advance(struct btrfs_path *path, 52937069830aSAlexander Block int *level, int root_level, 52947069830aSAlexander Block int allow_down, 52957069830aSAlexander Block struct btrfs_key *key) 52967069830aSAlexander Block { 52977069830aSAlexander Block int ret; 52987069830aSAlexander Block 52997069830aSAlexander Block if (*level == 0 || !allow_down) { 5300f1e30261SDavid Sterba ret = tree_move_next_or_upnext(path, level, root_level); 53017069830aSAlexander Block } else { 5302c7da9597SDavid Sterba ret = tree_move_down(path, level); 53037069830aSAlexander Block } 53047069830aSAlexander Block if (ret >= 0) { 53057069830aSAlexander Block if (*level == 0) 53067069830aSAlexander Block btrfs_item_key_to_cpu(path->nodes[*level], key, 53077069830aSAlexander Block path->slots[*level]); 53087069830aSAlexander Block else 53097069830aSAlexander Block btrfs_node_key_to_cpu(path->nodes[*level], key, 53107069830aSAlexander Block path->slots[*level]); 53117069830aSAlexander Block } 53127069830aSAlexander Block return ret; 53137069830aSAlexander Block } 53147069830aSAlexander Block 53152ff7e61eSJeff Mahoney static int tree_compare_item(struct btrfs_path *left_path, 53167069830aSAlexander Block struct btrfs_path *right_path, 53177069830aSAlexander Block char *tmp_buf) 53187069830aSAlexander Block { 53197069830aSAlexander Block int cmp; 53207069830aSAlexander Block int len1, len2; 53217069830aSAlexander Block unsigned long off1, off2; 53227069830aSAlexander Block 53237069830aSAlexander Block len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); 53247069830aSAlexander Block len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); 53257069830aSAlexander Block if (len1 != len2) 53267069830aSAlexander Block return 1; 53277069830aSAlexander Block 53287069830aSAlexander Block off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); 53297069830aSAlexander Block off2 = btrfs_item_ptr_offset(right_path->nodes[0], 53307069830aSAlexander Block right_path->slots[0]); 53317069830aSAlexander Block 53327069830aSAlexander Block read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); 53337069830aSAlexander Block 53347069830aSAlexander Block cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); 53357069830aSAlexander Block if (cmp) 53367069830aSAlexander Block return 1; 53377069830aSAlexander Block return 0; 53387069830aSAlexander Block } 53397069830aSAlexander Block 53407069830aSAlexander Block #define ADVANCE 1 53417069830aSAlexander Block #define ADVANCE_ONLY_NEXT -1 53427069830aSAlexander Block 53437069830aSAlexander Block /* 53447069830aSAlexander Block * This function compares two trees and calls the provided callback for 53457069830aSAlexander Block * every changed/new/deleted item it finds. 53467069830aSAlexander Block * If shared tree blocks are encountered, whole subtrees are skipped, making 53477069830aSAlexander Block * the compare pretty fast on snapshotted subvolumes. 53487069830aSAlexander Block * 53497069830aSAlexander Block * This currently works on commit roots only. As commit roots are read only, 53507069830aSAlexander Block * we don't do any locking. The commit roots are protected with transactions. 53517069830aSAlexander Block * Transactions are ended and rejoined when a commit is tried in between. 53527069830aSAlexander Block * 53537069830aSAlexander Block * This function checks for modifications done to the trees while comparing. 53547069830aSAlexander Block * If it detects a change, it aborts immediately. 53557069830aSAlexander Block */ 53567069830aSAlexander Block int btrfs_compare_trees(struct btrfs_root *left_root, 53577069830aSAlexander Block struct btrfs_root *right_root, 53587069830aSAlexander Block btrfs_changed_cb_t changed_cb, void *ctx) 53597069830aSAlexander Block { 53600b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = left_root->fs_info; 53617069830aSAlexander Block int ret; 53627069830aSAlexander Block int cmp; 53637069830aSAlexander Block struct btrfs_path *left_path = NULL; 53647069830aSAlexander Block struct btrfs_path *right_path = NULL; 53657069830aSAlexander Block struct btrfs_key left_key; 53667069830aSAlexander Block struct btrfs_key right_key; 53677069830aSAlexander Block char *tmp_buf = NULL; 53687069830aSAlexander Block int left_root_level; 53697069830aSAlexander Block int right_root_level; 53707069830aSAlexander Block int left_level; 53717069830aSAlexander Block int right_level; 53727069830aSAlexander Block int left_end_reached; 53737069830aSAlexander Block int right_end_reached; 53747069830aSAlexander Block int advance_left; 53757069830aSAlexander Block int advance_right; 53767069830aSAlexander Block u64 left_blockptr; 53777069830aSAlexander Block u64 right_blockptr; 53786baa4293SFilipe Manana u64 left_gen; 53796baa4293SFilipe Manana u64 right_gen; 53807069830aSAlexander Block 53817069830aSAlexander Block left_path = btrfs_alloc_path(); 53827069830aSAlexander Block if (!left_path) { 53837069830aSAlexander Block ret = -ENOMEM; 53847069830aSAlexander Block goto out; 53857069830aSAlexander Block } 53867069830aSAlexander Block right_path = btrfs_alloc_path(); 53877069830aSAlexander Block if (!right_path) { 53887069830aSAlexander Block ret = -ENOMEM; 53897069830aSAlexander Block goto out; 53907069830aSAlexander Block } 53917069830aSAlexander Block 5392752ade68SMichal Hocko tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); 53937069830aSAlexander Block if (!tmp_buf) { 53947069830aSAlexander Block ret = -ENOMEM; 53957069830aSAlexander Block goto out; 53967069830aSAlexander Block } 53977069830aSAlexander Block 53987069830aSAlexander Block left_path->search_commit_root = 1; 53997069830aSAlexander Block left_path->skip_locking = 1; 54007069830aSAlexander Block right_path->search_commit_root = 1; 54017069830aSAlexander Block right_path->skip_locking = 1; 54027069830aSAlexander Block 54037069830aSAlexander Block /* 54047069830aSAlexander Block * Strategy: Go to the first items of both trees. Then do 54057069830aSAlexander Block * 54067069830aSAlexander Block * If both trees are at level 0 54077069830aSAlexander Block * Compare keys of current items 54087069830aSAlexander Block * If left < right treat left item as new, advance left tree 54097069830aSAlexander Block * and repeat 54107069830aSAlexander Block * If left > right treat right item as deleted, advance right tree 54117069830aSAlexander Block * and repeat 54127069830aSAlexander Block * If left == right do deep compare of items, treat as changed if 54137069830aSAlexander Block * needed, advance both trees and repeat 54147069830aSAlexander Block * If both trees are at the same level but not at level 0 54157069830aSAlexander Block * Compare keys of current nodes/leafs 54167069830aSAlexander Block * If left < right advance left tree and repeat 54177069830aSAlexander Block * If left > right advance right tree and repeat 54187069830aSAlexander Block * If left == right compare blockptrs of the next nodes/leafs 54197069830aSAlexander Block * If they match advance both trees but stay at the same level 54207069830aSAlexander Block * and repeat 54217069830aSAlexander Block * If they don't match advance both trees while allowing to go 54227069830aSAlexander Block * deeper and repeat 54237069830aSAlexander Block * If tree levels are different 54247069830aSAlexander Block * Advance the tree that needs it and repeat 54257069830aSAlexander Block * 54267069830aSAlexander Block * Advancing a tree means: 54277069830aSAlexander Block * If we are at level 0, try to go to the next slot. If that's not 54287069830aSAlexander Block * possible, go one level up and repeat. Stop when we found a level 54297069830aSAlexander Block * where we could go to the next slot. We may at this point be on a 54307069830aSAlexander Block * node or a leaf. 54317069830aSAlexander Block * 54327069830aSAlexander Block * If we are not at level 0 and not on shared tree blocks, go one 54337069830aSAlexander Block * level deeper. 54347069830aSAlexander Block * 54357069830aSAlexander Block * If we are not at level 0 and on shared tree blocks, go one slot to 54367069830aSAlexander Block * the right if possible or go up and right. 54377069830aSAlexander Block */ 54387069830aSAlexander Block 54390b246afaSJeff Mahoney down_read(&fs_info->commit_root_sem); 54407069830aSAlexander Block left_level = btrfs_header_level(left_root->commit_root); 54417069830aSAlexander Block left_root_level = left_level; 54426f2f0b39SRobbie Ko left_path->nodes[left_level] = 54436f2f0b39SRobbie Ko btrfs_clone_extent_buffer(left_root->commit_root); 54446f2f0b39SRobbie Ko if (!left_path->nodes[left_level]) { 54456f2f0b39SRobbie Ko up_read(&fs_info->commit_root_sem); 54466f2f0b39SRobbie Ko ret = -ENOMEM; 54476f2f0b39SRobbie Ko goto out; 54486f2f0b39SRobbie Ko } 54497069830aSAlexander Block 54507069830aSAlexander Block right_level = btrfs_header_level(right_root->commit_root); 54517069830aSAlexander Block right_root_level = right_level; 54526f2f0b39SRobbie Ko right_path->nodes[right_level] = 54536f2f0b39SRobbie Ko btrfs_clone_extent_buffer(right_root->commit_root); 54546f2f0b39SRobbie Ko if (!right_path->nodes[right_level]) { 54556f2f0b39SRobbie Ko up_read(&fs_info->commit_root_sem); 54566f2f0b39SRobbie Ko ret = -ENOMEM; 54576f2f0b39SRobbie Ko goto out; 54586f2f0b39SRobbie Ko } 54590b246afaSJeff Mahoney up_read(&fs_info->commit_root_sem); 54607069830aSAlexander Block 54617069830aSAlexander Block if (left_level == 0) 54627069830aSAlexander Block btrfs_item_key_to_cpu(left_path->nodes[left_level], 54637069830aSAlexander Block &left_key, left_path->slots[left_level]); 54647069830aSAlexander Block else 54657069830aSAlexander Block btrfs_node_key_to_cpu(left_path->nodes[left_level], 54667069830aSAlexander Block &left_key, left_path->slots[left_level]); 54677069830aSAlexander Block if (right_level == 0) 54687069830aSAlexander Block btrfs_item_key_to_cpu(right_path->nodes[right_level], 54697069830aSAlexander Block &right_key, right_path->slots[right_level]); 54707069830aSAlexander Block else 54717069830aSAlexander Block btrfs_node_key_to_cpu(right_path->nodes[right_level], 54727069830aSAlexander Block &right_key, right_path->slots[right_level]); 54737069830aSAlexander Block 54747069830aSAlexander Block left_end_reached = right_end_reached = 0; 54757069830aSAlexander Block advance_left = advance_right = 0; 54767069830aSAlexander Block 54777069830aSAlexander Block while (1) { 54787069830aSAlexander Block if (advance_left && !left_end_reached) { 5479179d1e6aSDavid Sterba ret = tree_advance(left_path, &left_level, 54807069830aSAlexander Block left_root_level, 54817069830aSAlexander Block advance_left != ADVANCE_ONLY_NEXT, 54827069830aSAlexander Block &left_key); 5483fb770ae4SLiu Bo if (ret == -1) 54847069830aSAlexander Block left_end_reached = ADVANCE; 5485fb770ae4SLiu Bo else if (ret < 0) 5486fb770ae4SLiu Bo goto out; 54877069830aSAlexander Block advance_left = 0; 54887069830aSAlexander Block } 54897069830aSAlexander Block if (advance_right && !right_end_reached) { 5490179d1e6aSDavid Sterba ret = tree_advance(right_path, &right_level, 54917069830aSAlexander Block right_root_level, 54927069830aSAlexander Block advance_right != ADVANCE_ONLY_NEXT, 54937069830aSAlexander Block &right_key); 5494fb770ae4SLiu Bo if (ret == -1) 54957069830aSAlexander Block right_end_reached = ADVANCE; 5496fb770ae4SLiu Bo else if (ret < 0) 5497fb770ae4SLiu Bo goto out; 54987069830aSAlexander Block advance_right = 0; 54997069830aSAlexander Block } 55007069830aSAlexander Block 55017069830aSAlexander Block if (left_end_reached && right_end_reached) { 55027069830aSAlexander Block ret = 0; 55037069830aSAlexander Block goto out; 55047069830aSAlexander Block } else if (left_end_reached) { 55057069830aSAlexander Block if (right_level == 0) { 5506ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55077069830aSAlexander Block &right_key, 55087069830aSAlexander Block BTRFS_COMPARE_TREE_DELETED, 55097069830aSAlexander Block ctx); 55107069830aSAlexander Block if (ret < 0) 55117069830aSAlexander Block goto out; 55127069830aSAlexander Block } 55137069830aSAlexander Block advance_right = ADVANCE; 55147069830aSAlexander Block continue; 55157069830aSAlexander Block } else if (right_end_reached) { 55167069830aSAlexander Block if (left_level == 0) { 5517ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55187069830aSAlexander Block &left_key, 55197069830aSAlexander Block BTRFS_COMPARE_TREE_NEW, 55207069830aSAlexander Block ctx); 55217069830aSAlexander Block if (ret < 0) 55227069830aSAlexander Block goto out; 55237069830aSAlexander Block } 55247069830aSAlexander Block advance_left = ADVANCE; 55257069830aSAlexander Block continue; 55267069830aSAlexander Block } 55277069830aSAlexander Block 55287069830aSAlexander Block if (left_level == 0 && right_level == 0) { 55297069830aSAlexander Block cmp = btrfs_comp_cpu_keys(&left_key, &right_key); 55307069830aSAlexander Block if (cmp < 0) { 5531ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55327069830aSAlexander Block &left_key, 55337069830aSAlexander Block BTRFS_COMPARE_TREE_NEW, 55347069830aSAlexander Block ctx); 55357069830aSAlexander Block if (ret < 0) 55367069830aSAlexander Block goto out; 55377069830aSAlexander Block advance_left = ADVANCE; 55387069830aSAlexander Block } else if (cmp > 0) { 5539ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55407069830aSAlexander Block &right_key, 55417069830aSAlexander Block BTRFS_COMPARE_TREE_DELETED, 55427069830aSAlexander Block ctx); 55437069830aSAlexander Block if (ret < 0) 55447069830aSAlexander Block goto out; 55457069830aSAlexander Block advance_right = ADVANCE; 55467069830aSAlexander Block } else { 5547b99d9a6aSFabian Frederick enum btrfs_compare_tree_result result; 5548ba5e8f2eSJosef Bacik 554974dd17fbSChris Mason WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); 55502ff7e61eSJeff Mahoney ret = tree_compare_item(left_path, right_path, 55512ff7e61eSJeff Mahoney tmp_buf); 5552ba5e8f2eSJosef Bacik if (ret) 5553b99d9a6aSFabian Frederick result = BTRFS_COMPARE_TREE_CHANGED; 5554ba5e8f2eSJosef Bacik else 5555b99d9a6aSFabian Frederick result = BTRFS_COMPARE_TREE_SAME; 5556ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 5557b99d9a6aSFabian Frederick &left_key, result, ctx); 55587069830aSAlexander Block if (ret < 0) 55597069830aSAlexander Block goto out; 55607069830aSAlexander Block advance_left = ADVANCE; 55617069830aSAlexander Block advance_right = ADVANCE; 55627069830aSAlexander Block } 55637069830aSAlexander Block } else if (left_level == right_level) { 55647069830aSAlexander Block cmp = btrfs_comp_cpu_keys(&left_key, &right_key); 55657069830aSAlexander Block if (cmp < 0) { 55667069830aSAlexander Block advance_left = ADVANCE; 55677069830aSAlexander Block } else if (cmp > 0) { 55687069830aSAlexander Block advance_right = ADVANCE; 55697069830aSAlexander Block } else { 55707069830aSAlexander Block left_blockptr = btrfs_node_blockptr( 55717069830aSAlexander Block left_path->nodes[left_level], 55727069830aSAlexander Block left_path->slots[left_level]); 55737069830aSAlexander Block right_blockptr = btrfs_node_blockptr( 55747069830aSAlexander Block right_path->nodes[right_level], 55757069830aSAlexander Block right_path->slots[right_level]); 55766baa4293SFilipe Manana left_gen = btrfs_node_ptr_generation( 55776baa4293SFilipe Manana left_path->nodes[left_level], 55786baa4293SFilipe Manana left_path->slots[left_level]); 55796baa4293SFilipe Manana right_gen = btrfs_node_ptr_generation( 55806baa4293SFilipe Manana right_path->nodes[right_level], 55816baa4293SFilipe Manana right_path->slots[right_level]); 55826baa4293SFilipe Manana if (left_blockptr == right_blockptr && 55836baa4293SFilipe Manana left_gen == right_gen) { 55847069830aSAlexander Block /* 55857069830aSAlexander Block * As we're on a shared block, don't 55867069830aSAlexander Block * allow to go deeper. 55877069830aSAlexander Block */ 55887069830aSAlexander Block advance_left = ADVANCE_ONLY_NEXT; 55897069830aSAlexander Block advance_right = ADVANCE_ONLY_NEXT; 55907069830aSAlexander Block } else { 55917069830aSAlexander Block advance_left = ADVANCE; 55927069830aSAlexander Block advance_right = ADVANCE; 55937069830aSAlexander Block } 55947069830aSAlexander Block } 55957069830aSAlexander Block } else if (left_level < right_level) { 55967069830aSAlexander Block advance_right = ADVANCE; 55977069830aSAlexander Block } else { 55987069830aSAlexander Block advance_left = ADVANCE; 55997069830aSAlexander Block } 56007069830aSAlexander Block } 56017069830aSAlexander Block 56027069830aSAlexander Block out: 56037069830aSAlexander Block btrfs_free_path(left_path); 56047069830aSAlexander Block btrfs_free_path(right_path); 56058f282f71SDavid Sterba kvfree(tmp_buf); 56067069830aSAlexander Block return ret; 56077069830aSAlexander Block } 56087069830aSAlexander Block 56093f157a2fSChris Mason /* 56103f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 56113f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 5612de78b51aSEric Sandeen * tree based on the current path and the min_trans parameters. 56133f157a2fSChris Mason * 56143f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 56153f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 56163f157a2fSChris Mason * 56173f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 56183f157a2fSChris Mason * calling this function. 56193f157a2fSChris Mason */ 5620e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 5621de78b51aSEric Sandeen struct btrfs_key *key, int level, u64 min_trans) 5622e7a84565SChris Mason { 5623e7a84565SChris Mason int slot; 5624e7a84565SChris Mason struct extent_buffer *c; 5625e7a84565SChris Mason 5626934d375bSChris Mason WARN_ON(!path->keep_locks); 5627e7a84565SChris Mason while (level < BTRFS_MAX_LEVEL) { 5628e7a84565SChris Mason if (!path->nodes[level]) 5629e7a84565SChris Mason return 1; 5630e7a84565SChris Mason 5631e7a84565SChris Mason slot = path->slots[level] + 1; 5632e7a84565SChris Mason c = path->nodes[level]; 56333f157a2fSChris Mason next: 5634e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 563533c66f43SYan Zheng int ret; 563633c66f43SYan Zheng int orig_lowest; 563733c66f43SYan Zheng struct btrfs_key cur_key; 563833c66f43SYan Zheng if (level + 1 >= BTRFS_MAX_LEVEL || 563933c66f43SYan Zheng !path->nodes[level + 1]) 5640e7a84565SChris Mason return 1; 564133c66f43SYan Zheng 564233c66f43SYan Zheng if (path->locks[level + 1]) { 564333c66f43SYan Zheng level++; 5644e7a84565SChris Mason continue; 5645e7a84565SChris Mason } 564633c66f43SYan Zheng 564733c66f43SYan Zheng slot = btrfs_header_nritems(c) - 1; 564833c66f43SYan Zheng if (level == 0) 564933c66f43SYan Zheng btrfs_item_key_to_cpu(c, &cur_key, slot); 565033c66f43SYan Zheng else 565133c66f43SYan Zheng btrfs_node_key_to_cpu(c, &cur_key, slot); 565233c66f43SYan Zheng 565333c66f43SYan Zheng orig_lowest = path->lowest_level; 5654b3b4aa74SDavid Sterba btrfs_release_path(path); 565533c66f43SYan Zheng path->lowest_level = level; 565633c66f43SYan Zheng ret = btrfs_search_slot(NULL, root, &cur_key, path, 565733c66f43SYan Zheng 0, 0); 565833c66f43SYan Zheng path->lowest_level = orig_lowest; 565933c66f43SYan Zheng if (ret < 0) 566033c66f43SYan Zheng return ret; 566133c66f43SYan Zheng 566233c66f43SYan Zheng c = path->nodes[level]; 566333c66f43SYan Zheng slot = path->slots[level]; 566433c66f43SYan Zheng if (ret == 0) 566533c66f43SYan Zheng slot++; 566633c66f43SYan Zheng goto next; 566733c66f43SYan Zheng } 566833c66f43SYan Zheng 5669e7a84565SChris Mason if (level == 0) 5670e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 56713f157a2fSChris Mason else { 56723f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 56733f157a2fSChris Mason 56743f157a2fSChris Mason if (gen < min_trans) { 56753f157a2fSChris Mason slot++; 56763f157a2fSChris Mason goto next; 56773f157a2fSChris Mason } 5678e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 56793f157a2fSChris Mason } 5680e7a84565SChris Mason return 0; 5681e7a84565SChris Mason } 5682e7a84565SChris Mason return 1; 5683e7a84565SChris Mason } 5684e7a84565SChris Mason 56857bb86316SChris Mason /* 5686925baeddSChris Mason * search the tree again to find a leaf with greater keys 56870f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 56880f70abe2SChris Mason * returns < 0 on io errors. 568997571fd0SChris Mason */ 5690234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 5691d97e63b6SChris Mason { 56923d7806ecSJan Schmidt return btrfs_next_old_leaf(root, path, 0); 56933d7806ecSJan Schmidt } 56943d7806ecSJan Schmidt 56953d7806ecSJan Schmidt int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, 56963d7806ecSJan Schmidt u64 time_seq) 56973d7806ecSJan Schmidt { 5698d97e63b6SChris Mason int slot; 56998e73f275SChris Mason int level; 57005f39d397SChris Mason struct extent_buffer *c; 57018e73f275SChris Mason struct extent_buffer *next; 5702925baeddSChris Mason struct btrfs_key key; 5703925baeddSChris Mason u32 nritems; 5704925baeddSChris Mason int ret; 57058e73f275SChris Mason int old_spinning = path->leave_spinning; 5706bd681513SChris Mason int next_rw_lock = 0; 5707925baeddSChris Mason 5708925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 5709d397712bSChris Mason if (nritems == 0) 5710925baeddSChris Mason return 1; 5711925baeddSChris Mason 57128e73f275SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 57138e73f275SChris Mason again: 57148e73f275SChris Mason level = 1; 57158e73f275SChris Mason next = NULL; 5716bd681513SChris Mason next_rw_lock = 0; 5717b3b4aa74SDavid Sterba btrfs_release_path(path); 57188e73f275SChris Mason 5719a2135011SChris Mason path->keep_locks = 1; 57208e73f275SChris Mason path->leave_spinning = 1; 57218e73f275SChris Mason 57223d7806ecSJan Schmidt if (time_seq) 57233d7806ecSJan Schmidt ret = btrfs_search_old_slot(root, &key, path, time_seq); 57243d7806ecSJan Schmidt else 5725925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5726925baeddSChris Mason path->keep_locks = 0; 5727925baeddSChris Mason 5728925baeddSChris Mason if (ret < 0) 5729925baeddSChris Mason return ret; 5730925baeddSChris Mason 5731a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 5732168fd7d2SChris Mason /* 5733168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 5734168fd7d2SChris Mason * could have added more items next to the key that used to be 5735168fd7d2SChris Mason * at the very end of the block. So, check again here and 5736168fd7d2SChris Mason * advance the path if there are now more items available. 5737168fd7d2SChris Mason */ 5738a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 5739e457afecSYan Zheng if (ret == 0) 5740168fd7d2SChris Mason path->slots[0]++; 57418e73f275SChris Mason ret = 0; 5742925baeddSChris Mason goto done; 5743925baeddSChris Mason } 57440b43e04fSLiu Bo /* 57450b43e04fSLiu Bo * So the above check misses one case: 57460b43e04fSLiu Bo * - after releasing the path above, someone has removed the item that 57470b43e04fSLiu Bo * used to be at the very end of the block, and balance between leafs 57480b43e04fSLiu Bo * gets another one with bigger key.offset to replace it. 57490b43e04fSLiu Bo * 57500b43e04fSLiu Bo * This one should be returned as well, or we can get leaf corruption 57510b43e04fSLiu Bo * later(esp. in __btrfs_drop_extents()). 57520b43e04fSLiu Bo * 57530b43e04fSLiu Bo * And a bit more explanation about this check, 57540b43e04fSLiu Bo * with ret > 0, the key isn't found, the path points to the slot 57550b43e04fSLiu Bo * where it should be inserted, so the path->slots[0] item must be the 57560b43e04fSLiu Bo * bigger one. 57570b43e04fSLiu Bo */ 57580b43e04fSLiu Bo if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) { 57590b43e04fSLiu Bo ret = 0; 57600b43e04fSLiu Bo goto done; 57610b43e04fSLiu Bo } 5762d97e63b6SChris Mason 5763234b63a0SChris Mason while (level < BTRFS_MAX_LEVEL) { 57648e73f275SChris Mason if (!path->nodes[level]) { 57658e73f275SChris Mason ret = 1; 57668e73f275SChris Mason goto done; 57678e73f275SChris Mason } 57685f39d397SChris Mason 5769d97e63b6SChris Mason slot = path->slots[level] + 1; 5770d97e63b6SChris Mason c = path->nodes[level]; 57715f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 5772d97e63b6SChris Mason level++; 57738e73f275SChris Mason if (level == BTRFS_MAX_LEVEL) { 57748e73f275SChris Mason ret = 1; 57758e73f275SChris Mason goto done; 57768e73f275SChris Mason } 5777d97e63b6SChris Mason continue; 5778d97e63b6SChris Mason } 57795f39d397SChris Mason 5780925baeddSChris Mason if (next) { 5781bd681513SChris Mason btrfs_tree_unlock_rw(next, next_rw_lock); 57825f39d397SChris Mason free_extent_buffer(next); 5783925baeddSChris Mason } 57845f39d397SChris Mason 57858e73f275SChris Mason next = c; 5786bd681513SChris Mason next_rw_lock = path->locks[level]; 5787d07b8528SLiu Bo ret = read_block_for_search(root, path, &next, level, 5788cda79c54SDavid Sterba slot, &key); 57898e73f275SChris Mason if (ret == -EAGAIN) 57908e73f275SChris Mason goto again; 57915f39d397SChris Mason 579276a05b35SChris Mason if (ret < 0) { 5793b3b4aa74SDavid Sterba btrfs_release_path(path); 579476a05b35SChris Mason goto done; 579576a05b35SChris Mason } 579676a05b35SChris Mason 57975cd57b2cSChris Mason if (!path->skip_locking) { 5798bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 5799d42244a0SJan Schmidt if (!ret && time_seq) { 5800d42244a0SJan Schmidt /* 5801d42244a0SJan Schmidt * If we don't get the lock, we may be racing 5802d42244a0SJan Schmidt * with push_leaf_left, holding that lock while 5803d42244a0SJan Schmidt * itself waiting for the leaf we've currently 5804d42244a0SJan Schmidt * locked. To solve this situation, we give up 5805d42244a0SJan Schmidt * on our lock and cycle. 5806d42244a0SJan Schmidt */ 5807cf538830SJan Schmidt free_extent_buffer(next); 5808d42244a0SJan Schmidt btrfs_release_path(path); 5809d42244a0SJan Schmidt cond_resched(); 5810d42244a0SJan Schmidt goto again; 5811d42244a0SJan Schmidt } 58128e73f275SChris Mason if (!ret) { 58138e73f275SChris Mason btrfs_set_path_blocking(path); 5814bd681513SChris Mason btrfs_tree_read_lock(next); 58158e73f275SChris Mason } 5816bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 5817bd681513SChris Mason } 5818d97e63b6SChris Mason break; 5819d97e63b6SChris Mason } 5820d97e63b6SChris Mason path->slots[level] = slot; 5821d97e63b6SChris Mason while (1) { 5822d97e63b6SChris Mason level--; 5823d97e63b6SChris Mason c = path->nodes[level]; 5824925baeddSChris Mason if (path->locks[level]) 5825bd681513SChris Mason btrfs_tree_unlock_rw(c, path->locks[level]); 58268e73f275SChris Mason 58275f39d397SChris Mason free_extent_buffer(c); 5828d97e63b6SChris Mason path->nodes[level] = next; 5829d97e63b6SChris Mason path->slots[level] = 0; 5830a74a4b97SChris Mason if (!path->skip_locking) 5831bd681513SChris Mason path->locks[level] = next_rw_lock; 5832d97e63b6SChris Mason if (!level) 5833d97e63b6SChris Mason break; 5834b4ce94deSChris Mason 5835d07b8528SLiu Bo ret = read_block_for_search(root, path, &next, level, 5836cda79c54SDavid Sterba 0, &key); 58378e73f275SChris Mason if (ret == -EAGAIN) 58388e73f275SChris Mason goto again; 58398e73f275SChris Mason 584076a05b35SChris Mason if (ret < 0) { 5841b3b4aa74SDavid Sterba btrfs_release_path(path); 584276a05b35SChris Mason goto done; 584376a05b35SChris Mason } 584476a05b35SChris Mason 58455cd57b2cSChris Mason if (!path->skip_locking) { 5846bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 58478e73f275SChris Mason if (!ret) { 58488e73f275SChris Mason btrfs_set_path_blocking(path); 5849bd681513SChris Mason btrfs_tree_read_lock(next); 58508e73f275SChris Mason } 5851bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 5852bd681513SChris Mason } 5853d97e63b6SChris Mason } 58548e73f275SChris Mason ret = 0; 5855925baeddSChris Mason done: 5856f7c79f30SChris Mason unlock_up(path, 0, 1, 0, NULL); 58578e73f275SChris Mason path->leave_spinning = old_spinning; 58588e73f275SChris Mason if (!old_spinning) 58598e73f275SChris Mason btrfs_set_path_blocking(path); 58608e73f275SChris Mason 58618e73f275SChris Mason return ret; 5862d97e63b6SChris Mason } 58630b86a832SChris Mason 58643f157a2fSChris Mason /* 58653f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 58663f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 58673f157a2fSChris Mason * 58683f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 58693f157a2fSChris Mason */ 58700b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 58710b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 58720b86a832SChris Mason int type) 58730b86a832SChris Mason { 58740b86a832SChris Mason struct btrfs_key found_key; 58750b86a832SChris Mason struct extent_buffer *leaf; 5876e02119d5SChris Mason u32 nritems; 58770b86a832SChris Mason int ret; 58780b86a832SChris Mason 58790b86a832SChris Mason while (1) { 58800b86a832SChris Mason if (path->slots[0] == 0) { 5881b4ce94deSChris Mason btrfs_set_path_blocking(path); 58820b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 58830b86a832SChris Mason if (ret != 0) 58840b86a832SChris Mason return ret; 58850b86a832SChris Mason } else { 58860b86a832SChris Mason path->slots[0]--; 58870b86a832SChris Mason } 58880b86a832SChris Mason leaf = path->nodes[0]; 5889e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 5890e02119d5SChris Mason if (nritems == 0) 5891e02119d5SChris Mason return 1; 5892e02119d5SChris Mason if (path->slots[0] == nritems) 5893e02119d5SChris Mason path->slots[0]--; 5894e02119d5SChris Mason 58950b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 5896e02119d5SChris Mason if (found_key.objectid < min_objectid) 5897e02119d5SChris Mason break; 58980a4eefbbSYan Zheng if (found_key.type == type) 58990a4eefbbSYan Zheng return 0; 5900e02119d5SChris Mason if (found_key.objectid == min_objectid && 5901e02119d5SChris Mason found_key.type < type) 5902e02119d5SChris Mason break; 59030b86a832SChris Mason } 59040b86a832SChris Mason return 1; 59050b86a832SChris Mason } 5906ade2e0b3SWang Shilong 5907ade2e0b3SWang Shilong /* 5908ade2e0b3SWang Shilong * search in extent tree to find a previous Metadata/Data extent item with 5909ade2e0b3SWang Shilong * min objecitd. 5910ade2e0b3SWang Shilong * 5911ade2e0b3SWang Shilong * returns 0 if something is found, 1 if nothing was found and < 0 on error 5912ade2e0b3SWang Shilong */ 5913ade2e0b3SWang Shilong int btrfs_previous_extent_item(struct btrfs_root *root, 5914ade2e0b3SWang Shilong struct btrfs_path *path, u64 min_objectid) 5915ade2e0b3SWang Shilong { 5916ade2e0b3SWang Shilong struct btrfs_key found_key; 5917ade2e0b3SWang Shilong struct extent_buffer *leaf; 5918ade2e0b3SWang Shilong u32 nritems; 5919ade2e0b3SWang Shilong int ret; 5920ade2e0b3SWang Shilong 5921ade2e0b3SWang Shilong while (1) { 5922ade2e0b3SWang Shilong if (path->slots[0] == 0) { 5923ade2e0b3SWang Shilong btrfs_set_path_blocking(path); 5924ade2e0b3SWang Shilong ret = btrfs_prev_leaf(root, path); 5925ade2e0b3SWang Shilong if (ret != 0) 5926ade2e0b3SWang Shilong return ret; 5927ade2e0b3SWang Shilong } else { 5928ade2e0b3SWang Shilong path->slots[0]--; 5929ade2e0b3SWang Shilong } 5930ade2e0b3SWang Shilong leaf = path->nodes[0]; 5931ade2e0b3SWang Shilong nritems = btrfs_header_nritems(leaf); 5932ade2e0b3SWang Shilong if (nritems == 0) 5933ade2e0b3SWang Shilong return 1; 5934ade2e0b3SWang Shilong if (path->slots[0] == nritems) 5935ade2e0b3SWang Shilong path->slots[0]--; 5936ade2e0b3SWang Shilong 5937ade2e0b3SWang Shilong btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 5938ade2e0b3SWang Shilong if (found_key.objectid < min_objectid) 5939ade2e0b3SWang Shilong break; 5940ade2e0b3SWang Shilong if (found_key.type == BTRFS_EXTENT_ITEM_KEY || 5941ade2e0b3SWang Shilong found_key.type == BTRFS_METADATA_ITEM_KEY) 5942ade2e0b3SWang Shilong return 0; 5943ade2e0b3SWang Shilong if (found_key.objectid == min_objectid && 5944ade2e0b3SWang Shilong found_key.type < BTRFS_EXTENT_ITEM_KEY) 5945ade2e0b3SWang Shilong break; 5946ade2e0b3SWang Shilong } 5947ade2e0b3SWang Shilong return 1; 5948ade2e0b3SWang Shilong } 5949