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> 10e41d12f5SChristoph Hellwig #include <linux/error-injection.h> 119b569ea0SJosef Bacik #include "messages.h" 12eb60ceacSChris Mason #include "ctree.h" 13eb60ceacSChris Mason #include "disk-io.h" 147f5c1516SChris Mason #include "transaction.h" 155f39d397SChris Mason #include "print-tree.h" 16925baeddSChris Mason #include "locking.h" 17de37aa51SNikolay Borisov #include "volumes.h" 18f616f5cdSQu Wenruo #include "qgroup.h" 19f3a84ccdSFilipe Manana #include "tree-mod-log.h" 2088c602abSQu Wenruo #include "tree-checker.h" 21ec8eb376SJosef Bacik #include "fs.h" 22ad1ac501SJosef Bacik #include "accessors.h" 23a0231804SJosef Bacik #include "extent-tree.h" 2467707479SJosef Bacik #include "relocation.h" 256bfd0ffaSJosef Bacik #include "file-item.h" 269a8dd150SChris Mason 27226463d7SJosef Bacik static struct kmem_cache *btrfs_path_cachep; 28226463d7SJosef Bacik 29e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 30e089f05cSChris Mason *root, struct btrfs_path *path, int level); 31310712b2SOmar Sandoval static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root, 32310712b2SOmar Sandoval const struct btrfs_key *ins_key, struct btrfs_path *path, 33310712b2SOmar Sandoval int data_size, int extend); 345f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 352ff7e61eSJeff Mahoney struct extent_buffer *dst, 36971a1f66SChris Mason struct extent_buffer *src, int empty); 375f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 385f39d397SChris Mason struct extent_buffer *dst_buf, 395f39d397SChris Mason struct extent_buffer *src_buf); 40d97e63b6SChris Mason 41af024ed2SJohannes Thumshirn static const struct btrfs_csums { 42af024ed2SJohannes Thumshirn u16 size; 4359a0fcdbSDavid Sterba const char name[10]; 4459a0fcdbSDavid Sterba const char driver[12]; 45af024ed2SJohannes Thumshirn } btrfs_csums[] = { 46af024ed2SJohannes Thumshirn [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" }, 473951e7f0SJohannes Thumshirn [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" }, 483831bf00SJohannes Thumshirn [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" }, 49352ae07bSDavid Sterba [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b", 50352ae07bSDavid Sterba .driver = "blake2b-256" }, 51af024ed2SJohannes Thumshirn }; 52af024ed2SJohannes Thumshirn 533a3178c7SJosef Bacik /* 543a3178c7SJosef Bacik * The leaf data grows from end-to-front in the node. this returns the address 553a3178c7SJosef Bacik * of the start of the last item, which is the stop of the leaf data stack. 563a3178c7SJosef Bacik */ 573a3178c7SJosef Bacik static unsigned int leaf_data_end(const struct extent_buffer *leaf) 583a3178c7SJosef Bacik { 593a3178c7SJosef Bacik u32 nr = btrfs_header_nritems(leaf); 603a3178c7SJosef Bacik 613a3178c7SJosef Bacik if (nr == 0) 623a3178c7SJosef Bacik return BTRFS_LEAF_DATA_SIZE(leaf->fs_info); 633a3178c7SJosef Bacik return btrfs_item_offset(leaf, nr - 1); 643a3178c7SJosef Bacik } 653a3178c7SJosef Bacik 66637e3b48SJosef Bacik /* 67637e3b48SJosef Bacik * Move data in a @leaf (using memmove, safe for overlapping ranges). 68637e3b48SJosef Bacik * 69637e3b48SJosef Bacik * @leaf: leaf that we're doing a memmove on 70637e3b48SJosef Bacik * @dst_offset: item data offset we're moving to 71637e3b48SJosef Bacik * @src_offset: item data offset were' moving from 72637e3b48SJosef Bacik * @len: length of the data we're moving 73637e3b48SJosef Bacik * 74637e3b48SJosef Bacik * Wrapper around memmove_extent_buffer() that takes into account the header on 75637e3b48SJosef Bacik * the leaf. The btrfs_item offset's start directly after the header, so we 76637e3b48SJosef Bacik * have to adjust any offsets to account for the header in the leaf. This 77637e3b48SJosef Bacik * handles that math to simplify the callers. 78637e3b48SJosef Bacik */ 79637e3b48SJosef Bacik static inline void memmove_leaf_data(const struct extent_buffer *leaf, 80637e3b48SJosef Bacik unsigned long dst_offset, 81637e3b48SJosef Bacik unsigned long src_offset, 82637e3b48SJosef Bacik unsigned long len) 83637e3b48SJosef Bacik { 848009adf3SJosef Bacik memmove_extent_buffer(leaf, btrfs_item_nr_offset(leaf, 0) + dst_offset, 858009adf3SJosef Bacik btrfs_item_nr_offset(leaf, 0) + src_offset, len); 86637e3b48SJosef Bacik } 87637e3b48SJosef Bacik 88637e3b48SJosef Bacik /* 89637e3b48SJosef Bacik * Copy item data from @src into @dst at the given @offset. 90637e3b48SJosef Bacik * 91637e3b48SJosef Bacik * @dst: destination leaf that we're copying into 92637e3b48SJosef Bacik * @src: source leaf that we're copying from 93637e3b48SJosef Bacik * @dst_offset: item data offset we're copying to 94637e3b48SJosef Bacik * @src_offset: item data offset were' copying from 95637e3b48SJosef Bacik * @len: length of the data we're copying 96637e3b48SJosef Bacik * 97637e3b48SJosef Bacik * Wrapper around copy_extent_buffer() that takes into account the header on 98637e3b48SJosef Bacik * the leaf. The btrfs_item offset's start directly after the header, so we 99637e3b48SJosef Bacik * have to adjust any offsets to account for the header in the leaf. This 100637e3b48SJosef Bacik * handles that math to simplify the callers. 101637e3b48SJosef Bacik */ 102637e3b48SJosef Bacik static inline void copy_leaf_data(const struct extent_buffer *dst, 103637e3b48SJosef Bacik const struct extent_buffer *src, 104637e3b48SJosef Bacik unsigned long dst_offset, 105637e3b48SJosef Bacik unsigned long src_offset, unsigned long len) 106637e3b48SJosef Bacik { 1078009adf3SJosef Bacik copy_extent_buffer(dst, src, btrfs_item_nr_offset(dst, 0) + dst_offset, 1088009adf3SJosef Bacik btrfs_item_nr_offset(src, 0) + src_offset, len); 109637e3b48SJosef Bacik } 110637e3b48SJosef Bacik 111637e3b48SJosef Bacik /* 112637e3b48SJosef Bacik * Move items in a @leaf (using memmove). 113637e3b48SJosef Bacik * 114637e3b48SJosef Bacik * @dst: destination leaf for the items 115637e3b48SJosef Bacik * @dst_item: the item nr we're copying into 116637e3b48SJosef Bacik * @src_item: the item nr we're copying from 117637e3b48SJosef Bacik * @nr_items: the number of items to copy 118637e3b48SJosef Bacik * 119637e3b48SJosef Bacik * Wrapper around memmove_extent_buffer() that does the math to get the 120637e3b48SJosef Bacik * appropriate offsets into the leaf from the item numbers. 121637e3b48SJosef Bacik */ 122637e3b48SJosef Bacik static inline void memmove_leaf_items(const struct extent_buffer *leaf, 123637e3b48SJosef Bacik int dst_item, int src_item, int nr_items) 124637e3b48SJosef Bacik { 125637e3b48SJosef Bacik memmove_extent_buffer(leaf, btrfs_item_nr_offset(leaf, dst_item), 126637e3b48SJosef Bacik btrfs_item_nr_offset(leaf, src_item), 127637e3b48SJosef Bacik nr_items * sizeof(struct btrfs_item)); 128637e3b48SJosef Bacik } 129637e3b48SJosef Bacik 130637e3b48SJosef Bacik /* 131637e3b48SJosef Bacik * Copy items from @src into @dst at the given @offset. 132637e3b48SJosef Bacik * 133637e3b48SJosef Bacik * @dst: destination leaf for the items 134637e3b48SJosef Bacik * @src: source leaf for the items 135637e3b48SJosef Bacik * @dst_item: the item nr we're copying into 136637e3b48SJosef Bacik * @src_item: the item nr we're copying from 137637e3b48SJosef Bacik * @nr_items: the number of items to copy 138637e3b48SJosef Bacik * 139637e3b48SJosef Bacik * Wrapper around copy_extent_buffer() that does the math to get the 140637e3b48SJosef Bacik * appropriate offsets into the leaf from the item numbers. 141637e3b48SJosef Bacik */ 142637e3b48SJosef Bacik static inline void copy_leaf_items(const struct extent_buffer *dst, 143637e3b48SJosef Bacik const struct extent_buffer *src, 144637e3b48SJosef Bacik int dst_item, int src_item, int nr_items) 145637e3b48SJosef Bacik { 146637e3b48SJosef Bacik copy_extent_buffer(dst, src, btrfs_item_nr_offset(dst, dst_item), 147637e3b48SJosef Bacik btrfs_item_nr_offset(src, src_item), 148637e3b48SJosef Bacik nr_items * sizeof(struct btrfs_item)); 149637e3b48SJosef Bacik } 150637e3b48SJosef Bacik 151b3cbfb0dSJosef Bacik /* This exists for btrfs-progs usages. */ 152b3cbfb0dSJosef Bacik u16 btrfs_csum_type_size(u16 type) 153b3cbfb0dSJosef Bacik { 154b3cbfb0dSJosef Bacik return btrfs_csums[type].size; 155b3cbfb0dSJosef Bacik } 156b3cbfb0dSJosef Bacik 157af024ed2SJohannes Thumshirn int btrfs_super_csum_size(const struct btrfs_super_block *s) 158af024ed2SJohannes Thumshirn { 159af024ed2SJohannes Thumshirn u16 t = btrfs_super_csum_type(s); 160af024ed2SJohannes Thumshirn /* 161af024ed2SJohannes Thumshirn * csum type is validated at mount time 162af024ed2SJohannes Thumshirn */ 163b3cbfb0dSJosef Bacik return btrfs_csum_type_size(t); 164af024ed2SJohannes Thumshirn } 165af024ed2SJohannes Thumshirn 166af024ed2SJohannes Thumshirn const char *btrfs_super_csum_name(u16 csum_type) 167af024ed2SJohannes Thumshirn { 168af024ed2SJohannes Thumshirn /* csum type is validated at mount time */ 169af024ed2SJohannes Thumshirn return btrfs_csums[csum_type].name; 170af024ed2SJohannes Thumshirn } 171af024ed2SJohannes Thumshirn 172b4e967beSDavid Sterba /* 173b4e967beSDavid Sterba * Return driver name if defined, otherwise the name that's also a valid driver 174b4e967beSDavid Sterba * name 175b4e967beSDavid Sterba */ 176b4e967beSDavid Sterba const char *btrfs_super_csum_driver(u16 csum_type) 177b4e967beSDavid Sterba { 178b4e967beSDavid Sterba /* csum type is validated at mount time */ 17959a0fcdbSDavid Sterba return btrfs_csums[csum_type].driver[0] ? 18059a0fcdbSDavid Sterba btrfs_csums[csum_type].driver : 181b4e967beSDavid Sterba btrfs_csums[csum_type].name; 182b4e967beSDavid Sterba } 183b4e967beSDavid Sterba 184604997b4SDavid Sterba size_t __attribute_const__ btrfs_get_num_csums(void) 185f7cea56cSDavid Sterba { 186f7cea56cSDavid Sterba return ARRAY_SIZE(btrfs_csums); 187f7cea56cSDavid Sterba } 188f7cea56cSDavid Sterba 1892c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void) 1902c90e5d6SChris Mason { 191a4c853afSChenXiaoSong might_sleep(); 192a4c853afSChenXiaoSong 193e2c89907SMasahiro Yamada return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 1942c90e5d6SChris Mason } 1952c90e5d6SChris Mason 196d352ac68SChris Mason /* this also releases the path */ 1972c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p) 1982c90e5d6SChris Mason { 199ff175d57SJesper Juhl if (!p) 200ff175d57SJesper Juhl return; 201b3b4aa74SDavid Sterba btrfs_release_path(p); 2022c90e5d6SChris Mason kmem_cache_free(btrfs_path_cachep, p); 2032c90e5d6SChris Mason } 2042c90e5d6SChris Mason 205d352ac68SChris Mason /* 206d352ac68SChris Mason * path release drops references on the extent buffers in the path 207d352ac68SChris Mason * and it drops any locks held by this path 208d352ac68SChris Mason * 209d352ac68SChris Mason * It is safe to call this on paths that no locks or extent buffers held. 210d352ac68SChris Mason */ 211b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p) 212eb60ceacSChris Mason { 213eb60ceacSChris Mason int i; 214a2135011SChris Mason 215234b63a0SChris Mason for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 2163f157a2fSChris Mason p->slots[i] = 0; 217eb60ceacSChris Mason if (!p->nodes[i]) 218925baeddSChris Mason continue; 219925baeddSChris Mason if (p->locks[i]) { 220bd681513SChris Mason btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); 221925baeddSChris Mason p->locks[i] = 0; 222925baeddSChris Mason } 2235f39d397SChris Mason free_extent_buffer(p->nodes[i]); 2243f157a2fSChris Mason p->nodes[i] = NULL; 225eb60ceacSChris Mason } 226eb60ceacSChris Mason } 227eb60ceacSChris Mason 228d352ac68SChris Mason /* 2298bb808c6SDavid Sterba * We want the transaction abort to print stack trace only for errors where the 2308bb808c6SDavid Sterba * cause could be a bug, eg. due to ENOSPC, and not for common errors that are 2318bb808c6SDavid Sterba * caused by external factors. 2328bb808c6SDavid Sterba */ 2338bb808c6SDavid Sterba bool __cold abort_should_print_stack(int errno) 2348bb808c6SDavid Sterba { 2358bb808c6SDavid Sterba switch (errno) { 2368bb808c6SDavid Sterba case -EIO: 2378bb808c6SDavid Sterba case -EROFS: 2388bb808c6SDavid Sterba case -ENOMEM: 2398bb808c6SDavid Sterba return false; 2408bb808c6SDavid Sterba } 2418bb808c6SDavid Sterba return true; 2428bb808c6SDavid Sterba } 2438bb808c6SDavid Sterba 2448bb808c6SDavid Sterba /* 245d352ac68SChris Mason * safely gets a reference on the root node of a tree. A lock 246d352ac68SChris Mason * is not taken, so a concurrent writer may put a different node 247d352ac68SChris Mason * at the root of the tree. See btrfs_lock_root_node for the 248d352ac68SChris Mason * looping required. 249d352ac68SChris Mason * 250d352ac68SChris Mason * The extent buffer returned by this has a reference taken, so 251d352ac68SChris Mason * it won't disappear. It may stop being the root of the tree 252d352ac68SChris Mason * at any time because there are no locks held. 253d352ac68SChris Mason */ 254925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 255925baeddSChris Mason { 256925baeddSChris Mason struct extent_buffer *eb; 257240f62c8SChris Mason 2583083ee2eSJosef Bacik while (1) { 259240f62c8SChris Mason rcu_read_lock(); 260240f62c8SChris Mason eb = rcu_dereference(root->node); 2613083ee2eSJosef Bacik 2623083ee2eSJosef Bacik /* 2633083ee2eSJosef Bacik * RCU really hurts here, we could free up the root node because 26401327610SNicholas D Steeves * it was COWed but we may not get the new root node yet so do 2653083ee2eSJosef Bacik * the inc_not_zero dance and if it doesn't work then 2663083ee2eSJosef Bacik * synchronize_rcu and try again. 2673083ee2eSJosef Bacik */ 2683083ee2eSJosef Bacik if (atomic_inc_not_zero(&eb->refs)) { 269240f62c8SChris Mason rcu_read_unlock(); 2703083ee2eSJosef Bacik break; 2713083ee2eSJosef Bacik } 2723083ee2eSJosef Bacik rcu_read_unlock(); 2733083ee2eSJosef Bacik synchronize_rcu(); 2743083ee2eSJosef Bacik } 275925baeddSChris Mason return eb; 276925baeddSChris Mason } 277925baeddSChris Mason 27892a7cc42SQu Wenruo /* 27992a7cc42SQu Wenruo * Cowonly root (not-shareable trees, everything not subvolume or reloc roots), 28092a7cc42SQu Wenruo * just get put onto a simple dirty list. Transaction walks this list to make 28192a7cc42SQu Wenruo * sure they get properly updated on disk. 282d352ac68SChris Mason */ 2830b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root) 2840b86a832SChris Mason { 2850b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2860b246afaSJeff Mahoney 287e7070be1SJosef Bacik if (test_bit(BTRFS_ROOT_DIRTY, &root->state) || 288e7070be1SJosef Bacik !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state)) 289e7070be1SJosef Bacik return; 290e7070be1SJosef Bacik 2910b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 292e7070be1SJosef Bacik if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) { 293e7070be1SJosef Bacik /* Want the extent tree to be the last on the list */ 2944fd786e6SMisono Tomohiro if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID) 295e7070be1SJosef Bacik list_move_tail(&root->dirty_list, 2960b246afaSJeff Mahoney &fs_info->dirty_cowonly_roots); 297e7070be1SJosef Bacik else 298e7070be1SJosef Bacik list_move(&root->dirty_list, 2990b246afaSJeff Mahoney &fs_info->dirty_cowonly_roots); 3000b86a832SChris Mason } 3010b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 3020b86a832SChris Mason } 3030b86a832SChris Mason 304d352ac68SChris Mason /* 305d352ac68SChris Mason * used by snapshot creation to make a copy of a root for a tree with 306d352ac68SChris Mason * a given objectid. The buffer with the new root node is returned in 307d352ac68SChris Mason * cow_ret, and this func returns zero on success or a negative error code. 308d352ac68SChris Mason */ 309be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans, 310be20aa9dSChris Mason struct btrfs_root *root, 311be20aa9dSChris Mason struct extent_buffer *buf, 312be20aa9dSChris Mason struct extent_buffer **cow_ret, u64 new_root_objectid) 313be20aa9dSChris Mason { 3140b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 315be20aa9dSChris Mason struct extent_buffer *cow; 316be20aa9dSChris Mason int ret = 0; 317be20aa9dSChris Mason int level; 3185d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 319be20aa9dSChris Mason 32092a7cc42SQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && 3210b246afaSJeff Mahoney trans->transid != fs_info->running_transaction->transid); 32292a7cc42SQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && 32327cdeb70SMiao Xie trans->transid != root->last_trans); 324be20aa9dSChris Mason 325be20aa9dSChris Mason level = btrfs_header_level(buf); 3265d4f98a2SYan Zheng if (level == 0) 3275d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 3285d4f98a2SYan Zheng else 3295d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 33031840ae1SZheng Yan 3314d75f8a9SDavid Sterba cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, 332cf6f34aaSJosef Bacik &disk_key, level, buf->start, 0, 333cf6f34aaSJosef Bacik BTRFS_NESTING_NEW_ROOT); 3345d4f98a2SYan Zheng if (IS_ERR(cow)) 335be20aa9dSChris Mason return PTR_ERR(cow); 336be20aa9dSChris Mason 33758e8012cSDavid Sterba copy_extent_buffer_full(cow, buf); 338be20aa9dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 339be20aa9dSChris Mason btrfs_set_header_generation(cow, trans->transid); 3405d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 3415d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 3425d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 3435d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 3445d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 3455d4f98a2SYan Zheng else 346be20aa9dSChris Mason btrfs_set_header_owner(cow, new_root_objectid); 347be20aa9dSChris Mason 348de37aa51SNikolay Borisov write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); 3492b82032cSYan Zheng 350be20aa9dSChris Mason WARN_ON(btrfs_header_generation(buf) > trans->transid); 3515d4f98a2SYan Zheng if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 352e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 3535d4f98a2SYan Zheng else 354e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 355867ed321SJosef Bacik if (ret) { 35672c9925fSFilipe Manana btrfs_tree_unlock(cow); 35772c9925fSFilipe Manana free_extent_buffer(cow); 358867ed321SJosef Bacik btrfs_abort_transaction(trans, ret); 359be20aa9dSChris Mason return ret; 360867ed321SJosef Bacik } 361be20aa9dSChris Mason 362be20aa9dSChris Mason btrfs_mark_buffer_dirty(cow); 363be20aa9dSChris Mason *cow_ret = cow; 364be20aa9dSChris Mason return 0; 365be20aa9dSChris Mason } 366be20aa9dSChris Mason 367d352ac68SChris Mason /* 3685d4f98a2SYan Zheng * check if the tree block can be shared by multiple trees 3695d4f98a2SYan Zheng */ 3705d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root, 3715d4f98a2SYan Zheng struct extent_buffer *buf) 3725d4f98a2SYan Zheng { 3735d4f98a2SYan Zheng /* 37492a7cc42SQu Wenruo * Tree blocks not in shareable trees and tree roots are never shared. 37592a7cc42SQu Wenruo * If a block was allocated after the last snapshot and the block was 37692a7cc42SQu Wenruo * not allocated by tree relocation, we know the block is not shared. 3775d4f98a2SYan Zheng */ 37892a7cc42SQu Wenruo if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && 3795d4f98a2SYan Zheng buf != root->node && buf != root->commit_root && 3805d4f98a2SYan Zheng (btrfs_header_generation(buf) <= 3815d4f98a2SYan Zheng btrfs_root_last_snapshot(&root->root_item) || 3825d4f98a2SYan Zheng btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) 3835d4f98a2SYan Zheng return 1; 384a79865c6SNikolay Borisov 3855d4f98a2SYan Zheng return 0; 3865d4f98a2SYan Zheng } 3875d4f98a2SYan Zheng 3885d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, 3895d4f98a2SYan Zheng struct btrfs_root *root, 3905d4f98a2SYan Zheng struct extent_buffer *buf, 391f0486c68SYan, Zheng struct extent_buffer *cow, 392f0486c68SYan, Zheng int *last_ref) 3935d4f98a2SYan Zheng { 3940b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3955d4f98a2SYan Zheng u64 refs; 3965d4f98a2SYan Zheng u64 owner; 3975d4f98a2SYan Zheng u64 flags; 3985d4f98a2SYan Zheng u64 new_flags = 0; 3995d4f98a2SYan Zheng int ret; 4005d4f98a2SYan Zheng 4015d4f98a2SYan Zheng /* 4025d4f98a2SYan Zheng * Backrefs update rules: 4035d4f98a2SYan Zheng * 4045d4f98a2SYan Zheng * Always use full backrefs for extent pointers in tree block 4055d4f98a2SYan Zheng * allocated by tree relocation. 4065d4f98a2SYan Zheng * 4075d4f98a2SYan Zheng * If a shared tree block is no longer referenced by its owner 4085d4f98a2SYan Zheng * tree (btrfs_header_owner(buf) == root->root_key.objectid), 4095d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 4105d4f98a2SYan Zheng * 4115d4f98a2SYan Zheng * If a tree block is been relocating 4125d4f98a2SYan Zheng * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), 4135d4f98a2SYan Zheng * use full backrefs for extent pointers in tree block. 4145d4f98a2SYan Zheng * The reason for this is some operations (such as drop tree) 4155d4f98a2SYan Zheng * are only allowed for blocks use full backrefs. 4165d4f98a2SYan Zheng */ 4175d4f98a2SYan Zheng 4185d4f98a2SYan Zheng if (btrfs_block_can_be_shared(root, buf)) { 4192ff7e61eSJeff Mahoney ret = btrfs_lookup_extent_info(trans, fs_info, buf->start, 4203173a18fSJosef Bacik btrfs_header_level(buf), 1, 4213173a18fSJosef Bacik &refs, &flags); 422be1a5564SMark Fasheh if (ret) 423be1a5564SMark Fasheh return ret; 424eced687eSFilipe Manana if (unlikely(refs == 0)) { 425eced687eSFilipe Manana btrfs_crit(fs_info, 426eced687eSFilipe Manana "found 0 references for tree block at bytenr %llu level %d root %llu", 427eced687eSFilipe Manana buf->start, btrfs_header_level(buf), 428eced687eSFilipe Manana btrfs_root_id(root)); 429eced687eSFilipe Manana ret = -EUCLEAN; 430eced687eSFilipe Manana btrfs_abort_transaction(trans, ret); 431e5df9573SMark Fasheh return ret; 432e5df9573SMark Fasheh } 4335d4f98a2SYan Zheng } else { 4345d4f98a2SYan Zheng refs = 1; 4355d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 4365d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 4375d4f98a2SYan Zheng flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; 4385d4f98a2SYan Zheng else 4395d4f98a2SYan Zheng flags = 0; 4405d4f98a2SYan Zheng } 4415d4f98a2SYan Zheng 4425d4f98a2SYan Zheng owner = btrfs_header_owner(buf); 4435d4f98a2SYan Zheng BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && 4445d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); 4455d4f98a2SYan Zheng 4465d4f98a2SYan Zheng if (refs > 1) { 4475d4f98a2SYan Zheng if ((owner == root->root_key.objectid || 4485d4f98a2SYan Zheng root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && 4495d4f98a2SYan Zheng !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 450e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, buf, 1); 451692826b2SJeff Mahoney if (ret) 452692826b2SJeff Mahoney return ret; 4535d4f98a2SYan Zheng 4545d4f98a2SYan Zheng if (root->root_key.objectid == 4555d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) { 456e339a6b0SJosef Bacik ret = btrfs_dec_ref(trans, root, buf, 0); 457692826b2SJeff Mahoney if (ret) 458692826b2SJeff Mahoney return ret; 459e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 460692826b2SJeff Mahoney if (ret) 461692826b2SJeff Mahoney return ret; 4625d4f98a2SYan Zheng } 4635d4f98a2SYan Zheng new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; 4645d4f98a2SYan Zheng } else { 4655d4f98a2SYan Zheng 4665d4f98a2SYan Zheng if (root->root_key.objectid == 4675d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 468e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 4695d4f98a2SYan Zheng else 470e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 471692826b2SJeff Mahoney if (ret) 472692826b2SJeff Mahoney return ret; 4735d4f98a2SYan Zheng } 4745d4f98a2SYan Zheng if (new_flags != 0) { 4754aec05faSJosef Bacik ret = btrfs_set_disk_extent_flags(trans, buf, new_flags); 476be1a5564SMark Fasheh if (ret) 477be1a5564SMark Fasheh return ret; 4785d4f98a2SYan Zheng } 4795d4f98a2SYan Zheng } else { 4805d4f98a2SYan Zheng if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { 4815d4f98a2SYan Zheng if (root->root_key.objectid == 4825d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID) 483e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 1); 4845d4f98a2SYan Zheng else 485e339a6b0SJosef Bacik ret = btrfs_inc_ref(trans, root, cow, 0); 486692826b2SJeff Mahoney if (ret) 487692826b2SJeff Mahoney return ret; 488e339a6b0SJosef Bacik ret = btrfs_dec_ref(trans, root, buf, 1); 489692826b2SJeff Mahoney if (ret) 490692826b2SJeff Mahoney return ret; 4915d4f98a2SYan Zheng } 492190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, buf); 493f0486c68SYan, Zheng *last_ref = 1; 4945d4f98a2SYan Zheng } 4955d4f98a2SYan Zheng return 0; 4965d4f98a2SYan Zheng } 4975d4f98a2SYan Zheng 4985d4f98a2SYan Zheng /* 499d397712bSChris Mason * does the dirty work in cow of a single block. The parent block (if 500d397712bSChris Mason * supplied) is updated to point to the new cow copy. The new buffer is marked 501d397712bSChris Mason * dirty and returned locked. If you modify the block it needs to be marked 502d397712bSChris Mason * dirty again. 503d352ac68SChris Mason * 504d352ac68SChris Mason * search_start -- an allocation hint for the new block 505d352ac68SChris Mason * 506d397712bSChris Mason * empty_size -- a hint that you plan on doing more cow. This is the size in 507d397712bSChris Mason * bytes the allocator should try to find free next to the block it returns. 508d397712bSChris Mason * This is just a hint and may be ignored by the allocator. 509d352ac68SChris Mason */ 510d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, 5115f39d397SChris Mason struct btrfs_root *root, 5125f39d397SChris Mason struct extent_buffer *buf, 5135f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 5145f39d397SChris Mason struct extent_buffer **cow_ret, 5159631e4ccSJosef Bacik u64 search_start, u64 empty_size, 5169631e4ccSJosef Bacik enum btrfs_lock_nesting nest) 5176702ed49SChris Mason { 5180b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 5195d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 5205f39d397SChris Mason struct extent_buffer *cow; 521be1a5564SMark Fasheh int level, ret; 522f0486c68SYan, Zheng int last_ref = 0; 523925baeddSChris Mason int unlock_orig = 0; 5240f5053ebSGoldwyn Rodrigues u64 parent_start = 0; 5256702ed49SChris Mason 526925baeddSChris Mason if (*cow_ret == buf) 527925baeddSChris Mason unlock_orig = 1; 528925baeddSChris Mason 52949d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 530925baeddSChris Mason 53192a7cc42SQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && 5320b246afaSJeff Mahoney trans->transid != fs_info->running_transaction->transid); 53392a7cc42SQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && 53427cdeb70SMiao Xie trans->transid != root->last_trans); 5355f39d397SChris Mason 5367bb86316SChris Mason level = btrfs_header_level(buf); 53731840ae1SZheng Yan 5385d4f98a2SYan Zheng if (level == 0) 5395d4f98a2SYan Zheng btrfs_item_key(buf, &disk_key, 0); 5405d4f98a2SYan Zheng else 5415d4f98a2SYan Zheng btrfs_node_key(buf, &disk_key, 0); 5425d4f98a2SYan Zheng 5430f5053ebSGoldwyn Rodrigues if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent) 5445d4f98a2SYan Zheng parent_start = parent->start; 5455d4f98a2SYan Zheng 54679bd3712SFilipe Manana cow = btrfs_alloc_tree_block(trans, root, parent_start, 54779bd3712SFilipe Manana root->root_key.objectid, &disk_key, level, 54879bd3712SFilipe Manana search_start, empty_size, nest); 5496702ed49SChris Mason if (IS_ERR(cow)) 5506702ed49SChris Mason return PTR_ERR(cow); 5516702ed49SChris Mason 552b4ce94deSChris Mason /* cow is set to blocking by btrfs_init_new_buffer */ 553b4ce94deSChris Mason 55458e8012cSDavid Sterba copy_extent_buffer_full(cow, buf); 555db94535dSChris Mason btrfs_set_header_bytenr(cow, cow->start); 5565f39d397SChris Mason btrfs_set_header_generation(cow, trans->transid); 5575d4f98a2SYan Zheng btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); 5585d4f98a2SYan Zheng btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | 5595d4f98a2SYan Zheng BTRFS_HEADER_FLAG_RELOC); 5605d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 5615d4f98a2SYan Zheng btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); 5625d4f98a2SYan Zheng else 5635f39d397SChris Mason btrfs_set_header_owner(cow, root->root_key.objectid); 5646702ed49SChris Mason 565de37aa51SNikolay Borisov write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); 5662b82032cSYan Zheng 567be1a5564SMark Fasheh ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); 568b68dc2a9SMark Fasheh if (ret) { 569572c83acSJosef Bacik btrfs_tree_unlock(cow); 570572c83acSJosef Bacik free_extent_buffer(cow); 57166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 572b68dc2a9SMark Fasheh return ret; 573b68dc2a9SMark Fasheh } 5741a40e23bSZheng Yan 57592a7cc42SQu Wenruo if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { 57683d4cfd4SJosef Bacik ret = btrfs_reloc_cow_block(trans, root, buf, cow); 57793314e3bSZhaolei if (ret) { 578572c83acSJosef Bacik btrfs_tree_unlock(cow); 579572c83acSJosef Bacik free_extent_buffer(cow); 58066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 58183d4cfd4SJosef Bacik return ret; 58283d4cfd4SJosef Bacik } 58393314e3bSZhaolei } 5843fd0a558SYan, Zheng 5856702ed49SChris Mason if (buf == root->node) { 586925baeddSChris Mason WARN_ON(parent && parent != buf); 5875d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 5885d4f98a2SYan Zheng btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) 5895d4f98a2SYan Zheng parent_start = buf->start; 590925baeddSChris Mason 591406808abSFilipe Manana ret = btrfs_tree_mod_log_insert_root(root->node, cow, true); 59240b0a749SFilipe Manana if (ret < 0) { 59340b0a749SFilipe Manana btrfs_tree_unlock(cow); 59440b0a749SFilipe Manana free_extent_buffer(cow); 59540b0a749SFilipe Manana btrfs_abort_transaction(trans, ret); 59640b0a749SFilipe Manana return ret; 59740b0a749SFilipe Manana } 59840b0a749SFilipe Manana atomic_inc(&cow->refs); 599240f62c8SChris Mason rcu_assign_pointer(root->node, cow); 600925baeddSChris Mason 6017a163608SFilipe Manana btrfs_free_tree_block(trans, btrfs_root_id(root), buf, 6027a163608SFilipe Manana parent_start, last_ref); 6035f39d397SChris Mason free_extent_buffer(buf); 6040b86a832SChris Mason add_root_to_dirty_list(root); 6056702ed49SChris Mason } else { 6065d4f98a2SYan Zheng WARN_ON(trans->transid != btrfs_header_generation(parent)); 607d09c5152SFilipe Manana ret = btrfs_tree_mod_log_insert_key(parent, parent_slot, 60833cff222SFilipe Manana BTRFS_MOD_LOG_KEY_REPLACE); 609d09c5152SFilipe Manana if (ret) { 610d09c5152SFilipe Manana btrfs_tree_unlock(cow); 611d09c5152SFilipe Manana free_extent_buffer(cow); 612d09c5152SFilipe Manana btrfs_abort_transaction(trans, ret); 613d09c5152SFilipe Manana return ret; 614d09c5152SFilipe Manana } 6155f39d397SChris Mason btrfs_set_node_blockptr(parent, parent_slot, 616db94535dSChris Mason cow->start); 61774493f7aSChris Mason btrfs_set_node_ptr_generation(parent, parent_slot, 61874493f7aSChris Mason trans->transid); 6196702ed49SChris Mason btrfs_mark_buffer_dirty(parent); 6205de865eeSFilipe David Borba Manana if (last_ref) { 621f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_free_eb(buf); 6225de865eeSFilipe David Borba Manana if (ret) { 623572c83acSJosef Bacik btrfs_tree_unlock(cow); 624572c83acSJosef Bacik free_extent_buffer(cow); 62566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 6265de865eeSFilipe David Borba Manana return ret; 6275de865eeSFilipe David Borba Manana } 6285de865eeSFilipe David Borba Manana } 6297a163608SFilipe Manana btrfs_free_tree_block(trans, btrfs_root_id(root), buf, 6307a163608SFilipe Manana parent_start, last_ref); 6316702ed49SChris Mason } 632925baeddSChris Mason if (unlock_orig) 633925baeddSChris Mason btrfs_tree_unlock(buf); 6343083ee2eSJosef Bacik free_extent_buffer_stale(buf); 6356702ed49SChris Mason btrfs_mark_buffer_dirty(cow); 6366702ed49SChris Mason *cow_ret = cow; 6376702ed49SChris Mason return 0; 6386702ed49SChris Mason } 6396702ed49SChris Mason 6405d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans, 6415d4f98a2SYan Zheng struct btrfs_root *root, 6425d4f98a2SYan Zheng struct extent_buffer *buf) 6435d4f98a2SYan Zheng { 644f5ee5c9aSJeff Mahoney if (btrfs_is_testing(root->fs_info)) 645faa2dbf0SJosef Bacik return 0; 646fccb84c9SDavid Sterba 647d1980131SDavid Sterba /* Ensure we can see the FORCE_COW bit */ 648d1980131SDavid Sterba smp_mb__before_atomic(); 649f1ebcc74SLiu Bo 650f1ebcc74SLiu Bo /* 651f1ebcc74SLiu Bo * We do not need to cow a block if 652f1ebcc74SLiu Bo * 1) this block is not created or changed in this transaction; 653f1ebcc74SLiu Bo * 2) this block does not belong to TREE_RELOC tree; 654f1ebcc74SLiu Bo * 3) the root is not forced COW. 655f1ebcc74SLiu Bo * 656f1ebcc74SLiu Bo * What is forced COW: 65701327610SNicholas D Steeves * when we create snapshot during committing the transaction, 65852042d8eSAndrea Gelmini * after we've finished copying src root, we must COW the shared 659f1ebcc74SLiu Bo * block to ensure the metadata consistency. 660f1ebcc74SLiu Bo */ 6615d4f98a2SYan Zheng if (btrfs_header_generation(buf) == trans->transid && 6625d4f98a2SYan Zheng !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && 6635d4f98a2SYan Zheng !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 664f1ebcc74SLiu Bo btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && 66527cdeb70SMiao Xie !test_bit(BTRFS_ROOT_FORCE_COW, &root->state)) 6665d4f98a2SYan Zheng return 0; 6675d4f98a2SYan Zheng return 1; 6685d4f98a2SYan Zheng } 6695d4f98a2SYan Zheng 670d352ac68SChris Mason /* 671d352ac68SChris Mason * cows a single block, see __btrfs_cow_block for the real work. 67201327610SNicholas D Steeves * This version of it has extra checks so that a block isn't COWed more than 673d352ac68SChris Mason * once per transaction, as long as it hasn't been written yet 674d352ac68SChris Mason */ 675d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, 6765f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *buf, 6775f39d397SChris Mason struct extent_buffer *parent, int parent_slot, 6789631e4ccSJosef Bacik struct extent_buffer **cow_ret, 6799631e4ccSJosef Bacik enum btrfs_lock_nesting nest) 68002217ed2SChris Mason { 6810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6826702ed49SChris Mason u64 search_start; 683f510cfecSChris Mason int ret; 684dc17ff8fSChris Mason 68583354f07SJosef Bacik if (test_bit(BTRFS_ROOT_DELETING, &root->state)) 68683354f07SJosef Bacik btrfs_err(fs_info, 68783354f07SJosef Bacik "COW'ing blocks on a fs root that's being dropped"); 68883354f07SJosef Bacik 6890b246afaSJeff Mahoney if (trans->transaction != fs_info->running_transaction) 69031b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 691c1c9ff7cSGeert Uytterhoeven trans->transid, 6920b246afaSJeff Mahoney fs_info->running_transaction->transid); 69331b1a2bdSJulia Lawall 6940b246afaSJeff Mahoney if (trans->transid != fs_info->generation) 69531b1a2bdSJulia Lawall WARN(1, KERN_CRIT "trans %llu running %llu\n", 6960b246afaSJeff Mahoney trans->transid, fs_info->generation); 697dc17ff8fSChris Mason 6985d4f98a2SYan Zheng if (!should_cow_block(trans, root, buf)) { 69902217ed2SChris Mason *cow_ret = buf; 70002217ed2SChris Mason return 0; 70102217ed2SChris Mason } 702c487685dSChris Mason 703ee22184bSByongho Lee search_start = buf->start & ~((u64)SZ_1G - 1); 704b4ce94deSChris Mason 705f616f5cdSQu Wenruo /* 706f616f5cdSQu Wenruo * Before CoWing this block for later modification, check if it's 707f616f5cdSQu Wenruo * the subtree root and do the delayed subtree trace if needed. 708f616f5cdSQu Wenruo * 709f616f5cdSQu Wenruo * Also We don't care about the error, as it's handled internally. 710f616f5cdSQu Wenruo */ 711f616f5cdSQu Wenruo btrfs_qgroup_trace_subtree_after_cow(trans, root, buf); 712f510cfecSChris Mason ret = __btrfs_cow_block(trans, root, buf, parent, 7139631e4ccSJosef Bacik parent_slot, cow_ret, search_start, 0, nest); 7141abe9b8aSliubo 7151abe9b8aSliubo trace_btrfs_cow_block(root, buf, *cow_ret); 7161abe9b8aSliubo 717f510cfecSChris Mason return ret; 7182c90e5d6SChris Mason } 719f75e2b79SJosef Bacik ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO); 7206702ed49SChris Mason 721d352ac68SChris Mason /* 722d352ac68SChris Mason * helper function for defrag to decide if two blocks pointed to by a 723d352ac68SChris Mason * node are actually close by 724d352ac68SChris Mason */ 7256b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 7266702ed49SChris Mason { 7276b80053dSChris Mason if (blocknr < other && other - (blocknr + blocksize) < 32768) 7286702ed49SChris Mason return 1; 7296b80053dSChris Mason if (blocknr > other && blocknr - (other + blocksize) < 32768) 7306702ed49SChris Mason return 1; 73102217ed2SChris Mason return 0; 73202217ed2SChris Mason } 73302217ed2SChris Mason 734ce6ef5abSDavid Sterba #ifdef __LITTLE_ENDIAN 735ce6ef5abSDavid Sterba 736ce6ef5abSDavid Sterba /* 737ce6ef5abSDavid Sterba * Compare two keys, on little-endian the disk order is same as CPU order and 738ce6ef5abSDavid Sterba * we can avoid the conversion. 739ce6ef5abSDavid Sterba */ 740ce6ef5abSDavid Sterba static int comp_keys(const struct btrfs_disk_key *disk_key, 741ce6ef5abSDavid Sterba const struct btrfs_key *k2) 742ce6ef5abSDavid Sterba { 743ce6ef5abSDavid Sterba const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key; 744ce6ef5abSDavid Sterba 745ce6ef5abSDavid Sterba return btrfs_comp_cpu_keys(k1, k2); 746ce6ef5abSDavid Sterba } 747ce6ef5abSDavid Sterba 748ce6ef5abSDavid Sterba #else 749ce6ef5abSDavid Sterba 750081e9573SChris Mason /* 751081e9573SChris Mason * compare two keys in a memcmp fashion 752081e9573SChris Mason */ 753310712b2SOmar Sandoval static int comp_keys(const struct btrfs_disk_key *disk, 754310712b2SOmar Sandoval const struct btrfs_key *k2) 755081e9573SChris Mason { 756081e9573SChris Mason struct btrfs_key k1; 757081e9573SChris Mason 758081e9573SChris Mason btrfs_disk_key_to_cpu(&k1, disk); 759081e9573SChris Mason 76020736abaSDiego Calleja return btrfs_comp_cpu_keys(&k1, k2); 761081e9573SChris Mason } 762ce6ef5abSDavid Sterba #endif 763081e9573SChris Mason 764f3465ca4SJosef Bacik /* 765f3465ca4SJosef Bacik * same as comp_keys only with two btrfs_key's 766f3465ca4SJosef Bacik */ 767e1f60a65SDavid Sterba int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2) 768f3465ca4SJosef Bacik { 769f3465ca4SJosef Bacik if (k1->objectid > k2->objectid) 770f3465ca4SJosef Bacik return 1; 771f3465ca4SJosef Bacik if (k1->objectid < k2->objectid) 772f3465ca4SJosef Bacik return -1; 773f3465ca4SJosef Bacik if (k1->type > k2->type) 774f3465ca4SJosef Bacik return 1; 775f3465ca4SJosef Bacik if (k1->type < k2->type) 776f3465ca4SJosef Bacik return -1; 777f3465ca4SJosef Bacik if (k1->offset > k2->offset) 778f3465ca4SJosef Bacik return 1; 779f3465ca4SJosef Bacik if (k1->offset < k2->offset) 780f3465ca4SJosef Bacik return -1; 781f3465ca4SJosef Bacik return 0; 782f3465ca4SJosef Bacik } 783081e9573SChris Mason 784d352ac68SChris Mason /* 785d352ac68SChris Mason * this is used by the defrag code to go through all the 786d352ac68SChris Mason * leaves pointed to by a node and reallocate them so that 787d352ac68SChris Mason * disk order is close to key order 788d352ac68SChris Mason */ 7896702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans, 7905f39d397SChris Mason struct btrfs_root *root, struct extent_buffer *parent, 791de78b51aSEric Sandeen int start_slot, u64 *last_ret, 792a6b6e75eSChris Mason struct btrfs_key *progress) 7936702ed49SChris Mason { 7940b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 7956b80053dSChris Mason struct extent_buffer *cur; 7966702ed49SChris Mason u64 blocknr; 797e9d0b13bSChris Mason u64 search_start = *last_ret; 798e9d0b13bSChris Mason u64 last_block = 0; 7996702ed49SChris Mason u64 other; 8006702ed49SChris Mason u32 parent_nritems; 8016702ed49SChris Mason int end_slot; 8026702ed49SChris Mason int i; 8036702ed49SChris Mason int err = 0; 8046b80053dSChris Mason u32 blocksize; 805081e9573SChris Mason int progress_passed = 0; 806081e9573SChris Mason struct btrfs_disk_key disk_key; 8076702ed49SChris Mason 8080b246afaSJeff Mahoney WARN_ON(trans->transaction != fs_info->running_transaction); 8090b246afaSJeff Mahoney WARN_ON(trans->transid != fs_info->generation); 81086479a04SChris Mason 8116b80053dSChris Mason parent_nritems = btrfs_header_nritems(parent); 8120b246afaSJeff Mahoney blocksize = fs_info->nodesize; 8135dfe2be7SFilipe Manana end_slot = parent_nritems - 1; 8146702ed49SChris Mason 8155dfe2be7SFilipe Manana if (parent_nritems <= 1) 8166702ed49SChris Mason return 0; 8176702ed49SChris Mason 8185dfe2be7SFilipe Manana for (i = start_slot; i <= end_slot; i++) { 8196702ed49SChris Mason int close = 1; 820a6b6e75eSChris Mason 821081e9573SChris Mason btrfs_node_key(parent, &disk_key, i); 822081e9573SChris Mason if (!progress_passed && comp_keys(&disk_key, progress) < 0) 823081e9573SChris Mason continue; 824081e9573SChris Mason 825081e9573SChris Mason progress_passed = 1; 8266b80053dSChris Mason blocknr = btrfs_node_blockptr(parent, i); 827e9d0b13bSChris Mason if (last_block == 0) 828e9d0b13bSChris Mason last_block = blocknr; 8295708b959SChris Mason 8306702ed49SChris Mason if (i > 0) { 8316b80053dSChris Mason other = btrfs_node_blockptr(parent, i - 1); 8326b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 8336702ed49SChris Mason } 8345dfe2be7SFilipe Manana if (!close && i < end_slot) { 8356b80053dSChris Mason other = btrfs_node_blockptr(parent, i + 1); 8366b80053dSChris Mason close = close_blocks(blocknr, other, blocksize); 8376702ed49SChris Mason } 838e9d0b13bSChris Mason if (close) { 839e9d0b13bSChris Mason last_block = blocknr; 8406702ed49SChris Mason continue; 841e9d0b13bSChris Mason } 8426702ed49SChris Mason 843206983b7SJosef Bacik cur = btrfs_read_node_slot(parent, i); 844206983b7SJosef Bacik if (IS_ERR(cur)) 84564c043deSLiu Bo return PTR_ERR(cur); 846e9d0b13bSChris Mason if (search_start == 0) 8476b80053dSChris Mason search_start = last_block; 848e9d0b13bSChris Mason 849e7a84565SChris Mason btrfs_tree_lock(cur); 8506b80053dSChris Mason err = __btrfs_cow_block(trans, root, cur, parent, i, 851e7a84565SChris Mason &cur, search_start, 8526b80053dSChris Mason min(16 * blocksize, 8539631e4ccSJosef Bacik (end_slot - i) * blocksize), 8549631e4ccSJosef Bacik BTRFS_NESTING_COW); 855252c38f0SYan if (err) { 856e7a84565SChris Mason btrfs_tree_unlock(cur); 8576b80053dSChris Mason free_extent_buffer(cur); 8586702ed49SChris Mason break; 859252c38f0SYan } 860e7a84565SChris Mason search_start = cur->start; 861e7a84565SChris Mason last_block = cur->start; 862f2183bdeSChris Mason *last_ret = search_start; 863e7a84565SChris Mason btrfs_tree_unlock(cur); 864e7a84565SChris Mason free_extent_buffer(cur); 8656702ed49SChris Mason } 8666702ed49SChris Mason return err; 8676702ed49SChris Mason } 8686702ed49SChris Mason 86974123bd7SChris Mason /* 870fb81212cSFilipe Manana * Search for a key in the given extent_buffer. 8715f39d397SChris Mason * 872a724f313SFilipe Manana * The lower boundary for the search is specified by the slot number @first_slot. 873fdf8d595SAnand Jain * Use a value of 0 to search over the whole extent buffer. Works for both 874fdf8d595SAnand Jain * leaves and nodes. 87574123bd7SChris Mason * 876fb81212cSFilipe Manana * The slot in the extent buffer is returned via @slot. If the key exists in the 877fb81212cSFilipe Manana * extent buffer, then @slot will point to the slot where the key is, otherwise 878fb81212cSFilipe Manana * it points to the slot where you would insert the key. 879fb81212cSFilipe Manana * 880fb81212cSFilipe Manana * Slot may point to the total number of items (i.e. one position beyond the last 881fb81212cSFilipe Manana * key) if the key is bigger than the last key in the extent buffer. 88274123bd7SChris Mason */ 883fdf8d595SAnand Jain int btrfs_bin_search(struct extent_buffer *eb, int first_slot, 88467d5e289SMarcos Paulo de Souza const struct btrfs_key *key, int *slot) 885be0e5c09SChris Mason { 886fb81212cSFilipe Manana unsigned long p; 887fb81212cSFilipe Manana int item_size; 888a724f313SFilipe Manana /* 889a724f313SFilipe Manana * Use unsigned types for the low and high slots, so that we get a more 890a724f313SFilipe Manana * efficient division in the search loop below. 891a724f313SFilipe Manana */ 892a724f313SFilipe Manana u32 low = first_slot; 893a724f313SFilipe Manana u32 high = btrfs_header_nritems(eb); 894be0e5c09SChris Mason int ret; 8955cd17f34SDavid Sterba const int key_size = sizeof(struct btrfs_disk_key); 896be0e5c09SChris Mason 897a724f313SFilipe Manana if (unlikely(low > high)) { 8985e24e9afSLiu Bo btrfs_err(eb->fs_info, 899a724f313SFilipe Manana "%s: low (%u) > high (%u) eb %llu owner %llu level %d", 9005e24e9afSLiu Bo __func__, low, high, eb->start, 9015e24e9afSLiu Bo btrfs_header_owner(eb), btrfs_header_level(eb)); 9025e24e9afSLiu Bo return -EINVAL; 9035e24e9afSLiu Bo } 9045e24e9afSLiu Bo 905fb81212cSFilipe Manana if (btrfs_header_level(eb) == 0) { 906fb81212cSFilipe Manana p = offsetof(struct btrfs_leaf, items); 907fb81212cSFilipe Manana item_size = sizeof(struct btrfs_item); 908fb81212cSFilipe Manana } else { 909fb81212cSFilipe Manana p = offsetof(struct btrfs_node, ptrs); 910fb81212cSFilipe Manana item_size = sizeof(struct btrfs_key_ptr); 911fb81212cSFilipe Manana } 912fb81212cSFilipe Manana 913be0e5c09SChris Mason while (low < high) { 9145cd17f34SDavid Sterba unsigned long oip; 9155cd17f34SDavid Sterba unsigned long offset; 9165cd17f34SDavid Sterba struct btrfs_disk_key *tmp; 9175cd17f34SDavid Sterba struct btrfs_disk_key unaligned; 9185cd17f34SDavid Sterba int mid; 9195cd17f34SDavid Sterba 920be0e5c09SChris Mason mid = (low + high) / 2; 9215f39d397SChris Mason offset = p + mid * item_size; 9225cd17f34SDavid Sterba oip = offset_in_page(offset); 9235f39d397SChris Mason 9245cd17f34SDavid Sterba if (oip + key_size <= PAGE_SIZE) { 925884b07d0SQu Wenruo const unsigned long idx = get_eb_page_index(offset); 9265cd17f34SDavid Sterba char *kaddr = page_address(eb->pages[idx]); 927934d375bSChris Mason 928884b07d0SQu Wenruo oip = get_eb_offset_in_page(eb, offset); 9295cd17f34SDavid Sterba tmp = (struct btrfs_disk_key *)(kaddr + oip); 9305cd17f34SDavid Sterba } else { 9315cd17f34SDavid Sterba read_extent_buffer(eb, &unaligned, offset, key_size); 9325f39d397SChris Mason tmp = &unaligned; 933479965d6SChris Mason } 934479965d6SChris Mason 935be0e5c09SChris Mason ret = comp_keys(tmp, key); 936be0e5c09SChris Mason 937be0e5c09SChris Mason if (ret < 0) 938be0e5c09SChris Mason low = mid + 1; 939be0e5c09SChris Mason else if (ret > 0) 940be0e5c09SChris Mason high = mid; 941be0e5c09SChris Mason else { 942be0e5c09SChris Mason *slot = mid; 943be0e5c09SChris Mason return 0; 944be0e5c09SChris Mason } 945be0e5c09SChris Mason } 946be0e5c09SChris Mason *slot = low; 947be0e5c09SChris Mason return 1; 948be0e5c09SChris Mason } 949be0e5c09SChris Mason 950f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size) 951f0486c68SYan, Zheng { 952f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 953f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 954f0486c68SYan, Zheng btrfs_root_used(&root->root_item) + size); 955f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 956f0486c68SYan, Zheng } 957f0486c68SYan, Zheng 958f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size) 959f0486c68SYan, Zheng { 960f0486c68SYan, Zheng spin_lock(&root->accounting_lock); 961f0486c68SYan, Zheng btrfs_set_root_used(&root->root_item, 962f0486c68SYan, Zheng btrfs_root_used(&root->root_item) - size); 963f0486c68SYan, Zheng spin_unlock(&root->accounting_lock); 964f0486c68SYan, Zheng } 965f0486c68SYan, Zheng 966d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to. The 967d352ac68SChris Mason * extent buffer is returned with a reference taken (but unlocked). 968d352ac68SChris Mason */ 9694b231ae4SDavid Sterba struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent, 9704b231ae4SDavid Sterba int slot) 971bb803951SChris Mason { 972ca7a79adSChris Mason int level = btrfs_header_level(parent); 973789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 974416bc658SJosef Bacik struct extent_buffer *eb; 975416bc658SJosef Bacik 976fb770ae4SLiu Bo if (slot < 0 || slot >= btrfs_header_nritems(parent)) 977fb770ae4SLiu Bo return ERR_PTR(-ENOENT); 978ca7a79adSChris Mason 979d4694728SJosef Bacik ASSERT(level); 980ca7a79adSChris Mason 981789d6a3aSQu Wenruo check.level = level - 1; 982789d6a3aSQu Wenruo check.transid = btrfs_node_ptr_generation(parent, slot); 983789d6a3aSQu Wenruo check.owner_root = btrfs_header_owner(parent); 984789d6a3aSQu Wenruo check.has_first_key = true; 985789d6a3aSQu Wenruo btrfs_node_key_to_cpu(parent, &check.first_key, slot); 986789d6a3aSQu Wenruo 987d0d20b0fSDavid Sterba eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot), 988789d6a3aSQu Wenruo &check); 9894eb150d6SQu Wenruo if (IS_ERR(eb)) 9904eb150d6SQu Wenruo return eb; 9914eb150d6SQu Wenruo if (!extent_buffer_uptodate(eb)) { 992416bc658SJosef Bacik free_extent_buffer(eb); 9934eb150d6SQu Wenruo return ERR_PTR(-EIO); 994416bc658SJosef Bacik } 995416bc658SJosef Bacik 996416bc658SJosef Bacik return eb; 997bb803951SChris Mason } 998bb803951SChris Mason 999d352ac68SChris Mason /* 1000d352ac68SChris Mason * node level balancing, used to make sure nodes are in proper order for 1001d352ac68SChris Mason * item deletion. We balance from the top down, so we have to make sure 1002d352ac68SChris Mason * that a deletion won't leave an node completely empty later on. 1003d352ac68SChris Mason */ 1004e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans, 100598ed5174SChris Mason struct btrfs_root *root, 100698ed5174SChris Mason struct btrfs_path *path, int level) 1007bb803951SChris Mason { 10080b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10095f39d397SChris Mason struct extent_buffer *right = NULL; 10105f39d397SChris Mason struct extent_buffer *mid; 10115f39d397SChris Mason struct extent_buffer *left = NULL; 10125f39d397SChris Mason struct extent_buffer *parent = NULL; 1013bb803951SChris Mason int ret = 0; 1014bb803951SChris Mason int wret; 1015bb803951SChris Mason int pslot; 1016bb803951SChris Mason int orig_slot = path->slots[level]; 101779f95c82SChris Mason u64 orig_ptr; 1018bb803951SChris Mason 101998e6b1ebSLiu Bo ASSERT(level > 0); 1020bb803951SChris Mason 10215f39d397SChris Mason mid = path->nodes[level]; 1022b4ce94deSChris Mason 1023ac5887c8SJosef Bacik WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK); 10247bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 10257bb86316SChris Mason 10261d4f8a0cSChris Mason orig_ptr = btrfs_node_blockptr(mid, orig_slot); 102779f95c82SChris Mason 1028a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 10295f39d397SChris Mason parent = path->nodes[level + 1]; 1030bb803951SChris Mason pslot = path->slots[level + 1]; 1031a05a9bb1SLi Zefan } 1032bb803951SChris Mason 103340689478SChris Mason /* 103440689478SChris Mason * deal with the case where there is only one pointer in the root 103540689478SChris Mason * by promoting the node below to a root 103640689478SChris Mason */ 10375f39d397SChris Mason if (!parent) { 10385f39d397SChris Mason struct extent_buffer *child; 1039bb803951SChris Mason 10405f39d397SChris Mason if (btrfs_header_nritems(mid) != 1) 1041bb803951SChris Mason return 0; 1042bb803951SChris Mason 1043bb803951SChris Mason /* promote the child to a root */ 10444b231ae4SDavid Sterba child = btrfs_read_node_slot(mid, 0); 1045fb770ae4SLiu Bo if (IS_ERR(child)) { 1046fb770ae4SLiu Bo ret = PTR_ERR(child); 1047daefe4d4SFilipe Manana goto out; 1048305a26afSMark Fasheh } 1049305a26afSMark Fasheh 1050925baeddSChris Mason btrfs_tree_lock(child); 10519631e4ccSJosef Bacik ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 10529631e4ccSJosef Bacik BTRFS_NESTING_COW); 1053f0486c68SYan, Zheng if (ret) { 1054f0486c68SYan, Zheng btrfs_tree_unlock(child); 1055f0486c68SYan, Zheng free_extent_buffer(child); 1056daefe4d4SFilipe Manana goto out; 1057f0486c68SYan, Zheng } 10582f375ab9SYan 1059406808abSFilipe Manana ret = btrfs_tree_mod_log_insert_root(root->node, child, true); 106039020d8aSFilipe Manana if (ret < 0) { 106139020d8aSFilipe Manana btrfs_tree_unlock(child); 106239020d8aSFilipe Manana free_extent_buffer(child); 106339020d8aSFilipe Manana btrfs_abort_transaction(trans, ret); 1064daefe4d4SFilipe Manana goto out; 106539020d8aSFilipe Manana } 1066240f62c8SChris Mason rcu_assign_pointer(root->node, child); 1067925baeddSChris Mason 10680b86a832SChris Mason add_root_to_dirty_list(root); 1069925baeddSChris Mason btrfs_tree_unlock(child); 1070b4ce94deSChris Mason 1071925baeddSChris Mason path->locks[level] = 0; 1072bb803951SChris Mason path->nodes[level] = NULL; 1073190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, mid); 1074925baeddSChris Mason btrfs_tree_unlock(mid); 1075bb803951SChris Mason /* once for the path */ 10765f39d397SChris Mason free_extent_buffer(mid); 1077f0486c68SYan, Zheng 1078f0486c68SYan, Zheng root_sub_used(root, mid->len); 10797a163608SFilipe Manana btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1); 1080bb803951SChris Mason /* once for the root ptr */ 10813083ee2eSJosef Bacik free_extent_buffer_stale(mid); 1082f0486c68SYan, Zheng return 0; 1083bb803951SChris Mason } 10845f39d397SChris Mason if (btrfs_header_nritems(mid) > 10850b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) 1086bb803951SChris Mason return 0; 1087bb803951SChris Mason 10889cf14029SJosef Bacik if (pslot) { 10894b231ae4SDavid Sterba left = btrfs_read_node_slot(parent, pslot - 1); 10909cf14029SJosef Bacik if (IS_ERR(left)) { 10919cf14029SJosef Bacik ret = PTR_ERR(left); 1092fb770ae4SLiu Bo left = NULL; 1093daefe4d4SFilipe Manana goto out; 10949cf14029SJosef Bacik } 1095fb770ae4SLiu Bo 1096bf77467aSJosef Bacik __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); 10975f39d397SChris Mason wret = btrfs_cow_block(trans, root, left, 10989631e4ccSJosef Bacik parent, pslot - 1, &left, 1099bf59a5a2SJosef Bacik BTRFS_NESTING_LEFT_COW); 110054aa1f4dSChris Mason if (wret) { 110154aa1f4dSChris Mason ret = wret; 1102daefe4d4SFilipe Manana goto out; 110354aa1f4dSChris Mason } 11042cc58cf2SChris Mason } 1105fb770ae4SLiu Bo 11069cf14029SJosef Bacik if (pslot + 1 < btrfs_header_nritems(parent)) { 11074b231ae4SDavid Sterba right = btrfs_read_node_slot(parent, pslot + 1); 11089cf14029SJosef Bacik if (IS_ERR(right)) { 11099cf14029SJosef Bacik ret = PTR_ERR(right); 1110fb770ae4SLiu Bo right = NULL; 1111daefe4d4SFilipe Manana goto out; 11129cf14029SJosef Bacik } 1113fb770ae4SLiu Bo 1114bf77467aSJosef Bacik __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); 11155f39d397SChris Mason wret = btrfs_cow_block(trans, root, right, 11169631e4ccSJosef Bacik parent, pslot + 1, &right, 1117bf59a5a2SJosef Bacik BTRFS_NESTING_RIGHT_COW); 11182cc58cf2SChris Mason if (wret) { 11192cc58cf2SChris Mason ret = wret; 1120daefe4d4SFilipe Manana goto out; 11212cc58cf2SChris Mason } 11222cc58cf2SChris Mason } 11232cc58cf2SChris Mason 11242cc58cf2SChris Mason /* first, try to make some room in the middle buffer */ 11255f39d397SChris Mason if (left) { 11265f39d397SChris Mason orig_slot += btrfs_header_nritems(left); 1127d30a668fSDavid Sterba wret = push_node_left(trans, left, mid, 1); 112879f95c82SChris Mason if (wret < 0) 112979f95c82SChris Mason ret = wret; 1130bb803951SChris Mason } 113179f95c82SChris Mason 113279f95c82SChris Mason /* 113379f95c82SChris Mason * then try to empty the right most buffer into the middle 113479f95c82SChris Mason */ 11355f39d397SChris Mason if (right) { 1136d30a668fSDavid Sterba wret = push_node_left(trans, mid, right, 1); 113754aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 113879f95c82SChris Mason ret = wret; 11395f39d397SChris Mason if (btrfs_header_nritems(right) == 0) { 1140190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, right); 1141925baeddSChris Mason btrfs_tree_unlock(right); 1142016f9d0bSJosef Bacik btrfs_del_ptr(root, path, level + 1, pslot + 1); 1143f0486c68SYan, Zheng root_sub_used(root, right->len); 11447a163608SFilipe Manana btrfs_free_tree_block(trans, btrfs_root_id(root), right, 11457a163608SFilipe Manana 0, 1); 11463083ee2eSJosef Bacik free_extent_buffer_stale(right); 1147f0486c68SYan, Zheng right = NULL; 1148bb803951SChris Mason } else { 11495f39d397SChris Mason struct btrfs_disk_key right_key; 11505f39d397SChris Mason btrfs_node_key(right, &right_key, 0); 1151f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1, 115233cff222SFilipe Manana BTRFS_MOD_LOG_KEY_REPLACE); 115339020d8aSFilipe Manana if (ret < 0) { 115439020d8aSFilipe Manana btrfs_abort_transaction(trans, ret); 1155daefe4d4SFilipe Manana goto out; 115639020d8aSFilipe Manana } 11575f39d397SChris Mason btrfs_set_node_key(parent, &right_key, pslot + 1); 11585f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 1159bb803951SChris Mason } 1160bb803951SChris Mason } 11615f39d397SChris Mason if (btrfs_header_nritems(mid) == 1) { 116279f95c82SChris Mason /* 116379f95c82SChris Mason * we're not allowed to leave a node with one item in the 116479f95c82SChris Mason * tree during a delete. A deletion from lower in the tree 116579f95c82SChris Mason * could try to delete the only pointer in this node. 116679f95c82SChris Mason * So, pull some keys from the left. 116779f95c82SChris Mason * There has to be a left pointer at this point because 116879f95c82SChris Mason * otherwise we would have pulled some pointers from the 116979f95c82SChris Mason * right 117079f95c82SChris Mason */ 1171725026edSFilipe Manana if (unlikely(!left)) { 1172725026edSFilipe Manana btrfs_crit(fs_info, 1173725026edSFilipe Manana "missing left child when middle child only has 1 item, parent bytenr %llu level %d mid bytenr %llu root %llu", 1174725026edSFilipe Manana parent->start, btrfs_header_level(parent), 1175725026edSFilipe Manana mid->start, btrfs_root_id(root)); 1176725026edSFilipe Manana ret = -EUCLEAN; 1177725026edSFilipe Manana btrfs_abort_transaction(trans, ret); 1178daefe4d4SFilipe Manana goto out; 1179305a26afSMark Fasheh } 118055d32ed8SDavid Sterba wret = balance_node_right(trans, mid, left); 118154aa1f4dSChris Mason if (wret < 0) { 118279f95c82SChris Mason ret = wret; 1183daefe4d4SFilipe Manana goto out; 118454aa1f4dSChris Mason } 1185bce4eae9SChris Mason if (wret == 1) { 1186d30a668fSDavid Sterba wret = push_node_left(trans, left, mid, 1); 1187bce4eae9SChris Mason if (wret < 0) 1188bce4eae9SChris Mason ret = wret; 1189bce4eae9SChris Mason } 119079f95c82SChris Mason BUG_ON(wret == 1); 119179f95c82SChris Mason } 11925f39d397SChris Mason if (btrfs_header_nritems(mid) == 0) { 1193190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, mid); 1194925baeddSChris Mason btrfs_tree_unlock(mid); 1195016f9d0bSJosef Bacik btrfs_del_ptr(root, path, level + 1, pslot); 1196f0486c68SYan, Zheng root_sub_used(root, mid->len); 11977a163608SFilipe Manana btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1); 11983083ee2eSJosef Bacik free_extent_buffer_stale(mid); 1199f0486c68SYan, Zheng mid = NULL; 120079f95c82SChris Mason } else { 120179f95c82SChris Mason /* update the parent key to reflect our changes */ 12025f39d397SChris Mason struct btrfs_disk_key mid_key; 12035f39d397SChris Mason btrfs_node_key(mid, &mid_key, 0); 1204f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_key(parent, pslot, 120533cff222SFilipe Manana BTRFS_MOD_LOG_KEY_REPLACE); 120639020d8aSFilipe Manana if (ret < 0) { 120739020d8aSFilipe Manana btrfs_abort_transaction(trans, ret); 1208daefe4d4SFilipe Manana goto out; 120939020d8aSFilipe Manana } 12105f39d397SChris Mason btrfs_set_node_key(parent, &mid_key, pslot); 12115f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 121279f95c82SChris Mason } 1213bb803951SChris Mason 121479f95c82SChris Mason /* update the path */ 12155f39d397SChris Mason if (left) { 12165f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 121767439dadSDavid Sterba atomic_inc(&left->refs); 1218925baeddSChris Mason /* left was locked after cow */ 12195f39d397SChris Mason path->nodes[level] = left; 1220bb803951SChris Mason path->slots[level + 1] -= 1; 1221bb803951SChris Mason path->slots[level] = orig_slot; 1222925baeddSChris Mason if (mid) { 1223925baeddSChris Mason btrfs_tree_unlock(mid); 12245f39d397SChris Mason free_extent_buffer(mid); 1225925baeddSChris Mason } 1226bb803951SChris Mason } else { 12275f39d397SChris Mason orig_slot -= btrfs_header_nritems(left); 1228bb803951SChris Mason path->slots[level] = orig_slot; 1229bb803951SChris Mason } 1230bb803951SChris Mason } 123179f95c82SChris Mason /* double check we haven't messed things up */ 1232e20d96d6SChris Mason if (orig_ptr != 12335f39d397SChris Mason btrfs_node_blockptr(path->nodes[level], path->slots[level])) 123479f95c82SChris Mason BUG(); 1235daefe4d4SFilipe Manana out: 1236925baeddSChris Mason if (right) { 1237925baeddSChris Mason btrfs_tree_unlock(right); 12385f39d397SChris Mason free_extent_buffer(right); 1239925baeddSChris Mason } 1240925baeddSChris Mason if (left) { 1241925baeddSChris Mason if (path->nodes[level] != left) 1242925baeddSChris Mason btrfs_tree_unlock(left); 12435f39d397SChris Mason free_extent_buffer(left); 1244925baeddSChris Mason } 1245bb803951SChris Mason return ret; 1246bb803951SChris Mason } 1247bb803951SChris Mason 1248d352ac68SChris Mason /* Node balancing for insertion. Here we only split or push nodes around 1249d352ac68SChris Mason * when they are completely full. This is also done top down, so we 1250d352ac68SChris Mason * have to be pessimistic. 1251d352ac68SChris Mason */ 1252d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, 1253e66f709bSChris Mason struct btrfs_root *root, 1254e66f709bSChris Mason struct btrfs_path *path, int level) 1255e66f709bSChris Mason { 12560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 12575f39d397SChris Mason struct extent_buffer *right = NULL; 12585f39d397SChris Mason struct extent_buffer *mid; 12595f39d397SChris Mason struct extent_buffer *left = NULL; 12605f39d397SChris Mason struct extent_buffer *parent = NULL; 1261e66f709bSChris Mason int ret = 0; 1262e66f709bSChris Mason int wret; 1263e66f709bSChris Mason int pslot; 1264e66f709bSChris Mason int orig_slot = path->slots[level]; 1265e66f709bSChris Mason 1266e66f709bSChris Mason if (level == 0) 1267e66f709bSChris Mason return 1; 1268e66f709bSChris Mason 12695f39d397SChris Mason mid = path->nodes[level]; 12707bb86316SChris Mason WARN_ON(btrfs_header_generation(mid) != trans->transid); 1271e66f709bSChris Mason 1272a05a9bb1SLi Zefan if (level < BTRFS_MAX_LEVEL - 1) { 12735f39d397SChris Mason parent = path->nodes[level + 1]; 1274e66f709bSChris Mason pslot = path->slots[level + 1]; 1275a05a9bb1SLi Zefan } 1276e66f709bSChris Mason 12775f39d397SChris Mason if (!parent) 1278e66f709bSChris Mason return 1; 1279e66f709bSChris Mason 12809cf14029SJosef Bacik /* first, try to make some room in the middle buffer */ 12819cf14029SJosef Bacik if (pslot) { 12829cf14029SJosef Bacik u32 left_nr; 12839cf14029SJosef Bacik 12844b231ae4SDavid Sterba left = btrfs_read_node_slot(parent, pslot - 1); 1285fb770ae4SLiu Bo if (IS_ERR(left)) 12869cf14029SJosef Bacik return PTR_ERR(left); 1287925baeddSChris Mason 1288bf77467aSJosef Bacik __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); 1289b4ce94deSChris Mason 12905f39d397SChris Mason left_nr = btrfs_header_nritems(left); 12910b246afaSJeff Mahoney if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { 129233ade1f8SChris Mason wret = 1; 129333ade1f8SChris Mason } else { 12945f39d397SChris Mason ret = btrfs_cow_block(trans, root, left, parent, 12959631e4ccSJosef Bacik pslot - 1, &left, 1296bf59a5a2SJosef Bacik BTRFS_NESTING_LEFT_COW); 129754aa1f4dSChris Mason if (ret) 129854aa1f4dSChris Mason wret = 1; 129954aa1f4dSChris Mason else { 1300d30a668fSDavid Sterba wret = push_node_left(trans, left, mid, 0); 130154aa1f4dSChris Mason } 130233ade1f8SChris Mason } 1303e66f709bSChris Mason if (wret < 0) 1304e66f709bSChris Mason ret = wret; 1305e66f709bSChris Mason if (wret == 0) { 13065f39d397SChris Mason struct btrfs_disk_key disk_key; 1307e66f709bSChris Mason orig_slot += left_nr; 13085f39d397SChris Mason btrfs_node_key(mid, &disk_key, 0); 1309f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_key(parent, pslot, 131033cff222SFilipe Manana BTRFS_MOD_LOG_KEY_REPLACE); 131111d6ae03SFilipe Manana if (ret < 0) { 131211d6ae03SFilipe Manana btrfs_tree_unlock(left); 131311d6ae03SFilipe Manana free_extent_buffer(left); 131411d6ae03SFilipe Manana btrfs_abort_transaction(trans, ret); 131511d6ae03SFilipe Manana return ret; 131611d6ae03SFilipe Manana } 13175f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot); 13185f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 13195f39d397SChris Mason if (btrfs_header_nritems(left) > orig_slot) { 13205f39d397SChris Mason path->nodes[level] = left; 1321e66f709bSChris Mason path->slots[level + 1] -= 1; 1322e66f709bSChris Mason path->slots[level] = orig_slot; 1323925baeddSChris Mason btrfs_tree_unlock(mid); 13245f39d397SChris Mason free_extent_buffer(mid); 1325e66f709bSChris Mason } else { 1326e66f709bSChris Mason orig_slot -= 13275f39d397SChris Mason btrfs_header_nritems(left); 1328e66f709bSChris Mason path->slots[level] = orig_slot; 1329925baeddSChris Mason btrfs_tree_unlock(left); 13305f39d397SChris Mason free_extent_buffer(left); 1331e66f709bSChris Mason } 1332e66f709bSChris Mason return 0; 1333e66f709bSChris Mason } 1334925baeddSChris Mason btrfs_tree_unlock(left); 13355f39d397SChris Mason free_extent_buffer(left); 1336e66f709bSChris Mason } 1337e66f709bSChris Mason 1338e66f709bSChris Mason /* 1339e66f709bSChris Mason * then try to empty the right most buffer into the middle 1340e66f709bSChris Mason */ 13419cf14029SJosef Bacik if (pslot + 1 < btrfs_header_nritems(parent)) { 134233ade1f8SChris Mason u32 right_nr; 1343b4ce94deSChris Mason 13449cf14029SJosef Bacik right = btrfs_read_node_slot(parent, pslot + 1); 13459cf14029SJosef Bacik if (IS_ERR(right)) 13469cf14029SJosef Bacik return PTR_ERR(right); 13479cf14029SJosef Bacik 1348bf77467aSJosef Bacik __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); 1349b4ce94deSChris Mason 13505f39d397SChris Mason right_nr = btrfs_header_nritems(right); 13510b246afaSJeff Mahoney if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { 135233ade1f8SChris Mason wret = 1; 135333ade1f8SChris Mason } else { 13545f39d397SChris Mason ret = btrfs_cow_block(trans, root, right, 13555f39d397SChris Mason parent, pslot + 1, 1356bf59a5a2SJosef Bacik &right, BTRFS_NESTING_RIGHT_COW); 135754aa1f4dSChris Mason if (ret) 135854aa1f4dSChris Mason wret = 1; 135954aa1f4dSChris Mason else { 136055d32ed8SDavid Sterba wret = balance_node_right(trans, right, mid); 136133ade1f8SChris Mason } 136254aa1f4dSChris Mason } 1363e66f709bSChris Mason if (wret < 0) 1364e66f709bSChris Mason ret = wret; 1365e66f709bSChris Mason if (wret == 0) { 13665f39d397SChris Mason struct btrfs_disk_key disk_key; 13675f39d397SChris Mason 13685f39d397SChris Mason btrfs_node_key(right, &disk_key, 0); 1369f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1, 137033cff222SFilipe Manana BTRFS_MOD_LOG_KEY_REPLACE); 137111d6ae03SFilipe Manana if (ret < 0) { 137211d6ae03SFilipe Manana btrfs_tree_unlock(right); 137311d6ae03SFilipe Manana free_extent_buffer(right); 137411d6ae03SFilipe Manana btrfs_abort_transaction(trans, ret); 137511d6ae03SFilipe Manana return ret; 137611d6ae03SFilipe Manana } 13775f39d397SChris Mason btrfs_set_node_key(parent, &disk_key, pslot + 1); 13785f39d397SChris Mason btrfs_mark_buffer_dirty(parent); 13795f39d397SChris Mason 13805f39d397SChris Mason if (btrfs_header_nritems(mid) <= orig_slot) { 13815f39d397SChris Mason path->nodes[level] = right; 1382e66f709bSChris Mason path->slots[level + 1] += 1; 1383e66f709bSChris Mason path->slots[level] = orig_slot - 13845f39d397SChris Mason btrfs_header_nritems(mid); 1385925baeddSChris Mason btrfs_tree_unlock(mid); 13865f39d397SChris Mason free_extent_buffer(mid); 1387e66f709bSChris Mason } else { 1388925baeddSChris Mason btrfs_tree_unlock(right); 13895f39d397SChris Mason free_extent_buffer(right); 1390e66f709bSChris Mason } 1391e66f709bSChris Mason return 0; 1392e66f709bSChris Mason } 1393925baeddSChris Mason btrfs_tree_unlock(right); 13945f39d397SChris Mason free_extent_buffer(right); 1395e66f709bSChris Mason } 1396e66f709bSChris Mason return 1; 1397e66f709bSChris Mason } 1398e66f709bSChris Mason 139974123bd7SChris Mason /* 1400d352ac68SChris Mason * readahead one full node of leaves, finding things that are close 1401d352ac68SChris Mason * to the block in 'slot', and triggering ra on them. 14023c69faecSChris Mason */ 14032ff7e61eSJeff Mahoney static void reada_for_search(struct btrfs_fs_info *fs_info, 1404e02119d5SChris Mason struct btrfs_path *path, 140501f46658SChris Mason int level, int slot, u64 objectid) 14063c69faecSChris Mason { 14075f39d397SChris Mason struct extent_buffer *node; 140801f46658SChris Mason struct btrfs_disk_key disk_key; 14093c69faecSChris Mason u32 nritems; 14103c69faecSChris Mason u64 search; 1411a7175319SChris Mason u64 target; 14126b80053dSChris Mason u64 nread = 0; 1413ace75066SFilipe Manana u64 nread_max; 14146b80053dSChris Mason u32 nr; 14156b80053dSChris Mason u32 blocksize; 14166b80053dSChris Mason u32 nscan = 0; 1417db94535dSChris Mason 1418ace75066SFilipe Manana if (level != 1 && path->reada != READA_FORWARD_ALWAYS) 14193c69faecSChris Mason return; 14203c69faecSChris Mason 14216702ed49SChris Mason if (!path->nodes[level]) 14226702ed49SChris Mason return; 14236702ed49SChris Mason 14245f39d397SChris Mason node = path->nodes[level]; 1425925baeddSChris Mason 1426ace75066SFilipe Manana /* 1427ace75066SFilipe Manana * Since the time between visiting leaves is much shorter than the time 1428ace75066SFilipe Manana * between visiting nodes, limit read ahead of nodes to 1, to avoid too 1429ace75066SFilipe Manana * much IO at once (possibly random). 1430ace75066SFilipe Manana */ 1431ace75066SFilipe Manana if (path->reada == READA_FORWARD_ALWAYS) { 1432ace75066SFilipe Manana if (level > 1) 1433ace75066SFilipe Manana nread_max = node->fs_info->nodesize; 1434ace75066SFilipe Manana else 1435ace75066SFilipe Manana nread_max = SZ_128K; 1436ace75066SFilipe Manana } else { 1437ace75066SFilipe Manana nread_max = SZ_64K; 1438ace75066SFilipe Manana } 1439ace75066SFilipe Manana 14403c69faecSChris Mason search = btrfs_node_blockptr(node, slot); 14410b246afaSJeff Mahoney blocksize = fs_info->nodesize; 1442069a2e37SFilipe Manana if (path->reada != READA_FORWARD_ALWAYS) { 1443069a2e37SFilipe Manana struct extent_buffer *eb; 1444069a2e37SFilipe Manana 14450b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, search); 14465f39d397SChris Mason if (eb) { 14475f39d397SChris Mason free_extent_buffer(eb); 14483c69faecSChris Mason return; 14493c69faecSChris Mason } 1450069a2e37SFilipe Manana } 14513c69faecSChris Mason 1452a7175319SChris Mason target = search; 14536b80053dSChris Mason 14545f39d397SChris Mason nritems = btrfs_header_nritems(node); 14556b80053dSChris Mason nr = slot; 145625b8b936SJosef Bacik 14573c69faecSChris Mason while (1) { 1458e4058b54SDavid Sterba if (path->reada == READA_BACK) { 14596b80053dSChris Mason if (nr == 0) 14603c69faecSChris Mason break; 14616b80053dSChris Mason nr--; 1462ace75066SFilipe Manana } else if (path->reada == READA_FORWARD || 1463ace75066SFilipe Manana path->reada == READA_FORWARD_ALWAYS) { 14646b80053dSChris Mason nr++; 14656b80053dSChris Mason if (nr >= nritems) 14666b80053dSChris Mason break; 14673c69faecSChris Mason } 1468e4058b54SDavid Sterba if (path->reada == READA_BACK && objectid) { 146901f46658SChris Mason btrfs_node_key(node, &disk_key, nr); 147001f46658SChris Mason if (btrfs_disk_key_objectid(&disk_key) != objectid) 147101f46658SChris Mason break; 147201f46658SChris Mason } 14736b80053dSChris Mason search = btrfs_node_blockptr(node, nr); 1474ace75066SFilipe Manana if (path->reada == READA_FORWARD_ALWAYS || 1475ace75066SFilipe Manana (search <= target && target - search <= 65536) || 1476a7175319SChris Mason (search > target && search - target <= 65536)) { 1477bfb484d9SJosef Bacik btrfs_readahead_node_child(node, nr); 14786b80053dSChris Mason nread += blocksize; 14793c69faecSChris Mason } 14806b80053dSChris Mason nscan++; 1481ace75066SFilipe Manana if (nread > nread_max || nscan > 32) 14826b80053dSChris Mason break; 14833c69faecSChris Mason } 14843c69faecSChris Mason } 1485925baeddSChris Mason 1486bfb484d9SJosef Bacik static noinline void reada_for_balance(struct btrfs_path *path, int level) 1487b4ce94deSChris Mason { 1488bfb484d9SJosef Bacik struct extent_buffer *parent; 1489b4ce94deSChris Mason int slot; 1490b4ce94deSChris Mason int nritems; 1491b4ce94deSChris Mason 14928c594ea8SChris Mason parent = path->nodes[level + 1]; 1493b4ce94deSChris Mason if (!parent) 14940b08851fSJosef Bacik return; 1495b4ce94deSChris Mason 1496b4ce94deSChris Mason nritems = btrfs_header_nritems(parent); 14978c594ea8SChris Mason slot = path->slots[level + 1]; 1498b4ce94deSChris Mason 1499bfb484d9SJosef Bacik if (slot > 0) 1500bfb484d9SJosef Bacik btrfs_readahead_node_child(parent, slot - 1); 1501bfb484d9SJosef Bacik if (slot + 1 < nritems) 1502bfb484d9SJosef Bacik btrfs_readahead_node_child(parent, slot + 1); 1503b4ce94deSChris Mason } 1504b4ce94deSChris Mason 1505b4ce94deSChris Mason 1506b4ce94deSChris Mason /* 1507d397712bSChris Mason * when we walk down the tree, it is usually safe to unlock the higher layers 1508d397712bSChris Mason * in the tree. The exceptions are when our path goes through slot 0, because 1509d397712bSChris Mason * operations on the tree might require changing key pointers higher up in the 1510d397712bSChris Mason * tree. 1511d352ac68SChris Mason * 1512d397712bSChris Mason * callers might also have set path->keep_locks, which tells this code to keep 1513d397712bSChris Mason * the lock if the path points to the last slot in the block. This is part of 1514d397712bSChris Mason * walking through the tree, and selecting the next slot in the higher block. 1515d352ac68SChris Mason * 1516d397712bSChris Mason * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so 1517d397712bSChris Mason * if lowest_unlock is 1, level 0 won't be unlocked 1518d352ac68SChris Mason */ 1519e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level, 1520f7c79f30SChris Mason int lowest_unlock, int min_write_lock_level, 1521f7c79f30SChris Mason int *write_lock_level) 1522925baeddSChris Mason { 1523925baeddSChris Mason int i; 1524925baeddSChris Mason int skip_level = level; 1525c1227996SNikolay Borisov bool check_skip = true; 1526925baeddSChris Mason 1527925baeddSChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1528925baeddSChris Mason if (!path->nodes[i]) 1529925baeddSChris Mason break; 1530925baeddSChris Mason if (!path->locks[i]) 1531925baeddSChris Mason break; 1532c1227996SNikolay Borisov 1533c1227996SNikolay Borisov if (check_skip) { 1534c1227996SNikolay Borisov if (path->slots[i] == 0) { 1535925baeddSChris Mason skip_level = i + 1; 1536925baeddSChris Mason continue; 1537925baeddSChris Mason } 1538c1227996SNikolay Borisov 1539c1227996SNikolay Borisov if (path->keep_locks) { 1540925baeddSChris Mason u32 nritems; 1541c1227996SNikolay Borisov 1542c1227996SNikolay Borisov nritems = btrfs_header_nritems(path->nodes[i]); 1543051e1b9fSChris Mason if (nritems < 1 || path->slots[i] >= nritems - 1) { 1544925baeddSChris Mason skip_level = i + 1; 1545925baeddSChris Mason continue; 1546925baeddSChris Mason } 1547925baeddSChris Mason } 1548c1227996SNikolay Borisov } 1549051e1b9fSChris Mason 1550d80bb3f9SLiu Bo if (i >= lowest_unlock && i > skip_level) { 1551c1227996SNikolay Borisov check_skip = false; 1552c1227996SNikolay Borisov btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); 1553925baeddSChris Mason path->locks[i] = 0; 1554f7c79f30SChris Mason if (write_lock_level && 1555f7c79f30SChris Mason i > min_write_lock_level && 1556f7c79f30SChris Mason i <= *write_lock_level) { 1557f7c79f30SChris Mason *write_lock_level = i - 1; 1558f7c79f30SChris Mason } 1559925baeddSChris Mason } 1560925baeddSChris Mason } 1561925baeddSChris Mason } 1562925baeddSChris Mason 15633c69faecSChris Mason /* 1564376a21d7SFilipe Manana * Helper function for btrfs_search_slot() and other functions that do a search 1565376a21d7SFilipe Manana * on a btree. The goal is to find a tree block in the cache (the radix tree at 1566376a21d7SFilipe Manana * fs_info->buffer_radix), but if we can't find it, or it's not up to date, read 1567376a21d7SFilipe Manana * its pages from disk. 1568c8c42864SChris Mason * 1569376a21d7SFilipe Manana * Returns -EAGAIN, with the path unlocked, if the caller needs to repeat the 1570376a21d7SFilipe Manana * whole btree search, starting again from the current root node. 1571c8c42864SChris Mason */ 1572c8c42864SChris Mason static int 1573d07b8528SLiu Bo read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, 1574c8c42864SChris Mason struct extent_buffer **eb_ret, int level, int slot, 1575cda79c54SDavid Sterba const struct btrfs_key *key) 1576c8c42864SChris Mason { 15770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1578789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 1579c8c42864SChris Mason u64 blocknr; 1580c8c42864SChris Mason u64 gen; 1581c8c42864SChris Mason struct extent_buffer *tmp; 158276a05b35SChris Mason int ret; 1583581c1760SQu Wenruo int parent_level; 1584b246666eSFilipe Manana bool unlock_up; 1585c8c42864SChris Mason 1586b246666eSFilipe Manana unlock_up = ((level + 1 < BTRFS_MAX_LEVEL) && p->locks[level + 1]); 1587213ff4b7SNikolay Borisov blocknr = btrfs_node_blockptr(*eb_ret, slot); 1588213ff4b7SNikolay Borisov gen = btrfs_node_ptr_generation(*eb_ret, slot); 1589213ff4b7SNikolay Borisov parent_level = btrfs_header_level(*eb_ret); 1590789d6a3aSQu Wenruo btrfs_node_key_to_cpu(*eb_ret, &check.first_key, slot); 1591789d6a3aSQu Wenruo check.has_first_key = true; 1592789d6a3aSQu Wenruo check.level = parent_level - 1; 1593789d6a3aSQu Wenruo check.transid = gen; 1594789d6a3aSQu Wenruo check.owner_root = root->root_key.objectid; 1595c8c42864SChris Mason 1596b246666eSFilipe Manana /* 1597b246666eSFilipe Manana * If we need to read an extent buffer from disk and we are holding locks 1598b246666eSFilipe Manana * on upper level nodes, we unlock all the upper nodes before reading the 1599b246666eSFilipe Manana * extent buffer, and then return -EAGAIN to the caller as it needs to 1600b246666eSFilipe Manana * restart the search. We don't release the lock on the current level 1601b246666eSFilipe Manana * because we need to walk this node to figure out which blocks to read. 1602b246666eSFilipe Manana */ 16030b246afaSJeff Mahoney tmp = find_extent_buffer(fs_info, blocknr); 1604cb44921aSChris Mason if (tmp) { 1605ace75066SFilipe Manana if (p->reada == READA_FORWARD_ALWAYS) 1606ace75066SFilipe Manana reada_for_search(fs_info, p, level, slot, key->objectid); 1607ace75066SFilipe Manana 1608b9fab919SChris Mason /* first we do an atomic uptodate check */ 1609b9fab919SChris Mason if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { 1610448de471SQu Wenruo /* 1611448de471SQu Wenruo * Do extra check for first_key, eb can be stale due to 1612448de471SQu Wenruo * being cached, read from scrub, or have multiple 1613448de471SQu Wenruo * parents (shared tree blocks). 1614448de471SQu Wenruo */ 1615e064d5e9SDavid Sterba if (btrfs_verify_level_key(tmp, 1616789d6a3aSQu Wenruo parent_level - 1, &check.first_key, gen)) { 1617448de471SQu Wenruo free_extent_buffer(tmp); 1618448de471SQu Wenruo return -EUCLEAN; 1619448de471SQu Wenruo } 1620c8c42864SChris Mason *eb_ret = tmp; 1621c8c42864SChris Mason return 0; 1622c8c42864SChris Mason } 1623bdf7c00eSJosef Bacik 1624857bc13fSJosef Bacik if (p->nowait) { 1625857bc13fSJosef Bacik free_extent_buffer(tmp); 1626857bc13fSJosef Bacik return -EAGAIN; 1627857bc13fSJosef Bacik } 1628857bc13fSJosef Bacik 1629b246666eSFilipe Manana if (unlock_up) 1630b246666eSFilipe Manana btrfs_unlock_up_safe(p, level + 1); 1631b246666eSFilipe Manana 1632b9fab919SChris Mason /* now we're allowed to do a blocking uptodate check */ 1633789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(tmp, &check); 16349a4ffa1bSQu Wenruo if (ret) { 1635cb44921aSChris Mason free_extent_buffer(tmp); 1636b3b4aa74SDavid Sterba btrfs_release_path(p); 1637cb44921aSChris Mason return -EIO; 1638cb44921aSChris Mason } 163988c602abSQu Wenruo if (btrfs_check_eb_owner(tmp, root->root_key.objectid)) { 164088c602abSQu Wenruo free_extent_buffer(tmp); 164188c602abSQu Wenruo btrfs_release_path(p); 164288c602abSQu Wenruo return -EUCLEAN; 164388c602abSQu Wenruo } 1644b246666eSFilipe Manana 1645b246666eSFilipe Manana if (unlock_up) 1646b246666eSFilipe Manana ret = -EAGAIN; 1647b246666eSFilipe Manana 1648b246666eSFilipe Manana goto out; 1649857bc13fSJosef Bacik } else if (p->nowait) { 1650857bc13fSJosef Bacik return -EAGAIN; 16519a4ffa1bSQu Wenruo } 1652c8c42864SChris Mason 1653b246666eSFilipe Manana if (unlock_up) { 16548c594ea8SChris Mason btrfs_unlock_up_safe(p, level + 1); 16554bb59055SFilipe Manana ret = -EAGAIN; 16564bb59055SFilipe Manana } else { 16574bb59055SFilipe Manana ret = 0; 16584bb59055SFilipe Manana } 16598c594ea8SChris Mason 1660e4058b54SDavid Sterba if (p->reada != READA_NONE) 16612ff7e61eSJeff Mahoney reada_for_search(fs_info, p, level, slot, key->objectid); 1662c8c42864SChris Mason 1663789d6a3aSQu Wenruo tmp = read_tree_block(fs_info, blocknr, &check); 16644eb150d6SQu Wenruo if (IS_ERR(tmp)) { 16654eb150d6SQu Wenruo btrfs_release_path(p); 16664eb150d6SQu Wenruo return PTR_ERR(tmp); 16674eb150d6SQu Wenruo } 166876a05b35SChris Mason /* 166976a05b35SChris Mason * If the read above didn't mark this buffer up to date, 167076a05b35SChris Mason * it will never end up being up to date. Set ret to EIO now 167176a05b35SChris Mason * and give up so that our caller doesn't loop forever 167276a05b35SChris Mason * on our EAGAINs. 167376a05b35SChris Mason */ 1674e6a1d6fdSLiu Bo if (!extent_buffer_uptodate(tmp)) 167576a05b35SChris Mason ret = -EIO; 167602a3307aSLiu Bo 1677b246666eSFilipe Manana out: 16784bb59055SFilipe Manana if (ret == 0) { 16794bb59055SFilipe Manana *eb_ret = tmp; 16804bb59055SFilipe Manana } else { 16814bb59055SFilipe Manana free_extent_buffer(tmp); 168202a3307aSLiu Bo btrfs_release_path(p); 16834bb59055SFilipe Manana } 16844bb59055SFilipe Manana 168576a05b35SChris Mason return ret; 1686c8c42864SChris Mason } 1687c8c42864SChris Mason 1688c8c42864SChris Mason /* 1689c8c42864SChris Mason * helper function for btrfs_search_slot. This does all of the checks 1690c8c42864SChris Mason * for node-level blocks and does any balancing required based on 1691c8c42864SChris Mason * the ins_len. 1692c8c42864SChris Mason * 1693c8c42864SChris Mason * If no extra work was required, zero is returned. If we had to 1694c8c42864SChris Mason * drop the path, -EAGAIN is returned and btrfs_search_slot must 1695c8c42864SChris Mason * start over 1696c8c42864SChris Mason */ 1697c8c42864SChris Mason static int 1698c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans, 1699c8c42864SChris Mason struct btrfs_root *root, struct btrfs_path *p, 1700bd681513SChris Mason struct extent_buffer *b, int level, int ins_len, 1701bd681513SChris Mason int *write_lock_level) 1702c8c42864SChris Mason { 17030b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 170495b982deSNikolay Borisov int ret = 0; 17050b246afaSJeff Mahoney 1706c8c42864SChris Mason if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= 17070b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) { 1708c8c42864SChris Mason 1709bd681513SChris Mason if (*write_lock_level < level + 1) { 1710bd681513SChris Mason *write_lock_level = level + 1; 1711bd681513SChris Mason btrfs_release_path(p); 171295b982deSNikolay Borisov return -EAGAIN; 1713bd681513SChris Mason } 1714bd681513SChris Mason 1715bfb484d9SJosef Bacik reada_for_balance(p, level); 171695b982deSNikolay Borisov ret = split_node(trans, root, p, level); 1717c8c42864SChris Mason 1718c8c42864SChris Mason b = p->nodes[level]; 1719c8c42864SChris Mason } else if (ins_len < 0 && btrfs_header_nritems(b) < 17200b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) { 1721c8c42864SChris Mason 1722bd681513SChris Mason if (*write_lock_level < level + 1) { 1723bd681513SChris Mason *write_lock_level = level + 1; 1724bd681513SChris Mason btrfs_release_path(p); 172595b982deSNikolay Borisov return -EAGAIN; 1726bd681513SChris Mason } 1727bd681513SChris Mason 1728bfb484d9SJosef Bacik reada_for_balance(p, level); 172995b982deSNikolay Borisov ret = balance_level(trans, root, p, level); 173095b982deSNikolay Borisov if (ret) 173195b982deSNikolay Borisov return ret; 1732c8c42864SChris Mason 1733c8c42864SChris Mason b = p->nodes[level]; 1734c8c42864SChris Mason if (!b) { 1735b3b4aa74SDavid Sterba btrfs_release_path(p); 173695b982deSNikolay Borisov return -EAGAIN; 1737c8c42864SChris Mason } 1738c8c42864SChris Mason BUG_ON(btrfs_header_nritems(b) == 1); 1739c8c42864SChris Mason } 1740c8c42864SChris Mason return ret; 1741c8c42864SChris Mason } 1742c8c42864SChris Mason 1743381cf658SDavid Sterba int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, 1744e33d5c3dSKelley Nielsen u64 iobjectid, u64 ioff, u8 key_type, 1745e33d5c3dSKelley Nielsen struct btrfs_key *found_key) 1746e33d5c3dSKelley Nielsen { 1747e33d5c3dSKelley Nielsen int ret; 1748e33d5c3dSKelley Nielsen struct btrfs_key key; 1749e33d5c3dSKelley Nielsen struct extent_buffer *eb; 1750381cf658SDavid Sterba 1751381cf658SDavid Sterba ASSERT(path); 17521d4c08e0SDavid Sterba ASSERT(found_key); 1753e33d5c3dSKelley Nielsen 1754e33d5c3dSKelley Nielsen key.type = key_type; 1755e33d5c3dSKelley Nielsen key.objectid = iobjectid; 1756e33d5c3dSKelley Nielsen key.offset = ioff; 1757e33d5c3dSKelley Nielsen 1758e33d5c3dSKelley Nielsen ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 17591d4c08e0SDavid Sterba if (ret < 0) 1760e33d5c3dSKelley Nielsen return ret; 1761e33d5c3dSKelley Nielsen 1762e33d5c3dSKelley Nielsen eb = path->nodes[0]; 1763e33d5c3dSKelley Nielsen if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { 1764e33d5c3dSKelley Nielsen ret = btrfs_next_leaf(fs_root, path); 1765e33d5c3dSKelley Nielsen if (ret) 1766e33d5c3dSKelley Nielsen return ret; 1767e33d5c3dSKelley Nielsen eb = path->nodes[0]; 1768e33d5c3dSKelley Nielsen } 1769e33d5c3dSKelley Nielsen 1770e33d5c3dSKelley Nielsen btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); 1771e33d5c3dSKelley Nielsen if (found_key->type != key.type || 1772e33d5c3dSKelley Nielsen found_key->objectid != key.objectid) 1773e33d5c3dSKelley Nielsen return 1; 1774e33d5c3dSKelley Nielsen 1775e33d5c3dSKelley Nielsen return 0; 1776e33d5c3dSKelley Nielsen } 1777e33d5c3dSKelley Nielsen 17781fc28d8eSLiu Bo static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root, 17791fc28d8eSLiu Bo struct btrfs_path *p, 17801fc28d8eSLiu Bo int write_lock_level) 17811fc28d8eSLiu Bo { 17821fc28d8eSLiu Bo struct extent_buffer *b; 1783120de408SJosef Bacik int root_lock = 0; 17841fc28d8eSLiu Bo int level = 0; 17851fc28d8eSLiu Bo 17861fc28d8eSLiu Bo if (p->search_commit_root) { 17871fc28d8eSLiu Bo b = root->commit_root; 178867439dadSDavid Sterba atomic_inc(&b->refs); 17891fc28d8eSLiu Bo level = btrfs_header_level(b); 1790f9ddfd05SLiu Bo /* 1791f9ddfd05SLiu Bo * Ensure that all callers have set skip_locking when 1792f9ddfd05SLiu Bo * p->search_commit_root = 1. 1793f9ddfd05SLiu Bo */ 1794f9ddfd05SLiu Bo ASSERT(p->skip_locking == 1); 17951fc28d8eSLiu Bo 17961fc28d8eSLiu Bo goto out; 17971fc28d8eSLiu Bo } 17981fc28d8eSLiu Bo 17991fc28d8eSLiu Bo if (p->skip_locking) { 18001fc28d8eSLiu Bo b = btrfs_root_node(root); 18011fc28d8eSLiu Bo level = btrfs_header_level(b); 18021fc28d8eSLiu Bo goto out; 18031fc28d8eSLiu Bo } 18041fc28d8eSLiu Bo 1805120de408SJosef Bacik /* We try very hard to do read locks on the root */ 1806120de408SJosef Bacik root_lock = BTRFS_READ_LOCK; 1807120de408SJosef Bacik 18081fc28d8eSLiu Bo /* 1809662c653bSLiu Bo * If the level is set to maximum, we can skip trying to get the read 1810662c653bSLiu Bo * lock. 1811662c653bSLiu Bo */ 1812662c653bSLiu Bo if (write_lock_level < BTRFS_MAX_LEVEL) { 1813662c653bSLiu Bo /* 1814662c653bSLiu Bo * We don't know the level of the root node until we actually 1815662c653bSLiu Bo * have it read locked 18161fc28d8eSLiu Bo */ 1817857bc13fSJosef Bacik if (p->nowait) { 1818857bc13fSJosef Bacik b = btrfs_try_read_lock_root_node(root); 1819857bc13fSJosef Bacik if (IS_ERR(b)) 1820857bc13fSJosef Bacik return b; 1821857bc13fSJosef Bacik } else { 18221bb96598SJosef Bacik b = btrfs_read_lock_root_node(root); 1823857bc13fSJosef Bacik } 18241fc28d8eSLiu Bo level = btrfs_header_level(b); 18251fc28d8eSLiu Bo if (level > write_lock_level) 18261fc28d8eSLiu Bo goto out; 18271fc28d8eSLiu Bo 1828662c653bSLiu Bo /* Whoops, must trade for write lock */ 18291fc28d8eSLiu Bo btrfs_tree_read_unlock(b); 18301fc28d8eSLiu Bo free_extent_buffer(b); 1831662c653bSLiu Bo } 1832662c653bSLiu Bo 18331fc28d8eSLiu Bo b = btrfs_lock_root_node(root); 18341fc28d8eSLiu Bo root_lock = BTRFS_WRITE_LOCK; 18351fc28d8eSLiu Bo 18361fc28d8eSLiu Bo /* The level might have changed, check again */ 18371fc28d8eSLiu Bo level = btrfs_header_level(b); 18381fc28d8eSLiu Bo 18391fc28d8eSLiu Bo out: 1840120de408SJosef Bacik /* 1841120de408SJosef Bacik * The root may have failed to write out at some point, and thus is no 1842120de408SJosef Bacik * longer valid, return an error in this case. 1843120de408SJosef Bacik */ 1844120de408SJosef Bacik if (!extent_buffer_uptodate(b)) { 1845120de408SJosef Bacik if (root_lock) 1846120de408SJosef Bacik btrfs_tree_unlock_rw(b, root_lock); 1847120de408SJosef Bacik free_extent_buffer(b); 1848120de408SJosef Bacik return ERR_PTR(-EIO); 1849120de408SJosef Bacik } 1850120de408SJosef Bacik 18511fc28d8eSLiu Bo p->nodes[level] = b; 18521fc28d8eSLiu Bo if (!p->skip_locking) 18531fc28d8eSLiu Bo p->locks[level] = root_lock; 18541fc28d8eSLiu Bo /* 18551fc28d8eSLiu Bo * Callers are responsible for dropping b's references. 18561fc28d8eSLiu Bo */ 18571fc28d8eSLiu Bo return b; 18581fc28d8eSLiu Bo } 18591fc28d8eSLiu Bo 1860d96b3424SFilipe Manana /* 1861d96b3424SFilipe Manana * Replace the extent buffer at the lowest level of the path with a cloned 1862d96b3424SFilipe Manana * version. The purpose is to be able to use it safely, after releasing the 1863d96b3424SFilipe Manana * commit root semaphore, even if relocation is happening in parallel, the 1864d96b3424SFilipe Manana * transaction used for relocation is committed and the extent buffer is 1865d96b3424SFilipe Manana * reallocated in the next transaction. 1866d96b3424SFilipe Manana * 1867d96b3424SFilipe Manana * This is used in a context where the caller does not prevent transaction 1868d96b3424SFilipe Manana * commits from happening, either by holding a transaction handle or holding 1869d96b3424SFilipe Manana * some lock, while it's doing searches through a commit root. 1870d96b3424SFilipe Manana * At the moment it's only used for send operations. 1871d96b3424SFilipe Manana */ 1872d96b3424SFilipe Manana static int finish_need_commit_sem_search(struct btrfs_path *path) 1873d96b3424SFilipe Manana { 1874d96b3424SFilipe Manana const int i = path->lowest_level; 1875d96b3424SFilipe Manana const int slot = path->slots[i]; 1876d96b3424SFilipe Manana struct extent_buffer *lowest = path->nodes[i]; 1877d96b3424SFilipe Manana struct extent_buffer *clone; 1878d96b3424SFilipe Manana 1879d96b3424SFilipe Manana ASSERT(path->need_commit_sem); 1880d96b3424SFilipe Manana 1881d96b3424SFilipe Manana if (!lowest) 1882d96b3424SFilipe Manana return 0; 1883d96b3424SFilipe Manana 1884d96b3424SFilipe Manana lockdep_assert_held_read(&lowest->fs_info->commit_root_sem); 1885d96b3424SFilipe Manana 1886d96b3424SFilipe Manana clone = btrfs_clone_extent_buffer(lowest); 1887d96b3424SFilipe Manana if (!clone) 1888d96b3424SFilipe Manana return -ENOMEM; 1889d96b3424SFilipe Manana 1890d96b3424SFilipe Manana btrfs_release_path(path); 1891d96b3424SFilipe Manana path->nodes[i] = clone; 1892d96b3424SFilipe Manana path->slots[i] = slot; 1893d96b3424SFilipe Manana 1894d96b3424SFilipe Manana return 0; 1895d96b3424SFilipe Manana } 18961fc28d8eSLiu Bo 1897e2e58d0fSFilipe Manana static inline int search_for_key_slot(struct extent_buffer *eb, 1898e2e58d0fSFilipe Manana int search_low_slot, 1899e2e58d0fSFilipe Manana const struct btrfs_key *key, 1900e2e58d0fSFilipe Manana int prev_cmp, 1901e2e58d0fSFilipe Manana int *slot) 1902e2e58d0fSFilipe Manana { 1903e2e58d0fSFilipe Manana /* 1904e2e58d0fSFilipe Manana * If a previous call to btrfs_bin_search() on a parent node returned an 1905e2e58d0fSFilipe Manana * exact match (prev_cmp == 0), we can safely assume the target key will 1906e2e58d0fSFilipe Manana * always be at slot 0 on lower levels, since each key pointer 1907e2e58d0fSFilipe Manana * (struct btrfs_key_ptr) refers to the lowest key accessible from the 1908e2e58d0fSFilipe Manana * subtree it points to. Thus we can skip searching lower levels. 1909e2e58d0fSFilipe Manana */ 1910e2e58d0fSFilipe Manana if (prev_cmp == 0) { 1911e2e58d0fSFilipe Manana *slot = 0; 1912e2e58d0fSFilipe Manana return 0; 1913e2e58d0fSFilipe Manana } 1914e2e58d0fSFilipe Manana 1915fdf8d595SAnand Jain return btrfs_bin_search(eb, search_low_slot, key, slot); 1916e2e58d0fSFilipe Manana } 1917e2e58d0fSFilipe Manana 1918109324cfSFilipe Manana static int search_leaf(struct btrfs_trans_handle *trans, 1919109324cfSFilipe Manana struct btrfs_root *root, 1920109324cfSFilipe Manana const struct btrfs_key *key, 1921109324cfSFilipe Manana struct btrfs_path *path, 1922109324cfSFilipe Manana int ins_len, 1923109324cfSFilipe Manana int prev_cmp) 1924109324cfSFilipe Manana { 1925109324cfSFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 1926109324cfSFilipe Manana int leaf_free_space = -1; 1927109324cfSFilipe Manana int search_low_slot = 0; 1928109324cfSFilipe Manana int ret; 1929109324cfSFilipe Manana bool do_bin_search = true; 1930109324cfSFilipe Manana 1931109324cfSFilipe Manana /* 1932109324cfSFilipe Manana * If we are doing an insertion, the leaf has enough free space and the 1933109324cfSFilipe Manana * destination slot for the key is not slot 0, then we can unlock our 1934109324cfSFilipe Manana * write lock on the parent, and any other upper nodes, before doing the 1935109324cfSFilipe Manana * binary search on the leaf (with search_for_key_slot()), allowing other 1936109324cfSFilipe Manana * tasks to lock the parent and any other upper nodes. 1937109324cfSFilipe Manana */ 1938109324cfSFilipe Manana if (ins_len > 0) { 1939109324cfSFilipe Manana /* 1940109324cfSFilipe Manana * Cache the leaf free space, since we will need it later and it 1941109324cfSFilipe Manana * will not change until then. 1942109324cfSFilipe Manana */ 1943109324cfSFilipe Manana leaf_free_space = btrfs_leaf_free_space(leaf); 1944109324cfSFilipe Manana 1945109324cfSFilipe Manana /* 1946109324cfSFilipe Manana * !path->locks[1] means we have a single node tree, the leaf is 1947109324cfSFilipe Manana * the root of the tree. 1948109324cfSFilipe Manana */ 1949109324cfSFilipe Manana if (path->locks[1] && leaf_free_space >= ins_len) { 1950109324cfSFilipe Manana struct btrfs_disk_key first_key; 1951109324cfSFilipe Manana 1952109324cfSFilipe Manana ASSERT(btrfs_header_nritems(leaf) > 0); 1953109324cfSFilipe Manana btrfs_item_key(leaf, &first_key, 0); 1954109324cfSFilipe Manana 1955109324cfSFilipe Manana /* 1956109324cfSFilipe Manana * Doing the extra comparison with the first key is cheap, 1957109324cfSFilipe Manana * taking into account that the first key is very likely 1958109324cfSFilipe Manana * already in a cache line because it immediately follows 1959109324cfSFilipe Manana * the extent buffer's header and we have recently accessed 1960109324cfSFilipe Manana * the header's level field. 1961109324cfSFilipe Manana */ 1962109324cfSFilipe Manana ret = comp_keys(&first_key, key); 1963109324cfSFilipe Manana if (ret < 0) { 1964109324cfSFilipe Manana /* 1965109324cfSFilipe Manana * The first key is smaller than the key we want 1966109324cfSFilipe Manana * to insert, so we are safe to unlock all upper 1967109324cfSFilipe Manana * nodes and we have to do the binary search. 1968109324cfSFilipe Manana * 1969109324cfSFilipe Manana * We do use btrfs_unlock_up_safe() and not 1970109324cfSFilipe Manana * unlock_up() because the later does not unlock 1971109324cfSFilipe Manana * nodes with a slot of 0 - we can safely unlock 1972109324cfSFilipe Manana * any node even if its slot is 0 since in this 1973109324cfSFilipe Manana * case the key does not end up at slot 0 of the 1974109324cfSFilipe Manana * leaf and there's no need to split the leaf. 1975109324cfSFilipe Manana */ 1976109324cfSFilipe Manana btrfs_unlock_up_safe(path, 1); 1977109324cfSFilipe Manana search_low_slot = 1; 1978109324cfSFilipe Manana } else { 1979109324cfSFilipe Manana /* 1980109324cfSFilipe Manana * The first key is >= then the key we want to 1981109324cfSFilipe Manana * insert, so we can skip the binary search as 1982109324cfSFilipe Manana * the target key will be at slot 0. 1983109324cfSFilipe Manana * 1984109324cfSFilipe Manana * We can not unlock upper nodes when the key is 1985109324cfSFilipe Manana * less than the first key, because we will need 1986109324cfSFilipe Manana * to update the key at slot 0 of the parent node 1987109324cfSFilipe Manana * and possibly of other upper nodes too. 1988109324cfSFilipe Manana * If the key matches the first key, then we can 1989109324cfSFilipe Manana * unlock all the upper nodes, using 1990109324cfSFilipe Manana * btrfs_unlock_up_safe() instead of unlock_up() 1991109324cfSFilipe Manana * as stated above. 1992109324cfSFilipe Manana */ 1993109324cfSFilipe Manana if (ret == 0) 1994109324cfSFilipe Manana btrfs_unlock_up_safe(path, 1); 1995109324cfSFilipe Manana /* 1996109324cfSFilipe Manana * ret is already 0 or 1, matching the result of 1997109324cfSFilipe Manana * a btrfs_bin_search() call, so there is no need 1998109324cfSFilipe Manana * to adjust it. 1999109324cfSFilipe Manana */ 2000109324cfSFilipe Manana do_bin_search = false; 2001109324cfSFilipe Manana path->slots[0] = 0; 2002109324cfSFilipe Manana } 2003109324cfSFilipe Manana } 2004109324cfSFilipe Manana } 2005109324cfSFilipe Manana 2006109324cfSFilipe Manana if (do_bin_search) { 2007109324cfSFilipe Manana ret = search_for_key_slot(leaf, search_low_slot, key, 2008109324cfSFilipe Manana prev_cmp, &path->slots[0]); 2009109324cfSFilipe Manana if (ret < 0) 2010109324cfSFilipe Manana return ret; 2011109324cfSFilipe Manana } 2012109324cfSFilipe Manana 2013109324cfSFilipe Manana if (ins_len > 0) { 2014109324cfSFilipe Manana /* 2015109324cfSFilipe Manana * Item key already exists. In this case, if we are allowed to 2016109324cfSFilipe Manana * insert the item (for example, in dir_item case, item key 2017109324cfSFilipe Manana * collision is allowed), it will be merged with the original 2018109324cfSFilipe Manana * item. Only the item size grows, no new btrfs item will be 2019109324cfSFilipe Manana * added. If search_for_extension is not set, ins_len already 2020109324cfSFilipe Manana * accounts the size btrfs_item, deduct it here so leaf space 2021109324cfSFilipe Manana * check will be correct. 2022109324cfSFilipe Manana */ 2023109324cfSFilipe Manana if (ret == 0 && !path->search_for_extension) { 2024109324cfSFilipe Manana ASSERT(ins_len >= sizeof(struct btrfs_item)); 2025109324cfSFilipe Manana ins_len -= sizeof(struct btrfs_item); 2026109324cfSFilipe Manana } 2027109324cfSFilipe Manana 2028109324cfSFilipe Manana ASSERT(leaf_free_space >= 0); 2029109324cfSFilipe Manana 2030109324cfSFilipe Manana if (leaf_free_space < ins_len) { 2031109324cfSFilipe Manana int err; 2032109324cfSFilipe Manana 2033109324cfSFilipe Manana err = split_leaf(trans, root, key, path, ins_len, 2034109324cfSFilipe Manana (ret == 0)); 2035bb8e9a60SFilipe Manana ASSERT(err <= 0); 2036bb8e9a60SFilipe Manana if (WARN_ON(err > 0)) 2037bb8e9a60SFilipe Manana err = -EUCLEAN; 2038109324cfSFilipe Manana if (err) 2039109324cfSFilipe Manana ret = err; 2040109324cfSFilipe Manana } 2041109324cfSFilipe Manana } 2042109324cfSFilipe Manana 2043109324cfSFilipe Manana return ret; 2044109324cfSFilipe Manana } 2045109324cfSFilipe Manana 2046c8c42864SChris Mason /* 20474271eceaSNikolay Borisov * btrfs_search_slot - look for a key in a tree and perform necessary 20484271eceaSNikolay Borisov * modifications to preserve tree invariants. 204974123bd7SChris Mason * 20504271eceaSNikolay Borisov * @trans: Handle of transaction, used when modifying the tree 20514271eceaSNikolay Borisov * @p: Holds all btree nodes along the search path 20524271eceaSNikolay Borisov * @root: The root node of the tree 20534271eceaSNikolay Borisov * @key: The key we are looking for 20549a664971Sethanwu * @ins_len: Indicates purpose of search: 20559a664971Sethanwu * >0 for inserts it's size of item inserted (*) 20569a664971Sethanwu * <0 for deletions 20579a664971Sethanwu * 0 for plain searches, not modifying the tree 20589a664971Sethanwu * 20599a664971Sethanwu * (*) If size of item inserted doesn't include 20609a664971Sethanwu * sizeof(struct btrfs_item), then p->search_for_extension must 20619a664971Sethanwu * be set. 20624271eceaSNikolay Borisov * @cow: boolean should CoW operations be performed. Must always be 1 20634271eceaSNikolay Borisov * when modifying the tree. 206497571fd0SChris Mason * 20654271eceaSNikolay Borisov * If @ins_len > 0, nodes and leaves will be split as we walk down the tree. 20664271eceaSNikolay Borisov * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible) 20674271eceaSNikolay Borisov * 20684271eceaSNikolay Borisov * If @key is found, 0 is returned and you can find the item in the leaf level 20694271eceaSNikolay Borisov * of the path (level 0) 20704271eceaSNikolay Borisov * 20714271eceaSNikolay Borisov * If @key isn't found, 1 is returned and the leaf level of the path (level 0) 20724271eceaSNikolay Borisov * points to the slot where it should be inserted 20734271eceaSNikolay Borisov * 20744271eceaSNikolay Borisov * If an error is encountered while searching the tree a negative error number 20754271eceaSNikolay Borisov * is returned 207674123bd7SChris Mason */ 2077310712b2SOmar Sandoval int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2078310712b2SOmar Sandoval const struct btrfs_key *key, struct btrfs_path *p, 2079310712b2SOmar Sandoval int ins_len, int cow) 2080be0e5c09SChris Mason { 2081d96b3424SFilipe Manana struct btrfs_fs_info *fs_info = root->fs_info; 20825f39d397SChris Mason struct extent_buffer *b; 2083be0e5c09SChris Mason int slot; 2084be0e5c09SChris Mason int ret; 208533c66f43SYan Zheng int err; 2086be0e5c09SChris Mason int level; 2087925baeddSChris Mason int lowest_unlock = 1; 2088bd681513SChris Mason /* everything at write_lock_level or lower must be write locked */ 2089bd681513SChris Mason int write_lock_level = 0; 20909f3a7427SChris Mason u8 lowest_level = 0; 2091f7c79f30SChris Mason int min_write_lock_level; 2092d7396f07SFilipe David Borba Manana int prev_cmp; 20939f3a7427SChris Mason 2094a4c853afSChenXiaoSong might_sleep(); 2095a4c853afSChenXiaoSong 20966702ed49SChris Mason lowest_level = p->lowest_level; 2097323ac95bSChris Mason WARN_ON(lowest_level && ins_len > 0); 209822b0ebdaSChris Mason WARN_ON(p->nodes[0] != NULL); 2099eb653de1SFilipe David Borba Manana BUG_ON(!cow && ins_len); 210025179201SJosef Bacik 2101857bc13fSJosef Bacik /* 2102857bc13fSJosef Bacik * For now only allow nowait for read only operations. There's no 2103857bc13fSJosef Bacik * strict reason why we can't, we just only need it for reads so it's 2104857bc13fSJosef Bacik * only implemented for reads. 2105857bc13fSJosef Bacik */ 2106857bc13fSJosef Bacik ASSERT(!p->nowait || !cow); 2107857bc13fSJosef Bacik 2108bd681513SChris Mason if (ins_len < 0) { 2109925baeddSChris Mason lowest_unlock = 2; 211065b51a00SChris Mason 2111bd681513SChris Mason /* when we are removing items, we might have to go up to level 2112bd681513SChris Mason * two as we update tree pointers Make sure we keep write 2113bd681513SChris Mason * for those levels as well 2114bd681513SChris Mason */ 2115bd681513SChris Mason write_lock_level = 2; 2116bd681513SChris Mason } else if (ins_len > 0) { 2117bd681513SChris Mason /* 2118bd681513SChris Mason * for inserting items, make sure we have a write lock on 2119bd681513SChris Mason * level 1 so we can update keys 2120bd681513SChris Mason */ 2121bd681513SChris Mason write_lock_level = 1; 2122bd681513SChris Mason } 2123bd681513SChris Mason 2124bd681513SChris Mason if (!cow) 2125bd681513SChris Mason write_lock_level = -1; 2126bd681513SChris Mason 212709a2a8f9SJosef Bacik if (cow && (p->keep_locks || p->lowest_level)) 2128bd681513SChris Mason write_lock_level = BTRFS_MAX_LEVEL; 2129bd681513SChris Mason 2130f7c79f30SChris Mason min_write_lock_level = write_lock_level; 2131f7c79f30SChris Mason 2132d96b3424SFilipe Manana if (p->need_commit_sem) { 2133d96b3424SFilipe Manana ASSERT(p->search_commit_root); 2134857bc13fSJosef Bacik if (p->nowait) { 2135857bc13fSJosef Bacik if (!down_read_trylock(&fs_info->commit_root_sem)) 2136857bc13fSJosef Bacik return -EAGAIN; 2137857bc13fSJosef Bacik } else { 2138d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem); 2139d96b3424SFilipe Manana } 2140857bc13fSJosef Bacik } 2141d96b3424SFilipe Manana 2142bb803951SChris Mason again: 2143d7396f07SFilipe David Borba Manana prev_cmp = -1; 21441fc28d8eSLiu Bo b = btrfs_search_slot_get_root(root, p, write_lock_level); 2145be6821f8SFilipe Manana if (IS_ERR(b)) { 2146be6821f8SFilipe Manana ret = PTR_ERR(b); 2147be6821f8SFilipe Manana goto done; 2148be6821f8SFilipe Manana } 2149925baeddSChris Mason 2150eb60ceacSChris Mason while (b) { 2151f624d976SQu Wenruo int dec = 0; 2152f624d976SQu Wenruo 21535f39d397SChris Mason level = btrfs_header_level(b); 215465b51a00SChris Mason 215502217ed2SChris Mason if (cow) { 21569ea2c7c9SNikolay Borisov bool last_level = (level == (BTRFS_MAX_LEVEL - 1)); 21579ea2c7c9SNikolay Borisov 2158c8c42864SChris Mason /* 2159c8c42864SChris Mason * if we don't really need to cow this block 2160c8c42864SChris Mason * then we don't want to set the path blocking, 2161c8c42864SChris Mason * so we test it here 2162c8c42864SChris Mason */ 21635963ffcaSJosef Bacik if (!should_cow_block(trans, root, b)) 216465b51a00SChris Mason goto cow_done; 21655d4f98a2SYan Zheng 2166bd681513SChris Mason /* 2167bd681513SChris Mason * must have write locks on this node and the 2168bd681513SChris Mason * parent 2169bd681513SChris Mason */ 21705124e00eSJosef Bacik if (level > write_lock_level || 21715124e00eSJosef Bacik (level + 1 > write_lock_level && 21725124e00eSJosef Bacik level + 1 < BTRFS_MAX_LEVEL && 21735124e00eSJosef Bacik p->nodes[level + 1])) { 2174bd681513SChris Mason write_lock_level = level + 1; 2175bd681513SChris Mason btrfs_release_path(p); 2176bd681513SChris Mason goto again; 2177bd681513SChris Mason } 2178bd681513SChris Mason 21799ea2c7c9SNikolay Borisov if (last_level) 21809ea2c7c9SNikolay Borisov err = btrfs_cow_block(trans, root, b, NULL, 0, 21819631e4ccSJosef Bacik &b, 21829631e4ccSJosef Bacik BTRFS_NESTING_COW); 21839ea2c7c9SNikolay Borisov else 218433c66f43SYan Zheng err = btrfs_cow_block(trans, root, b, 2185e20d96d6SChris Mason p->nodes[level + 1], 21869631e4ccSJosef Bacik p->slots[level + 1], &b, 21879631e4ccSJosef Bacik BTRFS_NESTING_COW); 218833c66f43SYan Zheng if (err) { 218933c66f43SYan Zheng ret = err; 219065b51a00SChris Mason goto done; 219154aa1f4dSChris Mason } 219202217ed2SChris Mason } 219365b51a00SChris Mason cow_done: 2194eb60ceacSChris Mason p->nodes[level] = b; 2195b4ce94deSChris Mason 2196b4ce94deSChris Mason /* 2197b4ce94deSChris Mason * we have a lock on b and as long as we aren't changing 2198b4ce94deSChris Mason * the tree, there is no way to for the items in b to change. 2199b4ce94deSChris Mason * It is safe to drop the lock on our parent before we 2200b4ce94deSChris Mason * go through the expensive btree search on b. 2201b4ce94deSChris Mason * 2202eb653de1SFilipe David Borba Manana * If we're inserting or deleting (ins_len != 0), then we might 2203eb653de1SFilipe David Borba Manana * be changing slot zero, which may require changing the parent. 2204eb653de1SFilipe David Borba Manana * So, we can't drop the lock until after we know which slot 2205eb653de1SFilipe David Borba Manana * we're operating on. 2206b4ce94deSChris Mason */ 2207eb653de1SFilipe David Borba Manana if (!ins_len && !p->keep_locks) { 2208eb653de1SFilipe David Borba Manana int u = level + 1; 2209eb653de1SFilipe David Borba Manana 2210eb653de1SFilipe David Borba Manana if (u < BTRFS_MAX_LEVEL && p->locks[u]) { 2211eb653de1SFilipe David Borba Manana btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]); 2212eb653de1SFilipe David Borba Manana p->locks[u] = 0; 2213eb653de1SFilipe David Borba Manana } 2214eb653de1SFilipe David Borba Manana } 2215b4ce94deSChris Mason 2216e2e58d0fSFilipe Manana if (level == 0) { 2217109324cfSFilipe Manana if (ins_len > 0) 2218e5e1c174SFilipe Manana ASSERT(write_lock_level >= 1); 2219bd681513SChris Mason 2220109324cfSFilipe Manana ret = search_leaf(trans, root, key, p, ins_len, prev_cmp); 2221459931ecSChris Mason if (!p->search_for_split) 2222f7c79f30SChris Mason unlock_up(p, level, lowest_unlock, 22234b6f8e96SLiu Bo min_write_lock_level, NULL); 222465b51a00SChris Mason goto done; 222565b51a00SChris Mason } 2226e2e58d0fSFilipe Manana 2227e2e58d0fSFilipe Manana ret = search_for_key_slot(b, 0, key, prev_cmp, &slot); 2228e2e58d0fSFilipe Manana if (ret < 0) 2229e2e58d0fSFilipe Manana goto done; 2230e2e58d0fSFilipe Manana prev_cmp = ret; 2231e2e58d0fSFilipe Manana 2232f624d976SQu Wenruo if (ret && slot > 0) { 2233f624d976SQu Wenruo dec = 1; 2234f624d976SQu Wenruo slot--; 2235f624d976SQu Wenruo } 2236f624d976SQu Wenruo p->slots[level] = slot; 2237f624d976SQu Wenruo err = setup_nodes_for_search(trans, root, p, b, level, ins_len, 2238f624d976SQu Wenruo &write_lock_level); 2239f624d976SQu Wenruo if (err == -EAGAIN) 2240f624d976SQu Wenruo goto again; 2241f624d976SQu Wenruo if (err) { 2242f624d976SQu Wenruo ret = err; 2243f624d976SQu Wenruo goto done; 2244f624d976SQu Wenruo } 2245f624d976SQu Wenruo b = p->nodes[level]; 2246f624d976SQu Wenruo slot = p->slots[level]; 2247f624d976SQu Wenruo 2248f624d976SQu Wenruo /* 2249f624d976SQu Wenruo * Slot 0 is special, if we change the key we have to update 2250f624d976SQu Wenruo * the parent pointer which means we must have a write lock on 2251f624d976SQu Wenruo * the parent 2252f624d976SQu Wenruo */ 2253f624d976SQu Wenruo if (slot == 0 && ins_len && write_lock_level < level + 1) { 2254f624d976SQu Wenruo write_lock_level = level + 1; 2255f624d976SQu Wenruo btrfs_release_path(p); 2256f624d976SQu Wenruo goto again; 2257f624d976SQu Wenruo } 2258f624d976SQu Wenruo 2259f624d976SQu Wenruo unlock_up(p, level, lowest_unlock, min_write_lock_level, 2260f624d976SQu Wenruo &write_lock_level); 2261f624d976SQu Wenruo 2262f624d976SQu Wenruo if (level == lowest_level) { 2263f624d976SQu Wenruo if (dec) 2264f624d976SQu Wenruo p->slots[level]++; 2265f624d976SQu Wenruo goto done; 2266f624d976SQu Wenruo } 2267f624d976SQu Wenruo 2268f624d976SQu Wenruo err = read_block_for_search(root, p, &b, level, slot, key); 2269f624d976SQu Wenruo if (err == -EAGAIN) 2270f624d976SQu Wenruo goto again; 2271f624d976SQu Wenruo if (err) { 2272f624d976SQu Wenruo ret = err; 2273f624d976SQu Wenruo goto done; 2274f624d976SQu Wenruo } 2275f624d976SQu Wenruo 2276f624d976SQu Wenruo if (!p->skip_locking) { 2277f624d976SQu Wenruo level = btrfs_header_level(b); 2278b40130b2SJosef Bacik 2279b40130b2SJosef Bacik btrfs_maybe_reset_lockdep_class(root, b); 2280b40130b2SJosef Bacik 2281f624d976SQu Wenruo if (level <= write_lock_level) { 2282f624d976SQu Wenruo btrfs_tree_lock(b); 2283f624d976SQu Wenruo p->locks[level] = BTRFS_WRITE_LOCK; 2284f624d976SQu Wenruo } else { 2285857bc13fSJosef Bacik if (p->nowait) { 2286857bc13fSJosef Bacik if (!btrfs_try_tree_read_lock(b)) { 2287857bc13fSJosef Bacik free_extent_buffer(b); 2288857bc13fSJosef Bacik ret = -EAGAIN; 2289857bc13fSJosef Bacik goto done; 2290857bc13fSJosef Bacik } 2291857bc13fSJosef Bacik } else { 2292fe596ca3SJosef Bacik btrfs_tree_read_lock(b); 2293857bc13fSJosef Bacik } 2294f624d976SQu Wenruo p->locks[level] = BTRFS_READ_LOCK; 2295f624d976SQu Wenruo } 2296f624d976SQu Wenruo p->nodes[level] = b; 2297f624d976SQu Wenruo } 229865b51a00SChris Mason } 229965b51a00SChris Mason ret = 1; 230065b51a00SChris Mason done: 23015f5bc6b1SFilipe Manana if (ret < 0 && !p->skip_release_on_error) 2302b3b4aa74SDavid Sterba btrfs_release_path(p); 2303d96b3424SFilipe Manana 2304d96b3424SFilipe Manana if (p->need_commit_sem) { 2305d96b3424SFilipe Manana int ret2; 2306d96b3424SFilipe Manana 2307d96b3424SFilipe Manana ret2 = finish_need_commit_sem_search(p); 2308d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem); 2309d96b3424SFilipe Manana if (ret2) 2310d96b3424SFilipe Manana ret = ret2; 2311d96b3424SFilipe Manana } 2312d96b3424SFilipe Manana 2313be0e5c09SChris Mason return ret; 2314be0e5c09SChris Mason } 2315f75e2b79SJosef Bacik ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO); 2316be0e5c09SChris Mason 231774123bd7SChris Mason /* 23185d9e75c4SJan Schmidt * Like btrfs_search_slot, this looks for a key in the given tree. It uses the 23195d9e75c4SJan Schmidt * current state of the tree together with the operations recorded in the tree 23205d9e75c4SJan Schmidt * modification log to search for the key in a previous version of this tree, as 23215d9e75c4SJan Schmidt * denoted by the time_seq parameter. 23225d9e75c4SJan Schmidt * 23235d9e75c4SJan Schmidt * Naturally, there is no support for insert, delete or cow operations. 23245d9e75c4SJan Schmidt * 23255d9e75c4SJan Schmidt * The resulting path and return value will be set up as if we called 23265d9e75c4SJan Schmidt * btrfs_search_slot at that point in time with ins_len and cow both set to 0. 23275d9e75c4SJan Schmidt */ 2328310712b2SOmar Sandoval int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key, 23295d9e75c4SJan Schmidt struct btrfs_path *p, u64 time_seq) 23305d9e75c4SJan Schmidt { 23310b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 23325d9e75c4SJan Schmidt struct extent_buffer *b; 23335d9e75c4SJan Schmidt int slot; 23345d9e75c4SJan Schmidt int ret; 23355d9e75c4SJan Schmidt int err; 23365d9e75c4SJan Schmidt int level; 23375d9e75c4SJan Schmidt int lowest_unlock = 1; 23385d9e75c4SJan Schmidt u8 lowest_level = 0; 23395d9e75c4SJan Schmidt 23405d9e75c4SJan Schmidt lowest_level = p->lowest_level; 23415d9e75c4SJan Schmidt WARN_ON(p->nodes[0] != NULL); 2342c922b016SStefan Roesch ASSERT(!p->nowait); 23435d9e75c4SJan Schmidt 23445d9e75c4SJan Schmidt if (p->search_commit_root) { 23455d9e75c4SJan Schmidt BUG_ON(time_seq); 23465d9e75c4SJan Schmidt return btrfs_search_slot(NULL, root, key, p, 0, 0); 23475d9e75c4SJan Schmidt } 23485d9e75c4SJan Schmidt 23495d9e75c4SJan Schmidt again: 2350f3a84ccdSFilipe Manana b = btrfs_get_old_root(root, time_seq); 2351315bed43SNikolay Borisov if (!b) { 2352315bed43SNikolay Borisov ret = -EIO; 2353315bed43SNikolay Borisov goto done; 2354315bed43SNikolay Borisov } 23555d9e75c4SJan Schmidt level = btrfs_header_level(b); 23565d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 23575d9e75c4SJan Schmidt 23585d9e75c4SJan Schmidt while (b) { 2359abe9339dSQu Wenruo int dec = 0; 2360abe9339dSQu Wenruo 23615d9e75c4SJan Schmidt level = btrfs_header_level(b); 23625d9e75c4SJan Schmidt p->nodes[level] = b; 23635d9e75c4SJan Schmidt 23645d9e75c4SJan Schmidt /* 23655d9e75c4SJan Schmidt * we have a lock on b and as long as we aren't changing 23665d9e75c4SJan Schmidt * the tree, there is no way to for the items in b to change. 23675d9e75c4SJan Schmidt * It is safe to drop the lock on our parent before we 23685d9e75c4SJan Schmidt * go through the expensive btree search on b. 23695d9e75c4SJan Schmidt */ 23705d9e75c4SJan Schmidt btrfs_unlock_up_safe(p, level + 1); 23715d9e75c4SJan Schmidt 2372fdf8d595SAnand Jain ret = btrfs_bin_search(b, 0, key, &slot); 2373cbca7d59SFilipe Manana if (ret < 0) 2374cbca7d59SFilipe Manana goto done; 23755d9e75c4SJan Schmidt 2376abe9339dSQu Wenruo if (level == 0) { 2377abe9339dSQu Wenruo p->slots[level] = slot; 2378abe9339dSQu Wenruo unlock_up(p, level, lowest_unlock, 0, NULL); 2379abe9339dSQu Wenruo goto done; 2380abe9339dSQu Wenruo } 2381abe9339dSQu Wenruo 23825d9e75c4SJan Schmidt if (ret && slot > 0) { 23835d9e75c4SJan Schmidt dec = 1; 2384abe9339dSQu Wenruo slot--; 23855d9e75c4SJan Schmidt } 23865d9e75c4SJan Schmidt p->slots[level] = slot; 23875d9e75c4SJan Schmidt unlock_up(p, level, lowest_unlock, 0, NULL); 23885d9e75c4SJan Schmidt 23895d9e75c4SJan Schmidt if (level == lowest_level) { 23905d9e75c4SJan Schmidt if (dec) 23915d9e75c4SJan Schmidt p->slots[level]++; 23925d9e75c4SJan Schmidt goto done; 23935d9e75c4SJan Schmidt } 23945d9e75c4SJan Schmidt 2395abe9339dSQu Wenruo err = read_block_for_search(root, p, &b, level, slot, key); 23965d9e75c4SJan Schmidt if (err == -EAGAIN) 23975d9e75c4SJan Schmidt goto again; 23985d9e75c4SJan Schmidt if (err) { 23995d9e75c4SJan Schmidt ret = err; 24005d9e75c4SJan Schmidt goto done; 24015d9e75c4SJan Schmidt } 24025d9e75c4SJan Schmidt 24035d9e75c4SJan Schmidt level = btrfs_header_level(b); 24045d9e75c4SJan Schmidt btrfs_tree_read_lock(b); 2405f3a84ccdSFilipe Manana b = btrfs_tree_mod_log_rewind(fs_info, p, b, time_seq); 2406db7f3436SJosef Bacik if (!b) { 2407db7f3436SJosef Bacik ret = -ENOMEM; 2408db7f3436SJosef Bacik goto done; 2409db7f3436SJosef Bacik } 24105d9e75c4SJan Schmidt p->locks[level] = BTRFS_READ_LOCK; 24115d9e75c4SJan Schmidt p->nodes[level] = b; 24125d9e75c4SJan Schmidt } 24135d9e75c4SJan Schmidt ret = 1; 24145d9e75c4SJan Schmidt done: 24155d9e75c4SJan Schmidt if (ret < 0) 24165d9e75c4SJan Schmidt btrfs_release_path(p); 24175d9e75c4SJan Schmidt 24185d9e75c4SJan Schmidt return ret; 24195d9e75c4SJan Schmidt } 24205d9e75c4SJan Schmidt 24215d9e75c4SJan Schmidt /* 2422f469c8bdSFilipe Manana * Search the tree again to find a leaf with smaller keys. 2423f469c8bdSFilipe Manana * Returns 0 if it found something. 2424f469c8bdSFilipe Manana * Returns 1 if there are no smaller keys. 2425f469c8bdSFilipe Manana * Returns < 0 on error. 2426f469c8bdSFilipe Manana * 2427f469c8bdSFilipe Manana * This may release the path, and so you may lose any locks held at the 2428f469c8bdSFilipe Manana * time you call it. 2429f469c8bdSFilipe Manana */ 2430f469c8bdSFilipe Manana static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 2431f469c8bdSFilipe Manana { 2432f469c8bdSFilipe Manana struct btrfs_key key; 2433f469c8bdSFilipe Manana struct btrfs_key orig_key; 2434f469c8bdSFilipe Manana struct btrfs_disk_key found_key; 2435f469c8bdSFilipe Manana int ret; 2436f469c8bdSFilipe Manana 2437f469c8bdSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 0); 2438f469c8bdSFilipe Manana orig_key = key; 2439f469c8bdSFilipe Manana 2440f469c8bdSFilipe Manana if (key.offset > 0) { 2441f469c8bdSFilipe Manana key.offset--; 2442f469c8bdSFilipe Manana } else if (key.type > 0) { 2443f469c8bdSFilipe Manana key.type--; 2444f469c8bdSFilipe Manana key.offset = (u64)-1; 2445f469c8bdSFilipe Manana } else if (key.objectid > 0) { 2446f469c8bdSFilipe Manana key.objectid--; 2447f469c8bdSFilipe Manana key.type = (u8)-1; 2448f469c8bdSFilipe Manana key.offset = (u64)-1; 2449f469c8bdSFilipe Manana } else { 2450f469c8bdSFilipe Manana return 1; 2451f469c8bdSFilipe Manana } 2452f469c8bdSFilipe Manana 2453f469c8bdSFilipe Manana btrfs_release_path(path); 2454f469c8bdSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2455f469c8bdSFilipe Manana if (ret <= 0) 2456f469c8bdSFilipe Manana return ret; 2457f469c8bdSFilipe Manana 2458f469c8bdSFilipe Manana /* 2459f469c8bdSFilipe Manana * Previous key not found. Even if we were at slot 0 of the leaf we had 2460f469c8bdSFilipe Manana * before releasing the path and calling btrfs_search_slot(), we now may 2461f469c8bdSFilipe Manana * be in a slot pointing to the same original key - this can happen if 2462f469c8bdSFilipe Manana * after we released the path, one of more items were moved from a 2463f469c8bdSFilipe Manana * sibling leaf into the front of the leaf we had due to an insertion 2464f469c8bdSFilipe Manana * (see push_leaf_right()). 2465f469c8bdSFilipe Manana * If we hit this case and our slot is > 0 and just decrement the slot 2466f469c8bdSFilipe Manana * so that the caller does not process the same key again, which may or 2467f469c8bdSFilipe Manana * may not break the caller, depending on its logic. 2468f469c8bdSFilipe Manana */ 2469f469c8bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) { 2470f469c8bdSFilipe Manana btrfs_item_key(path->nodes[0], &found_key, path->slots[0]); 2471f469c8bdSFilipe Manana ret = comp_keys(&found_key, &orig_key); 2472f469c8bdSFilipe Manana if (ret == 0) { 2473f469c8bdSFilipe Manana if (path->slots[0] > 0) { 2474f469c8bdSFilipe Manana path->slots[0]--; 2475f469c8bdSFilipe Manana return 0; 2476f469c8bdSFilipe Manana } 2477f469c8bdSFilipe Manana /* 2478f469c8bdSFilipe Manana * At slot 0, same key as before, it means orig_key is 2479f469c8bdSFilipe Manana * the lowest, leftmost, key in the tree. We're done. 2480f469c8bdSFilipe Manana */ 2481f469c8bdSFilipe Manana return 1; 2482f469c8bdSFilipe Manana } 2483f469c8bdSFilipe Manana } 2484f469c8bdSFilipe Manana 2485f469c8bdSFilipe Manana btrfs_item_key(path->nodes[0], &found_key, 0); 2486f469c8bdSFilipe Manana ret = comp_keys(&found_key, &key); 2487f469c8bdSFilipe Manana /* 2488f469c8bdSFilipe Manana * We might have had an item with the previous key in the tree right 2489f469c8bdSFilipe Manana * before we released our path. And after we released our path, that 2490f469c8bdSFilipe Manana * item might have been pushed to the first slot (0) of the leaf we 2491f469c8bdSFilipe Manana * were holding due to a tree balance. Alternatively, an item with the 2492f469c8bdSFilipe Manana * previous key can exist as the only element of a leaf (big fat item). 2493f469c8bdSFilipe Manana * Therefore account for these 2 cases, so that our callers (like 2494f469c8bdSFilipe Manana * btrfs_previous_item) don't miss an existing item with a key matching 2495f469c8bdSFilipe Manana * the previous key we computed above. 2496f469c8bdSFilipe Manana */ 2497f469c8bdSFilipe Manana if (ret <= 0) 2498f469c8bdSFilipe Manana return 0; 2499f469c8bdSFilipe Manana return 1; 2500f469c8bdSFilipe Manana } 2501f469c8bdSFilipe Manana 2502f469c8bdSFilipe Manana /* 25032f38b3e1SArne Jansen * helper to use instead of search slot if no exact match is needed but 25042f38b3e1SArne Jansen * instead the next or previous item should be returned. 25052f38b3e1SArne Jansen * When find_higher is true, the next higher item is returned, the next lower 25062f38b3e1SArne Jansen * otherwise. 25072f38b3e1SArne Jansen * When return_any and find_higher are both true, and no higher item is found, 25082f38b3e1SArne Jansen * return the next lower instead. 25092f38b3e1SArne Jansen * When return_any is true and find_higher is false, and no lower item is found, 25102f38b3e1SArne Jansen * return the next higher instead. 25112f38b3e1SArne Jansen * It returns 0 if any item is found, 1 if none is found (tree empty), and 25122f38b3e1SArne Jansen * < 0 on error 25132f38b3e1SArne Jansen */ 25142f38b3e1SArne Jansen int btrfs_search_slot_for_read(struct btrfs_root *root, 2515310712b2SOmar Sandoval const struct btrfs_key *key, 2516310712b2SOmar Sandoval struct btrfs_path *p, int find_higher, 2517310712b2SOmar Sandoval int return_any) 25182f38b3e1SArne Jansen { 25192f38b3e1SArne Jansen int ret; 25202f38b3e1SArne Jansen struct extent_buffer *leaf; 25212f38b3e1SArne Jansen 25222f38b3e1SArne Jansen again: 25232f38b3e1SArne Jansen ret = btrfs_search_slot(NULL, root, key, p, 0, 0); 25242f38b3e1SArne Jansen if (ret <= 0) 25252f38b3e1SArne Jansen return ret; 25262f38b3e1SArne Jansen /* 25272f38b3e1SArne Jansen * a return value of 1 means the path is at the position where the 25282f38b3e1SArne Jansen * item should be inserted. Normally this is the next bigger item, 25292f38b3e1SArne Jansen * but in case the previous item is the last in a leaf, path points 25302f38b3e1SArne Jansen * to the first free slot in the previous leaf, i.e. at an invalid 25312f38b3e1SArne Jansen * item. 25322f38b3e1SArne Jansen */ 25332f38b3e1SArne Jansen leaf = p->nodes[0]; 25342f38b3e1SArne Jansen 25352f38b3e1SArne Jansen if (find_higher) { 25362f38b3e1SArne Jansen if (p->slots[0] >= btrfs_header_nritems(leaf)) { 25372f38b3e1SArne Jansen ret = btrfs_next_leaf(root, p); 25382f38b3e1SArne Jansen if (ret <= 0) 25392f38b3e1SArne Jansen return ret; 25402f38b3e1SArne Jansen if (!return_any) 25412f38b3e1SArne Jansen return 1; 25422f38b3e1SArne Jansen /* 25432f38b3e1SArne Jansen * no higher item found, return the next 25442f38b3e1SArne Jansen * lower instead 25452f38b3e1SArne Jansen */ 25462f38b3e1SArne Jansen return_any = 0; 25472f38b3e1SArne Jansen find_higher = 0; 25482f38b3e1SArne Jansen btrfs_release_path(p); 25492f38b3e1SArne Jansen goto again; 25502f38b3e1SArne Jansen } 25512f38b3e1SArne Jansen } else { 25522f38b3e1SArne Jansen if (p->slots[0] == 0) { 25532f38b3e1SArne Jansen ret = btrfs_prev_leaf(root, p); 2554e6793769SArne Jansen if (ret < 0) 25552f38b3e1SArne Jansen return ret; 2556e6793769SArne Jansen if (!ret) { 255723c6bf6aSFilipe David Borba Manana leaf = p->nodes[0]; 255823c6bf6aSFilipe David Borba Manana if (p->slots[0] == btrfs_header_nritems(leaf)) 255923c6bf6aSFilipe David Borba Manana p->slots[0]--; 2560e6793769SArne Jansen return 0; 2561e6793769SArne Jansen } 25622f38b3e1SArne Jansen if (!return_any) 25632f38b3e1SArne Jansen return 1; 25642f38b3e1SArne Jansen /* 25652f38b3e1SArne Jansen * no lower item found, return the next 25662f38b3e1SArne Jansen * higher instead 25672f38b3e1SArne Jansen */ 25682f38b3e1SArne Jansen return_any = 0; 25692f38b3e1SArne Jansen find_higher = 1; 25702f38b3e1SArne Jansen btrfs_release_path(p); 25712f38b3e1SArne Jansen goto again; 2572e6793769SArne Jansen } else { 25732f38b3e1SArne Jansen --p->slots[0]; 25742f38b3e1SArne Jansen } 25752f38b3e1SArne Jansen } 25762f38b3e1SArne Jansen return 0; 25772f38b3e1SArne Jansen } 25782f38b3e1SArne Jansen 25792f38b3e1SArne Jansen /* 25800ff40a91SMarcos Paulo de Souza * Execute search and call btrfs_previous_item to traverse backwards if the item 25810ff40a91SMarcos Paulo de Souza * was not found. 25820ff40a91SMarcos Paulo de Souza * 25830ff40a91SMarcos Paulo de Souza * Return 0 if found, 1 if not found and < 0 if error. 25840ff40a91SMarcos Paulo de Souza */ 25850ff40a91SMarcos Paulo de Souza int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key, 25860ff40a91SMarcos Paulo de Souza struct btrfs_path *path) 25870ff40a91SMarcos Paulo de Souza { 25880ff40a91SMarcos Paulo de Souza int ret; 25890ff40a91SMarcos Paulo de Souza 25900ff40a91SMarcos Paulo de Souza ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 25910ff40a91SMarcos Paulo de Souza if (ret > 0) 25920ff40a91SMarcos Paulo de Souza ret = btrfs_previous_item(root, path, key->objectid, key->type); 25930ff40a91SMarcos Paulo de Souza 25940ff40a91SMarcos Paulo de Souza if (ret == 0) 25950ff40a91SMarcos Paulo de Souza btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]); 25960ff40a91SMarcos Paulo de Souza 25970ff40a91SMarcos Paulo de Souza return ret; 25980ff40a91SMarcos Paulo de Souza } 25990ff40a91SMarcos Paulo de Souza 260043dd529aSDavid Sterba /* 260162142be3SGabriel Niebler * Search for a valid slot for the given path. 260262142be3SGabriel Niebler * 260362142be3SGabriel Niebler * @root: The root node of the tree. 260462142be3SGabriel Niebler * @key: Will contain a valid item if found. 260562142be3SGabriel Niebler * @path: The starting point to validate the slot. 260662142be3SGabriel Niebler * 260762142be3SGabriel Niebler * Return: 0 if the item is valid 260862142be3SGabriel Niebler * 1 if not found 260962142be3SGabriel Niebler * <0 if error. 261062142be3SGabriel Niebler */ 261162142be3SGabriel Niebler int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key, 261262142be3SGabriel Niebler struct btrfs_path *path) 261362142be3SGabriel Niebler { 2614524f14bbSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 261562142be3SGabriel Niebler int ret; 261662142be3SGabriel Niebler 261762142be3SGabriel Niebler ret = btrfs_next_leaf(root, path); 261862142be3SGabriel Niebler if (ret) 261962142be3SGabriel Niebler return ret; 262062142be3SGabriel Niebler } 2621524f14bbSFilipe Manana 2622524f14bbSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]); 262362142be3SGabriel Niebler return 0; 262462142be3SGabriel Niebler } 262562142be3SGabriel Niebler 26260ff40a91SMarcos Paulo de Souza /* 262774123bd7SChris Mason * adjust the pointers going up the tree, starting at level 262874123bd7SChris Mason * making sure the right key of each node is points to 'key'. 262974123bd7SChris Mason * This is used after shifting pointers to the left, so it stops 263074123bd7SChris Mason * fixing up pointers when a given leaf/node is not in slot 0 of the 263174123bd7SChris Mason * higher levels 2632aa5d6bedSChris Mason * 263374123bd7SChris Mason */ 2634b167fa91SNikolay Borisov static void fixup_low_keys(struct btrfs_path *path, 26355f39d397SChris Mason struct btrfs_disk_key *key, int level) 2636be0e5c09SChris Mason { 2637be0e5c09SChris Mason int i; 26385f39d397SChris Mason struct extent_buffer *t; 26390e82bcfeSDavid Sterba int ret; 26405f39d397SChris Mason 2641234b63a0SChris Mason for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2642be0e5c09SChris Mason int tslot = path->slots[i]; 26430e82bcfeSDavid Sterba 2644eb60ceacSChris Mason if (!path->nodes[i]) 2645be0e5c09SChris Mason break; 26465f39d397SChris Mason t = path->nodes[i]; 2647f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_key(t, tslot, 264833cff222SFilipe Manana BTRFS_MOD_LOG_KEY_REPLACE); 26490e82bcfeSDavid Sterba BUG_ON(ret < 0); 26505f39d397SChris Mason btrfs_set_node_key(t, key, tslot); 2651d6025579SChris Mason btrfs_mark_buffer_dirty(path->nodes[i]); 2652be0e5c09SChris Mason if (tslot != 0) 2653be0e5c09SChris Mason break; 2654be0e5c09SChris Mason } 2655be0e5c09SChris Mason } 2656be0e5c09SChris Mason 265774123bd7SChris Mason /* 265831840ae1SZheng Yan * update item key. 265931840ae1SZheng Yan * 266031840ae1SZheng Yan * This function isn't completely safe. It's the caller's responsibility 266131840ae1SZheng Yan * that the new key won't break the order 266231840ae1SZheng Yan */ 2663b7a0365eSDaniel Dressler void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, 2664b7a0365eSDaniel Dressler struct btrfs_path *path, 2665310712b2SOmar Sandoval const struct btrfs_key *new_key) 266631840ae1SZheng Yan { 266731840ae1SZheng Yan struct btrfs_disk_key disk_key; 266831840ae1SZheng Yan struct extent_buffer *eb; 266931840ae1SZheng Yan int slot; 267031840ae1SZheng Yan 267131840ae1SZheng Yan eb = path->nodes[0]; 267231840ae1SZheng Yan slot = path->slots[0]; 267331840ae1SZheng Yan if (slot > 0) { 267431840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot - 1); 26757c15d410SQu Wenruo if (unlikely(comp_keys(&disk_key, new_key) >= 0)) { 2676eee3b811SQu Wenruo btrfs_print_leaf(eb); 26777c15d410SQu Wenruo btrfs_crit(fs_info, 26787c15d410SQu Wenruo "slot %u key (%llu %u %llu) new key (%llu %u %llu)", 26797c15d410SQu Wenruo slot, btrfs_disk_key_objectid(&disk_key), 26807c15d410SQu Wenruo btrfs_disk_key_type(&disk_key), 26817c15d410SQu Wenruo btrfs_disk_key_offset(&disk_key), 26827c15d410SQu Wenruo new_key->objectid, new_key->type, 26837c15d410SQu Wenruo new_key->offset); 26847c15d410SQu Wenruo BUG(); 26857c15d410SQu Wenruo } 268631840ae1SZheng Yan } 268731840ae1SZheng Yan if (slot < btrfs_header_nritems(eb) - 1) { 268831840ae1SZheng Yan btrfs_item_key(eb, &disk_key, slot + 1); 26897c15d410SQu Wenruo if (unlikely(comp_keys(&disk_key, new_key) <= 0)) { 2690eee3b811SQu Wenruo btrfs_print_leaf(eb); 26917c15d410SQu Wenruo btrfs_crit(fs_info, 26927c15d410SQu Wenruo "slot %u key (%llu %u %llu) new key (%llu %u %llu)", 26937c15d410SQu Wenruo slot, btrfs_disk_key_objectid(&disk_key), 26947c15d410SQu Wenruo btrfs_disk_key_type(&disk_key), 26957c15d410SQu Wenruo btrfs_disk_key_offset(&disk_key), 26967c15d410SQu Wenruo new_key->objectid, new_key->type, 26977c15d410SQu Wenruo new_key->offset); 26987c15d410SQu Wenruo BUG(); 26997c15d410SQu Wenruo } 270031840ae1SZheng Yan } 270131840ae1SZheng Yan 270231840ae1SZheng Yan btrfs_cpu_key_to_disk(&disk_key, new_key); 270331840ae1SZheng Yan btrfs_set_item_key(eb, &disk_key, slot); 270431840ae1SZheng Yan btrfs_mark_buffer_dirty(eb); 270531840ae1SZheng Yan if (slot == 0) 2706b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 270731840ae1SZheng Yan } 270831840ae1SZheng Yan 270931840ae1SZheng Yan /* 2710d16c702fSQu Wenruo * Check key order of two sibling extent buffers. 2711d16c702fSQu Wenruo * 2712d16c702fSQu Wenruo * Return true if something is wrong. 2713d16c702fSQu Wenruo * Return false if everything is fine. 2714d16c702fSQu Wenruo * 2715d16c702fSQu Wenruo * Tree-checker only works inside one tree block, thus the following 2716d16c702fSQu Wenruo * corruption can not be detected by tree-checker: 2717d16c702fSQu Wenruo * 2718d16c702fSQu Wenruo * Leaf @left | Leaf @right 2719d16c702fSQu Wenruo * -------------------------------------------------------------- 2720d16c702fSQu Wenruo * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 | 2721d16c702fSQu Wenruo * 2722d16c702fSQu Wenruo * Key f6 in leaf @left itself is valid, but not valid when the next 2723d16c702fSQu Wenruo * key in leaf @right is 7. 2724d16c702fSQu Wenruo * This can only be checked at tree block merge time. 2725d16c702fSQu Wenruo * And since tree checker has ensured all key order in each tree block 2726d16c702fSQu Wenruo * is correct, we only need to bother the last key of @left and the first 2727d16c702fSQu Wenruo * key of @right. 2728d16c702fSQu Wenruo */ 2729d16c702fSQu Wenruo static bool check_sibling_keys(struct extent_buffer *left, 2730d16c702fSQu Wenruo struct extent_buffer *right) 2731d16c702fSQu Wenruo { 2732d16c702fSQu Wenruo struct btrfs_key left_last; 2733d16c702fSQu Wenruo struct btrfs_key right_first; 2734d16c702fSQu Wenruo int level = btrfs_header_level(left); 2735d16c702fSQu Wenruo int nr_left = btrfs_header_nritems(left); 2736d16c702fSQu Wenruo int nr_right = btrfs_header_nritems(right); 2737d16c702fSQu Wenruo 2738d16c702fSQu Wenruo /* No key to check in one of the tree blocks */ 2739d16c702fSQu Wenruo if (!nr_left || !nr_right) 2740d16c702fSQu Wenruo return false; 2741d16c702fSQu Wenruo 2742d16c702fSQu Wenruo if (level) { 2743d16c702fSQu Wenruo btrfs_node_key_to_cpu(left, &left_last, nr_left - 1); 2744d16c702fSQu Wenruo btrfs_node_key_to_cpu(right, &right_first, 0); 2745d16c702fSQu Wenruo } else { 2746d16c702fSQu Wenruo btrfs_item_key_to_cpu(left, &left_last, nr_left - 1); 2747d16c702fSQu Wenruo btrfs_item_key_to_cpu(right, &right_first, 0); 2748d16c702fSQu Wenruo } 2749d16c702fSQu Wenruo 275088ad95b0SFilipe Manana if (unlikely(btrfs_comp_cpu_keys(&left_last, &right_first) >= 0)) { 2751a2cea677SFilipe Manana btrfs_crit(left->fs_info, "left extent buffer:"); 2752a2cea677SFilipe Manana btrfs_print_tree(left, false); 2753a2cea677SFilipe Manana btrfs_crit(left->fs_info, "right extent buffer:"); 2754a2cea677SFilipe Manana btrfs_print_tree(right, false); 2755d16c702fSQu Wenruo btrfs_crit(left->fs_info, 2756d16c702fSQu Wenruo "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)", 2757d16c702fSQu Wenruo left_last.objectid, left_last.type, 2758d16c702fSQu Wenruo left_last.offset, right_first.objectid, 2759d16c702fSQu Wenruo right_first.type, right_first.offset); 2760d16c702fSQu Wenruo return true; 2761d16c702fSQu Wenruo } 2762d16c702fSQu Wenruo return false; 2763d16c702fSQu Wenruo } 2764d16c702fSQu Wenruo 2765d16c702fSQu Wenruo /* 276674123bd7SChris Mason * try to push data from one node into the next node left in the 276779f95c82SChris Mason * tree. 2768aa5d6bedSChris Mason * 2769aa5d6bedSChris Mason * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 2770aa5d6bedSChris Mason * error, and > 0 if there was no room in the left hand block. 277174123bd7SChris Mason */ 277298ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans, 27732ff7e61eSJeff Mahoney struct extent_buffer *dst, 2774971a1f66SChris Mason struct extent_buffer *src, int empty) 2775be0e5c09SChris Mason { 2776d30a668fSDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 2777be0e5c09SChris Mason int push_items = 0; 2778bb803951SChris Mason int src_nritems; 2779bb803951SChris Mason int dst_nritems; 2780aa5d6bedSChris Mason int ret = 0; 2781be0e5c09SChris Mason 27825f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 27835f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 27840b246afaSJeff Mahoney push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; 27857bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 27867bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 278754aa1f4dSChris Mason 2788bce4eae9SChris Mason if (!empty && src_nritems <= 8) 2789971a1f66SChris Mason return 1; 2790971a1f66SChris Mason 2791d397712bSChris Mason if (push_items <= 0) 2792be0e5c09SChris Mason return 1; 2793be0e5c09SChris Mason 2794bce4eae9SChris Mason if (empty) { 2795971a1f66SChris Mason push_items = min(src_nritems, push_items); 2796bce4eae9SChris Mason if (push_items < src_nritems) { 2797bce4eae9SChris Mason /* leave at least 8 pointers in the node if 2798bce4eae9SChris Mason * we aren't going to empty it 2799bce4eae9SChris Mason */ 2800bce4eae9SChris Mason if (src_nritems - push_items < 8) { 2801bce4eae9SChris Mason if (push_items <= 8) 2802bce4eae9SChris Mason return 1; 2803bce4eae9SChris Mason push_items -= 8; 2804bce4eae9SChris Mason } 2805bce4eae9SChris Mason } 2806bce4eae9SChris Mason } else 2807bce4eae9SChris Mason push_items = min(src_nritems - 8, push_items); 280879f95c82SChris Mason 2809d16c702fSQu Wenruo /* dst is the left eb, src is the middle eb */ 2810d16c702fSQu Wenruo if (check_sibling_keys(dst, src)) { 2811d16c702fSQu Wenruo ret = -EUCLEAN; 2812d16c702fSQu Wenruo btrfs_abort_transaction(trans, ret); 2813d16c702fSQu Wenruo return ret; 2814d16c702fSQu Wenruo } 2815f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items); 28165de865eeSFilipe David Borba Manana if (ret) { 281766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 28185de865eeSFilipe David Borba Manana return ret; 28195de865eeSFilipe David Borba Manana } 28205f39d397SChris Mason copy_extent_buffer(dst, src, 2821e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(dst, dst_nritems), 2822e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(src, 0), 2823123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 28245f39d397SChris Mason 2825bb803951SChris Mason if (push_items < src_nritems) { 282657911b8bSJan Schmidt /* 28275cead542SBoris Burkov * btrfs_tree_mod_log_eb_copy handles logging the move, so we 28285cead542SBoris Burkov * don't need to do an explicit tree mod log operation for it. 282957911b8bSJan Schmidt */ 2830e23efd8eSJosef Bacik memmove_extent_buffer(src, btrfs_node_key_ptr_offset(src, 0), 2831e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(src, push_items), 2832e2fa7227SChris Mason (src_nritems - push_items) * 2833123abc88SChris Mason sizeof(struct btrfs_key_ptr)); 2834bb803951SChris Mason } 28355f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 28365f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 28375f39d397SChris Mason btrfs_mark_buffer_dirty(src); 28385f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 283931840ae1SZheng Yan 2840bb803951SChris Mason return ret; 2841be0e5c09SChris Mason } 2842be0e5c09SChris Mason 284397571fd0SChris Mason /* 284479f95c82SChris Mason * try to push data from one node into the next node right in the 284579f95c82SChris Mason * tree. 284679f95c82SChris Mason * 284779f95c82SChris Mason * returns 0 if some ptrs were pushed, < 0 if there was some horrible 284879f95c82SChris Mason * error, and > 0 if there was no room in the right hand block. 284979f95c82SChris Mason * 285079f95c82SChris Mason * this will only push up to 1/2 the contents of the left node over 285179f95c82SChris Mason */ 28525f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans, 28535f39d397SChris Mason struct extent_buffer *dst, 28545f39d397SChris Mason struct extent_buffer *src) 285579f95c82SChris Mason { 285655d32ed8SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 285779f95c82SChris Mason int push_items = 0; 285879f95c82SChris Mason int max_push; 285979f95c82SChris Mason int src_nritems; 286079f95c82SChris Mason int dst_nritems; 286179f95c82SChris Mason int ret = 0; 286279f95c82SChris Mason 28637bb86316SChris Mason WARN_ON(btrfs_header_generation(src) != trans->transid); 28647bb86316SChris Mason WARN_ON(btrfs_header_generation(dst) != trans->transid); 28657bb86316SChris Mason 28665f39d397SChris Mason src_nritems = btrfs_header_nritems(src); 28675f39d397SChris Mason dst_nritems = btrfs_header_nritems(dst); 28680b246afaSJeff Mahoney push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; 2869d397712bSChris Mason if (push_items <= 0) 287079f95c82SChris Mason return 1; 2871bce4eae9SChris Mason 2872d397712bSChris Mason if (src_nritems < 4) 2873bce4eae9SChris Mason return 1; 287479f95c82SChris Mason 287579f95c82SChris Mason max_push = src_nritems / 2 + 1; 287679f95c82SChris Mason /* don't try to empty the node */ 2877d397712bSChris Mason if (max_push >= src_nritems) 287879f95c82SChris Mason return 1; 2879252c38f0SYan 288079f95c82SChris Mason if (max_push < push_items) 288179f95c82SChris Mason push_items = max_push; 288279f95c82SChris Mason 2883d16c702fSQu Wenruo /* dst is the right eb, src is the middle eb */ 2884d16c702fSQu Wenruo if (check_sibling_keys(src, dst)) { 2885d16c702fSQu Wenruo ret = -EUCLEAN; 2886d16c702fSQu Wenruo btrfs_abort_transaction(trans, ret); 2887d16c702fSQu Wenruo return ret; 2888d16c702fSQu Wenruo } 28895cead542SBoris Burkov 28905cead542SBoris Burkov /* 28915cead542SBoris Burkov * btrfs_tree_mod_log_eb_copy handles logging the move, so we don't 28925cead542SBoris Burkov * need to do an explicit tree mod log operation for it. 28935cead542SBoris Burkov */ 2894e23efd8eSJosef Bacik memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(dst, push_items), 2895e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(dst, 0), 28965f39d397SChris Mason (dst_nritems) * 28975f39d397SChris Mason sizeof(struct btrfs_key_ptr)); 2898d6025579SChris Mason 2899f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items, 2900ed874f0dSDavid Sterba push_items); 29015de865eeSFilipe David Borba Manana if (ret) { 290266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 29035de865eeSFilipe David Borba Manana return ret; 29045de865eeSFilipe David Borba Manana } 29055f39d397SChris Mason copy_extent_buffer(dst, src, 2906e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(dst, 0), 2907e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(src, src_nritems - push_items), 2908123abc88SChris Mason push_items * sizeof(struct btrfs_key_ptr)); 290979f95c82SChris Mason 29105f39d397SChris Mason btrfs_set_header_nritems(src, src_nritems - push_items); 29115f39d397SChris Mason btrfs_set_header_nritems(dst, dst_nritems + push_items); 291279f95c82SChris Mason 29135f39d397SChris Mason btrfs_mark_buffer_dirty(src); 29145f39d397SChris Mason btrfs_mark_buffer_dirty(dst); 291531840ae1SZheng Yan 291679f95c82SChris Mason return ret; 291779f95c82SChris Mason } 291879f95c82SChris Mason 291979f95c82SChris Mason /* 292097571fd0SChris Mason * helper function to insert a new root level in the tree. 292197571fd0SChris Mason * A new node is allocated, and a single item is inserted to 292297571fd0SChris Mason * point to the existing root 2923aa5d6bedSChris Mason * 2924aa5d6bedSChris Mason * returns zero on success or < 0 on failure. 292597571fd0SChris Mason */ 2926d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans, 29275f39d397SChris Mason struct btrfs_root *root, 2928fdd99c72SLiu Bo struct btrfs_path *path, int level) 292974123bd7SChris Mason { 29300b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 29317bb86316SChris Mason u64 lower_gen; 29325f39d397SChris Mason struct extent_buffer *lower; 29335f39d397SChris Mason struct extent_buffer *c; 2934925baeddSChris Mason struct extent_buffer *old; 29355f39d397SChris Mason struct btrfs_disk_key lower_key; 2936d9d19a01SDavid Sterba int ret; 29375c680ed6SChris Mason 29385c680ed6SChris Mason BUG_ON(path->nodes[level]); 29395c680ed6SChris Mason BUG_ON(path->nodes[level-1] != root->node); 29405c680ed6SChris Mason 29417bb86316SChris Mason lower = path->nodes[level-1]; 29427bb86316SChris Mason if (level == 1) 29437bb86316SChris Mason btrfs_item_key(lower, &lower_key, 0); 29447bb86316SChris Mason else 29457bb86316SChris Mason btrfs_node_key(lower, &lower_key, 0); 29467bb86316SChris Mason 294779bd3712SFilipe Manana c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid, 294879bd3712SFilipe Manana &lower_key, level, root->node->start, 0, 2949cf6f34aaSJosef Bacik BTRFS_NESTING_NEW_ROOT); 29505f39d397SChris Mason if (IS_ERR(c)) 29515f39d397SChris Mason return PTR_ERR(c); 2952925baeddSChris Mason 29530b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 2954f0486c68SYan, Zheng 29555f39d397SChris Mason btrfs_set_header_nritems(c, 1); 29565f39d397SChris Mason btrfs_set_node_key(c, &lower_key, 0); 2957db94535dSChris Mason btrfs_set_node_blockptr(c, 0, lower->start); 29587bb86316SChris Mason lower_gen = btrfs_header_generation(lower); 295931840ae1SZheng Yan WARN_ON(lower_gen != trans->transid); 29607bb86316SChris Mason 29617bb86316SChris Mason btrfs_set_node_ptr_generation(c, 0, lower_gen); 29625f39d397SChris Mason 29635f39d397SChris Mason btrfs_mark_buffer_dirty(c); 2964d5719762SChris Mason 2965925baeddSChris Mason old = root->node; 2966406808abSFilipe Manana ret = btrfs_tree_mod_log_insert_root(root->node, c, false); 2967f61aa7baSFilipe Manana if (ret < 0) { 2968f61aa7baSFilipe Manana btrfs_free_tree_block(trans, btrfs_root_id(root), c, 0, 1); 2969f61aa7baSFilipe Manana btrfs_tree_unlock(c); 2970f61aa7baSFilipe Manana free_extent_buffer(c); 2971f61aa7baSFilipe Manana return ret; 2972f61aa7baSFilipe Manana } 2973240f62c8SChris Mason rcu_assign_pointer(root->node, c); 2974925baeddSChris Mason 2975925baeddSChris Mason /* the super has an extra ref to root->node */ 2976925baeddSChris Mason free_extent_buffer(old); 2977925baeddSChris Mason 29780b86a832SChris Mason add_root_to_dirty_list(root); 297967439dadSDavid Sterba atomic_inc(&c->refs); 29805f39d397SChris Mason path->nodes[level] = c; 2981ac5887c8SJosef Bacik path->locks[level] = BTRFS_WRITE_LOCK; 298274123bd7SChris Mason path->slots[level] = 0; 298374123bd7SChris Mason return 0; 298474123bd7SChris Mason } 29855c680ed6SChris Mason 29865c680ed6SChris Mason /* 29875c680ed6SChris Mason * worker function to insert a single pointer in a node. 29885c680ed6SChris Mason * the node should have enough room for the pointer already 298997571fd0SChris Mason * 29905c680ed6SChris Mason * slot and level indicate where you want the key to go, and 29915c680ed6SChris Mason * blocknr is the block the key points to. 29925c680ed6SChris Mason */ 2993*50b5d1fcSFilipe Manana static int insert_ptr(struct btrfs_trans_handle *trans, 29946ad3cf6dSDavid Sterba struct btrfs_path *path, 2995143bede5SJeff Mahoney struct btrfs_disk_key *key, u64 bytenr, 2996c3e06965SJan Schmidt int slot, int level) 29975c680ed6SChris Mason { 29985f39d397SChris Mason struct extent_buffer *lower; 29995c680ed6SChris Mason int nritems; 3000f3ea38daSJan Schmidt int ret; 30015c680ed6SChris Mason 30025c680ed6SChris Mason BUG_ON(!path->nodes[level]); 300349d0c642SFilipe Manana btrfs_assert_tree_write_locked(path->nodes[level]); 30045f39d397SChris Mason lower = path->nodes[level]; 30055f39d397SChris Mason nritems = btrfs_header_nritems(lower); 3006c293498bSStoyan Gaydarov BUG_ON(slot > nritems); 30076ad3cf6dSDavid Sterba BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info)); 300874123bd7SChris Mason if (slot != nritems) { 3009bf1d3425SDavid Sterba if (level) { 3010f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_move(lower, slot + 1, 3011f3a84ccdSFilipe Manana slot, nritems - slot); 3012*50b5d1fcSFilipe Manana if (ret < 0) { 3013*50b5d1fcSFilipe Manana btrfs_abort_transaction(trans, ret); 3014*50b5d1fcSFilipe Manana return ret; 3015*50b5d1fcSFilipe Manana } 3016bf1d3425SDavid Sterba } 30175f39d397SChris Mason memmove_extent_buffer(lower, 3018e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(lower, slot + 1), 3019e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(lower, slot), 3020123abc88SChris Mason (nritems - slot) * sizeof(struct btrfs_key_ptr)); 302174123bd7SChris Mason } 3022c3e06965SJan Schmidt if (level) { 3023f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_key(lower, slot, 302433cff222SFilipe Manana BTRFS_MOD_LOG_KEY_ADD); 3025*50b5d1fcSFilipe Manana if (ret < 0) { 3026*50b5d1fcSFilipe Manana btrfs_abort_transaction(trans, ret); 3027*50b5d1fcSFilipe Manana return ret; 3028*50b5d1fcSFilipe Manana } 3029f3ea38daSJan Schmidt } 30305f39d397SChris Mason btrfs_set_node_key(lower, key, slot); 3031db94535dSChris Mason btrfs_set_node_blockptr(lower, slot, bytenr); 303274493f7aSChris Mason WARN_ON(trans->transid == 0); 303374493f7aSChris Mason btrfs_set_node_ptr_generation(lower, slot, trans->transid); 30345f39d397SChris Mason btrfs_set_header_nritems(lower, nritems + 1); 30355f39d397SChris Mason btrfs_mark_buffer_dirty(lower); 3036*50b5d1fcSFilipe Manana 3037*50b5d1fcSFilipe Manana return 0; 303874123bd7SChris Mason } 303974123bd7SChris Mason 304097571fd0SChris Mason /* 304197571fd0SChris Mason * split the node at the specified level in path in two. 304297571fd0SChris Mason * The path is corrected to point to the appropriate node after the split 304397571fd0SChris Mason * 304497571fd0SChris Mason * Before splitting this tries to make some room in the node by pushing 304597571fd0SChris Mason * left and right, if either one works, it returns right away. 3046aa5d6bedSChris Mason * 3047aa5d6bedSChris Mason * returns 0 on success and < 0 on failure 304897571fd0SChris Mason */ 3049e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans, 3050e02119d5SChris Mason struct btrfs_root *root, 3051e02119d5SChris Mason struct btrfs_path *path, int level) 3052be0e5c09SChris Mason { 30530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 30545f39d397SChris Mason struct extent_buffer *c; 30555f39d397SChris Mason struct extent_buffer *split; 30565f39d397SChris Mason struct btrfs_disk_key disk_key; 3057be0e5c09SChris Mason int mid; 30585c680ed6SChris Mason int ret; 30597518a238SChris Mason u32 c_nritems; 3060be0e5c09SChris Mason 30615f39d397SChris Mason c = path->nodes[level]; 30627bb86316SChris Mason WARN_ON(btrfs_header_generation(c) != trans->transid); 30635f39d397SChris Mason if (c == root->node) { 3064d9abbf1cSJan Schmidt /* 306590f8d62eSJan Schmidt * trying to split the root, lets make a new one 306690f8d62eSJan Schmidt * 3067fdd99c72SLiu Bo * tree mod log: We don't log_removal old root in 306890f8d62eSJan Schmidt * insert_new_root, because that root buffer will be kept as a 306990f8d62eSJan Schmidt * normal node. We are going to log removal of half of the 3070f3a84ccdSFilipe Manana * elements below with btrfs_tree_mod_log_eb_copy(). We're 3071f3a84ccdSFilipe Manana * holding a tree lock on the buffer, which is why we cannot 3072f3a84ccdSFilipe Manana * race with other tree_mod_log users. 3073d9abbf1cSJan Schmidt */ 3074fdd99c72SLiu Bo ret = insert_new_root(trans, root, path, level + 1); 30755c680ed6SChris Mason if (ret) 30765c680ed6SChris Mason return ret; 3077b3612421SChris Mason } else { 3078e66f709bSChris Mason ret = push_nodes_for_insert(trans, root, path, level); 30795f39d397SChris Mason c = path->nodes[level]; 30805f39d397SChris Mason if (!ret && btrfs_header_nritems(c) < 30810b246afaSJeff Mahoney BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) 3082e66f709bSChris Mason return 0; 308354aa1f4dSChris Mason if (ret < 0) 308454aa1f4dSChris Mason return ret; 30855c680ed6SChris Mason } 3086e66f709bSChris Mason 30875f39d397SChris Mason c_nritems = btrfs_header_nritems(c); 30885d4f98a2SYan Zheng mid = (c_nritems + 1) / 2; 30895d4f98a2SYan Zheng btrfs_node_key(c, &disk_key, mid); 30907bb86316SChris Mason 309179bd3712SFilipe Manana split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid, 309279bd3712SFilipe Manana &disk_key, level, c->start, 0, 309379bd3712SFilipe Manana BTRFS_NESTING_SPLIT); 30945f39d397SChris Mason if (IS_ERR(split)) 30955f39d397SChris Mason return PTR_ERR(split); 309654aa1f4dSChris Mason 30970b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 3098bc877d28SNikolay Borisov ASSERT(btrfs_header_level(c) == level); 30995f39d397SChris Mason 3100f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid); 31015de865eeSFilipe David Borba Manana if (ret) { 3102ede600e4SFilipe Manana btrfs_tree_unlock(split); 3103ede600e4SFilipe Manana free_extent_buffer(split); 310466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 31055de865eeSFilipe David Borba Manana return ret; 31065de865eeSFilipe David Borba Manana } 31075f39d397SChris Mason copy_extent_buffer(split, c, 3108e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(split, 0), 3109e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(c, mid), 3110123abc88SChris Mason (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 31115f39d397SChris Mason btrfs_set_header_nritems(split, c_nritems - mid); 31125f39d397SChris Mason btrfs_set_header_nritems(c, mid); 3113aa5d6bedSChris Mason 31145f39d397SChris Mason btrfs_mark_buffer_dirty(c); 31155f39d397SChris Mason btrfs_mark_buffer_dirty(split); 31165f39d397SChris Mason 3117*50b5d1fcSFilipe Manana ret = insert_ptr(trans, path, &disk_key, split->start, 3118c3e06965SJan Schmidt path->slots[level + 1] + 1, level + 1); 3119*50b5d1fcSFilipe Manana if (ret < 0) { 3120*50b5d1fcSFilipe Manana btrfs_tree_unlock(split); 3121*50b5d1fcSFilipe Manana free_extent_buffer(split); 3122*50b5d1fcSFilipe Manana return ret; 3123*50b5d1fcSFilipe Manana } 3124aa5d6bedSChris Mason 31255de08d7dSChris Mason if (path->slots[level] >= mid) { 31265c680ed6SChris Mason path->slots[level] -= mid; 3127925baeddSChris Mason btrfs_tree_unlock(c); 31285f39d397SChris Mason free_extent_buffer(c); 31295f39d397SChris Mason path->nodes[level] = split; 31305c680ed6SChris Mason path->slots[level + 1] += 1; 3131eb60ceacSChris Mason } else { 3132925baeddSChris Mason btrfs_tree_unlock(split); 31335f39d397SChris Mason free_extent_buffer(split); 3134be0e5c09SChris Mason } 3135d5286a92SNikolay Borisov return 0; 3136be0e5c09SChris Mason } 3137be0e5c09SChris Mason 313874123bd7SChris Mason /* 313974123bd7SChris Mason * how many bytes are required to store the items in a leaf. start 314074123bd7SChris Mason * and nr indicate which items in the leaf to check. This totals up the 314174123bd7SChris Mason * space used both by the item structs and the item data 314274123bd7SChris Mason */ 31436c75a589SQu Wenruo static int leaf_space_used(const struct extent_buffer *l, int start, int nr) 3144be0e5c09SChris Mason { 3145be0e5c09SChris Mason int data_len; 31465f39d397SChris Mason int nritems = btrfs_header_nritems(l); 3147d4dbff95SChris Mason int end = min(nritems, start + nr) - 1; 3148be0e5c09SChris Mason 3149be0e5c09SChris Mason if (!nr) 3150be0e5c09SChris Mason return 0; 31513212fa14SJosef Bacik data_len = btrfs_item_offset(l, start) + btrfs_item_size(l, start); 31523212fa14SJosef Bacik data_len = data_len - btrfs_item_offset(l, end); 31530783fcfcSChris Mason data_len += sizeof(struct btrfs_item) * nr; 3154d4dbff95SChris Mason WARN_ON(data_len < 0); 3155be0e5c09SChris Mason return data_len; 3156be0e5c09SChris Mason } 3157be0e5c09SChris Mason 315874123bd7SChris Mason /* 3159d4dbff95SChris Mason * The space between the end of the leaf items and 3160d4dbff95SChris Mason * the start of the leaf data. IOW, how much room 3161d4dbff95SChris Mason * the leaf has left for both items and data 3162d4dbff95SChris Mason */ 31636c75a589SQu Wenruo int btrfs_leaf_free_space(const struct extent_buffer *leaf) 3164d4dbff95SChris Mason { 3165e902baacSDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 31665f39d397SChris Mason int nritems = btrfs_header_nritems(leaf); 31675f39d397SChris Mason int ret; 31680b246afaSJeff Mahoney 31690b246afaSJeff Mahoney ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems); 31705f39d397SChris Mason if (ret < 0) { 31710b246afaSJeff Mahoney btrfs_crit(fs_info, 3172efe120a0SFrank Holton "leaf free space ret %d, leaf data size %lu, used %d nritems %d", 3173da17066cSJeff Mahoney ret, 31740b246afaSJeff Mahoney (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info), 31755f39d397SChris Mason leaf_space_used(leaf, 0, nritems), nritems); 31765f39d397SChris Mason } 31775f39d397SChris Mason return ret; 3178d4dbff95SChris Mason } 3179d4dbff95SChris Mason 318099d8f83cSChris Mason /* 318199d8f83cSChris Mason * min slot controls the lowest index we're willing to push to the 318299d8f83cSChris Mason * right. We'll push up to and including min_slot, but no lower 318399d8f83cSChris Mason */ 3184ed25dab3SJosef Bacik static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, 3185ed25dab3SJosef Bacik struct btrfs_path *path, 318644871b1bSChris Mason int data_size, int empty, 318744871b1bSChris Mason struct extent_buffer *right, 318899d8f83cSChris Mason int free_space, u32 left_nritems, 318999d8f83cSChris Mason u32 min_slot) 319000ec4c51SChris Mason { 3191f72f0010SDavid Sterba struct btrfs_fs_info *fs_info = right->fs_info; 31925f39d397SChris Mason struct extent_buffer *left = path->nodes[0]; 319344871b1bSChris Mason struct extent_buffer *upper = path->nodes[1]; 3194cfed81a0SChris Mason struct btrfs_map_token token; 31955f39d397SChris Mason struct btrfs_disk_key disk_key; 319600ec4c51SChris Mason int slot; 319734a38218SChris Mason u32 i; 319800ec4c51SChris Mason int push_space = 0; 319900ec4c51SChris Mason int push_items = 0; 320034a38218SChris Mason u32 nr; 32017518a238SChris Mason u32 right_nritems; 32025f39d397SChris Mason u32 data_end; 3203db94535dSChris Mason u32 this_item_size; 320400ec4c51SChris Mason 320534a38218SChris Mason if (empty) 320634a38218SChris Mason nr = 0; 320734a38218SChris Mason else 320899d8f83cSChris Mason nr = max_t(u32, 1, min_slot); 320934a38218SChris Mason 321031840ae1SZheng Yan if (path->slots[0] >= left_nritems) 321187b29b20SYan Zheng push_space += data_size; 321231840ae1SZheng Yan 321344871b1bSChris Mason slot = path->slots[1]; 321434a38218SChris Mason i = left_nritems - 1; 321534a38218SChris Mason while (i >= nr) { 321631840ae1SZheng Yan if (!empty && push_items > 0) { 321731840ae1SZheng Yan if (path->slots[0] > i) 321831840ae1SZheng Yan break; 321931840ae1SZheng Yan if (path->slots[0] == i) { 3220e902baacSDavid Sterba int space = btrfs_leaf_free_space(left); 3221e902baacSDavid Sterba 322231840ae1SZheng Yan if (space + push_space * 2 > free_space) 322331840ae1SZheng Yan break; 322431840ae1SZheng Yan } 322531840ae1SZheng Yan } 322631840ae1SZheng Yan 322700ec4c51SChris Mason if (path->slots[0] == i) 322887b29b20SYan Zheng push_space += data_size; 3229db94535dSChris Mason 32303212fa14SJosef Bacik this_item_size = btrfs_item_size(left, i); 323174794207SJosef Bacik if (this_item_size + sizeof(struct btrfs_item) + 323274794207SJosef Bacik push_space > free_space) 323300ec4c51SChris Mason break; 323431840ae1SZheng Yan 323500ec4c51SChris Mason push_items++; 323674794207SJosef Bacik push_space += this_item_size + sizeof(struct btrfs_item); 323734a38218SChris Mason if (i == 0) 323834a38218SChris Mason break; 323934a38218SChris Mason i--; 3240db94535dSChris Mason } 32415f39d397SChris Mason 3242925baeddSChris Mason if (push_items == 0) 3243925baeddSChris Mason goto out_unlock; 32445f39d397SChris Mason 32456c1500f2SJulia Lawall WARN_ON(!empty && push_items == left_nritems); 32465f39d397SChris Mason 324700ec4c51SChris Mason /* push left to right */ 32485f39d397SChris Mason right_nritems = btrfs_header_nritems(right); 324934a38218SChris Mason 3250dc2e724eSJosef Bacik push_space = btrfs_item_data_end(left, left_nritems - push_items); 32518f881e8cSDavid Sterba push_space -= leaf_data_end(left); 32525f39d397SChris Mason 325300ec4c51SChris Mason /* make room in the right data area */ 32548f881e8cSDavid Sterba data_end = leaf_data_end(right); 3255637e3b48SJosef Bacik memmove_leaf_data(right, data_end - push_space, data_end, 32560b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - data_end); 32575f39d397SChris Mason 325800ec4c51SChris Mason /* copy from the left data area */ 3259637e3b48SJosef Bacik copy_leaf_data(right, left, BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 3260637e3b48SJosef Bacik leaf_data_end(left), push_space); 32615f39d397SChris Mason 3262637e3b48SJosef Bacik memmove_leaf_items(right, push_items, 0, right_nritems); 32635f39d397SChris Mason 326400ec4c51SChris Mason /* copy the items from left to right */ 3265637e3b48SJosef Bacik copy_leaf_items(right, left, 0, left_nritems - push_items, push_items); 326600ec4c51SChris Mason 326700ec4c51SChris Mason /* update the item pointers */ 3268c82f823cSDavid Sterba btrfs_init_map_token(&token, right); 32697518a238SChris Mason right_nritems += push_items; 32705f39d397SChris Mason btrfs_set_header_nritems(right, right_nritems); 32710b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info); 32727518a238SChris Mason for (i = 0; i < right_nritems; i++) { 32733212fa14SJosef Bacik push_space -= btrfs_token_item_size(&token, i); 32743212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, push_space); 3275db94535dSChris Mason } 3276db94535dSChris Mason 32777518a238SChris Mason left_nritems -= push_items; 32785f39d397SChris Mason btrfs_set_header_nritems(left, left_nritems); 327900ec4c51SChris Mason 328034a38218SChris Mason if (left_nritems) 32815f39d397SChris Mason btrfs_mark_buffer_dirty(left); 3282f0486c68SYan, Zheng else 3283190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, left); 3284f0486c68SYan, Zheng 32855f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3286a429e513SChris Mason 32875f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 32885f39d397SChris Mason btrfs_set_node_key(upper, &disk_key, slot + 1); 3289d6025579SChris Mason btrfs_mark_buffer_dirty(upper); 329002217ed2SChris Mason 329100ec4c51SChris Mason /* then fixup the leaf pointer in the path */ 32927518a238SChris Mason if (path->slots[0] >= left_nritems) { 32937518a238SChris Mason path->slots[0] -= left_nritems; 3294925baeddSChris Mason if (btrfs_header_nritems(path->nodes[0]) == 0) 3295190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, path->nodes[0]); 3296925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 32975f39d397SChris Mason free_extent_buffer(path->nodes[0]); 32985f39d397SChris Mason path->nodes[0] = right; 329900ec4c51SChris Mason path->slots[1] += 1; 330000ec4c51SChris Mason } else { 3301925baeddSChris Mason btrfs_tree_unlock(right); 33025f39d397SChris Mason free_extent_buffer(right); 330300ec4c51SChris Mason } 330400ec4c51SChris Mason return 0; 3305925baeddSChris Mason 3306925baeddSChris Mason out_unlock: 3307925baeddSChris Mason btrfs_tree_unlock(right); 3308925baeddSChris Mason free_extent_buffer(right); 3309925baeddSChris Mason return 1; 331000ec4c51SChris Mason } 3311925baeddSChris Mason 331200ec4c51SChris Mason /* 331344871b1bSChris Mason * push some data in the path leaf to the right, trying to free up at 331474123bd7SChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 331544871b1bSChris Mason * 331644871b1bSChris Mason * returns 1 if the push failed because the other node didn't have enough 331744871b1bSChris Mason * room, 0 if everything worked out and < 0 if there were major errors. 331899d8f83cSChris Mason * 331999d8f83cSChris Mason * this will push starting from min_slot to the end of the leaf. It won't 332099d8f83cSChris Mason * push any slot lower than min_slot 332174123bd7SChris Mason */ 332244871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 332399d8f83cSChris Mason *root, struct btrfs_path *path, 332499d8f83cSChris Mason int min_data_size, int data_size, 332599d8f83cSChris Mason int empty, u32 min_slot) 3326be0e5c09SChris Mason { 332744871b1bSChris Mason struct extent_buffer *left = path->nodes[0]; 332844871b1bSChris Mason struct extent_buffer *right; 332944871b1bSChris Mason struct extent_buffer *upper; 333044871b1bSChris Mason int slot; 333144871b1bSChris Mason int free_space; 333244871b1bSChris Mason u32 left_nritems; 333344871b1bSChris Mason int ret; 333444871b1bSChris Mason 333544871b1bSChris Mason if (!path->nodes[1]) 333644871b1bSChris Mason return 1; 333744871b1bSChris Mason 333844871b1bSChris Mason slot = path->slots[1]; 333944871b1bSChris Mason upper = path->nodes[1]; 334044871b1bSChris Mason if (slot >= btrfs_header_nritems(upper) - 1) 334144871b1bSChris Mason return 1; 334244871b1bSChris Mason 334349d0c642SFilipe Manana btrfs_assert_tree_write_locked(path->nodes[1]); 334444871b1bSChris Mason 33454b231ae4SDavid Sterba right = btrfs_read_node_slot(upper, slot + 1); 3346fb770ae4SLiu Bo if (IS_ERR(right)) 33479cf14029SJosef Bacik return PTR_ERR(right); 334891ca338dSTsutomu Itoh 3349bf77467aSJosef Bacik __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); 335044871b1bSChris Mason 3351e902baacSDavid Sterba free_space = btrfs_leaf_free_space(right); 335244871b1bSChris Mason if (free_space < data_size) 335344871b1bSChris Mason goto out_unlock; 335444871b1bSChris Mason 335544871b1bSChris Mason ret = btrfs_cow_block(trans, root, right, upper, 3356bf59a5a2SJosef Bacik slot + 1, &right, BTRFS_NESTING_RIGHT_COW); 335744871b1bSChris Mason if (ret) 335844871b1bSChris Mason goto out_unlock; 335944871b1bSChris Mason 336044871b1bSChris Mason left_nritems = btrfs_header_nritems(left); 336144871b1bSChris Mason if (left_nritems == 0) 336244871b1bSChris Mason goto out_unlock; 336344871b1bSChris Mason 3364d16c702fSQu Wenruo if (check_sibling_keys(left, right)) { 3365d16c702fSQu Wenruo ret = -EUCLEAN; 33669ae5afd0SFilipe Manana btrfs_abort_transaction(trans, ret); 3367d16c702fSQu Wenruo btrfs_tree_unlock(right); 3368d16c702fSQu Wenruo free_extent_buffer(right); 3369d16c702fSQu Wenruo return ret; 3370d16c702fSQu Wenruo } 33712ef1fed2SFilipe David Borba Manana if (path->slots[0] == left_nritems && !empty) { 33722ef1fed2SFilipe David Borba Manana /* Key greater than all keys in the leaf, right neighbor has 33732ef1fed2SFilipe David Borba Manana * enough room for it and we're not emptying our leaf to delete 33742ef1fed2SFilipe David Borba Manana * it, therefore use right neighbor to insert the new item and 337552042d8eSAndrea Gelmini * no need to touch/dirty our left leaf. */ 33762ef1fed2SFilipe David Borba Manana btrfs_tree_unlock(left); 33772ef1fed2SFilipe David Borba Manana free_extent_buffer(left); 33782ef1fed2SFilipe David Borba Manana path->nodes[0] = right; 33792ef1fed2SFilipe David Borba Manana path->slots[0] = 0; 33802ef1fed2SFilipe David Borba Manana path->slots[1]++; 33812ef1fed2SFilipe David Borba Manana return 0; 33822ef1fed2SFilipe David Borba Manana } 33832ef1fed2SFilipe David Borba Manana 3384ed25dab3SJosef Bacik return __push_leaf_right(trans, path, min_data_size, empty, right, 3385ed25dab3SJosef Bacik free_space, left_nritems, min_slot); 338644871b1bSChris Mason out_unlock: 338744871b1bSChris Mason btrfs_tree_unlock(right); 338844871b1bSChris Mason free_extent_buffer(right); 338944871b1bSChris Mason return 1; 339044871b1bSChris Mason } 339144871b1bSChris Mason 339244871b1bSChris Mason /* 339344871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 339444871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 339599d8f83cSChris Mason * 339699d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 339799d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the 339899d8f83cSChris Mason * items 339944871b1bSChris Mason */ 3400ed25dab3SJosef Bacik static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, 3401ed25dab3SJosef Bacik struct btrfs_path *path, int data_size, 340244871b1bSChris Mason int empty, struct extent_buffer *left, 340399d8f83cSChris Mason int free_space, u32 right_nritems, 340499d8f83cSChris Mason u32 max_slot) 340544871b1bSChris Mason { 34068087c193SDavid Sterba struct btrfs_fs_info *fs_info = left->fs_info; 34075f39d397SChris Mason struct btrfs_disk_key disk_key; 34085f39d397SChris Mason struct extent_buffer *right = path->nodes[0]; 3409be0e5c09SChris Mason int i; 3410be0e5c09SChris Mason int push_space = 0; 3411be0e5c09SChris Mason int push_items = 0; 34127518a238SChris Mason u32 old_left_nritems; 341334a38218SChris Mason u32 nr; 3414aa5d6bedSChris Mason int ret = 0; 3415db94535dSChris Mason u32 this_item_size; 3416db94535dSChris Mason u32 old_left_item_size; 3417cfed81a0SChris Mason struct btrfs_map_token token; 3418cfed81a0SChris Mason 341934a38218SChris Mason if (empty) 342099d8f83cSChris Mason nr = min(right_nritems, max_slot); 342134a38218SChris Mason else 342299d8f83cSChris Mason nr = min(right_nritems - 1, max_slot); 342334a38218SChris Mason 342434a38218SChris Mason for (i = 0; i < nr; i++) { 342531840ae1SZheng Yan if (!empty && push_items > 0) { 342631840ae1SZheng Yan if (path->slots[0] < i) 342731840ae1SZheng Yan break; 342831840ae1SZheng Yan if (path->slots[0] == i) { 3429e902baacSDavid Sterba int space = btrfs_leaf_free_space(right); 3430e902baacSDavid Sterba 343131840ae1SZheng Yan if (space + push_space * 2 > free_space) 343231840ae1SZheng Yan break; 343331840ae1SZheng Yan } 343431840ae1SZheng Yan } 343531840ae1SZheng Yan 3436be0e5c09SChris Mason if (path->slots[0] == i) 343787b29b20SYan Zheng push_space += data_size; 3438db94535dSChris Mason 34393212fa14SJosef Bacik this_item_size = btrfs_item_size(right, i); 344074794207SJosef Bacik if (this_item_size + sizeof(struct btrfs_item) + push_space > 344174794207SJosef Bacik free_space) 3442be0e5c09SChris Mason break; 3443db94535dSChris Mason 3444be0e5c09SChris Mason push_items++; 344574794207SJosef Bacik push_space += this_item_size + sizeof(struct btrfs_item); 3446be0e5c09SChris Mason } 3447db94535dSChris Mason 3448be0e5c09SChris Mason if (push_items == 0) { 3449925baeddSChris Mason ret = 1; 3450925baeddSChris Mason goto out; 3451be0e5c09SChris Mason } 3452fae7f21cSDulshani Gunawardhana WARN_ON(!empty && push_items == btrfs_header_nritems(right)); 34535f39d397SChris Mason 3454be0e5c09SChris Mason /* push data from right to left */ 3455637e3b48SJosef Bacik copy_leaf_items(left, right, btrfs_header_nritems(left), 0, push_items); 34565f39d397SChris Mason 34570b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info) - 34583212fa14SJosef Bacik btrfs_item_offset(right, push_items - 1); 34595f39d397SChris Mason 3460637e3b48SJosef Bacik copy_leaf_data(left, right, leaf_data_end(left) - push_space, 3461637e3b48SJosef Bacik btrfs_item_offset(right, push_items - 1), push_space); 34625f39d397SChris Mason old_left_nritems = btrfs_header_nritems(left); 346387b29b20SYan Zheng BUG_ON(old_left_nritems <= 0); 3464eb60ceacSChris Mason 3465c82f823cSDavid Sterba btrfs_init_map_token(&token, left); 34663212fa14SJosef Bacik old_left_item_size = btrfs_item_offset(left, old_left_nritems - 1); 3467be0e5c09SChris Mason for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 34685f39d397SChris Mason u32 ioff; 3469db94535dSChris Mason 34703212fa14SJosef Bacik ioff = btrfs_token_item_offset(&token, i); 34713212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, 3472cc4c13d5SDavid Sterba ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size)); 3473be0e5c09SChris Mason } 34745f39d397SChris Mason btrfs_set_header_nritems(left, old_left_nritems + push_items); 3475be0e5c09SChris Mason 3476be0e5c09SChris Mason /* fixup right node */ 347731b1a2bdSJulia Lawall if (push_items > right_nritems) 347831b1a2bdSJulia Lawall WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, 3479d397712bSChris Mason right_nritems); 348034a38218SChris Mason 348134a38218SChris Mason if (push_items < right_nritems) { 34823212fa14SJosef Bacik push_space = btrfs_item_offset(right, push_items - 1) - 34838f881e8cSDavid Sterba leaf_data_end(right); 3484637e3b48SJosef Bacik memmove_leaf_data(right, 34850b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 34868f881e8cSDavid Sterba leaf_data_end(right), push_space); 34875f39d397SChris Mason 3488637e3b48SJosef Bacik memmove_leaf_items(right, 0, push_items, 3489637e3b48SJosef Bacik btrfs_header_nritems(right) - push_items); 349034a38218SChris Mason } 3491c82f823cSDavid Sterba 3492c82f823cSDavid Sterba btrfs_init_map_token(&token, right); 3493eef1c494SYan right_nritems -= push_items; 3494eef1c494SYan btrfs_set_header_nritems(right, right_nritems); 34950b246afaSJeff Mahoney push_space = BTRFS_LEAF_DATA_SIZE(fs_info); 34965f39d397SChris Mason for (i = 0; i < right_nritems; i++) { 34973212fa14SJosef Bacik push_space = push_space - btrfs_token_item_size(&token, i); 34983212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, push_space); 3499db94535dSChris Mason } 3500eb60ceacSChris Mason 35015f39d397SChris Mason btrfs_mark_buffer_dirty(left); 350234a38218SChris Mason if (right_nritems) 35035f39d397SChris Mason btrfs_mark_buffer_dirty(right); 3504f0486c68SYan, Zheng else 3505190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, right); 3506098f59c2SChris Mason 35075f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 3508b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 3509be0e5c09SChris Mason 3510be0e5c09SChris Mason /* then fixup the leaf pointer in the path */ 3511be0e5c09SChris Mason if (path->slots[0] < push_items) { 3512be0e5c09SChris Mason path->slots[0] += old_left_nritems; 3513925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 35145f39d397SChris Mason free_extent_buffer(path->nodes[0]); 35155f39d397SChris Mason path->nodes[0] = left; 3516be0e5c09SChris Mason path->slots[1] -= 1; 3517be0e5c09SChris Mason } else { 3518925baeddSChris Mason btrfs_tree_unlock(left); 35195f39d397SChris Mason free_extent_buffer(left); 3520be0e5c09SChris Mason path->slots[0] -= push_items; 3521be0e5c09SChris Mason } 3522eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 3523aa5d6bedSChris Mason return ret; 3524925baeddSChris Mason out: 3525925baeddSChris Mason btrfs_tree_unlock(left); 3526925baeddSChris Mason free_extent_buffer(left); 3527925baeddSChris Mason return ret; 3528be0e5c09SChris Mason } 3529be0e5c09SChris Mason 353074123bd7SChris Mason /* 353144871b1bSChris Mason * push some data in the path leaf to the left, trying to free up at 353244871b1bSChris Mason * least data_size bytes. returns zero if the push worked, nonzero otherwise 353399d8f83cSChris Mason * 353499d8f83cSChris Mason * max_slot can put a limit on how far into the leaf we'll push items. The 353599d8f83cSChris Mason * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the 353699d8f83cSChris Mason * items 353744871b1bSChris Mason */ 353844871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 353999d8f83cSChris Mason *root, struct btrfs_path *path, int min_data_size, 354099d8f83cSChris Mason int data_size, int empty, u32 max_slot) 354144871b1bSChris Mason { 354244871b1bSChris Mason struct extent_buffer *right = path->nodes[0]; 354344871b1bSChris Mason struct extent_buffer *left; 354444871b1bSChris Mason int slot; 354544871b1bSChris Mason int free_space; 354644871b1bSChris Mason u32 right_nritems; 354744871b1bSChris Mason int ret = 0; 354844871b1bSChris Mason 354944871b1bSChris Mason slot = path->slots[1]; 355044871b1bSChris Mason if (slot == 0) 355144871b1bSChris Mason return 1; 355244871b1bSChris Mason if (!path->nodes[1]) 355344871b1bSChris Mason return 1; 355444871b1bSChris Mason 355544871b1bSChris Mason right_nritems = btrfs_header_nritems(right); 355644871b1bSChris Mason if (right_nritems == 0) 355744871b1bSChris Mason return 1; 355844871b1bSChris Mason 355949d0c642SFilipe Manana btrfs_assert_tree_write_locked(path->nodes[1]); 356044871b1bSChris Mason 35614b231ae4SDavid Sterba left = btrfs_read_node_slot(path->nodes[1], slot - 1); 3562fb770ae4SLiu Bo if (IS_ERR(left)) 35639cf14029SJosef Bacik return PTR_ERR(left); 356491ca338dSTsutomu Itoh 3565bf77467aSJosef Bacik __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); 356644871b1bSChris Mason 3567e902baacSDavid Sterba free_space = btrfs_leaf_free_space(left); 356844871b1bSChris Mason if (free_space < data_size) { 356944871b1bSChris Mason ret = 1; 357044871b1bSChris Mason goto out; 357144871b1bSChris Mason } 357244871b1bSChris Mason 357344871b1bSChris Mason ret = btrfs_cow_block(trans, root, left, 35749631e4ccSJosef Bacik path->nodes[1], slot - 1, &left, 3575bf59a5a2SJosef Bacik BTRFS_NESTING_LEFT_COW); 357644871b1bSChris Mason if (ret) { 357744871b1bSChris Mason /* we hit -ENOSPC, but it isn't fatal here */ 357879787eaaSJeff Mahoney if (ret == -ENOSPC) 357944871b1bSChris Mason ret = 1; 358044871b1bSChris Mason goto out; 358144871b1bSChris Mason } 358244871b1bSChris Mason 3583d16c702fSQu Wenruo if (check_sibling_keys(left, right)) { 3584d16c702fSQu Wenruo ret = -EUCLEAN; 35859ae5afd0SFilipe Manana btrfs_abort_transaction(trans, ret); 3586d16c702fSQu Wenruo goto out; 3587d16c702fSQu Wenruo } 3588ed25dab3SJosef Bacik return __push_leaf_left(trans, path, min_data_size, empty, left, 3589ed25dab3SJosef Bacik free_space, right_nritems, max_slot); 359044871b1bSChris Mason out: 359144871b1bSChris Mason btrfs_tree_unlock(left); 359244871b1bSChris Mason free_extent_buffer(left); 359344871b1bSChris Mason return ret; 359444871b1bSChris Mason } 359544871b1bSChris Mason 359644871b1bSChris Mason /* 359774123bd7SChris Mason * split the path's leaf in two, making sure there is at least data_size 359874123bd7SChris Mason * available for the resulting leaf level of the path. 359974123bd7SChris Mason */ 3600*50b5d1fcSFilipe Manana static noinline int copy_for_split(struct btrfs_trans_handle *trans, 360144871b1bSChris Mason struct btrfs_path *path, 360244871b1bSChris Mason struct extent_buffer *l, 360344871b1bSChris Mason struct extent_buffer *right, 360444871b1bSChris Mason int slot, int mid, int nritems) 3605be0e5c09SChris Mason { 360694f94ad9SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 3607be0e5c09SChris Mason int data_copy_size; 3608be0e5c09SChris Mason int rt_data_off; 3609be0e5c09SChris Mason int i; 3610*50b5d1fcSFilipe Manana int ret; 3611d4dbff95SChris Mason struct btrfs_disk_key disk_key; 3612cfed81a0SChris Mason struct btrfs_map_token token; 3613cfed81a0SChris Mason 36145f39d397SChris Mason nritems = nritems - mid; 36155f39d397SChris Mason btrfs_set_header_nritems(right, nritems); 3616dc2e724eSJosef Bacik data_copy_size = btrfs_item_data_end(l, mid) - leaf_data_end(l); 36175f39d397SChris Mason 3618637e3b48SJosef Bacik copy_leaf_items(right, l, 0, mid, nritems); 36195f39d397SChris Mason 3620637e3b48SJosef Bacik copy_leaf_data(right, l, BTRFS_LEAF_DATA_SIZE(fs_info) - data_copy_size, 36218f881e8cSDavid Sterba leaf_data_end(l), data_copy_size); 362274123bd7SChris Mason 3623dc2e724eSJosef Bacik rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_data_end(l, mid); 36245f39d397SChris Mason 3625c82f823cSDavid Sterba btrfs_init_map_token(&token, right); 36265f39d397SChris Mason for (i = 0; i < nritems; i++) { 3627db94535dSChris Mason u32 ioff; 3628db94535dSChris Mason 36293212fa14SJosef Bacik ioff = btrfs_token_item_offset(&token, i); 36303212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, ioff + rt_data_off); 36310783fcfcSChris Mason } 363274123bd7SChris Mason 36335f39d397SChris Mason btrfs_set_header_nritems(l, mid); 36345f39d397SChris Mason btrfs_item_key(right, &disk_key, 0); 3635*50b5d1fcSFilipe Manana ret = insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1); 3636*50b5d1fcSFilipe Manana if (ret < 0) 3637*50b5d1fcSFilipe Manana return ret; 36385f39d397SChris Mason 36395f39d397SChris Mason btrfs_mark_buffer_dirty(right); 36405f39d397SChris Mason btrfs_mark_buffer_dirty(l); 3641eb60ceacSChris Mason BUG_ON(path->slots[0] != slot); 36425f39d397SChris Mason 3643be0e5c09SChris Mason if (mid <= slot) { 3644925baeddSChris Mason btrfs_tree_unlock(path->nodes[0]); 36455f39d397SChris Mason free_extent_buffer(path->nodes[0]); 36465f39d397SChris Mason path->nodes[0] = right; 3647be0e5c09SChris Mason path->slots[0] -= mid; 3648be0e5c09SChris Mason path->slots[1] += 1; 3649925baeddSChris Mason } else { 3650925baeddSChris Mason btrfs_tree_unlock(right); 36515f39d397SChris Mason free_extent_buffer(right); 3652925baeddSChris Mason } 36535f39d397SChris Mason 3654eb60ceacSChris Mason BUG_ON(path->slots[0] < 0); 3655*50b5d1fcSFilipe Manana 3656*50b5d1fcSFilipe Manana return 0; 365744871b1bSChris Mason } 365844871b1bSChris Mason 365944871b1bSChris Mason /* 366099d8f83cSChris Mason * double splits happen when we need to insert a big item in the middle 366199d8f83cSChris Mason * of a leaf. A double split can leave us with 3 mostly empty leaves: 366299d8f83cSChris Mason * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] 366399d8f83cSChris Mason * A B C 366499d8f83cSChris Mason * 366599d8f83cSChris Mason * We avoid this by trying to push the items on either side of our target 366699d8f83cSChris Mason * into the adjacent leaves. If all goes well we can avoid the double split 366799d8f83cSChris Mason * completely. 366899d8f83cSChris Mason */ 366999d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans, 367099d8f83cSChris Mason struct btrfs_root *root, 367199d8f83cSChris Mason struct btrfs_path *path, 367299d8f83cSChris Mason int data_size) 367399d8f83cSChris Mason { 367499d8f83cSChris Mason int ret; 367599d8f83cSChris Mason int progress = 0; 367699d8f83cSChris Mason int slot; 367799d8f83cSChris Mason u32 nritems; 36785a4267caSFilipe David Borba Manana int space_needed = data_size; 367999d8f83cSChris Mason 368099d8f83cSChris Mason slot = path->slots[0]; 36815a4267caSFilipe David Borba Manana if (slot < btrfs_header_nritems(path->nodes[0])) 3682e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(path->nodes[0]); 368399d8f83cSChris Mason 368499d8f83cSChris Mason /* 368599d8f83cSChris Mason * try to push all the items after our slot into the 368699d8f83cSChris Mason * right leaf 368799d8f83cSChris Mason */ 36885a4267caSFilipe David Borba Manana ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot); 368999d8f83cSChris Mason if (ret < 0) 369099d8f83cSChris Mason return ret; 369199d8f83cSChris Mason 369299d8f83cSChris Mason if (ret == 0) 369399d8f83cSChris Mason progress++; 369499d8f83cSChris Mason 369599d8f83cSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 369699d8f83cSChris Mason /* 369799d8f83cSChris Mason * our goal is to get our slot at the start or end of a leaf. If 369899d8f83cSChris Mason * we've done so we're done 369999d8f83cSChris Mason */ 370099d8f83cSChris Mason if (path->slots[0] == 0 || path->slots[0] == nritems) 370199d8f83cSChris Mason return 0; 370299d8f83cSChris Mason 3703e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) 370499d8f83cSChris Mason return 0; 370599d8f83cSChris Mason 370699d8f83cSChris Mason /* try to push all the items before our slot into the next leaf */ 370799d8f83cSChris Mason slot = path->slots[0]; 3708263d3995SFilipe Manana space_needed = data_size; 3709263d3995SFilipe Manana if (slot > 0) 3710e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(path->nodes[0]); 37115a4267caSFilipe David Borba Manana ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot); 371299d8f83cSChris Mason if (ret < 0) 371399d8f83cSChris Mason return ret; 371499d8f83cSChris Mason 371599d8f83cSChris Mason if (ret == 0) 371699d8f83cSChris Mason progress++; 371799d8f83cSChris Mason 371899d8f83cSChris Mason if (progress) 371999d8f83cSChris Mason return 0; 372099d8f83cSChris Mason return 1; 372199d8f83cSChris Mason } 372299d8f83cSChris Mason 372399d8f83cSChris Mason /* 372444871b1bSChris Mason * split the path's leaf in two, making sure there is at least data_size 372544871b1bSChris Mason * available for the resulting leaf level of the path. 372644871b1bSChris Mason * 372744871b1bSChris Mason * returns 0 if all went well and < 0 on failure. 372844871b1bSChris Mason */ 372944871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans, 373044871b1bSChris Mason struct btrfs_root *root, 3731310712b2SOmar Sandoval const struct btrfs_key *ins_key, 373244871b1bSChris Mason struct btrfs_path *path, int data_size, 373344871b1bSChris Mason int extend) 373444871b1bSChris Mason { 37355d4f98a2SYan Zheng struct btrfs_disk_key disk_key; 373644871b1bSChris Mason struct extent_buffer *l; 373744871b1bSChris Mason u32 nritems; 373844871b1bSChris Mason int mid; 373944871b1bSChris Mason int slot; 374044871b1bSChris Mason struct extent_buffer *right; 3741b7a0365eSDaniel Dressler struct btrfs_fs_info *fs_info = root->fs_info; 374244871b1bSChris Mason int ret = 0; 374344871b1bSChris Mason int wret; 37445d4f98a2SYan Zheng int split; 374544871b1bSChris Mason int num_doubles = 0; 374699d8f83cSChris Mason int tried_avoid_double = 0; 374744871b1bSChris Mason 3748a5719521SYan, Zheng l = path->nodes[0]; 3749a5719521SYan, Zheng slot = path->slots[0]; 37503212fa14SJosef Bacik if (extend && data_size + btrfs_item_size(l, slot) + 37510b246afaSJeff Mahoney sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info)) 3752a5719521SYan, Zheng return -EOVERFLOW; 3753a5719521SYan, Zheng 375444871b1bSChris Mason /* first try to make some room by pushing left and right */ 375533157e05SLiu Bo if (data_size && path->nodes[1]) { 37565a4267caSFilipe David Borba Manana int space_needed = data_size; 37575a4267caSFilipe David Borba Manana 37585a4267caSFilipe David Borba Manana if (slot < btrfs_header_nritems(l)) 3759e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(l); 37605a4267caSFilipe David Borba Manana 37615a4267caSFilipe David Borba Manana wret = push_leaf_right(trans, root, path, space_needed, 37625a4267caSFilipe David Borba Manana space_needed, 0, 0); 376344871b1bSChris Mason if (wret < 0) 376444871b1bSChris Mason return wret; 376544871b1bSChris Mason if (wret) { 3766263d3995SFilipe Manana space_needed = data_size; 3767263d3995SFilipe Manana if (slot > 0) 3768e902baacSDavid Sterba space_needed -= btrfs_leaf_free_space(l); 37695a4267caSFilipe David Borba Manana wret = push_leaf_left(trans, root, path, space_needed, 37705a4267caSFilipe David Borba Manana space_needed, 0, (u32)-1); 377144871b1bSChris Mason if (wret < 0) 377244871b1bSChris Mason return wret; 377344871b1bSChris Mason } 377444871b1bSChris Mason l = path->nodes[0]; 377544871b1bSChris Mason 377644871b1bSChris Mason /* did the pushes work? */ 3777e902baacSDavid Sterba if (btrfs_leaf_free_space(l) >= data_size) 377844871b1bSChris Mason return 0; 377944871b1bSChris Mason } 378044871b1bSChris Mason 378144871b1bSChris Mason if (!path->nodes[1]) { 3782fdd99c72SLiu Bo ret = insert_new_root(trans, root, path, 1); 378344871b1bSChris Mason if (ret) 378444871b1bSChris Mason return ret; 378544871b1bSChris Mason } 378644871b1bSChris Mason again: 37875d4f98a2SYan Zheng split = 1; 378844871b1bSChris Mason l = path->nodes[0]; 378944871b1bSChris Mason slot = path->slots[0]; 379044871b1bSChris Mason nritems = btrfs_header_nritems(l); 379144871b1bSChris Mason mid = (nritems + 1) / 2; 379244871b1bSChris Mason 37935d4f98a2SYan Zheng if (mid <= slot) { 37945d4f98a2SYan Zheng if (nritems == 1 || 37955d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + data_size > 37960b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 37975d4f98a2SYan Zheng if (slot >= nritems) { 37985d4f98a2SYan Zheng split = 0; 37995d4f98a2SYan Zheng } else { 38005d4f98a2SYan Zheng mid = slot; 38015d4f98a2SYan Zheng if (mid != nritems && 38025d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 38030b246afaSJeff Mahoney data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { 380499d8f83cSChris Mason if (data_size && !tried_avoid_double) 380599d8f83cSChris Mason goto push_for_double; 38065d4f98a2SYan Zheng split = 2; 38075d4f98a2SYan Zheng } 38085d4f98a2SYan Zheng } 38095d4f98a2SYan Zheng } 38105d4f98a2SYan Zheng } else { 38115d4f98a2SYan Zheng if (leaf_space_used(l, 0, mid) + data_size > 38120b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 38135d4f98a2SYan Zheng if (!extend && data_size && slot == 0) { 38145d4f98a2SYan Zheng split = 0; 38155d4f98a2SYan Zheng } else if ((extend || !data_size) && slot == 0) { 38165d4f98a2SYan Zheng mid = 1; 38175d4f98a2SYan Zheng } else { 38185d4f98a2SYan Zheng mid = slot; 38195d4f98a2SYan Zheng if (mid != nritems && 38205d4f98a2SYan Zheng leaf_space_used(l, mid, nritems - mid) + 38210b246afaSJeff Mahoney data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { 382299d8f83cSChris Mason if (data_size && !tried_avoid_double) 382399d8f83cSChris Mason goto push_for_double; 38245d4f98a2SYan Zheng split = 2; 38255d4f98a2SYan Zheng } 38265d4f98a2SYan Zheng } 38275d4f98a2SYan Zheng } 38285d4f98a2SYan Zheng } 38295d4f98a2SYan Zheng 38305d4f98a2SYan Zheng if (split == 0) 38315d4f98a2SYan Zheng btrfs_cpu_key_to_disk(&disk_key, ins_key); 38325d4f98a2SYan Zheng else 38335d4f98a2SYan Zheng btrfs_item_key(l, &disk_key, mid); 38345d4f98a2SYan Zheng 3835ca9d473aSJosef Bacik /* 3836ca9d473aSJosef Bacik * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double 3837ca9d473aSJosef Bacik * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES 3838ca9d473aSJosef Bacik * subclasses, which is 8 at the time of this patch, and we've maxed it 3839ca9d473aSJosef Bacik * out. In the future we could add a 3840ca9d473aSJosef Bacik * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just 3841ca9d473aSJosef Bacik * use BTRFS_NESTING_NEW_ROOT. 3842ca9d473aSJosef Bacik */ 384379bd3712SFilipe Manana right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid, 384479bd3712SFilipe Manana &disk_key, 0, l->start, 0, 384579bd3712SFilipe Manana num_doubles ? BTRFS_NESTING_NEW_ROOT : 3846ca9d473aSJosef Bacik BTRFS_NESTING_SPLIT); 3847f0486c68SYan, Zheng if (IS_ERR(right)) 384844871b1bSChris Mason return PTR_ERR(right); 3849f0486c68SYan, Zheng 38500b246afaSJeff Mahoney root_add_used(root, fs_info->nodesize); 385144871b1bSChris Mason 38525d4f98a2SYan Zheng if (split == 0) { 385344871b1bSChris Mason if (mid <= slot) { 385444871b1bSChris Mason btrfs_set_header_nritems(right, 0); 3855*50b5d1fcSFilipe Manana ret = insert_ptr(trans, path, &disk_key, 38562ff7e61eSJeff Mahoney right->start, path->slots[1] + 1, 1); 3857*50b5d1fcSFilipe Manana if (ret < 0) { 3858*50b5d1fcSFilipe Manana btrfs_tree_unlock(right); 3859*50b5d1fcSFilipe Manana free_extent_buffer(right); 3860*50b5d1fcSFilipe Manana return ret; 3861*50b5d1fcSFilipe Manana } 386244871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 386344871b1bSChris Mason free_extent_buffer(path->nodes[0]); 386444871b1bSChris Mason path->nodes[0] = right; 386544871b1bSChris Mason path->slots[0] = 0; 386644871b1bSChris Mason path->slots[1] += 1; 386744871b1bSChris Mason } else { 386844871b1bSChris Mason btrfs_set_header_nritems(right, 0); 3869*50b5d1fcSFilipe Manana ret = insert_ptr(trans, path, &disk_key, 38702ff7e61eSJeff Mahoney right->start, path->slots[1], 1); 3871*50b5d1fcSFilipe Manana if (ret < 0) { 3872*50b5d1fcSFilipe Manana btrfs_tree_unlock(right); 3873*50b5d1fcSFilipe Manana free_extent_buffer(right); 3874*50b5d1fcSFilipe Manana return ret; 3875*50b5d1fcSFilipe Manana } 387644871b1bSChris Mason btrfs_tree_unlock(path->nodes[0]); 387744871b1bSChris Mason free_extent_buffer(path->nodes[0]); 387844871b1bSChris Mason path->nodes[0] = right; 387944871b1bSChris Mason path->slots[0] = 0; 3880143bede5SJeff Mahoney if (path->slots[1] == 0) 3881b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 38825d4f98a2SYan Zheng } 3883196e0249SLiu Bo /* 3884196e0249SLiu Bo * We create a new leaf 'right' for the required ins_len and 3885196e0249SLiu Bo * we'll do btrfs_mark_buffer_dirty() on this leaf after copying 3886196e0249SLiu Bo * the content of ins_len to 'right'. 3887196e0249SLiu Bo */ 388844871b1bSChris Mason return ret; 388944871b1bSChris Mason } 389044871b1bSChris Mason 3891*50b5d1fcSFilipe Manana ret = copy_for_split(trans, path, l, right, slot, mid, nritems); 3892*50b5d1fcSFilipe Manana if (ret < 0) { 3893*50b5d1fcSFilipe Manana btrfs_tree_unlock(right); 3894*50b5d1fcSFilipe Manana free_extent_buffer(right); 3895*50b5d1fcSFilipe Manana return ret; 3896*50b5d1fcSFilipe Manana } 389744871b1bSChris Mason 38985d4f98a2SYan Zheng if (split == 2) { 3899cc0c5538SChris Mason BUG_ON(num_doubles != 0); 3900cc0c5538SChris Mason num_doubles++; 3901cc0c5538SChris Mason goto again; 39023326d1b0SChris Mason } 390344871b1bSChris Mason 3904143bede5SJeff Mahoney return 0; 390599d8f83cSChris Mason 390699d8f83cSChris Mason push_for_double: 390799d8f83cSChris Mason push_for_double_split(trans, root, path, data_size); 390899d8f83cSChris Mason tried_avoid_double = 1; 3909e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) 391099d8f83cSChris Mason return 0; 391199d8f83cSChris Mason goto again; 3912be0e5c09SChris Mason } 3913be0e5c09SChris Mason 3914ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, 3915ad48fd75SYan, Zheng struct btrfs_root *root, 3916ad48fd75SYan, Zheng struct btrfs_path *path, int ins_len) 3917ad48fd75SYan, Zheng { 3918ad48fd75SYan, Zheng struct btrfs_key key; 3919ad48fd75SYan, Zheng struct extent_buffer *leaf; 3920ad48fd75SYan, Zheng struct btrfs_file_extent_item *fi; 3921ad48fd75SYan, Zheng u64 extent_len = 0; 3922ad48fd75SYan, Zheng u32 item_size; 3923ad48fd75SYan, Zheng int ret; 3924ad48fd75SYan, Zheng 3925ad48fd75SYan, Zheng leaf = path->nodes[0]; 3926ad48fd75SYan, Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3927ad48fd75SYan, Zheng 3928ad48fd75SYan, Zheng BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 3929ad48fd75SYan, Zheng key.type != BTRFS_EXTENT_CSUM_KEY); 3930ad48fd75SYan, Zheng 3931e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) >= ins_len) 3932ad48fd75SYan, Zheng return 0; 3933ad48fd75SYan, Zheng 39343212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]); 3935ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 3936ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 3937ad48fd75SYan, Zheng struct btrfs_file_extent_item); 3938ad48fd75SYan, Zheng extent_len = btrfs_file_extent_num_bytes(leaf, fi); 3939ad48fd75SYan, Zheng } 3940b3b4aa74SDavid Sterba btrfs_release_path(path); 3941ad48fd75SYan, Zheng 3942ad48fd75SYan, Zheng path->keep_locks = 1; 3943ad48fd75SYan, Zheng path->search_for_split = 1; 3944ad48fd75SYan, Zheng ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 3945ad48fd75SYan, Zheng path->search_for_split = 0; 3946a8df6fe6SFilipe Manana if (ret > 0) 3947a8df6fe6SFilipe Manana ret = -EAGAIN; 3948ad48fd75SYan, Zheng if (ret < 0) 3949ad48fd75SYan, Zheng goto err; 3950ad48fd75SYan, Zheng 3951ad48fd75SYan, Zheng ret = -EAGAIN; 3952ad48fd75SYan, Zheng leaf = path->nodes[0]; 3953a8df6fe6SFilipe Manana /* if our item isn't there, return now */ 39543212fa14SJosef Bacik if (item_size != btrfs_item_size(leaf, path->slots[0])) 3955ad48fd75SYan, Zheng goto err; 3956ad48fd75SYan, Zheng 3957109f6aefSChris Mason /* the leaf has changed, it now has room. return now */ 3958e902baacSDavid Sterba if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len) 3959109f6aefSChris Mason goto err; 3960109f6aefSChris Mason 3961ad48fd75SYan, Zheng if (key.type == BTRFS_EXTENT_DATA_KEY) { 3962ad48fd75SYan, Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 3963ad48fd75SYan, Zheng struct btrfs_file_extent_item); 3964ad48fd75SYan, Zheng if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) 3965ad48fd75SYan, Zheng goto err; 3966ad48fd75SYan, Zheng } 3967ad48fd75SYan, Zheng 3968ad48fd75SYan, Zheng ret = split_leaf(trans, root, &key, path, ins_len, 1); 3969f0486c68SYan, Zheng if (ret) 3970f0486c68SYan, Zheng goto err; 3971ad48fd75SYan, Zheng 3972ad48fd75SYan, Zheng path->keep_locks = 0; 3973ad48fd75SYan, Zheng btrfs_unlock_up_safe(path, 1); 3974ad48fd75SYan, Zheng return 0; 3975ad48fd75SYan, Zheng err: 3976ad48fd75SYan, Zheng path->keep_locks = 0; 3977ad48fd75SYan, Zheng return ret; 3978ad48fd75SYan, Zheng } 3979ad48fd75SYan, Zheng 398025263cd7SDavid Sterba static noinline int split_item(struct btrfs_path *path, 3981310712b2SOmar Sandoval const struct btrfs_key *new_key, 3982459931ecSChris Mason unsigned long split_offset) 3983459931ecSChris Mason { 3984459931ecSChris Mason struct extent_buffer *leaf; 3985c91666b1SJosef Bacik int orig_slot, slot; 3986ad48fd75SYan, Zheng char *buf; 3987459931ecSChris Mason u32 nritems; 3988ad48fd75SYan, Zheng u32 item_size; 3989459931ecSChris Mason u32 orig_offset; 3990459931ecSChris Mason struct btrfs_disk_key disk_key; 3991459931ecSChris Mason 3992459931ecSChris Mason leaf = path->nodes[0]; 3993e902baacSDavid Sterba BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)); 3994b9473439SChris Mason 3995c91666b1SJosef Bacik orig_slot = path->slots[0]; 39963212fa14SJosef Bacik orig_offset = btrfs_item_offset(leaf, path->slots[0]); 39973212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]); 3998459931ecSChris Mason 3999459931ecSChris Mason buf = kmalloc(item_size, GFP_NOFS); 4000ad48fd75SYan, Zheng if (!buf) 4001ad48fd75SYan, Zheng return -ENOMEM; 4002ad48fd75SYan, Zheng 4003459931ecSChris Mason read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, 4004459931ecSChris Mason path->slots[0]), item_size); 4005ad48fd75SYan, Zheng 4006459931ecSChris Mason slot = path->slots[0] + 1; 4007459931ecSChris Mason nritems = btrfs_header_nritems(leaf); 4008459931ecSChris Mason if (slot != nritems) { 4009459931ecSChris Mason /* shift the items */ 4010637e3b48SJosef Bacik memmove_leaf_items(leaf, slot + 1, slot, nritems - slot); 4011459931ecSChris Mason } 4012459931ecSChris Mason 4013459931ecSChris Mason btrfs_cpu_key_to_disk(&disk_key, new_key); 4014459931ecSChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4015459931ecSChris Mason 40163212fa14SJosef Bacik btrfs_set_item_offset(leaf, slot, orig_offset); 40173212fa14SJosef Bacik btrfs_set_item_size(leaf, slot, item_size - split_offset); 4018459931ecSChris Mason 40193212fa14SJosef Bacik btrfs_set_item_offset(leaf, orig_slot, 4020459931ecSChris Mason orig_offset + item_size - split_offset); 40213212fa14SJosef Bacik btrfs_set_item_size(leaf, orig_slot, split_offset); 4022459931ecSChris Mason 4023459931ecSChris Mason btrfs_set_header_nritems(leaf, nritems + 1); 4024459931ecSChris Mason 4025459931ecSChris Mason /* write the data for the start of the original item */ 4026459931ecSChris Mason write_extent_buffer(leaf, buf, 4027459931ecSChris Mason btrfs_item_ptr_offset(leaf, path->slots[0]), 4028459931ecSChris Mason split_offset); 4029459931ecSChris Mason 4030459931ecSChris Mason /* write the data for the new item */ 4031459931ecSChris Mason write_extent_buffer(leaf, buf + split_offset, 4032459931ecSChris Mason btrfs_item_ptr_offset(leaf, slot), 4033459931ecSChris Mason item_size - split_offset); 4034459931ecSChris Mason btrfs_mark_buffer_dirty(leaf); 4035459931ecSChris Mason 4036e902baacSDavid Sterba BUG_ON(btrfs_leaf_free_space(leaf) < 0); 4037459931ecSChris Mason kfree(buf); 4038ad48fd75SYan, Zheng return 0; 4039ad48fd75SYan, Zheng } 4040ad48fd75SYan, Zheng 4041ad48fd75SYan, Zheng /* 4042ad48fd75SYan, Zheng * This function splits a single item into two items, 4043ad48fd75SYan, Zheng * giving 'new_key' to the new item and splitting the 4044ad48fd75SYan, Zheng * old one at split_offset (from the start of the item). 4045ad48fd75SYan, Zheng * 4046ad48fd75SYan, Zheng * The path may be released by this operation. After 4047ad48fd75SYan, Zheng * the split, the path is pointing to the old item. The 4048ad48fd75SYan, Zheng * new item is going to be in the same node as the old one. 4049ad48fd75SYan, Zheng * 4050ad48fd75SYan, Zheng * Note, the item being split must be smaller enough to live alone on 4051ad48fd75SYan, Zheng * a tree block with room for one extra struct btrfs_item 4052ad48fd75SYan, Zheng * 4053ad48fd75SYan, Zheng * This allows us to split the item in place, keeping a lock on the 4054ad48fd75SYan, Zheng * leaf the entire time. 4055ad48fd75SYan, Zheng */ 4056ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans, 4057ad48fd75SYan, Zheng struct btrfs_root *root, 4058ad48fd75SYan, Zheng struct btrfs_path *path, 4059310712b2SOmar Sandoval const struct btrfs_key *new_key, 4060ad48fd75SYan, Zheng unsigned long split_offset) 4061ad48fd75SYan, Zheng { 4062ad48fd75SYan, Zheng int ret; 4063ad48fd75SYan, Zheng ret = setup_leaf_for_split(trans, root, path, 4064ad48fd75SYan, Zheng sizeof(struct btrfs_item)); 4065ad48fd75SYan, Zheng if (ret) 4066459931ecSChris Mason return ret; 4067ad48fd75SYan, Zheng 406825263cd7SDavid Sterba ret = split_item(path, new_key, split_offset); 4069ad48fd75SYan, Zheng return ret; 4070ad48fd75SYan, Zheng } 4071ad48fd75SYan, Zheng 4072ad48fd75SYan, Zheng /* 4073d352ac68SChris Mason * make the item pointed to by the path smaller. new_size indicates 4074d352ac68SChris Mason * how small to make it, and from_end tells us if we just chop bytes 4075d352ac68SChris Mason * off the end of the item or if we shift the item to chop bytes off 4076d352ac68SChris Mason * the front. 4077d352ac68SChris Mason */ 407878ac4f9eSDavid Sterba void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) 4079b18c6685SChris Mason { 4080b18c6685SChris Mason int slot; 40815f39d397SChris Mason struct extent_buffer *leaf; 4082b18c6685SChris Mason u32 nritems; 4083b18c6685SChris Mason unsigned int data_end; 4084b18c6685SChris Mason unsigned int old_data_start; 4085b18c6685SChris Mason unsigned int old_size; 4086b18c6685SChris Mason unsigned int size_diff; 4087b18c6685SChris Mason int i; 4088cfed81a0SChris Mason struct btrfs_map_token token; 4089cfed81a0SChris Mason 40905f39d397SChris Mason leaf = path->nodes[0]; 4091179e29e4SChris Mason slot = path->slots[0]; 4092179e29e4SChris Mason 40933212fa14SJosef Bacik old_size = btrfs_item_size(leaf, slot); 4094179e29e4SChris Mason if (old_size == new_size) 4095143bede5SJeff Mahoney return; 4096b18c6685SChris Mason 40975f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 40988f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 4099b18c6685SChris Mason 41003212fa14SJosef Bacik old_data_start = btrfs_item_offset(leaf, slot); 4101179e29e4SChris Mason 4102b18c6685SChris Mason size_diff = old_size - new_size; 4103b18c6685SChris Mason 4104b18c6685SChris Mason BUG_ON(slot < 0); 4105b18c6685SChris Mason BUG_ON(slot >= nritems); 4106b18c6685SChris Mason 4107b18c6685SChris Mason /* 4108b18c6685SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4109b18c6685SChris Mason */ 4110b18c6685SChris Mason /* first correct the data pointers */ 4111c82f823cSDavid Sterba btrfs_init_map_token(&token, leaf); 4112b18c6685SChris Mason for (i = slot; i < nritems; i++) { 41135f39d397SChris Mason u32 ioff; 4114db94535dSChris Mason 41153212fa14SJosef Bacik ioff = btrfs_token_item_offset(&token, i); 41163212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, ioff + size_diff); 4117b18c6685SChris Mason } 4118db94535dSChris Mason 4119b18c6685SChris Mason /* shift the data */ 4120179e29e4SChris Mason if (from_end) { 4121637e3b48SJosef Bacik memmove_leaf_data(leaf, data_end + size_diff, data_end, 4122637e3b48SJosef Bacik old_data_start + new_size - data_end); 4123179e29e4SChris Mason } else { 4124179e29e4SChris Mason struct btrfs_disk_key disk_key; 4125179e29e4SChris Mason u64 offset; 4126179e29e4SChris Mason 4127179e29e4SChris Mason btrfs_item_key(leaf, &disk_key, slot); 4128179e29e4SChris Mason 4129179e29e4SChris Mason if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 4130179e29e4SChris Mason unsigned long ptr; 4131179e29e4SChris Mason struct btrfs_file_extent_item *fi; 4132179e29e4SChris Mason 4133179e29e4SChris Mason fi = btrfs_item_ptr(leaf, slot, 4134179e29e4SChris Mason struct btrfs_file_extent_item); 4135179e29e4SChris Mason fi = (struct btrfs_file_extent_item *)( 4136179e29e4SChris Mason (unsigned long)fi - size_diff); 4137179e29e4SChris Mason 4138179e29e4SChris Mason if (btrfs_file_extent_type(leaf, fi) == 4139179e29e4SChris Mason BTRFS_FILE_EXTENT_INLINE) { 4140179e29e4SChris Mason ptr = btrfs_item_ptr_offset(leaf, slot); 4141179e29e4SChris Mason memmove_extent_buffer(leaf, ptr, 4142179e29e4SChris Mason (unsigned long)fi, 41437ec20afbSDavid Sterba BTRFS_FILE_EXTENT_INLINE_DATA_START); 4144179e29e4SChris Mason } 4145179e29e4SChris Mason } 4146179e29e4SChris Mason 4147637e3b48SJosef Bacik memmove_leaf_data(leaf, data_end + size_diff, data_end, 4148637e3b48SJosef Bacik old_data_start - data_end); 4149179e29e4SChris Mason 4150179e29e4SChris Mason offset = btrfs_disk_key_offset(&disk_key); 4151179e29e4SChris Mason btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 4152179e29e4SChris Mason btrfs_set_item_key(leaf, &disk_key, slot); 4153179e29e4SChris Mason if (slot == 0) 4154b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 4155179e29e4SChris Mason } 41565f39d397SChris Mason 41573212fa14SJosef Bacik btrfs_set_item_size(leaf, slot, new_size); 41585f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 4159b18c6685SChris Mason 4160e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4161a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4162b18c6685SChris Mason BUG(); 41635f39d397SChris Mason } 4164b18c6685SChris Mason } 4165b18c6685SChris Mason 4166d352ac68SChris Mason /* 41678f69dbd2SStefan Behrens * make the item pointed to by the path bigger, data_size is the added size. 4168d352ac68SChris Mason */ 4169c71dd880SDavid Sterba void btrfs_extend_item(struct btrfs_path *path, u32 data_size) 41706567e837SChris Mason { 41716567e837SChris Mason int slot; 41725f39d397SChris Mason struct extent_buffer *leaf; 41736567e837SChris Mason u32 nritems; 41746567e837SChris Mason unsigned int data_end; 41756567e837SChris Mason unsigned int old_data; 41766567e837SChris Mason unsigned int old_size; 41776567e837SChris Mason int i; 4178cfed81a0SChris Mason struct btrfs_map_token token; 4179cfed81a0SChris Mason 41805f39d397SChris Mason leaf = path->nodes[0]; 41816567e837SChris Mason 41825f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 41838f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 41846567e837SChris Mason 4185e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < data_size) { 4186a4f78750SDavid Sterba btrfs_print_leaf(leaf); 41876567e837SChris Mason BUG(); 41885f39d397SChris Mason } 41896567e837SChris Mason slot = path->slots[0]; 4190dc2e724eSJosef Bacik old_data = btrfs_item_data_end(leaf, slot); 41916567e837SChris Mason 41926567e837SChris Mason BUG_ON(slot < 0); 41933326d1b0SChris Mason if (slot >= nritems) { 4194a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4195c71dd880SDavid Sterba btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d", 4196d397712bSChris Mason slot, nritems); 4197290342f6SArnd Bergmann BUG(); 41983326d1b0SChris Mason } 41996567e837SChris Mason 42006567e837SChris Mason /* 42016567e837SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 42026567e837SChris Mason */ 42036567e837SChris Mason /* first correct the data pointers */ 4204c82f823cSDavid Sterba btrfs_init_map_token(&token, leaf); 42056567e837SChris Mason for (i = slot; i < nritems; i++) { 42065f39d397SChris Mason u32 ioff; 4207db94535dSChris Mason 42083212fa14SJosef Bacik ioff = btrfs_token_item_offset(&token, i); 42093212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, ioff - data_size); 42106567e837SChris Mason } 42115f39d397SChris Mason 42126567e837SChris Mason /* shift the data */ 4213637e3b48SJosef Bacik memmove_leaf_data(leaf, data_end - data_size, data_end, 4214637e3b48SJosef Bacik old_data - data_end); 42155f39d397SChris Mason 42166567e837SChris Mason data_end = old_data; 42173212fa14SJosef Bacik old_size = btrfs_item_size(leaf, slot); 42183212fa14SJosef Bacik btrfs_set_item_size(leaf, slot, old_size + data_size); 42195f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 42206567e837SChris Mason 4221e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4222a4f78750SDavid Sterba btrfs_print_leaf(leaf); 42236567e837SChris Mason BUG(); 42245f39d397SChris Mason } 42256567e837SChris Mason } 42266567e837SChris Mason 422743dd529aSDavid Sterba /* 422843dd529aSDavid Sterba * Make space in the node before inserting one or more items. 4229da9ffb24SNikolay Borisov * 4230da9ffb24SNikolay Borisov * @root: root we are inserting items to 4231da9ffb24SNikolay Borisov * @path: points to the leaf/slot where we are going to insert new items 4232b7ef5f3aSFilipe Manana * @batch: information about the batch of items to insert 423343dd529aSDavid Sterba * 423443dd529aSDavid Sterba * Main purpose is to save stack depth by doing the bulk of the work in a 423543dd529aSDavid Sterba * function that doesn't call btrfs_search_slot 423674123bd7SChris Mason */ 4237f0641656SFilipe Manana static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, 4238b7ef5f3aSFilipe Manana const struct btrfs_item_batch *batch) 4239be0e5c09SChris Mason { 42400b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 42419c58309dSChris Mason int i; 42427518a238SChris Mason u32 nritems; 4243be0e5c09SChris Mason unsigned int data_end; 4244e2fa7227SChris Mason struct btrfs_disk_key disk_key; 424544871b1bSChris Mason struct extent_buffer *leaf; 424644871b1bSChris Mason int slot; 4247cfed81a0SChris Mason struct btrfs_map_token token; 4248fc0d82e1SNikolay Borisov u32 total_size; 4249fc0d82e1SNikolay Borisov 4250b7ef5f3aSFilipe Manana /* 4251b7ef5f3aSFilipe Manana * Before anything else, update keys in the parent and other ancestors 4252b7ef5f3aSFilipe Manana * if needed, then release the write locks on them, so that other tasks 4253b7ef5f3aSFilipe Manana * can use them while we modify the leaf. 4254b7ef5f3aSFilipe Manana */ 425524cdc847SFilipe Manana if (path->slots[0] == 0) { 4256b7ef5f3aSFilipe Manana btrfs_cpu_key_to_disk(&disk_key, &batch->keys[0]); 4257b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 425824cdc847SFilipe Manana } 425924cdc847SFilipe Manana btrfs_unlock_up_safe(path, 1); 426024cdc847SFilipe Manana 42615f39d397SChris Mason leaf = path->nodes[0]; 426244871b1bSChris Mason slot = path->slots[0]; 426374123bd7SChris Mason 42645f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 42658f881e8cSDavid Sterba data_end = leaf_data_end(leaf); 4266b7ef5f3aSFilipe Manana total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item)); 4267eb60ceacSChris Mason 4268e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < total_size) { 4269a4f78750SDavid Sterba btrfs_print_leaf(leaf); 42700b246afaSJeff Mahoney btrfs_crit(fs_info, "not enough freespace need %u have %d", 4271e902baacSDavid Sterba total_size, btrfs_leaf_free_space(leaf)); 4272be0e5c09SChris Mason BUG(); 4273d4dbff95SChris Mason } 42745f39d397SChris Mason 4275c82f823cSDavid Sterba btrfs_init_map_token(&token, leaf); 4276be0e5c09SChris Mason if (slot != nritems) { 4277dc2e724eSJosef Bacik unsigned int old_data = btrfs_item_data_end(leaf, slot); 4278be0e5c09SChris Mason 42795f39d397SChris Mason if (old_data < data_end) { 4280a4f78750SDavid Sterba btrfs_print_leaf(leaf); 42817269ddd2SNikolay Borisov btrfs_crit(fs_info, 42827269ddd2SNikolay Borisov "item at slot %d with data offset %u beyond data end of leaf %u", 42835f39d397SChris Mason slot, old_data, data_end); 4284290342f6SArnd Bergmann BUG(); 42855f39d397SChris Mason } 4286be0e5c09SChris Mason /* 4287be0e5c09SChris Mason * item0..itemN ... dataN.offset..dataN.size .. data0.size 4288be0e5c09SChris Mason */ 4289be0e5c09SChris Mason /* first correct the data pointers */ 42900783fcfcSChris Mason for (i = slot; i < nritems; i++) { 42915f39d397SChris Mason u32 ioff; 4292db94535dSChris Mason 42933212fa14SJosef Bacik ioff = btrfs_token_item_offset(&token, i); 42943212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, 4295b7ef5f3aSFilipe Manana ioff - batch->total_data_size); 42960783fcfcSChris Mason } 4297be0e5c09SChris Mason /* shift the items */ 4298637e3b48SJosef Bacik memmove_leaf_items(leaf, slot + batch->nr, slot, nritems - slot); 4299be0e5c09SChris Mason 4300be0e5c09SChris Mason /* shift the data */ 4301637e3b48SJosef Bacik memmove_leaf_data(leaf, data_end - batch->total_data_size, 4302637e3b48SJosef Bacik data_end, old_data - data_end); 4303be0e5c09SChris Mason data_end = old_data; 4304be0e5c09SChris Mason } 43055f39d397SChris Mason 430662e2749eSChris Mason /* setup the item for the new data */ 4307b7ef5f3aSFilipe Manana for (i = 0; i < batch->nr; i++) { 4308b7ef5f3aSFilipe Manana btrfs_cpu_key_to_disk(&disk_key, &batch->keys[i]); 43099c58309dSChris Mason btrfs_set_item_key(leaf, &disk_key, slot + i); 4310b7ef5f3aSFilipe Manana data_end -= batch->data_sizes[i]; 43113212fa14SJosef Bacik btrfs_set_token_item_offset(&token, slot + i, data_end); 43123212fa14SJosef Bacik btrfs_set_token_item_size(&token, slot + i, batch->data_sizes[i]); 43139c58309dSChris Mason } 431444871b1bSChris Mason 4315b7ef5f3aSFilipe Manana btrfs_set_header_nritems(leaf, nritems + batch->nr); 4316b9473439SChris Mason btrfs_mark_buffer_dirty(leaf); 4317aa5d6bedSChris Mason 4318e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 0) { 4319a4f78750SDavid Sterba btrfs_print_leaf(leaf); 4320be0e5c09SChris Mason BUG(); 43215f39d397SChris Mason } 432244871b1bSChris Mason } 432344871b1bSChris Mason 432444871b1bSChris Mason /* 4325f0641656SFilipe Manana * Insert a new item into a leaf. 4326f0641656SFilipe Manana * 4327f0641656SFilipe Manana * @root: The root of the btree. 4328f0641656SFilipe Manana * @path: A path pointing to the target leaf and slot. 4329f0641656SFilipe Manana * @key: The key of the new item. 4330f0641656SFilipe Manana * @data_size: The size of the data associated with the new key. 4331f0641656SFilipe Manana */ 4332f0641656SFilipe Manana void btrfs_setup_item_for_insert(struct btrfs_root *root, 4333f0641656SFilipe Manana struct btrfs_path *path, 4334f0641656SFilipe Manana const struct btrfs_key *key, 4335f0641656SFilipe Manana u32 data_size) 4336f0641656SFilipe Manana { 4337f0641656SFilipe Manana struct btrfs_item_batch batch; 4338f0641656SFilipe Manana 4339f0641656SFilipe Manana batch.keys = key; 4340f0641656SFilipe Manana batch.data_sizes = &data_size; 4341f0641656SFilipe Manana batch.total_data_size = data_size; 4342f0641656SFilipe Manana batch.nr = 1; 4343f0641656SFilipe Manana 4344f0641656SFilipe Manana setup_items_for_insert(root, path, &batch); 4345f0641656SFilipe Manana } 4346f0641656SFilipe Manana 4347f0641656SFilipe Manana /* 434844871b1bSChris Mason * Given a key and some data, insert items into the tree. 434944871b1bSChris Mason * This does all the path init required, making room in the tree if needed. 435044871b1bSChris Mason */ 435144871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 435244871b1bSChris Mason struct btrfs_root *root, 435344871b1bSChris Mason struct btrfs_path *path, 4354b7ef5f3aSFilipe Manana const struct btrfs_item_batch *batch) 435544871b1bSChris Mason { 435644871b1bSChris Mason int ret = 0; 435744871b1bSChris Mason int slot; 4358b7ef5f3aSFilipe Manana u32 total_size; 435944871b1bSChris Mason 4360b7ef5f3aSFilipe Manana total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item)); 4361b7ef5f3aSFilipe Manana ret = btrfs_search_slot(trans, root, &batch->keys[0], path, total_size, 1); 436244871b1bSChris Mason if (ret == 0) 436344871b1bSChris Mason return -EEXIST; 436444871b1bSChris Mason if (ret < 0) 4365143bede5SJeff Mahoney return ret; 436644871b1bSChris Mason 436744871b1bSChris Mason slot = path->slots[0]; 436844871b1bSChris Mason BUG_ON(slot < 0); 436944871b1bSChris Mason 4370b7ef5f3aSFilipe Manana setup_items_for_insert(root, path, batch); 4371143bede5SJeff Mahoney return 0; 437262e2749eSChris Mason } 437362e2749eSChris Mason 437462e2749eSChris Mason /* 437562e2749eSChris Mason * Given a key and some data, insert an item into the tree. 437662e2749eSChris Mason * This does all the path init required, making room in the tree if needed. 437762e2749eSChris Mason */ 4378310712b2SOmar Sandoval int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, 4379310712b2SOmar Sandoval const struct btrfs_key *cpu_key, void *data, 4380310712b2SOmar Sandoval u32 data_size) 438162e2749eSChris Mason { 438262e2749eSChris Mason int ret = 0; 43832c90e5d6SChris Mason struct btrfs_path *path; 43845f39d397SChris Mason struct extent_buffer *leaf; 43855f39d397SChris Mason unsigned long ptr; 438662e2749eSChris Mason 43872c90e5d6SChris Mason path = btrfs_alloc_path(); 4388db5b493aSTsutomu Itoh if (!path) 4389db5b493aSTsutomu Itoh return -ENOMEM; 43902c90e5d6SChris Mason ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 439162e2749eSChris Mason if (!ret) { 43925f39d397SChris Mason leaf = path->nodes[0]; 43935f39d397SChris Mason ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 43945f39d397SChris Mason write_extent_buffer(leaf, data, ptr, data_size); 43955f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 439662e2749eSChris Mason } 43972c90e5d6SChris Mason btrfs_free_path(path); 4398aa5d6bedSChris Mason return ret; 4399be0e5c09SChris Mason } 4400be0e5c09SChris Mason 440174123bd7SChris Mason /* 4402f0641656SFilipe Manana * This function duplicates an item, giving 'new_key' to the new item. 4403f0641656SFilipe Manana * It guarantees both items live in the same tree leaf and the new item is 4404f0641656SFilipe Manana * contiguous with the original item. 4405f0641656SFilipe Manana * 4406f0641656SFilipe Manana * This allows us to split a file extent in place, keeping a lock on the leaf 4407f0641656SFilipe Manana * the entire time. 4408f0641656SFilipe Manana */ 4409f0641656SFilipe Manana int btrfs_duplicate_item(struct btrfs_trans_handle *trans, 4410f0641656SFilipe Manana struct btrfs_root *root, 4411f0641656SFilipe Manana struct btrfs_path *path, 4412f0641656SFilipe Manana const struct btrfs_key *new_key) 4413f0641656SFilipe Manana { 4414f0641656SFilipe Manana struct extent_buffer *leaf; 4415f0641656SFilipe Manana int ret; 4416f0641656SFilipe Manana u32 item_size; 4417f0641656SFilipe Manana 4418f0641656SFilipe Manana leaf = path->nodes[0]; 44193212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]); 4420f0641656SFilipe Manana ret = setup_leaf_for_split(trans, root, path, 4421f0641656SFilipe Manana item_size + sizeof(struct btrfs_item)); 4422f0641656SFilipe Manana if (ret) 4423f0641656SFilipe Manana return ret; 4424f0641656SFilipe Manana 4425f0641656SFilipe Manana path->slots[0]++; 4426f0641656SFilipe Manana btrfs_setup_item_for_insert(root, path, new_key, item_size); 4427f0641656SFilipe Manana leaf = path->nodes[0]; 4428f0641656SFilipe Manana memcpy_extent_buffer(leaf, 4429f0641656SFilipe Manana btrfs_item_ptr_offset(leaf, path->slots[0]), 4430f0641656SFilipe Manana btrfs_item_ptr_offset(leaf, path->slots[0] - 1), 4431f0641656SFilipe Manana item_size); 4432f0641656SFilipe Manana return 0; 4433f0641656SFilipe Manana } 4434f0641656SFilipe Manana 4435f0641656SFilipe Manana /* 44365de08d7dSChris Mason * delete the pointer from a given node. 443774123bd7SChris Mason * 4438d352ac68SChris Mason * the tree should have been previously balanced so the deletion does not 4439d352ac68SChris Mason * empty a node. 4440016f9d0bSJosef Bacik * 4441016f9d0bSJosef Bacik * This is exported for use inside btrfs-progs, don't un-export it. 444274123bd7SChris Mason */ 4443016f9d0bSJosef Bacik void btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level, 4444016f9d0bSJosef Bacik int slot) 4445be0e5c09SChris Mason { 44465f39d397SChris Mason struct extent_buffer *parent = path->nodes[level]; 44477518a238SChris Mason u32 nritems; 4448f3ea38daSJan Schmidt int ret; 4449be0e5c09SChris Mason 44505f39d397SChris Mason nritems = btrfs_header_nritems(parent); 4451be0e5c09SChris Mason if (slot != nritems - 1) { 4452bf1d3425SDavid Sterba if (level) { 4453f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_move(parent, slot, 4454f3a84ccdSFilipe Manana slot + 1, nritems - slot - 1); 4455bf1d3425SDavid Sterba BUG_ON(ret < 0); 4456bf1d3425SDavid Sterba } 44575f39d397SChris Mason memmove_extent_buffer(parent, 4458e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(parent, slot), 4459e23efd8eSJosef Bacik btrfs_node_key_ptr_offset(parent, slot + 1), 4460d6025579SChris Mason sizeof(struct btrfs_key_ptr) * 4461d6025579SChris Mason (nritems - slot - 1)); 446257ba86c0SChris Mason } else if (level) { 4463f3a84ccdSFilipe Manana ret = btrfs_tree_mod_log_insert_key(parent, slot, 446433cff222SFilipe Manana BTRFS_MOD_LOG_KEY_REMOVE); 446557ba86c0SChris Mason BUG_ON(ret < 0); 4466be0e5c09SChris Mason } 4467f3ea38daSJan Schmidt 44687518a238SChris Mason nritems--; 44695f39d397SChris Mason btrfs_set_header_nritems(parent, nritems); 44707518a238SChris Mason if (nritems == 0 && parent == root->node) { 44715f39d397SChris Mason BUG_ON(btrfs_header_level(root->node) != 1); 4472eb60ceacSChris Mason /* just turn the root into a leaf and break */ 44735f39d397SChris Mason btrfs_set_header_level(root->node, 0); 4474bb803951SChris Mason } else if (slot == 0) { 44755f39d397SChris Mason struct btrfs_disk_key disk_key; 44765f39d397SChris Mason 44775f39d397SChris Mason btrfs_node_key(parent, &disk_key, 0); 4478b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, level + 1); 4479be0e5c09SChris Mason } 4480d6025579SChris Mason btrfs_mark_buffer_dirty(parent); 4481be0e5c09SChris Mason } 4482be0e5c09SChris Mason 448374123bd7SChris Mason /* 4484323ac95bSChris Mason * a helper function to delete the leaf pointed to by path->slots[1] and 44855d4f98a2SYan Zheng * path->nodes[1]. 4486323ac95bSChris Mason * 4487323ac95bSChris Mason * This deletes the pointer in path->nodes[1] and frees the leaf 4488323ac95bSChris Mason * block extent. zero is returned if it all worked out, < 0 otherwise. 4489323ac95bSChris Mason * 4490323ac95bSChris Mason * The path must have already been setup for deleting the leaf, including 4491323ac95bSChris Mason * all the proper balancing. path->nodes[1] must be locked. 4492323ac95bSChris Mason */ 4493143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, 4494323ac95bSChris Mason struct btrfs_root *root, 44955d4f98a2SYan Zheng struct btrfs_path *path, 44965d4f98a2SYan Zheng struct extent_buffer *leaf) 4497323ac95bSChris Mason { 44985d4f98a2SYan Zheng WARN_ON(btrfs_header_generation(leaf) != trans->transid); 4499016f9d0bSJosef Bacik btrfs_del_ptr(root, path, 1, path->slots[1]); 4500323ac95bSChris Mason 45014d081c41SChris Mason /* 45024d081c41SChris Mason * btrfs_free_extent is expensive, we want to make sure we 45034d081c41SChris Mason * aren't holding any locks when we call it 45044d081c41SChris Mason */ 45054d081c41SChris Mason btrfs_unlock_up_safe(path, 0); 45064d081c41SChris Mason 4507f0486c68SYan, Zheng root_sub_used(root, leaf->len); 4508f0486c68SYan, Zheng 450967439dadSDavid Sterba atomic_inc(&leaf->refs); 45107a163608SFilipe Manana btrfs_free_tree_block(trans, btrfs_root_id(root), leaf, 0, 1); 45113083ee2eSJosef Bacik free_extent_buffer_stale(leaf); 4512323ac95bSChris Mason } 4513323ac95bSChris Mason /* 451474123bd7SChris Mason * delete the item at the leaf level in path. If that empties 451574123bd7SChris Mason * the leaf, remove it from the tree 451674123bd7SChris Mason */ 451785e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 451885e21bacSChris Mason struct btrfs_path *path, int slot, int nr) 4519be0e5c09SChris Mason { 45200b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 45215f39d397SChris Mason struct extent_buffer *leaf; 4522aa5d6bedSChris Mason int ret = 0; 4523aa5d6bedSChris Mason int wret; 45247518a238SChris Mason u32 nritems; 4525be0e5c09SChris Mason 45265f39d397SChris Mason leaf = path->nodes[0]; 45275f39d397SChris Mason nritems = btrfs_header_nritems(leaf); 4528be0e5c09SChris Mason 452985e21bacSChris Mason if (slot + nr != nritems) { 45300cae23b6SFilipe Manana const u32 last_off = btrfs_item_offset(leaf, slot + nr - 1); 45310cae23b6SFilipe Manana const int data_end = leaf_data_end(leaf); 4532c82f823cSDavid Sterba struct btrfs_map_token token; 45330cae23b6SFilipe Manana u32 dsize = 0; 45340cae23b6SFilipe Manana int i; 45350cae23b6SFilipe Manana 45360cae23b6SFilipe Manana for (i = 0; i < nr; i++) 45370cae23b6SFilipe Manana dsize += btrfs_item_size(leaf, slot + i); 45385f39d397SChris Mason 4539637e3b48SJosef Bacik memmove_leaf_data(leaf, data_end + dsize, data_end, 454085e21bacSChris Mason last_off - data_end); 45415f39d397SChris Mason 4542c82f823cSDavid Sterba btrfs_init_map_token(&token, leaf); 454385e21bacSChris Mason for (i = slot + nr; i < nritems; i++) { 45445f39d397SChris Mason u32 ioff; 4545db94535dSChris Mason 45463212fa14SJosef Bacik ioff = btrfs_token_item_offset(&token, i); 45473212fa14SJosef Bacik btrfs_set_token_item_offset(&token, i, ioff + dsize); 45480783fcfcSChris Mason } 4549db94535dSChris Mason 4550637e3b48SJosef Bacik memmove_leaf_items(leaf, slot, slot + nr, nritems - slot - nr); 4551be0e5c09SChris Mason } 455285e21bacSChris Mason btrfs_set_header_nritems(leaf, nritems - nr); 455385e21bacSChris Mason nritems -= nr; 45545f39d397SChris Mason 455574123bd7SChris Mason /* delete the leaf if we've emptied it */ 45567518a238SChris Mason if (nritems == 0) { 45575f39d397SChris Mason if (leaf == root->node) { 45585f39d397SChris Mason btrfs_set_header_level(leaf, 0); 45599a8dd150SChris Mason } else { 4560190a8339SJosef Bacik btrfs_clear_buffer_dirty(trans, leaf); 4561143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 45629a8dd150SChris Mason } 4563be0e5c09SChris Mason } else { 45647518a238SChris Mason int used = leaf_space_used(leaf, 0, nritems); 4565aa5d6bedSChris Mason if (slot == 0) { 45665f39d397SChris Mason struct btrfs_disk_key disk_key; 45675f39d397SChris Mason 45685f39d397SChris Mason btrfs_item_key(leaf, &disk_key, 0); 4569b167fa91SNikolay Borisov fixup_low_keys(path, &disk_key, 1); 4570aa5d6bedSChris Mason } 4571aa5d6bedSChris Mason 45727c4063d1SFilipe Manana /* 45737c4063d1SFilipe Manana * Try to delete the leaf if it is mostly empty. We do this by 45747c4063d1SFilipe Manana * trying to move all its items into its left and right neighbours. 45757c4063d1SFilipe Manana * If we can't move all the items, then we don't delete it - it's 45767c4063d1SFilipe Manana * not ideal, but future insertions might fill the leaf with more 45777c4063d1SFilipe Manana * items, or items from other leaves might be moved later into our 45787c4063d1SFilipe Manana * leaf due to deletions on those leaves. 45797c4063d1SFilipe Manana */ 45800b246afaSJeff Mahoney if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) { 45817c4063d1SFilipe Manana u32 min_push_space; 45827c4063d1SFilipe Manana 4583be0e5c09SChris Mason /* push_leaf_left fixes the path. 4584be0e5c09SChris Mason * make sure the path still points to our leaf 4585016f9d0bSJosef Bacik * for possible call to btrfs_del_ptr below 4586be0e5c09SChris Mason */ 45874920c9acSChris Mason slot = path->slots[1]; 458867439dadSDavid Sterba atomic_inc(&leaf->refs); 45897c4063d1SFilipe Manana /* 45907c4063d1SFilipe Manana * We want to be able to at least push one item to the 45917c4063d1SFilipe Manana * left neighbour leaf, and that's the first item. 45927c4063d1SFilipe Manana */ 45937c4063d1SFilipe Manana min_push_space = sizeof(struct btrfs_item) + 45947c4063d1SFilipe Manana btrfs_item_size(leaf, 0); 45957c4063d1SFilipe Manana wret = push_leaf_left(trans, root, path, 0, 45967c4063d1SFilipe Manana min_push_space, 1, (u32)-1); 459754aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 4598aa5d6bedSChris Mason ret = wret; 45995f39d397SChris Mason 46005f39d397SChris Mason if (path->nodes[0] == leaf && 46015f39d397SChris Mason btrfs_header_nritems(leaf)) { 46027c4063d1SFilipe Manana /* 46037c4063d1SFilipe Manana * If we were not able to push all items from our 46047c4063d1SFilipe Manana * leaf to its left neighbour, then attempt to 46057c4063d1SFilipe Manana * either push all the remaining items to the 46067c4063d1SFilipe Manana * right neighbour or none. There's no advantage 46077c4063d1SFilipe Manana * in pushing only some items, instead of all, as 46087c4063d1SFilipe Manana * it's pointless to end up with a leaf having 46097c4063d1SFilipe Manana * too few items while the neighbours can be full 46107c4063d1SFilipe Manana * or nearly full. 46117c4063d1SFilipe Manana */ 46127c4063d1SFilipe Manana nritems = btrfs_header_nritems(leaf); 46137c4063d1SFilipe Manana min_push_space = leaf_space_used(leaf, 0, nritems); 46147c4063d1SFilipe Manana wret = push_leaf_right(trans, root, path, 0, 46157c4063d1SFilipe Manana min_push_space, 1, 0); 461654aa1f4dSChris Mason if (wret < 0 && wret != -ENOSPC) 4617aa5d6bedSChris Mason ret = wret; 4618aa5d6bedSChris Mason } 46195f39d397SChris Mason 46205f39d397SChris Mason if (btrfs_header_nritems(leaf) == 0) { 4621323ac95bSChris Mason path->slots[1] = slot; 4622143bede5SJeff Mahoney btrfs_del_leaf(trans, root, path, leaf); 46235f39d397SChris Mason free_extent_buffer(leaf); 4624143bede5SJeff Mahoney ret = 0; 46255de08d7dSChris Mason } else { 4626925baeddSChris Mason /* if we're still in the path, make sure 4627925baeddSChris Mason * we're dirty. Otherwise, one of the 4628925baeddSChris Mason * push_leaf functions must have already 4629925baeddSChris Mason * dirtied this buffer 4630925baeddSChris Mason */ 4631925baeddSChris Mason if (path->nodes[0] == leaf) 46325f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 46335f39d397SChris Mason free_extent_buffer(leaf); 4634be0e5c09SChris Mason } 4635d5719762SChris Mason } else { 46365f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 4637be0e5c09SChris Mason } 46389a8dd150SChris Mason } 4639aa5d6bedSChris Mason return ret; 46409a8dd150SChris Mason } 46419a8dd150SChris Mason 464297571fd0SChris Mason /* 46433f157a2fSChris Mason * A helper function to walk down the tree starting at min_key, and looking 4644de78b51aSEric Sandeen * for nodes or leaves that are have a minimum transaction id. 4645de78b51aSEric Sandeen * This is used by the btree defrag code, and tree logging 46463f157a2fSChris Mason * 46473f157a2fSChris Mason * This does not cow, but it does stuff the starting key it finds back 46483f157a2fSChris Mason * into min_key, so you can call btrfs_search_slot with cow=1 on the 46493f157a2fSChris Mason * key and get a writable path. 46503f157a2fSChris Mason * 46513f157a2fSChris Mason * This honors path->lowest_level to prevent descent past a given level 46523f157a2fSChris Mason * of the tree. 46533f157a2fSChris Mason * 4654d352ac68SChris Mason * min_trans indicates the oldest transaction that you are interested 4655d352ac68SChris Mason * in walking through. Any nodes or leaves older than min_trans are 4656d352ac68SChris Mason * skipped over (without reading them). 4657d352ac68SChris Mason * 46583f157a2fSChris Mason * returns zero if something useful was found, < 0 on error and 1 if there 46593f157a2fSChris Mason * was nothing in the tree that matched the search criteria. 46603f157a2fSChris Mason */ 46613f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, 4662de78b51aSEric Sandeen struct btrfs_path *path, 46633f157a2fSChris Mason u64 min_trans) 46643f157a2fSChris Mason { 46653f157a2fSChris Mason struct extent_buffer *cur; 46663f157a2fSChris Mason struct btrfs_key found_key; 46673f157a2fSChris Mason int slot; 46689652480bSYan int sret; 46693f157a2fSChris Mason u32 nritems; 46703f157a2fSChris Mason int level; 46713f157a2fSChris Mason int ret = 1; 4672f98de9b9SFilipe Manana int keep_locks = path->keep_locks; 46733f157a2fSChris Mason 4674c922b016SStefan Roesch ASSERT(!path->nowait); 4675f98de9b9SFilipe Manana path->keep_locks = 1; 46763f157a2fSChris Mason again: 4677bd681513SChris Mason cur = btrfs_read_lock_root_node(root); 46783f157a2fSChris Mason level = btrfs_header_level(cur); 4679e02119d5SChris Mason WARN_ON(path->nodes[level]); 46803f157a2fSChris Mason path->nodes[level] = cur; 4681bd681513SChris Mason path->locks[level] = BTRFS_READ_LOCK; 46823f157a2fSChris Mason 46833f157a2fSChris Mason if (btrfs_header_generation(cur) < min_trans) { 46843f157a2fSChris Mason ret = 1; 46853f157a2fSChris Mason goto out; 46863f157a2fSChris Mason } 46873f157a2fSChris Mason while (1) { 46883f157a2fSChris Mason nritems = btrfs_header_nritems(cur); 46893f157a2fSChris Mason level = btrfs_header_level(cur); 4690fdf8d595SAnand Jain sret = btrfs_bin_search(cur, 0, min_key, &slot); 4691cbca7d59SFilipe Manana if (sret < 0) { 4692cbca7d59SFilipe Manana ret = sret; 4693cbca7d59SFilipe Manana goto out; 4694cbca7d59SFilipe Manana } 46953f157a2fSChris Mason 4696323ac95bSChris Mason /* at the lowest level, we're done, setup the path and exit */ 4697323ac95bSChris Mason if (level == path->lowest_level) { 4698e02119d5SChris Mason if (slot >= nritems) 4699e02119d5SChris Mason goto find_next_key; 47003f157a2fSChris Mason ret = 0; 47013f157a2fSChris Mason path->slots[level] = slot; 47023f157a2fSChris Mason btrfs_item_key_to_cpu(cur, &found_key, slot); 47033f157a2fSChris Mason goto out; 47043f157a2fSChris Mason } 47059652480bSYan if (sret && slot > 0) 47069652480bSYan slot--; 47073f157a2fSChris Mason /* 4708de78b51aSEric Sandeen * check this node pointer against the min_trans parameters. 4709260db43cSRandy Dunlap * If it is too old, skip to the next one. 47103f157a2fSChris Mason */ 47113f157a2fSChris Mason while (slot < nritems) { 47123f157a2fSChris Mason u64 gen; 4713e02119d5SChris Mason 47143f157a2fSChris Mason gen = btrfs_node_ptr_generation(cur, slot); 47153f157a2fSChris Mason if (gen < min_trans) { 47163f157a2fSChris Mason slot++; 47173f157a2fSChris Mason continue; 47183f157a2fSChris Mason } 47193f157a2fSChris Mason break; 47203f157a2fSChris Mason } 4721e02119d5SChris Mason find_next_key: 47223f157a2fSChris Mason /* 47233f157a2fSChris Mason * we didn't find a candidate key in this node, walk forward 47243f157a2fSChris Mason * and find another one 47253f157a2fSChris Mason */ 47263f157a2fSChris Mason if (slot >= nritems) { 4727e02119d5SChris Mason path->slots[level] = slot; 4728e02119d5SChris Mason sret = btrfs_find_next_key(root, path, min_key, level, 4729de78b51aSEric Sandeen min_trans); 4730e02119d5SChris Mason if (sret == 0) { 4731b3b4aa74SDavid Sterba btrfs_release_path(path); 47323f157a2fSChris Mason goto again; 47333f157a2fSChris Mason } else { 47343f157a2fSChris Mason goto out; 47353f157a2fSChris Mason } 47363f157a2fSChris Mason } 47373f157a2fSChris Mason /* save our key for returning back */ 47383f157a2fSChris Mason btrfs_node_key_to_cpu(cur, &found_key, slot); 47393f157a2fSChris Mason path->slots[level] = slot; 47403f157a2fSChris Mason if (level == path->lowest_level) { 47413f157a2fSChris Mason ret = 0; 47423f157a2fSChris Mason goto out; 47433f157a2fSChris Mason } 47444b231ae4SDavid Sterba cur = btrfs_read_node_slot(cur, slot); 4745fb770ae4SLiu Bo if (IS_ERR(cur)) { 4746fb770ae4SLiu Bo ret = PTR_ERR(cur); 4747fb770ae4SLiu Bo goto out; 4748fb770ae4SLiu Bo } 47493f157a2fSChris Mason 4750bd681513SChris Mason btrfs_tree_read_lock(cur); 4751b4ce94deSChris Mason 4752bd681513SChris Mason path->locks[level - 1] = BTRFS_READ_LOCK; 47533f157a2fSChris Mason path->nodes[level - 1] = cur; 4754f7c79f30SChris Mason unlock_up(path, level, 1, 0, NULL); 47553f157a2fSChris Mason } 47563f157a2fSChris Mason out: 4757f98de9b9SFilipe Manana path->keep_locks = keep_locks; 4758f98de9b9SFilipe Manana if (ret == 0) { 4759f98de9b9SFilipe Manana btrfs_unlock_up_safe(path, path->lowest_level + 1); 4760f98de9b9SFilipe Manana memcpy(min_key, &found_key, sizeof(found_key)); 4761f98de9b9SFilipe Manana } 47623f157a2fSChris Mason return ret; 47633f157a2fSChris Mason } 47643f157a2fSChris Mason 47653f157a2fSChris Mason /* 47663f157a2fSChris Mason * this is similar to btrfs_next_leaf, but does not try to preserve 47673f157a2fSChris Mason * and fixup the path. It looks for and returns the next key in the 4768de78b51aSEric Sandeen * tree based on the current path and the min_trans parameters. 47693f157a2fSChris Mason * 47703f157a2fSChris Mason * 0 is returned if another key is found, < 0 if there are any errors 47713f157a2fSChris Mason * and 1 is returned if there are no higher keys in the tree 47723f157a2fSChris Mason * 47733f157a2fSChris Mason * path->keep_locks should be set to 1 on the search made before 47743f157a2fSChris Mason * calling this function. 47753f157a2fSChris Mason */ 4776e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, 4777de78b51aSEric Sandeen struct btrfs_key *key, int level, u64 min_trans) 4778e7a84565SChris Mason { 4779e7a84565SChris Mason int slot; 4780e7a84565SChris Mason struct extent_buffer *c; 4781e7a84565SChris Mason 47826a9fb468SJosef Bacik WARN_ON(!path->keep_locks && !path->skip_locking); 4783e7a84565SChris Mason while (level < BTRFS_MAX_LEVEL) { 4784e7a84565SChris Mason if (!path->nodes[level]) 4785e7a84565SChris Mason return 1; 4786e7a84565SChris Mason 4787e7a84565SChris Mason slot = path->slots[level] + 1; 4788e7a84565SChris Mason c = path->nodes[level]; 47893f157a2fSChris Mason next: 4790e7a84565SChris Mason if (slot >= btrfs_header_nritems(c)) { 479133c66f43SYan Zheng int ret; 479233c66f43SYan Zheng int orig_lowest; 479333c66f43SYan Zheng struct btrfs_key cur_key; 479433c66f43SYan Zheng if (level + 1 >= BTRFS_MAX_LEVEL || 479533c66f43SYan Zheng !path->nodes[level + 1]) 4796e7a84565SChris Mason return 1; 479733c66f43SYan Zheng 47986a9fb468SJosef Bacik if (path->locks[level + 1] || path->skip_locking) { 479933c66f43SYan Zheng level++; 4800e7a84565SChris Mason continue; 4801e7a84565SChris Mason } 480233c66f43SYan Zheng 480333c66f43SYan Zheng slot = btrfs_header_nritems(c) - 1; 480433c66f43SYan Zheng if (level == 0) 480533c66f43SYan Zheng btrfs_item_key_to_cpu(c, &cur_key, slot); 480633c66f43SYan Zheng else 480733c66f43SYan Zheng btrfs_node_key_to_cpu(c, &cur_key, slot); 480833c66f43SYan Zheng 480933c66f43SYan Zheng orig_lowest = path->lowest_level; 4810b3b4aa74SDavid Sterba btrfs_release_path(path); 481133c66f43SYan Zheng path->lowest_level = level; 481233c66f43SYan Zheng ret = btrfs_search_slot(NULL, root, &cur_key, path, 481333c66f43SYan Zheng 0, 0); 481433c66f43SYan Zheng path->lowest_level = orig_lowest; 481533c66f43SYan Zheng if (ret < 0) 481633c66f43SYan Zheng return ret; 481733c66f43SYan Zheng 481833c66f43SYan Zheng c = path->nodes[level]; 481933c66f43SYan Zheng slot = path->slots[level]; 482033c66f43SYan Zheng if (ret == 0) 482133c66f43SYan Zheng slot++; 482233c66f43SYan Zheng goto next; 482333c66f43SYan Zheng } 482433c66f43SYan Zheng 4825e7a84565SChris Mason if (level == 0) 4826e7a84565SChris Mason btrfs_item_key_to_cpu(c, key, slot); 48273f157a2fSChris Mason else { 48283f157a2fSChris Mason u64 gen = btrfs_node_ptr_generation(c, slot); 48293f157a2fSChris Mason 48303f157a2fSChris Mason if (gen < min_trans) { 48313f157a2fSChris Mason slot++; 48323f157a2fSChris Mason goto next; 48333f157a2fSChris Mason } 4834e7a84565SChris Mason btrfs_node_key_to_cpu(c, key, slot); 48353f157a2fSChris Mason } 4836e7a84565SChris Mason return 0; 4837e7a84565SChris Mason } 4838e7a84565SChris Mason return 1; 4839e7a84565SChris Mason } 4840e7a84565SChris Mason 48413d7806ecSJan Schmidt int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, 48423d7806ecSJan Schmidt u64 time_seq) 48433d7806ecSJan Schmidt { 4844d97e63b6SChris Mason int slot; 48458e73f275SChris Mason int level; 48465f39d397SChris Mason struct extent_buffer *c; 48478e73f275SChris Mason struct extent_buffer *next; 4848d96b3424SFilipe Manana struct btrfs_fs_info *fs_info = root->fs_info; 4849925baeddSChris Mason struct btrfs_key key; 4850d96b3424SFilipe Manana bool need_commit_sem = false; 4851925baeddSChris Mason u32 nritems; 4852925baeddSChris Mason int ret; 48530e46318dSJosef Bacik int i; 4854925baeddSChris Mason 4855bdcdd86cSFilipe Manana /* 4856bdcdd86cSFilipe Manana * The nowait semantics are used only for write paths, where we don't 4857bdcdd86cSFilipe Manana * use the tree mod log and sequence numbers. 4858bdcdd86cSFilipe Manana */ 4859bdcdd86cSFilipe Manana if (time_seq) 4860c922b016SStefan Roesch ASSERT(!path->nowait); 4861c922b016SStefan Roesch 4862925baeddSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4863d397712bSChris Mason if (nritems == 0) 4864925baeddSChris Mason return 1; 4865925baeddSChris Mason 48668e73f275SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 48678e73f275SChris Mason again: 48688e73f275SChris Mason level = 1; 48698e73f275SChris Mason next = NULL; 4870b3b4aa74SDavid Sterba btrfs_release_path(path); 48718e73f275SChris Mason 4872a2135011SChris Mason path->keep_locks = 1; 48738e73f275SChris Mason 4874d96b3424SFilipe Manana if (time_seq) { 48753d7806ecSJan Schmidt ret = btrfs_search_old_slot(root, &key, path, time_seq); 4876d96b3424SFilipe Manana } else { 4877d96b3424SFilipe Manana if (path->need_commit_sem) { 4878d96b3424SFilipe Manana path->need_commit_sem = 0; 4879d96b3424SFilipe Manana need_commit_sem = true; 4880bdcdd86cSFilipe Manana if (path->nowait) { 4881bdcdd86cSFilipe Manana if (!down_read_trylock(&fs_info->commit_root_sem)) { 4882bdcdd86cSFilipe Manana ret = -EAGAIN; 4883bdcdd86cSFilipe Manana goto done; 4884bdcdd86cSFilipe Manana } 4885bdcdd86cSFilipe Manana } else { 4886d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem); 4887d96b3424SFilipe Manana } 4888bdcdd86cSFilipe Manana } 4889925baeddSChris Mason ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4890d96b3424SFilipe Manana } 4891925baeddSChris Mason path->keep_locks = 0; 4892925baeddSChris Mason 4893925baeddSChris Mason if (ret < 0) 4894d96b3424SFilipe Manana goto done; 4895925baeddSChris Mason 4896a2135011SChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 4897168fd7d2SChris Mason /* 4898168fd7d2SChris Mason * by releasing the path above we dropped all our locks. A balance 4899168fd7d2SChris Mason * could have added more items next to the key that used to be 4900168fd7d2SChris Mason * at the very end of the block. So, check again here and 4901168fd7d2SChris Mason * advance the path if there are now more items available. 4902168fd7d2SChris Mason */ 4903a2135011SChris Mason if (nritems > 0 && path->slots[0] < nritems - 1) { 4904e457afecSYan Zheng if (ret == 0) 4905168fd7d2SChris Mason path->slots[0]++; 49068e73f275SChris Mason ret = 0; 4907925baeddSChris Mason goto done; 4908925baeddSChris Mason } 49090b43e04fSLiu Bo /* 49100b43e04fSLiu Bo * So the above check misses one case: 49110b43e04fSLiu Bo * - after releasing the path above, someone has removed the item that 49120b43e04fSLiu Bo * used to be at the very end of the block, and balance between leafs 49130b43e04fSLiu Bo * gets another one with bigger key.offset to replace it. 49140b43e04fSLiu Bo * 49150b43e04fSLiu Bo * This one should be returned as well, or we can get leaf corruption 49160b43e04fSLiu Bo * later(esp. in __btrfs_drop_extents()). 49170b43e04fSLiu Bo * 49180b43e04fSLiu Bo * And a bit more explanation about this check, 49190b43e04fSLiu Bo * with ret > 0, the key isn't found, the path points to the slot 49200b43e04fSLiu Bo * where it should be inserted, so the path->slots[0] item must be the 49210b43e04fSLiu Bo * bigger one. 49220b43e04fSLiu Bo */ 49230b43e04fSLiu Bo if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) { 49240b43e04fSLiu Bo ret = 0; 49250b43e04fSLiu Bo goto done; 49260b43e04fSLiu Bo } 4927d97e63b6SChris Mason 4928234b63a0SChris Mason while (level < BTRFS_MAX_LEVEL) { 49298e73f275SChris Mason if (!path->nodes[level]) { 49308e73f275SChris Mason ret = 1; 49318e73f275SChris Mason goto done; 49328e73f275SChris Mason } 49335f39d397SChris Mason 4934d97e63b6SChris Mason slot = path->slots[level] + 1; 4935d97e63b6SChris Mason c = path->nodes[level]; 49365f39d397SChris Mason if (slot >= btrfs_header_nritems(c)) { 4937d97e63b6SChris Mason level++; 49388e73f275SChris Mason if (level == BTRFS_MAX_LEVEL) { 49398e73f275SChris Mason ret = 1; 49408e73f275SChris Mason goto done; 49418e73f275SChris Mason } 4942d97e63b6SChris Mason continue; 4943d97e63b6SChris Mason } 49445f39d397SChris Mason 49450e46318dSJosef Bacik 49460e46318dSJosef Bacik /* 49470e46318dSJosef Bacik * Our current level is where we're going to start from, and to 49480e46318dSJosef Bacik * make sure lockdep doesn't complain we need to drop our locks 49490e46318dSJosef Bacik * and nodes from 0 to our current level. 49500e46318dSJosef Bacik */ 49510e46318dSJosef Bacik for (i = 0; i < level; i++) { 49520e46318dSJosef Bacik if (path->locks[level]) { 49530e46318dSJosef Bacik btrfs_tree_read_unlock(path->nodes[i]); 49540e46318dSJosef Bacik path->locks[i] = 0; 49550e46318dSJosef Bacik } 49560e46318dSJosef Bacik free_extent_buffer(path->nodes[i]); 49570e46318dSJosef Bacik path->nodes[i] = NULL; 4958925baeddSChris Mason } 49595f39d397SChris Mason 49608e73f275SChris Mason next = c; 4961d07b8528SLiu Bo ret = read_block_for_search(root, path, &next, level, 4962cda79c54SDavid Sterba slot, &key); 4963bdcdd86cSFilipe Manana if (ret == -EAGAIN && !path->nowait) 49648e73f275SChris Mason goto again; 49655f39d397SChris Mason 496676a05b35SChris Mason if (ret < 0) { 4967b3b4aa74SDavid Sterba btrfs_release_path(path); 496876a05b35SChris Mason goto done; 496976a05b35SChris Mason } 497076a05b35SChris Mason 49715cd57b2cSChris Mason if (!path->skip_locking) { 4972bd681513SChris Mason ret = btrfs_try_tree_read_lock(next); 4973bdcdd86cSFilipe Manana if (!ret && path->nowait) { 4974bdcdd86cSFilipe Manana ret = -EAGAIN; 4975bdcdd86cSFilipe Manana goto done; 4976bdcdd86cSFilipe Manana } 4977d42244a0SJan Schmidt if (!ret && time_seq) { 4978d42244a0SJan Schmidt /* 4979d42244a0SJan Schmidt * If we don't get the lock, we may be racing 4980d42244a0SJan Schmidt * with push_leaf_left, holding that lock while 4981d42244a0SJan Schmidt * itself waiting for the leaf we've currently 4982d42244a0SJan Schmidt * locked. To solve this situation, we give up 4983d42244a0SJan Schmidt * on our lock and cycle. 4984d42244a0SJan Schmidt */ 4985cf538830SJan Schmidt free_extent_buffer(next); 4986d42244a0SJan Schmidt btrfs_release_path(path); 4987d42244a0SJan Schmidt cond_resched(); 4988d42244a0SJan Schmidt goto again; 4989d42244a0SJan Schmidt } 49900e46318dSJosef Bacik if (!ret) 49910e46318dSJosef Bacik btrfs_tree_read_lock(next); 4992bd681513SChris Mason } 4993d97e63b6SChris Mason break; 4994d97e63b6SChris Mason } 4995d97e63b6SChris Mason path->slots[level] = slot; 4996d97e63b6SChris Mason while (1) { 4997d97e63b6SChris Mason level--; 4998d97e63b6SChris Mason path->nodes[level] = next; 4999d97e63b6SChris Mason path->slots[level] = 0; 5000a74a4b97SChris Mason if (!path->skip_locking) 5001ffeb03cfSJosef Bacik path->locks[level] = BTRFS_READ_LOCK; 5002d97e63b6SChris Mason if (!level) 5003d97e63b6SChris Mason break; 5004b4ce94deSChris Mason 5005d07b8528SLiu Bo ret = read_block_for_search(root, path, &next, level, 5006cda79c54SDavid Sterba 0, &key); 5007bdcdd86cSFilipe Manana if (ret == -EAGAIN && !path->nowait) 50088e73f275SChris Mason goto again; 50098e73f275SChris Mason 501076a05b35SChris Mason if (ret < 0) { 5011b3b4aa74SDavid Sterba btrfs_release_path(path); 501276a05b35SChris Mason goto done; 501376a05b35SChris Mason } 501476a05b35SChris Mason 5015bdcdd86cSFilipe Manana if (!path->skip_locking) { 5016bdcdd86cSFilipe Manana if (path->nowait) { 5017bdcdd86cSFilipe Manana if (!btrfs_try_tree_read_lock(next)) { 5018bdcdd86cSFilipe Manana ret = -EAGAIN; 5019bdcdd86cSFilipe Manana goto done; 5020bdcdd86cSFilipe Manana } 5021bdcdd86cSFilipe Manana } else { 50220e46318dSJosef Bacik btrfs_tree_read_lock(next); 5023d97e63b6SChris Mason } 5024bdcdd86cSFilipe Manana } 5025bdcdd86cSFilipe Manana } 50268e73f275SChris Mason ret = 0; 5027925baeddSChris Mason done: 5028f7c79f30SChris Mason unlock_up(path, 0, 1, 0, NULL); 5029d96b3424SFilipe Manana if (need_commit_sem) { 5030d96b3424SFilipe Manana int ret2; 5031d96b3424SFilipe Manana 5032d96b3424SFilipe Manana path->need_commit_sem = 1; 5033d96b3424SFilipe Manana ret2 = finish_need_commit_sem_search(path); 5034d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem); 5035d96b3424SFilipe Manana if (ret2) 5036d96b3424SFilipe Manana ret = ret2; 5037d96b3424SFilipe Manana } 50388e73f275SChris Mason 50398e73f275SChris Mason return ret; 5040d97e63b6SChris Mason } 50410b86a832SChris Mason 5042890d2b1aSJosef Bacik int btrfs_next_old_item(struct btrfs_root *root, struct btrfs_path *path, u64 time_seq) 5043890d2b1aSJosef Bacik { 5044890d2b1aSJosef Bacik path->slots[0]++; 5045890d2b1aSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) 5046890d2b1aSJosef Bacik return btrfs_next_old_leaf(root, path, time_seq); 5047890d2b1aSJosef Bacik return 0; 5048890d2b1aSJosef Bacik } 5049890d2b1aSJosef Bacik 50503f157a2fSChris Mason /* 50513f157a2fSChris Mason * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps 50523f157a2fSChris Mason * searching until it gets past min_objectid or finds an item of 'type' 50533f157a2fSChris Mason * 50543f157a2fSChris Mason * returns 0 if something is found, 1 if nothing was found and < 0 on error 50553f157a2fSChris Mason */ 50560b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root, 50570b86a832SChris Mason struct btrfs_path *path, u64 min_objectid, 50580b86a832SChris Mason int type) 50590b86a832SChris Mason { 50600b86a832SChris Mason struct btrfs_key found_key; 50610b86a832SChris Mason struct extent_buffer *leaf; 5062e02119d5SChris Mason u32 nritems; 50630b86a832SChris Mason int ret; 50640b86a832SChris Mason 50650b86a832SChris Mason while (1) { 50660b86a832SChris Mason if (path->slots[0] == 0) { 50670b86a832SChris Mason ret = btrfs_prev_leaf(root, path); 50680b86a832SChris Mason if (ret != 0) 50690b86a832SChris Mason return ret; 50700b86a832SChris Mason } else { 50710b86a832SChris Mason path->slots[0]--; 50720b86a832SChris Mason } 50730b86a832SChris Mason leaf = path->nodes[0]; 5074e02119d5SChris Mason nritems = btrfs_header_nritems(leaf); 5075e02119d5SChris Mason if (nritems == 0) 5076e02119d5SChris Mason return 1; 5077e02119d5SChris Mason if (path->slots[0] == nritems) 5078e02119d5SChris Mason path->slots[0]--; 5079e02119d5SChris Mason 50800b86a832SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 5081e02119d5SChris Mason if (found_key.objectid < min_objectid) 5082e02119d5SChris Mason break; 50830a4eefbbSYan Zheng if (found_key.type == type) 50840a4eefbbSYan Zheng return 0; 5085e02119d5SChris Mason if (found_key.objectid == min_objectid && 5086e02119d5SChris Mason found_key.type < type) 5087e02119d5SChris Mason break; 50880b86a832SChris Mason } 50890b86a832SChris Mason return 1; 50900b86a832SChris Mason } 5091ade2e0b3SWang Shilong 5092ade2e0b3SWang Shilong /* 5093ade2e0b3SWang Shilong * search in extent tree to find a previous Metadata/Data extent item with 5094ade2e0b3SWang Shilong * min objecitd. 5095ade2e0b3SWang Shilong * 5096ade2e0b3SWang Shilong * returns 0 if something is found, 1 if nothing was found and < 0 on error 5097ade2e0b3SWang Shilong */ 5098ade2e0b3SWang Shilong int btrfs_previous_extent_item(struct btrfs_root *root, 5099ade2e0b3SWang Shilong struct btrfs_path *path, u64 min_objectid) 5100ade2e0b3SWang Shilong { 5101ade2e0b3SWang Shilong struct btrfs_key found_key; 5102ade2e0b3SWang Shilong struct extent_buffer *leaf; 5103ade2e0b3SWang Shilong u32 nritems; 5104ade2e0b3SWang Shilong int ret; 5105ade2e0b3SWang Shilong 5106ade2e0b3SWang Shilong while (1) { 5107ade2e0b3SWang Shilong if (path->slots[0] == 0) { 5108ade2e0b3SWang Shilong ret = btrfs_prev_leaf(root, path); 5109ade2e0b3SWang Shilong if (ret != 0) 5110ade2e0b3SWang Shilong return ret; 5111ade2e0b3SWang Shilong } else { 5112ade2e0b3SWang Shilong path->slots[0]--; 5113ade2e0b3SWang Shilong } 5114ade2e0b3SWang Shilong leaf = path->nodes[0]; 5115ade2e0b3SWang Shilong nritems = btrfs_header_nritems(leaf); 5116ade2e0b3SWang Shilong if (nritems == 0) 5117ade2e0b3SWang Shilong return 1; 5118ade2e0b3SWang Shilong if (path->slots[0] == nritems) 5119ade2e0b3SWang Shilong path->slots[0]--; 5120ade2e0b3SWang Shilong 5121ade2e0b3SWang Shilong btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 5122ade2e0b3SWang Shilong if (found_key.objectid < min_objectid) 5123ade2e0b3SWang Shilong break; 5124ade2e0b3SWang Shilong if (found_key.type == BTRFS_EXTENT_ITEM_KEY || 5125ade2e0b3SWang Shilong found_key.type == BTRFS_METADATA_ITEM_KEY) 5126ade2e0b3SWang Shilong return 0; 5127ade2e0b3SWang Shilong if (found_key.objectid == min_objectid && 5128ade2e0b3SWang Shilong found_key.type < BTRFS_EXTENT_ITEM_KEY) 5129ade2e0b3SWang Shilong break; 5130ade2e0b3SWang Shilong } 5131ade2e0b3SWang Shilong return 1; 5132ade2e0b3SWang Shilong } 5133226463d7SJosef Bacik 5134226463d7SJosef Bacik int __init btrfs_ctree_init(void) 5135226463d7SJosef Bacik { 5136226463d7SJosef Bacik btrfs_path_cachep = kmem_cache_create("btrfs_path", 5137226463d7SJosef Bacik sizeof(struct btrfs_path), 0, 5138226463d7SJosef Bacik SLAB_MEM_SPREAD, NULL); 5139226463d7SJosef Bacik if (!btrfs_path_cachep) 5140226463d7SJosef Bacik return -ENOMEM; 5141226463d7SJosef Bacik return 0; 5142226463d7SJosef Bacik } 5143226463d7SJosef Bacik 5144226463d7SJosef Bacik void __cold btrfs_ctree_exit(void) 5145226463d7SJosef Bacik { 5146226463d7SJosef Bacik kmem_cache_destroy(btrfs_path_cachep); 5147226463d7SJosef Bacik } 5148