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 btrfs_fs_info *fs_info, 252ff7e61eSJeff Mahoney struct extent_buffer *dst, 26971a1f66SChris Mason struct extent_buffer *src, int empty); 275f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 282ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 295f39d397SChris Mason struct extent_buffer *dst_buf, 305f39d397SChris Mason struct extent_buffer *src_buf); 31afe5fea7STsutomu Itoh static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 32afe5fea7STsutomu Itoh int level, int slot); 33d97e63b6SChris Mason 342c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 352c90e5d6SChris Mason { 36e2c89907SMasahiro Yamada return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 372c90e5d6SChris Mason } 382c90e5d6SChris Mason 39b4ce94deSChris Mason /* 40b4ce94deSChris Mason * set all locked nodes in the path to blocking locks. This should 41b4ce94deSChris Mason * be done before scheduling 42b4ce94deSChris Mason */ 43b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p) 44b4ce94deSChris Mason { 45b4ce94deSChris Mason int i; 46b4ce94deSChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 47bd681513SChris Mason if (!p->nodes[i] || !p->locks[i]) 48bd681513SChris Mason continue; 49766ece54SDavid Sterba /* 50766ece54SDavid Sterba * If we currently have a spinning reader or writer lock this 51766ece54SDavid Sterba * will bump the count of blocking holders and drop the 52766ece54SDavid Sterba * spinlock. 53766ece54SDavid Sterba */ 54766ece54SDavid Sterba if (p->locks[i] == BTRFS_READ_LOCK) { 55766ece54SDavid Sterba btrfs_set_lock_blocking_read(p->nodes[i]); 56bd681513SChris Mason p->locks[i] = BTRFS_READ_LOCK_BLOCKING; 57766ece54SDavid Sterba } else if (p->locks[i] == BTRFS_WRITE_LOCK) { 58766ece54SDavid Sterba btrfs_set_lock_blocking_write(p->nodes[i]); 59bd681513SChris Mason p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; 60b4ce94deSChris Mason } 61b4ce94deSChris Mason } 62766ece54SDavid Sterba } 63b4ce94deSChris Mason 64d352ac68SChris Mason /* this also releases the path */ 652c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 662c90e5d6SChris Mason { 67ff175d57SJesper Juhl if (!p) 68ff175d57SJesper Juhl return; 69b3b4aa74SDavid Sterba btrfs_release_path(p); 702c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 712c90e5d6SChris Mason } 722c90e5d6SChris Mason 73d352ac68SChris Mason /* 74d352ac68SChris Mason * path release drops references on the extent buffers in the path 75d352ac68SChris Mason * and it drops any locks held by this path 76d352ac68SChris Mason * 77d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 78d352ac68SChris Mason */ 79b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p) 80eb60ceacSChris Mason { 81eb60ceacSChris Mason int i; 82a2135011SChris Mason 83234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 843f157a2fSChris Mason p->slots[i] = 0; 85eb60ceacSChris Mason if (!p->nodes[i]) 86925baeddSChris Mason continue; 87925baeddSChris Mason if (p->locks[i]) { 88bd681513SChris Mason btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); 89925baeddSChris Mason p->locks[i] = 0; 90925baeddSChris Mason } 915f39d397SChris Mason free_extent_buffer(p->nodes[i]); 923f157a2fSChris Mason p->nodes[i] = NULL; 93eb60ceacSChris Mason } 94eb60ceacSChris Mason } 95eb60ceacSChris Mason 96d352ac68SChris Mason /* 97d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 98d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 99d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 100d352ac68SChris Mason * looping required. 101d352ac68SChris Mason * 102d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 103d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 104d352ac68SChris Mason * at any time because there are no locks held. 105d352ac68SChris Mason */ 106925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 107925baeddSChris Mason { 108925baeddSChris Mason struct extent_buffer *eb; 109240f62c8SChris Mason 1103083ee2eSJosef Bacik while (1) { 111240f62c8SChris Mason rcu_read_lock(); 112240f62c8SChris Mason eb = rcu_dereference(root->node); 1133083ee2eSJosef Bacik 1143083ee2eSJosef Bacik /* 1153083ee2eSJosef Bacik * RCU really hurts here, we could free up the root node because 11601327610SNicholas D Steeves * it was COWed but we may not get the new root node yet so do 1173083ee2eSJosef Bacik * the inc_not_zero dance and if it doesn't work then 1183083ee2eSJosef Bacik * synchronize_rcu and try again. 1193083ee2eSJosef Bacik */ 1203083ee2eSJosef Bacik if (atomic_inc_not_zero(&eb->refs)) { 121240f62c8SChris Mason rcu_read_unlock(); 1223083ee2eSJosef Bacik break; 1233083ee2eSJosef Bacik } 1243083ee2eSJosef Bacik rcu_read_unlock(); 1253083ee2eSJosef Bacik synchronize_rcu(); 1263083ee2eSJosef Bacik } 127925baeddSChris Mason return eb; 128925baeddSChris Mason } 129925baeddSChris Mason 130d352ac68SChris Mason /* loop around taking references on and locking the root node of the 131d352ac68SChris Mason * tree until you end up with a lock on the root. A locked buffer 132d352ac68SChris Mason * is returned, with a reference held. 133d352ac68SChris Mason */ 134925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) 135925baeddSChris Mason { 136925baeddSChris Mason struct extent_buffer *eb; 137925baeddSChris Mason 138925baeddSChris Mason while (1) { 139925baeddSChris Mason eb = btrfs_root_node(root); 140925baeddSChris Mason btrfs_tree_lock(eb); 141240f62c8SChris Mason if (eb == root->node) 142925baeddSChris Mason break; 143925baeddSChris Mason btrfs_tree_unlock(eb); 144925baeddSChris Mason free_extent_buffer(eb); 145925baeddSChris Mason } 146925baeddSChris Mason return eb; 147925baeddSChris Mason } 148925baeddSChris Mason 149bd681513SChris Mason /* loop around taking references on and locking the root node of the 150bd681513SChris Mason * tree until you end up with a lock on the root. A locked buffer 151bd681513SChris Mason * is returned, with a reference held. 152bd681513SChris Mason */ 15384f7d8e6SJosef Bacik struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root) 154bd681513SChris Mason { 155bd681513SChris Mason struct extent_buffer *eb; 156bd681513SChris Mason 157bd681513SChris Mason while (1) { 158bd681513SChris Mason eb = btrfs_root_node(root); 159bd681513SChris Mason btrfs_tree_read_lock(eb); 160bd681513SChris Mason if (eb == root->node) 161bd681513SChris Mason break; 162bd681513SChris Mason btrfs_tree_read_unlock(eb); 163bd681513SChris Mason free_extent_buffer(eb); 164bd681513SChris Mason } 165bd681513SChris Mason return eb; 166bd681513SChris Mason } 167bd681513SChris Mason 168d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get 169d352ac68SChris Mason * put onto a simple dirty list. transaction.c walks this to make sure they 170d352ac68SChris Mason * get properly updated on disk. 171d352ac68SChris Mason */ 1720b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 1730b86a832SChris Mason { 1740b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1750b246afaSJeff Mahoney 176e7070be1SJosef Bacik if (test_bit(BTRFS_ROOT_DIRTY, &root->state) || 177e7070be1SJosef Bacik !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state)) 178e7070be1SJosef Bacik return; 179e7070be1SJosef Bacik 1800b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 181e7070be1SJosef Bacik if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) { 182e7070be1SJosef Bacik /* Want the extent tree to be the last on the list */ 1834fd786e6SMisono Tomohiro if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID) 184e7070be1SJosef Bacik list_move_tail(&root->dirty_list, 1850b246afaSJeff Mahoney &fs_info->dirty_cowonly_roots); 186e7070be1SJosef Bacik else 187e7070be1SJosef Bacik list_move(&root->dirty_list, 1880b246afaSJeff Mahoney &fs_info->dirty_cowonly_roots); 1890b86a832SChris Mason } 1900b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1910b86a832SChris Mason } 1920b86a832SChris Mason 193d352ac68SChris Mason /* 194d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 195d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 196d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 197d352ac68SChris Mason */ 198be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 199be20aa9dSChris Mason struct btrfs_root *root, 200be20aa9dSChris Mason struct extent_buffer *buf, 201be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 202be20aa9dSChris Mason { 2030b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 204be20aa9dSChris Mason struct extent_buffer *cow; 205be20aa9dSChris Mason int ret = 0; 206be20aa9dSChris Mason int level; 2075d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 208be20aa9dSChris Mason 20927cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 2100b246afaSJeff Mahoney trans->transid != fs_info->running_transaction->transid); 21127cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 21227cdeb70SMiao Xie trans->transid != root->last_trans); 213be20aa9dSChris Mason 214be20aa9dSChris Mason level = btrfs_header_level(buf); 2155d4f98a2SYan Zheng if (level == 0) 2165d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 2175d4f98a2SYan Zheng else 2185d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 21931840ae1SZheng Yan 2204d75f8a9SDavid Sterba cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, 2214d75f8a9SDavid Sterba &disk_key, level, buf->start, 0); 2225d4f98a2SYan Zheng if (IS_ERR(cow)) 223be20aa9dSChris Mason return PTR_ERR(cow); 224be20aa9dSChris Mason 22558e8012cSDavid Sterba copy_extent_buffer_full(cow, buf); 226be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 227be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 2285d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 2295d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 2305d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 2315d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 2325d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 2335d4f98a2SYan Zheng else 234be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 235be20aa9dSChris Mason 236de37aa51SNikolay Borisov write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); 2372b82032cSYan Zheng 238be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 2395d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 240e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 2415d4f98a2SYan Zheng else 242e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 2434aec2b52SChris Mason 244be20aa9dSChris Mason if (ret) 245be20aa9dSChris Mason return ret; 246be20aa9dSChris Mason 247be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 248be20aa9dSChris Mason *cow_ret = cow; 249be20aa9dSChris Mason return 0; 250be20aa9dSChris Mason } 251be20aa9dSChris Mason 252bd989ba3SJan Schmidt enum mod_log_op { 253bd989ba3SJan Schmidt MOD_LOG_KEY_REPLACE, 254bd989ba3SJan Schmidt MOD_LOG_KEY_ADD, 255bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE, 256bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_FREEING, 257bd989ba3SJan Schmidt MOD_LOG_KEY_REMOVE_WHILE_MOVING, 258bd989ba3SJan Schmidt MOD_LOG_MOVE_KEYS, 259bd989ba3SJan Schmidt MOD_LOG_ROOT_REPLACE, 260bd989ba3SJan Schmidt }; 261bd989ba3SJan Schmidt 262bd989ba3SJan Schmidt struct tree_mod_root { 263bd989ba3SJan Schmidt u64 logical; 264bd989ba3SJan Schmidt u8 level; 265bd989ba3SJan Schmidt }; 266bd989ba3SJan Schmidt 267bd989ba3SJan Schmidt struct tree_mod_elem { 268bd989ba3SJan Schmidt struct rb_node node; 269298cfd36SChandan Rajendra u64 logical; 270097b8a7cSJan Schmidt u64 seq; 271bd989ba3SJan Schmidt enum mod_log_op op; 272bd989ba3SJan Schmidt 273bd989ba3SJan Schmidt /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ 274bd989ba3SJan Schmidt int slot; 275bd989ba3SJan Schmidt 276bd989ba3SJan Schmidt /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ 277bd989ba3SJan Schmidt u64 generation; 278bd989ba3SJan Schmidt 279bd989ba3SJan Schmidt /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ 280bd989ba3SJan Schmidt struct btrfs_disk_key key; 281bd989ba3SJan Schmidt u64 blockptr; 282bd989ba3SJan Schmidt 283bd989ba3SJan Schmidt /* this is used for op == MOD_LOG_MOVE_KEYS */ 284b6dfa35bSDavid Sterba struct { 285b6dfa35bSDavid Sterba int dst_slot; 286b6dfa35bSDavid Sterba int nr_items; 287b6dfa35bSDavid Sterba } move; 288bd989ba3SJan Schmidt 289bd989ba3SJan Schmidt /* this is used for op == MOD_LOG_ROOT_REPLACE */ 290bd989ba3SJan Schmidt struct tree_mod_root old_root; 291bd989ba3SJan Schmidt }; 292bd989ba3SJan Schmidt 293097b8a7cSJan Schmidt /* 294fcebe456SJosef Bacik * Pull a new tree mod seq number for our operation. 295fc36ed7eSJan Schmidt */ 296fcebe456SJosef Bacik static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info) 297fc36ed7eSJan Schmidt { 298fc36ed7eSJan Schmidt return atomic64_inc_return(&fs_info->tree_mod_seq); 299fc36ed7eSJan Schmidt } 300fc36ed7eSJan Schmidt 301fc36ed7eSJan Schmidt /* 302097b8a7cSJan Schmidt * This adds a new blocker to the tree mod log's blocker list if the @elem 303097b8a7cSJan Schmidt * passed does not already have a sequence number set. So when a caller expects 304097b8a7cSJan Schmidt * to record tree modifications, it should ensure to set elem->seq to zero 305097b8a7cSJan Schmidt * before calling btrfs_get_tree_mod_seq. 306097b8a7cSJan Schmidt * Returns a fresh, unused tree log modification sequence number, even if no new 307097b8a7cSJan Schmidt * blocker was added. 308097b8a7cSJan Schmidt */ 309097b8a7cSJan Schmidt u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, 310bd989ba3SJan Schmidt struct seq_list *elem) 311bd989ba3SJan Schmidt { 312b1a09f1eSDavid Sterba write_lock(&fs_info->tree_mod_log_lock); 313bd989ba3SJan Schmidt spin_lock(&fs_info->tree_mod_seq_lock); 314097b8a7cSJan Schmidt if (!elem->seq) { 315fcebe456SJosef Bacik elem->seq = btrfs_inc_tree_mod_seq(fs_info); 316097b8a7cSJan Schmidt list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); 317097b8a7cSJan Schmidt } 318bd989ba3SJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 319b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 320097b8a7cSJan Schmidt 321fcebe456SJosef Bacik return elem->seq; 322bd989ba3SJan Schmidt } 323bd989ba3SJan Schmidt 324bd989ba3SJan Schmidt void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, 325bd989ba3SJan Schmidt struct seq_list *elem) 326bd989ba3SJan Schmidt { 327bd989ba3SJan Schmidt struct rb_root *tm_root; 328bd989ba3SJan Schmidt struct rb_node *node; 329bd989ba3SJan Schmidt struct rb_node *next; 330bd989ba3SJan Schmidt struct seq_list *cur_elem; 331bd989ba3SJan Schmidt struct tree_mod_elem *tm; 332bd989ba3SJan Schmidt u64 min_seq = (u64)-1; 333bd989ba3SJan Schmidt u64 seq_putting = elem->seq; 334bd989ba3SJan Schmidt 335bd989ba3SJan Schmidt if (!seq_putting) 336bd989ba3SJan Schmidt return; 337bd989ba3SJan Schmidt 338bd989ba3SJan Schmidt spin_lock(&fs_info->tree_mod_seq_lock); 339bd989ba3SJan Schmidt list_del(&elem->list); 340097b8a7cSJan Schmidt elem->seq = 0; 341bd989ba3SJan Schmidt 342bd989ba3SJan Schmidt list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) { 343097b8a7cSJan Schmidt if (cur_elem->seq < min_seq) { 344bd989ba3SJan Schmidt if (seq_putting > cur_elem->seq) { 345bd989ba3SJan Schmidt /* 346bd989ba3SJan Schmidt * blocker with lower sequence number exists, we 347bd989ba3SJan Schmidt * cannot remove anything from the log 348bd989ba3SJan Schmidt */ 349097b8a7cSJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 350097b8a7cSJan Schmidt return; 351bd989ba3SJan Schmidt } 352bd989ba3SJan Schmidt min_seq = cur_elem->seq; 353bd989ba3SJan Schmidt } 354bd989ba3SJan Schmidt } 355097b8a7cSJan Schmidt spin_unlock(&fs_info->tree_mod_seq_lock); 356097b8a7cSJan Schmidt 357097b8a7cSJan Schmidt /* 358bd989ba3SJan Schmidt * anything that's lower than the lowest existing (read: blocked) 359bd989ba3SJan Schmidt * sequence number can be removed from the tree. 360bd989ba3SJan Schmidt */ 361b1a09f1eSDavid Sterba write_lock(&fs_info->tree_mod_log_lock); 362bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 363bd989ba3SJan Schmidt for (node = rb_first(tm_root); node; node = next) { 364bd989ba3SJan Schmidt next = rb_next(node); 3656b4df8b6SGeliang Tang tm = rb_entry(node, struct tree_mod_elem, node); 366097b8a7cSJan Schmidt if (tm->seq > min_seq) 367bd989ba3SJan Schmidt continue; 368bd989ba3SJan Schmidt rb_erase(node, tm_root); 369bd989ba3SJan Schmidt kfree(tm); 370bd989ba3SJan Schmidt } 371b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 372bd989ba3SJan Schmidt } 373bd989ba3SJan Schmidt 374bd989ba3SJan Schmidt /* 375bd989ba3SJan Schmidt * key order of the log: 376298cfd36SChandan Rajendra * node/leaf start address -> sequence 377bd989ba3SJan Schmidt * 378298cfd36SChandan Rajendra * The 'start address' is the logical address of the *new* root node 379298cfd36SChandan Rajendra * for root replace operations, or the logical address of the affected 380298cfd36SChandan Rajendra * block for all other operations. 3815de865eeSFilipe David Borba Manana * 382b1a09f1eSDavid Sterba * Note: must be called with write lock for fs_info::tree_mod_log_lock. 383bd989ba3SJan Schmidt */ 384bd989ba3SJan Schmidt static noinline int 385bd989ba3SJan Schmidt __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) 386bd989ba3SJan Schmidt { 387bd989ba3SJan Schmidt struct rb_root *tm_root; 388bd989ba3SJan Schmidt struct rb_node **new; 389bd989ba3SJan Schmidt struct rb_node *parent = NULL; 390bd989ba3SJan Schmidt struct tree_mod_elem *cur; 391bd989ba3SJan Schmidt 392fcebe456SJosef Bacik tm->seq = btrfs_inc_tree_mod_seq(fs_info); 393bd989ba3SJan Schmidt 394bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 395bd989ba3SJan Schmidt new = &tm_root->rb_node; 396bd989ba3SJan Schmidt while (*new) { 3976b4df8b6SGeliang Tang cur = rb_entry(*new, struct tree_mod_elem, node); 398bd989ba3SJan Schmidt parent = *new; 399298cfd36SChandan Rajendra if (cur->logical < tm->logical) 400bd989ba3SJan Schmidt new = &((*new)->rb_left); 401298cfd36SChandan Rajendra else if (cur->logical > tm->logical) 402bd989ba3SJan Schmidt new = &((*new)->rb_right); 403097b8a7cSJan Schmidt else if (cur->seq < tm->seq) 404bd989ba3SJan Schmidt new = &((*new)->rb_left); 405097b8a7cSJan Schmidt else if (cur->seq > tm->seq) 406bd989ba3SJan Schmidt new = &((*new)->rb_right); 4075de865eeSFilipe David Borba Manana else 4085de865eeSFilipe David Borba Manana return -EEXIST; 409bd989ba3SJan Schmidt } 410bd989ba3SJan Schmidt 411bd989ba3SJan Schmidt rb_link_node(&tm->node, parent, new); 412bd989ba3SJan Schmidt rb_insert_color(&tm->node, tm_root); 4135de865eeSFilipe David Borba Manana return 0; 414bd989ba3SJan Schmidt } 415bd989ba3SJan Schmidt 416097b8a7cSJan Schmidt /* 417097b8a7cSJan Schmidt * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it 418097b8a7cSJan Schmidt * returns zero with the tree_mod_log_lock acquired. The caller must hold 419097b8a7cSJan Schmidt * this until all tree mod log insertions are recorded in the rb tree and then 420b1a09f1eSDavid Sterba * write unlock fs_info::tree_mod_log_lock. 421097b8a7cSJan Schmidt */ 422e9b7fd4dSJan Schmidt static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, 423e9b7fd4dSJan Schmidt struct extent_buffer *eb) { 424e9b7fd4dSJan Schmidt smp_mb(); 425e9b7fd4dSJan Schmidt if (list_empty(&(fs_info)->tree_mod_seq_list)) 426e9b7fd4dSJan Schmidt return 1; 427097b8a7cSJan Schmidt if (eb && btrfs_header_level(eb) == 0) 428e9b7fd4dSJan Schmidt return 1; 4295de865eeSFilipe David Borba Manana 430b1a09f1eSDavid Sterba write_lock(&fs_info->tree_mod_log_lock); 4315de865eeSFilipe David Borba Manana if (list_empty(&(fs_info)->tree_mod_seq_list)) { 432b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 4335de865eeSFilipe David Borba Manana return 1; 4345de865eeSFilipe David Borba Manana } 4355de865eeSFilipe David Borba Manana 436e9b7fd4dSJan Schmidt return 0; 437e9b7fd4dSJan Schmidt } 438e9b7fd4dSJan Schmidt 4395de865eeSFilipe David Borba Manana /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */ 4405de865eeSFilipe David Borba Manana static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info, 4415de865eeSFilipe David Borba Manana struct extent_buffer *eb) 4425de865eeSFilipe David Borba Manana { 4435de865eeSFilipe David Borba Manana smp_mb(); 4445de865eeSFilipe David Borba Manana if (list_empty(&(fs_info)->tree_mod_seq_list)) 4455de865eeSFilipe David Borba Manana return 0; 4465de865eeSFilipe David Borba Manana if (eb && btrfs_header_level(eb) == 0) 4475de865eeSFilipe David Borba Manana return 0; 4485de865eeSFilipe David Borba Manana 4495de865eeSFilipe David Borba Manana return 1; 4505de865eeSFilipe David Borba Manana } 4515de865eeSFilipe David Borba Manana 4525de865eeSFilipe David Borba Manana static struct tree_mod_elem * 4535de865eeSFilipe David Borba Manana alloc_tree_mod_elem(struct extent_buffer *eb, int slot, 454bd989ba3SJan Schmidt enum mod_log_op op, gfp_t flags) 455bd989ba3SJan Schmidt { 456097b8a7cSJan Schmidt struct tree_mod_elem *tm; 457bd989ba3SJan Schmidt 458c8cc6341SJosef Bacik tm = kzalloc(sizeof(*tm), flags); 459c8cc6341SJosef Bacik if (!tm) 4605de865eeSFilipe David Borba Manana return NULL; 461bd989ba3SJan Schmidt 462298cfd36SChandan Rajendra tm->logical = eb->start; 463bd989ba3SJan Schmidt if (op != MOD_LOG_KEY_ADD) { 464bd989ba3SJan Schmidt btrfs_node_key(eb, &tm->key, slot); 465bd989ba3SJan Schmidt tm->blockptr = btrfs_node_blockptr(eb, slot); 466bd989ba3SJan Schmidt } 467bd989ba3SJan Schmidt tm->op = op; 468bd989ba3SJan Schmidt tm->slot = slot; 469bd989ba3SJan Schmidt tm->generation = btrfs_node_ptr_generation(eb, slot); 4705de865eeSFilipe David Borba Manana RB_CLEAR_NODE(&tm->node); 471bd989ba3SJan Schmidt 4725de865eeSFilipe David Borba Manana return tm; 473097b8a7cSJan Schmidt } 474097b8a7cSJan Schmidt 475e09c2efeSDavid Sterba static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot, 476097b8a7cSJan Schmidt enum mod_log_op op, gfp_t flags) 477097b8a7cSJan Schmidt { 4785de865eeSFilipe David Borba Manana struct tree_mod_elem *tm; 4795de865eeSFilipe David Borba Manana int ret; 4805de865eeSFilipe David Borba Manana 481e09c2efeSDavid Sterba if (!tree_mod_need_log(eb->fs_info, eb)) 482097b8a7cSJan Schmidt return 0; 483097b8a7cSJan Schmidt 4845de865eeSFilipe David Borba Manana tm = alloc_tree_mod_elem(eb, slot, op, flags); 4855de865eeSFilipe David Borba Manana if (!tm) 4865de865eeSFilipe David Borba Manana return -ENOMEM; 4875de865eeSFilipe David Borba Manana 488e09c2efeSDavid Sterba if (tree_mod_dont_log(eb->fs_info, eb)) { 4895de865eeSFilipe David Borba Manana kfree(tm); 4905de865eeSFilipe David Borba Manana return 0; 4915de865eeSFilipe David Borba Manana } 4925de865eeSFilipe David Borba Manana 493e09c2efeSDavid Sterba ret = __tree_mod_log_insert(eb->fs_info, tm); 494b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 4955de865eeSFilipe David Borba Manana if (ret) 4965de865eeSFilipe David Borba Manana kfree(tm); 4975de865eeSFilipe David Borba Manana 4985de865eeSFilipe David Borba Manana return ret; 499097b8a7cSJan Schmidt } 500097b8a7cSJan Schmidt 5016074d45fSDavid Sterba static noinline int tree_mod_log_insert_move(struct extent_buffer *eb, 5026074d45fSDavid Sterba int dst_slot, int src_slot, int nr_items) 503bd989ba3SJan Schmidt { 5045de865eeSFilipe David Borba Manana struct tree_mod_elem *tm = NULL; 5055de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 5065de865eeSFilipe David Borba Manana int ret = 0; 507bd989ba3SJan Schmidt int i; 5085de865eeSFilipe David Borba Manana int locked = 0; 509bd989ba3SJan Schmidt 5106074d45fSDavid Sterba if (!tree_mod_need_log(eb->fs_info, eb)) 511f395694cSJan Schmidt return 0; 512bd989ba3SJan Schmidt 513176ef8f5SDavid Sterba tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS); 5145de865eeSFilipe David Borba Manana if (!tm_list) 5155de865eeSFilipe David Borba Manana return -ENOMEM; 516bd989ba3SJan Schmidt 517176ef8f5SDavid Sterba tm = kzalloc(sizeof(*tm), GFP_NOFS); 5185de865eeSFilipe David Borba Manana if (!tm) { 5195de865eeSFilipe David Borba Manana ret = -ENOMEM; 5205de865eeSFilipe David Borba Manana goto free_tms; 5215de865eeSFilipe David Borba Manana } 522f395694cSJan Schmidt 523298cfd36SChandan Rajendra tm->logical = eb->start; 524bd989ba3SJan Schmidt tm->slot = src_slot; 525bd989ba3SJan Schmidt tm->move.dst_slot = dst_slot; 526bd989ba3SJan Schmidt tm->move.nr_items = nr_items; 527bd989ba3SJan Schmidt tm->op = MOD_LOG_MOVE_KEYS; 528bd989ba3SJan Schmidt 5295de865eeSFilipe David Borba Manana for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { 5305de865eeSFilipe David Borba Manana tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot, 531176ef8f5SDavid Sterba MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); 5325de865eeSFilipe David Borba Manana if (!tm_list[i]) { 5335de865eeSFilipe David Borba Manana ret = -ENOMEM; 5345de865eeSFilipe David Borba Manana goto free_tms; 5355de865eeSFilipe David Borba Manana } 536bd989ba3SJan Schmidt } 537bd989ba3SJan Schmidt 5386074d45fSDavid Sterba if (tree_mod_dont_log(eb->fs_info, eb)) 5395de865eeSFilipe David Borba Manana goto free_tms; 5405de865eeSFilipe David Borba Manana locked = 1; 5415de865eeSFilipe David Borba Manana 5425de865eeSFilipe David Borba Manana /* 5435de865eeSFilipe David Borba Manana * When we override something during the move, we log these removals. 5445de865eeSFilipe David Borba Manana * This can only happen when we move towards the beginning of the 5455de865eeSFilipe David Borba Manana * buffer, i.e. dst_slot < src_slot. 5465de865eeSFilipe David Borba Manana */ 5475de865eeSFilipe David Borba Manana for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { 5486074d45fSDavid Sterba ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]); 5495de865eeSFilipe David Borba Manana if (ret) 5505de865eeSFilipe David Borba Manana goto free_tms; 5515de865eeSFilipe David Borba Manana } 5525de865eeSFilipe David Borba Manana 5536074d45fSDavid Sterba ret = __tree_mod_log_insert(eb->fs_info, tm); 5545de865eeSFilipe David Borba Manana if (ret) 5555de865eeSFilipe David Borba Manana goto free_tms; 556b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 5575de865eeSFilipe David Borba Manana kfree(tm_list); 5585de865eeSFilipe David Borba Manana 5595de865eeSFilipe David Borba Manana return 0; 5605de865eeSFilipe David Borba Manana free_tms: 5615de865eeSFilipe David Borba Manana for (i = 0; i < nr_items; i++) { 5625de865eeSFilipe David Borba Manana if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) 5636074d45fSDavid Sterba rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log); 5645de865eeSFilipe David Borba Manana kfree(tm_list[i]); 5655de865eeSFilipe David Borba Manana } 5665de865eeSFilipe David Borba Manana if (locked) 567b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 5685de865eeSFilipe David Borba Manana kfree(tm_list); 5695de865eeSFilipe David Borba Manana kfree(tm); 5705de865eeSFilipe David Borba Manana 5715de865eeSFilipe David Borba Manana return ret; 5725de865eeSFilipe David Borba Manana } 5735de865eeSFilipe David Borba Manana 5745de865eeSFilipe David Borba Manana static inline int 5755de865eeSFilipe David Borba Manana __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, 5765de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list, 5775de865eeSFilipe David Borba Manana int nritems) 578097b8a7cSJan Schmidt { 5795de865eeSFilipe David Borba Manana int i, j; 580097b8a7cSJan Schmidt int ret; 581097b8a7cSJan Schmidt 582097b8a7cSJan Schmidt for (i = nritems - 1; i >= 0; i--) { 5835de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm_list[i]); 5845de865eeSFilipe David Borba Manana if (ret) { 5855de865eeSFilipe David Borba Manana for (j = nritems - 1; j > i; j--) 5865de865eeSFilipe David Borba Manana rb_erase(&tm_list[j]->node, 5875de865eeSFilipe David Borba Manana &fs_info->tree_mod_log); 5885de865eeSFilipe David Borba Manana return ret; 589097b8a7cSJan Schmidt } 590097b8a7cSJan Schmidt } 591097b8a7cSJan Schmidt 5925de865eeSFilipe David Borba Manana return 0; 5935de865eeSFilipe David Borba Manana } 5945de865eeSFilipe David Borba Manana 59595b757c1SDavid Sterba static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root, 59695b757c1SDavid Sterba struct extent_buffer *new_root, int log_removal) 597bd989ba3SJan Schmidt { 59895b757c1SDavid Sterba struct btrfs_fs_info *fs_info = old_root->fs_info; 5995de865eeSFilipe David Borba Manana struct tree_mod_elem *tm = NULL; 6005de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 6015de865eeSFilipe David Borba Manana int nritems = 0; 6025de865eeSFilipe David Borba Manana int ret = 0; 6035de865eeSFilipe David Borba Manana int i; 604bd989ba3SJan Schmidt 6055de865eeSFilipe David Borba Manana if (!tree_mod_need_log(fs_info, NULL)) 606097b8a7cSJan Schmidt return 0; 607097b8a7cSJan Schmidt 6085de865eeSFilipe David Borba Manana if (log_removal && btrfs_header_level(old_root) > 0) { 6095de865eeSFilipe David Borba Manana nritems = btrfs_header_nritems(old_root); 61031e818feSDavid Sterba tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), 611bcc8e07fSDavid Sterba GFP_NOFS); 6125de865eeSFilipe David Borba Manana if (!tm_list) { 6135de865eeSFilipe David Borba Manana ret = -ENOMEM; 6145de865eeSFilipe David Borba Manana goto free_tms; 6155de865eeSFilipe David Borba Manana } 6165de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) { 6175de865eeSFilipe David Borba Manana tm_list[i] = alloc_tree_mod_elem(old_root, i, 618bcc8e07fSDavid Sterba MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); 6195de865eeSFilipe David Borba Manana if (!tm_list[i]) { 6205de865eeSFilipe David Borba Manana ret = -ENOMEM; 6215de865eeSFilipe David Borba Manana goto free_tms; 6225de865eeSFilipe David Borba Manana } 6235de865eeSFilipe David Borba Manana } 6245de865eeSFilipe David Borba Manana } 625d9abbf1cSJan Schmidt 626bcc8e07fSDavid Sterba tm = kzalloc(sizeof(*tm), GFP_NOFS); 6275de865eeSFilipe David Borba Manana if (!tm) { 6285de865eeSFilipe David Borba Manana ret = -ENOMEM; 6295de865eeSFilipe David Borba Manana goto free_tms; 6305de865eeSFilipe David Borba Manana } 631bd989ba3SJan Schmidt 632298cfd36SChandan Rajendra tm->logical = new_root->start; 633bd989ba3SJan Schmidt tm->old_root.logical = old_root->start; 634bd989ba3SJan Schmidt tm->old_root.level = btrfs_header_level(old_root); 635bd989ba3SJan Schmidt tm->generation = btrfs_header_generation(old_root); 636bd989ba3SJan Schmidt tm->op = MOD_LOG_ROOT_REPLACE; 637bd989ba3SJan Schmidt 6385de865eeSFilipe David Borba Manana if (tree_mod_dont_log(fs_info, NULL)) 6395de865eeSFilipe David Borba Manana goto free_tms; 6405de865eeSFilipe David Borba Manana 6415de865eeSFilipe David Borba Manana if (tm_list) 6425de865eeSFilipe David Borba Manana ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems); 6435de865eeSFilipe David Borba Manana if (!ret) 6445de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm); 6455de865eeSFilipe David Borba Manana 646b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 6475de865eeSFilipe David Borba Manana if (ret) 6485de865eeSFilipe David Borba Manana goto free_tms; 6495de865eeSFilipe David Borba Manana kfree(tm_list); 6505de865eeSFilipe David Borba Manana 6515de865eeSFilipe David Borba Manana return ret; 6525de865eeSFilipe David Borba Manana 6535de865eeSFilipe David Borba Manana free_tms: 6545de865eeSFilipe David Borba Manana if (tm_list) { 6555de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) 6565de865eeSFilipe David Borba Manana kfree(tm_list[i]); 6575de865eeSFilipe David Borba Manana kfree(tm_list); 6585de865eeSFilipe David Borba Manana } 6595de865eeSFilipe David Borba Manana kfree(tm); 6605de865eeSFilipe David Borba Manana 6615de865eeSFilipe David Borba Manana return ret; 662bd989ba3SJan Schmidt } 663bd989ba3SJan Schmidt 664bd989ba3SJan Schmidt static struct tree_mod_elem * 665bd989ba3SJan Schmidt __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, 666bd989ba3SJan Schmidt int smallest) 667bd989ba3SJan Schmidt { 668bd989ba3SJan Schmidt struct rb_root *tm_root; 669bd989ba3SJan Schmidt struct rb_node *node; 670bd989ba3SJan Schmidt struct tree_mod_elem *cur = NULL; 671bd989ba3SJan Schmidt struct tree_mod_elem *found = NULL; 672bd989ba3SJan Schmidt 673b1a09f1eSDavid Sterba read_lock(&fs_info->tree_mod_log_lock); 674bd989ba3SJan Schmidt tm_root = &fs_info->tree_mod_log; 675bd989ba3SJan Schmidt node = tm_root->rb_node; 676bd989ba3SJan Schmidt while (node) { 6776b4df8b6SGeliang Tang cur = rb_entry(node, struct tree_mod_elem, node); 678298cfd36SChandan Rajendra if (cur->logical < start) { 679bd989ba3SJan Schmidt node = node->rb_left; 680298cfd36SChandan Rajendra } else if (cur->logical > start) { 681bd989ba3SJan Schmidt node = node->rb_right; 682097b8a7cSJan Schmidt } else if (cur->seq < min_seq) { 683bd989ba3SJan Schmidt node = node->rb_left; 684bd989ba3SJan Schmidt } else if (!smallest) { 685bd989ba3SJan Schmidt /* we want the node with the highest seq */ 686bd989ba3SJan Schmidt if (found) 687097b8a7cSJan Schmidt BUG_ON(found->seq > cur->seq); 688bd989ba3SJan Schmidt found = cur; 689bd989ba3SJan Schmidt node = node->rb_left; 690097b8a7cSJan Schmidt } else if (cur->seq > min_seq) { 691bd989ba3SJan Schmidt /* we want the node with the smallest seq */ 692bd989ba3SJan Schmidt if (found) 693097b8a7cSJan Schmidt BUG_ON(found->seq < cur->seq); 694bd989ba3SJan Schmidt found = cur; 695bd989ba3SJan Schmidt node = node->rb_right; 696bd989ba3SJan Schmidt } else { 697bd989ba3SJan Schmidt found = cur; 698bd989ba3SJan Schmidt break; 699bd989ba3SJan Schmidt } 700bd989ba3SJan Schmidt } 701b1a09f1eSDavid Sterba read_unlock(&fs_info->tree_mod_log_lock); 702bd989ba3SJan Schmidt 703bd989ba3SJan Schmidt return found; 704bd989ba3SJan Schmidt } 705bd989ba3SJan Schmidt 706bd989ba3SJan Schmidt /* 707bd989ba3SJan Schmidt * this returns the element from the log with the smallest time sequence 708bd989ba3SJan Schmidt * value that's in the log (the oldest log item). any element with a time 709bd989ba3SJan Schmidt * sequence lower than min_seq will be ignored. 710bd989ba3SJan Schmidt */ 711bd989ba3SJan Schmidt static struct tree_mod_elem * 712bd989ba3SJan Schmidt tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, 713bd989ba3SJan Schmidt u64 min_seq) 714bd989ba3SJan Schmidt { 715bd989ba3SJan Schmidt return __tree_mod_log_search(fs_info, start, min_seq, 1); 716bd989ba3SJan Schmidt } 717bd989ba3SJan Schmidt 718bd989ba3SJan Schmidt /* 719bd989ba3SJan Schmidt * this returns the element from the log with the largest time sequence 720bd989ba3SJan Schmidt * value that's in the log (the most recent log item). any element with 721bd989ba3SJan Schmidt * a time sequence lower than min_seq will be ignored. 722bd989ba3SJan Schmidt */ 723bd989ba3SJan Schmidt static struct tree_mod_elem * 724bd989ba3SJan Schmidt tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) 725bd989ba3SJan Schmidt { 726bd989ba3SJan Schmidt return __tree_mod_log_search(fs_info, start, min_seq, 0); 727bd989ba3SJan Schmidt } 728bd989ba3SJan Schmidt 729ed874f0dSDavid Sterba static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst, 730bd989ba3SJan Schmidt struct extent_buffer *src, unsigned long dst_offset, 73190f8d62eSJan Schmidt unsigned long src_offset, int nr_items) 732bd989ba3SJan Schmidt { 733ed874f0dSDavid Sterba struct btrfs_fs_info *fs_info = dst->fs_info; 7345de865eeSFilipe David Borba Manana int ret = 0; 7355de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 7365de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list_add, **tm_list_rem; 737bd989ba3SJan Schmidt int i; 7385de865eeSFilipe David Borba Manana int locked = 0; 739bd989ba3SJan Schmidt 7405de865eeSFilipe David Borba Manana if (!tree_mod_need_log(fs_info, NULL)) 7415de865eeSFilipe David Borba Manana return 0; 742bd989ba3SJan Schmidt 743c8cc6341SJosef Bacik if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) 7445de865eeSFilipe David Borba Manana return 0; 7455de865eeSFilipe David Borba Manana 74631e818feSDavid Sterba tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *), 7475de865eeSFilipe David Borba Manana GFP_NOFS); 7485de865eeSFilipe David Borba Manana if (!tm_list) 7495de865eeSFilipe David Borba Manana return -ENOMEM; 7505de865eeSFilipe David Borba Manana 7515de865eeSFilipe David Borba Manana tm_list_add = tm_list; 7525de865eeSFilipe David Borba Manana tm_list_rem = tm_list + nr_items; 7535de865eeSFilipe David Borba Manana for (i = 0; i < nr_items; i++) { 7545de865eeSFilipe David Borba Manana tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset, 7555de865eeSFilipe David Borba Manana MOD_LOG_KEY_REMOVE, GFP_NOFS); 7565de865eeSFilipe David Borba Manana if (!tm_list_rem[i]) { 7575de865eeSFilipe David Borba Manana ret = -ENOMEM; 7585de865eeSFilipe David Borba Manana goto free_tms; 7595de865eeSFilipe David Borba Manana } 7605de865eeSFilipe David Borba Manana 7615de865eeSFilipe David Borba Manana tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset, 7625de865eeSFilipe David Borba Manana MOD_LOG_KEY_ADD, GFP_NOFS); 7635de865eeSFilipe David Borba Manana if (!tm_list_add[i]) { 7645de865eeSFilipe David Borba Manana ret = -ENOMEM; 7655de865eeSFilipe David Borba Manana goto free_tms; 7665de865eeSFilipe David Borba Manana } 7675de865eeSFilipe David Borba Manana } 7685de865eeSFilipe David Borba Manana 7695de865eeSFilipe David Borba Manana if (tree_mod_dont_log(fs_info, NULL)) 7705de865eeSFilipe David Borba Manana goto free_tms; 7715de865eeSFilipe David Borba Manana locked = 1; 772bd989ba3SJan Schmidt 773bd989ba3SJan Schmidt for (i = 0; i < nr_items; i++) { 7745de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]); 7755de865eeSFilipe David Borba Manana if (ret) 7765de865eeSFilipe David Borba Manana goto free_tms; 7775de865eeSFilipe David Borba Manana ret = __tree_mod_log_insert(fs_info, tm_list_add[i]); 7785de865eeSFilipe David Borba Manana if (ret) 7795de865eeSFilipe David Borba Manana goto free_tms; 780bd989ba3SJan Schmidt } 7815de865eeSFilipe David Borba Manana 782b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 7835de865eeSFilipe David Borba Manana kfree(tm_list); 7845de865eeSFilipe David Borba Manana 7855de865eeSFilipe David Borba Manana return 0; 7865de865eeSFilipe David Borba Manana 7875de865eeSFilipe David Borba Manana free_tms: 7885de865eeSFilipe David Borba Manana for (i = 0; i < nr_items * 2; i++) { 7895de865eeSFilipe David Borba Manana if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) 7905de865eeSFilipe David Borba Manana rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log); 7915de865eeSFilipe David Borba Manana kfree(tm_list[i]); 7925de865eeSFilipe David Borba Manana } 7935de865eeSFilipe David Borba Manana if (locked) 794b1a09f1eSDavid Sterba write_unlock(&fs_info->tree_mod_log_lock); 7955de865eeSFilipe David Borba Manana kfree(tm_list); 7965de865eeSFilipe David Borba Manana 7975de865eeSFilipe David Borba Manana return ret; 798bd989ba3SJan Schmidt } 799bd989ba3SJan Schmidt 800db7279a2SDavid Sterba static noinline int tree_mod_log_free_eb(struct extent_buffer *eb) 801bd989ba3SJan Schmidt { 8025de865eeSFilipe David Borba Manana struct tree_mod_elem **tm_list = NULL; 8035de865eeSFilipe David Borba Manana int nritems = 0; 8045de865eeSFilipe David Borba Manana int i; 8055de865eeSFilipe David Borba Manana int ret = 0; 8065de865eeSFilipe David Borba Manana 8075de865eeSFilipe David Borba Manana if (btrfs_header_level(eb) == 0) 8085de865eeSFilipe David Borba Manana return 0; 8095de865eeSFilipe David Borba Manana 810db7279a2SDavid Sterba if (!tree_mod_need_log(eb->fs_info, NULL)) 8115de865eeSFilipe David Borba Manana return 0; 8125de865eeSFilipe David Borba Manana 8135de865eeSFilipe David Borba Manana nritems = btrfs_header_nritems(eb); 81431e818feSDavid Sterba tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS); 8155de865eeSFilipe David Borba Manana if (!tm_list) 8165de865eeSFilipe David Borba Manana return -ENOMEM; 8175de865eeSFilipe David Borba Manana 8185de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) { 8195de865eeSFilipe David Borba Manana tm_list[i] = alloc_tree_mod_elem(eb, i, 8205de865eeSFilipe David Borba Manana MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); 8215de865eeSFilipe David Borba Manana if (!tm_list[i]) { 8225de865eeSFilipe David Borba Manana ret = -ENOMEM; 8235de865eeSFilipe David Borba Manana goto free_tms; 8245de865eeSFilipe David Borba Manana } 8255de865eeSFilipe David Borba Manana } 8265de865eeSFilipe David Borba Manana 827db7279a2SDavid Sterba if (tree_mod_dont_log(eb->fs_info, eb)) 8285de865eeSFilipe David Borba Manana goto free_tms; 8295de865eeSFilipe David Borba Manana 830db7279a2SDavid Sterba ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems); 831b1a09f1eSDavid Sterba write_unlock(&eb->fs_info->tree_mod_log_lock); 8325de865eeSFilipe David Borba Manana if (ret) 8335de865eeSFilipe David Borba Manana goto free_tms; 8345de865eeSFilipe David Borba Manana kfree(tm_list); 8355de865eeSFilipe David Borba Manana 8365de865eeSFilipe David Borba Manana return 0; 8375de865eeSFilipe David Borba Manana 8385de865eeSFilipe David Borba Manana free_tms: 8395de865eeSFilipe David Borba Manana for (i = 0; i < nritems; i++) 8405de865eeSFilipe David Borba Manana kfree(tm_list[i]); 8415de865eeSFilipe David Borba Manana kfree(tm_list); 8425de865eeSFilipe David Borba Manana 8435de865eeSFilipe David Borba Manana return ret; 844bd989ba3SJan Schmidt } 845bd989ba3SJan Schmidt 846d352ac68SChris Mason /* 8475d4f98a2SYan Zheng * check if the tree block can be shared by multiple trees 8485d4f98a2SYan Zheng */ 8495d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root, 8505d4f98a2SYan Zheng struct extent_buffer *buf) 8515d4f98a2SYan Zheng { 8525d4f98a2SYan Zheng /* 85301327610SNicholas D Steeves * Tree blocks not in reference counted trees and tree roots 8545d4f98a2SYan Zheng * are never shared. If a block was allocated after the last 8555d4f98a2SYan Zheng * snapshot and the block was not allocated by tree relocation, 8565d4f98a2SYan Zheng * we know the block is not shared. 8575d4f98a2SYan Zheng */ 85827cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 8595d4f98a2SYan Zheng buf != root->node && buf != root->commit_root && 8605d4f98a2SYan Zheng (btrfs_header_generation(buf) <= 8615d4f98a2SYan Zheng btrfs_root_last_snapshot(&root->root_item) || 8625d4f98a2SYan Zheng btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) 8635d4f98a2SYan Zheng return 1; 864a79865c6SNikolay Borisov 8655d4f98a2SYan Zheng return 0; 8665d4f98a2SYan Zheng } 8675d4f98a2SYan Zheng 8685d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, 8695d4f98a2SYan Zheng struct btrfs_root *root, 8705d4f98a2SYan Zheng struct extent_buffer *buf, 871f0486c68SYan, Zheng struct extent_buffer *cow, 872f0486c68SYan, Zheng int *last_ref) 8735d4f98a2SYan Zheng { 8740b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 8755d4f98a2SYan Zheng u64 refs; 8765d4f98a2SYan Zheng u64 owner; 8775d4f98a2SYan Zheng u64 flags; 8785d4f98a2SYan Zheng u64 new_flags = 0; 8795d4f98a2SYan Zheng int ret; 8805d4f98a2SYan Zheng 8815d4f98a2SYan Zheng /* 8825d4f98a2SYan Zheng * Backrefs update rules: 8835d4f98a2SYan Zheng * 8845d4f98a2SYan Zheng * Always use full backrefs for extent pointers in tree block 8855d4f98a2SYan Zheng * allocated by tree relocation. 8865d4f98a2SYan Zheng * 8875d4f98a2SYan Zheng * If a shared tree block is no longer referenced by its owner 8885d4f98a2SYan Zheng * tree (btrfs_header_owner(buf) == root->root_key.objectid), 8895d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 8905d4f98a2SYan Zheng * 8915d4f98a2SYan Zheng * If a tree block is been relocating 8925d4f98a2SYan Zheng * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), 8935d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 8945d4f98a2SYan Zheng * The reason for this is some operations (such as drop tree) 8955d4f98a2SYan Zheng * are only allowed for blocks use full backrefs. 8965d4f98a2SYan Zheng */ 8975d4f98a2SYan Zheng 8985d4f98a2SYan Zheng if (btrfs_block_can_be_shared(root, buf)) { 8992ff7e61eSJeff Mahoney ret = btrfs_lookup_extent_info(trans, fs_info, buf->start, 9003173a18fSJosef Bacik btrfs_header_level(buf), 1, 9013173a18fSJosef Bacik &refs, &flags); 902be1a5564SMark Fasheh if (ret) 903be1a5564SMark Fasheh return ret; 904e5df9573SMark Fasheh if (refs == 0) { 905e5df9573SMark Fasheh ret = -EROFS; 9060b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, NULL); 907e5df9573SMark Fasheh return ret; 908e5df9573SMark Fasheh } 9095d4f98a2SYan Zheng } else { 9105d4f98a2SYan Zheng refs = 1; 9115d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 9125d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 9135d4f98a2SYan Zheng flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; 9145d4f98a2SYan Zheng else 9155d4f98a2SYan Zheng flags = 0; 9165d4f98a2SYan Zheng } 9175d4f98a2SYan Zheng 9185d4f98a2SYan Zheng owner = btrfs_header_owner(buf); 9195d4f98a2SYan Zheng BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && 9205d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); 9215d4f98a2SYan Zheng 9225d4f98a2SYan Zheng if (refs > 1) { 9235d4f98a2SYan Zheng if ((owner == root->root_key.objectid || 9245d4f98a2SYan Zheng root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && 9255d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 926e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, buf, 1); 927692826b2SJeff Mahoney if (ret) 928692826b2SJeff Mahoney return ret; 9295d4f98a2SYan Zheng 9305d4f98a2SYan Zheng if (root->root_key.objectid == 9315d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 932e339a6b0SJosef Bacik ret = btrfs_dec_ref(trans, root, buf, 0); 933692826b2SJeff Mahoney if (ret) 934692826b2SJeff Mahoney return ret; 935e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 936692826b2SJeff Mahoney if (ret) 937692826b2SJeff Mahoney return ret; 9385d4f98a2SYan Zheng } 9395d4f98a2SYan Zheng new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; 9405d4f98a2SYan Zheng } else { 9415d4f98a2SYan Zheng 9425d4f98a2SYan Zheng if (root->root_key.objectid == 9435d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 944e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 9455d4f98a2SYan Zheng else 946e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 947692826b2SJeff Mahoney if (ret) 948692826b2SJeff Mahoney return ret; 9495d4f98a2SYan Zheng } 9505d4f98a2SYan Zheng if (new_flags != 0) { 951b1c79e09SJosef Bacik int level = btrfs_header_level(buf); 952b1c79e09SJosef Bacik 9532ff7e61eSJeff Mahoney ret = btrfs_set_disk_extent_flags(trans, fs_info, 9545d4f98a2SYan Zheng buf->start, 9555d4f98a2SYan Zheng buf->len, 956b1c79e09SJosef Bacik new_flags, level, 0); 957be1a5564SMark Fasheh if (ret) 958be1a5564SMark Fasheh return ret; 9595d4f98a2SYan Zheng } 9605d4f98a2SYan Zheng } else { 9615d4f98a2SYan Zheng if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { 9625d4f98a2SYan Zheng if (root->root_key.objectid == 9635d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 964e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 9655d4f98a2SYan Zheng else 966e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 967692826b2SJeff Mahoney if (ret) 968692826b2SJeff Mahoney return ret; 969e339a6b0SJosef Bacik ret = btrfs_dec_ref(trans, root, buf, 1); 970692826b2SJeff Mahoney if (ret) 971692826b2SJeff Mahoney return ret; 9725d4f98a2SYan Zheng } 9736a884d7dSDavid Sterba btrfs_clean_tree_block(buf); 974f0486c68SYan, Zheng *last_ref = 1; 9755d4f98a2SYan Zheng } 9765d4f98a2SYan Zheng return 0; 9775d4f98a2SYan Zheng } 9785d4f98a2SYan Zheng 979a6279470SFilipe Manana static struct extent_buffer *alloc_tree_block_no_bg_flush( 980a6279470SFilipe Manana struct btrfs_trans_handle *trans, 981a6279470SFilipe Manana struct btrfs_root *root, 982a6279470SFilipe Manana u64 parent_start, 983a6279470SFilipe Manana const struct btrfs_disk_key *disk_key, 984a6279470SFilipe Manana int level, 985a6279470SFilipe Manana u64 hint, 986a6279470SFilipe Manana u64 empty_size) 987a6279470SFilipe Manana { 988a6279470SFilipe Manana struct btrfs_fs_info *fs_info = root->fs_info; 989a6279470SFilipe Manana struct extent_buffer *ret; 990a6279470SFilipe Manana 991a6279470SFilipe Manana /* 992a6279470SFilipe Manana * If we are COWing a node/leaf from the extent, chunk, device or free 993a6279470SFilipe Manana * space trees, make sure that we do not finish block group creation of 994a6279470SFilipe Manana * pending block groups. We do this to avoid a deadlock. 995a6279470SFilipe Manana * COWing can result in allocation of a new chunk, and flushing pending 996a6279470SFilipe Manana * block groups (btrfs_create_pending_block_groups()) can be triggered 997a6279470SFilipe Manana * when finishing allocation of a new chunk. Creation of a pending block 998a6279470SFilipe Manana * group modifies the extent, chunk, device and free space trees, 999a6279470SFilipe Manana * therefore we could deadlock with ourselves since we are holding a 1000a6279470SFilipe Manana * lock on an extent buffer that btrfs_create_pending_block_groups() may 1001a6279470SFilipe Manana * try to COW later. 1002a6279470SFilipe Manana * For similar reasons, we also need to delay flushing pending block 1003a6279470SFilipe Manana * groups when splitting a leaf or node, from one of those trees, since 1004a6279470SFilipe Manana * we are holding a write lock on it and its parent or when inserting a 1005a6279470SFilipe Manana * new root node for one of those trees. 1006a6279470SFilipe Manana */ 1007a6279470SFilipe Manana if (root == fs_info->extent_root || 1008a6279470SFilipe Manana root == fs_info->chunk_root || 1009a6279470SFilipe Manana root == fs_info->dev_root || 1010a6279470SFilipe Manana root == fs_info->free_space_root) 1011a6279470SFilipe Manana trans->can_flush_pending_bgs = false; 1012a6279470SFilipe Manana 1013a6279470SFilipe Manana ret = btrfs_alloc_tree_block(trans, root, parent_start, 1014a6279470SFilipe Manana root->root_key.objectid, disk_key, level, 1015a6279470SFilipe Manana hint, empty_size); 1016a6279470SFilipe Manana trans->can_flush_pending_bgs = true; 1017a6279470SFilipe Manana 1018a6279470SFilipe Manana return ret; 1019a6279470SFilipe Manana } 1020a6279470SFilipe Manana 10215d4f98a2SYan Zheng /* 1022d397712bSChris Mason * does the dirty work in cow of a single block. The parent block (if 1023d397712bSChris Mason * supplied) is updated to point to the new cow copy. The new buffer is marked 1024d397712bSChris Mason * dirty and returned locked. If you modify the block it needs to be marked 1025d397712bSChris Mason * dirty again. 1026d352ac68SChris Mason * 1027d352ac68SChris Mason * search_start -- an allocation hint for the new block 1028d352ac68SChris Mason * 1029d397712bSChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in 1030d397712bSChris Mason * bytes the allocator should try to find free next to the block it returns. 1031d397712bSChris Mason * This is just a hint and may be ignored by the allocator. 1032d352ac68SChris Mason */ 1033d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, 10345f39d397SChris Mason struct btrfs_root *root, 10355f39d397SChris Mason struct extent_buffer *buf, 10365f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 10375f39d397SChris Mason struct extent_buffer **cow_ret, 10389fa8cfe7SChris Mason u64 search_start, u64 empty_size) 10396702ed49SChris Mason { 10400b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10415d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 10425f39d397SChris Mason struct extent_buffer *cow; 1043be1a5564SMark Fasheh int level, ret; 1044f0486c68SYan, Zheng int last_ref = 0; 1045925baeddSChris Mason int unlock_orig = 0; 10460f5053ebSGoldwyn Rodrigues u64 parent_start = 0; 10476702ed49SChris Mason 1048925baeddSChris Mason if (*cow_ret == buf) 1049925baeddSChris Mason unlock_orig = 1; 1050925baeddSChris Mason 1051b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1052925baeddSChris Mason 105327cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 10540b246afaSJeff Mahoney trans->transid != fs_info->running_transaction->transid); 105527cdeb70SMiao Xie WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 105627cdeb70SMiao Xie trans->transid != root->last_trans); 10575f39d397SChris Mason 10587bb86316SChris Mason level = btrfs_header_level(buf); 105931840ae1SZheng Yan 10605d4f98a2SYan Zheng if (level == 0) 10615d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 10625d4f98a2SYan Zheng else 10635d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 10645d4f98a2SYan Zheng 10650f5053ebSGoldwyn Rodrigues if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent) 10665d4f98a2SYan Zheng parent_start = parent->start; 10675d4f98a2SYan Zheng 1068a6279470SFilipe Manana cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key, 1069a6279470SFilipe Manana level, search_start, empty_size); 10706702ed49SChris Mason if (IS_ERR(cow)) 10716702ed49SChris Mason return PTR_ERR(cow); 10726702ed49SChris Mason 1073b4ce94deSChris Mason /* cow is set to blocking by btrfs_init_new_buffer */ 1074b4ce94deSChris Mason 107558e8012cSDavid Sterba copy_extent_buffer_full(cow, buf); 1076db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 10775f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 10785d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 10795d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 10805d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 10815d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 10825d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 10835d4f98a2SYan Zheng else 10845f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 10856702ed49SChris Mason 1086de37aa51SNikolay Borisov write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); 10872b82032cSYan Zheng 1088be1a5564SMark Fasheh ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); 1089b68dc2a9SMark Fasheh if (ret) { 109066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1091b68dc2a9SMark Fasheh return ret; 1092b68dc2a9SMark Fasheh } 10931a40e23bSZheng Yan 109427cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) { 109583d4cfd4SJosef Bacik ret = btrfs_reloc_cow_block(trans, root, buf, cow); 109693314e3bSZhaolei if (ret) { 109766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 109883d4cfd4SJosef Bacik return ret; 109983d4cfd4SJosef Bacik } 110093314e3bSZhaolei } 11013fd0a558SYan, Zheng 11026702ed49SChris Mason if (buf == root->node) { 1103925baeddSChris Mason WARN_ON(parent && parent != buf); 11045d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 11055d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 11065d4f98a2SYan Zheng parent_start = buf->start; 1107925baeddSChris Mason 11085f39d397SChris Mason extent_buffer_get(cow); 1109d9d19a01SDavid Sterba ret = tree_mod_log_insert_root(root->node, cow, 1); 1110d9d19a01SDavid Sterba BUG_ON(ret < 0); 1111240f62c8SChris Mason rcu_assign_pointer(root->node, cow); 1112925baeddSChris Mason 1113f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 11145581a51aSJan Schmidt last_ref); 11155f39d397SChris Mason free_extent_buffer(buf); 11160b86a832SChris Mason add_root_to_dirty_list(root); 11176702ed49SChris Mason } else { 11185d4f98a2SYan Zheng WARN_ON(trans->transid != btrfs_header_generation(parent)); 1119e09c2efeSDavid Sterba tree_mod_log_insert_key(parent, parent_slot, 1120c8cc6341SJosef Bacik MOD_LOG_KEY_REPLACE, GFP_NOFS); 11215f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 1122db94535dSChris Mason cow->start); 112374493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 112474493f7aSChris Mason trans->transid); 11256702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 11265de865eeSFilipe David Borba Manana if (last_ref) { 1127db7279a2SDavid Sterba ret = tree_mod_log_free_eb(buf); 11285de865eeSFilipe David Borba Manana if (ret) { 112966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 11305de865eeSFilipe David Borba Manana return ret; 11315de865eeSFilipe David Borba Manana } 11325de865eeSFilipe David Borba Manana } 1133f0486c68SYan, Zheng btrfs_free_tree_block(trans, root, buf, parent_start, 11345581a51aSJan Schmidt last_ref); 11356702ed49SChris Mason } 1136925baeddSChris Mason if (unlock_orig) 1137925baeddSChris Mason btrfs_tree_unlock(buf); 11383083ee2eSJosef Bacik free_extent_buffer_stale(buf); 11396702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 11406702ed49SChris Mason *cow_ret = cow; 11416702ed49SChris Mason return 0; 11426702ed49SChris Mason } 11436702ed49SChris Mason 11445d9e75c4SJan Schmidt /* 11455d9e75c4SJan Schmidt * returns the logical address of the oldest predecessor of the given root. 11465d9e75c4SJan Schmidt * entries older than time_seq are ignored. 11475d9e75c4SJan Schmidt */ 1148bcd24dabSDavid Sterba static struct tree_mod_elem *__tree_mod_log_oldest_root( 114930b0463aSJan Schmidt struct extent_buffer *eb_root, u64 time_seq) 11505d9e75c4SJan Schmidt { 11515d9e75c4SJan Schmidt struct tree_mod_elem *tm; 11525d9e75c4SJan Schmidt struct tree_mod_elem *found = NULL; 115330b0463aSJan Schmidt u64 root_logical = eb_root->start; 11545d9e75c4SJan Schmidt int looped = 0; 11555d9e75c4SJan Schmidt 11565d9e75c4SJan Schmidt if (!time_seq) 115735a3621bSStefan Behrens return NULL; 11585d9e75c4SJan Schmidt 11595d9e75c4SJan Schmidt /* 1160298cfd36SChandan Rajendra * the very last operation that's logged for a root is the 1161298cfd36SChandan Rajendra * replacement operation (if it is replaced at all). this has 1162298cfd36SChandan Rajendra * the logical address of the *new* root, making it the very 1163298cfd36SChandan Rajendra * first operation that's logged for this root. 11645d9e75c4SJan Schmidt */ 11655d9e75c4SJan Schmidt while (1) { 1166bcd24dabSDavid Sterba tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical, 11675d9e75c4SJan Schmidt time_seq); 11685d9e75c4SJan Schmidt if (!looped && !tm) 116935a3621bSStefan Behrens return NULL; 11705d9e75c4SJan Schmidt /* 117128da9fb4SJan Schmidt * if there are no tree operation for the oldest root, we simply 117228da9fb4SJan Schmidt * return it. this should only happen if that (old) root is at 117328da9fb4SJan Schmidt * level 0. 11745d9e75c4SJan Schmidt */ 117528da9fb4SJan Schmidt if (!tm) 117628da9fb4SJan Schmidt break; 11775d9e75c4SJan Schmidt 117828da9fb4SJan Schmidt /* 117928da9fb4SJan Schmidt * if there's an operation that's not a root replacement, we 118028da9fb4SJan Schmidt * found the oldest version of our root. normally, we'll find a 118128da9fb4SJan Schmidt * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. 118228da9fb4SJan Schmidt */ 11835d9e75c4SJan Schmidt if (tm->op != MOD_LOG_ROOT_REPLACE) 11845d9e75c4SJan Schmidt break; 11855d9e75c4SJan Schmidt 11865d9e75c4SJan Schmidt found = tm; 11875d9e75c4SJan Schmidt root_logical = tm->old_root.logical; 11885d9e75c4SJan Schmidt looped = 1; 11895d9e75c4SJan Schmidt } 11905d9e75c4SJan Schmidt 1191a95236d9SJan Schmidt /* if there's no old root to return, return what we found instead */ 1192a95236d9SJan Schmidt if (!found) 1193a95236d9SJan Schmidt found = tm; 1194a95236d9SJan Schmidt 11955d9e75c4SJan Schmidt return found; 11965d9e75c4SJan Schmidt } 11975d9e75c4SJan Schmidt 11985d9e75c4SJan Schmidt /* 11995d9e75c4SJan Schmidt * tm is a pointer to the first operation to rewind within eb. then, all 120001327610SNicholas D Steeves * previous operations will be rewound (until we reach something older than 12015d9e75c4SJan Schmidt * time_seq). 12025d9e75c4SJan Schmidt */ 12035d9e75c4SJan Schmidt static void 1204f1ca7e98SJosef Bacik __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, 1205f1ca7e98SJosef Bacik u64 time_seq, struct tree_mod_elem *first_tm) 12065d9e75c4SJan Schmidt { 12075d9e75c4SJan Schmidt u32 n; 12085d9e75c4SJan Schmidt struct rb_node *next; 12095d9e75c4SJan Schmidt struct tree_mod_elem *tm = first_tm; 12105d9e75c4SJan Schmidt unsigned long o_dst; 12115d9e75c4SJan Schmidt unsigned long o_src; 12125d9e75c4SJan Schmidt unsigned long p_size = sizeof(struct btrfs_key_ptr); 12135d9e75c4SJan Schmidt 12145d9e75c4SJan Schmidt n = btrfs_header_nritems(eb); 1215b1a09f1eSDavid Sterba read_lock(&fs_info->tree_mod_log_lock); 1216097b8a7cSJan Schmidt while (tm && tm->seq >= time_seq) { 12175d9e75c4SJan Schmidt /* 12185d9e75c4SJan Schmidt * all the operations are recorded with the operator used for 12195d9e75c4SJan Schmidt * the modification. as we're going backwards, we do the 12205d9e75c4SJan Schmidt * opposite of each operation here. 12215d9e75c4SJan Schmidt */ 12225d9e75c4SJan Schmidt switch (tm->op) { 12235d9e75c4SJan Schmidt case MOD_LOG_KEY_REMOVE_WHILE_FREEING: 12245d9e75c4SJan Schmidt BUG_ON(tm->slot < n); 12251c697d4aSEric Sandeen /* Fallthrough */ 122695c80bb1SLiu Bo case MOD_LOG_KEY_REMOVE_WHILE_MOVING: 12274c3e6969SChris Mason case MOD_LOG_KEY_REMOVE: 12285d9e75c4SJan Schmidt btrfs_set_node_key(eb, &tm->key, tm->slot); 12295d9e75c4SJan Schmidt btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); 12305d9e75c4SJan Schmidt btrfs_set_node_ptr_generation(eb, tm->slot, 12315d9e75c4SJan Schmidt tm->generation); 12324c3e6969SChris Mason n++; 12335d9e75c4SJan Schmidt break; 12345d9e75c4SJan Schmidt case MOD_LOG_KEY_REPLACE: 12355d9e75c4SJan Schmidt BUG_ON(tm->slot >= n); 12365d9e75c4SJan Schmidt btrfs_set_node_key(eb, &tm->key, tm->slot); 12375d9e75c4SJan Schmidt btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); 12385d9e75c4SJan Schmidt btrfs_set_node_ptr_generation(eb, tm->slot, 12395d9e75c4SJan Schmidt tm->generation); 12405d9e75c4SJan Schmidt break; 12415d9e75c4SJan Schmidt case MOD_LOG_KEY_ADD: 124219956c7eSJan Schmidt /* if a move operation is needed it's in the log */ 12435d9e75c4SJan Schmidt n--; 12445d9e75c4SJan Schmidt break; 12455d9e75c4SJan Schmidt case MOD_LOG_MOVE_KEYS: 1246c3193108SJan Schmidt o_dst = btrfs_node_key_ptr_offset(tm->slot); 1247c3193108SJan Schmidt o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); 1248c3193108SJan Schmidt memmove_extent_buffer(eb, o_dst, o_src, 12495d9e75c4SJan Schmidt tm->move.nr_items * p_size); 12505d9e75c4SJan Schmidt break; 12515d9e75c4SJan Schmidt case MOD_LOG_ROOT_REPLACE: 12525d9e75c4SJan Schmidt /* 12535d9e75c4SJan Schmidt * this operation is special. for roots, this must be 12545d9e75c4SJan Schmidt * handled explicitly before rewinding. 12555d9e75c4SJan Schmidt * for non-roots, this operation may exist if the node 12565d9e75c4SJan Schmidt * was a root: root A -> child B; then A gets empty and 12575d9e75c4SJan Schmidt * B is promoted to the new root. in the mod log, we'll 12585d9e75c4SJan Schmidt * have a root-replace operation for B, a tree block 12595d9e75c4SJan Schmidt * that is no root. we simply ignore that operation. 12605d9e75c4SJan Schmidt */ 12615d9e75c4SJan Schmidt break; 12625d9e75c4SJan Schmidt } 12635d9e75c4SJan Schmidt next = rb_next(&tm->node); 12645d9e75c4SJan Schmidt if (!next) 12655d9e75c4SJan Schmidt break; 12666b4df8b6SGeliang Tang tm = rb_entry(next, struct tree_mod_elem, node); 1267298cfd36SChandan Rajendra if (tm->logical != first_tm->logical) 12685d9e75c4SJan Schmidt break; 12695d9e75c4SJan Schmidt } 1270b1a09f1eSDavid Sterba read_unlock(&fs_info->tree_mod_log_lock); 12715d9e75c4SJan Schmidt btrfs_set_header_nritems(eb, n); 12725d9e75c4SJan Schmidt } 12735d9e75c4SJan Schmidt 127447fb091fSJan Schmidt /* 127501327610SNicholas D Steeves * Called with eb read locked. If the buffer cannot be rewound, the same buffer 127647fb091fSJan Schmidt * is returned. If rewind operations happen, a fresh buffer is returned. The 127747fb091fSJan Schmidt * returned buffer is always read-locked. If the returned buffer is not the 127847fb091fSJan Schmidt * input buffer, the lock on the input buffer is released and the input buffer 127947fb091fSJan Schmidt * is freed (its refcount is decremented). 128047fb091fSJan Schmidt */ 12815d9e75c4SJan Schmidt static struct extent_buffer * 12829ec72677SJosef Bacik tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, 12839ec72677SJosef Bacik struct extent_buffer *eb, u64 time_seq) 12845d9e75c4SJan Schmidt { 12855d9e75c4SJan Schmidt struct extent_buffer *eb_rewin; 12865d9e75c4SJan Schmidt struct tree_mod_elem *tm; 12875d9e75c4SJan Schmidt 12885d9e75c4SJan Schmidt if (!time_seq) 12895d9e75c4SJan Schmidt return eb; 12905d9e75c4SJan Schmidt 12915d9e75c4SJan Schmidt if (btrfs_header_level(eb) == 0) 12925d9e75c4SJan Schmidt return eb; 12935d9e75c4SJan Schmidt 12945d9e75c4SJan Schmidt tm = tree_mod_log_search(fs_info, eb->start, time_seq); 12955d9e75c4SJan Schmidt if (!tm) 12965d9e75c4SJan Schmidt return eb; 12975d9e75c4SJan Schmidt 12989ec72677SJosef Bacik btrfs_set_path_blocking(path); 1299300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 13009ec72677SJosef Bacik 13015d9e75c4SJan Schmidt if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 13025d9e75c4SJan Schmidt BUG_ON(tm->slot != 0); 1303da17066cSJeff Mahoney eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start); 1304db7f3436SJosef Bacik if (!eb_rewin) { 13059ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb); 1306db7f3436SJosef Bacik free_extent_buffer(eb); 1307db7f3436SJosef Bacik return NULL; 1308db7f3436SJosef Bacik } 13095d9e75c4SJan Schmidt btrfs_set_header_bytenr(eb_rewin, eb->start); 13105d9e75c4SJan Schmidt btrfs_set_header_backref_rev(eb_rewin, 13115d9e75c4SJan Schmidt btrfs_header_backref_rev(eb)); 13125d9e75c4SJan Schmidt btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); 1313c3193108SJan Schmidt btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); 13145d9e75c4SJan Schmidt } else { 13155d9e75c4SJan Schmidt eb_rewin = btrfs_clone_extent_buffer(eb); 1316db7f3436SJosef Bacik if (!eb_rewin) { 13179ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb); 1318db7f3436SJosef Bacik free_extent_buffer(eb); 1319db7f3436SJosef Bacik return NULL; 1320db7f3436SJosef Bacik } 13215d9e75c4SJan Schmidt } 13225d9e75c4SJan Schmidt 13239ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb); 13245d9e75c4SJan Schmidt free_extent_buffer(eb); 13255d9e75c4SJan Schmidt 132647fb091fSJan Schmidt btrfs_tree_read_lock(eb_rewin); 1327f1ca7e98SJosef Bacik __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); 132857911b8bSJan Schmidt WARN_ON(btrfs_header_nritems(eb_rewin) > 1329da17066cSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 13305d9e75c4SJan Schmidt 13315d9e75c4SJan Schmidt return eb_rewin; 13325d9e75c4SJan Schmidt } 13335d9e75c4SJan Schmidt 13348ba97a15SJan Schmidt /* 13358ba97a15SJan Schmidt * get_old_root() rewinds the state of @root's root node to the given @time_seq 13368ba97a15SJan Schmidt * value. If there are no changes, the current root->root_node is returned. If 13378ba97a15SJan Schmidt * anything changed in between, there's a fresh buffer allocated on which the 13388ba97a15SJan Schmidt * rewind operations are done. In any case, the returned buffer is read locked. 13398ba97a15SJan Schmidt * Returns NULL on error (with no locks held). 13408ba97a15SJan Schmidt */ 13415d9e75c4SJan Schmidt static inline struct extent_buffer * 13425d9e75c4SJan Schmidt get_old_root(struct btrfs_root *root, u64 time_seq) 13435d9e75c4SJan Schmidt { 13440b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13455d9e75c4SJan Schmidt struct tree_mod_elem *tm; 134630b0463aSJan Schmidt struct extent_buffer *eb = NULL; 134730b0463aSJan Schmidt struct extent_buffer *eb_root; 13487bfdcf7fSLiu Bo struct extent_buffer *old; 1349a95236d9SJan Schmidt struct tree_mod_root *old_root = NULL; 13504325edd0SChris Mason u64 old_generation = 0; 1351a95236d9SJan Schmidt u64 logical; 1352581c1760SQu Wenruo int level; 13535d9e75c4SJan Schmidt 135430b0463aSJan Schmidt eb_root = btrfs_read_lock_root_node(root); 1355bcd24dabSDavid Sterba tm = __tree_mod_log_oldest_root(eb_root, time_seq); 13565d9e75c4SJan Schmidt if (!tm) 135730b0463aSJan Schmidt return eb_root; 13585d9e75c4SJan Schmidt 1359a95236d9SJan Schmidt if (tm->op == MOD_LOG_ROOT_REPLACE) { 13605d9e75c4SJan Schmidt old_root = &tm->old_root; 13615d9e75c4SJan Schmidt old_generation = tm->generation; 1362a95236d9SJan Schmidt logical = old_root->logical; 1363581c1760SQu Wenruo level = old_root->level; 1364a95236d9SJan Schmidt } else { 136530b0463aSJan Schmidt logical = eb_root->start; 1366581c1760SQu Wenruo level = btrfs_header_level(eb_root); 1367a95236d9SJan Schmidt } 13685d9e75c4SJan Schmidt 13690b246afaSJeff Mahoney tm = tree_mod_log_search(fs_info, logical, time_seq); 1370834328a8SJan Schmidt if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 137130b0463aSJan Schmidt btrfs_tree_read_unlock(eb_root); 137230b0463aSJan Schmidt free_extent_buffer(eb_root); 1373581c1760SQu Wenruo old = read_tree_block(fs_info, logical, 0, level, NULL); 137464c043deSLiu Bo if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) { 137564c043deSLiu Bo if (!IS_ERR(old)) 1376416bc658SJosef Bacik free_extent_buffer(old); 13770b246afaSJeff Mahoney btrfs_warn(fs_info, 13780b246afaSJeff Mahoney "failed to read tree block %llu from get_old_root", 13790b246afaSJeff Mahoney logical); 1380834328a8SJan Schmidt } else { 13817bfdcf7fSLiu Bo eb = btrfs_clone_extent_buffer(old); 13827bfdcf7fSLiu Bo free_extent_buffer(old); 1383834328a8SJan Schmidt } 1384834328a8SJan Schmidt } else if (old_root) { 138530b0463aSJan Schmidt btrfs_tree_read_unlock(eb_root); 138630b0463aSJan Schmidt free_extent_buffer(eb_root); 13870b246afaSJeff Mahoney eb = alloc_dummy_extent_buffer(fs_info, logical); 1388834328a8SJan Schmidt } else { 1389300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb_root); 139030b0463aSJan Schmidt eb = btrfs_clone_extent_buffer(eb_root); 13919ec72677SJosef Bacik btrfs_tree_read_unlock_blocking(eb_root); 139230b0463aSJan Schmidt free_extent_buffer(eb_root); 1393834328a8SJan Schmidt } 1394834328a8SJan Schmidt 13958ba97a15SJan Schmidt if (!eb) 13968ba97a15SJan Schmidt return NULL; 13978ba97a15SJan Schmidt btrfs_tree_read_lock(eb); 1398a95236d9SJan Schmidt if (old_root) { 13995d9e75c4SJan Schmidt btrfs_set_header_bytenr(eb, eb->start); 14005d9e75c4SJan Schmidt btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); 140130b0463aSJan Schmidt btrfs_set_header_owner(eb, btrfs_header_owner(eb_root)); 14025d9e75c4SJan Schmidt btrfs_set_header_level(eb, old_root->level); 14035d9e75c4SJan Schmidt btrfs_set_header_generation(eb, old_generation); 1404a95236d9SJan Schmidt } 140528da9fb4SJan Schmidt if (tm) 14060b246afaSJeff Mahoney __tree_mod_log_rewind(fs_info, eb, time_seq, tm); 140728da9fb4SJan Schmidt else 140828da9fb4SJan Schmidt WARN_ON(btrfs_header_level(eb) != 0); 14090b246afaSJeff Mahoney WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 14105d9e75c4SJan Schmidt 14115d9e75c4SJan Schmidt return eb; 14125d9e75c4SJan Schmidt } 14135d9e75c4SJan Schmidt 14145b6602e7SJan Schmidt int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) 14155b6602e7SJan Schmidt { 14165b6602e7SJan Schmidt struct tree_mod_elem *tm; 14175b6602e7SJan Schmidt int level; 141830b0463aSJan Schmidt struct extent_buffer *eb_root = btrfs_root_node(root); 14195b6602e7SJan Schmidt 1420bcd24dabSDavid Sterba tm = __tree_mod_log_oldest_root(eb_root, time_seq); 14215b6602e7SJan Schmidt if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { 14225b6602e7SJan Schmidt level = tm->old_root.level; 14235b6602e7SJan Schmidt } else { 142430b0463aSJan Schmidt level = btrfs_header_level(eb_root); 14255b6602e7SJan Schmidt } 142630b0463aSJan Schmidt free_extent_buffer(eb_root); 14275b6602e7SJan Schmidt 14285b6602e7SJan Schmidt return level; 14295b6602e7SJan Schmidt } 14305b6602e7SJan Schmidt 14315d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans, 14325d4f98a2SYan Zheng struct btrfs_root *root, 14335d4f98a2SYan Zheng struct extent_buffer *buf) 14345d4f98a2SYan Zheng { 1435f5ee5c9aSJeff Mahoney if (btrfs_is_testing(root->fs_info)) 1436faa2dbf0SJosef Bacik return 0; 1437fccb84c9SDavid Sterba 1438d1980131SDavid Sterba /* Ensure we can see the FORCE_COW bit */ 1439d1980131SDavid Sterba smp_mb__before_atomic(); 1440f1ebcc74SLiu Bo 1441f1ebcc74SLiu Bo /* 1442f1ebcc74SLiu Bo * We do not need to cow a block if 1443f1ebcc74SLiu Bo * 1) this block is not created or changed in this transaction; 1444f1ebcc74SLiu Bo * 2) this block does not belong to TREE_RELOC tree; 1445f1ebcc74SLiu Bo * 3) the root is not forced COW. 1446f1ebcc74SLiu Bo * 1447f1ebcc74SLiu Bo * What is forced COW: 144801327610SNicholas D Steeves * when we create snapshot during committing the transaction, 144952042d8eSAndrea Gelmini * after we've finished copying src root, we must COW the shared 1450f1ebcc74SLiu Bo * block to ensure the metadata consistency. 1451f1ebcc74SLiu Bo */ 14525d4f98a2SYan Zheng if (btrfs_header_generation(buf) == trans->transid && 14535d4f98a2SYan Zheng !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && 14545d4f98a2SYan Zheng !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 1455f1ebcc74SLiu Bo btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && 145627cdeb70SMiao Xie !test_bit(BTRFS_ROOT_FORCE_COW, &root->state)) 14575d4f98a2SYan Zheng return 0; 14585d4f98a2SYan Zheng return 1; 14595d4f98a2SYan Zheng } 14605d4f98a2SYan Zheng 1461d352ac68SChris Mason /* 1462d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 146301327610SNicholas D Steeves * This version of it has extra checks so that a block isn't COWed more than 1464d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 1465d352ac68SChris Mason */ 1466d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, 14675f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 14685f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 14699fa8cfe7SChris Mason struct extent_buffer **cow_ret) 147002217ed2SChris Mason { 14710b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14726702ed49SChris Mason u64 search_start; 1473f510cfecSChris Mason int ret; 1474dc17ff8fSChris Mason 147583354f07SJosef Bacik if (test_bit(BTRFS_ROOT_DELETING, &root->state)) 147683354f07SJosef Bacik btrfs_err(fs_info, 147783354f07SJosef Bacik "COW'ing blocks on a fs root that's being dropped"); 147883354f07SJosef Bacik 14790b246afaSJeff Mahoney if (trans->transaction != fs_info->running_transaction) 148031b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 1481c1c9ff7cSGeert Uytterhoeven trans->transid, 14820b246afaSJeff Mahoney fs_info->running_transaction->transid); 148331b1a2bdSJulia Lawall 14840b246afaSJeff Mahoney if (trans->transid != fs_info->generation) 148531b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 14860b246afaSJeff Mahoney trans->transid, fs_info->generation); 1487dc17ff8fSChris Mason 14885d4f98a2SYan Zheng if (!should_cow_block(trans, root, buf)) { 148964c12921SJeff Mahoney trans->dirty = true; 149002217ed2SChris Mason *cow_ret = buf; 149102217ed2SChris Mason return 0; 149202217ed2SChris Mason } 1493c487685dSChris Mason 1494ee22184bSByongho Lee search_start = buf->start & ~((u64)SZ_1G - 1); 1495b4ce94deSChris Mason 1496b4ce94deSChris Mason if (parent) 14978bead258SDavid Sterba btrfs_set_lock_blocking_write(parent); 14988bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 1499b4ce94deSChris Mason 1500f616f5cdSQu Wenruo /* 1501f616f5cdSQu Wenruo * Before CoWing this block for later modification, check if it's 1502f616f5cdSQu Wenruo * the subtree root and do the delayed subtree trace if needed. 1503f616f5cdSQu Wenruo * 1504f616f5cdSQu Wenruo * Also We don't care about the error, as it's handled internally. 1505f616f5cdSQu Wenruo */ 1506f616f5cdSQu Wenruo btrfs_qgroup_trace_subtree_after_cow(trans, root, buf); 1507f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 15089fa8cfe7SChris Mason parent_slot, cow_ret, search_start, 0); 15091abe9b8aSliubo 15101abe9b8aSliubo trace_btrfs_cow_block(root, buf, *cow_ret); 15111abe9b8aSliubo 1512f510cfecSChris Mason return ret; 15132c90e5d6SChris Mason } 15146702ed49SChris Mason 1515d352ac68SChris Mason /* 1516d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 1517d352ac68SChris Mason * node are actually close by 1518d352ac68SChris Mason */ 15196b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 15206702ed49SChris Mason { 15216b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 15226702ed49SChris Mason return 1; 15236b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 15246702ed49SChris Mason return 1; 152502217ed2SChris Mason return 0; 152602217ed2SChris Mason } 152702217ed2SChris Mason 1528081e9573SChris Mason /* 1529081e9573SChris Mason * compare two keys in a memcmp fashion 1530081e9573SChris Mason */ 1531310712b2SOmar Sandoval static int comp_keys(const struct btrfs_disk_key *disk, 1532310712b2SOmar Sandoval const struct btrfs_key *k2) 1533081e9573SChris Mason { 1534081e9573SChris Mason struct btrfs_key k1; 1535081e9573SChris Mason 1536081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 1537081e9573SChris Mason 153820736abaSDiego Calleja return btrfs_comp_cpu_keys(&k1, k2); 1539081e9573SChris Mason } 1540081e9573SChris Mason 1541f3465ca4SJosef Bacik /* 1542f3465ca4SJosef Bacik * same as comp_keys only with two btrfs_key's 1543f3465ca4SJosef Bacik */ 1544310712b2SOmar Sandoval int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2) 1545f3465ca4SJosef Bacik { 1546f3465ca4SJosef Bacik if (k1->objectid > k2->objectid) 1547f3465ca4SJosef Bacik return 1; 1548f3465ca4SJosef Bacik if (k1->objectid < k2->objectid) 1549f3465ca4SJosef Bacik return -1; 1550f3465ca4SJosef Bacik if (k1->type > k2->type) 1551f3465ca4SJosef Bacik return 1; 1552f3465ca4SJosef Bacik if (k1->type < k2->type) 1553f3465ca4SJosef Bacik return -1; 1554f3465ca4SJosef Bacik if (k1->offset > k2->offset) 1555f3465ca4SJosef Bacik return 1; 1556f3465ca4SJosef Bacik if (k1->offset < k2->offset) 1557f3465ca4SJosef Bacik return -1; 1558f3465ca4SJosef Bacik return 0; 1559f3465ca4SJosef Bacik } 1560081e9573SChris Mason 1561d352ac68SChris Mason /* 1562d352ac68SChris Mason * this is used by the defrag code to go through all the 1563d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 1564d352ac68SChris Mason * disk order is close to key order 1565d352ac68SChris Mason */ 15666702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 15675f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 1568de78b51aSEric Sandeen int start_slot, u64 *last_ret, 1569a6b6e75eSChris Mason struct btrfs_key *progress) 15706702ed49SChris Mason { 15710b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 15726b80053dSChris Mason struct extent_buffer *cur; 15736702ed49SChris Mason u64 blocknr; 1574ca7a79adSChris Mason u64 gen; 1575e9d0b13bSChris Mason u64 search_start = *last_ret; 1576e9d0b13bSChris Mason u64 last_block = 0; 15776702ed49SChris Mason u64 other; 15786702ed49SChris Mason u32 parent_nritems; 15796702ed49SChris Mason int end_slot; 15806702ed49SChris Mason int i; 15816702ed49SChris Mason int err = 0; 1582f2183bdeSChris Mason int parent_level; 15836b80053dSChris Mason int uptodate; 15846b80053dSChris Mason u32 blocksize; 1585081e9573SChris Mason int progress_passed = 0; 1586081e9573SChris Mason struct btrfs_disk_key disk_key; 15876702ed49SChris Mason 15885708b959SChris Mason parent_level = btrfs_header_level(parent); 15895708b959SChris Mason 15900b246afaSJeff Mahoney WARN_ON(trans->transaction != fs_info->running_transaction); 15910b246afaSJeff Mahoney WARN_ON(trans->transid != fs_info->generation); 159286479a04SChris Mason 15936b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 15940b246afaSJeff Mahoney blocksize = fs_info->nodesize; 15955dfe2be7SFilipe Manana end_slot = parent_nritems - 1; 15966702ed49SChris Mason 15975dfe2be7SFilipe Manana if (parent_nritems <= 1) 15986702ed49SChris Mason return 0; 15996702ed49SChris Mason 16008bead258SDavid Sterba btrfs_set_lock_blocking_write(parent); 1601b4ce94deSChris Mason 16025dfe2be7SFilipe Manana for (i = start_slot; i <= end_slot; i++) { 1603581c1760SQu Wenruo struct btrfs_key first_key; 16046702ed49SChris Mason int close = 1; 1605a6b6e75eSChris Mason 1606081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 1607081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 1608081e9573SChris Mason continue; 1609081e9573SChris Mason 1610081e9573SChris Mason progress_passed = 1; 16116b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 1612ca7a79adSChris Mason gen = btrfs_node_ptr_generation(parent, i); 1613581c1760SQu Wenruo btrfs_node_key_to_cpu(parent, &first_key, i); 1614e9d0b13bSChris Mason if (last_block == 0) 1615e9d0b13bSChris Mason last_block = blocknr; 16165708b959SChris Mason 16176702ed49SChris Mason if (i > 0) { 16186b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 16196b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 16206702ed49SChris Mason } 16215dfe2be7SFilipe Manana if (!close && i < end_slot) { 16226b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 16236b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 16246702ed49SChris Mason } 1625e9d0b13bSChris Mason if (close) { 1626e9d0b13bSChris Mason last_block = blocknr; 16276702ed49SChris Mason continue; 1628e9d0b13bSChris Mason } 16296702ed49SChris Mason 16300b246afaSJeff Mahoney cur = find_extent_buffer(fs_info, blocknr); 16316b80053dSChris Mason if (cur) 1632b9fab919SChris Mason uptodate = btrfs_buffer_uptodate(cur, gen, 0); 16336b80053dSChris Mason else 16346b80053dSChris Mason uptodate = 0; 16355708b959SChris Mason if (!cur || !uptodate) { 16366b80053dSChris Mason if (!cur) { 1637581c1760SQu Wenruo cur = read_tree_block(fs_info, blocknr, gen, 1638581c1760SQu Wenruo parent_level - 1, 1639581c1760SQu Wenruo &first_key); 164064c043deSLiu Bo if (IS_ERR(cur)) { 164164c043deSLiu Bo return PTR_ERR(cur); 164264c043deSLiu Bo } else if (!extent_buffer_uptodate(cur)) { 1643416bc658SJosef Bacik free_extent_buffer(cur); 164497d9a8a4STsutomu Itoh return -EIO; 1645416bc658SJosef Bacik } 16466b80053dSChris Mason } else if (!uptodate) { 1647581c1760SQu Wenruo err = btrfs_read_buffer(cur, gen, 1648581c1760SQu Wenruo parent_level - 1,&first_key); 1649018642a1STsutomu Itoh if (err) { 1650018642a1STsutomu Itoh free_extent_buffer(cur); 1651018642a1STsutomu Itoh return err; 1652018642a1STsutomu Itoh } 16536702ed49SChris Mason } 1654f2183bdeSChris Mason } 1655e9d0b13bSChris Mason if (search_start == 0) 16566b80053dSChris Mason search_start = last_block; 1657e9d0b13bSChris Mason 1658e7a84565SChris Mason btrfs_tree_lock(cur); 16598bead258SDavid Sterba btrfs_set_lock_blocking_write(cur); 16606b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 1661e7a84565SChris Mason &cur, search_start, 16626b80053dSChris Mason min(16 * blocksize, 16639fa8cfe7SChris Mason (end_slot - i) * blocksize)); 1664252c38f0SYan if (err) { 1665e7a84565SChris Mason btrfs_tree_unlock(cur); 16666b80053dSChris Mason free_extent_buffer(cur); 16676702ed49SChris Mason break; 1668252c38f0SYan } 1669e7a84565SChris Mason search_start = cur->start; 1670e7a84565SChris Mason last_block = cur->start; 1671f2183bdeSChris Mason *last_ret = search_start; 1672e7a84565SChris Mason btrfs_tree_unlock(cur); 1673e7a84565SChris Mason free_extent_buffer(cur); 16746702ed49SChris Mason } 16756702ed49SChris Mason return err; 16766702ed49SChris Mason } 16776702ed49SChris Mason 167874123bd7SChris Mason /* 16795f39d397SChris Mason * search for key in the extent_buffer. The items start at offset p, 16805f39d397SChris Mason * and they are item_size apart. There are 'max' items in p. 16815f39d397SChris Mason * 168274123bd7SChris Mason * the slot in the array is returned via slot, and it points to 168374123bd7SChris Mason * the place where you would insert key if it is not found in 168474123bd7SChris Mason * the array. 168574123bd7SChris Mason * 168674123bd7SChris Mason * slot may point to max if the key is bigger than all of the keys 168774123bd7SChris Mason */ 1688e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb, 1689310712b2SOmar Sandoval unsigned long p, int item_size, 1690310712b2SOmar Sandoval const struct btrfs_key *key, 1691be0e5c09SChris Mason int max, int *slot) 1692be0e5c09SChris Mason { 1693be0e5c09SChris Mason int low = 0; 1694be0e5c09SChris Mason int high = max; 1695be0e5c09SChris Mason int mid; 1696be0e5c09SChris Mason int ret; 1697479965d6SChris Mason struct btrfs_disk_key *tmp = NULL; 16985f39d397SChris Mason struct btrfs_disk_key unaligned; 16995f39d397SChris Mason unsigned long offset; 17005f39d397SChris Mason char *kaddr = NULL; 17015f39d397SChris Mason unsigned long map_start = 0; 17025f39d397SChris Mason unsigned long map_len = 0; 1703479965d6SChris Mason int err; 1704be0e5c09SChris Mason 17055e24e9afSLiu Bo if (low > high) { 17065e24e9afSLiu Bo btrfs_err(eb->fs_info, 17075e24e9afSLiu Bo "%s: low (%d) > high (%d) eb %llu owner %llu level %d", 17085e24e9afSLiu Bo __func__, low, high, eb->start, 17095e24e9afSLiu Bo btrfs_header_owner(eb), btrfs_header_level(eb)); 17105e24e9afSLiu Bo return -EINVAL; 17115e24e9afSLiu Bo } 17125e24e9afSLiu Bo 1713be0e5c09SChris Mason while (low < high) { 1714be0e5c09SChris Mason mid = (low + high) / 2; 17155f39d397SChris Mason offset = p + mid * item_size; 17165f39d397SChris Mason 1717a6591715SChris Mason if (!kaddr || offset < map_start || 17185f39d397SChris Mason (offset + sizeof(struct btrfs_disk_key)) > 17195f39d397SChris Mason map_start + map_len) { 1720934d375bSChris Mason 1721934d375bSChris Mason err = map_private_extent_buffer(eb, offset, 1722479965d6SChris Mason sizeof(struct btrfs_disk_key), 1723a6591715SChris Mason &kaddr, &map_start, &map_len); 17245f39d397SChris Mason 1725479965d6SChris Mason if (!err) { 1726479965d6SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 1727479965d6SChris Mason map_start); 1728415b35a5SLiu Bo } else if (err == 1) { 17295f39d397SChris Mason read_extent_buffer(eb, &unaligned, 17305f39d397SChris Mason offset, sizeof(unaligned)); 17315f39d397SChris Mason tmp = &unaligned; 1732415b35a5SLiu Bo } else { 1733415b35a5SLiu Bo return err; 1734479965d6SChris Mason } 1735479965d6SChris Mason 17365f39d397SChris Mason } else { 17375f39d397SChris Mason tmp = (struct btrfs_disk_key *)(kaddr + offset - 17385f39d397SChris Mason map_start); 17395f39d397SChris Mason } 1740be0e5c09SChris Mason ret = comp_keys(tmp, key); 1741be0e5c09SChris Mason 1742be0e5c09SChris Mason if (ret < 0) 1743be0e5c09SChris Mason low = mid + 1; 1744be0e5c09SChris Mason else if (ret > 0) 1745be0e5c09SChris Mason high = mid; 1746be0e5c09SChris Mason else { 1747be0e5c09SChris Mason *slot = mid; 1748be0e5c09SChris Mason return 0; 1749be0e5c09SChris Mason } 1750be0e5c09SChris Mason } 1751be0e5c09SChris Mason *slot = low; 1752be0e5c09SChris Mason return 1; 1753be0e5c09SChris Mason } 1754be0e5c09SChris Mason 175597571fd0SChris Mason /* 175697571fd0SChris Mason * simple bin_search frontend that does the right thing for 175797571fd0SChris Mason * leaves vs nodes 175897571fd0SChris Mason */ 1759a74b35ecSNikolay Borisov int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key, 17605f39d397SChris Mason int level, int *slot) 1761be0e5c09SChris Mason { 1762f775738fSWang Sheng-Hui if (level == 0) 17635f39d397SChris Mason return generic_bin_search(eb, 17645f39d397SChris Mason offsetof(struct btrfs_leaf, items), 17650783fcfcSChris Mason sizeof(struct btrfs_item), 17665f39d397SChris Mason key, btrfs_header_nritems(eb), 17677518a238SChris Mason slot); 1768f775738fSWang Sheng-Hui else 17695f39d397SChris Mason return generic_bin_search(eb, 17705f39d397SChris Mason offsetof(struct btrfs_node, ptrs), 1771123abc88SChris Mason sizeof(struct btrfs_key_ptr), 17725f39d397SChris Mason key, btrfs_header_nritems(eb), 17737518a238SChris Mason slot); 1774be0e5c09SChris Mason } 1775be0e5c09SChris Mason 1776f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size) 1777f0486c68SYan, Zheng { 1778f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 1779f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 1780f0486c68SYan, Zheng btrfs_root_used(&root->root_item) + size); 1781f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 1782f0486c68SYan, Zheng } 1783f0486c68SYan, Zheng 1784f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size) 1785f0486c68SYan, Zheng { 1786f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 1787f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 1788f0486c68SYan, Zheng btrfs_root_used(&root->root_item) - size); 1789f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 1790f0486c68SYan, Zheng } 1791f0486c68SYan, Zheng 1792d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 1793d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 1794d352ac68SChris Mason */ 1795d0d20b0fSDavid Sterba static noinline struct extent_buffer *read_node_slot( 1796d0d20b0fSDavid Sterba struct extent_buffer *parent, int slot) 1797bb803951SChris Mason { 1798ca7a79adSChris Mason int level = btrfs_header_level(parent); 1799416bc658SJosef Bacik struct extent_buffer *eb; 1800581c1760SQu Wenruo struct btrfs_key first_key; 1801416bc658SJosef Bacik 1802fb770ae4SLiu Bo if (slot < 0 || slot >= btrfs_header_nritems(parent)) 1803fb770ae4SLiu Bo return ERR_PTR(-ENOENT); 1804ca7a79adSChris Mason 1805ca7a79adSChris Mason BUG_ON(level == 0); 1806ca7a79adSChris Mason 1807581c1760SQu Wenruo btrfs_node_key_to_cpu(parent, &first_key, slot); 1808d0d20b0fSDavid Sterba eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot), 1809581c1760SQu Wenruo btrfs_node_ptr_generation(parent, slot), 1810581c1760SQu Wenruo level - 1, &first_key); 1811fb770ae4SLiu Bo if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { 1812416bc658SJosef Bacik free_extent_buffer(eb); 1813fb770ae4SLiu Bo eb = ERR_PTR(-EIO); 1814416bc658SJosef Bacik } 1815416bc658SJosef Bacik 1816416bc658SJosef Bacik return eb; 1817bb803951SChris Mason } 1818bb803951SChris Mason 1819d352ac68SChris Mason /* 1820d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 1821d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 1822d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 1823d352ac68SChris Mason */ 1824e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 182598ed5174SChris Mason struct btrfs_root *root, 182698ed5174SChris Mason struct btrfs_path *path, int level) 1827bb803951SChris Mason { 18280b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 18295f39d397SChris Mason struct extent_buffer *right = NULL; 18305f39d397SChris Mason struct extent_buffer *mid; 18315f39d397SChris Mason struct extent_buffer *left = NULL; 18325f39d397SChris Mason struct extent_buffer *parent = NULL; 1833bb803951SChris Mason int ret = 0; 1834bb803951SChris Mason int wret; 1835bb803951SChris Mason int pslot; 1836bb803951SChris Mason int orig_slot = path->slots[level]; 183779f95c82SChris Mason u64 orig_ptr; 1838bb803951SChris Mason 183998e6b1ebSLiu Bo ASSERT(level > 0); 1840bb803951SChris Mason 18415f39d397SChris Mason mid = path->nodes[level]; 1842b4ce94deSChris Mason 1843bd681513SChris Mason WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && 1844bd681513SChris Mason path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); 18457bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 18467bb86316SChris Mason 18471d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 184879f95c82SChris Mason 1849a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 18505f39d397SChris Mason parent = path->nodes[level + 1]; 1851bb803951SChris Mason pslot = path->slots[level + 1]; 1852a05a9bb1SLi Zefan } 1853bb803951SChris Mason 185440689478SChris Mason /* 185540689478SChris Mason * deal with the case where there is only one pointer in the root 185640689478SChris Mason * by promoting the node below to a root 185740689478SChris Mason */ 18585f39d397SChris Mason if (!parent) { 18595f39d397SChris Mason struct extent_buffer *child; 1860bb803951SChris Mason 18615f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 1862bb803951SChris Mason return 0; 1863bb803951SChris Mason 1864bb803951SChris Mason /* promote the child to a root */ 1865d0d20b0fSDavid Sterba child = read_node_slot(mid, 0); 1866fb770ae4SLiu Bo if (IS_ERR(child)) { 1867fb770ae4SLiu Bo ret = PTR_ERR(child); 18680b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, NULL); 1869305a26afSMark Fasheh goto enospc; 1870305a26afSMark Fasheh } 1871305a26afSMark Fasheh 1872925baeddSChris Mason btrfs_tree_lock(child); 18738bead258SDavid Sterba btrfs_set_lock_blocking_write(child); 18749fa8cfe7SChris Mason ret = btrfs_cow_block(trans, root, child, mid, 0, &child); 1875f0486c68SYan, Zheng if (ret) { 1876f0486c68SYan, Zheng btrfs_tree_unlock(child); 1877f0486c68SYan, Zheng free_extent_buffer(child); 1878f0486c68SYan, Zheng goto enospc; 1879f0486c68SYan, Zheng } 18802f375ab9SYan 1881d9d19a01SDavid Sterba ret = tree_mod_log_insert_root(root->node, child, 1); 1882d9d19a01SDavid Sterba BUG_ON(ret < 0); 1883240f62c8SChris Mason rcu_assign_pointer(root->node, child); 1884925baeddSChris Mason 18850b86a832SChris Mason add_root_to_dirty_list(root); 1886925baeddSChris Mason btrfs_tree_unlock(child); 1887b4ce94deSChris Mason 1888925baeddSChris Mason path->locks[level] = 0; 1889bb803951SChris Mason path->nodes[level] = NULL; 18906a884d7dSDavid Sterba btrfs_clean_tree_block(mid); 1891925baeddSChris Mason btrfs_tree_unlock(mid); 1892bb803951SChris Mason /* once for the path */ 18935f39d397SChris Mason free_extent_buffer(mid); 1894f0486c68SYan, Zheng 1895f0486c68SYan, Zheng root_sub_used(root, mid->len); 18965581a51aSJan Schmidt btrfs_free_tree_block(trans, root, mid, 0, 1); 1897bb803951SChris Mason /* once for the root ptr */ 18983083ee2eSJosef Bacik free_extent_buffer_stale(mid); 1899f0486c68SYan, Zheng return 0; 1900bb803951SChris Mason } 19015f39d397SChris Mason if (btrfs_header_nritems(mid) > 19020b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) 1903bb803951SChris Mason return 0; 1904bb803951SChris Mason 1905d0d20b0fSDavid Sterba left = read_node_slot(parent, pslot - 1); 1906fb770ae4SLiu Bo if (IS_ERR(left)) 1907fb770ae4SLiu Bo left = NULL; 1908fb770ae4SLiu Bo 19095f39d397SChris Mason if (left) { 1910925baeddSChris Mason btrfs_tree_lock(left); 19118bead258SDavid Sterba btrfs_set_lock_blocking_write(left); 19125f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 19139fa8cfe7SChris Mason parent, pslot - 1, &left); 191454aa1f4dSChris Mason if (wret) { 191554aa1f4dSChris Mason ret = wret; 191654aa1f4dSChris Mason goto enospc; 191754aa1f4dSChris Mason } 19182cc58cf2SChris Mason } 1919fb770ae4SLiu Bo 1920d0d20b0fSDavid Sterba right = read_node_slot(parent, pslot + 1); 1921fb770ae4SLiu Bo if (IS_ERR(right)) 1922fb770ae4SLiu Bo right = NULL; 1923fb770ae4SLiu Bo 19245f39d397SChris Mason if (right) { 1925925baeddSChris Mason btrfs_tree_lock(right); 19268bead258SDavid Sterba btrfs_set_lock_blocking_write(right); 19275f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 19289fa8cfe7SChris Mason parent, pslot + 1, &right); 19292cc58cf2SChris Mason if (wret) { 19302cc58cf2SChris Mason ret = wret; 19312cc58cf2SChris Mason goto enospc; 19322cc58cf2SChris Mason } 19332cc58cf2SChris Mason } 19342cc58cf2SChris Mason 19352cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 19365f39d397SChris Mason if (left) { 19375f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 19382ff7e61eSJeff Mahoney wret = push_node_left(trans, fs_info, left, mid, 1); 193979f95c82SChris Mason if (wret < 0) 194079f95c82SChris Mason ret = wret; 1941bb803951SChris Mason } 194279f95c82SChris Mason 194379f95c82SChris Mason /* 194479f95c82SChris Mason * then try to empty the right most buffer into the middle 194579f95c82SChris Mason */ 19465f39d397SChris Mason if (right) { 19472ff7e61eSJeff Mahoney wret = push_node_left(trans, fs_info, mid, right, 1); 194854aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 194979f95c82SChris Mason ret = wret; 19505f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 19516a884d7dSDavid Sterba btrfs_clean_tree_block(right); 1952925baeddSChris Mason btrfs_tree_unlock(right); 1953afe5fea7STsutomu Itoh del_ptr(root, path, level + 1, pslot + 1); 1954f0486c68SYan, Zheng root_sub_used(root, right->len); 19555581a51aSJan Schmidt btrfs_free_tree_block(trans, root, right, 0, 1); 19563083ee2eSJosef Bacik free_extent_buffer_stale(right); 1957f0486c68SYan, Zheng right = NULL; 1958bb803951SChris Mason } else { 19595f39d397SChris Mason struct btrfs_disk_key right_key; 19605f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 19610e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot + 1, 19620e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 19630e82bcfeSDavid Sterba BUG_ON(ret < 0); 19645f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 19655f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1966bb803951SChris Mason } 1967bb803951SChris Mason } 19685f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 196979f95c82SChris Mason /* 197079f95c82SChris Mason * we're not allowed to leave a node with one item in the 197179f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 197279f95c82SChris Mason * could try to delete the only pointer in this node. 197379f95c82SChris Mason * So, pull some keys from the left. 197479f95c82SChris Mason * There has to be a left pointer at this point because 197579f95c82SChris Mason * otherwise we would have pulled some pointers from the 197679f95c82SChris Mason * right 197779f95c82SChris Mason */ 1978305a26afSMark Fasheh if (!left) { 1979305a26afSMark Fasheh ret = -EROFS; 19800b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, NULL); 1981305a26afSMark Fasheh goto enospc; 1982305a26afSMark Fasheh } 19832ff7e61eSJeff Mahoney wret = balance_node_right(trans, fs_info, mid, left); 198454aa1f4dSChris Mason if (wret < 0) { 198579f95c82SChris Mason ret = wret; 198654aa1f4dSChris Mason goto enospc; 198754aa1f4dSChris Mason } 1988bce4eae9SChris Mason if (wret == 1) { 19892ff7e61eSJeff Mahoney wret = push_node_left(trans, fs_info, left, mid, 1); 1990bce4eae9SChris Mason if (wret < 0) 1991bce4eae9SChris Mason ret = wret; 1992bce4eae9SChris Mason } 199379f95c82SChris Mason BUG_ON(wret == 1); 199479f95c82SChris Mason } 19955f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 19966a884d7dSDavid Sterba btrfs_clean_tree_block(mid); 1997925baeddSChris Mason btrfs_tree_unlock(mid); 1998afe5fea7STsutomu Itoh del_ptr(root, path, level + 1, pslot); 1999f0486c68SYan, Zheng root_sub_used(root, mid->len); 20005581a51aSJan Schmidt btrfs_free_tree_block(trans, root, mid, 0, 1); 20013083ee2eSJosef Bacik free_extent_buffer_stale(mid); 2002f0486c68SYan, Zheng mid = NULL; 200379f95c82SChris Mason } else { 200479f95c82SChris Mason /* update the parent key to reflect our changes */ 20055f39d397SChris Mason struct btrfs_disk_key mid_key; 20065f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 20070e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot, 20080e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 20090e82bcfeSDavid Sterba BUG_ON(ret < 0); 20105f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 20115f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 201279f95c82SChris Mason } 2013bb803951SChris Mason 201479f95c82SChris Mason /* update the path */ 20155f39d397SChris Mason if (left) { 20165f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 20175f39d397SChris Mason extent_buffer_get(left); 2018925baeddSChris Mason /* left was locked after cow */ 20195f39d397SChris Mason path->nodes[level] = left; 2020bb803951SChris Mason path->slots[level + 1] -= 1; 2021bb803951SChris Mason path->slots[level] = orig_slot; 2022925baeddSChris Mason if (mid) { 2023925baeddSChris Mason btrfs_tree_unlock(mid); 20245f39d397SChris Mason free_extent_buffer(mid); 2025925baeddSChris Mason } 2026bb803951SChris Mason } else { 20275f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 2028bb803951SChris Mason path->slots[level] = orig_slot; 2029bb803951SChris Mason } 2030bb803951SChris Mason } 203179f95c82SChris Mason /* double check we haven't messed things up */ 2032e20d96d6SChris Mason if (orig_ptr != 20335f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 203479f95c82SChris Mason BUG(); 203554aa1f4dSChris Mason enospc: 2036925baeddSChris Mason if (right) { 2037925baeddSChris Mason btrfs_tree_unlock(right); 20385f39d397SChris Mason free_extent_buffer(right); 2039925baeddSChris Mason } 2040925baeddSChris Mason if (left) { 2041925baeddSChris Mason if (path->nodes[level] != left) 2042925baeddSChris Mason btrfs_tree_unlock(left); 20435f39d397SChris Mason free_extent_buffer(left); 2044925baeddSChris Mason } 2045bb803951SChris Mason return ret; 2046bb803951SChris Mason } 2047bb803951SChris Mason 2048d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 2049d352ac68SChris Mason * when they are completely full. This is also done top down, so we 2050d352ac68SChris Mason * have to be pessimistic. 2051d352ac68SChris Mason */ 2052d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, 2053e66f709bSChris Mason struct btrfs_root *root, 2054e66f709bSChris Mason struct btrfs_path *path, int level) 2055e66f709bSChris Mason { 20560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 20575f39d397SChris Mason struct extent_buffer *right = NULL; 20585f39d397SChris Mason struct extent_buffer *mid; 20595f39d397SChris Mason struct extent_buffer *left = NULL; 20605f39d397SChris Mason struct extent_buffer *parent = NULL; 2061e66f709bSChris Mason int ret = 0; 2062e66f709bSChris Mason int wret; 2063e66f709bSChris Mason int pslot; 2064e66f709bSChris Mason int orig_slot = path->slots[level]; 2065e66f709bSChris Mason 2066e66f709bSChris Mason if (level == 0) 2067e66f709bSChris Mason return 1; 2068e66f709bSChris Mason 20695f39d397SChris Mason mid = path->nodes[level]; 20707bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 2071e66f709bSChris Mason 2072a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 20735f39d397SChris Mason parent = path->nodes[level + 1]; 2074e66f709bSChris Mason pslot = path->slots[level + 1]; 2075a05a9bb1SLi Zefan } 2076e66f709bSChris Mason 20775f39d397SChris Mason if (!parent) 2078e66f709bSChris Mason return 1; 2079e66f709bSChris Mason 2080d0d20b0fSDavid Sterba left = read_node_slot(parent, pslot - 1); 2081fb770ae4SLiu Bo if (IS_ERR(left)) 2082fb770ae4SLiu Bo left = NULL; 2083e66f709bSChris Mason 2084e66f709bSChris Mason /* first, try to make some room in the middle buffer */ 20855f39d397SChris Mason if (left) { 2086e66f709bSChris Mason u32 left_nr; 2087925baeddSChris Mason 2088925baeddSChris Mason btrfs_tree_lock(left); 20898bead258SDavid Sterba btrfs_set_lock_blocking_write(left); 2090b4ce94deSChris Mason 20915f39d397SChris Mason left_nr = btrfs_header_nritems(left); 20920b246afaSJeff Mahoney if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { 209333ade1f8SChris Mason wret = 1; 209433ade1f8SChris Mason } else { 20955f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 20969fa8cfe7SChris Mason pslot - 1, &left); 209754aa1f4dSChris Mason if (ret) 209854aa1f4dSChris Mason wret = 1; 209954aa1f4dSChris Mason else { 21002ff7e61eSJeff Mahoney wret = push_node_left(trans, fs_info, 2101971a1f66SChris Mason left, mid, 0); 210254aa1f4dSChris Mason } 210333ade1f8SChris Mason } 2104e66f709bSChris Mason if (wret < 0) 2105e66f709bSChris Mason ret = wret; 2106e66f709bSChris Mason if (wret == 0) { 21075f39d397SChris Mason struct btrfs_disk_key disk_key; 2108e66f709bSChris Mason orig_slot += left_nr; 21095f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 21100e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot, 21110e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 21120e82bcfeSDavid Sterba BUG_ON(ret < 0); 21135f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 21145f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 21155f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 21165f39d397SChris Mason path->nodes[level] = left; 2117e66f709bSChris Mason path->slots[level + 1] -= 1; 2118e66f709bSChris Mason path->slots[level] = orig_slot; 2119925baeddSChris Mason btrfs_tree_unlock(mid); 21205f39d397SChris Mason free_extent_buffer(mid); 2121e66f709bSChris Mason } else { 2122e66f709bSChris Mason orig_slot -= 21235f39d397SChris Mason btrfs_header_nritems(left); 2124e66f709bSChris Mason path->slots[level] = orig_slot; 2125925baeddSChris Mason btrfs_tree_unlock(left); 21265f39d397SChris Mason free_extent_buffer(left); 2127e66f709bSChris Mason } 2128e66f709bSChris Mason return 0; 2129e66f709bSChris Mason } 2130925baeddSChris Mason btrfs_tree_unlock(left); 21315f39d397SChris Mason free_extent_buffer(left); 2132e66f709bSChris Mason } 2133d0d20b0fSDavid Sterba right = read_node_slot(parent, pslot + 1); 2134fb770ae4SLiu Bo if (IS_ERR(right)) 2135fb770ae4SLiu Bo right = NULL; 2136e66f709bSChris Mason 2137e66f709bSChris Mason /* 2138e66f709bSChris Mason * then try to empty the right most buffer into the middle 2139e66f709bSChris Mason */ 21405f39d397SChris Mason if (right) { 214133ade1f8SChris Mason u32 right_nr; 2142b4ce94deSChris Mason 2143925baeddSChris Mason btrfs_tree_lock(right); 21448bead258SDavid Sterba btrfs_set_lock_blocking_write(right); 2145b4ce94deSChris Mason 21465f39d397SChris Mason right_nr = btrfs_header_nritems(right); 21470b246afaSJeff Mahoney if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { 214833ade1f8SChris Mason wret = 1; 214933ade1f8SChris Mason } else { 21505f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 21515f39d397SChris Mason parent, pslot + 1, 21529fa8cfe7SChris Mason &right); 215354aa1f4dSChris Mason if (ret) 215454aa1f4dSChris Mason wret = 1; 215554aa1f4dSChris Mason else { 21562ff7e61eSJeff Mahoney wret = balance_node_right(trans, fs_info, 21575f39d397SChris Mason right, mid); 215833ade1f8SChris Mason } 215954aa1f4dSChris Mason } 2160e66f709bSChris Mason if (wret < 0) 2161e66f709bSChris Mason ret = wret; 2162e66f709bSChris Mason if (wret == 0) { 21635f39d397SChris Mason struct btrfs_disk_key disk_key; 21645f39d397SChris Mason 21655f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 21660e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(parent, pslot + 1, 21670e82bcfeSDavid Sterba MOD_LOG_KEY_REPLACE, GFP_NOFS); 21680e82bcfeSDavid Sterba BUG_ON(ret < 0); 21695f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 21705f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 21715f39d397SChris Mason 21725f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 21735f39d397SChris Mason path->nodes[level] = right; 2174e66f709bSChris Mason path->slots[level + 1] += 1; 2175e66f709bSChris Mason path->slots[level] = orig_slot - 21765f39d397SChris Mason btrfs_header_nritems(mid); 2177925baeddSChris Mason btrfs_tree_unlock(mid); 21785f39d397SChris Mason free_extent_buffer(mid); 2179e66f709bSChris Mason } else { 2180925baeddSChris Mason btrfs_tree_unlock(right); 21815f39d397SChris Mason free_extent_buffer(right); 2182e66f709bSChris Mason } 2183e66f709bSChris Mason return 0; 2184e66f709bSChris Mason } 2185925baeddSChris Mason btrfs_tree_unlock(right); 21865f39d397SChris Mason free_extent_buffer(right); 2187e66f709bSChris Mason } 2188e66f709bSChris Mason return 1; 2189e66f709bSChris Mason } 2190e66f709bSChris Mason 219174123bd7SChris Mason /* 2192d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 2193d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 21943c69faecSChris Mason */ 21952ff7e61eSJeff Mahoney static void reada_for_search(struct btrfs_fs_info *fs_info, 2196e02119d5SChris Mason struct btrfs_path *path, 219701f46658SChris Mason int level, int slot, u64 objectid) 21983c69faecSChris Mason { 21995f39d397SChris Mason struct extent_buffer *node; 220001f46658SChris Mason struct btrfs_disk_key disk_key; 22013c69faecSChris Mason u32 nritems; 22023c69faecSChris Mason u64 search; 2203a7175319SChris Mason u64 target; 22046b80053dSChris Mason u64 nread = 0; 22055f39d397SChris Mason struct extent_buffer *eb; 22066b80053dSChris Mason u32 nr; 22076b80053dSChris Mason u32 blocksize; 22086b80053dSChris Mason u32 nscan = 0; 2209db94535dSChris Mason 2210a6b6e75eSChris Mason if (level != 1) 22113c69faecSChris Mason return; 22123c69faecSChris Mason 22136702ed49SChris Mason if (!path->nodes[level]) 22146702ed49SChris Mason return; 22156702ed49SChris Mason 22165f39d397SChris Mason node = path->nodes[level]; 2217925baeddSChris Mason 22183c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 22190b246afaSJeff Mahoney blocksize = fs_info->nodesize; 22200b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, search); 22215f39d397SChris Mason if (eb) { 22225f39d397SChris Mason free_extent_buffer(eb); 22233c69faecSChris Mason return; 22243c69faecSChris Mason } 22253c69faecSChris Mason 2226a7175319SChris Mason target = search; 22276b80053dSChris Mason 22285f39d397SChris Mason nritems = btrfs_header_nritems(node); 22296b80053dSChris Mason nr = slot; 223025b8b936SJosef Bacik 22313c69faecSChris Mason while (1) { 2232e4058b54SDavid Sterba if (path->reada == READA_BACK) { 22336b80053dSChris Mason if (nr == 0) 22343c69faecSChris Mason break; 22356b80053dSChris Mason nr--; 2236e4058b54SDavid Sterba } else if (path->reada == READA_FORWARD) { 22376b80053dSChris Mason nr++; 22386b80053dSChris Mason if (nr >= nritems) 22396b80053dSChris Mason break; 22403c69faecSChris Mason } 2241e4058b54SDavid Sterba if (path->reada == READA_BACK && objectid) { 224201f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 224301f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 224401f46658SChris Mason break; 224501f46658SChris Mason } 22466b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 2247a7175319SChris Mason if ((search <= target && target - search <= 65536) || 2248a7175319SChris Mason (search > target && search - target <= 65536)) { 22492ff7e61eSJeff Mahoney readahead_tree_block(fs_info, search); 22506b80053dSChris Mason nread += blocksize; 22513c69faecSChris Mason } 22526b80053dSChris Mason nscan++; 2253a7175319SChris Mason if ((nread > 65536 || nscan > 32)) 22546b80053dSChris Mason break; 22553c69faecSChris Mason } 22563c69faecSChris Mason } 2257925baeddSChris Mason 22582ff7e61eSJeff Mahoney static noinline void reada_for_balance(struct btrfs_fs_info *fs_info, 2259b4ce94deSChris Mason struct btrfs_path *path, int level) 2260b4ce94deSChris Mason { 2261b4ce94deSChris Mason int slot; 2262b4ce94deSChris Mason int nritems; 2263b4ce94deSChris Mason struct extent_buffer *parent; 2264b4ce94deSChris Mason struct extent_buffer *eb; 2265b4ce94deSChris Mason u64 gen; 2266b4ce94deSChris Mason u64 block1 = 0; 2267b4ce94deSChris Mason u64 block2 = 0; 2268b4ce94deSChris Mason 22698c594ea8SChris Mason parent = path->nodes[level + 1]; 2270b4ce94deSChris Mason if (!parent) 22710b08851fSJosef Bacik return; 2272b4ce94deSChris Mason 2273b4ce94deSChris Mason nritems = btrfs_header_nritems(parent); 22748c594ea8SChris Mason slot = path->slots[level + 1]; 2275b4ce94deSChris Mason 2276b4ce94deSChris Mason if (slot > 0) { 2277b4ce94deSChris Mason block1 = btrfs_node_blockptr(parent, slot - 1); 2278b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot - 1); 22790b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, block1); 2280b9fab919SChris Mason /* 2281b9fab919SChris Mason * if we get -eagain from btrfs_buffer_uptodate, we 2282b9fab919SChris Mason * don't want to return eagain here. That will loop 2283b9fab919SChris Mason * forever 2284b9fab919SChris Mason */ 2285b9fab919SChris Mason if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) 2286b4ce94deSChris Mason block1 = 0; 2287b4ce94deSChris Mason free_extent_buffer(eb); 2288b4ce94deSChris Mason } 22898c594ea8SChris Mason if (slot + 1 < nritems) { 2290b4ce94deSChris Mason block2 = btrfs_node_blockptr(parent, slot + 1); 2291b4ce94deSChris Mason gen = btrfs_node_ptr_generation(parent, slot + 1); 22920b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, block2); 2293b9fab919SChris Mason if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) 2294b4ce94deSChris Mason block2 = 0; 2295b4ce94deSChris Mason free_extent_buffer(eb); 2296b4ce94deSChris Mason } 22978c594ea8SChris Mason 2298b4ce94deSChris Mason if (block1) 22992ff7e61eSJeff Mahoney readahead_tree_block(fs_info, block1); 2300b4ce94deSChris Mason if (block2) 23012ff7e61eSJeff Mahoney readahead_tree_block(fs_info, block2); 2302b4ce94deSChris Mason } 2303b4ce94deSChris Mason 2304b4ce94deSChris Mason 2305b4ce94deSChris Mason /* 2306d397712bSChris Mason * when we walk down the tree, it is usually safe to unlock the higher layers 2307d397712bSChris Mason * in the tree. The exceptions are when our path goes through slot 0, because 2308d397712bSChris Mason * operations on the tree might require changing key pointers higher up in the 2309d397712bSChris Mason * tree. 2310d352ac68SChris Mason * 2311d397712bSChris Mason * callers might also have set path->keep_locks, which tells this code to keep 2312d397712bSChris Mason * the lock if the path points to the last slot in the block. This is part of 2313d397712bSChris Mason * walking through the tree, and selecting the next slot in the higher block. 2314d352ac68SChris Mason * 2315d397712bSChris Mason * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so 2316d397712bSChris Mason * if lowest_unlock is 1, level 0 won't be unlocked 2317d352ac68SChris Mason */ 2318e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level, 2319f7c79f30SChris Mason int lowest_unlock, int min_write_lock_level, 2320f7c79f30SChris Mason int *write_lock_level) 2321925baeddSChris Mason { 2322925baeddSChris Mason int i; 2323925baeddSChris Mason int skip_level = level; 2324051e1b9fSChris Mason int no_skips = 0; 2325925baeddSChris Mason struct extent_buffer *t; 2326925baeddSChris Mason 2327925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2328925baeddSChris Mason if (!path->nodes[i]) 2329925baeddSChris Mason break; 2330925baeddSChris Mason if (!path->locks[i]) 2331925baeddSChris Mason break; 2332051e1b9fSChris Mason if (!no_skips && path->slots[i] == 0) { 2333925baeddSChris Mason skip_level = i + 1; 2334925baeddSChris Mason continue; 2335925baeddSChris Mason } 2336051e1b9fSChris Mason if (!no_skips && path->keep_locks) { 2337925baeddSChris Mason u32 nritems; 2338925baeddSChris Mason t = path->nodes[i]; 2339925baeddSChris Mason nritems = btrfs_header_nritems(t); 2340051e1b9fSChris Mason if (nritems < 1 || path->slots[i] >= nritems - 1) { 2341925baeddSChris Mason skip_level = i + 1; 2342925baeddSChris Mason continue; 2343925baeddSChris Mason } 2344925baeddSChris Mason } 2345051e1b9fSChris Mason if (skip_level < i && i >= lowest_unlock) 2346051e1b9fSChris Mason no_skips = 1; 2347051e1b9fSChris Mason 2348925baeddSChris Mason t = path->nodes[i]; 2349d80bb3f9SLiu Bo if (i >= lowest_unlock && i > skip_level) { 2350bd681513SChris Mason btrfs_tree_unlock_rw(t, path->locks[i]); 2351925baeddSChris Mason path->locks[i] = 0; 2352f7c79f30SChris Mason if (write_lock_level && 2353f7c79f30SChris Mason i > min_write_lock_level && 2354f7c79f30SChris Mason i <= *write_lock_level) { 2355f7c79f30SChris Mason *write_lock_level = i - 1; 2356f7c79f30SChris Mason } 2357925baeddSChris Mason } 2358925baeddSChris Mason } 2359925baeddSChris Mason } 2360925baeddSChris Mason 23613c69faecSChris Mason /* 2362b4ce94deSChris Mason * This releases any locks held in the path starting at level and 2363b4ce94deSChris Mason * going all the way up to the root. 2364b4ce94deSChris Mason * 2365b4ce94deSChris Mason * btrfs_search_slot will keep the lock held on higher nodes in a few 2366b4ce94deSChris Mason * corner cases, such as COW of the block at slot zero in the node. This 2367b4ce94deSChris Mason * ignores those rules, and it should only be called when there are no 2368b4ce94deSChris Mason * more updates to be done higher up in the tree. 2369b4ce94deSChris Mason */ 2370b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) 2371b4ce94deSChris Mason { 2372b4ce94deSChris Mason int i; 2373b4ce94deSChris Mason 237409a2a8f9SJosef Bacik if (path->keep_locks) 2375b4ce94deSChris Mason return; 2376b4ce94deSChris Mason 2377b4ce94deSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2378b4ce94deSChris Mason if (!path->nodes[i]) 237912f4daccSChris Mason continue; 2380b4ce94deSChris Mason if (!path->locks[i]) 238112f4daccSChris Mason continue; 2382bd681513SChris Mason btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); 2383b4ce94deSChris Mason path->locks[i] = 0; 2384b4ce94deSChris Mason } 2385b4ce94deSChris Mason } 2386b4ce94deSChris Mason 2387b4ce94deSChris Mason /* 2388c8c42864SChris Mason * helper function for btrfs_search_slot. The goal is to find a block 2389c8c42864SChris Mason * in cache without setting the path to blocking. If we find the block 2390c8c42864SChris Mason * we return zero and the path is unchanged. 2391c8c42864SChris Mason * 2392c8c42864SChris Mason * If we can't find the block, we set the path blocking and do some 2393c8c42864SChris Mason * reada. -EAGAIN is returned and the search must be repeated. 2394c8c42864SChris Mason */ 2395c8c42864SChris Mason static int 2396d07b8528SLiu Bo read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, 2397c8c42864SChris Mason struct extent_buffer **eb_ret, int level, int slot, 2398cda79c54SDavid Sterba const struct btrfs_key *key) 2399c8c42864SChris Mason { 24000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2401c8c42864SChris Mason u64 blocknr; 2402c8c42864SChris Mason u64 gen; 2403c8c42864SChris Mason struct extent_buffer *b = *eb_ret; 2404c8c42864SChris Mason struct extent_buffer *tmp; 2405581c1760SQu Wenruo struct btrfs_key first_key; 240676a05b35SChris Mason int ret; 2407581c1760SQu Wenruo int parent_level; 2408c8c42864SChris Mason 2409c8c42864SChris Mason blocknr = btrfs_node_blockptr(b, slot); 2410c8c42864SChris Mason gen = btrfs_node_ptr_generation(b, slot); 2411581c1760SQu Wenruo parent_level = btrfs_header_level(b); 2412581c1760SQu Wenruo btrfs_node_key_to_cpu(b, &first_key, slot); 2413c8c42864SChris Mason 24140b246afaSJeff Mahoney tmp = find_extent_buffer(fs_info, blocknr); 2415cb44921aSChris Mason if (tmp) { 2416b9fab919SChris Mason /* first we do an atomic uptodate check */ 2417b9fab919SChris Mason if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { 2418448de471SQu Wenruo /* 2419448de471SQu Wenruo * Do extra check for first_key, eb can be stale due to 2420448de471SQu Wenruo * being cached, read from scrub, or have multiple 2421448de471SQu Wenruo * parents (shared tree blocks). 2422448de471SQu Wenruo */ 2423*e064d5e9SDavid Sterba if (btrfs_verify_level_key(tmp, 2424448de471SQu Wenruo parent_level - 1, &first_key, gen)) { 2425448de471SQu Wenruo free_extent_buffer(tmp); 2426448de471SQu Wenruo return -EUCLEAN; 2427448de471SQu Wenruo } 2428c8c42864SChris Mason *eb_ret = tmp; 2429c8c42864SChris Mason return 0; 2430c8c42864SChris Mason } 2431bdf7c00eSJosef Bacik 2432cb44921aSChris Mason /* the pages were up to date, but we failed 2433cb44921aSChris Mason * the generation number check. Do a full 2434cb44921aSChris Mason * read for the generation number that is correct. 2435cb44921aSChris Mason * We must do this without dropping locks so 2436cb44921aSChris Mason * we can trust our generation number 2437cb44921aSChris Mason */ 2438bd681513SChris Mason btrfs_set_path_blocking(p); 2439bd681513SChris Mason 2440b9fab919SChris Mason /* now we're allowed to do a blocking uptodate check */ 2441581c1760SQu Wenruo ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key); 2442bdf7c00eSJosef Bacik if (!ret) { 2443cb44921aSChris Mason *eb_ret = tmp; 2444cb44921aSChris Mason return 0; 2445cb44921aSChris Mason } 2446cb44921aSChris Mason free_extent_buffer(tmp); 2447b3b4aa74SDavid Sterba btrfs_release_path(p); 2448cb44921aSChris Mason return -EIO; 2449cb44921aSChris Mason } 2450c8c42864SChris Mason 2451c8c42864SChris Mason /* 2452c8c42864SChris Mason * reduce lock contention at high levels 2453c8c42864SChris Mason * of the btree by dropping locks before 245476a05b35SChris Mason * we read. Don't release the lock on the current 245576a05b35SChris Mason * level because we need to walk this node to figure 245676a05b35SChris Mason * out which blocks to read. 2457c8c42864SChris Mason */ 24588c594ea8SChris Mason btrfs_unlock_up_safe(p, level + 1); 24598c594ea8SChris Mason btrfs_set_path_blocking(p); 24608c594ea8SChris Mason 2461e4058b54SDavid Sterba if (p->reada != READA_NONE) 24622ff7e61eSJeff Mahoney reada_for_search(fs_info, p, level, slot, key->objectid); 2463c8c42864SChris Mason 246476a05b35SChris Mason ret = -EAGAIN; 246502a3307aSLiu Bo tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1, 2466581c1760SQu Wenruo &first_key); 246764c043deSLiu Bo if (!IS_ERR(tmp)) { 246876a05b35SChris Mason /* 246976a05b35SChris Mason * If the read above didn't mark this buffer up to date, 247076a05b35SChris Mason * it will never end up being up to date. Set ret to EIO now 247176a05b35SChris Mason * and give up so that our caller doesn't loop forever 247276a05b35SChris Mason * on our EAGAINs. 247376a05b35SChris Mason */ 2474e6a1d6fdSLiu Bo if (!extent_buffer_uptodate(tmp)) 247576a05b35SChris Mason ret = -EIO; 2476c8c42864SChris Mason free_extent_buffer(tmp); 2477c871b0f2SLiu Bo } else { 2478c871b0f2SLiu Bo ret = PTR_ERR(tmp); 247976a05b35SChris Mason } 248002a3307aSLiu Bo 248102a3307aSLiu Bo btrfs_release_path(p); 248276a05b35SChris Mason return ret; 2483c8c42864SChris Mason } 2484c8c42864SChris Mason 2485c8c42864SChris Mason /* 2486c8c42864SChris Mason * helper function for btrfs_search_slot. This does all of the checks 2487c8c42864SChris Mason * for node-level blocks and does any balancing required based on 2488c8c42864SChris Mason * the ins_len. 2489c8c42864SChris Mason * 2490c8c42864SChris Mason * If no extra work was required, zero is returned. If we had to 2491c8c42864SChris Mason * drop the path, -EAGAIN is returned and btrfs_search_slot must 2492c8c42864SChris Mason * start over 2493c8c42864SChris Mason */ 2494c8c42864SChris Mason static int 2495c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans, 2496c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 2497bd681513SChris Mason struct extent_buffer *b, int level, int ins_len, 2498bd681513SChris Mason int *write_lock_level) 2499c8c42864SChris Mason { 25000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2501c8c42864SChris Mason int ret; 25020b246afaSJeff Mahoney 2503c8c42864SChris Mason if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= 25040b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) { 2505c8c42864SChris Mason int sret; 2506c8c42864SChris Mason 2507bd681513SChris Mason if (*write_lock_level < level + 1) { 2508bd681513SChris Mason *write_lock_level = level + 1; 2509bd681513SChris Mason btrfs_release_path(p); 2510bd681513SChris Mason goto again; 2511bd681513SChris Mason } 2512bd681513SChris Mason 2513c8c42864SChris Mason btrfs_set_path_blocking(p); 25142ff7e61eSJeff Mahoney reada_for_balance(fs_info, p, level); 2515c8c42864SChris Mason sret = split_node(trans, root, p, level); 2516c8c42864SChris Mason 2517c8c42864SChris Mason BUG_ON(sret > 0); 2518c8c42864SChris Mason if (sret) { 2519c8c42864SChris Mason ret = sret; 2520c8c42864SChris Mason goto done; 2521c8c42864SChris Mason } 2522c8c42864SChris Mason b = p->nodes[level]; 2523c8c42864SChris Mason } else if (ins_len < 0 && btrfs_header_nritems(b) < 25240b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) { 2525c8c42864SChris Mason int sret; 2526c8c42864SChris Mason 2527bd681513SChris Mason if (*write_lock_level < level + 1) { 2528bd681513SChris Mason *write_lock_level = level + 1; 2529bd681513SChris Mason btrfs_release_path(p); 2530bd681513SChris Mason goto again; 2531bd681513SChris Mason } 2532bd681513SChris Mason 2533c8c42864SChris Mason btrfs_set_path_blocking(p); 25342ff7e61eSJeff Mahoney reada_for_balance(fs_info, p, level); 2535c8c42864SChris Mason sret = balance_level(trans, root, p, level); 2536c8c42864SChris Mason 2537c8c42864SChris Mason if (sret) { 2538c8c42864SChris Mason ret = sret; 2539c8c42864SChris Mason goto done; 2540c8c42864SChris Mason } 2541c8c42864SChris Mason b = p->nodes[level]; 2542c8c42864SChris Mason if (!b) { 2543b3b4aa74SDavid Sterba btrfs_release_path(p); 2544c8c42864SChris Mason goto again; 2545c8c42864SChris Mason } 2546c8c42864SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 2547c8c42864SChris Mason } 2548c8c42864SChris Mason return 0; 2549c8c42864SChris Mason 2550c8c42864SChris Mason again: 2551c8c42864SChris Mason ret = -EAGAIN; 2552c8c42864SChris Mason done: 2553c8c42864SChris Mason return ret; 2554c8c42864SChris Mason } 2555c8c42864SChris Mason 2556310712b2SOmar Sandoval static int key_search(struct extent_buffer *b, const struct btrfs_key *key, 2557d7396f07SFilipe David Borba Manana int level, int *prev_cmp, int *slot) 2558d7396f07SFilipe David Borba Manana { 2559d7396f07SFilipe David Borba Manana if (*prev_cmp != 0) { 2560a74b35ecSNikolay Borisov *prev_cmp = btrfs_bin_search(b, key, level, slot); 2561d7396f07SFilipe David Borba Manana return *prev_cmp; 2562d7396f07SFilipe David Borba Manana } 2563d7396f07SFilipe David Borba Manana 2564d7396f07SFilipe David Borba Manana *slot = 0; 2565d7396f07SFilipe David Borba Manana 2566d7396f07SFilipe David Borba Manana return 0; 2567d7396f07SFilipe David Borba Manana } 2568d7396f07SFilipe David Borba Manana 2569381cf658SDavid Sterba int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, 2570e33d5c3dSKelley Nielsen u64 iobjectid, u64 ioff, u8 key_type, 2571e33d5c3dSKelley Nielsen struct btrfs_key *found_key) 2572e33d5c3dSKelley Nielsen { 2573e33d5c3dSKelley Nielsen int ret; 2574e33d5c3dSKelley Nielsen struct btrfs_key key; 2575e33d5c3dSKelley Nielsen struct extent_buffer *eb; 2576381cf658SDavid Sterba 2577381cf658SDavid Sterba ASSERT(path); 25781d4c08e0SDavid Sterba ASSERT(found_key); 2579e33d5c3dSKelley Nielsen 2580e33d5c3dSKelley Nielsen key.type = key_type; 2581e33d5c3dSKelley Nielsen key.objectid = iobjectid; 2582e33d5c3dSKelley Nielsen key.offset = ioff; 2583e33d5c3dSKelley Nielsen 2584e33d5c3dSKelley Nielsen ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 25851d4c08e0SDavid Sterba if (ret < 0) 2586e33d5c3dSKelley Nielsen return ret; 2587e33d5c3dSKelley Nielsen 2588e33d5c3dSKelley Nielsen eb = path->nodes[0]; 2589e33d5c3dSKelley Nielsen if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { 2590e33d5c3dSKelley Nielsen ret = btrfs_next_leaf(fs_root, path); 2591e33d5c3dSKelley Nielsen if (ret) 2592e33d5c3dSKelley Nielsen return ret; 2593e33d5c3dSKelley Nielsen eb = path->nodes[0]; 2594e33d5c3dSKelley Nielsen } 2595e33d5c3dSKelley Nielsen 2596e33d5c3dSKelley Nielsen btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); 2597e33d5c3dSKelley Nielsen if (found_key->type != key.type || 2598e33d5c3dSKelley Nielsen found_key->objectid != key.objectid) 2599e33d5c3dSKelley Nielsen return 1; 2600e33d5c3dSKelley Nielsen 2601e33d5c3dSKelley Nielsen return 0; 2602e33d5c3dSKelley Nielsen } 2603e33d5c3dSKelley Nielsen 26041fc28d8eSLiu Bo static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root, 26051fc28d8eSLiu Bo struct btrfs_path *p, 26061fc28d8eSLiu Bo int write_lock_level) 26071fc28d8eSLiu Bo { 26081fc28d8eSLiu Bo struct btrfs_fs_info *fs_info = root->fs_info; 26091fc28d8eSLiu Bo struct extent_buffer *b; 26101fc28d8eSLiu Bo int root_lock; 26111fc28d8eSLiu Bo int level = 0; 26121fc28d8eSLiu Bo 26131fc28d8eSLiu Bo /* We try very hard to do read locks on the root */ 26141fc28d8eSLiu Bo root_lock = BTRFS_READ_LOCK; 26151fc28d8eSLiu Bo 26161fc28d8eSLiu Bo if (p->search_commit_root) { 2617be6821f8SFilipe Manana /* 2618be6821f8SFilipe Manana * The commit roots are read only so we always do read locks, 2619be6821f8SFilipe Manana * and we always must hold the commit_root_sem when doing 2620be6821f8SFilipe Manana * searches on them, the only exception is send where we don't 2621be6821f8SFilipe Manana * want to block transaction commits for a long time, so 2622be6821f8SFilipe Manana * we need to clone the commit root in order to avoid races 2623be6821f8SFilipe Manana * with transaction commits that create a snapshot of one of 2624be6821f8SFilipe Manana * the roots used by a send operation. 2625be6821f8SFilipe Manana */ 2626be6821f8SFilipe Manana if (p->need_commit_sem) { 26271fc28d8eSLiu Bo down_read(&fs_info->commit_root_sem); 2628be6821f8SFilipe Manana b = btrfs_clone_extent_buffer(root->commit_root); 2629be6821f8SFilipe Manana up_read(&fs_info->commit_root_sem); 2630be6821f8SFilipe Manana if (!b) 2631be6821f8SFilipe Manana return ERR_PTR(-ENOMEM); 2632be6821f8SFilipe Manana 2633be6821f8SFilipe Manana } else { 26341fc28d8eSLiu Bo b = root->commit_root; 26351fc28d8eSLiu Bo extent_buffer_get(b); 2636be6821f8SFilipe Manana } 26371fc28d8eSLiu Bo level = btrfs_header_level(b); 2638f9ddfd05SLiu Bo /* 2639f9ddfd05SLiu Bo * Ensure that all callers have set skip_locking when 2640f9ddfd05SLiu Bo * p->search_commit_root = 1. 2641f9ddfd05SLiu Bo */ 2642f9ddfd05SLiu Bo ASSERT(p->skip_locking == 1); 26431fc28d8eSLiu Bo 26441fc28d8eSLiu Bo goto out; 26451fc28d8eSLiu Bo } 26461fc28d8eSLiu Bo 26471fc28d8eSLiu Bo if (p->skip_locking) { 26481fc28d8eSLiu Bo b = btrfs_root_node(root); 26491fc28d8eSLiu Bo level = btrfs_header_level(b); 26501fc28d8eSLiu Bo goto out; 26511fc28d8eSLiu Bo } 26521fc28d8eSLiu Bo 26531fc28d8eSLiu Bo /* 2654662c653bSLiu Bo * If the level is set to maximum, we can skip trying to get the read 2655662c653bSLiu Bo * lock. 2656662c653bSLiu Bo */ 2657662c653bSLiu Bo if (write_lock_level < BTRFS_MAX_LEVEL) { 2658662c653bSLiu Bo /* 2659662c653bSLiu Bo * We don't know the level of the root node until we actually 2660662c653bSLiu Bo * have it read locked 26611fc28d8eSLiu Bo */ 26621fc28d8eSLiu Bo b = btrfs_read_lock_root_node(root); 26631fc28d8eSLiu Bo level = btrfs_header_level(b); 26641fc28d8eSLiu Bo if (level > write_lock_level) 26651fc28d8eSLiu Bo goto out; 26661fc28d8eSLiu Bo 2667662c653bSLiu Bo /* Whoops, must trade for write lock */ 26681fc28d8eSLiu Bo btrfs_tree_read_unlock(b); 26691fc28d8eSLiu Bo free_extent_buffer(b); 2670662c653bSLiu Bo } 2671662c653bSLiu Bo 26721fc28d8eSLiu Bo b = btrfs_lock_root_node(root); 26731fc28d8eSLiu Bo root_lock = BTRFS_WRITE_LOCK; 26741fc28d8eSLiu Bo 26751fc28d8eSLiu Bo /* The level might have changed, check again */ 26761fc28d8eSLiu Bo level = btrfs_header_level(b); 26771fc28d8eSLiu Bo 26781fc28d8eSLiu Bo out: 26791fc28d8eSLiu Bo p->nodes[level] = b; 26801fc28d8eSLiu Bo if (!p->skip_locking) 26811fc28d8eSLiu Bo p->locks[level] = root_lock; 26821fc28d8eSLiu Bo /* 26831fc28d8eSLiu Bo * Callers are responsible for dropping b's references. 26841fc28d8eSLiu Bo */ 26851fc28d8eSLiu Bo return b; 26861fc28d8eSLiu Bo } 26871fc28d8eSLiu Bo 26881fc28d8eSLiu Bo 2689c8c42864SChris Mason /* 26904271eceaSNikolay Borisov * btrfs_search_slot - look for a key in a tree and perform necessary 26914271eceaSNikolay Borisov * modifications to preserve tree invariants. 269274123bd7SChris Mason * 26934271eceaSNikolay Borisov * @trans: Handle of transaction, used when modifying the tree 26944271eceaSNikolay Borisov * @p: Holds all btree nodes along the search path 26954271eceaSNikolay Borisov * @root: The root node of the tree 26964271eceaSNikolay Borisov * @key: The key we are looking for 26974271eceaSNikolay Borisov * @ins_len: Indicates purpose of search, for inserts it is 1, for 26984271eceaSNikolay Borisov * deletions it's -1. 0 for plain searches 26994271eceaSNikolay Borisov * @cow: boolean should CoW operations be performed. Must always be 1 27004271eceaSNikolay Borisov * when modifying the tree. 270197571fd0SChris Mason * 27024271eceaSNikolay Borisov * If @ins_len > 0, nodes and leaves will be split as we walk down the tree. 27034271eceaSNikolay Borisov * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible) 27044271eceaSNikolay Borisov * 27054271eceaSNikolay Borisov * If @key is found, 0 is returned and you can find the item in the leaf level 27064271eceaSNikolay Borisov * of the path (level 0) 27074271eceaSNikolay Borisov * 27084271eceaSNikolay Borisov * If @key isn't found, 1 is returned and the leaf level of the path (level 0) 27094271eceaSNikolay Borisov * points to the slot where it should be inserted 27104271eceaSNikolay Borisov * 27114271eceaSNikolay Borisov * If an error is encountered while searching the tree a negative error number 27124271eceaSNikolay Borisov * is returned 271374123bd7SChris Mason */ 2714310712b2SOmar Sandoval int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2715310712b2SOmar Sandoval const struct btrfs_key *key, struct btrfs_path *p, 2716310712b2SOmar Sandoval int ins_len, int cow) 2717be0e5c09SChris Mason { 27185f39d397SChris Mason struct extent_buffer *b; 2719be0e5c09SChris Mason int slot; 2720be0e5c09SChris Mason int ret; 272133c66f43SYan Zheng int err; 2722be0e5c09SChris Mason int level; 2723925baeddSChris Mason int lowest_unlock = 1; 2724bd681513SChris Mason /* everything at write_lock_level or lower must be write locked */ 2725bd681513SChris Mason int write_lock_level = 0; 27269f3a7427SChris Mason u8 lowest_level = 0; 2727f7c79f30SChris Mason int min_write_lock_level; 2728d7396f07SFilipe David Borba Manana int prev_cmp; 27299f3a7427SChris Mason 27306702ed49SChris Mason lowest_level = p->lowest_level; 2731323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 273222b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 2733eb653de1SFilipe David Borba Manana BUG_ON(!cow && ins_len); 273425179201SJosef Bacik 2735bd681513SChris Mason if (ins_len < 0) { 2736925baeddSChris Mason lowest_unlock = 2; 273765b51a00SChris Mason 2738bd681513SChris Mason /* when we are removing items, we might have to go up to level 2739bd681513SChris Mason * two as we update tree pointers Make sure we keep write 2740bd681513SChris Mason * for those levels as well 2741bd681513SChris Mason */ 2742bd681513SChris Mason write_lock_level = 2; 2743bd681513SChris Mason } else if (ins_len > 0) { 2744bd681513SChris Mason /* 2745bd681513SChris Mason * for inserting items, make sure we have a write lock on 2746bd681513SChris Mason * level 1 so we can update keys 2747bd681513SChris Mason */ 2748bd681513SChris Mason write_lock_level = 1; 2749bd681513SChris Mason } 2750bd681513SChris Mason 2751bd681513SChris Mason if (!cow) 2752bd681513SChris Mason write_lock_level = -1; 2753bd681513SChris Mason 275409a2a8f9SJosef Bacik if (cow && (p->keep_locks || p->lowest_level)) 2755bd681513SChris Mason write_lock_level = BTRFS_MAX_LEVEL; 2756bd681513SChris Mason 2757f7c79f30SChris Mason min_write_lock_level = write_lock_level; 2758f7c79f30SChris Mason 2759bb803951SChris Mason again: 2760d7396f07SFilipe David Borba Manana prev_cmp = -1; 27611fc28d8eSLiu Bo b = btrfs_search_slot_get_root(root, p, write_lock_level); 2762be6821f8SFilipe Manana if (IS_ERR(b)) { 2763be6821f8SFilipe Manana ret = PTR_ERR(b); 2764be6821f8SFilipe Manana goto done; 2765be6821f8SFilipe Manana } 2766925baeddSChris Mason 2767eb60ceacSChris Mason while (b) { 27685f39d397SChris Mason level = btrfs_header_level(b); 276965b51a00SChris Mason 277065b51a00SChris Mason /* 277165b51a00SChris Mason * setup the path here so we can release it under lock 277265b51a00SChris Mason * contention with the cow code 277365b51a00SChris Mason */ 277402217ed2SChris Mason if (cow) { 27759ea2c7c9SNikolay Borisov bool last_level = (level == (BTRFS_MAX_LEVEL - 1)); 27769ea2c7c9SNikolay Borisov 2777c8c42864SChris Mason /* 2778c8c42864SChris Mason * if we don't really need to cow this block 2779c8c42864SChris Mason * then we don't want to set the path blocking, 2780c8c42864SChris Mason * so we test it here 2781c8c42864SChris Mason */ 278264c12921SJeff Mahoney if (!should_cow_block(trans, root, b)) { 278364c12921SJeff Mahoney trans->dirty = true; 278465b51a00SChris Mason goto cow_done; 278564c12921SJeff Mahoney } 27865d4f98a2SYan Zheng 2787bd681513SChris Mason /* 2788bd681513SChris Mason * must have write locks on this node and the 2789bd681513SChris Mason * parent 2790bd681513SChris Mason */ 27915124e00eSJosef Bacik if (level > write_lock_level || 27925124e00eSJosef Bacik (level + 1 > write_lock_level && 27935124e00eSJosef Bacik level + 1 < BTRFS_MAX_LEVEL && 27945124e00eSJosef Bacik p->nodes[level + 1])) { 2795bd681513SChris Mason write_lock_level = level + 1; 2796bd681513SChris Mason btrfs_release_path(p); 2797bd681513SChris Mason goto again; 2798bd681513SChris Mason } 2799bd681513SChris Mason 2800160f4089SFilipe Manana btrfs_set_path_blocking(p); 28019ea2c7c9SNikolay Borisov if (last_level) 28029ea2c7c9SNikolay Borisov err = btrfs_cow_block(trans, root, b, NULL, 0, 28039ea2c7c9SNikolay Borisov &b); 28049ea2c7c9SNikolay Borisov else 280533c66f43SYan Zheng err = btrfs_cow_block(trans, root, b, 2806e20d96d6SChris Mason p->nodes[level + 1], 28079fa8cfe7SChris Mason p->slots[level + 1], &b); 280833c66f43SYan Zheng if (err) { 280933c66f43SYan Zheng ret = err; 281065b51a00SChris Mason goto done; 281154aa1f4dSChris Mason } 281202217ed2SChris Mason } 281365b51a00SChris Mason cow_done: 2814eb60ceacSChris Mason p->nodes[level] = b; 281552398340SLiu Bo /* 281652398340SLiu Bo * Leave path with blocking locks to avoid massive 281752398340SLiu Bo * lock context switch, this is made on purpose. 281852398340SLiu Bo */ 2819b4ce94deSChris Mason 2820b4ce94deSChris Mason /* 2821b4ce94deSChris Mason * we have a lock on b and as long as we aren't changing 2822b4ce94deSChris Mason * the tree, there is no way to for the items in b to change. 2823b4ce94deSChris Mason * It is safe to drop the lock on our parent before we 2824b4ce94deSChris Mason * go through the expensive btree search on b. 2825b4ce94deSChris Mason * 2826eb653de1SFilipe David Borba Manana * If we're inserting or deleting (ins_len != 0), then we might 2827eb653de1SFilipe David Borba Manana * be changing slot zero, which may require changing the parent. 2828eb653de1SFilipe David Borba Manana * So, we can't drop the lock until after we know which slot 2829eb653de1SFilipe David Borba Manana * we're operating on. 2830b4ce94deSChris Mason */ 2831eb653de1SFilipe David Borba Manana if (!ins_len && !p->keep_locks) { 2832eb653de1SFilipe David Borba Manana int u = level + 1; 2833eb653de1SFilipe David Borba Manana 2834eb653de1SFilipe David Borba Manana if (u < BTRFS_MAX_LEVEL && p->locks[u]) { 2835eb653de1SFilipe David Borba Manana btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]); 2836eb653de1SFilipe David Borba Manana p->locks[u] = 0; 2837eb653de1SFilipe David Borba Manana } 2838eb653de1SFilipe David Borba Manana } 2839b4ce94deSChris Mason 2840d7396f07SFilipe David Borba Manana ret = key_search(b, key, level, &prev_cmp, &slot); 2841415b35a5SLiu Bo if (ret < 0) 2842415b35a5SLiu Bo goto done; 2843b4ce94deSChris Mason 28445f39d397SChris Mason if (level != 0) { 284533c66f43SYan Zheng int dec = 0; 284633c66f43SYan Zheng if (ret && slot > 0) { 284733c66f43SYan Zheng dec = 1; 2848be0e5c09SChris Mason slot -= 1; 284933c66f43SYan Zheng } 2850be0e5c09SChris Mason p->slots[level] = slot; 285133c66f43SYan Zheng err = setup_nodes_for_search(trans, root, p, b, level, 2852bd681513SChris Mason ins_len, &write_lock_level); 285333c66f43SYan Zheng if (err == -EAGAIN) 2854b4ce94deSChris Mason goto again; 285533c66f43SYan Zheng if (err) { 285633c66f43SYan Zheng ret = err; 285765b51a00SChris Mason goto done; 285833c66f43SYan Zheng } 28595c680ed6SChris Mason b = p->nodes[level]; 28605c680ed6SChris Mason slot = p->slots[level]; 2861b4ce94deSChris Mason 2862bd681513SChris Mason /* 2863bd681513SChris Mason * slot 0 is special, if we change the key 2864bd681513SChris Mason * we have to update the parent pointer 2865bd681513SChris Mason * which means we must have a write lock 2866bd681513SChris Mason * on the parent 2867bd681513SChris Mason */ 2868eb653de1SFilipe David Borba Manana if (slot == 0 && ins_len && 2869bd681513SChris Mason write_lock_level < level + 1) { 2870bd681513SChris Mason write_lock_level = level + 1; 2871bd681513SChris Mason btrfs_release_path(p); 2872bd681513SChris Mason goto again; 2873bd681513SChris Mason } 2874bd681513SChris Mason 2875f7c79f30SChris Mason unlock_up(p, level, lowest_unlock, 2876f7c79f30SChris Mason min_write_lock_level, &write_lock_level); 2877f9efa9c7SChris Mason 2878925baeddSChris Mason if (level == lowest_level) { 287933c66f43SYan Zheng if (dec) 288033c66f43SYan Zheng p->slots[level]++; 28815b21f2edSZheng Yan goto done; 2882925baeddSChris Mason } 2883ca7a79adSChris Mason 2884d07b8528SLiu Bo err = read_block_for_search(root, p, &b, level, 2885cda79c54SDavid Sterba slot, key); 288633c66f43SYan Zheng if (err == -EAGAIN) 2887051e1b9fSChris Mason goto again; 288833c66f43SYan Zheng if (err) { 288933c66f43SYan Zheng ret = err; 289076a05b35SChris Mason goto done; 289133c66f43SYan Zheng } 289276a05b35SChris Mason 2893b4ce94deSChris Mason if (!p->skip_locking) { 2894bd681513SChris Mason level = btrfs_header_level(b); 2895bd681513SChris Mason if (level <= write_lock_level) { 2896bd681513SChris Mason err = btrfs_try_tree_write_lock(b); 289733c66f43SYan Zheng if (!err) { 2898b4ce94deSChris Mason btrfs_set_path_blocking(p); 2899925baeddSChris Mason btrfs_tree_lock(b); 2900b4ce94deSChris Mason } 2901bd681513SChris Mason p->locks[level] = BTRFS_WRITE_LOCK; 2902bd681513SChris Mason } else { 2903f82c458aSChris Mason err = btrfs_tree_read_lock_atomic(b); 2904bd681513SChris Mason if (!err) { 2905bd681513SChris Mason btrfs_set_path_blocking(p); 2906bd681513SChris Mason btrfs_tree_read_lock(b); 2907bd681513SChris Mason } 2908bd681513SChris Mason p->locks[level] = BTRFS_READ_LOCK; 2909bd681513SChris Mason } 2910bd681513SChris Mason p->nodes[level] = b; 2911b4ce94deSChris Mason } 2912be0e5c09SChris Mason } else { 2913be0e5c09SChris Mason p->slots[level] = slot; 291487b29b20SYan Zheng if (ins_len > 0 && 2915e902baacSDavid Sterba btrfs_leaf_free_space(b) < ins_len) { 2916bd681513SChris Mason if (write_lock_level < 1) { 2917bd681513SChris Mason write_lock_level = 1; 2918bd681513SChris Mason btrfs_release_path(p); 2919bd681513SChris Mason goto again; 2920bd681513SChris Mason } 2921bd681513SChris Mason 2922b4ce94deSChris Mason btrfs_set_path_blocking(p); 292333c66f43SYan Zheng err = split_leaf(trans, root, key, 2924cc0c5538SChris Mason p, ins_len, ret == 0); 2925b4ce94deSChris Mason 292633c66f43SYan Zheng BUG_ON(err > 0); 292733c66f43SYan Zheng if (err) { 292833c66f43SYan Zheng ret = err; 292965b51a00SChris Mason goto done; 293065b51a00SChris Mason } 29315c680ed6SChris Mason } 2932459931ecSChris Mason if (!p->search_for_split) 2933f7c79f30SChris Mason unlock_up(p, level, lowest_unlock, 29344b6f8e96SLiu Bo min_write_lock_level, NULL); 293565b51a00SChris Mason goto done; 293665b51a00SChris Mason } 293765b51a00SChris Mason } 293865b51a00SChris Mason ret = 1; 293965b51a00SChris Mason done: 2940b4ce94deSChris Mason /* 2941b4ce94deSChris Mason * we don't really know what they plan on doing with the path 2942b4ce94deSChris Mason * from here on, so for now just mark it as blocking 2943b4ce94deSChris Mason */ 2944b9473439SChris Mason if (!p->leave_spinning) 2945b4ce94deSChris Mason btrfs_set_path_blocking(p); 29465f5bc6b1SFilipe Manana if (ret < 0 && !p->skip_release_on_error) 2947b3b4aa74SDavid Sterba btrfs_release_path(p); 2948be0e5c09SChris Mason return ret; 2949be0e5c09SChris Mason } 2950be0e5c09SChris Mason 295174123bd7SChris Mason /* 29525d9e75c4SJan Schmidt * Like btrfs_search_slot, this looks for a key in the given tree. It uses the 29535d9e75c4SJan Schmidt * current state of the tree together with the operations recorded in the tree 29545d9e75c4SJan Schmidt * modification log to search for the key in a previous version of this tree, as 29555d9e75c4SJan Schmidt * denoted by the time_seq parameter. 29565d9e75c4SJan Schmidt * 29575d9e75c4SJan Schmidt * Naturally, there is no support for insert, delete or cow operations. 29585d9e75c4SJan Schmidt * 29595d9e75c4SJan Schmidt * The resulting path and return value will be set up as if we called 29605d9e75c4SJan Schmidt * btrfs_search_slot at that point in time with ins_len and cow both set to 0. 29615d9e75c4SJan Schmidt */ 2962310712b2SOmar Sandoval int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key, 29635d9e75c4SJan Schmidt struct btrfs_path *p, u64 time_seq) 29645d9e75c4SJan Schmidt { 29650b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 29665d9e75c4SJan Schmidt struct extent_buffer *b; 29675d9e75c4SJan Schmidt int slot; 29685d9e75c4SJan Schmidt int ret; 29695d9e75c4SJan Schmidt int err; 29705d9e75c4SJan Schmidt int level; 29715d9e75c4SJan Schmidt int lowest_unlock = 1; 29725d9e75c4SJan Schmidt u8 lowest_level = 0; 2973d4b4087cSJosef Bacik int prev_cmp = -1; 29745d9e75c4SJan Schmidt 29755d9e75c4SJan Schmidt lowest_level = p->lowest_level; 29765d9e75c4SJan Schmidt WARN_ON(p->nodes[0] != NULL); 29775d9e75c4SJan Schmidt 29785d9e75c4SJan Schmidt if (p->search_commit_root) { 29795d9e75c4SJan Schmidt BUG_ON(time_seq); 29805d9e75c4SJan Schmidt return btrfs_search_slot(NULL, root, key, p, 0, 0); 29815d9e75c4SJan Schmidt } 29825d9e75c4SJan Schmidt 29835d9e75c4SJan Schmidt again: 29845d9e75c4SJan Schmidt b = get_old_root(root, time_seq); 2985315bed43SNikolay Borisov if (!b) { 2986315bed43SNikolay Borisov ret = -EIO; 2987315bed43SNikolay Borisov goto done; 2988315bed43SNikolay Borisov } 29895d9e75c4SJan Schmidt level = btrfs_header_level(b); 29905d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 29915d9e75c4SJan Schmidt 29925d9e75c4SJan Schmidt while (b) { 29935d9e75c4SJan Schmidt level = btrfs_header_level(b); 29945d9e75c4SJan Schmidt p->nodes[level] = b; 29955d9e75c4SJan Schmidt 29965d9e75c4SJan Schmidt /* 29975d9e75c4SJan Schmidt * we have a lock on b and as long as we aren't changing 29985d9e75c4SJan Schmidt * the tree, there is no way to for the items in b to change. 29995d9e75c4SJan Schmidt * It is safe to drop the lock on our parent before we 30005d9e75c4SJan Schmidt * go through the expensive btree search on b. 30015d9e75c4SJan Schmidt */ 30025d9e75c4SJan Schmidt btrfs_unlock_up_safe(p, level + 1); 30035d9e75c4SJan Schmidt 3004d4b4087cSJosef Bacik /* 300501327610SNicholas D Steeves * Since we can unwind ebs we want to do a real search every 3006d4b4087cSJosef Bacik * time. 3007d4b4087cSJosef Bacik */ 3008d4b4087cSJosef Bacik prev_cmp = -1; 3009d7396f07SFilipe David Borba Manana ret = key_search(b, key, level, &prev_cmp, &slot); 3010cbca7d59SFilipe Manana if (ret < 0) 3011cbca7d59SFilipe Manana goto done; 30125d9e75c4SJan Schmidt 30135d9e75c4SJan Schmidt if (level != 0) { 30145d9e75c4SJan Schmidt int dec = 0; 30155d9e75c4SJan Schmidt if (ret && slot > 0) { 30165d9e75c4SJan Schmidt dec = 1; 30175d9e75c4SJan Schmidt slot -= 1; 30185d9e75c4SJan Schmidt } 30195d9e75c4SJan Schmidt p->slots[level] = slot; 30205d9e75c4SJan Schmidt unlock_up(p, level, lowest_unlock, 0, NULL); 30215d9e75c4SJan Schmidt 30225d9e75c4SJan Schmidt if (level == lowest_level) { 30235d9e75c4SJan Schmidt if (dec) 30245d9e75c4SJan Schmidt p->slots[level]++; 30255d9e75c4SJan Schmidt goto done; 30265d9e75c4SJan Schmidt } 30275d9e75c4SJan Schmidt 3028d07b8528SLiu Bo err = read_block_for_search(root, p, &b, level, 3029cda79c54SDavid Sterba slot, key); 30305d9e75c4SJan Schmidt if (err == -EAGAIN) 30315d9e75c4SJan Schmidt goto again; 30325d9e75c4SJan Schmidt if (err) { 30335d9e75c4SJan Schmidt ret = err; 30345d9e75c4SJan Schmidt goto done; 30355d9e75c4SJan Schmidt } 30365d9e75c4SJan Schmidt 30375d9e75c4SJan Schmidt level = btrfs_header_level(b); 3038f82c458aSChris Mason err = btrfs_tree_read_lock_atomic(b); 30395d9e75c4SJan Schmidt if (!err) { 30405d9e75c4SJan Schmidt btrfs_set_path_blocking(p); 30415d9e75c4SJan Schmidt btrfs_tree_read_lock(b); 30425d9e75c4SJan Schmidt } 30430b246afaSJeff Mahoney b = tree_mod_log_rewind(fs_info, p, b, time_seq); 3044db7f3436SJosef Bacik if (!b) { 3045db7f3436SJosef Bacik ret = -ENOMEM; 3046db7f3436SJosef Bacik goto done; 3047db7f3436SJosef Bacik } 30485d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 30495d9e75c4SJan Schmidt p->nodes[level] = b; 30505d9e75c4SJan Schmidt } else { 30515d9e75c4SJan Schmidt p->slots[level] = slot; 30525d9e75c4SJan Schmidt unlock_up(p, level, lowest_unlock, 0, NULL); 30535d9e75c4SJan Schmidt goto done; 30545d9e75c4SJan Schmidt } 30555d9e75c4SJan Schmidt } 30565d9e75c4SJan Schmidt ret = 1; 30575d9e75c4SJan Schmidt done: 30585d9e75c4SJan Schmidt if (!p->leave_spinning) 30595d9e75c4SJan Schmidt btrfs_set_path_blocking(p); 30605d9e75c4SJan Schmidt if (ret < 0) 30615d9e75c4SJan Schmidt btrfs_release_path(p); 30625d9e75c4SJan Schmidt 30635d9e75c4SJan Schmidt return ret; 30645d9e75c4SJan Schmidt } 30655d9e75c4SJan Schmidt 30665d9e75c4SJan Schmidt /* 30672f38b3e1SArne Jansen * helper to use instead of search slot if no exact match is needed but 30682f38b3e1SArne Jansen * instead the next or previous item should be returned. 30692f38b3e1SArne Jansen * When find_higher is true, the next higher item is returned, the next lower 30702f38b3e1SArne Jansen * otherwise. 30712f38b3e1SArne Jansen * When return_any and find_higher are both true, and no higher item is found, 30722f38b3e1SArne Jansen * return the next lower instead. 30732f38b3e1SArne Jansen * When return_any is true and find_higher is false, and no lower item is found, 30742f38b3e1SArne Jansen * return the next higher instead. 30752f38b3e1SArne Jansen * It returns 0 if any item is found, 1 if none is found (tree empty), and 30762f38b3e1SArne Jansen * < 0 on error 30772f38b3e1SArne Jansen */ 30782f38b3e1SArne Jansen int btrfs_search_slot_for_read(struct btrfs_root *root, 3079310712b2SOmar Sandoval const struct btrfs_key *key, 3080310712b2SOmar Sandoval struct btrfs_path *p, int find_higher, 3081310712b2SOmar Sandoval int return_any) 30822f38b3e1SArne Jansen { 30832f38b3e1SArne Jansen int ret; 30842f38b3e1SArne Jansen struct extent_buffer *leaf; 30852f38b3e1SArne Jansen 30862f38b3e1SArne Jansen again: 30872f38b3e1SArne Jansen ret = btrfs_search_slot(NULL, root, key, p, 0, 0); 30882f38b3e1SArne Jansen if (ret <= 0) 30892f38b3e1SArne Jansen return ret; 30902f38b3e1SArne Jansen /* 30912f38b3e1SArne Jansen * a return value of 1 means the path is at the position where the 30922f38b3e1SArne Jansen * item should be inserted. Normally this is the next bigger item, 30932f38b3e1SArne Jansen * but in case the previous item is the last in a leaf, path points 30942f38b3e1SArne Jansen * to the first free slot in the previous leaf, i.e. at an invalid 30952f38b3e1SArne Jansen * item. 30962f38b3e1SArne Jansen */ 30972f38b3e1SArne Jansen leaf = p->nodes[0]; 30982f38b3e1SArne Jansen 30992f38b3e1SArne Jansen if (find_higher) { 31002f38b3e1SArne Jansen if (p->slots[0] >= btrfs_header_nritems(leaf)) { 31012f38b3e1SArne Jansen ret = btrfs_next_leaf(root, p); 31022f38b3e1SArne Jansen if (ret <= 0) 31032f38b3e1SArne Jansen return ret; 31042f38b3e1SArne Jansen if (!return_any) 31052f38b3e1SArne Jansen return 1; 31062f38b3e1SArne Jansen /* 31072f38b3e1SArne Jansen * no higher item found, return the next 31082f38b3e1SArne Jansen * lower instead 31092f38b3e1SArne Jansen */ 31102f38b3e1SArne Jansen return_any = 0; 31112f38b3e1SArne Jansen find_higher = 0; 31122f38b3e1SArne Jansen btrfs_release_path(p); 31132f38b3e1SArne Jansen goto again; 31142f38b3e1SArne Jansen } 31152f38b3e1SArne Jansen } else { 31162f38b3e1SArne Jansen if (p->slots[0] == 0) { 31172f38b3e1SArne Jansen ret = btrfs_prev_leaf(root, p); 3118e6793769SArne Jansen if (ret < 0) 31192f38b3e1SArne Jansen return ret; 3120e6793769SArne Jansen if (!ret) { 312123c6bf6aSFilipe David Borba Manana leaf = p->nodes[0]; 312223c6bf6aSFilipe David Borba Manana if (p->slots[0] == btrfs_header_nritems(leaf)) 312323c6bf6aSFilipe David Borba Manana p->slots[0]--; 3124e6793769SArne Jansen return 0; 3125e6793769SArne Jansen } 31262f38b3e1SArne Jansen if (!return_any) 31272f38b3e1SArne Jansen return 1; 31282f38b3e1SArne Jansen /* 31292f38b3e1SArne Jansen * no lower item found, return the next 31302f38b3e1SArne Jansen * higher instead 31312f38b3e1SArne Jansen */ 31322f38b3e1SArne Jansen return_any = 0; 31332f38b3e1SArne Jansen find_higher = 1; 31342f38b3e1SArne Jansen btrfs_release_path(p); 31352f38b3e1SArne Jansen goto again; 3136e6793769SArne Jansen } else { 31372f38b3e1SArne Jansen --p->slots[0]; 31382f38b3e1SArne Jansen } 31392f38b3e1SArne Jansen } 31402f38b3e1SArne Jansen return 0; 31412f38b3e1SArne Jansen } 31422f38b3e1SArne Jansen 31432f38b3e1SArne Jansen /* 314474123bd7SChris Mason * adjust the pointers going up the tree, starting at level 314574123bd7SChris Mason * making sure the right key of each node is points to 'key'. 314674123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 314774123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 314874123bd7SChris Mason * higher levels 3149aa5d6bedSChris Mason * 315074123bd7SChris Mason */ 3151b167fa91SNikolay Borisov static void fixup_low_keys(struct btrfs_path *path, 31525f39d397SChris Mason struct btrfs_disk_key *key, int level) 3153be0e5c09SChris Mason { 3154be0e5c09SChris Mason int i; 31555f39d397SChris Mason struct extent_buffer *t; 31560e82bcfeSDavid Sterba int ret; 31575f39d397SChris Mason 3158234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 3159be0e5c09SChris Mason int tslot = path->slots[i]; 31600e82bcfeSDavid Sterba 3161eb60ceacSChris Mason if (!path->nodes[i]) 3162be0e5c09SChris Mason break; 31635f39d397SChris Mason t = path->nodes[i]; 31640e82bcfeSDavid Sterba ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE, 31650e82bcfeSDavid Sterba GFP_ATOMIC); 31660e82bcfeSDavid Sterba BUG_ON(ret < 0); 31675f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 3168d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 3169be0e5c09SChris Mason if (tslot != 0) 3170be0e5c09SChris Mason break; 3171be0e5c09SChris Mason } 3172be0e5c09SChris Mason } 3173be0e5c09SChris Mason 317474123bd7SChris Mason /* 317531840ae1SZheng Yan * update item key. 317631840ae1SZheng Yan * 317731840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 317831840ae1SZheng Yan * that the new key won't break the order 317931840ae1SZheng Yan */ 3180b7a0365eSDaniel Dressler void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, 3181b7a0365eSDaniel Dressler struct btrfs_path *path, 3182310712b2SOmar Sandoval const struct btrfs_key *new_key) 318331840ae1SZheng Yan { 318431840ae1SZheng Yan struct btrfs_disk_key disk_key; 318531840ae1SZheng Yan struct extent_buffer *eb; 318631840ae1SZheng Yan int slot; 318731840ae1SZheng Yan 318831840ae1SZheng Yan eb = path->nodes[0]; 318931840ae1SZheng Yan slot = path->slots[0]; 319031840ae1SZheng Yan if (slot > 0) { 319131840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 3192143bede5SJeff Mahoney BUG_ON(comp_keys(&disk_key, new_key) >= 0); 319331840ae1SZheng Yan } 319431840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 319531840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 3196143bede5SJeff Mahoney BUG_ON(comp_keys(&disk_key, new_key) <= 0); 319731840ae1SZheng Yan } 319831840ae1SZheng Yan 319931840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 320031840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 320131840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 320231840ae1SZheng Yan if (slot == 0) 3203b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 320431840ae1SZheng Yan } 320531840ae1SZheng Yan 320631840ae1SZheng Yan /* 320774123bd7SChris Mason * try to push data from one node into the next node left in the 320879f95c82SChris Mason * tree. 3209aa5d6bedSChris Mason * 3210aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 3211aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 321274123bd7SChris Mason */ 321398ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 32142ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 32152ff7e61eSJeff Mahoney struct extent_buffer *dst, 3216971a1f66SChris Mason struct extent_buffer *src, int empty) 3217be0e5c09SChris Mason { 3218be0e5c09SChris Mason int push_items = 0; 3219bb803951SChris Mason int src_nritems; 3220bb803951SChris Mason int dst_nritems; 3221aa5d6bedSChris Mason int ret = 0; 3222be0e5c09SChris Mason 32235f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 32245f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 32250b246afaSJeff Mahoney push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; 32267bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 32277bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 322854aa1f4dSChris Mason 3229bce4eae9SChris Mason if (!empty && src_nritems <= 8) 3230971a1f66SChris Mason return 1; 3231971a1f66SChris Mason 3232d397712bSChris Mason if (push_items <= 0) 3233be0e5c09SChris Mason return 1; 3234be0e5c09SChris Mason 3235bce4eae9SChris Mason if (empty) { 3236971a1f66SChris Mason push_items = min(src_nritems, push_items); 3237bce4eae9SChris Mason if (push_items < src_nritems) { 3238bce4eae9SChris Mason /* leave at least 8 pointers in the node if 3239bce4eae9SChris Mason * we aren't going to empty it 3240bce4eae9SChris Mason */ 3241bce4eae9SChris Mason if (src_nritems - push_items < 8) { 3242bce4eae9SChris Mason if (push_items <= 8) 3243bce4eae9SChris Mason return 1; 3244bce4eae9SChris Mason push_items -= 8; 3245bce4eae9SChris Mason } 3246bce4eae9SChris Mason } 3247bce4eae9SChris Mason } else 3248bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 324979f95c82SChris Mason 3250ed874f0dSDavid Sterba ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items); 32515de865eeSFilipe David Borba Manana if (ret) { 325266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 32535de865eeSFilipe David Borba Manana return ret; 32545de865eeSFilipe David Borba Manana } 32555f39d397SChris Mason copy_extent_buffer(dst, src, 32565f39d397SChris Mason btrfs_node_key_ptr_offset(dst_nritems), 32575f39d397SChris Mason btrfs_node_key_ptr_offset(0), 3258123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 32595f39d397SChris Mason 3260bb803951SChris Mason if (push_items < src_nritems) { 326157911b8bSJan Schmidt /* 3262bf1d3425SDavid Sterba * Don't call tree_mod_log_insert_move here, key removal was 3263bf1d3425SDavid Sterba * already fully logged by tree_mod_log_eb_copy above. 326457911b8bSJan Schmidt */ 32655f39d397SChris Mason memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 32665f39d397SChris Mason btrfs_node_key_ptr_offset(push_items), 3267e2fa7227SChris Mason (src_nritems - push_items) * 3268123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 3269bb803951SChris Mason } 32705f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 32715f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 32725f39d397SChris Mason btrfs_mark_buffer_dirty(src); 32735f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 327431840ae1SZheng Yan 3275bb803951SChris Mason return ret; 3276be0e5c09SChris Mason } 3277be0e5c09SChris Mason 327897571fd0SChris Mason /* 327979f95c82SChris Mason * try to push data from one node into the next node right in the 328079f95c82SChris Mason * tree. 328179f95c82SChris Mason * 328279f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 328379f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 328479f95c82SChris Mason * 328579f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 328679f95c82SChris Mason */ 32875f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 32882ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 32895f39d397SChris Mason struct extent_buffer *dst, 32905f39d397SChris Mason struct extent_buffer *src) 329179f95c82SChris Mason { 329279f95c82SChris Mason int push_items = 0; 329379f95c82SChris Mason int max_push; 329479f95c82SChris Mason int src_nritems; 329579f95c82SChris Mason int dst_nritems; 329679f95c82SChris Mason int ret = 0; 329779f95c82SChris Mason 32987bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 32997bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 33007bb86316SChris Mason 33015f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 33025f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 33030b246afaSJeff Mahoney push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; 3304d397712bSChris Mason if (push_items <= 0) 330579f95c82SChris Mason return 1; 3306bce4eae9SChris Mason 3307d397712bSChris Mason if (src_nritems < 4) 3308bce4eae9SChris Mason return 1; 330979f95c82SChris Mason 331079f95c82SChris Mason max_push = src_nritems / 2 + 1; 331179f95c82SChris Mason /* don't try to empty the node */ 3312d397712bSChris Mason if (max_push >= src_nritems) 331379f95c82SChris Mason return 1; 3314252c38f0SYan 331579f95c82SChris Mason if (max_push < push_items) 331679f95c82SChris Mason push_items = max_push; 331779f95c82SChris Mason 3318bf1d3425SDavid Sterba ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems); 3319bf1d3425SDavid Sterba BUG_ON(ret < 0); 33205f39d397SChris Mason memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 33215f39d397SChris Mason btrfs_node_key_ptr_offset(0), 33225f39d397SChris Mason (dst_nritems) * 33235f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 3324d6025579SChris Mason 3325ed874f0dSDavid Sterba ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items, 3326ed874f0dSDavid Sterba push_items); 33275de865eeSFilipe David Borba Manana if (ret) { 332866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 33295de865eeSFilipe David Borba Manana return ret; 33305de865eeSFilipe David Borba Manana } 33315f39d397SChris Mason copy_extent_buffer(dst, src, 33325f39d397SChris Mason btrfs_node_key_ptr_offset(0), 33335f39d397SChris Mason btrfs_node_key_ptr_offset(src_nritems - push_items), 3334123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 333579f95c82SChris Mason 33365f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 33375f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 333879f95c82SChris Mason 33395f39d397SChris Mason btrfs_mark_buffer_dirty(src); 33405f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 334131840ae1SZheng Yan 334279f95c82SChris Mason return ret; 334379f95c82SChris Mason } 334479f95c82SChris Mason 334579f95c82SChris Mason /* 334697571fd0SChris Mason * helper function to insert a new root level in the tree. 334797571fd0SChris Mason * A new node is allocated, and a single item is inserted to 334897571fd0SChris Mason * point to the existing root 3349aa5d6bedSChris Mason * 3350aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 335197571fd0SChris Mason */ 3352d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans, 33535f39d397SChris Mason struct btrfs_root *root, 3354fdd99c72SLiu Bo struct btrfs_path *path, int level) 335574123bd7SChris Mason { 33560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 33577bb86316SChris Mason u64 lower_gen; 33585f39d397SChris Mason struct extent_buffer *lower; 33595f39d397SChris Mason struct extent_buffer *c; 3360925baeddSChris Mason struct extent_buffer *old; 33615f39d397SChris Mason struct btrfs_disk_key lower_key; 3362d9d19a01SDavid Sterba int ret; 33635c680ed6SChris Mason 33645c680ed6SChris Mason BUG_ON(path->nodes[level]); 33655c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 33665c680ed6SChris Mason 33677bb86316SChris Mason lower = path->nodes[level-1]; 33687bb86316SChris Mason if (level == 1) 33697bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 33707bb86316SChris Mason else 33717bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 33727bb86316SChris Mason 3373a6279470SFilipe Manana c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level, 3374a6279470SFilipe Manana root->node->start, 0); 33755f39d397SChris Mason if (IS_ERR(c)) 33765f39d397SChris Mason return PTR_ERR(c); 3377925baeddSChris Mason 33780b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 3379f0486c68SYan, Zheng 33805f39d397SChris Mason btrfs_set_header_nritems(c, 1); 33815f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 3382db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 33837bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 338431840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 33857bb86316SChris Mason 33867bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 33875f39d397SChris Mason 33885f39d397SChris Mason btrfs_mark_buffer_dirty(c); 3389d5719762SChris Mason 3390925baeddSChris Mason old = root->node; 3391d9d19a01SDavid Sterba ret = tree_mod_log_insert_root(root->node, c, 0); 3392d9d19a01SDavid Sterba BUG_ON(ret < 0); 3393240f62c8SChris Mason rcu_assign_pointer(root->node, c); 3394925baeddSChris Mason 3395925baeddSChris Mason /* the super has an extra ref to root->node */ 3396925baeddSChris Mason free_extent_buffer(old); 3397925baeddSChris Mason 33980b86a832SChris Mason add_root_to_dirty_list(root); 33995f39d397SChris Mason extent_buffer_get(c); 34005f39d397SChris Mason path->nodes[level] = c; 340195449a16Schandan path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; 340274123bd7SChris Mason path->slots[level] = 0; 340374123bd7SChris Mason return 0; 340474123bd7SChris Mason } 34055c680ed6SChris Mason 34065c680ed6SChris Mason /* 34075c680ed6SChris Mason * worker function to insert a single pointer in a node. 34085c680ed6SChris Mason * the node should have enough room for the pointer already 340997571fd0SChris Mason * 34105c680ed6SChris Mason * slot and level indicate where you want the key to go, and 34115c680ed6SChris Mason * blocknr is the block the key points to. 34125c680ed6SChris Mason */ 3413143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans, 34142ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, struct btrfs_path *path, 3415143bede5SJeff Mahoney struct btrfs_disk_key *key, u64 bytenr, 3416c3e06965SJan Schmidt int slot, int level) 34175c680ed6SChris Mason { 34185f39d397SChris Mason struct extent_buffer *lower; 34195c680ed6SChris Mason int nritems; 3420f3ea38daSJan Schmidt int ret; 34215c680ed6SChris Mason 34225c680ed6SChris Mason BUG_ON(!path->nodes[level]); 3423f0486c68SYan, Zheng btrfs_assert_tree_locked(path->nodes[level]); 34245f39d397SChris Mason lower = path->nodes[level]; 34255f39d397SChris Mason nritems = btrfs_header_nritems(lower); 3426c293498bSStoyan Gaydarov BUG_ON(slot > nritems); 34270b246afaSJeff Mahoney BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 342874123bd7SChris Mason if (slot != nritems) { 3429bf1d3425SDavid Sterba if (level) { 3430bf1d3425SDavid Sterba ret = tree_mod_log_insert_move(lower, slot + 1, slot, 3431a446a979SDavid Sterba nritems - slot); 3432bf1d3425SDavid Sterba BUG_ON(ret < 0); 3433bf1d3425SDavid Sterba } 34345f39d397SChris Mason memmove_extent_buffer(lower, 34355f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 34365f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 3437123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 343874123bd7SChris Mason } 3439c3e06965SJan Schmidt if (level) { 3440e09c2efeSDavid Sterba ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD, 3441e09c2efeSDavid Sterba GFP_NOFS); 3442f3ea38daSJan Schmidt BUG_ON(ret < 0); 3443f3ea38daSJan Schmidt } 34445f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 3445db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 344674493f7aSChris Mason WARN_ON(trans->transid == 0); 344774493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 34485f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 34495f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 345074123bd7SChris Mason } 345174123bd7SChris Mason 345297571fd0SChris Mason /* 345397571fd0SChris Mason * split the node at the specified level in path in two. 345497571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 345597571fd0SChris Mason * 345697571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 345797571fd0SChris Mason * left and right, if either one works, it returns right away. 3458aa5d6bedSChris Mason * 3459aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 346097571fd0SChris Mason */ 3461e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 3462e02119d5SChris Mason struct btrfs_root *root, 3463e02119d5SChris Mason struct btrfs_path *path, int level) 3464be0e5c09SChris Mason { 34650b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 34665f39d397SChris Mason struct extent_buffer *c; 34675f39d397SChris Mason struct extent_buffer *split; 34685f39d397SChris Mason struct btrfs_disk_key disk_key; 3469be0e5c09SChris Mason int mid; 34705c680ed6SChris Mason int ret; 34717518a238SChris Mason u32 c_nritems; 3472be0e5c09SChris Mason 34735f39d397SChris Mason c = path->nodes[level]; 34747bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 34755f39d397SChris Mason if (c == root->node) { 3476d9abbf1cSJan Schmidt /* 347790f8d62eSJan Schmidt * trying to split the root, lets make a new one 347890f8d62eSJan Schmidt * 3479fdd99c72SLiu Bo * tree mod log: We don't log_removal old root in 348090f8d62eSJan Schmidt * insert_new_root, because that root buffer will be kept as a 348190f8d62eSJan Schmidt * normal node. We are going to log removal of half of the 348290f8d62eSJan Schmidt * elements below with tree_mod_log_eb_copy. We're holding a 348390f8d62eSJan Schmidt * tree lock on the buffer, which is why we cannot race with 348490f8d62eSJan Schmidt * other tree_mod_log users. 3485d9abbf1cSJan Schmidt */ 3486fdd99c72SLiu Bo ret = insert_new_root(trans, root, path, level + 1); 34875c680ed6SChris Mason if (ret) 34885c680ed6SChris Mason return ret; 3489b3612421SChris Mason } else { 3490e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 34915f39d397SChris Mason c = path->nodes[level]; 34925f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 34930b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) 3494e66f709bSChris Mason return 0; 349554aa1f4dSChris Mason if (ret < 0) 349654aa1f4dSChris Mason return ret; 34975c680ed6SChris Mason } 3498e66f709bSChris Mason 34995f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 35005d4f98a2SYan Zheng mid = (c_nritems + 1) / 2; 35015d4f98a2SYan Zheng btrfs_node_key(c, &disk_key, mid); 35027bb86316SChris Mason 3503a6279470SFilipe Manana split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level, 3504a6279470SFilipe Manana c->start, 0); 35055f39d397SChris Mason if (IS_ERR(split)) 35065f39d397SChris Mason return PTR_ERR(split); 350754aa1f4dSChris Mason 35080b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 3509bc877d28SNikolay Borisov ASSERT(btrfs_header_level(c) == level); 35105f39d397SChris Mason 3511ed874f0dSDavid Sterba ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid); 35125de865eeSFilipe David Borba Manana if (ret) { 351366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 35145de865eeSFilipe David Borba Manana return ret; 35155de865eeSFilipe David Borba Manana } 35165f39d397SChris Mason copy_extent_buffer(split, c, 35175f39d397SChris Mason btrfs_node_key_ptr_offset(0), 35185f39d397SChris Mason btrfs_node_key_ptr_offset(mid), 3519123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 35205f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 35215f39d397SChris Mason btrfs_set_header_nritems(c, mid); 3522aa5d6bedSChris Mason ret = 0; 3523aa5d6bedSChris Mason 35245f39d397SChris Mason btrfs_mark_buffer_dirty(c); 35255f39d397SChris Mason btrfs_mark_buffer_dirty(split); 35265f39d397SChris Mason 35272ff7e61eSJeff Mahoney insert_ptr(trans, fs_info, path, &disk_key, split->start, 3528c3e06965SJan Schmidt path->slots[level + 1] + 1, level + 1); 3529aa5d6bedSChris Mason 35305de08d7dSChris Mason if (path->slots[level] >= mid) { 35315c680ed6SChris Mason path->slots[level] -= mid; 3532925baeddSChris Mason btrfs_tree_unlock(c); 35335f39d397SChris Mason free_extent_buffer(c); 35345f39d397SChris Mason path->nodes[level] = split; 35355c680ed6SChris Mason path->slots[level + 1] += 1; 3536eb60ceacSChris Mason } else { 3537925baeddSChris Mason btrfs_tree_unlock(split); 35385f39d397SChris Mason free_extent_buffer(split); 3539be0e5c09SChris Mason } 3540aa5d6bedSChris Mason return ret; 3541be0e5c09SChris Mason } 3542be0e5c09SChris Mason 354374123bd7SChris Mason /* 354474123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 354574123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 354674123bd7SChris Mason * space used both by the item structs and the item data 354774123bd7SChris Mason */ 35485f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr) 3549be0e5c09SChris Mason { 355041be1f3bSJosef Bacik struct btrfs_item *start_item; 355141be1f3bSJosef Bacik struct btrfs_item *end_item; 355241be1f3bSJosef Bacik struct btrfs_map_token token; 3553be0e5c09SChris Mason int data_len; 35545f39d397SChris Mason int nritems = btrfs_header_nritems(l); 3555d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 3556be0e5c09SChris Mason 3557be0e5c09SChris Mason if (!nr) 3558be0e5c09SChris Mason return 0; 355941be1f3bSJosef Bacik btrfs_init_map_token(&token); 3560dd3cc16bSRoss Kirk start_item = btrfs_item_nr(start); 3561dd3cc16bSRoss Kirk end_item = btrfs_item_nr(end); 356241be1f3bSJosef Bacik data_len = btrfs_token_item_offset(l, start_item, &token) + 356341be1f3bSJosef Bacik btrfs_token_item_size(l, start_item, &token); 356441be1f3bSJosef Bacik data_len = data_len - btrfs_token_item_offset(l, end_item, &token); 35650783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 3566d4dbff95SChris Mason WARN_ON(data_len < 0); 3567be0e5c09SChris Mason return data_len; 3568be0e5c09SChris Mason } 3569be0e5c09SChris Mason 357074123bd7SChris Mason /* 3571d4dbff95SChris Mason * The space between the end of the leaf items and 3572d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 3573d4dbff95SChris Mason * the leaf has left for both items and data 3574d4dbff95SChris Mason */ 3575e902baacSDavid Sterba noinline int btrfs_leaf_free_space(struct extent_buffer *leaf) 3576d4dbff95SChris Mason { 3577e902baacSDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 35785f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 35795f39d397SChris Mason int ret; 35800b246afaSJeff Mahoney 35810b246afaSJeff Mahoney ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems); 35825f39d397SChris Mason if (ret < 0) { 35830b246afaSJeff Mahoney btrfs_crit(fs_info, 3584efe120a0SFrank Holton "leaf free space ret %d, leaf data size %lu, used %d nritems %d", 3585da17066cSJeff Mahoney ret, 35860b246afaSJeff Mahoney (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info), 35875f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 35885f39d397SChris Mason } 35895f39d397SChris Mason return ret; 3590d4dbff95SChris Mason } 3591d4dbff95SChris Mason 359299d8f83cSChris Mason /* 359399d8f83cSChris Mason * min slot controls the lowest index we're willing to push to the 359499d8f83cSChris Mason * right. We'll push up to and including min_slot, but no lower 359599d8f83cSChris Mason */ 35961e47eef2SDavid Sterba static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info, 359744871b1bSChris Mason struct btrfs_path *path, 359844871b1bSChris Mason int data_size, int empty, 359944871b1bSChris Mason struct extent_buffer *right, 360099d8f83cSChris Mason int free_space, u32 left_nritems, 360199d8f83cSChris Mason u32 min_slot) 360200ec4c51SChris Mason { 36035f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 360444871b1bSChris Mason struct extent_buffer *upper = path->nodes[1]; 3605cfed81a0SChris Mason struct btrfs_map_token token; 36065f39d397SChris Mason struct btrfs_disk_key disk_key; 360700ec4c51SChris Mason int slot; 360834a38218SChris Mason u32 i; 360900ec4c51SChris Mason int push_space = 0; 361000ec4c51SChris Mason int push_items = 0; 36110783fcfcSChris Mason struct btrfs_item *item; 361234a38218SChris Mason u32 nr; 36137518a238SChris Mason u32 right_nritems; 36145f39d397SChris Mason u32 data_end; 3615db94535dSChris Mason u32 this_item_size; 361600ec4c51SChris Mason 3617cfed81a0SChris Mason btrfs_init_map_token(&token); 3618cfed81a0SChris Mason 361934a38218SChris Mason if (empty) 362034a38218SChris Mason nr = 0; 362134a38218SChris Mason else 362299d8f83cSChris Mason nr = max_t(u32, 1, min_slot); 362334a38218SChris Mason 362431840ae1SZheng Yan if (path->slots[0] >= left_nritems) 362587b29b20SYan Zheng push_space += data_size; 362631840ae1SZheng Yan 362744871b1bSChris Mason slot = path->slots[1]; 362834a38218SChris Mason i = left_nritems - 1; 362934a38218SChris Mason while (i >= nr) { 3630dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3631db94535dSChris Mason 363231840ae1SZheng Yan if (!empty && push_items > 0) { 363331840ae1SZheng Yan if (path->slots[0] > i) 363431840ae1SZheng Yan break; 363531840ae1SZheng Yan if (path->slots[0] == i) { 3636e902baacSDavid Sterba int space = btrfs_leaf_free_space(left); 3637e902baacSDavid Sterba 363831840ae1SZheng Yan if (space + push_space * 2 > free_space) 363931840ae1SZheng Yan break; 364031840ae1SZheng Yan } 364131840ae1SZheng Yan } 364231840ae1SZheng Yan 364300ec4c51SChris Mason if (path->slots[0] == i) 364487b29b20SYan Zheng push_space += data_size; 3645db94535dSChris Mason 3646db94535dSChris Mason this_item_size = btrfs_item_size(left, item); 3647db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 364800ec4c51SChris Mason break; 364931840ae1SZheng Yan 365000ec4c51SChris Mason push_items++; 3651db94535dSChris Mason push_space += this_item_size + sizeof(*item); 365234a38218SChris Mason if (i == 0) 365334a38218SChris Mason break; 365434a38218SChris Mason i--; 3655db94535dSChris Mason } 36565f39d397SChris Mason 3657925baeddSChris Mason if (push_items == 0) 3658925baeddSChris Mason goto out_unlock; 36595f39d397SChris Mason 36606c1500f2SJulia Lawall WARN_ON(!empty && push_items == left_nritems); 36615f39d397SChris Mason 366200ec4c51SChris Mason /* push left to right */ 36635f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 366434a38218SChris Mason 36655f39d397SChris Mason push_space = btrfs_item_end_nr(left, left_nritems - push_items); 36668f881e8cSDavid Sterba push_space -= leaf_data_end(left); 36675f39d397SChris Mason 366800ec4c51SChris Mason /* make room in the right data area */ 36698f881e8cSDavid Sterba data_end = leaf_data_end(right); 36705f39d397SChris Mason memmove_extent_buffer(right, 36713d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + data_end - push_space, 36723d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + data_end, 36730b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - data_end); 36745f39d397SChris Mason 367500ec4c51SChris Mason /* copy from the left data area */ 36763d9ec8c4SNikolay Borisov copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET + 36770b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 36788f881e8cSDavid Sterba BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left), 3679d6025579SChris Mason push_space); 36805f39d397SChris Mason 36815f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 36825f39d397SChris Mason btrfs_item_nr_offset(0), 36830783fcfcSChris Mason right_nritems * sizeof(struct btrfs_item)); 36845f39d397SChris Mason 368500ec4c51SChris Mason /* copy the items from left to right */ 36865f39d397SChris Mason copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 36875f39d397SChris Mason btrfs_item_nr_offset(left_nritems - push_items), 36880783fcfcSChris Mason push_items * sizeof(struct btrfs_item)); 368900ec4c51SChris Mason 369000ec4c51SChris Mason /* update the item pointers */ 36917518a238SChris Mason right_nritems += push_items; 36925f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 36930b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info); 36947518a238SChris Mason for (i = 0; i < right_nritems; i++) { 3695dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3696cfed81a0SChris Mason push_space -= btrfs_token_item_size(right, item, &token); 3697cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, push_space, &token); 3698db94535dSChris Mason } 3699db94535dSChris Mason 37007518a238SChris Mason left_nritems -= push_items; 37015f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 370200ec4c51SChris Mason 370334a38218SChris Mason if (left_nritems) 37045f39d397SChris Mason btrfs_mark_buffer_dirty(left); 3705f0486c68SYan, Zheng else 37066a884d7dSDavid Sterba btrfs_clean_tree_block(left); 3707f0486c68SYan, Zheng 37085f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3709a429e513SChris Mason 37105f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 37115f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 3712d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 371302217ed2SChris Mason 371400ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 37157518a238SChris Mason if (path->slots[0] >= left_nritems) { 37167518a238SChris Mason path->slots[0] -= left_nritems; 3717925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 37186a884d7dSDavid Sterba btrfs_clean_tree_block(path->nodes[0]); 3719925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 37205f39d397SChris Mason free_extent_buffer(path->nodes[0]); 37215f39d397SChris Mason path->nodes[0] = right; 372200ec4c51SChris Mason path->slots[1] += 1; 372300ec4c51SChris Mason } else { 3724925baeddSChris Mason btrfs_tree_unlock(right); 37255f39d397SChris Mason free_extent_buffer(right); 372600ec4c51SChris Mason } 372700ec4c51SChris Mason return 0; 3728925baeddSChris Mason 3729925baeddSChris Mason out_unlock: 3730925baeddSChris Mason btrfs_tree_unlock(right); 3731925baeddSChris Mason free_extent_buffer(right); 3732925baeddSChris Mason return 1; 373300ec4c51SChris Mason } 3734925baeddSChris Mason 373500ec4c51SChris Mason /* 373644871b1bSChris Mason * push some data in the path leaf to the right, trying to free up at 373774123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 373844871b1bSChris Mason * 373944871b1bSChris Mason * returns 1 if the push failed because the other node didn't have enough 374044871b1bSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 374199d8f83cSChris Mason * 374299d8f83cSChris Mason * this will push starting from min_slot to the end of the leaf. It won't 374399d8f83cSChris Mason * push any slot lower than min_slot 374474123bd7SChris Mason */ 374544871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 374699d8f83cSChris Mason *root, struct btrfs_path *path, 374799d8f83cSChris Mason int min_data_size, int data_size, 374899d8f83cSChris Mason int empty, u32 min_slot) 3749be0e5c09SChris Mason { 37502ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 375144871b1bSChris Mason struct extent_buffer *left = path->nodes[0]; 375244871b1bSChris Mason struct extent_buffer *right; 375344871b1bSChris Mason struct extent_buffer *upper; 375444871b1bSChris Mason int slot; 375544871b1bSChris Mason int free_space; 375644871b1bSChris Mason u32 left_nritems; 375744871b1bSChris Mason int ret; 375844871b1bSChris Mason 375944871b1bSChris Mason if (!path->nodes[1]) 376044871b1bSChris Mason return 1; 376144871b1bSChris Mason 376244871b1bSChris Mason slot = path->slots[1]; 376344871b1bSChris Mason upper = path->nodes[1]; 376444871b1bSChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 376544871b1bSChris Mason return 1; 376644871b1bSChris Mason 376744871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 376844871b1bSChris Mason 3769d0d20b0fSDavid Sterba right = read_node_slot(upper, slot + 1); 3770fb770ae4SLiu Bo /* 3771fb770ae4SLiu Bo * slot + 1 is not valid or we fail to read the right node, 3772fb770ae4SLiu Bo * no big deal, just return. 3773fb770ae4SLiu Bo */ 3774fb770ae4SLiu Bo if (IS_ERR(right)) 377591ca338dSTsutomu Itoh return 1; 377691ca338dSTsutomu Itoh 377744871b1bSChris Mason btrfs_tree_lock(right); 37788bead258SDavid Sterba btrfs_set_lock_blocking_write(right); 377944871b1bSChris Mason 3780e902baacSDavid Sterba free_space = btrfs_leaf_free_space(right); 378144871b1bSChris Mason if (free_space < data_size) 378244871b1bSChris Mason goto out_unlock; 378344871b1bSChris Mason 378444871b1bSChris Mason /* cow and double check */ 378544871b1bSChris Mason ret = btrfs_cow_block(trans, root, right, upper, 378644871b1bSChris Mason slot + 1, &right); 378744871b1bSChris Mason if (ret) 378844871b1bSChris Mason goto out_unlock; 378944871b1bSChris Mason 3790e902baacSDavid Sterba free_space = btrfs_leaf_free_space(right); 379144871b1bSChris Mason if (free_space < data_size) 379244871b1bSChris Mason goto out_unlock; 379344871b1bSChris Mason 379444871b1bSChris Mason left_nritems = btrfs_header_nritems(left); 379544871b1bSChris Mason if (left_nritems == 0) 379644871b1bSChris Mason goto out_unlock; 379744871b1bSChris Mason 37982ef1fed2SFilipe David Borba Manana if (path->slots[0] == left_nritems && !empty) { 37992ef1fed2SFilipe David Borba Manana /* Key greater than all keys in the leaf, right neighbor has 38002ef1fed2SFilipe David Borba Manana * enough room for it and we're not emptying our leaf to delete 38012ef1fed2SFilipe David Borba Manana * it, therefore use right neighbor to insert the new item and 380252042d8eSAndrea Gelmini * no need to touch/dirty our left leaf. */ 38032ef1fed2SFilipe David Borba Manana btrfs_tree_unlock(left); 38042ef1fed2SFilipe David Borba Manana free_extent_buffer(left); 38052ef1fed2SFilipe David Borba Manana path->nodes[0] = right; 38062ef1fed2SFilipe David Borba Manana path->slots[0] = 0; 38072ef1fed2SFilipe David Borba Manana path->slots[1]++; 38082ef1fed2SFilipe David Borba Manana return 0; 38092ef1fed2SFilipe David Borba Manana } 38102ef1fed2SFilipe David Borba Manana 38111e47eef2SDavid Sterba return __push_leaf_right(fs_info, path, min_data_size, empty, 381299d8f83cSChris Mason right, free_space, left_nritems, min_slot); 381344871b1bSChris Mason out_unlock: 381444871b1bSChris Mason btrfs_tree_unlock(right); 381544871b1bSChris Mason free_extent_buffer(right); 381644871b1bSChris Mason return 1; 381744871b1bSChris Mason } 381844871b1bSChris Mason 381944871b1bSChris Mason /* 382044871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 382144871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 382299d8f83cSChris Mason * 382399d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 382499d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the 382599d8f83cSChris Mason * items 382644871b1bSChris Mason */ 382766cb7ddbSDavid Sterba static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info, 382844871b1bSChris Mason struct btrfs_path *path, int data_size, 382944871b1bSChris Mason int empty, struct extent_buffer *left, 383099d8f83cSChris Mason int free_space, u32 right_nritems, 383199d8f83cSChris Mason u32 max_slot) 383244871b1bSChris Mason { 38335f39d397SChris Mason struct btrfs_disk_key disk_key; 38345f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 3835be0e5c09SChris Mason int i; 3836be0e5c09SChris Mason int push_space = 0; 3837be0e5c09SChris Mason int push_items = 0; 38380783fcfcSChris Mason struct btrfs_item *item; 38397518a238SChris Mason u32 old_left_nritems; 384034a38218SChris Mason u32 nr; 3841aa5d6bedSChris Mason int ret = 0; 3842db94535dSChris Mason u32 this_item_size; 3843db94535dSChris Mason u32 old_left_item_size; 3844cfed81a0SChris Mason struct btrfs_map_token token; 3845cfed81a0SChris Mason 3846cfed81a0SChris Mason btrfs_init_map_token(&token); 3847be0e5c09SChris Mason 384834a38218SChris Mason if (empty) 384999d8f83cSChris Mason nr = min(right_nritems, max_slot); 385034a38218SChris Mason else 385199d8f83cSChris Mason nr = min(right_nritems - 1, max_slot); 385234a38218SChris Mason 385334a38218SChris Mason for (i = 0; i < nr; i++) { 3854dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3855db94535dSChris Mason 385631840ae1SZheng Yan if (!empty && push_items > 0) { 385731840ae1SZheng Yan if (path->slots[0] < i) 385831840ae1SZheng Yan break; 385931840ae1SZheng Yan if (path->slots[0] == i) { 3860e902baacSDavid Sterba int space = btrfs_leaf_free_space(right); 3861e902baacSDavid Sterba 386231840ae1SZheng Yan if (space + push_space * 2 > free_space) 386331840ae1SZheng Yan break; 386431840ae1SZheng Yan } 386531840ae1SZheng Yan } 386631840ae1SZheng Yan 3867be0e5c09SChris Mason if (path->slots[0] == i) 386887b29b20SYan Zheng push_space += data_size; 3869db94535dSChris Mason 3870db94535dSChris Mason this_item_size = btrfs_item_size(right, item); 3871db94535dSChris Mason if (this_item_size + sizeof(*item) + push_space > free_space) 3872be0e5c09SChris Mason break; 3873db94535dSChris Mason 3874be0e5c09SChris Mason push_items++; 3875db94535dSChris Mason push_space += this_item_size + sizeof(*item); 3876be0e5c09SChris Mason } 3877db94535dSChris Mason 3878be0e5c09SChris Mason if (push_items == 0) { 3879925baeddSChris Mason ret = 1; 3880925baeddSChris Mason goto out; 3881be0e5c09SChris Mason } 3882fae7f21cSDulshani Gunawardhana WARN_ON(!empty && push_items == btrfs_header_nritems(right)); 38835f39d397SChris Mason 3884be0e5c09SChris Mason /* push data from right to left */ 38855f39d397SChris Mason copy_extent_buffer(left, right, 38865f39d397SChris Mason btrfs_item_nr_offset(btrfs_header_nritems(left)), 38875f39d397SChris Mason btrfs_item_nr_offset(0), 38885f39d397SChris Mason push_items * sizeof(struct btrfs_item)); 38895f39d397SChris Mason 38900b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info) - 38915f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1); 38925f39d397SChris Mason 38933d9ec8c4SNikolay Borisov copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET + 38948f881e8cSDavid Sterba leaf_data_end(left) - push_space, 38953d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + 38965f39d397SChris Mason btrfs_item_offset_nr(right, push_items - 1), 3897be0e5c09SChris Mason push_space); 38985f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 389987b29b20SYan Zheng BUG_ON(old_left_nritems <= 0); 3900eb60ceacSChris Mason 3901db94535dSChris Mason old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 3902be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 39035f39d397SChris Mason u32 ioff; 3904db94535dSChris Mason 3905dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3906db94535dSChris Mason 3907cfed81a0SChris Mason ioff = btrfs_token_item_offset(left, item, &token); 3908cfed81a0SChris Mason btrfs_set_token_item_offset(left, item, 39090b246afaSJeff Mahoney ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size), 3910cfed81a0SChris Mason &token); 3911be0e5c09SChris Mason } 39125f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 3913be0e5c09SChris Mason 3914be0e5c09SChris Mason /* fixup right node */ 391531b1a2bdSJulia Lawall if (push_items > right_nritems) 391631b1a2bdSJulia Lawall WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, 3917d397712bSChris Mason right_nritems); 391834a38218SChris Mason 391934a38218SChris Mason if (push_items < right_nritems) { 39205f39d397SChris Mason push_space = btrfs_item_offset_nr(right, push_items - 1) - 39218f881e8cSDavid Sterba leaf_data_end(right); 39223d9ec8c4SNikolay Borisov memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET + 39230b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 39243d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + 39258f881e8cSDavid Sterba leaf_data_end(right), push_space); 39265f39d397SChris Mason 39275f39d397SChris Mason memmove_extent_buffer(right, btrfs_item_nr_offset(0), 39285f39d397SChris Mason btrfs_item_nr_offset(push_items), 39295f39d397SChris Mason (btrfs_header_nritems(right) - push_items) * 39300783fcfcSChris Mason sizeof(struct btrfs_item)); 393134a38218SChris Mason } 3932eef1c494SYan right_nritems -= push_items; 3933eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 39340b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info); 39355f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 3936dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 3937db94535dSChris Mason 3938cfed81a0SChris Mason push_space = push_space - btrfs_token_item_size(right, 3939cfed81a0SChris Mason item, &token); 3940cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, push_space, &token); 3941db94535dSChris Mason } 3942eb60ceacSChris Mason 39435f39d397SChris Mason btrfs_mark_buffer_dirty(left); 394434a38218SChris Mason if (right_nritems) 39455f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3946f0486c68SYan, Zheng else 39476a884d7dSDavid Sterba btrfs_clean_tree_block(right); 3948098f59c2SChris Mason 39495f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 3950b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 3951be0e5c09SChris Mason 3952be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 3953be0e5c09SChris Mason if (path->slots[0] < push_items) { 3954be0e5c09SChris Mason path->slots[0] += old_left_nritems; 3955925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 39565f39d397SChris Mason free_extent_buffer(path->nodes[0]); 39575f39d397SChris Mason path->nodes[0] = left; 3958be0e5c09SChris Mason path->slots[1] -= 1; 3959be0e5c09SChris Mason } else { 3960925baeddSChris Mason btrfs_tree_unlock(left); 39615f39d397SChris Mason free_extent_buffer(left); 3962be0e5c09SChris Mason path->slots[0] -= push_items; 3963be0e5c09SChris Mason } 3964eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 3965aa5d6bedSChris Mason return ret; 3966925baeddSChris Mason out: 3967925baeddSChris Mason btrfs_tree_unlock(left); 3968925baeddSChris Mason free_extent_buffer(left); 3969925baeddSChris Mason return ret; 3970be0e5c09SChris Mason } 3971be0e5c09SChris Mason 397274123bd7SChris Mason /* 397344871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 397444871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 397599d8f83cSChris Mason * 397699d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 397799d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the 397899d8f83cSChris Mason * items 397944871b1bSChris Mason */ 398044871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 398199d8f83cSChris Mason *root, struct btrfs_path *path, int min_data_size, 398299d8f83cSChris Mason int data_size, int empty, u32 max_slot) 398344871b1bSChris Mason { 39842ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 398544871b1bSChris Mason struct extent_buffer *right = path->nodes[0]; 398644871b1bSChris Mason struct extent_buffer *left; 398744871b1bSChris Mason int slot; 398844871b1bSChris Mason int free_space; 398944871b1bSChris Mason u32 right_nritems; 399044871b1bSChris Mason int ret = 0; 399144871b1bSChris Mason 399244871b1bSChris Mason slot = path->slots[1]; 399344871b1bSChris Mason if (slot == 0) 399444871b1bSChris Mason return 1; 399544871b1bSChris Mason if (!path->nodes[1]) 399644871b1bSChris Mason return 1; 399744871b1bSChris Mason 399844871b1bSChris Mason right_nritems = btrfs_header_nritems(right); 399944871b1bSChris Mason if (right_nritems == 0) 400044871b1bSChris Mason return 1; 400144871b1bSChris Mason 400244871b1bSChris Mason btrfs_assert_tree_locked(path->nodes[1]); 400344871b1bSChris Mason 4004d0d20b0fSDavid Sterba left = read_node_slot(path->nodes[1], slot - 1); 4005fb770ae4SLiu Bo /* 4006fb770ae4SLiu Bo * slot - 1 is not valid or we fail to read the left node, 4007fb770ae4SLiu Bo * no big deal, just return. 4008fb770ae4SLiu Bo */ 4009fb770ae4SLiu Bo if (IS_ERR(left)) 401091ca338dSTsutomu Itoh return 1; 401191ca338dSTsutomu Itoh 401244871b1bSChris Mason btrfs_tree_lock(left); 40138bead258SDavid Sterba btrfs_set_lock_blocking_write(left); 401444871b1bSChris Mason 4015e902baacSDavid Sterba free_space = btrfs_leaf_free_space(left); 401644871b1bSChris Mason if (free_space < data_size) { 401744871b1bSChris Mason ret = 1; 401844871b1bSChris Mason goto out; 401944871b1bSChris Mason } 402044871b1bSChris Mason 402144871b1bSChris Mason /* cow and double check */ 402244871b1bSChris Mason ret = btrfs_cow_block(trans, root, left, 402344871b1bSChris Mason path->nodes[1], slot - 1, &left); 402444871b1bSChris Mason if (ret) { 402544871b1bSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 402679787eaaSJeff Mahoney if (ret == -ENOSPC) 402744871b1bSChris Mason ret = 1; 402844871b1bSChris Mason goto out; 402944871b1bSChris Mason } 403044871b1bSChris Mason 4031e902baacSDavid Sterba free_space = btrfs_leaf_free_space(left); 403244871b1bSChris Mason if (free_space < data_size) { 403344871b1bSChris Mason ret = 1; 403444871b1bSChris Mason goto out; 403544871b1bSChris Mason } 403644871b1bSChris Mason 403766cb7ddbSDavid Sterba return __push_leaf_left(fs_info, path, min_data_size, 403899d8f83cSChris Mason empty, left, free_space, right_nritems, 403999d8f83cSChris Mason max_slot); 404044871b1bSChris Mason out: 404144871b1bSChris Mason btrfs_tree_unlock(left); 404244871b1bSChris Mason free_extent_buffer(left); 404344871b1bSChris Mason return ret; 404444871b1bSChris Mason } 404544871b1bSChris Mason 404644871b1bSChris Mason /* 404774123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 404874123bd7SChris Mason * available for the resulting leaf level of the path. 404974123bd7SChris Mason */ 4050143bede5SJeff Mahoney static noinline void copy_for_split(struct btrfs_trans_handle *trans, 40512ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 405244871b1bSChris Mason struct btrfs_path *path, 405344871b1bSChris Mason struct extent_buffer *l, 405444871b1bSChris Mason struct extent_buffer *right, 405544871b1bSChris Mason int slot, int mid, int nritems) 4056be0e5c09SChris Mason { 4057be0e5c09SChris Mason int data_copy_size; 4058be0e5c09SChris Mason int rt_data_off; 4059be0e5c09SChris Mason int i; 4060d4dbff95SChris Mason struct btrfs_disk_key disk_key; 4061cfed81a0SChris Mason struct btrfs_map_token token; 4062cfed81a0SChris Mason 4063cfed81a0SChris Mason btrfs_init_map_token(&token); 4064be0e5c09SChris Mason 40655f39d397SChris Mason nritems = nritems - mid; 40665f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 40678f881e8cSDavid Sterba data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l); 40685f39d397SChris Mason 40695f39d397SChris Mason copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 40705f39d397SChris Mason btrfs_item_nr_offset(mid), 40715f39d397SChris Mason nritems * sizeof(struct btrfs_item)); 40725f39d397SChris Mason 40735f39d397SChris Mason copy_extent_buffer(right, l, 40743d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) - 40753d9ec8c4SNikolay Borisov data_copy_size, BTRFS_LEAF_DATA_OFFSET + 40768f881e8cSDavid Sterba leaf_data_end(l), data_copy_size); 407774123bd7SChris Mason 40780b246afaSJeff Mahoney rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid); 40795f39d397SChris Mason 40805f39d397SChris Mason for (i = 0; i < nritems; i++) { 4081dd3cc16bSRoss Kirk struct btrfs_item *item = btrfs_item_nr(i); 4082db94535dSChris Mason u32 ioff; 4083db94535dSChris Mason 4084cfed81a0SChris Mason ioff = btrfs_token_item_offset(right, item, &token); 4085cfed81a0SChris Mason btrfs_set_token_item_offset(right, item, 4086cfed81a0SChris Mason ioff + rt_data_off, &token); 40870783fcfcSChris Mason } 408874123bd7SChris Mason 40895f39d397SChris Mason btrfs_set_header_nritems(l, mid); 40905f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 40912ff7e61eSJeff Mahoney insert_ptr(trans, fs_info, path, &disk_key, right->start, 4092c3e06965SJan Schmidt path->slots[1] + 1, 1); 40935f39d397SChris Mason 40945f39d397SChris Mason btrfs_mark_buffer_dirty(right); 40955f39d397SChris Mason btrfs_mark_buffer_dirty(l); 4096eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 40975f39d397SChris Mason 4098be0e5c09SChris Mason if (mid <= slot) { 4099925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 41005f39d397SChris Mason free_extent_buffer(path->nodes[0]); 41015f39d397SChris Mason path->nodes[0] = right; 4102be0e5c09SChris Mason path->slots[0] -= mid; 4103be0e5c09SChris Mason path->slots[1] += 1; 4104925baeddSChris Mason } else { 4105925baeddSChris Mason btrfs_tree_unlock(right); 41065f39d397SChris Mason free_extent_buffer(right); 4107925baeddSChris Mason } 41085f39d397SChris Mason 4109eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 411044871b1bSChris Mason } 411144871b1bSChris Mason 411244871b1bSChris Mason /* 411399d8f83cSChris Mason * double splits happen when we need to insert a big item in the middle 411499d8f83cSChris Mason * of a leaf. A double split can leave us with 3 mostly empty leaves: 411599d8f83cSChris Mason * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] 411699d8f83cSChris Mason * A B C 411799d8f83cSChris Mason * 411899d8f83cSChris Mason * We avoid this by trying to push the items on either side of our target 411999d8f83cSChris Mason * into the adjacent leaves. If all goes well we can avoid the double split 412099d8f83cSChris Mason * completely. 412199d8f83cSChris Mason */ 412299d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans, 412399d8f83cSChris Mason struct btrfs_root *root, 412499d8f83cSChris Mason struct btrfs_path *path, 412599d8f83cSChris Mason int data_size) 412699d8f83cSChris Mason { 412799d8f83cSChris Mason int ret; 412899d8f83cSChris Mason int progress = 0; 412999d8f83cSChris Mason int slot; 413099d8f83cSChris Mason u32 nritems; 41315a4267caSFilipe David Borba Manana int space_needed = data_size; 413299d8f83cSChris Mason 413399d8f83cSChris Mason slot = path->slots[0]; 41345a4267caSFilipe David Borba Manana if (slot < btrfs_header_nritems(path->nodes[0])) 4135e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(path->nodes[0]); 413699d8f83cSChris Mason 413799d8f83cSChris Mason /* 413899d8f83cSChris Mason * try to push all the items after our slot into the 413999d8f83cSChris Mason * right leaf 414099d8f83cSChris Mason */ 41415a4267caSFilipe David Borba Manana ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot); 414299d8f83cSChris Mason if (ret < 0) 414399d8f83cSChris Mason return ret; 414499d8f83cSChris Mason 414599d8f83cSChris Mason if (ret == 0) 414699d8f83cSChris Mason progress++; 414799d8f83cSChris Mason 414899d8f83cSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 414999d8f83cSChris Mason /* 415099d8f83cSChris Mason * our goal is to get our slot at the start or end of a leaf. If 415199d8f83cSChris Mason * we've done so we're done 415299d8f83cSChris Mason */ 415399d8f83cSChris Mason if (path->slots[0] == 0 || path->slots[0] == nritems) 415499d8f83cSChris Mason return 0; 415599d8f83cSChris Mason 4156e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) 415799d8f83cSChris Mason return 0; 415899d8f83cSChris Mason 415999d8f83cSChris Mason /* try to push all the items before our slot into the next leaf */ 416099d8f83cSChris Mason slot = path->slots[0]; 4161263d3995SFilipe Manana space_needed = data_size; 4162263d3995SFilipe Manana if (slot > 0) 4163e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(path->nodes[0]); 41645a4267caSFilipe David Borba Manana ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot); 416599d8f83cSChris Mason if (ret < 0) 416699d8f83cSChris Mason return ret; 416799d8f83cSChris Mason 416899d8f83cSChris Mason if (ret == 0) 416999d8f83cSChris Mason progress++; 417099d8f83cSChris Mason 417199d8f83cSChris Mason if (progress) 417299d8f83cSChris Mason return 0; 417399d8f83cSChris Mason return 1; 417499d8f83cSChris Mason } 417599d8f83cSChris Mason 417699d8f83cSChris Mason /* 417744871b1bSChris Mason * split the path's leaf in two, making sure there is at least data_size 417844871b1bSChris Mason * available for the resulting leaf level of the path. 417944871b1bSChris Mason * 418044871b1bSChris Mason * returns 0 if all went well and < 0 on failure. 418144871b1bSChris Mason */ 418244871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 418344871b1bSChris Mason struct btrfs_root *root, 4184310712b2SOmar Sandoval const struct btrfs_key *ins_key, 418544871b1bSChris Mason struct btrfs_path *path, int data_size, 418644871b1bSChris Mason int extend) 418744871b1bSChris Mason { 41885d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 418944871b1bSChris Mason struct extent_buffer *l; 419044871b1bSChris Mason u32 nritems; 419144871b1bSChris Mason int mid; 419244871b1bSChris Mason int slot; 419344871b1bSChris Mason struct extent_buffer *right; 4194b7a0365eSDaniel Dressler struct btrfs_fs_info *fs_info = root->fs_info; 419544871b1bSChris Mason int ret = 0; 419644871b1bSChris Mason int wret; 41975d4f98a2SYan Zheng int split; 419844871b1bSChris Mason int num_doubles = 0; 419999d8f83cSChris Mason int tried_avoid_double = 0; 420044871b1bSChris Mason 4201a5719521SYan, Zheng l = path->nodes[0]; 4202a5719521SYan, Zheng slot = path->slots[0]; 4203a5719521SYan, Zheng if (extend && data_size + btrfs_item_size_nr(l, slot) + 42040b246afaSJeff Mahoney sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info)) 4205a5719521SYan, Zheng return -EOVERFLOW; 4206a5719521SYan, Zheng 420744871b1bSChris Mason /* first try to make some room by pushing left and right */ 420833157e05SLiu Bo if (data_size && path->nodes[1]) { 42095a4267caSFilipe David Borba Manana int space_needed = data_size; 42105a4267caSFilipe David Borba Manana 42115a4267caSFilipe David Borba Manana if (slot < btrfs_header_nritems(l)) 4212e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(l); 42135a4267caSFilipe David Borba Manana 42145a4267caSFilipe David Borba Manana wret = push_leaf_right(trans, root, path, space_needed, 42155a4267caSFilipe David Borba Manana space_needed, 0, 0); 421644871b1bSChris Mason if (wret < 0) 421744871b1bSChris Mason return wret; 421844871b1bSChris Mason if (wret) { 4219263d3995SFilipe Manana space_needed = data_size; 4220263d3995SFilipe Manana if (slot > 0) 4221e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(l); 42225a4267caSFilipe David Borba Manana wret = push_leaf_left(trans, root, path, space_needed, 42235a4267caSFilipe David Borba Manana space_needed, 0, (u32)-1); 422444871b1bSChris Mason if (wret < 0) 422544871b1bSChris Mason return wret; 422644871b1bSChris Mason } 422744871b1bSChris Mason l = path->nodes[0]; 422844871b1bSChris Mason 422944871b1bSChris Mason /* did the pushes work? */ 4230e902baacSDavid Sterba if (btrfs_leaf_free_space(l) >= data_size) 423144871b1bSChris Mason return 0; 423244871b1bSChris Mason } 423344871b1bSChris Mason 423444871b1bSChris Mason if (!path->nodes[1]) { 4235fdd99c72SLiu Bo ret = insert_new_root(trans, root, path, 1); 423644871b1bSChris Mason if (ret) 423744871b1bSChris Mason return ret; 423844871b1bSChris Mason } 423944871b1bSChris Mason again: 42405d4f98a2SYan Zheng split = 1; 424144871b1bSChris Mason l = path->nodes[0]; 424244871b1bSChris Mason slot = path->slots[0]; 424344871b1bSChris Mason nritems = btrfs_header_nritems(l); 424444871b1bSChris Mason mid = (nritems + 1) / 2; 424544871b1bSChris Mason 42465d4f98a2SYan Zheng if (mid <= slot) { 42475d4f98a2SYan Zheng if (nritems == 1 || 42485d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + data_size > 42490b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 42505d4f98a2SYan Zheng if (slot >= nritems) { 42515d4f98a2SYan Zheng split = 0; 42525d4f98a2SYan Zheng } else { 42535d4f98a2SYan Zheng mid = slot; 42545d4f98a2SYan Zheng if (mid != nritems && 42555d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 42560b246afaSJeff Mahoney data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { 425799d8f83cSChris Mason if (data_size && !tried_avoid_double) 425899d8f83cSChris Mason goto push_for_double; 42595d4f98a2SYan Zheng split = 2; 42605d4f98a2SYan Zheng } 42615d4f98a2SYan Zheng } 42625d4f98a2SYan Zheng } 42635d4f98a2SYan Zheng } else { 42645d4f98a2SYan Zheng if (leaf_space_used(l, 0, mid) + data_size > 42650b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 42665d4f98a2SYan Zheng if (!extend && data_size && slot == 0) { 42675d4f98a2SYan Zheng split = 0; 42685d4f98a2SYan Zheng } else if ((extend || !data_size) && slot == 0) { 42695d4f98a2SYan Zheng mid = 1; 42705d4f98a2SYan Zheng } else { 42715d4f98a2SYan Zheng mid = slot; 42725d4f98a2SYan Zheng if (mid != nritems && 42735d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 42740b246afaSJeff Mahoney data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { 427599d8f83cSChris Mason if (data_size && !tried_avoid_double) 427699d8f83cSChris Mason goto push_for_double; 42775d4f98a2SYan Zheng split = 2; 42785d4f98a2SYan Zheng } 42795d4f98a2SYan Zheng } 42805d4f98a2SYan Zheng } 42815d4f98a2SYan Zheng } 42825d4f98a2SYan Zheng 42835d4f98a2SYan Zheng if (split == 0) 42845d4f98a2SYan Zheng btrfs_cpu_key_to_disk(&disk_key, ins_key); 42855d4f98a2SYan Zheng else 42865d4f98a2SYan Zheng btrfs_item_key(l, &disk_key, mid); 42875d4f98a2SYan Zheng 4288a6279470SFilipe Manana right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0, 4289a6279470SFilipe Manana l->start, 0); 4290f0486c68SYan, Zheng if (IS_ERR(right)) 429144871b1bSChris Mason return PTR_ERR(right); 4292f0486c68SYan, Zheng 42930b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 429444871b1bSChris Mason 42955d4f98a2SYan Zheng if (split == 0) { 429644871b1bSChris Mason if (mid <= slot) { 429744871b1bSChris Mason btrfs_set_header_nritems(right, 0); 42982ff7e61eSJeff Mahoney insert_ptr(trans, fs_info, path, &disk_key, 42992ff7e61eSJeff Mahoney right->start, path->slots[1] + 1, 1); 430044871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 430144871b1bSChris Mason free_extent_buffer(path->nodes[0]); 430244871b1bSChris Mason path->nodes[0] = right; 430344871b1bSChris Mason path->slots[0] = 0; 430444871b1bSChris Mason path->slots[1] += 1; 430544871b1bSChris Mason } else { 430644871b1bSChris Mason btrfs_set_header_nritems(right, 0); 43072ff7e61eSJeff Mahoney insert_ptr(trans, fs_info, path, &disk_key, 43082ff7e61eSJeff Mahoney right->start, path->slots[1], 1); 430944871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 431044871b1bSChris Mason free_extent_buffer(path->nodes[0]); 431144871b1bSChris Mason path->nodes[0] = right; 431244871b1bSChris Mason path->slots[0] = 0; 4313143bede5SJeff Mahoney if (path->slots[1] == 0) 4314b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 43155d4f98a2SYan Zheng } 4316196e0249SLiu Bo /* 4317196e0249SLiu Bo * We create a new leaf 'right' for the required ins_len and 4318196e0249SLiu Bo * we'll do btrfs_mark_buffer_dirty() on this leaf after copying 4319196e0249SLiu Bo * the content of ins_len to 'right'. 4320196e0249SLiu Bo */ 432144871b1bSChris Mason return ret; 432244871b1bSChris Mason } 432344871b1bSChris Mason 43242ff7e61eSJeff Mahoney copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems); 432544871b1bSChris Mason 43265d4f98a2SYan Zheng if (split == 2) { 4327cc0c5538SChris Mason BUG_ON(num_doubles != 0); 4328cc0c5538SChris Mason num_doubles++; 4329cc0c5538SChris Mason goto again; 43303326d1b0SChris Mason } 433144871b1bSChris Mason 4332143bede5SJeff Mahoney return 0; 433399d8f83cSChris Mason 433499d8f83cSChris Mason push_for_double: 433599d8f83cSChris Mason push_for_double_split(trans, root, path, data_size); 433699d8f83cSChris Mason tried_avoid_double = 1; 4337e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) 433899d8f83cSChris Mason return 0; 433999d8f83cSChris Mason goto again; 4340be0e5c09SChris Mason } 4341be0e5c09SChris Mason 4342ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, 4343ad48fd75SYan, Zheng struct btrfs_root *root, 4344ad48fd75SYan, Zheng struct btrfs_path *path, int ins_len) 4345ad48fd75SYan, Zheng { 4346ad48fd75SYan, Zheng struct btrfs_key key; 4347ad48fd75SYan, Zheng struct extent_buffer *leaf; 4348ad48fd75SYan, Zheng struct btrfs_file_extent_item *fi; 4349ad48fd75SYan, Zheng u64 extent_len = 0; 4350ad48fd75SYan, Zheng u32 item_size; 4351ad48fd75SYan, Zheng int ret; 4352ad48fd75SYan, Zheng 4353ad48fd75SYan, Zheng leaf = path->nodes[0]; 4354ad48fd75SYan, Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 4355ad48fd75SYan, Zheng 4356ad48fd75SYan, Zheng BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 4357ad48fd75SYan, Zheng key.type != BTRFS_EXTENT_CSUM_KEY); 4358ad48fd75SYan, Zheng 4359e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) >= ins_len) 4360ad48fd75SYan, Zheng return 0; 4361ad48fd75SYan, Zheng 4362ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 4363ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 4364ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 4365ad48fd75SYan, Zheng struct btrfs_file_extent_item); 4366ad48fd75SYan, Zheng extent_len = btrfs_file_extent_num_bytes(leaf, fi); 4367ad48fd75SYan, Zheng } 4368b3b4aa74SDavid Sterba btrfs_release_path(path); 4369ad48fd75SYan, Zheng 4370ad48fd75SYan, Zheng path->keep_locks = 1; 4371ad48fd75SYan, Zheng path->search_for_split = 1; 4372ad48fd75SYan, Zheng ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 4373ad48fd75SYan, Zheng path->search_for_split = 0; 4374a8df6fe6SFilipe Manana if (ret > 0) 4375a8df6fe6SFilipe Manana ret = -EAGAIN; 4376ad48fd75SYan, Zheng if (ret < 0) 4377ad48fd75SYan, Zheng goto err; 4378ad48fd75SYan, Zheng 4379ad48fd75SYan, Zheng ret = -EAGAIN; 4380ad48fd75SYan, Zheng leaf = path->nodes[0]; 4381a8df6fe6SFilipe Manana /* if our item isn't there, return now */ 4382a8df6fe6SFilipe Manana if (item_size != btrfs_item_size_nr(leaf, path->slots[0])) 4383ad48fd75SYan, Zheng goto err; 4384ad48fd75SYan, Zheng 4385109f6aefSChris Mason /* the leaf has changed, it now has room. return now */ 4386e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len) 4387109f6aefSChris Mason goto err; 4388109f6aefSChris Mason 4389ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 4390ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 4391ad48fd75SYan, Zheng struct btrfs_file_extent_item); 4392ad48fd75SYan, Zheng if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) 4393ad48fd75SYan, Zheng goto err; 4394ad48fd75SYan, Zheng } 4395ad48fd75SYan, Zheng 4396ad48fd75SYan, Zheng btrfs_set_path_blocking(path); 4397ad48fd75SYan, Zheng ret = split_leaf(trans, root, &key, path, ins_len, 1); 4398f0486c68SYan, Zheng if (ret) 4399f0486c68SYan, Zheng goto err; 4400ad48fd75SYan, Zheng 4401ad48fd75SYan, Zheng path->keep_locks = 0; 4402ad48fd75SYan, Zheng btrfs_unlock_up_safe(path, 1); 4403ad48fd75SYan, Zheng return 0; 4404ad48fd75SYan, Zheng err: 4405ad48fd75SYan, Zheng path->keep_locks = 0; 4406ad48fd75SYan, Zheng return ret; 4407ad48fd75SYan, Zheng } 4408ad48fd75SYan, Zheng 44094961e293SDavid Sterba static noinline int split_item(struct btrfs_fs_info *fs_info, 4410459931ecSChris Mason struct btrfs_path *path, 4411310712b2SOmar Sandoval const struct btrfs_key *new_key, 4412459931ecSChris Mason unsigned long split_offset) 4413459931ecSChris Mason { 4414459931ecSChris Mason struct extent_buffer *leaf; 4415459931ecSChris Mason struct btrfs_item *item; 4416459931ecSChris Mason struct btrfs_item *new_item; 4417459931ecSChris Mason int slot; 4418ad48fd75SYan, Zheng char *buf; 4419459931ecSChris Mason u32 nritems; 4420ad48fd75SYan, Zheng u32 item_size; 4421459931ecSChris Mason u32 orig_offset; 4422459931ecSChris Mason struct btrfs_disk_key disk_key; 4423459931ecSChris Mason 4424459931ecSChris Mason leaf = path->nodes[0]; 4425e902baacSDavid Sterba BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)); 4426b9473439SChris Mason 4427b4ce94deSChris Mason btrfs_set_path_blocking(path); 4428b4ce94deSChris Mason 4429dd3cc16bSRoss Kirk item = btrfs_item_nr(path->slots[0]); 4430459931ecSChris Mason orig_offset = btrfs_item_offset(leaf, item); 4431459931ecSChris Mason item_size = btrfs_item_size(leaf, item); 4432459931ecSChris Mason 4433459931ecSChris Mason buf = kmalloc(item_size, GFP_NOFS); 4434ad48fd75SYan, Zheng if (!buf) 4435ad48fd75SYan, Zheng return -ENOMEM; 4436ad48fd75SYan, Zheng 4437459931ecSChris Mason read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, 4438459931ecSChris Mason path->slots[0]), item_size); 4439ad48fd75SYan, Zheng 4440459931ecSChris Mason slot = path->slots[0] + 1; 4441459931ecSChris Mason nritems = btrfs_header_nritems(leaf); 4442459931ecSChris Mason if (slot != nritems) { 4443459931ecSChris Mason /* shift the items */ 4444459931ecSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), 4445459931ecSChris Mason btrfs_item_nr_offset(slot), 4446459931ecSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 4447459931ecSChris Mason } 4448459931ecSChris Mason 4449459931ecSChris Mason btrfs_cpu_key_to_disk(&disk_key, new_key); 4450459931ecSChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4451459931ecSChris Mason 4452dd3cc16bSRoss Kirk new_item = btrfs_item_nr(slot); 4453459931ecSChris Mason 4454459931ecSChris Mason btrfs_set_item_offset(leaf, new_item, orig_offset); 4455459931ecSChris Mason btrfs_set_item_size(leaf, new_item, item_size - split_offset); 4456459931ecSChris Mason 4457459931ecSChris Mason btrfs_set_item_offset(leaf, item, 4458459931ecSChris Mason orig_offset + item_size - split_offset); 4459459931ecSChris Mason btrfs_set_item_size(leaf, item, split_offset); 4460459931ecSChris Mason 4461459931ecSChris Mason btrfs_set_header_nritems(leaf, nritems + 1); 4462459931ecSChris Mason 4463459931ecSChris Mason /* write the data for the start of the original item */ 4464459931ecSChris Mason write_extent_buffer(leaf, buf, 4465459931ecSChris Mason btrfs_item_ptr_offset(leaf, path->slots[0]), 4466459931ecSChris Mason split_offset); 4467459931ecSChris Mason 4468459931ecSChris Mason /* write the data for the new item */ 4469459931ecSChris Mason write_extent_buffer(leaf, buf + split_offset, 4470459931ecSChris Mason btrfs_item_ptr_offset(leaf, slot), 4471459931ecSChris Mason item_size - split_offset); 4472459931ecSChris Mason btrfs_mark_buffer_dirty(leaf); 4473459931ecSChris Mason 4474e902baacSDavid Sterba BUG_ON(btrfs_leaf_free_space(leaf) < 0); 4475459931ecSChris Mason kfree(buf); 4476ad48fd75SYan, Zheng return 0; 4477ad48fd75SYan, Zheng } 4478ad48fd75SYan, Zheng 4479ad48fd75SYan, Zheng /* 4480ad48fd75SYan, Zheng * This function splits a single item into two items, 4481ad48fd75SYan, Zheng * giving 'new_key' to the new item and splitting the 4482ad48fd75SYan, Zheng * old one at split_offset (from the start of the item). 4483ad48fd75SYan, Zheng * 4484ad48fd75SYan, Zheng * The path may be released by this operation. After 4485ad48fd75SYan, Zheng * the split, the path is pointing to the old item. The 4486ad48fd75SYan, Zheng * new item is going to be in the same node as the old one. 4487ad48fd75SYan, Zheng * 4488ad48fd75SYan, Zheng * Note, the item being split must be smaller enough to live alone on 4489ad48fd75SYan, Zheng * a tree block with room for one extra struct btrfs_item 4490ad48fd75SYan, Zheng * 4491ad48fd75SYan, Zheng * This allows us to split the item in place, keeping a lock on the 4492ad48fd75SYan, Zheng * leaf the entire time. 4493ad48fd75SYan, Zheng */ 4494ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans, 4495ad48fd75SYan, Zheng struct btrfs_root *root, 4496ad48fd75SYan, Zheng struct btrfs_path *path, 4497310712b2SOmar Sandoval const struct btrfs_key *new_key, 4498ad48fd75SYan, Zheng unsigned long split_offset) 4499ad48fd75SYan, Zheng { 4500ad48fd75SYan, Zheng int ret; 4501ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 4502ad48fd75SYan, Zheng sizeof(struct btrfs_item)); 4503ad48fd75SYan, Zheng if (ret) 4504459931ecSChris Mason return ret; 4505ad48fd75SYan, Zheng 45064961e293SDavid Sterba ret = split_item(root->fs_info, path, new_key, split_offset); 4507ad48fd75SYan, Zheng return ret; 4508ad48fd75SYan, Zheng } 4509ad48fd75SYan, Zheng 4510ad48fd75SYan, Zheng /* 4511ad48fd75SYan, Zheng * This function duplicate a item, giving 'new_key' to the new item. 4512ad48fd75SYan, Zheng * It guarantees both items live in the same tree leaf and the new item 4513ad48fd75SYan, Zheng * is contiguous with the original item. 4514ad48fd75SYan, Zheng * 4515ad48fd75SYan, Zheng * This allows us to split file extent in place, keeping a lock on the 4516ad48fd75SYan, Zheng * leaf the entire time. 4517ad48fd75SYan, Zheng */ 4518ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans, 4519ad48fd75SYan, Zheng struct btrfs_root *root, 4520ad48fd75SYan, Zheng struct btrfs_path *path, 4521310712b2SOmar Sandoval const struct btrfs_key *new_key) 4522ad48fd75SYan, Zheng { 4523ad48fd75SYan, Zheng struct extent_buffer *leaf; 4524ad48fd75SYan, Zheng int ret; 4525ad48fd75SYan, Zheng u32 item_size; 4526ad48fd75SYan, Zheng 4527ad48fd75SYan, Zheng leaf = path->nodes[0]; 4528ad48fd75SYan, Zheng item_size = btrfs_item_size_nr(leaf, path->slots[0]); 4529ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 4530ad48fd75SYan, Zheng item_size + sizeof(struct btrfs_item)); 4531ad48fd75SYan, Zheng if (ret) 4532ad48fd75SYan, Zheng return ret; 4533ad48fd75SYan, Zheng 4534ad48fd75SYan, Zheng path->slots[0]++; 4535afe5fea7STsutomu Itoh setup_items_for_insert(root, path, new_key, &item_size, 4536ad48fd75SYan, Zheng item_size, item_size + 4537ad48fd75SYan, Zheng sizeof(struct btrfs_item), 1); 4538ad48fd75SYan, Zheng leaf = path->nodes[0]; 4539ad48fd75SYan, Zheng memcpy_extent_buffer(leaf, 4540ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0]), 4541ad48fd75SYan, Zheng btrfs_item_ptr_offset(leaf, path->slots[0] - 1), 4542ad48fd75SYan, Zheng item_size); 4543ad48fd75SYan, Zheng return 0; 4544459931ecSChris Mason } 4545459931ecSChris Mason 4546459931ecSChris Mason /* 4547d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 4548d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 4549d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 4550d352ac68SChris Mason * the front. 4551d352ac68SChris Mason */ 45522ff7e61eSJeff Mahoney void btrfs_truncate_item(struct btrfs_fs_info *fs_info, 45532ff7e61eSJeff Mahoney struct btrfs_path *path, u32 new_size, int from_end) 4554b18c6685SChris Mason { 4555b18c6685SChris Mason int slot; 45565f39d397SChris Mason struct extent_buffer *leaf; 45575f39d397SChris Mason struct btrfs_item *item; 4558b18c6685SChris Mason u32 nritems; 4559b18c6685SChris Mason unsigned int data_end; 4560b18c6685SChris Mason unsigned int old_data_start; 4561b18c6685SChris Mason unsigned int old_size; 4562b18c6685SChris Mason unsigned int size_diff; 4563b18c6685SChris Mason int i; 4564cfed81a0SChris Mason struct btrfs_map_token token; 4565cfed81a0SChris Mason 4566cfed81a0SChris Mason btrfs_init_map_token(&token); 4567b18c6685SChris Mason 45685f39d397SChris Mason leaf = path->nodes[0]; 4569179e29e4SChris Mason slot = path->slots[0]; 4570179e29e4SChris Mason 4571179e29e4SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 4572179e29e4SChris Mason if (old_size == new_size) 4573143bede5SJeff Mahoney return; 4574b18c6685SChris Mason 45755f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 45768f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 4577b18c6685SChris Mason 45785f39d397SChris Mason old_data_start = btrfs_item_offset_nr(leaf, slot); 4579179e29e4SChris Mason 4580b18c6685SChris Mason size_diff = old_size - new_size; 4581b18c6685SChris Mason 4582b18c6685SChris Mason BUG_ON(slot < 0); 4583b18c6685SChris Mason BUG_ON(slot >= nritems); 4584b18c6685SChris Mason 4585b18c6685SChris Mason /* 4586b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4587b18c6685SChris Mason */ 4588b18c6685SChris Mason /* first correct the data pointers */ 4589b18c6685SChris Mason for (i = slot; i < nritems; i++) { 45905f39d397SChris Mason u32 ioff; 4591dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4592db94535dSChris Mason 4593cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4594cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4595cfed81a0SChris Mason ioff + size_diff, &token); 4596b18c6685SChris Mason } 4597db94535dSChris Mason 4598b18c6685SChris Mason /* shift the data */ 4599179e29e4SChris Mason if (from_end) { 46003d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 46013d9ec8c4SNikolay Borisov data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + 4602b18c6685SChris Mason data_end, old_data_start + new_size - data_end); 4603179e29e4SChris Mason } else { 4604179e29e4SChris Mason struct btrfs_disk_key disk_key; 4605179e29e4SChris Mason u64 offset; 4606179e29e4SChris Mason 4607179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 4608179e29e4SChris Mason 4609179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 4610179e29e4SChris Mason unsigned long ptr; 4611179e29e4SChris Mason struct btrfs_file_extent_item *fi; 4612179e29e4SChris Mason 4613179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 4614179e29e4SChris Mason struct btrfs_file_extent_item); 4615179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 4616179e29e4SChris Mason (unsigned long)fi - size_diff); 4617179e29e4SChris Mason 4618179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 4619179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 4620179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 4621179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 4622179e29e4SChris Mason (unsigned long)fi, 46237ec20afbSDavid Sterba BTRFS_FILE_EXTENT_INLINE_DATA_START); 4624179e29e4SChris Mason } 4625179e29e4SChris Mason } 4626179e29e4SChris Mason 46273d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 46283d9ec8c4SNikolay Borisov data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + 4629179e29e4SChris Mason data_end, old_data_start - data_end); 4630179e29e4SChris Mason 4631179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 4632179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 4633179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4634179e29e4SChris Mason if (slot == 0) 4635b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 4636179e29e4SChris Mason } 46375f39d397SChris Mason 4638dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 46395f39d397SChris Mason btrfs_set_item_size(leaf, item, new_size); 46405f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 4641b18c6685SChris Mason 4642e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4643a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4644b18c6685SChris Mason BUG(); 46455f39d397SChris Mason } 4646b18c6685SChris Mason } 4647b18c6685SChris Mason 4648d352ac68SChris Mason /* 46498f69dbd2SStefan Behrens * make the item pointed to by the path bigger, data_size is the added size. 4650d352ac68SChris Mason */ 46512ff7e61eSJeff Mahoney void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path, 46525f39d397SChris Mason u32 data_size) 46536567e837SChris Mason { 46546567e837SChris Mason int slot; 46555f39d397SChris Mason struct extent_buffer *leaf; 46565f39d397SChris Mason struct btrfs_item *item; 46576567e837SChris Mason u32 nritems; 46586567e837SChris Mason unsigned int data_end; 46596567e837SChris Mason unsigned int old_data; 46606567e837SChris Mason unsigned int old_size; 46616567e837SChris Mason int i; 4662cfed81a0SChris Mason struct btrfs_map_token token; 4663cfed81a0SChris Mason 4664cfed81a0SChris Mason btrfs_init_map_token(&token); 46656567e837SChris Mason 46665f39d397SChris Mason leaf = path->nodes[0]; 46676567e837SChris Mason 46685f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 46698f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 46706567e837SChris Mason 4671e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < data_size) { 4672a4f78750SDavid Sterba btrfs_print_leaf(leaf); 46736567e837SChris Mason BUG(); 46745f39d397SChris Mason } 46756567e837SChris Mason slot = path->slots[0]; 46765f39d397SChris Mason old_data = btrfs_item_end_nr(leaf, slot); 46776567e837SChris Mason 46786567e837SChris Mason BUG_ON(slot < 0); 46793326d1b0SChris Mason if (slot >= nritems) { 4680a4f78750SDavid Sterba btrfs_print_leaf(leaf); 46810b246afaSJeff Mahoney btrfs_crit(fs_info, "slot %d too large, nritems %d", 4682d397712bSChris Mason slot, nritems); 4683290342f6SArnd Bergmann BUG(); 46843326d1b0SChris Mason } 46856567e837SChris Mason 46866567e837SChris Mason /* 46876567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 46886567e837SChris Mason */ 46896567e837SChris Mason /* first correct the data pointers */ 46906567e837SChris Mason for (i = slot; i < nritems; i++) { 46915f39d397SChris Mason u32 ioff; 4692dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4693db94535dSChris Mason 4694cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4695cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4696cfed81a0SChris Mason ioff - data_size, &token); 46976567e837SChris Mason } 46985f39d397SChris Mason 46996567e837SChris Mason /* shift the data */ 47003d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 47013d9ec8c4SNikolay Borisov data_end - data_size, BTRFS_LEAF_DATA_OFFSET + 47026567e837SChris Mason data_end, old_data - data_end); 47035f39d397SChris Mason 47046567e837SChris Mason data_end = old_data; 47055f39d397SChris Mason old_size = btrfs_item_size_nr(leaf, slot); 4706dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 47075f39d397SChris Mason btrfs_set_item_size(leaf, item, old_size + data_size); 47085f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 47096567e837SChris Mason 4710e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4711a4f78750SDavid Sterba btrfs_print_leaf(leaf); 47126567e837SChris Mason BUG(); 47135f39d397SChris Mason } 47146567e837SChris Mason } 47156567e837SChris Mason 471674123bd7SChris Mason /* 471744871b1bSChris Mason * this is a helper for btrfs_insert_empty_items, the main goal here is 471844871b1bSChris Mason * to save stack depth by doing the bulk of the work in a function 471944871b1bSChris Mason * that doesn't call btrfs_search_slot 472074123bd7SChris Mason */ 4721afe5fea7STsutomu Itoh void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, 4722310712b2SOmar Sandoval const struct btrfs_key *cpu_key, u32 *data_size, 472344871b1bSChris Mason u32 total_data, u32 total_size, int nr) 4724be0e5c09SChris Mason { 47250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 47265f39d397SChris Mason struct btrfs_item *item; 47279c58309dSChris Mason int i; 47287518a238SChris Mason u32 nritems; 4729be0e5c09SChris Mason unsigned int data_end; 4730e2fa7227SChris Mason struct btrfs_disk_key disk_key; 473144871b1bSChris Mason struct extent_buffer *leaf; 473244871b1bSChris Mason int slot; 4733cfed81a0SChris Mason struct btrfs_map_token token; 4734cfed81a0SChris Mason 473524cdc847SFilipe Manana if (path->slots[0] == 0) { 473624cdc847SFilipe Manana btrfs_cpu_key_to_disk(&disk_key, cpu_key); 4737b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 473824cdc847SFilipe Manana } 473924cdc847SFilipe Manana btrfs_unlock_up_safe(path, 1); 474024cdc847SFilipe Manana 4741cfed81a0SChris Mason btrfs_init_map_token(&token); 4742e2fa7227SChris Mason 47435f39d397SChris Mason leaf = path->nodes[0]; 474444871b1bSChris Mason slot = path->slots[0]; 474574123bd7SChris Mason 47465f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 47478f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 4748eb60ceacSChris Mason 4749e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < total_size) { 4750a4f78750SDavid Sterba btrfs_print_leaf(leaf); 47510b246afaSJeff Mahoney btrfs_crit(fs_info, "not enough freespace need %u have %d", 4752e902baacSDavid Sterba total_size, btrfs_leaf_free_space(leaf)); 4753be0e5c09SChris Mason BUG(); 4754d4dbff95SChris Mason } 47555f39d397SChris Mason 4756be0e5c09SChris Mason if (slot != nritems) { 47575f39d397SChris Mason unsigned int old_data = btrfs_item_end_nr(leaf, slot); 4758be0e5c09SChris Mason 47595f39d397SChris Mason if (old_data < data_end) { 4760a4f78750SDavid Sterba btrfs_print_leaf(leaf); 47610b246afaSJeff Mahoney btrfs_crit(fs_info, "slot %d old_data %d data_end %d", 47625f39d397SChris Mason slot, old_data, data_end); 4763290342f6SArnd Bergmann BUG(); 47645f39d397SChris Mason } 4765be0e5c09SChris Mason /* 4766be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4767be0e5c09SChris Mason */ 4768be0e5c09SChris Mason /* first correct the data pointers */ 47690783fcfcSChris Mason for (i = slot; i < nritems; i++) { 47705f39d397SChris Mason u32 ioff; 4771db94535dSChris Mason 4772dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4773cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4774cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4775cfed81a0SChris Mason ioff - total_data, &token); 47760783fcfcSChris Mason } 4777be0e5c09SChris Mason /* shift the items */ 47789c58309dSChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 47795f39d397SChris Mason btrfs_item_nr_offset(slot), 47800783fcfcSChris Mason (nritems - slot) * sizeof(struct btrfs_item)); 4781be0e5c09SChris Mason 4782be0e5c09SChris Mason /* shift the data */ 47833d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 47843d9ec8c4SNikolay Borisov data_end - total_data, BTRFS_LEAF_DATA_OFFSET + 4785be0e5c09SChris Mason data_end, old_data - data_end); 4786be0e5c09SChris Mason data_end = old_data; 4787be0e5c09SChris Mason } 47885f39d397SChris Mason 478962e2749eSChris Mason /* setup the item for the new data */ 47909c58309dSChris Mason for (i = 0; i < nr; i++) { 47919c58309dSChris Mason btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 47929c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 4793dd3cc16bSRoss Kirk item = btrfs_item_nr(slot + i); 4794cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4795cfed81a0SChris Mason data_end - data_size[i], &token); 47969c58309dSChris Mason data_end -= data_size[i]; 4797cfed81a0SChris Mason btrfs_set_token_item_size(leaf, item, data_size[i], &token); 47989c58309dSChris Mason } 479944871b1bSChris Mason 48009c58309dSChris Mason btrfs_set_header_nritems(leaf, nritems + nr); 4801b9473439SChris Mason btrfs_mark_buffer_dirty(leaf); 4802aa5d6bedSChris Mason 4803e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4804a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4805be0e5c09SChris Mason BUG(); 48065f39d397SChris Mason } 480744871b1bSChris Mason } 480844871b1bSChris Mason 480944871b1bSChris Mason /* 481044871b1bSChris Mason * Given a key and some data, insert items into the tree. 481144871b1bSChris Mason * This does all the path init required, making room in the tree if needed. 481244871b1bSChris Mason */ 481344871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 481444871b1bSChris Mason struct btrfs_root *root, 481544871b1bSChris Mason struct btrfs_path *path, 4816310712b2SOmar Sandoval const struct btrfs_key *cpu_key, u32 *data_size, 481744871b1bSChris Mason int nr) 481844871b1bSChris Mason { 481944871b1bSChris Mason int ret = 0; 482044871b1bSChris Mason int slot; 482144871b1bSChris Mason int i; 482244871b1bSChris Mason u32 total_size = 0; 482344871b1bSChris Mason u32 total_data = 0; 482444871b1bSChris Mason 482544871b1bSChris Mason for (i = 0; i < nr; i++) 482644871b1bSChris Mason total_data += data_size[i]; 482744871b1bSChris Mason 482844871b1bSChris Mason total_size = total_data + (nr * sizeof(struct btrfs_item)); 482944871b1bSChris Mason ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 483044871b1bSChris Mason if (ret == 0) 483144871b1bSChris Mason return -EEXIST; 483244871b1bSChris Mason if (ret < 0) 4833143bede5SJeff Mahoney return ret; 483444871b1bSChris Mason 483544871b1bSChris Mason slot = path->slots[0]; 483644871b1bSChris Mason BUG_ON(slot < 0); 483744871b1bSChris Mason 4838afe5fea7STsutomu Itoh setup_items_for_insert(root, path, cpu_key, data_size, 483944871b1bSChris Mason total_data, total_size, nr); 4840143bede5SJeff Mahoney return 0; 484162e2749eSChris Mason } 484262e2749eSChris Mason 484362e2749eSChris Mason /* 484462e2749eSChris Mason * Given a key and some data, insert an item into the tree. 484562e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 484662e2749eSChris Mason */ 4847310712b2SOmar Sandoval int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, 4848310712b2SOmar Sandoval const struct btrfs_key *cpu_key, void *data, 4849310712b2SOmar Sandoval u32 data_size) 485062e2749eSChris Mason { 485162e2749eSChris Mason int ret = 0; 48522c90e5d6SChris Mason struct btrfs_path *path; 48535f39d397SChris Mason struct extent_buffer *leaf; 48545f39d397SChris Mason unsigned long ptr; 485562e2749eSChris Mason 48562c90e5d6SChris Mason path = btrfs_alloc_path(); 4857db5b493aSTsutomu Itoh if (!path) 4858db5b493aSTsutomu Itoh return -ENOMEM; 48592c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 486062e2749eSChris Mason if (!ret) { 48615f39d397SChris Mason leaf = path->nodes[0]; 48625f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 48635f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 48645f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 486562e2749eSChris Mason } 48662c90e5d6SChris Mason btrfs_free_path(path); 4867aa5d6bedSChris Mason return ret; 4868be0e5c09SChris Mason } 4869be0e5c09SChris Mason 487074123bd7SChris Mason /* 48715de08d7dSChris Mason * delete the pointer from a given node. 487274123bd7SChris Mason * 4873d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 4874d352ac68SChris Mason * empty a node. 487574123bd7SChris Mason */ 4876afe5fea7STsutomu Itoh static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 4877afe5fea7STsutomu Itoh int level, int slot) 4878be0e5c09SChris Mason { 48795f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 48807518a238SChris Mason u32 nritems; 4881f3ea38daSJan Schmidt int ret; 4882be0e5c09SChris Mason 48835f39d397SChris Mason nritems = btrfs_header_nritems(parent); 4884be0e5c09SChris Mason if (slot != nritems - 1) { 4885bf1d3425SDavid Sterba if (level) { 4886bf1d3425SDavid Sterba ret = tree_mod_log_insert_move(parent, slot, slot + 1, 4887a446a979SDavid Sterba nritems - slot - 1); 4888bf1d3425SDavid Sterba BUG_ON(ret < 0); 4889bf1d3425SDavid Sterba } 48905f39d397SChris Mason memmove_extent_buffer(parent, 48915f39d397SChris Mason btrfs_node_key_ptr_offset(slot), 48925f39d397SChris Mason btrfs_node_key_ptr_offset(slot + 1), 4893d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 4894d6025579SChris Mason (nritems - slot - 1)); 489557ba86c0SChris Mason } else if (level) { 4896e09c2efeSDavid Sterba ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE, 4897e09c2efeSDavid Sterba GFP_NOFS); 489857ba86c0SChris Mason BUG_ON(ret < 0); 4899be0e5c09SChris Mason } 4900f3ea38daSJan Schmidt 49017518a238SChris Mason nritems--; 49025f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 49037518a238SChris Mason if (nritems == 0 && parent == root->node) { 49045f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 4905eb60ceacSChris Mason /* just turn the root into a leaf and break */ 49065f39d397SChris Mason btrfs_set_header_level(root->node, 0); 4907bb803951SChris Mason } else if (slot == 0) { 49085f39d397SChris Mason struct btrfs_disk_key disk_key; 49095f39d397SChris Mason 49105f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 4911b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, level + 1); 4912be0e5c09SChris Mason } 4913d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 4914be0e5c09SChris Mason } 4915be0e5c09SChris Mason 491674123bd7SChris Mason /* 4917323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 49185d4f98a2SYan Zheng * path->nodes[1]. 4919323ac95bSChris Mason * 4920323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 4921323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 4922323ac95bSChris Mason * 4923323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 4924323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 4925323ac95bSChris Mason */ 4926143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, 4927323ac95bSChris Mason struct btrfs_root *root, 49285d4f98a2SYan Zheng struct btrfs_path *path, 49295d4f98a2SYan Zheng struct extent_buffer *leaf) 4930323ac95bSChris Mason { 49315d4f98a2SYan Zheng WARN_ON(btrfs_header_generation(leaf) != trans->transid); 4932afe5fea7STsutomu Itoh del_ptr(root, path, 1, path->slots[1]); 4933323ac95bSChris Mason 49344d081c41SChris Mason /* 49354d081c41SChris Mason * btrfs_free_extent is expensive, we want to make sure we 49364d081c41SChris Mason * aren't holding any locks when we call it 49374d081c41SChris Mason */ 49384d081c41SChris Mason btrfs_unlock_up_safe(path, 0); 49394d081c41SChris Mason 4940f0486c68SYan, Zheng root_sub_used(root, leaf->len); 4941f0486c68SYan, Zheng 49423083ee2eSJosef Bacik extent_buffer_get(leaf); 49435581a51aSJan Schmidt btrfs_free_tree_block(trans, root, leaf, 0, 1); 49443083ee2eSJosef Bacik free_extent_buffer_stale(leaf); 4945323ac95bSChris Mason } 4946323ac95bSChris Mason /* 494774123bd7SChris Mason * delete the item at the leaf level in path. If that empties 494874123bd7SChris Mason * the leaf, remove it from the tree 494974123bd7SChris Mason */ 495085e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 495185e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 4952be0e5c09SChris Mason { 49530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 49545f39d397SChris Mason struct extent_buffer *leaf; 49555f39d397SChris Mason struct btrfs_item *item; 4956ce0eac2aSAlexandru Moise u32 last_off; 4957ce0eac2aSAlexandru Moise u32 dsize = 0; 4958aa5d6bedSChris Mason int ret = 0; 4959aa5d6bedSChris Mason int wret; 496085e21bacSChris Mason int i; 49617518a238SChris Mason u32 nritems; 4962cfed81a0SChris Mason struct btrfs_map_token token; 4963cfed81a0SChris Mason 4964cfed81a0SChris Mason btrfs_init_map_token(&token); 4965be0e5c09SChris Mason 49665f39d397SChris Mason leaf = path->nodes[0]; 496785e21bacSChris Mason last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 496885e21bacSChris Mason 496985e21bacSChris Mason for (i = 0; i < nr; i++) 497085e21bacSChris Mason dsize += btrfs_item_size_nr(leaf, slot + i); 497185e21bacSChris Mason 49725f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 4973be0e5c09SChris Mason 497485e21bacSChris Mason if (slot + nr != nritems) { 49758f881e8cSDavid Sterba int data_end = leaf_data_end(leaf); 49765f39d397SChris Mason 49773d9ec8c4SNikolay Borisov memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + 4978d6025579SChris Mason data_end + dsize, 49793d9ec8c4SNikolay Borisov BTRFS_LEAF_DATA_OFFSET + data_end, 498085e21bacSChris Mason last_off - data_end); 49815f39d397SChris Mason 498285e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 49835f39d397SChris Mason u32 ioff; 4984db94535dSChris Mason 4985dd3cc16bSRoss Kirk item = btrfs_item_nr(i); 4986cfed81a0SChris Mason ioff = btrfs_token_item_offset(leaf, item, &token); 4987cfed81a0SChris Mason btrfs_set_token_item_offset(leaf, item, 4988cfed81a0SChris Mason ioff + dsize, &token); 49890783fcfcSChris Mason } 4990db94535dSChris Mason 49915f39d397SChris Mason memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 499285e21bacSChris Mason btrfs_item_nr_offset(slot + nr), 49930783fcfcSChris Mason sizeof(struct btrfs_item) * 499485e21bacSChris Mason (nritems - slot - nr)); 4995be0e5c09SChris Mason } 499685e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 499785e21bacSChris Mason nritems -= nr; 49985f39d397SChris Mason 499974123bd7SChris Mason /* delete the leaf if we've emptied it */ 50007518a238SChris Mason if (nritems == 0) { 50015f39d397SChris Mason if (leaf == root->node) { 50025f39d397SChris Mason btrfs_set_header_level(leaf, 0); 50039a8dd150SChris Mason } else { 5004f0486c68SYan, Zheng btrfs_set_path_blocking(path); 50056a884d7dSDavid Sterba btrfs_clean_tree_block(leaf); 5006143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 50079a8dd150SChris Mason } 5008be0e5c09SChris Mason } else { 50097518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 5010aa5d6bedSChris Mason if (slot == 0) { 50115f39d397SChris Mason struct btrfs_disk_key disk_key; 50125f39d397SChris Mason 50135f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 5014b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 5015aa5d6bedSChris Mason } 5016aa5d6bedSChris Mason 501774123bd7SChris Mason /* delete the leaf if it is mostly empty */ 50180b246afaSJeff Mahoney if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) { 5019be0e5c09SChris Mason /* push_leaf_left fixes the path. 5020be0e5c09SChris Mason * make sure the path still points to our leaf 5021be0e5c09SChris Mason * for possible call to del_ptr below 5022be0e5c09SChris Mason */ 50234920c9acSChris Mason slot = path->slots[1]; 50245f39d397SChris Mason extent_buffer_get(leaf); 50255f39d397SChris Mason 5026b9473439SChris Mason btrfs_set_path_blocking(path); 502799d8f83cSChris Mason wret = push_leaf_left(trans, root, path, 1, 1, 502899d8f83cSChris Mason 1, (u32)-1); 502954aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 5030aa5d6bedSChris Mason ret = wret; 50315f39d397SChris Mason 50325f39d397SChris Mason if (path->nodes[0] == leaf && 50335f39d397SChris Mason btrfs_header_nritems(leaf)) { 503499d8f83cSChris Mason wret = push_leaf_right(trans, root, path, 1, 503599d8f83cSChris Mason 1, 1, 0); 503654aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 5037aa5d6bedSChris Mason ret = wret; 5038aa5d6bedSChris Mason } 50395f39d397SChris Mason 50405f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 5041323ac95bSChris Mason path->slots[1] = slot; 5042143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 50435f39d397SChris Mason free_extent_buffer(leaf); 5044143bede5SJeff Mahoney ret = 0; 50455de08d7dSChris Mason } else { 5046925baeddSChris Mason /* if we're still in the path, make sure 5047925baeddSChris Mason * we're dirty. Otherwise, one of the 5048925baeddSChris Mason * push_leaf functions must have already 5049925baeddSChris Mason * dirtied this buffer 5050925baeddSChris Mason */ 5051925baeddSChris Mason if (path->nodes[0] == leaf) 50525f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 50535f39d397SChris Mason free_extent_buffer(leaf); 5054be0e5c09SChris Mason } 5055d5719762SChris Mason } else { 50565f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 5057be0e5c09SChris Mason } 50589a8dd150SChris Mason } 5059aa5d6bedSChris Mason return ret; 50609a8dd150SChris Mason } 50619a8dd150SChris Mason 506297571fd0SChris Mason /* 5063925baeddSChris Mason * search the tree again to find a leaf with lesser keys 50647bb86316SChris Mason * returns 0 if it found something or 1 if there are no lesser leaves. 50657bb86316SChris Mason * returns < 0 on io errors. 5066d352ac68SChris Mason * 5067d352ac68SChris Mason * This may release the path, and so you may lose any locks held at the 5068d352ac68SChris Mason * time you call it. 50697bb86316SChris Mason */ 507016e7549fSJosef Bacik int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 50717bb86316SChris Mason { 5072925baeddSChris Mason struct btrfs_key key; 5073925baeddSChris Mason struct btrfs_disk_key found_key; 5074925baeddSChris Mason int ret; 50757bb86316SChris Mason 5076925baeddSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 5077925baeddSChris Mason 5078e8b0d724SFilipe David Borba Manana if (key.offset > 0) { 5079925baeddSChris Mason key.offset--; 5080e8b0d724SFilipe David Borba Manana } else if (key.type > 0) { 5081925baeddSChris Mason key.type--; 5082e8b0d724SFilipe David Borba Manana key.offset = (u64)-1; 5083e8b0d724SFilipe David Borba Manana } else if (key.objectid > 0) { 5084925baeddSChris Mason key.objectid--; 5085e8b0d724SFilipe David Borba Manana key.type = (u8)-1; 5086e8b0d724SFilipe David Borba Manana key.offset = (u64)-1; 5087e8b0d724SFilipe David Borba Manana } else { 50887bb86316SChris Mason return 1; 5089e8b0d724SFilipe David Borba Manana } 50907bb86316SChris Mason 5091b3b4aa74SDavid Sterba btrfs_release_path(path); 5092925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5093925baeddSChris Mason if (ret < 0) 5094925baeddSChris Mason return ret; 5095925baeddSChris Mason btrfs_item_key(path->nodes[0], &found_key, 0); 5096925baeddSChris Mason ret = comp_keys(&found_key, &key); 5097337c6f68SFilipe Manana /* 5098337c6f68SFilipe Manana * We might have had an item with the previous key in the tree right 5099337c6f68SFilipe Manana * before we released our path. And after we released our path, that 5100337c6f68SFilipe Manana * item might have been pushed to the first slot (0) of the leaf we 5101337c6f68SFilipe Manana * were holding due to a tree balance. Alternatively, an item with the 5102337c6f68SFilipe Manana * previous key can exist as the only element of a leaf (big fat item). 5103337c6f68SFilipe Manana * Therefore account for these 2 cases, so that our callers (like 5104337c6f68SFilipe Manana * btrfs_previous_item) don't miss an existing item with a key matching 5105337c6f68SFilipe Manana * the previous key we computed above. 5106337c6f68SFilipe Manana */ 5107337c6f68SFilipe Manana if (ret <= 0) 51087bb86316SChris Mason return 0; 5109925baeddSChris Mason return 1; 51107bb86316SChris Mason } 51117bb86316SChris Mason 51123f157a2fSChris Mason /* 51133f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 5114de78b51aSEric Sandeen * for nodes or leaves that are have a minimum transaction id. 5115de78b51aSEric Sandeen * This is used by the btree defrag code, and tree logging 51163f157a2fSChris Mason * 51173f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 51183f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 51193f157a2fSChris Mason * key and get a writable path. 51203f157a2fSChris Mason * 51213f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 51223f157a2fSChris Mason * of the tree. 51233f157a2fSChris Mason * 5124d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 5125d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 5126d352ac68SChris Mason * skipped over (without reading them). 5127d352ac68SChris Mason * 51283f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 51293f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 51303f157a2fSChris Mason */ 51313f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 5132de78b51aSEric Sandeen struct btrfs_path *path, 51333f157a2fSChris Mason u64 min_trans) 51343f157a2fSChris Mason { 51353f157a2fSChris Mason struct extent_buffer *cur; 51363f157a2fSChris Mason struct btrfs_key found_key; 51373f157a2fSChris Mason int slot; 51389652480bSYan int sret; 51393f157a2fSChris Mason u32 nritems; 51403f157a2fSChris Mason int level; 51413f157a2fSChris Mason int ret = 1; 5142f98de9b9SFilipe Manana int keep_locks = path->keep_locks; 51433f157a2fSChris Mason 5144f98de9b9SFilipe Manana path->keep_locks = 1; 51453f157a2fSChris Mason again: 5146bd681513SChris Mason cur = btrfs_read_lock_root_node(root); 51473f157a2fSChris Mason level = btrfs_header_level(cur); 5148e02119d5SChris Mason WARN_ON(path->nodes[level]); 51493f157a2fSChris Mason path->nodes[level] = cur; 5150bd681513SChris Mason path->locks[level] = BTRFS_READ_LOCK; 51513f157a2fSChris Mason 51523f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 51533f157a2fSChris Mason ret = 1; 51543f157a2fSChris Mason goto out; 51553f157a2fSChris Mason } 51563f157a2fSChris Mason while (1) { 51573f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 51583f157a2fSChris Mason level = btrfs_header_level(cur); 5159a74b35ecSNikolay Borisov sret = btrfs_bin_search(cur, min_key, level, &slot); 5160cbca7d59SFilipe Manana if (sret < 0) { 5161cbca7d59SFilipe Manana ret = sret; 5162cbca7d59SFilipe Manana goto out; 5163cbca7d59SFilipe Manana } 51643f157a2fSChris Mason 5165323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 5166323ac95bSChris Mason if (level == path->lowest_level) { 5167e02119d5SChris Mason if (slot >= nritems) 5168e02119d5SChris Mason goto find_next_key; 51693f157a2fSChris Mason ret = 0; 51703f157a2fSChris Mason path->slots[level] = slot; 51713f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 51723f157a2fSChris Mason goto out; 51733f157a2fSChris Mason } 51749652480bSYan if (sret && slot > 0) 51759652480bSYan slot--; 51763f157a2fSChris Mason /* 5177de78b51aSEric Sandeen * check this node pointer against the min_trans parameters. 5178de78b51aSEric Sandeen * If it is too old, old, skip to the next one. 51793f157a2fSChris Mason */ 51803f157a2fSChris Mason while (slot < nritems) { 51813f157a2fSChris Mason u64 gen; 5182e02119d5SChris Mason 51833f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 51843f157a2fSChris Mason if (gen < min_trans) { 51853f157a2fSChris Mason slot++; 51863f157a2fSChris Mason continue; 51873f157a2fSChris Mason } 51883f157a2fSChris Mason break; 51893f157a2fSChris Mason } 5190e02119d5SChris Mason find_next_key: 51913f157a2fSChris Mason /* 51923f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 51933f157a2fSChris Mason * and find another one 51943f157a2fSChris Mason */ 51953f157a2fSChris Mason if (slot >= nritems) { 5196e02119d5SChris Mason path->slots[level] = slot; 5197b4ce94deSChris Mason btrfs_set_path_blocking(path); 5198e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 5199de78b51aSEric Sandeen min_trans); 5200e02119d5SChris Mason if (sret == 0) { 5201b3b4aa74SDavid Sterba btrfs_release_path(path); 52023f157a2fSChris Mason goto again; 52033f157a2fSChris Mason } else { 52043f157a2fSChris Mason goto out; 52053f157a2fSChris Mason } 52063f157a2fSChris Mason } 52073f157a2fSChris Mason /* save our key for returning back */ 52083f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 52093f157a2fSChris Mason path->slots[level] = slot; 52103f157a2fSChris Mason if (level == path->lowest_level) { 52113f157a2fSChris Mason ret = 0; 52123f157a2fSChris Mason goto out; 52133f157a2fSChris Mason } 5214b4ce94deSChris Mason btrfs_set_path_blocking(path); 5215d0d20b0fSDavid Sterba cur = read_node_slot(cur, slot); 5216fb770ae4SLiu Bo if (IS_ERR(cur)) { 5217fb770ae4SLiu Bo ret = PTR_ERR(cur); 5218fb770ae4SLiu Bo goto out; 5219fb770ae4SLiu Bo } 52203f157a2fSChris Mason 5221bd681513SChris Mason btrfs_tree_read_lock(cur); 5222b4ce94deSChris Mason 5223bd681513SChris Mason path->locks[level - 1] = BTRFS_READ_LOCK; 52243f157a2fSChris Mason path->nodes[level - 1] = cur; 5225f7c79f30SChris Mason unlock_up(path, level, 1, 0, NULL); 52263f157a2fSChris Mason } 52273f157a2fSChris Mason out: 5228f98de9b9SFilipe Manana path->keep_locks = keep_locks; 5229f98de9b9SFilipe Manana if (ret == 0) { 5230f98de9b9SFilipe Manana btrfs_unlock_up_safe(path, path->lowest_level + 1); 5231b4ce94deSChris Mason btrfs_set_path_blocking(path); 5232f98de9b9SFilipe Manana memcpy(min_key, &found_key, sizeof(found_key)); 5233f98de9b9SFilipe Manana } 52343f157a2fSChris Mason return ret; 52353f157a2fSChris Mason } 52363f157a2fSChris Mason 52372ff7e61eSJeff Mahoney static int tree_move_down(struct btrfs_fs_info *fs_info, 52387069830aSAlexander Block struct btrfs_path *path, 5239ab6a43e1SDavid Sterba int *level) 52407069830aSAlexander Block { 5241fb770ae4SLiu Bo struct extent_buffer *eb; 5242fb770ae4SLiu Bo 524374dd17fbSChris Mason BUG_ON(*level == 0); 5244d0d20b0fSDavid Sterba eb = read_node_slot(path->nodes[*level], path->slots[*level]); 5245fb770ae4SLiu Bo if (IS_ERR(eb)) 5246fb770ae4SLiu Bo return PTR_ERR(eb); 5247fb770ae4SLiu Bo 5248fb770ae4SLiu Bo path->nodes[*level - 1] = eb; 52497069830aSAlexander Block path->slots[*level - 1] = 0; 52507069830aSAlexander Block (*level)--; 5251fb770ae4SLiu Bo return 0; 52527069830aSAlexander Block } 52537069830aSAlexander Block 5254f1e30261SDavid Sterba static int tree_move_next_or_upnext(struct btrfs_path *path, 52557069830aSAlexander Block int *level, int root_level) 52567069830aSAlexander Block { 52577069830aSAlexander Block int ret = 0; 52587069830aSAlexander Block int nritems; 52597069830aSAlexander Block nritems = btrfs_header_nritems(path->nodes[*level]); 52607069830aSAlexander Block 52617069830aSAlexander Block path->slots[*level]++; 52627069830aSAlexander Block 526374dd17fbSChris Mason while (path->slots[*level] >= nritems) { 52647069830aSAlexander Block if (*level == root_level) 52657069830aSAlexander Block return -1; 52667069830aSAlexander Block 52677069830aSAlexander Block /* move upnext */ 52687069830aSAlexander Block path->slots[*level] = 0; 52697069830aSAlexander Block free_extent_buffer(path->nodes[*level]); 52707069830aSAlexander Block path->nodes[*level] = NULL; 52717069830aSAlexander Block (*level)++; 52727069830aSAlexander Block path->slots[*level]++; 52737069830aSAlexander Block 52747069830aSAlexander Block nritems = btrfs_header_nritems(path->nodes[*level]); 52757069830aSAlexander Block ret = 1; 52767069830aSAlexander Block } 52777069830aSAlexander Block return ret; 52787069830aSAlexander Block } 52797069830aSAlexander Block 52807069830aSAlexander Block /* 52817069830aSAlexander Block * Returns 1 if it had to move up and next. 0 is returned if it moved only next 52827069830aSAlexander Block * or down. 52837069830aSAlexander Block */ 52842ff7e61eSJeff Mahoney static int tree_advance(struct btrfs_fs_info *fs_info, 52857069830aSAlexander Block struct btrfs_path *path, 52867069830aSAlexander Block int *level, int root_level, 52877069830aSAlexander Block int allow_down, 52887069830aSAlexander Block struct btrfs_key *key) 52897069830aSAlexander Block { 52907069830aSAlexander Block int ret; 52917069830aSAlexander Block 52927069830aSAlexander Block if (*level == 0 || !allow_down) { 5293f1e30261SDavid Sterba ret = tree_move_next_or_upnext(path, level, root_level); 52947069830aSAlexander Block } else { 5295ab6a43e1SDavid Sterba ret = tree_move_down(fs_info, path, level); 52967069830aSAlexander Block } 52977069830aSAlexander Block if (ret >= 0) { 52987069830aSAlexander Block if (*level == 0) 52997069830aSAlexander Block btrfs_item_key_to_cpu(path->nodes[*level], key, 53007069830aSAlexander Block path->slots[*level]); 53017069830aSAlexander Block else 53027069830aSAlexander Block btrfs_node_key_to_cpu(path->nodes[*level], key, 53037069830aSAlexander Block path->slots[*level]); 53047069830aSAlexander Block } 53057069830aSAlexander Block return ret; 53067069830aSAlexander Block } 53077069830aSAlexander Block 53082ff7e61eSJeff Mahoney static int tree_compare_item(struct btrfs_path *left_path, 53097069830aSAlexander Block struct btrfs_path *right_path, 53107069830aSAlexander Block char *tmp_buf) 53117069830aSAlexander Block { 53127069830aSAlexander Block int cmp; 53137069830aSAlexander Block int len1, len2; 53147069830aSAlexander Block unsigned long off1, off2; 53157069830aSAlexander Block 53167069830aSAlexander Block len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); 53177069830aSAlexander Block len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); 53187069830aSAlexander Block if (len1 != len2) 53197069830aSAlexander Block return 1; 53207069830aSAlexander Block 53217069830aSAlexander Block off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); 53227069830aSAlexander Block off2 = btrfs_item_ptr_offset(right_path->nodes[0], 53237069830aSAlexander Block right_path->slots[0]); 53247069830aSAlexander Block 53257069830aSAlexander Block read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); 53267069830aSAlexander Block 53277069830aSAlexander Block cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); 53287069830aSAlexander Block if (cmp) 53297069830aSAlexander Block return 1; 53307069830aSAlexander Block return 0; 53317069830aSAlexander Block } 53327069830aSAlexander Block 53337069830aSAlexander Block #define ADVANCE 1 53347069830aSAlexander Block #define ADVANCE_ONLY_NEXT -1 53357069830aSAlexander Block 53367069830aSAlexander Block /* 53377069830aSAlexander Block * This function compares two trees and calls the provided callback for 53387069830aSAlexander Block * every changed/new/deleted item it finds. 53397069830aSAlexander Block * If shared tree blocks are encountered, whole subtrees are skipped, making 53407069830aSAlexander Block * the compare pretty fast on snapshotted subvolumes. 53417069830aSAlexander Block * 53427069830aSAlexander Block * This currently works on commit roots only. As commit roots are read only, 53437069830aSAlexander Block * we don't do any locking. The commit roots are protected with transactions. 53447069830aSAlexander Block * Transactions are ended and rejoined when a commit is tried in between. 53457069830aSAlexander Block * 53467069830aSAlexander Block * This function checks for modifications done to the trees while comparing. 53477069830aSAlexander Block * If it detects a change, it aborts immediately. 53487069830aSAlexander Block */ 53497069830aSAlexander Block int btrfs_compare_trees(struct btrfs_root *left_root, 53507069830aSAlexander Block struct btrfs_root *right_root, 53517069830aSAlexander Block btrfs_changed_cb_t changed_cb, void *ctx) 53527069830aSAlexander Block { 53530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = left_root->fs_info; 53547069830aSAlexander Block int ret; 53557069830aSAlexander Block int cmp; 53567069830aSAlexander Block struct btrfs_path *left_path = NULL; 53577069830aSAlexander Block struct btrfs_path *right_path = NULL; 53587069830aSAlexander Block struct btrfs_key left_key; 53597069830aSAlexander Block struct btrfs_key right_key; 53607069830aSAlexander Block char *tmp_buf = NULL; 53617069830aSAlexander Block int left_root_level; 53627069830aSAlexander Block int right_root_level; 53637069830aSAlexander Block int left_level; 53647069830aSAlexander Block int right_level; 53657069830aSAlexander Block int left_end_reached; 53667069830aSAlexander Block int right_end_reached; 53677069830aSAlexander Block int advance_left; 53687069830aSAlexander Block int advance_right; 53697069830aSAlexander Block u64 left_blockptr; 53707069830aSAlexander Block u64 right_blockptr; 53716baa4293SFilipe Manana u64 left_gen; 53726baa4293SFilipe Manana u64 right_gen; 53737069830aSAlexander Block 53747069830aSAlexander Block left_path = btrfs_alloc_path(); 53757069830aSAlexander Block if (!left_path) { 53767069830aSAlexander Block ret = -ENOMEM; 53777069830aSAlexander Block goto out; 53787069830aSAlexander Block } 53797069830aSAlexander Block right_path = btrfs_alloc_path(); 53807069830aSAlexander Block if (!right_path) { 53817069830aSAlexander Block ret = -ENOMEM; 53827069830aSAlexander Block goto out; 53837069830aSAlexander Block } 53847069830aSAlexander Block 5385752ade68SMichal Hocko tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); 53867069830aSAlexander Block if (!tmp_buf) { 53877069830aSAlexander Block ret = -ENOMEM; 53887069830aSAlexander Block goto out; 53897069830aSAlexander Block } 53907069830aSAlexander Block 53917069830aSAlexander Block left_path->search_commit_root = 1; 53927069830aSAlexander Block left_path->skip_locking = 1; 53937069830aSAlexander Block right_path->search_commit_root = 1; 53947069830aSAlexander Block right_path->skip_locking = 1; 53957069830aSAlexander Block 53967069830aSAlexander Block /* 53977069830aSAlexander Block * Strategy: Go to the first items of both trees. Then do 53987069830aSAlexander Block * 53997069830aSAlexander Block * If both trees are at level 0 54007069830aSAlexander Block * Compare keys of current items 54017069830aSAlexander Block * If left < right treat left item as new, advance left tree 54027069830aSAlexander Block * and repeat 54037069830aSAlexander Block * If left > right treat right item as deleted, advance right tree 54047069830aSAlexander Block * and repeat 54057069830aSAlexander Block * If left == right do deep compare of items, treat as changed if 54067069830aSAlexander Block * needed, advance both trees and repeat 54077069830aSAlexander Block * If both trees are at the same level but not at level 0 54087069830aSAlexander Block * Compare keys of current nodes/leafs 54097069830aSAlexander Block * If left < right advance left tree and repeat 54107069830aSAlexander Block * If left > right advance right tree and repeat 54117069830aSAlexander Block * If left == right compare blockptrs of the next nodes/leafs 54127069830aSAlexander Block * If they match advance both trees but stay at the same level 54137069830aSAlexander Block * and repeat 54147069830aSAlexander Block * If they don't match advance both trees while allowing to go 54157069830aSAlexander Block * deeper and repeat 54167069830aSAlexander Block * If tree levels are different 54177069830aSAlexander Block * Advance the tree that needs it and repeat 54187069830aSAlexander Block * 54197069830aSAlexander Block * Advancing a tree means: 54207069830aSAlexander Block * If we are at level 0, try to go to the next slot. If that's not 54217069830aSAlexander Block * possible, go one level up and repeat. Stop when we found a level 54227069830aSAlexander Block * where we could go to the next slot. We may at this point be on a 54237069830aSAlexander Block * node or a leaf. 54247069830aSAlexander Block * 54257069830aSAlexander Block * If we are not at level 0 and not on shared tree blocks, go one 54267069830aSAlexander Block * level deeper. 54277069830aSAlexander Block * 54287069830aSAlexander Block * If we are not at level 0 and on shared tree blocks, go one slot to 54297069830aSAlexander Block * the right if possible or go up and right. 54307069830aSAlexander Block */ 54317069830aSAlexander Block 54320b246afaSJeff Mahoney down_read(&fs_info->commit_root_sem); 54337069830aSAlexander Block left_level = btrfs_header_level(left_root->commit_root); 54347069830aSAlexander Block left_root_level = left_level; 54356f2f0b39SRobbie Ko left_path->nodes[left_level] = 54366f2f0b39SRobbie Ko btrfs_clone_extent_buffer(left_root->commit_root); 54376f2f0b39SRobbie Ko if (!left_path->nodes[left_level]) { 54386f2f0b39SRobbie Ko up_read(&fs_info->commit_root_sem); 54396f2f0b39SRobbie Ko ret = -ENOMEM; 54406f2f0b39SRobbie Ko goto out; 54416f2f0b39SRobbie Ko } 54427069830aSAlexander Block 54437069830aSAlexander Block right_level = btrfs_header_level(right_root->commit_root); 54447069830aSAlexander Block right_root_level = right_level; 54456f2f0b39SRobbie Ko right_path->nodes[right_level] = 54466f2f0b39SRobbie Ko btrfs_clone_extent_buffer(right_root->commit_root); 54476f2f0b39SRobbie Ko if (!right_path->nodes[right_level]) { 54486f2f0b39SRobbie Ko up_read(&fs_info->commit_root_sem); 54496f2f0b39SRobbie Ko ret = -ENOMEM; 54506f2f0b39SRobbie Ko goto out; 54516f2f0b39SRobbie Ko } 54520b246afaSJeff Mahoney up_read(&fs_info->commit_root_sem); 54537069830aSAlexander Block 54547069830aSAlexander Block if (left_level == 0) 54557069830aSAlexander Block btrfs_item_key_to_cpu(left_path->nodes[left_level], 54567069830aSAlexander Block &left_key, left_path->slots[left_level]); 54577069830aSAlexander Block else 54587069830aSAlexander Block btrfs_node_key_to_cpu(left_path->nodes[left_level], 54597069830aSAlexander Block &left_key, left_path->slots[left_level]); 54607069830aSAlexander Block if (right_level == 0) 54617069830aSAlexander Block btrfs_item_key_to_cpu(right_path->nodes[right_level], 54627069830aSAlexander Block &right_key, right_path->slots[right_level]); 54637069830aSAlexander Block else 54647069830aSAlexander Block btrfs_node_key_to_cpu(right_path->nodes[right_level], 54657069830aSAlexander Block &right_key, right_path->slots[right_level]); 54667069830aSAlexander Block 54677069830aSAlexander Block left_end_reached = right_end_reached = 0; 54687069830aSAlexander Block advance_left = advance_right = 0; 54697069830aSAlexander Block 54707069830aSAlexander Block while (1) { 54717069830aSAlexander Block if (advance_left && !left_end_reached) { 54722ff7e61eSJeff Mahoney ret = tree_advance(fs_info, left_path, &left_level, 54737069830aSAlexander Block left_root_level, 54747069830aSAlexander Block advance_left != ADVANCE_ONLY_NEXT, 54757069830aSAlexander Block &left_key); 5476fb770ae4SLiu Bo if (ret == -1) 54777069830aSAlexander Block left_end_reached = ADVANCE; 5478fb770ae4SLiu Bo else if (ret < 0) 5479fb770ae4SLiu Bo goto out; 54807069830aSAlexander Block advance_left = 0; 54817069830aSAlexander Block } 54827069830aSAlexander Block if (advance_right && !right_end_reached) { 54832ff7e61eSJeff Mahoney ret = tree_advance(fs_info, right_path, &right_level, 54847069830aSAlexander Block right_root_level, 54857069830aSAlexander Block advance_right != ADVANCE_ONLY_NEXT, 54867069830aSAlexander Block &right_key); 5487fb770ae4SLiu Bo if (ret == -1) 54887069830aSAlexander Block right_end_reached = ADVANCE; 5489fb770ae4SLiu Bo else if (ret < 0) 5490fb770ae4SLiu Bo goto out; 54917069830aSAlexander Block advance_right = 0; 54927069830aSAlexander Block } 54937069830aSAlexander Block 54947069830aSAlexander Block if (left_end_reached && right_end_reached) { 54957069830aSAlexander Block ret = 0; 54967069830aSAlexander Block goto out; 54977069830aSAlexander Block } else if (left_end_reached) { 54987069830aSAlexander Block if (right_level == 0) { 5499ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55007069830aSAlexander Block &right_key, 55017069830aSAlexander Block BTRFS_COMPARE_TREE_DELETED, 55027069830aSAlexander Block ctx); 55037069830aSAlexander Block if (ret < 0) 55047069830aSAlexander Block goto out; 55057069830aSAlexander Block } 55067069830aSAlexander Block advance_right = ADVANCE; 55077069830aSAlexander Block continue; 55087069830aSAlexander Block } else if (right_end_reached) { 55097069830aSAlexander Block if (left_level == 0) { 5510ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55117069830aSAlexander Block &left_key, 55127069830aSAlexander Block BTRFS_COMPARE_TREE_NEW, 55137069830aSAlexander Block ctx); 55147069830aSAlexander Block if (ret < 0) 55157069830aSAlexander Block goto out; 55167069830aSAlexander Block } 55177069830aSAlexander Block advance_left = ADVANCE; 55187069830aSAlexander Block continue; 55197069830aSAlexander Block } 55207069830aSAlexander Block 55217069830aSAlexander Block if (left_level == 0 && right_level == 0) { 55227069830aSAlexander Block cmp = btrfs_comp_cpu_keys(&left_key, &right_key); 55237069830aSAlexander Block if (cmp < 0) { 5524ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55257069830aSAlexander Block &left_key, 55267069830aSAlexander Block BTRFS_COMPARE_TREE_NEW, 55277069830aSAlexander Block ctx); 55287069830aSAlexander Block if (ret < 0) 55297069830aSAlexander Block goto out; 55307069830aSAlexander Block advance_left = ADVANCE; 55317069830aSAlexander Block } else if (cmp > 0) { 5532ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 55337069830aSAlexander Block &right_key, 55347069830aSAlexander Block BTRFS_COMPARE_TREE_DELETED, 55357069830aSAlexander Block ctx); 55367069830aSAlexander Block if (ret < 0) 55377069830aSAlexander Block goto out; 55387069830aSAlexander Block advance_right = ADVANCE; 55397069830aSAlexander Block } else { 5540b99d9a6aSFabian Frederick enum btrfs_compare_tree_result result; 5541ba5e8f2eSJosef Bacik 554274dd17fbSChris Mason WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); 55432ff7e61eSJeff Mahoney ret = tree_compare_item(left_path, right_path, 55442ff7e61eSJeff Mahoney tmp_buf); 5545ba5e8f2eSJosef Bacik if (ret) 5546b99d9a6aSFabian Frederick result = BTRFS_COMPARE_TREE_CHANGED; 5547ba5e8f2eSJosef Bacik else 5548b99d9a6aSFabian Frederick result = BTRFS_COMPARE_TREE_SAME; 5549ee8c494fSNikolay Borisov ret = changed_cb(left_path, right_path, 5550b99d9a6aSFabian Frederick &left_key, result, ctx); 55517069830aSAlexander Block if (ret < 0) 55527069830aSAlexander Block goto out; 55537069830aSAlexander Block advance_left = ADVANCE; 55547069830aSAlexander Block advance_right = ADVANCE; 55557069830aSAlexander Block } 55567069830aSAlexander Block } else if (left_level == right_level) { 55577069830aSAlexander Block cmp = btrfs_comp_cpu_keys(&left_key, &right_key); 55587069830aSAlexander Block if (cmp < 0) { 55597069830aSAlexander Block advance_left = ADVANCE; 55607069830aSAlexander Block } else if (cmp > 0) { 55617069830aSAlexander Block advance_right = ADVANCE; 55627069830aSAlexander Block } else { 55637069830aSAlexander Block left_blockptr = btrfs_node_blockptr( 55647069830aSAlexander Block left_path->nodes[left_level], 55657069830aSAlexander Block left_path->slots[left_level]); 55667069830aSAlexander Block right_blockptr = btrfs_node_blockptr( 55677069830aSAlexander Block right_path->nodes[right_level], 55687069830aSAlexander Block right_path->slots[right_level]); 55696baa4293SFilipe Manana left_gen = btrfs_node_ptr_generation( 55706baa4293SFilipe Manana left_path->nodes[left_level], 55716baa4293SFilipe Manana left_path->slots[left_level]); 55726baa4293SFilipe Manana right_gen = btrfs_node_ptr_generation( 55736baa4293SFilipe Manana right_path->nodes[right_level], 55746baa4293SFilipe Manana right_path->slots[right_level]); 55756baa4293SFilipe Manana if (left_blockptr == right_blockptr && 55766baa4293SFilipe Manana left_gen == right_gen) { 55777069830aSAlexander Block /* 55787069830aSAlexander Block * As we're on a shared block, don't 55797069830aSAlexander Block * allow to go deeper. 55807069830aSAlexander Block */ 55817069830aSAlexander Block advance_left = ADVANCE_ONLY_NEXT; 55827069830aSAlexander Block advance_right = ADVANCE_ONLY_NEXT; 55837069830aSAlexander Block } else { 55847069830aSAlexander Block advance_left = ADVANCE; 55857069830aSAlexander Block advance_right = ADVANCE; 55867069830aSAlexander Block } 55877069830aSAlexander Block } 55887069830aSAlexander Block } else if (left_level < right_level) { 55897069830aSAlexander Block advance_right = ADVANCE; 55907069830aSAlexander Block } else { 55917069830aSAlexander Block advance_left = ADVANCE; 55927069830aSAlexander Block } 55937069830aSAlexander Block } 55947069830aSAlexander Block 55957069830aSAlexander Block out: 55967069830aSAlexander Block btrfs_free_path(left_path); 55977069830aSAlexander Block btrfs_free_path(right_path); 55988f282f71SDavid Sterba kvfree(tmp_buf); 55997069830aSAlexander Block return ret; 56007069830aSAlexander Block } 56017069830aSAlexander Block 56023f157a2fSChris Mason /* 56033f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 56043f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 5605de78b51aSEric Sandeen * tree based on the current path and the min_trans parameters. 56063f157a2fSChris Mason * 56073f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 56083f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 56093f157a2fSChris Mason * 56103f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 56113f157a2fSChris Mason * calling this function. 56123f157a2fSChris Mason */ 5613e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 5614de78b51aSEric Sandeen struct btrfs_key *key, int level, u64 min_trans) 5615e7a84565SChris Mason { 5616e7a84565SChris Mason int slot; 5617e7a84565SChris Mason struct extent_buffer *c; 5618e7a84565SChris Mason 5619934d375bSChris Mason WARN_ON(!path->keep_locks); 5620e7a84565SChris Mason while (level < BTRFS_MAX_LEVEL) { 5621e7a84565SChris Mason if (!path->nodes[level]) 5622e7a84565SChris Mason return 1; 5623e7a84565SChris Mason 5624e7a84565SChris Mason slot = path->slots[level] + 1; 5625e7a84565SChris Mason c = path->nodes[level]; 56263f157a2fSChris Mason next: 5627e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 562833c66f43SYan Zheng int ret; 562933c66f43SYan Zheng int orig_lowest; 563033c66f43SYan Zheng struct btrfs_key cur_key; 563133c66f43SYan Zheng if (level + 1 >= BTRFS_MAX_LEVEL || 563233c66f43SYan Zheng !path->nodes[level + 1]) 5633e7a84565SChris Mason return 1; 563433c66f43SYan Zheng 563533c66f43SYan Zheng if (path->locks[level + 1]) { 563633c66f43SYan Zheng level++; 5637e7a84565SChris Mason continue; 5638e7a84565SChris Mason } 563933c66f43SYan Zheng 564033c66f43SYan Zheng slot = btrfs_header_nritems(c) - 1; 564133c66f43SYan Zheng if (level == 0) 564233c66f43SYan Zheng btrfs_item_key_to_cpu(c, &cur_key, slot); 564333c66f43SYan Zheng else 564433c66f43SYan Zheng btrfs_node_key_to_cpu(c, &cur_key, slot); 564533c66f43SYan Zheng 564633c66f43SYan Zheng orig_lowest = path->lowest_level; 5647b3b4aa74SDavid Sterba btrfs_release_path(path); 564833c66f43SYan Zheng path->lowest_level = level; 564933c66f43SYan Zheng ret = btrfs_search_slot(NULL, root, &cur_key, path, 565033c66f43SYan Zheng 0, 0); 565133c66f43SYan Zheng path->lowest_level = orig_lowest; 565233c66f43SYan Zheng if (ret < 0) 565333c66f43SYan Zheng return ret; 565433c66f43SYan Zheng 565533c66f43SYan Zheng c = path->nodes[level]; 565633c66f43SYan Zheng slot = path->slots[level]; 565733c66f43SYan Zheng if (ret == 0) 565833c66f43SYan Zheng slot++; 565933c66f43SYan Zheng goto next; 566033c66f43SYan Zheng } 566133c66f43SYan Zheng 5662e7a84565SChris Mason if (level == 0) 5663e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 56643f157a2fSChris Mason else { 56653f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 56663f157a2fSChris Mason 56673f157a2fSChris Mason if (gen < min_trans) { 56683f157a2fSChris Mason slot++; 56693f157a2fSChris Mason goto next; 56703f157a2fSChris Mason } 5671e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 56723f157a2fSChris Mason } 5673e7a84565SChris Mason return 0; 5674e7a84565SChris Mason } 5675e7a84565SChris Mason return 1; 5676e7a84565SChris Mason } 5677e7a84565SChris Mason 56787bb86316SChris Mason /* 5679925baeddSChris Mason * search the tree again to find a leaf with greater keys 56800f70abe2SChris Mason * returns 0 if it found something or 1 if there are no greater leaves. 56810f70abe2SChris Mason * returns < 0 on io errors. 568297571fd0SChris Mason */ 5683234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 5684d97e63b6SChris Mason { 56853d7806ecSJan Schmidt return btrfs_next_old_leaf(root, path, 0); 56863d7806ecSJan Schmidt } 56873d7806ecSJan Schmidt 56883d7806ecSJan Schmidt int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, 56893d7806ecSJan Schmidt u64 time_seq) 56903d7806ecSJan Schmidt { 5691d97e63b6SChris Mason int slot; 56928e73f275SChris Mason int level; 56935f39d397SChris Mason struct extent_buffer *c; 56948e73f275SChris Mason struct extent_buffer *next; 5695925baeddSChris Mason struct btrfs_key key; 5696925baeddSChris Mason u32 nritems; 5697925baeddSChris Mason int ret; 56988e73f275SChris Mason int old_spinning = path->leave_spinning; 5699bd681513SChris Mason int next_rw_lock = 0; 5700925baeddSChris Mason 5701925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 5702d397712bSChris Mason if (nritems == 0) 5703925baeddSChris Mason return 1; 5704925baeddSChris Mason 57058e73f275SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 57068e73f275SChris Mason again: 57078e73f275SChris Mason level = 1; 57088e73f275SChris Mason next = NULL; 5709bd681513SChris Mason next_rw_lock = 0; 5710b3b4aa74SDavid Sterba btrfs_release_path(path); 57118e73f275SChris Mason 5712a2135011SChris Mason path->keep_locks = 1; 57138e73f275SChris Mason path->leave_spinning = 1; 57148e73f275SChris Mason 57153d7806ecSJan Schmidt if (time_seq) 57163d7806ecSJan Schmidt ret = btrfs_search_old_slot(root, &key, path, time_seq); 57173d7806ecSJan Schmidt else 5718925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5719925baeddSChris Mason path->keep_locks = 0; 5720925baeddSChris Mason 5721925baeddSChris Mason if (ret < 0) 5722925baeddSChris Mason return ret; 5723925baeddSChris Mason 5724a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 5725168fd7d2SChris Mason /* 5726168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 5727168fd7d2SChris Mason * could have added more items next to the key that used to be 5728168fd7d2SChris Mason * at the very end of the block. So, check again here and 5729168fd7d2SChris Mason * advance the path if there are now more items available. 5730168fd7d2SChris Mason */ 5731a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 5732e457afecSYan Zheng if (ret == 0) 5733168fd7d2SChris Mason path->slots[0]++; 57348e73f275SChris Mason ret = 0; 5735925baeddSChris Mason goto done; 5736925baeddSChris Mason } 57370b43e04fSLiu Bo /* 57380b43e04fSLiu Bo * So the above check misses one case: 57390b43e04fSLiu Bo * - after releasing the path above, someone has removed the item that 57400b43e04fSLiu Bo * used to be at the very end of the block, and balance between leafs 57410b43e04fSLiu Bo * gets another one with bigger key.offset to replace it. 57420b43e04fSLiu Bo * 57430b43e04fSLiu Bo * This one should be returned as well, or we can get leaf corruption 57440b43e04fSLiu Bo * later(esp. in __btrfs_drop_extents()). 57450b43e04fSLiu Bo * 57460b43e04fSLiu Bo * And a bit more explanation about this check, 57470b43e04fSLiu Bo * with ret > 0, the key isn't found, the path points to the slot 57480b43e04fSLiu Bo * where it should be inserted, so the path->slots[0] item must be the 57490b43e04fSLiu Bo * bigger one. 57500b43e04fSLiu Bo */ 57510b43e04fSLiu Bo if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) { 57520b43e04fSLiu Bo ret = 0; 57530b43e04fSLiu Bo goto done; 57540b43e04fSLiu Bo } 5755d97e63b6SChris Mason 5756234b63a0SChris Mason while (level < BTRFS_MAX_LEVEL) { 57578e73f275SChris Mason if (!path->nodes[level]) { 57588e73f275SChris Mason ret = 1; 57598e73f275SChris Mason goto done; 57608e73f275SChris Mason } 57615f39d397SChris Mason 5762d97e63b6SChris Mason slot = path->slots[level] + 1; 5763d97e63b6SChris Mason c = path->nodes[level]; 57645f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 5765d97e63b6SChris Mason level++; 57668e73f275SChris Mason if (level == BTRFS_MAX_LEVEL) { 57678e73f275SChris Mason ret = 1; 57688e73f275SChris Mason goto done; 57698e73f275SChris Mason } 5770d97e63b6SChris Mason continue; 5771d97e63b6SChris Mason } 57725f39d397SChris Mason 5773925baeddSChris Mason if (next) { 5774bd681513SChris Mason btrfs_tree_unlock_rw(next, next_rw_lock); 57755f39d397SChris Mason free_extent_buffer(next); 5776925baeddSChris Mason } 57775f39d397SChris Mason 57788e73f275SChris Mason next = c; 5779bd681513SChris Mason next_rw_lock = path->locks[level]; 5780d07b8528SLiu Bo ret = read_block_for_search(root, path, &next, level, 5781cda79c54SDavid Sterba slot, &key); 57828e73f275SChris Mason if (ret == -EAGAIN) 57838e73f275SChris Mason goto again; 57845f39d397SChris Mason 578576a05b35SChris Mason if (ret < 0) { 5786b3b4aa74SDavid Sterba btrfs_release_path(path); 578776a05b35SChris Mason goto done; 578876a05b35SChris Mason } 578976a05b35SChris Mason 57905cd57b2cSChris Mason if (!path->skip_locking) { 5791bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 5792d42244a0SJan Schmidt if (!ret && time_seq) { 5793d42244a0SJan Schmidt /* 5794d42244a0SJan Schmidt * If we don't get the lock, we may be racing 5795d42244a0SJan Schmidt * with push_leaf_left, holding that lock while 5796d42244a0SJan Schmidt * itself waiting for the leaf we've currently 5797d42244a0SJan Schmidt * locked. To solve this situation, we give up 5798d42244a0SJan Schmidt * on our lock and cycle. 5799d42244a0SJan Schmidt */ 5800cf538830SJan Schmidt free_extent_buffer(next); 5801d42244a0SJan Schmidt btrfs_release_path(path); 5802d42244a0SJan Schmidt cond_resched(); 5803d42244a0SJan Schmidt goto again; 5804d42244a0SJan Schmidt } 58058e73f275SChris Mason if (!ret) { 58068e73f275SChris Mason btrfs_set_path_blocking(path); 5807bd681513SChris Mason btrfs_tree_read_lock(next); 58088e73f275SChris Mason } 5809bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 5810bd681513SChris Mason } 5811d97e63b6SChris Mason break; 5812d97e63b6SChris Mason } 5813d97e63b6SChris Mason path->slots[level] = slot; 5814d97e63b6SChris Mason while (1) { 5815d97e63b6SChris Mason level--; 5816d97e63b6SChris Mason c = path->nodes[level]; 5817925baeddSChris Mason if (path->locks[level]) 5818bd681513SChris Mason btrfs_tree_unlock_rw(c, path->locks[level]); 58198e73f275SChris Mason 58205f39d397SChris Mason free_extent_buffer(c); 5821d97e63b6SChris Mason path->nodes[level] = next; 5822d97e63b6SChris Mason path->slots[level] = 0; 5823a74a4b97SChris Mason if (!path->skip_locking) 5824bd681513SChris Mason path->locks[level] = next_rw_lock; 5825d97e63b6SChris Mason if (!level) 5826d97e63b6SChris Mason break; 5827b4ce94deSChris Mason 5828d07b8528SLiu Bo ret = read_block_for_search(root, path, &next, level, 5829cda79c54SDavid Sterba 0, &key); 58308e73f275SChris Mason if (ret == -EAGAIN) 58318e73f275SChris Mason goto again; 58328e73f275SChris Mason 583376a05b35SChris Mason if (ret < 0) { 5834b3b4aa74SDavid Sterba btrfs_release_path(path); 583576a05b35SChris Mason goto done; 583676a05b35SChris Mason } 583776a05b35SChris Mason 58385cd57b2cSChris Mason if (!path->skip_locking) { 5839bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 58408e73f275SChris Mason if (!ret) { 58418e73f275SChris Mason btrfs_set_path_blocking(path); 5842bd681513SChris Mason btrfs_tree_read_lock(next); 58438e73f275SChris Mason } 5844bd681513SChris Mason next_rw_lock = BTRFS_READ_LOCK; 5845bd681513SChris Mason } 5846d97e63b6SChris Mason } 58478e73f275SChris Mason ret = 0; 5848925baeddSChris Mason done: 5849f7c79f30SChris Mason unlock_up(path, 0, 1, 0, NULL); 58508e73f275SChris Mason path->leave_spinning = old_spinning; 58518e73f275SChris Mason if (!old_spinning) 58528e73f275SChris Mason btrfs_set_path_blocking(path); 58538e73f275SChris Mason 58548e73f275SChris Mason return ret; 5855d97e63b6SChris Mason } 58560b86a832SChris Mason 58573f157a2fSChris Mason /* 58583f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 58593f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 58603f157a2fSChris Mason * 58613f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 58623f157a2fSChris Mason */ 58630b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 58640b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 58650b86a832SChris Mason int type) 58660b86a832SChris Mason { 58670b86a832SChris Mason struct btrfs_key found_key; 58680b86a832SChris Mason struct extent_buffer *leaf; 5869e02119d5SChris Mason u32 nritems; 58700b86a832SChris Mason int ret; 58710b86a832SChris Mason 58720b86a832SChris Mason while (1) { 58730b86a832SChris Mason if (path->slots[0] == 0) { 5874b4ce94deSChris Mason btrfs_set_path_blocking(path); 58750b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 58760b86a832SChris Mason if (ret != 0) 58770b86a832SChris Mason return ret; 58780b86a832SChris Mason } else { 58790b86a832SChris Mason path->slots[0]--; 58800b86a832SChris Mason } 58810b86a832SChris Mason leaf = path->nodes[0]; 5882e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 5883e02119d5SChris Mason if (nritems == 0) 5884e02119d5SChris Mason return 1; 5885e02119d5SChris Mason if (path->slots[0] == nritems) 5886e02119d5SChris Mason path->slots[0]--; 5887e02119d5SChris Mason 58880b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 5889e02119d5SChris Mason if (found_key.objectid < min_objectid) 5890e02119d5SChris Mason break; 58910a4eefbbSYan Zheng if (found_key.type == type) 58920a4eefbbSYan Zheng return 0; 5893e02119d5SChris Mason if (found_key.objectid == min_objectid && 5894e02119d5SChris Mason found_key.type < type) 5895e02119d5SChris Mason break; 58960b86a832SChris Mason } 58970b86a832SChris Mason return 1; 58980b86a832SChris Mason } 5899ade2e0b3SWang Shilong 5900ade2e0b3SWang Shilong /* 5901ade2e0b3SWang Shilong * search in extent tree to find a previous Metadata/Data extent item with 5902ade2e0b3SWang Shilong * min objecitd. 5903ade2e0b3SWang Shilong * 5904ade2e0b3SWang Shilong * returns 0 if something is found, 1 if nothing was found and < 0 on error 5905ade2e0b3SWang Shilong */ 5906ade2e0b3SWang Shilong int btrfs_previous_extent_item(struct btrfs_root *root, 5907ade2e0b3SWang Shilong struct btrfs_path *path, u64 min_objectid) 5908ade2e0b3SWang Shilong { 5909ade2e0b3SWang Shilong struct btrfs_key found_key; 5910ade2e0b3SWang Shilong struct extent_buffer *leaf; 5911ade2e0b3SWang Shilong u32 nritems; 5912ade2e0b3SWang Shilong int ret; 5913ade2e0b3SWang Shilong 5914ade2e0b3SWang Shilong while (1) { 5915ade2e0b3SWang Shilong if (path->slots[0] == 0) { 5916ade2e0b3SWang Shilong btrfs_set_path_blocking(path); 5917ade2e0b3SWang Shilong ret = btrfs_prev_leaf(root, path); 5918ade2e0b3SWang Shilong if (ret != 0) 5919ade2e0b3SWang Shilong return ret; 5920ade2e0b3SWang Shilong } else { 5921ade2e0b3SWang Shilong path->slots[0]--; 5922ade2e0b3SWang Shilong } 5923ade2e0b3SWang Shilong leaf = path->nodes[0]; 5924ade2e0b3SWang Shilong nritems = btrfs_header_nritems(leaf); 5925ade2e0b3SWang Shilong if (nritems == 0) 5926ade2e0b3SWang Shilong return 1; 5927ade2e0b3SWang Shilong if (path->slots[0] == nritems) 5928ade2e0b3SWang Shilong path->slots[0]--; 5929ade2e0b3SWang Shilong 5930ade2e0b3SWang Shilong btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 5931ade2e0b3SWang Shilong if (found_key.objectid < min_objectid) 5932ade2e0b3SWang Shilong break; 5933ade2e0b3SWang Shilong if (found_key.type == BTRFS_EXTENT_ITEM_KEY || 5934ade2e0b3SWang Shilong found_key.type == BTRFS_METADATA_ITEM_KEY) 5935ade2e0b3SWang Shilong return 0; 5936ade2e0b3SWang Shilong if (found_key.objectid == min_objectid && 5937ade2e0b3SWang Shilong found_key.type < BTRFS_EXTENT_ITEM_KEY) 5938ade2e0b3SWang Shilong break; 5939ade2e0b3SWang Shilong } 5940ade2e0b3SWang Shilong return 1; 5941ade2e0b3SWang Shilong } 5942