1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007,2008 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/rbtree.h>
10 #include <linux/error-injection.h>
13 #include "transaction.h"
14 #include "print-tree.h"
18 #include "tree-mod-log.h"
20 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
21 *root, struct btrfs_path *path, int level);
22 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
23 const struct btrfs_key *ins_key, struct btrfs_path *path,
24 int data_size, int extend);
25 static int push_node_left(struct btrfs_trans_handle *trans,
26 struct extent_buffer *dst,
27 struct extent_buffer *src, int empty);
28 static int balance_node_right(struct btrfs_trans_handle *trans,
29 struct extent_buffer *dst_buf,
30 struct extent_buffer *src_buf);
31 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
34 static const struct btrfs_csums {
37 const char driver[12];
39 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
40 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
41 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
42 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
43 .driver = "blake2b-256" },
46 int btrfs_super_csum_size(const struct btrfs_super_block *s)
48 u16 t = btrfs_super_csum_type(s);
50 * csum type is validated at mount time
52 return btrfs_csums[t].size;
55 const char *btrfs_super_csum_name(u16 csum_type)
57 /* csum type is validated at mount time */
58 return btrfs_csums[csum_type].name;
62 * Return driver name if defined, otherwise the name that's also a valid driver
65 const char *btrfs_super_csum_driver(u16 csum_type)
67 /* csum type is validated at mount time */
68 return btrfs_csums[csum_type].driver[0] ?
69 btrfs_csums[csum_type].driver :
70 btrfs_csums[csum_type].name;
73 size_t __attribute_const__ btrfs_get_num_csums(void)
75 return ARRAY_SIZE(btrfs_csums);
78 struct btrfs_path *btrfs_alloc_path(void)
80 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
83 /* this also releases the path */
84 void btrfs_free_path(struct btrfs_path *p)
88 btrfs_release_path(p);
89 kmem_cache_free(btrfs_path_cachep, p);
93 * path release drops references on the extent buffers in the path
94 * and it drops any locks held by this path
96 * It is safe to call this on paths that no locks or extent buffers held.
98 noinline void btrfs_release_path(struct btrfs_path *p)
102 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
107 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
110 free_extent_buffer(p->nodes[i]);
116 * safely gets a reference on the root node of a tree. A lock
117 * is not taken, so a concurrent writer may put a different node
118 * at the root of the tree. See btrfs_lock_root_node for the
121 * The extent buffer returned by this has a reference taken, so
122 * it won't disappear. It may stop being the root of the tree
123 * at any time because there are no locks held.
125 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
127 struct extent_buffer *eb;
131 eb = rcu_dereference(root->node);
134 * RCU really hurts here, we could free up the root node because
135 * it was COWed but we may not get the new root node yet so do
136 * the inc_not_zero dance and if it doesn't work then
137 * synchronize_rcu and try again.
139 if (atomic_inc_not_zero(&eb->refs)) {
150 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
151 * just get put onto a simple dirty list. Transaction walks this list to make
152 * sure they get properly updated on disk.
154 static void add_root_to_dirty_list(struct btrfs_root *root)
156 struct btrfs_fs_info *fs_info = root->fs_info;
158 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
159 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
162 spin_lock(&fs_info->trans_lock);
163 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
164 /* Want the extent tree to be the last on the list */
165 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
166 list_move_tail(&root->dirty_list,
167 &fs_info->dirty_cowonly_roots);
169 list_move(&root->dirty_list,
170 &fs_info->dirty_cowonly_roots);
172 spin_unlock(&fs_info->trans_lock);
176 * used by snapshot creation to make a copy of a root for a tree with
177 * a given objectid. The buffer with the new root node is returned in
178 * cow_ret, and this func returns zero on success or a negative error code.
180 int btrfs_copy_root(struct btrfs_trans_handle *trans,
181 struct btrfs_root *root,
182 struct extent_buffer *buf,
183 struct extent_buffer **cow_ret, u64 new_root_objectid)
185 struct btrfs_fs_info *fs_info = root->fs_info;
186 struct extent_buffer *cow;
189 struct btrfs_disk_key disk_key;
191 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
192 trans->transid != fs_info->running_transaction->transid);
193 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
194 trans->transid != root->last_trans);
196 level = btrfs_header_level(buf);
198 btrfs_item_key(buf, &disk_key, 0);
200 btrfs_node_key(buf, &disk_key, 0);
202 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
203 &disk_key, level, buf->start, 0,
204 BTRFS_NESTING_NEW_ROOT);
208 copy_extent_buffer_full(cow, buf);
209 btrfs_set_header_bytenr(cow, cow->start);
210 btrfs_set_header_generation(cow, trans->transid);
211 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
212 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
213 BTRFS_HEADER_FLAG_RELOC);
214 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
215 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
217 btrfs_set_header_owner(cow, new_root_objectid);
219 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
221 WARN_ON(btrfs_header_generation(buf) > trans->transid);
222 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
223 ret = btrfs_inc_ref(trans, root, cow, 1);
225 ret = btrfs_inc_ref(trans, root, cow, 0);
227 btrfs_tree_unlock(cow);
228 free_extent_buffer(cow);
229 btrfs_abort_transaction(trans, ret);
233 btrfs_mark_buffer_dirty(cow);
239 * check if the tree block can be shared by multiple trees
241 int btrfs_block_can_be_shared(struct btrfs_root *root,
242 struct extent_buffer *buf)
245 * Tree blocks not in shareable trees and tree roots are never shared.
246 * If a block was allocated after the last snapshot and the block was
247 * not allocated by tree relocation, we know the block is not shared.
249 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
250 buf != root->node && buf != root->commit_root &&
251 (btrfs_header_generation(buf) <=
252 btrfs_root_last_snapshot(&root->root_item) ||
253 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
259 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root,
261 struct extent_buffer *buf,
262 struct extent_buffer *cow,
265 struct btrfs_fs_info *fs_info = root->fs_info;
273 * Backrefs update rules:
275 * Always use full backrefs for extent pointers in tree block
276 * allocated by tree relocation.
278 * If a shared tree block is no longer referenced by its owner
279 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
280 * use full backrefs for extent pointers in tree block.
282 * If a tree block is been relocating
283 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
284 * use full backrefs for extent pointers in tree block.
285 * The reason for this is some operations (such as drop tree)
286 * are only allowed for blocks use full backrefs.
289 if (btrfs_block_can_be_shared(root, buf)) {
290 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
291 btrfs_header_level(buf), 1,
297 btrfs_handle_fs_error(fs_info, ret, NULL);
302 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
303 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
304 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
309 owner = btrfs_header_owner(buf);
310 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
311 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
314 if ((owner == root->root_key.objectid ||
315 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
316 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
317 ret = btrfs_inc_ref(trans, root, buf, 1);
321 if (root->root_key.objectid ==
322 BTRFS_TREE_RELOC_OBJECTID) {
323 ret = btrfs_dec_ref(trans, root, buf, 0);
326 ret = btrfs_inc_ref(trans, root, cow, 1);
330 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
333 if (root->root_key.objectid ==
334 BTRFS_TREE_RELOC_OBJECTID)
335 ret = btrfs_inc_ref(trans, root, cow, 1);
337 ret = btrfs_inc_ref(trans, root, cow, 0);
341 if (new_flags != 0) {
342 int level = btrfs_header_level(buf);
344 ret = btrfs_set_disk_extent_flags(trans, buf,
345 new_flags, level, 0);
350 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
351 if (root->root_key.objectid ==
352 BTRFS_TREE_RELOC_OBJECTID)
353 ret = btrfs_inc_ref(trans, root, cow, 1);
355 ret = btrfs_inc_ref(trans, root, cow, 0);
358 ret = btrfs_dec_ref(trans, root, buf, 1);
362 btrfs_clean_tree_block(buf);
369 * does the dirty work in cow of a single block. The parent block (if
370 * supplied) is updated to point to the new cow copy. The new buffer is marked
371 * dirty and returned locked. If you modify the block it needs to be marked
374 * search_start -- an allocation hint for the new block
376 * empty_size -- a hint that you plan on doing more cow. This is the size in
377 * bytes the allocator should try to find free next to the block it returns.
378 * This is just a hint and may be ignored by the allocator.
380 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
381 struct btrfs_root *root,
382 struct extent_buffer *buf,
383 struct extent_buffer *parent, int parent_slot,
384 struct extent_buffer **cow_ret,
385 u64 search_start, u64 empty_size,
386 enum btrfs_lock_nesting nest)
388 struct btrfs_fs_info *fs_info = root->fs_info;
389 struct btrfs_disk_key disk_key;
390 struct extent_buffer *cow;
394 u64 parent_start = 0;
399 btrfs_assert_tree_write_locked(buf);
401 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
402 trans->transid != fs_info->running_transaction->transid);
403 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
404 trans->transid != root->last_trans);
406 level = btrfs_header_level(buf);
409 btrfs_item_key(buf, &disk_key, 0);
411 btrfs_node_key(buf, &disk_key, 0);
413 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
414 parent_start = parent->start;
416 cow = btrfs_alloc_tree_block(trans, root, parent_start,
417 root->root_key.objectid, &disk_key, level,
418 search_start, empty_size, nest);
422 /* cow is set to blocking by btrfs_init_new_buffer */
424 copy_extent_buffer_full(cow, buf);
425 btrfs_set_header_bytenr(cow, cow->start);
426 btrfs_set_header_generation(cow, trans->transid);
427 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
428 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
429 BTRFS_HEADER_FLAG_RELOC);
430 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
431 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
433 btrfs_set_header_owner(cow, root->root_key.objectid);
435 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
437 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
439 btrfs_tree_unlock(cow);
440 free_extent_buffer(cow);
441 btrfs_abort_transaction(trans, ret);
445 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
446 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
448 btrfs_tree_unlock(cow);
449 free_extent_buffer(cow);
450 btrfs_abort_transaction(trans, ret);
455 if (buf == root->node) {
456 WARN_ON(parent && parent != buf);
457 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
458 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
459 parent_start = buf->start;
461 atomic_inc(&cow->refs);
462 ret = btrfs_tree_mod_log_insert_root(root->node, cow, true);
464 rcu_assign_pointer(root->node, cow);
466 btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
467 parent_start, last_ref);
468 free_extent_buffer(buf);
469 add_root_to_dirty_list(root);
471 WARN_ON(trans->transid != btrfs_header_generation(parent));
472 btrfs_tree_mod_log_insert_key(parent, parent_slot,
473 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
474 btrfs_set_node_blockptr(parent, parent_slot,
476 btrfs_set_node_ptr_generation(parent, parent_slot,
478 btrfs_mark_buffer_dirty(parent);
480 ret = btrfs_tree_mod_log_free_eb(buf);
482 btrfs_tree_unlock(cow);
483 free_extent_buffer(cow);
484 btrfs_abort_transaction(trans, ret);
488 btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
489 parent_start, last_ref);
492 btrfs_tree_unlock(buf);
493 free_extent_buffer_stale(buf);
494 btrfs_mark_buffer_dirty(cow);
499 static inline int should_cow_block(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root,
501 struct extent_buffer *buf)
503 if (btrfs_is_testing(root->fs_info))
506 /* Ensure we can see the FORCE_COW bit */
507 smp_mb__before_atomic();
510 * We do not need to cow a block if
511 * 1) this block is not created or changed in this transaction;
512 * 2) this block does not belong to TREE_RELOC tree;
513 * 3) the root is not forced COW.
515 * What is forced COW:
516 * when we create snapshot during committing the transaction,
517 * after we've finished copying src root, we must COW the shared
518 * block to ensure the metadata consistency.
520 if (btrfs_header_generation(buf) == trans->transid &&
521 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
522 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
523 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
524 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
530 * cows a single block, see __btrfs_cow_block for the real work.
531 * This version of it has extra checks so that a block isn't COWed more than
532 * once per transaction, as long as it hasn't been written yet
534 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
535 struct btrfs_root *root, struct extent_buffer *buf,
536 struct extent_buffer *parent, int parent_slot,
537 struct extent_buffer **cow_ret,
538 enum btrfs_lock_nesting nest)
540 struct btrfs_fs_info *fs_info = root->fs_info;
544 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
546 "COW'ing blocks on a fs root that's being dropped");
548 if (trans->transaction != fs_info->running_transaction)
549 WARN(1, KERN_CRIT "trans %llu running %llu\n",
551 fs_info->running_transaction->transid);
553 if (trans->transid != fs_info->generation)
554 WARN(1, KERN_CRIT "trans %llu running %llu\n",
555 trans->transid, fs_info->generation);
557 if (!should_cow_block(trans, root, buf)) {
562 search_start = buf->start & ~((u64)SZ_1G - 1);
565 * Before CoWing this block for later modification, check if it's
566 * the subtree root and do the delayed subtree trace if needed.
568 * Also We don't care about the error, as it's handled internally.
570 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
571 ret = __btrfs_cow_block(trans, root, buf, parent,
572 parent_slot, cow_ret, search_start, 0, nest);
574 trace_btrfs_cow_block(root, buf, *cow_ret);
578 ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
581 * helper function for defrag to decide if two blocks pointed to by a
582 * node are actually close by
584 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
586 if (blocknr < other && other - (blocknr + blocksize) < 32768)
588 if (blocknr > other && blocknr - (other + blocksize) < 32768)
593 #ifdef __LITTLE_ENDIAN
596 * Compare two keys, on little-endian the disk order is same as CPU order and
597 * we can avoid the conversion.
599 static int comp_keys(const struct btrfs_disk_key *disk_key,
600 const struct btrfs_key *k2)
602 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
604 return btrfs_comp_cpu_keys(k1, k2);
610 * compare two keys in a memcmp fashion
612 static int comp_keys(const struct btrfs_disk_key *disk,
613 const struct btrfs_key *k2)
617 btrfs_disk_key_to_cpu(&k1, disk);
619 return btrfs_comp_cpu_keys(&k1, k2);
624 * same as comp_keys only with two btrfs_key's
626 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
628 if (k1->objectid > k2->objectid)
630 if (k1->objectid < k2->objectid)
632 if (k1->type > k2->type)
634 if (k1->type < k2->type)
636 if (k1->offset > k2->offset)
638 if (k1->offset < k2->offset)
644 * this is used by the defrag code to go through all the
645 * leaves pointed to by a node and reallocate them so that
646 * disk order is close to key order
648 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
649 struct btrfs_root *root, struct extent_buffer *parent,
650 int start_slot, u64 *last_ret,
651 struct btrfs_key *progress)
653 struct btrfs_fs_info *fs_info = root->fs_info;
654 struct extent_buffer *cur;
656 u64 search_start = *last_ret;
664 int progress_passed = 0;
665 struct btrfs_disk_key disk_key;
667 WARN_ON(trans->transaction != fs_info->running_transaction);
668 WARN_ON(trans->transid != fs_info->generation);
670 parent_nritems = btrfs_header_nritems(parent);
671 blocksize = fs_info->nodesize;
672 end_slot = parent_nritems - 1;
674 if (parent_nritems <= 1)
677 for (i = start_slot; i <= end_slot; i++) {
680 btrfs_node_key(parent, &disk_key, i);
681 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
685 blocknr = btrfs_node_blockptr(parent, i);
687 last_block = blocknr;
690 other = btrfs_node_blockptr(parent, i - 1);
691 close = close_blocks(blocknr, other, blocksize);
693 if (!close && i < end_slot) {
694 other = btrfs_node_blockptr(parent, i + 1);
695 close = close_blocks(blocknr, other, blocksize);
698 last_block = blocknr;
702 cur = btrfs_read_node_slot(parent, i);
705 if (search_start == 0)
706 search_start = last_block;
708 btrfs_tree_lock(cur);
709 err = __btrfs_cow_block(trans, root, cur, parent, i,
712 (end_slot - i) * blocksize),
715 btrfs_tree_unlock(cur);
716 free_extent_buffer(cur);
719 search_start = cur->start;
720 last_block = cur->start;
721 *last_ret = search_start;
722 btrfs_tree_unlock(cur);
723 free_extent_buffer(cur);
729 * Search for a key in the given extent_buffer.
731 * The lower boundary for the search is specified by the slot number @low. Use a
732 * value of 0 to search over the whole extent buffer.
734 * The slot in the extent buffer is returned via @slot. If the key exists in the
735 * extent buffer, then @slot will point to the slot where the key is, otherwise
736 * it points to the slot where you would insert the key.
738 * Slot may point to the total number of items (i.e. one position beyond the last
739 * key) if the key is bigger than the last key in the extent buffer.
741 static noinline int generic_bin_search(struct extent_buffer *eb, int low,
742 const struct btrfs_key *key, int *slot)
746 int high = btrfs_header_nritems(eb);
748 const int key_size = sizeof(struct btrfs_disk_key);
751 btrfs_err(eb->fs_info,
752 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
753 __func__, low, high, eb->start,
754 btrfs_header_owner(eb), btrfs_header_level(eb));
758 if (btrfs_header_level(eb) == 0) {
759 p = offsetof(struct btrfs_leaf, items);
760 item_size = sizeof(struct btrfs_item);
762 p = offsetof(struct btrfs_node, ptrs);
763 item_size = sizeof(struct btrfs_key_ptr);
768 unsigned long offset;
769 struct btrfs_disk_key *tmp;
770 struct btrfs_disk_key unaligned;
773 mid = (low + high) / 2;
774 offset = p + mid * item_size;
775 oip = offset_in_page(offset);
777 if (oip + key_size <= PAGE_SIZE) {
778 const unsigned long idx = get_eb_page_index(offset);
779 char *kaddr = page_address(eb->pages[idx]);
781 oip = get_eb_offset_in_page(eb, offset);
782 tmp = (struct btrfs_disk_key *)(kaddr + oip);
784 read_extent_buffer(eb, &unaligned, offset, key_size);
788 ret = comp_keys(tmp, key);
804 * Simple binary search on an extent buffer. Works for both leaves and nodes, and
805 * always searches over the whole range of keys (slot 0 to slot 'nritems - 1').
807 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
810 return generic_bin_search(eb, 0, key, slot);
813 static void root_add_used(struct btrfs_root *root, u32 size)
815 spin_lock(&root->accounting_lock);
816 btrfs_set_root_used(&root->root_item,
817 btrfs_root_used(&root->root_item) + size);
818 spin_unlock(&root->accounting_lock);
821 static void root_sub_used(struct btrfs_root *root, u32 size)
823 spin_lock(&root->accounting_lock);
824 btrfs_set_root_used(&root->root_item,
825 btrfs_root_used(&root->root_item) - size);
826 spin_unlock(&root->accounting_lock);
829 /* given a node and slot number, this reads the blocks it points to. The
830 * extent buffer is returned with a reference taken (but unlocked).
832 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
835 int level = btrfs_header_level(parent);
836 struct extent_buffer *eb;
837 struct btrfs_key first_key;
839 if (slot < 0 || slot >= btrfs_header_nritems(parent))
840 return ERR_PTR(-ENOENT);
844 btrfs_node_key_to_cpu(parent, &first_key, slot);
845 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
846 btrfs_header_owner(parent),
847 btrfs_node_ptr_generation(parent, slot),
848 level - 1, &first_key);
849 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
850 free_extent_buffer(eb);
858 * node level balancing, used to make sure nodes are in proper order for
859 * item deletion. We balance from the top down, so we have to make sure
860 * that a deletion won't leave an node completely empty later on.
862 static noinline int balance_level(struct btrfs_trans_handle *trans,
863 struct btrfs_root *root,
864 struct btrfs_path *path, int level)
866 struct btrfs_fs_info *fs_info = root->fs_info;
867 struct extent_buffer *right = NULL;
868 struct extent_buffer *mid;
869 struct extent_buffer *left = NULL;
870 struct extent_buffer *parent = NULL;
874 int orig_slot = path->slots[level];
879 mid = path->nodes[level];
881 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK);
882 WARN_ON(btrfs_header_generation(mid) != trans->transid);
884 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
886 if (level < BTRFS_MAX_LEVEL - 1) {
887 parent = path->nodes[level + 1];
888 pslot = path->slots[level + 1];
892 * deal with the case where there is only one pointer in the root
893 * by promoting the node below to a root
896 struct extent_buffer *child;
898 if (btrfs_header_nritems(mid) != 1)
901 /* promote the child to a root */
902 child = btrfs_read_node_slot(mid, 0);
904 ret = PTR_ERR(child);
905 btrfs_handle_fs_error(fs_info, ret, NULL);
909 btrfs_tree_lock(child);
910 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
913 btrfs_tree_unlock(child);
914 free_extent_buffer(child);
918 ret = btrfs_tree_mod_log_insert_root(root->node, child, true);
920 rcu_assign_pointer(root->node, child);
922 add_root_to_dirty_list(root);
923 btrfs_tree_unlock(child);
925 path->locks[level] = 0;
926 path->nodes[level] = NULL;
927 btrfs_clean_tree_block(mid);
928 btrfs_tree_unlock(mid);
929 /* once for the path */
930 free_extent_buffer(mid);
932 root_sub_used(root, mid->len);
933 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
934 /* once for the root ptr */
935 free_extent_buffer_stale(mid);
938 if (btrfs_header_nritems(mid) >
939 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
942 left = btrfs_read_node_slot(parent, pslot - 1);
947 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
948 wret = btrfs_cow_block(trans, root, left,
949 parent, pslot - 1, &left,
950 BTRFS_NESTING_LEFT_COW);
957 right = btrfs_read_node_slot(parent, pslot + 1);
962 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
963 wret = btrfs_cow_block(trans, root, right,
964 parent, pslot + 1, &right,
965 BTRFS_NESTING_RIGHT_COW);
972 /* first, try to make some room in the middle buffer */
974 orig_slot += btrfs_header_nritems(left);
975 wret = push_node_left(trans, left, mid, 1);
981 * then try to empty the right most buffer into the middle
984 wret = push_node_left(trans, mid, right, 1);
985 if (wret < 0 && wret != -ENOSPC)
987 if (btrfs_header_nritems(right) == 0) {
988 btrfs_clean_tree_block(right);
989 btrfs_tree_unlock(right);
990 del_ptr(root, path, level + 1, pslot + 1);
991 root_sub_used(root, right->len);
992 btrfs_free_tree_block(trans, btrfs_root_id(root), right,
994 free_extent_buffer_stale(right);
997 struct btrfs_disk_key right_key;
998 btrfs_node_key(right, &right_key, 0);
999 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1000 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1002 btrfs_set_node_key(parent, &right_key, pslot + 1);
1003 btrfs_mark_buffer_dirty(parent);
1006 if (btrfs_header_nritems(mid) == 1) {
1008 * we're not allowed to leave a node with one item in the
1009 * tree during a delete. A deletion from lower in the tree
1010 * could try to delete the only pointer in this node.
1011 * So, pull some keys from the left.
1012 * There has to be a left pointer at this point because
1013 * otherwise we would have pulled some pointers from the
1018 btrfs_handle_fs_error(fs_info, ret, NULL);
1021 wret = balance_node_right(trans, mid, left);
1027 wret = push_node_left(trans, left, mid, 1);
1033 if (btrfs_header_nritems(mid) == 0) {
1034 btrfs_clean_tree_block(mid);
1035 btrfs_tree_unlock(mid);
1036 del_ptr(root, path, level + 1, pslot);
1037 root_sub_used(root, mid->len);
1038 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1039 free_extent_buffer_stale(mid);
1042 /* update the parent key to reflect our changes */
1043 struct btrfs_disk_key mid_key;
1044 btrfs_node_key(mid, &mid_key, 0);
1045 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1046 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1048 btrfs_set_node_key(parent, &mid_key, pslot);
1049 btrfs_mark_buffer_dirty(parent);
1052 /* update the path */
1054 if (btrfs_header_nritems(left) > orig_slot) {
1055 atomic_inc(&left->refs);
1056 /* left was locked after cow */
1057 path->nodes[level] = left;
1058 path->slots[level + 1] -= 1;
1059 path->slots[level] = orig_slot;
1061 btrfs_tree_unlock(mid);
1062 free_extent_buffer(mid);
1065 orig_slot -= btrfs_header_nritems(left);
1066 path->slots[level] = orig_slot;
1069 /* double check we haven't messed things up */
1071 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1075 btrfs_tree_unlock(right);
1076 free_extent_buffer(right);
1079 if (path->nodes[level] != left)
1080 btrfs_tree_unlock(left);
1081 free_extent_buffer(left);
1086 /* Node balancing for insertion. Here we only split or push nodes around
1087 * when they are completely full. This is also done top down, so we
1088 * have to be pessimistic.
1090 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1091 struct btrfs_root *root,
1092 struct btrfs_path *path, int level)
1094 struct btrfs_fs_info *fs_info = root->fs_info;
1095 struct extent_buffer *right = NULL;
1096 struct extent_buffer *mid;
1097 struct extent_buffer *left = NULL;
1098 struct extent_buffer *parent = NULL;
1102 int orig_slot = path->slots[level];
1107 mid = path->nodes[level];
1108 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1110 if (level < BTRFS_MAX_LEVEL - 1) {
1111 parent = path->nodes[level + 1];
1112 pslot = path->slots[level + 1];
1118 left = btrfs_read_node_slot(parent, pslot - 1);
1122 /* first, try to make some room in the middle buffer */
1126 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
1128 left_nr = btrfs_header_nritems(left);
1129 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1132 ret = btrfs_cow_block(trans, root, left, parent,
1134 BTRFS_NESTING_LEFT_COW);
1138 wret = push_node_left(trans, left, mid, 0);
1144 struct btrfs_disk_key disk_key;
1145 orig_slot += left_nr;
1146 btrfs_node_key(mid, &disk_key, 0);
1147 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1148 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1150 btrfs_set_node_key(parent, &disk_key, pslot);
1151 btrfs_mark_buffer_dirty(parent);
1152 if (btrfs_header_nritems(left) > orig_slot) {
1153 path->nodes[level] = left;
1154 path->slots[level + 1] -= 1;
1155 path->slots[level] = orig_slot;
1156 btrfs_tree_unlock(mid);
1157 free_extent_buffer(mid);
1160 btrfs_header_nritems(left);
1161 path->slots[level] = orig_slot;
1162 btrfs_tree_unlock(left);
1163 free_extent_buffer(left);
1167 btrfs_tree_unlock(left);
1168 free_extent_buffer(left);
1170 right = btrfs_read_node_slot(parent, pslot + 1);
1175 * then try to empty the right most buffer into the middle
1180 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1182 right_nr = btrfs_header_nritems(right);
1183 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1186 ret = btrfs_cow_block(trans, root, right,
1188 &right, BTRFS_NESTING_RIGHT_COW);
1192 wret = balance_node_right(trans, right, mid);
1198 struct btrfs_disk_key disk_key;
1200 btrfs_node_key(right, &disk_key, 0);
1201 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1202 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1204 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1205 btrfs_mark_buffer_dirty(parent);
1207 if (btrfs_header_nritems(mid) <= orig_slot) {
1208 path->nodes[level] = right;
1209 path->slots[level + 1] += 1;
1210 path->slots[level] = orig_slot -
1211 btrfs_header_nritems(mid);
1212 btrfs_tree_unlock(mid);
1213 free_extent_buffer(mid);
1215 btrfs_tree_unlock(right);
1216 free_extent_buffer(right);
1220 btrfs_tree_unlock(right);
1221 free_extent_buffer(right);
1227 * readahead one full node of leaves, finding things that are close
1228 * to the block in 'slot', and triggering ra on them.
1230 static void reada_for_search(struct btrfs_fs_info *fs_info,
1231 struct btrfs_path *path,
1232 int level, int slot, u64 objectid)
1234 struct extent_buffer *node;
1235 struct btrfs_disk_key disk_key;
1245 if (level != 1 && path->reada != READA_FORWARD_ALWAYS)
1248 if (!path->nodes[level])
1251 node = path->nodes[level];
1254 * Since the time between visiting leaves is much shorter than the time
1255 * between visiting nodes, limit read ahead of nodes to 1, to avoid too
1256 * much IO at once (possibly random).
1258 if (path->reada == READA_FORWARD_ALWAYS) {
1260 nread_max = node->fs_info->nodesize;
1262 nread_max = SZ_128K;
1267 search = btrfs_node_blockptr(node, slot);
1268 blocksize = fs_info->nodesize;
1269 if (path->reada != READA_FORWARD_ALWAYS) {
1270 struct extent_buffer *eb;
1272 eb = find_extent_buffer(fs_info, search);
1274 free_extent_buffer(eb);
1281 nritems = btrfs_header_nritems(node);
1285 if (path->reada == READA_BACK) {
1289 } else if (path->reada == READA_FORWARD ||
1290 path->reada == READA_FORWARD_ALWAYS) {
1295 if (path->reada == READA_BACK && objectid) {
1296 btrfs_node_key(node, &disk_key, nr);
1297 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1300 search = btrfs_node_blockptr(node, nr);
1301 if (path->reada == READA_FORWARD_ALWAYS ||
1302 (search <= target && target - search <= 65536) ||
1303 (search > target && search - target <= 65536)) {
1304 btrfs_readahead_node_child(node, nr);
1308 if (nread > nread_max || nscan > 32)
1313 static noinline void reada_for_balance(struct btrfs_path *path, int level)
1315 struct extent_buffer *parent;
1319 parent = path->nodes[level + 1];
1323 nritems = btrfs_header_nritems(parent);
1324 slot = path->slots[level + 1];
1327 btrfs_readahead_node_child(parent, slot - 1);
1328 if (slot + 1 < nritems)
1329 btrfs_readahead_node_child(parent, slot + 1);
1334 * when we walk down the tree, it is usually safe to unlock the higher layers
1335 * in the tree. The exceptions are when our path goes through slot 0, because
1336 * operations on the tree might require changing key pointers higher up in the
1339 * callers might also have set path->keep_locks, which tells this code to keep
1340 * the lock if the path points to the last slot in the block. This is part of
1341 * walking through the tree, and selecting the next slot in the higher block.
1343 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1344 * if lowest_unlock is 1, level 0 won't be unlocked
1346 static noinline void unlock_up(struct btrfs_path *path, int level,
1347 int lowest_unlock, int min_write_lock_level,
1348 int *write_lock_level)
1351 int skip_level = level;
1352 bool check_skip = true;
1354 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1355 if (!path->nodes[i])
1357 if (!path->locks[i])
1361 if (path->slots[i] == 0) {
1366 if (path->keep_locks) {
1369 nritems = btrfs_header_nritems(path->nodes[i]);
1370 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1377 if (i >= lowest_unlock && i > skip_level) {
1379 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1381 if (write_lock_level &&
1382 i > min_write_lock_level &&
1383 i <= *write_lock_level) {
1384 *write_lock_level = i - 1;
1391 * helper function for btrfs_search_slot. The goal is to find a block
1392 * in cache without setting the path to blocking. If we find the block
1393 * we return zero and the path is unchanged.
1395 * If we can't find the block, we set the path blocking and do some
1396 * reada. -EAGAIN is returned and the search must be repeated.
1399 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
1400 struct extent_buffer **eb_ret, int level, int slot,
1401 const struct btrfs_key *key)
1403 struct btrfs_fs_info *fs_info = root->fs_info;
1406 struct extent_buffer *tmp;
1407 struct btrfs_key first_key;
1411 blocknr = btrfs_node_blockptr(*eb_ret, slot);
1412 gen = btrfs_node_ptr_generation(*eb_ret, slot);
1413 parent_level = btrfs_header_level(*eb_ret);
1414 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
1416 tmp = find_extent_buffer(fs_info, blocknr);
1418 if (p->reada == READA_FORWARD_ALWAYS)
1419 reada_for_search(fs_info, p, level, slot, key->objectid);
1421 /* first we do an atomic uptodate check */
1422 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
1424 * Do extra check for first_key, eb can be stale due to
1425 * being cached, read from scrub, or have multiple
1426 * parents (shared tree blocks).
1428 if (btrfs_verify_level_key(tmp,
1429 parent_level - 1, &first_key, gen)) {
1430 free_extent_buffer(tmp);
1437 /* now we're allowed to do a blocking uptodate check */
1438 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
1443 free_extent_buffer(tmp);
1444 btrfs_release_path(p);
1449 * reduce lock contention at high levels
1450 * of the btree by dropping locks before
1451 * we read. Don't release the lock on the current
1452 * level because we need to walk this node to figure
1453 * out which blocks to read.
1455 btrfs_unlock_up_safe(p, level + 1);
1457 if (p->reada != READA_NONE)
1458 reada_for_search(fs_info, p, level, slot, key->objectid);
1461 tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid,
1462 gen, parent_level - 1, &first_key);
1465 * If the read above didn't mark this buffer up to date,
1466 * it will never end up being up to date. Set ret to EIO now
1467 * and give up so that our caller doesn't loop forever
1470 if (!extent_buffer_uptodate(tmp))
1472 free_extent_buffer(tmp);
1477 btrfs_release_path(p);
1482 * helper function for btrfs_search_slot. This does all of the checks
1483 * for node-level blocks and does any balancing required based on
1486 * If no extra work was required, zero is returned. If we had to
1487 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1491 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1492 struct btrfs_root *root, struct btrfs_path *p,
1493 struct extent_buffer *b, int level, int ins_len,
1494 int *write_lock_level)
1496 struct btrfs_fs_info *fs_info = root->fs_info;
1499 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1500 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1502 if (*write_lock_level < level + 1) {
1503 *write_lock_level = level + 1;
1504 btrfs_release_path(p);
1508 reada_for_balance(p, level);
1509 ret = split_node(trans, root, p, level);
1511 b = p->nodes[level];
1512 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1513 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
1515 if (*write_lock_level < level + 1) {
1516 *write_lock_level = level + 1;
1517 btrfs_release_path(p);
1521 reada_for_balance(p, level);
1522 ret = balance_level(trans, root, p, level);
1526 b = p->nodes[level];
1528 btrfs_release_path(p);
1531 BUG_ON(btrfs_header_nritems(b) == 1);
1536 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
1537 u64 iobjectid, u64 ioff, u8 key_type,
1538 struct btrfs_key *found_key)
1541 struct btrfs_key key;
1542 struct extent_buffer *eb;
1547 key.type = key_type;
1548 key.objectid = iobjectid;
1551 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1555 eb = path->nodes[0];
1556 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1557 ret = btrfs_next_leaf(fs_root, path);
1560 eb = path->nodes[0];
1563 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1564 if (found_key->type != key.type ||
1565 found_key->objectid != key.objectid)
1571 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
1572 struct btrfs_path *p,
1573 int write_lock_level)
1575 struct extent_buffer *b;
1579 if (p->search_commit_root) {
1580 b = root->commit_root;
1581 atomic_inc(&b->refs);
1582 level = btrfs_header_level(b);
1584 * Ensure that all callers have set skip_locking when
1585 * p->search_commit_root = 1.
1587 ASSERT(p->skip_locking == 1);
1592 if (p->skip_locking) {
1593 b = btrfs_root_node(root);
1594 level = btrfs_header_level(b);
1598 /* We try very hard to do read locks on the root */
1599 root_lock = BTRFS_READ_LOCK;
1602 * If the level is set to maximum, we can skip trying to get the read
1605 if (write_lock_level < BTRFS_MAX_LEVEL) {
1607 * We don't know the level of the root node until we actually
1608 * have it read locked
1610 b = btrfs_read_lock_root_node(root);
1611 level = btrfs_header_level(b);
1612 if (level > write_lock_level)
1615 /* Whoops, must trade for write lock */
1616 btrfs_tree_read_unlock(b);
1617 free_extent_buffer(b);
1620 b = btrfs_lock_root_node(root);
1621 root_lock = BTRFS_WRITE_LOCK;
1623 /* The level might have changed, check again */
1624 level = btrfs_header_level(b);
1628 * The root may have failed to write out at some point, and thus is no
1629 * longer valid, return an error in this case.
1631 if (!extent_buffer_uptodate(b)) {
1633 btrfs_tree_unlock_rw(b, root_lock);
1634 free_extent_buffer(b);
1635 return ERR_PTR(-EIO);
1638 p->nodes[level] = b;
1639 if (!p->skip_locking)
1640 p->locks[level] = root_lock;
1642 * Callers are responsible for dropping b's references.
1648 * Replace the extent buffer at the lowest level of the path with a cloned
1649 * version. The purpose is to be able to use it safely, after releasing the
1650 * commit root semaphore, even if relocation is happening in parallel, the
1651 * transaction used for relocation is committed and the extent buffer is
1652 * reallocated in the next transaction.
1654 * This is used in a context where the caller does not prevent transaction
1655 * commits from happening, either by holding a transaction handle or holding
1656 * some lock, while it's doing searches through a commit root.
1657 * At the moment it's only used for send operations.
1659 static int finish_need_commit_sem_search(struct btrfs_path *path)
1661 const int i = path->lowest_level;
1662 const int slot = path->slots[i];
1663 struct extent_buffer *lowest = path->nodes[i];
1664 struct extent_buffer *clone;
1666 ASSERT(path->need_commit_sem);
1671 lockdep_assert_held_read(&lowest->fs_info->commit_root_sem);
1673 clone = btrfs_clone_extent_buffer(lowest);
1677 btrfs_release_path(path);
1678 path->nodes[i] = clone;
1679 path->slots[i] = slot;
1684 static inline int search_for_key_slot(struct extent_buffer *eb,
1685 int search_low_slot,
1686 const struct btrfs_key *key,
1691 * If a previous call to btrfs_bin_search() on a parent node returned an
1692 * exact match (prev_cmp == 0), we can safely assume the target key will
1693 * always be at slot 0 on lower levels, since each key pointer
1694 * (struct btrfs_key_ptr) refers to the lowest key accessible from the
1695 * subtree it points to. Thus we can skip searching lower levels.
1697 if (prev_cmp == 0) {
1702 return generic_bin_search(eb, search_low_slot, key, slot);
1705 static int search_leaf(struct btrfs_trans_handle *trans,
1706 struct btrfs_root *root,
1707 const struct btrfs_key *key,
1708 struct btrfs_path *path,
1712 struct extent_buffer *leaf = path->nodes[0];
1713 int leaf_free_space = -1;
1714 int search_low_slot = 0;
1716 bool do_bin_search = true;
1719 * If we are doing an insertion, the leaf has enough free space and the
1720 * destination slot for the key is not slot 0, then we can unlock our
1721 * write lock on the parent, and any other upper nodes, before doing the
1722 * binary search on the leaf (with search_for_key_slot()), allowing other
1723 * tasks to lock the parent and any other upper nodes.
1727 * Cache the leaf free space, since we will need it later and it
1728 * will not change until then.
1730 leaf_free_space = btrfs_leaf_free_space(leaf);
1733 * !path->locks[1] means we have a single node tree, the leaf is
1734 * the root of the tree.
1736 if (path->locks[1] && leaf_free_space >= ins_len) {
1737 struct btrfs_disk_key first_key;
1739 ASSERT(btrfs_header_nritems(leaf) > 0);
1740 btrfs_item_key(leaf, &first_key, 0);
1743 * Doing the extra comparison with the first key is cheap,
1744 * taking into account that the first key is very likely
1745 * already in a cache line because it immediately follows
1746 * the extent buffer's header and we have recently accessed
1747 * the header's level field.
1749 ret = comp_keys(&first_key, key);
1752 * The first key is smaller than the key we want
1753 * to insert, so we are safe to unlock all upper
1754 * nodes and we have to do the binary search.
1756 * We do use btrfs_unlock_up_safe() and not
1757 * unlock_up() because the later does not unlock
1758 * nodes with a slot of 0 - we can safely unlock
1759 * any node even if its slot is 0 since in this
1760 * case the key does not end up at slot 0 of the
1761 * leaf and there's no need to split the leaf.
1763 btrfs_unlock_up_safe(path, 1);
1764 search_low_slot = 1;
1767 * The first key is >= then the key we want to
1768 * insert, so we can skip the binary search as
1769 * the target key will be at slot 0.
1771 * We can not unlock upper nodes when the key is
1772 * less than the first key, because we will need
1773 * to update the key at slot 0 of the parent node
1774 * and possibly of other upper nodes too.
1775 * If the key matches the first key, then we can
1776 * unlock all the upper nodes, using
1777 * btrfs_unlock_up_safe() instead of unlock_up()
1781 btrfs_unlock_up_safe(path, 1);
1783 * ret is already 0 or 1, matching the result of
1784 * a btrfs_bin_search() call, so there is no need
1787 do_bin_search = false;
1793 if (do_bin_search) {
1794 ret = search_for_key_slot(leaf, search_low_slot, key,
1795 prev_cmp, &path->slots[0]);
1802 * Item key already exists. In this case, if we are allowed to
1803 * insert the item (for example, in dir_item case, item key
1804 * collision is allowed), it will be merged with the original
1805 * item. Only the item size grows, no new btrfs item will be
1806 * added. If search_for_extension is not set, ins_len already
1807 * accounts the size btrfs_item, deduct it here so leaf space
1808 * check will be correct.
1810 if (ret == 0 && !path->search_for_extension) {
1811 ASSERT(ins_len >= sizeof(struct btrfs_item));
1812 ins_len -= sizeof(struct btrfs_item);
1815 ASSERT(leaf_free_space >= 0);
1817 if (leaf_free_space < ins_len) {
1820 err = split_leaf(trans, root, key, path, ins_len,
1823 if (WARN_ON(err > 0))
1834 * btrfs_search_slot - look for a key in a tree and perform necessary
1835 * modifications to preserve tree invariants.
1837 * @trans: Handle of transaction, used when modifying the tree
1838 * @p: Holds all btree nodes along the search path
1839 * @root: The root node of the tree
1840 * @key: The key we are looking for
1841 * @ins_len: Indicates purpose of search:
1842 * >0 for inserts it's size of item inserted (*)
1844 * 0 for plain searches, not modifying the tree
1846 * (*) If size of item inserted doesn't include
1847 * sizeof(struct btrfs_item), then p->search_for_extension must
1849 * @cow: boolean should CoW operations be performed. Must always be 1
1850 * when modifying the tree.
1852 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
1853 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
1855 * If @key is found, 0 is returned and you can find the item in the leaf level
1856 * of the path (level 0)
1858 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
1859 * points to the slot where it should be inserted
1861 * If an error is encountered while searching the tree a negative error number
1864 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1865 const struct btrfs_key *key, struct btrfs_path *p,
1866 int ins_len, int cow)
1868 struct btrfs_fs_info *fs_info = root->fs_info;
1869 struct extent_buffer *b;
1874 int lowest_unlock = 1;
1875 /* everything at write_lock_level or lower must be write locked */
1876 int write_lock_level = 0;
1877 u8 lowest_level = 0;
1878 int min_write_lock_level;
1881 lowest_level = p->lowest_level;
1882 WARN_ON(lowest_level && ins_len > 0);
1883 WARN_ON(p->nodes[0] != NULL);
1884 BUG_ON(!cow && ins_len);
1889 /* when we are removing items, we might have to go up to level
1890 * two as we update tree pointers Make sure we keep write
1891 * for those levels as well
1893 write_lock_level = 2;
1894 } else if (ins_len > 0) {
1896 * for inserting items, make sure we have a write lock on
1897 * level 1 so we can update keys
1899 write_lock_level = 1;
1903 write_lock_level = -1;
1905 if (cow && (p->keep_locks || p->lowest_level))
1906 write_lock_level = BTRFS_MAX_LEVEL;
1908 min_write_lock_level = write_lock_level;
1910 if (p->need_commit_sem) {
1911 ASSERT(p->search_commit_root);
1912 down_read(&fs_info->commit_root_sem);
1917 b = btrfs_search_slot_get_root(root, p, write_lock_level);
1926 level = btrfs_header_level(b);
1929 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
1932 * if we don't really need to cow this block
1933 * then we don't want to set the path blocking,
1934 * so we test it here
1936 if (!should_cow_block(trans, root, b))
1940 * must have write locks on this node and the
1943 if (level > write_lock_level ||
1944 (level + 1 > write_lock_level &&
1945 level + 1 < BTRFS_MAX_LEVEL &&
1946 p->nodes[level + 1])) {
1947 write_lock_level = level + 1;
1948 btrfs_release_path(p);
1953 err = btrfs_cow_block(trans, root, b, NULL, 0,
1957 err = btrfs_cow_block(trans, root, b,
1958 p->nodes[level + 1],
1959 p->slots[level + 1], &b,
1967 p->nodes[level] = b;
1970 * we have a lock on b and as long as we aren't changing
1971 * the tree, there is no way to for the items in b to change.
1972 * It is safe to drop the lock on our parent before we
1973 * go through the expensive btree search on b.
1975 * If we're inserting or deleting (ins_len != 0), then we might
1976 * be changing slot zero, which may require changing the parent.
1977 * So, we can't drop the lock until after we know which slot
1978 * we're operating on.
1980 if (!ins_len && !p->keep_locks) {
1983 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
1984 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
1991 ASSERT(write_lock_level >= 1);
1993 ret = search_leaf(trans, root, key, p, ins_len, prev_cmp);
1994 if (!p->search_for_split)
1995 unlock_up(p, level, lowest_unlock,
1996 min_write_lock_level, NULL);
2000 ret = search_for_key_slot(b, 0, key, prev_cmp, &slot);
2005 if (ret && slot > 0) {
2009 p->slots[level] = slot;
2010 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2018 b = p->nodes[level];
2019 slot = p->slots[level];
2022 * Slot 0 is special, if we change the key we have to update
2023 * the parent pointer which means we must have a write lock on
2026 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2027 write_lock_level = level + 1;
2028 btrfs_release_path(p);
2032 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2035 if (level == lowest_level) {
2041 err = read_block_for_search(root, p, &b, level, slot, key);
2049 if (!p->skip_locking) {
2050 level = btrfs_header_level(b);
2051 if (level <= write_lock_level) {
2053 p->locks[level] = BTRFS_WRITE_LOCK;
2055 btrfs_tree_read_lock(b);
2056 p->locks[level] = BTRFS_READ_LOCK;
2058 p->nodes[level] = b;
2063 if (ret < 0 && !p->skip_release_on_error)
2064 btrfs_release_path(p);
2066 if (p->need_commit_sem) {
2069 ret2 = finish_need_commit_sem_search(p);
2070 up_read(&fs_info->commit_root_sem);
2077 ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
2080 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2081 * current state of the tree together with the operations recorded in the tree
2082 * modification log to search for the key in a previous version of this tree, as
2083 * denoted by the time_seq parameter.
2085 * Naturally, there is no support for insert, delete or cow operations.
2087 * The resulting path and return value will be set up as if we called
2088 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2090 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2091 struct btrfs_path *p, u64 time_seq)
2093 struct btrfs_fs_info *fs_info = root->fs_info;
2094 struct extent_buffer *b;
2099 int lowest_unlock = 1;
2100 u8 lowest_level = 0;
2102 lowest_level = p->lowest_level;
2103 WARN_ON(p->nodes[0] != NULL);
2105 if (p->search_commit_root) {
2107 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2111 b = btrfs_get_old_root(root, time_seq);
2116 level = btrfs_header_level(b);
2117 p->locks[level] = BTRFS_READ_LOCK;
2122 level = btrfs_header_level(b);
2123 p->nodes[level] = b;
2126 * we have a lock on b and as long as we aren't changing
2127 * the tree, there is no way to for the items in b to change.
2128 * It is safe to drop the lock on our parent before we
2129 * go through the expensive btree search on b.
2131 btrfs_unlock_up_safe(p, level + 1);
2133 ret = btrfs_bin_search(b, key, &slot);
2138 p->slots[level] = slot;
2139 unlock_up(p, level, lowest_unlock, 0, NULL);
2143 if (ret && slot > 0) {
2147 p->slots[level] = slot;
2148 unlock_up(p, level, lowest_unlock, 0, NULL);
2150 if (level == lowest_level) {
2156 err = read_block_for_search(root, p, &b, level, slot, key);
2164 level = btrfs_header_level(b);
2165 btrfs_tree_read_lock(b);
2166 b = btrfs_tree_mod_log_rewind(fs_info, p, b, time_seq);
2171 p->locks[level] = BTRFS_READ_LOCK;
2172 p->nodes[level] = b;
2177 btrfs_release_path(p);
2183 * helper to use instead of search slot if no exact match is needed but
2184 * instead the next or previous item should be returned.
2185 * When find_higher is true, the next higher item is returned, the next lower
2187 * When return_any and find_higher are both true, and no higher item is found,
2188 * return the next lower instead.
2189 * When return_any is true and find_higher is false, and no lower item is found,
2190 * return the next higher instead.
2191 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2194 int btrfs_search_slot_for_read(struct btrfs_root *root,
2195 const struct btrfs_key *key,
2196 struct btrfs_path *p, int find_higher,
2200 struct extent_buffer *leaf;
2203 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2207 * a return value of 1 means the path is at the position where the
2208 * item should be inserted. Normally this is the next bigger item,
2209 * but in case the previous item is the last in a leaf, path points
2210 * to the first free slot in the previous leaf, i.e. at an invalid
2216 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2217 ret = btrfs_next_leaf(root, p);
2223 * no higher item found, return the next
2228 btrfs_release_path(p);
2232 if (p->slots[0] == 0) {
2233 ret = btrfs_prev_leaf(root, p);
2238 if (p->slots[0] == btrfs_header_nritems(leaf))
2245 * no lower item found, return the next
2250 btrfs_release_path(p);
2260 * Execute search and call btrfs_previous_item to traverse backwards if the item
2263 * Return 0 if found, 1 if not found and < 0 if error.
2265 int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
2266 struct btrfs_path *path)
2270 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2272 ret = btrfs_previous_item(root, path, key->objectid, key->type);
2275 btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]);
2281 * adjust the pointers going up the tree, starting at level
2282 * making sure the right key of each node is points to 'key'.
2283 * This is used after shifting pointers to the left, so it stops
2284 * fixing up pointers when a given leaf/node is not in slot 0 of the
2288 static void fixup_low_keys(struct btrfs_path *path,
2289 struct btrfs_disk_key *key, int level)
2292 struct extent_buffer *t;
2295 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2296 int tslot = path->slots[i];
2298 if (!path->nodes[i])
2301 ret = btrfs_tree_mod_log_insert_key(t, tslot,
2302 BTRFS_MOD_LOG_KEY_REPLACE, GFP_ATOMIC);
2304 btrfs_set_node_key(t, key, tslot);
2305 btrfs_mark_buffer_dirty(path->nodes[i]);
2314 * This function isn't completely safe. It's the caller's responsibility
2315 * that the new key won't break the order
2317 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2318 struct btrfs_path *path,
2319 const struct btrfs_key *new_key)
2321 struct btrfs_disk_key disk_key;
2322 struct extent_buffer *eb;
2325 eb = path->nodes[0];
2326 slot = path->slots[0];
2328 btrfs_item_key(eb, &disk_key, slot - 1);
2329 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
2331 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2332 slot, btrfs_disk_key_objectid(&disk_key),
2333 btrfs_disk_key_type(&disk_key),
2334 btrfs_disk_key_offset(&disk_key),
2335 new_key->objectid, new_key->type,
2337 btrfs_print_leaf(eb);
2341 if (slot < btrfs_header_nritems(eb) - 1) {
2342 btrfs_item_key(eb, &disk_key, slot + 1);
2343 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
2345 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2346 slot, btrfs_disk_key_objectid(&disk_key),
2347 btrfs_disk_key_type(&disk_key),
2348 btrfs_disk_key_offset(&disk_key),
2349 new_key->objectid, new_key->type,
2351 btrfs_print_leaf(eb);
2356 btrfs_cpu_key_to_disk(&disk_key, new_key);
2357 btrfs_set_item_key(eb, &disk_key, slot);
2358 btrfs_mark_buffer_dirty(eb);
2360 fixup_low_keys(path, &disk_key, 1);
2364 * Check key order of two sibling extent buffers.
2366 * Return true if something is wrong.
2367 * Return false if everything is fine.
2369 * Tree-checker only works inside one tree block, thus the following
2370 * corruption can not be detected by tree-checker:
2372 * Leaf @left | Leaf @right
2373 * --------------------------------------------------------------
2374 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
2376 * Key f6 in leaf @left itself is valid, but not valid when the next
2377 * key in leaf @right is 7.
2378 * This can only be checked at tree block merge time.
2379 * And since tree checker has ensured all key order in each tree block
2380 * is correct, we only need to bother the last key of @left and the first
2383 static bool check_sibling_keys(struct extent_buffer *left,
2384 struct extent_buffer *right)
2386 struct btrfs_key left_last;
2387 struct btrfs_key right_first;
2388 int level = btrfs_header_level(left);
2389 int nr_left = btrfs_header_nritems(left);
2390 int nr_right = btrfs_header_nritems(right);
2392 /* No key to check in one of the tree blocks */
2393 if (!nr_left || !nr_right)
2397 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
2398 btrfs_node_key_to_cpu(right, &right_first, 0);
2400 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
2401 btrfs_item_key_to_cpu(right, &right_first, 0);
2404 if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
2405 btrfs_crit(left->fs_info,
2406 "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
2407 left_last.objectid, left_last.type,
2408 left_last.offset, right_first.objectid,
2409 right_first.type, right_first.offset);
2416 * try to push data from one node into the next node left in the
2419 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2420 * error, and > 0 if there was no room in the left hand block.
2422 static int push_node_left(struct btrfs_trans_handle *trans,
2423 struct extent_buffer *dst,
2424 struct extent_buffer *src, int empty)
2426 struct btrfs_fs_info *fs_info = trans->fs_info;
2432 src_nritems = btrfs_header_nritems(src);
2433 dst_nritems = btrfs_header_nritems(dst);
2434 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2435 WARN_ON(btrfs_header_generation(src) != trans->transid);
2436 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2438 if (!empty && src_nritems <= 8)
2441 if (push_items <= 0)
2445 push_items = min(src_nritems, push_items);
2446 if (push_items < src_nritems) {
2447 /* leave at least 8 pointers in the node if
2448 * we aren't going to empty it
2450 if (src_nritems - push_items < 8) {
2451 if (push_items <= 8)
2457 push_items = min(src_nritems - 8, push_items);
2459 /* dst is the left eb, src is the middle eb */
2460 if (check_sibling_keys(dst, src)) {
2462 btrfs_abort_transaction(trans, ret);
2465 ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
2467 btrfs_abort_transaction(trans, ret);
2470 copy_extent_buffer(dst, src,
2471 btrfs_node_key_ptr_offset(dst_nritems),
2472 btrfs_node_key_ptr_offset(0),
2473 push_items * sizeof(struct btrfs_key_ptr));
2475 if (push_items < src_nritems) {
2477 * Don't call btrfs_tree_mod_log_insert_move() here, key removal
2478 * was already fully logged by btrfs_tree_mod_log_eb_copy() above.
2480 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2481 btrfs_node_key_ptr_offset(push_items),
2482 (src_nritems - push_items) *
2483 sizeof(struct btrfs_key_ptr));
2485 btrfs_set_header_nritems(src, src_nritems - push_items);
2486 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2487 btrfs_mark_buffer_dirty(src);
2488 btrfs_mark_buffer_dirty(dst);
2494 * try to push data from one node into the next node right in the
2497 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2498 * error, and > 0 if there was no room in the right hand block.
2500 * this will only push up to 1/2 the contents of the left node over
2502 static int balance_node_right(struct btrfs_trans_handle *trans,
2503 struct extent_buffer *dst,
2504 struct extent_buffer *src)
2506 struct btrfs_fs_info *fs_info = trans->fs_info;
2513 WARN_ON(btrfs_header_generation(src) != trans->transid);
2514 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2516 src_nritems = btrfs_header_nritems(src);
2517 dst_nritems = btrfs_header_nritems(dst);
2518 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2519 if (push_items <= 0)
2522 if (src_nritems < 4)
2525 max_push = src_nritems / 2 + 1;
2526 /* don't try to empty the node */
2527 if (max_push >= src_nritems)
2530 if (max_push < push_items)
2531 push_items = max_push;
2533 /* dst is the right eb, src is the middle eb */
2534 if (check_sibling_keys(src, dst)) {
2536 btrfs_abort_transaction(trans, ret);
2539 ret = btrfs_tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
2541 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2542 btrfs_node_key_ptr_offset(0),
2544 sizeof(struct btrfs_key_ptr));
2546 ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
2549 btrfs_abort_transaction(trans, ret);
2552 copy_extent_buffer(dst, src,
2553 btrfs_node_key_ptr_offset(0),
2554 btrfs_node_key_ptr_offset(src_nritems - push_items),
2555 push_items * sizeof(struct btrfs_key_ptr));
2557 btrfs_set_header_nritems(src, src_nritems - push_items);
2558 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2560 btrfs_mark_buffer_dirty(src);
2561 btrfs_mark_buffer_dirty(dst);
2567 * helper function to insert a new root level in the tree.
2568 * A new node is allocated, and a single item is inserted to
2569 * point to the existing root
2571 * returns zero on success or < 0 on failure.
2573 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2574 struct btrfs_root *root,
2575 struct btrfs_path *path, int level)
2577 struct btrfs_fs_info *fs_info = root->fs_info;
2579 struct extent_buffer *lower;
2580 struct extent_buffer *c;
2581 struct extent_buffer *old;
2582 struct btrfs_disk_key lower_key;
2585 BUG_ON(path->nodes[level]);
2586 BUG_ON(path->nodes[level-1] != root->node);
2588 lower = path->nodes[level-1];
2590 btrfs_item_key(lower, &lower_key, 0);
2592 btrfs_node_key(lower, &lower_key, 0);
2594 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
2595 &lower_key, level, root->node->start, 0,
2596 BTRFS_NESTING_NEW_ROOT);
2600 root_add_used(root, fs_info->nodesize);
2602 btrfs_set_header_nritems(c, 1);
2603 btrfs_set_node_key(c, &lower_key, 0);
2604 btrfs_set_node_blockptr(c, 0, lower->start);
2605 lower_gen = btrfs_header_generation(lower);
2606 WARN_ON(lower_gen != trans->transid);
2608 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2610 btrfs_mark_buffer_dirty(c);
2613 ret = btrfs_tree_mod_log_insert_root(root->node, c, false);
2615 rcu_assign_pointer(root->node, c);
2617 /* the super has an extra ref to root->node */
2618 free_extent_buffer(old);
2620 add_root_to_dirty_list(root);
2621 atomic_inc(&c->refs);
2622 path->nodes[level] = c;
2623 path->locks[level] = BTRFS_WRITE_LOCK;
2624 path->slots[level] = 0;
2629 * worker function to insert a single pointer in a node.
2630 * the node should have enough room for the pointer already
2632 * slot and level indicate where you want the key to go, and
2633 * blocknr is the block the key points to.
2635 static void insert_ptr(struct btrfs_trans_handle *trans,
2636 struct btrfs_path *path,
2637 struct btrfs_disk_key *key, u64 bytenr,
2638 int slot, int level)
2640 struct extent_buffer *lower;
2644 BUG_ON(!path->nodes[level]);
2645 btrfs_assert_tree_write_locked(path->nodes[level]);
2646 lower = path->nodes[level];
2647 nritems = btrfs_header_nritems(lower);
2648 BUG_ON(slot > nritems);
2649 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
2650 if (slot != nritems) {
2652 ret = btrfs_tree_mod_log_insert_move(lower, slot + 1,
2653 slot, nritems - slot);
2656 memmove_extent_buffer(lower,
2657 btrfs_node_key_ptr_offset(slot + 1),
2658 btrfs_node_key_ptr_offset(slot),
2659 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2662 ret = btrfs_tree_mod_log_insert_key(lower, slot,
2663 BTRFS_MOD_LOG_KEY_ADD, GFP_NOFS);
2666 btrfs_set_node_key(lower, key, slot);
2667 btrfs_set_node_blockptr(lower, slot, bytenr);
2668 WARN_ON(trans->transid == 0);
2669 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2670 btrfs_set_header_nritems(lower, nritems + 1);
2671 btrfs_mark_buffer_dirty(lower);
2675 * split the node at the specified level in path in two.
2676 * The path is corrected to point to the appropriate node after the split
2678 * Before splitting this tries to make some room in the node by pushing
2679 * left and right, if either one works, it returns right away.
2681 * returns 0 on success and < 0 on failure
2683 static noinline int split_node(struct btrfs_trans_handle *trans,
2684 struct btrfs_root *root,
2685 struct btrfs_path *path, int level)
2687 struct btrfs_fs_info *fs_info = root->fs_info;
2688 struct extent_buffer *c;
2689 struct extent_buffer *split;
2690 struct btrfs_disk_key disk_key;
2695 c = path->nodes[level];
2696 WARN_ON(btrfs_header_generation(c) != trans->transid);
2697 if (c == root->node) {
2699 * trying to split the root, lets make a new one
2701 * tree mod log: We don't log_removal old root in
2702 * insert_new_root, because that root buffer will be kept as a
2703 * normal node. We are going to log removal of half of the
2704 * elements below with btrfs_tree_mod_log_eb_copy(). We're
2705 * holding a tree lock on the buffer, which is why we cannot
2706 * race with other tree_mod_log users.
2708 ret = insert_new_root(trans, root, path, level + 1);
2712 ret = push_nodes_for_insert(trans, root, path, level);
2713 c = path->nodes[level];
2714 if (!ret && btrfs_header_nritems(c) <
2715 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
2721 c_nritems = btrfs_header_nritems(c);
2722 mid = (c_nritems + 1) / 2;
2723 btrfs_node_key(c, &disk_key, mid);
2725 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
2726 &disk_key, level, c->start, 0,
2727 BTRFS_NESTING_SPLIT);
2729 return PTR_ERR(split);
2731 root_add_used(root, fs_info->nodesize);
2732 ASSERT(btrfs_header_level(c) == level);
2734 ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
2736 btrfs_abort_transaction(trans, ret);
2739 copy_extent_buffer(split, c,
2740 btrfs_node_key_ptr_offset(0),
2741 btrfs_node_key_ptr_offset(mid),
2742 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2743 btrfs_set_header_nritems(split, c_nritems - mid);
2744 btrfs_set_header_nritems(c, mid);
2746 btrfs_mark_buffer_dirty(c);
2747 btrfs_mark_buffer_dirty(split);
2749 insert_ptr(trans, path, &disk_key, split->start,
2750 path->slots[level + 1] + 1, level + 1);
2752 if (path->slots[level] >= mid) {
2753 path->slots[level] -= mid;
2754 btrfs_tree_unlock(c);
2755 free_extent_buffer(c);
2756 path->nodes[level] = split;
2757 path->slots[level + 1] += 1;
2759 btrfs_tree_unlock(split);
2760 free_extent_buffer(split);
2766 * how many bytes are required to store the items in a leaf. start
2767 * and nr indicate which items in the leaf to check. This totals up the
2768 * space used both by the item structs and the item data
2770 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2773 int nritems = btrfs_header_nritems(l);
2774 int end = min(nritems, start + nr) - 1;
2778 data_len = btrfs_item_offset(l, start) + btrfs_item_size(l, start);
2779 data_len = data_len - btrfs_item_offset(l, end);
2780 data_len += sizeof(struct btrfs_item) * nr;
2781 WARN_ON(data_len < 0);
2786 * The space between the end of the leaf items and
2787 * the start of the leaf data. IOW, how much room
2788 * the leaf has left for both items and data
2790 noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
2792 struct btrfs_fs_info *fs_info = leaf->fs_info;
2793 int nritems = btrfs_header_nritems(leaf);
2796 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
2799 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
2801 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
2802 leaf_space_used(leaf, 0, nritems), nritems);
2808 * min slot controls the lowest index we're willing to push to the
2809 * right. We'll push up to and including min_slot, but no lower
2811 static noinline int __push_leaf_right(struct btrfs_path *path,
2812 int data_size, int empty,
2813 struct extent_buffer *right,
2814 int free_space, u32 left_nritems,
2817 struct btrfs_fs_info *fs_info = right->fs_info;
2818 struct extent_buffer *left = path->nodes[0];
2819 struct extent_buffer *upper = path->nodes[1];
2820 struct btrfs_map_token token;
2821 struct btrfs_disk_key disk_key;
2834 nr = max_t(u32, 1, min_slot);
2836 if (path->slots[0] >= left_nritems)
2837 push_space += data_size;
2839 slot = path->slots[1];
2840 i = left_nritems - 1;
2842 if (!empty && push_items > 0) {
2843 if (path->slots[0] > i)
2845 if (path->slots[0] == i) {
2846 int space = btrfs_leaf_free_space(left);
2848 if (space + push_space * 2 > free_space)
2853 if (path->slots[0] == i)
2854 push_space += data_size;
2856 this_item_size = btrfs_item_size(left, i);
2857 if (this_item_size + sizeof(struct btrfs_item) +
2858 push_space > free_space)
2862 push_space += this_item_size + sizeof(struct btrfs_item);
2868 if (push_items == 0)
2871 WARN_ON(!empty && push_items == left_nritems);
2873 /* push left to right */
2874 right_nritems = btrfs_header_nritems(right);
2876 push_space = btrfs_item_data_end(left, left_nritems - push_items);
2877 push_space -= leaf_data_end(left);
2879 /* make room in the right data area */
2880 data_end = leaf_data_end(right);
2881 memmove_extent_buffer(right,
2882 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
2883 BTRFS_LEAF_DATA_OFFSET + data_end,
2884 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
2886 /* copy from the left data area */
2887 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
2888 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
2889 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
2892 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2893 btrfs_item_nr_offset(0),
2894 right_nritems * sizeof(struct btrfs_item));
2896 /* copy the items from left to right */
2897 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2898 btrfs_item_nr_offset(left_nritems - push_items),
2899 push_items * sizeof(struct btrfs_item));
2901 /* update the item pointers */
2902 btrfs_init_map_token(&token, right);
2903 right_nritems += push_items;
2904 btrfs_set_header_nritems(right, right_nritems);
2905 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
2906 for (i = 0; i < right_nritems; i++) {
2907 push_space -= btrfs_token_item_size(&token, i);
2908 btrfs_set_token_item_offset(&token, i, push_space);
2911 left_nritems -= push_items;
2912 btrfs_set_header_nritems(left, left_nritems);
2915 btrfs_mark_buffer_dirty(left);
2917 btrfs_clean_tree_block(left);
2919 btrfs_mark_buffer_dirty(right);
2921 btrfs_item_key(right, &disk_key, 0);
2922 btrfs_set_node_key(upper, &disk_key, slot + 1);
2923 btrfs_mark_buffer_dirty(upper);
2925 /* then fixup the leaf pointer in the path */
2926 if (path->slots[0] >= left_nritems) {
2927 path->slots[0] -= left_nritems;
2928 if (btrfs_header_nritems(path->nodes[0]) == 0)
2929 btrfs_clean_tree_block(path->nodes[0]);
2930 btrfs_tree_unlock(path->nodes[0]);
2931 free_extent_buffer(path->nodes[0]);
2932 path->nodes[0] = right;
2933 path->slots[1] += 1;
2935 btrfs_tree_unlock(right);
2936 free_extent_buffer(right);
2941 btrfs_tree_unlock(right);
2942 free_extent_buffer(right);
2947 * push some data in the path leaf to the right, trying to free up at
2948 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2950 * returns 1 if the push failed because the other node didn't have enough
2951 * room, 0 if everything worked out and < 0 if there were major errors.
2953 * this will push starting from min_slot to the end of the leaf. It won't
2954 * push any slot lower than min_slot
2956 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2957 *root, struct btrfs_path *path,
2958 int min_data_size, int data_size,
2959 int empty, u32 min_slot)
2961 struct extent_buffer *left = path->nodes[0];
2962 struct extent_buffer *right;
2963 struct extent_buffer *upper;
2969 if (!path->nodes[1])
2972 slot = path->slots[1];
2973 upper = path->nodes[1];
2974 if (slot >= btrfs_header_nritems(upper) - 1)
2977 btrfs_assert_tree_write_locked(path->nodes[1]);
2979 right = btrfs_read_node_slot(upper, slot + 1);
2981 * slot + 1 is not valid or we fail to read the right node,
2982 * no big deal, just return.
2987 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
2989 free_space = btrfs_leaf_free_space(right);
2990 if (free_space < data_size)
2993 /* cow and double check */
2994 ret = btrfs_cow_block(trans, root, right, upper,
2995 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
2999 free_space = btrfs_leaf_free_space(right);
3000 if (free_space < data_size)
3003 left_nritems = btrfs_header_nritems(left);
3004 if (left_nritems == 0)
3007 if (check_sibling_keys(left, right)) {
3009 btrfs_tree_unlock(right);
3010 free_extent_buffer(right);
3013 if (path->slots[0] == left_nritems && !empty) {
3014 /* Key greater than all keys in the leaf, right neighbor has
3015 * enough room for it and we're not emptying our leaf to delete
3016 * it, therefore use right neighbor to insert the new item and
3017 * no need to touch/dirty our left leaf. */
3018 btrfs_tree_unlock(left);
3019 free_extent_buffer(left);
3020 path->nodes[0] = right;
3026 return __push_leaf_right(path, min_data_size, empty,
3027 right, free_space, left_nritems, min_slot);
3029 btrfs_tree_unlock(right);
3030 free_extent_buffer(right);
3035 * push some data in the path leaf to the left, trying to free up at
3036 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3038 * max_slot can put a limit on how far into the leaf we'll push items. The
3039 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3042 static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
3043 int empty, struct extent_buffer *left,
3044 int free_space, u32 right_nritems,
3047 struct btrfs_fs_info *fs_info = left->fs_info;
3048 struct btrfs_disk_key disk_key;
3049 struct extent_buffer *right = path->nodes[0];
3053 u32 old_left_nritems;
3057 u32 old_left_item_size;
3058 struct btrfs_map_token token;
3061 nr = min(right_nritems, max_slot);
3063 nr = min(right_nritems - 1, max_slot);
3065 for (i = 0; i < nr; i++) {
3066 if (!empty && push_items > 0) {
3067 if (path->slots[0] < i)
3069 if (path->slots[0] == i) {
3070 int space = btrfs_leaf_free_space(right);
3072 if (space + push_space * 2 > free_space)
3077 if (path->slots[0] == i)
3078 push_space += data_size;
3080 this_item_size = btrfs_item_size(right, i);
3081 if (this_item_size + sizeof(struct btrfs_item) + push_space >
3086 push_space += this_item_size + sizeof(struct btrfs_item);
3089 if (push_items == 0) {
3093 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3095 /* push data from right to left */
3096 copy_extent_buffer(left, right,
3097 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3098 btrfs_item_nr_offset(0),
3099 push_items * sizeof(struct btrfs_item));
3101 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3102 btrfs_item_offset(right, push_items - 1);
3104 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3105 leaf_data_end(left) - push_space,
3106 BTRFS_LEAF_DATA_OFFSET +
3107 btrfs_item_offset(right, push_items - 1),
3109 old_left_nritems = btrfs_header_nritems(left);
3110 BUG_ON(old_left_nritems <= 0);
3112 btrfs_init_map_token(&token, left);
3113 old_left_item_size = btrfs_item_offset(left, old_left_nritems - 1);
3114 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3117 ioff = btrfs_token_item_offset(&token, i);
3118 btrfs_set_token_item_offset(&token, i,
3119 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
3121 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3123 /* fixup right node */
3124 if (push_items > right_nritems)
3125 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3128 if (push_items < right_nritems) {
3129 push_space = btrfs_item_offset(right, push_items - 1) -
3130 leaf_data_end(right);
3131 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
3132 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3133 BTRFS_LEAF_DATA_OFFSET +
3134 leaf_data_end(right), push_space);
3136 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3137 btrfs_item_nr_offset(push_items),
3138 (btrfs_header_nritems(right) - push_items) *
3139 sizeof(struct btrfs_item));
3142 btrfs_init_map_token(&token, right);
3143 right_nritems -= push_items;
3144 btrfs_set_header_nritems(right, right_nritems);
3145 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3146 for (i = 0; i < right_nritems; i++) {
3147 push_space = push_space - btrfs_token_item_size(&token, i);
3148 btrfs_set_token_item_offset(&token, i, push_space);
3151 btrfs_mark_buffer_dirty(left);
3153 btrfs_mark_buffer_dirty(right);
3155 btrfs_clean_tree_block(right);
3157 btrfs_item_key(right, &disk_key, 0);
3158 fixup_low_keys(path, &disk_key, 1);
3160 /* then fixup the leaf pointer in the path */
3161 if (path->slots[0] < push_items) {
3162 path->slots[0] += old_left_nritems;
3163 btrfs_tree_unlock(path->nodes[0]);
3164 free_extent_buffer(path->nodes[0]);
3165 path->nodes[0] = left;
3166 path->slots[1] -= 1;
3168 btrfs_tree_unlock(left);
3169 free_extent_buffer(left);
3170 path->slots[0] -= push_items;
3172 BUG_ON(path->slots[0] < 0);
3175 btrfs_tree_unlock(left);
3176 free_extent_buffer(left);
3181 * push some data in the path leaf to the left, trying to free up at
3182 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3184 * max_slot can put a limit on how far into the leaf we'll push items. The
3185 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3188 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3189 *root, struct btrfs_path *path, int min_data_size,
3190 int data_size, int empty, u32 max_slot)
3192 struct extent_buffer *right = path->nodes[0];
3193 struct extent_buffer *left;
3199 slot = path->slots[1];
3202 if (!path->nodes[1])
3205 right_nritems = btrfs_header_nritems(right);
3206 if (right_nritems == 0)
3209 btrfs_assert_tree_write_locked(path->nodes[1]);
3211 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
3213 * slot - 1 is not valid or we fail to read the left node,
3214 * no big deal, just return.
3219 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
3221 free_space = btrfs_leaf_free_space(left);
3222 if (free_space < data_size) {
3227 /* cow and double check */
3228 ret = btrfs_cow_block(trans, root, left,
3229 path->nodes[1], slot - 1, &left,
3230 BTRFS_NESTING_LEFT_COW);
3232 /* we hit -ENOSPC, but it isn't fatal here */
3238 free_space = btrfs_leaf_free_space(left);
3239 if (free_space < data_size) {
3244 if (check_sibling_keys(left, right)) {
3248 return __push_leaf_left(path, min_data_size,
3249 empty, left, free_space, right_nritems,
3252 btrfs_tree_unlock(left);
3253 free_extent_buffer(left);
3258 * split the path's leaf in two, making sure there is at least data_size
3259 * available for the resulting leaf level of the path.
3261 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3262 struct btrfs_path *path,
3263 struct extent_buffer *l,
3264 struct extent_buffer *right,
3265 int slot, int mid, int nritems)
3267 struct btrfs_fs_info *fs_info = trans->fs_info;
3271 struct btrfs_disk_key disk_key;
3272 struct btrfs_map_token token;
3274 nritems = nritems - mid;
3275 btrfs_set_header_nritems(right, nritems);
3276 data_copy_size = btrfs_item_data_end(l, mid) - leaf_data_end(l);
3278 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3279 btrfs_item_nr_offset(mid),
3280 nritems * sizeof(struct btrfs_item));
3282 copy_extent_buffer(right, l,
3283 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
3284 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
3285 leaf_data_end(l), data_copy_size);
3287 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_data_end(l, mid);
3289 btrfs_init_map_token(&token, right);
3290 for (i = 0; i < nritems; i++) {
3293 ioff = btrfs_token_item_offset(&token, i);
3294 btrfs_set_token_item_offset(&token, i, ioff + rt_data_off);
3297 btrfs_set_header_nritems(l, mid);
3298 btrfs_item_key(right, &disk_key, 0);
3299 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
3301 btrfs_mark_buffer_dirty(right);
3302 btrfs_mark_buffer_dirty(l);
3303 BUG_ON(path->slots[0] != slot);
3306 btrfs_tree_unlock(path->nodes[0]);
3307 free_extent_buffer(path->nodes[0]);
3308 path->nodes[0] = right;
3309 path->slots[0] -= mid;
3310 path->slots[1] += 1;
3312 btrfs_tree_unlock(right);
3313 free_extent_buffer(right);
3316 BUG_ON(path->slots[0] < 0);
3320 * double splits happen when we need to insert a big item in the middle
3321 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3322 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3325 * We avoid this by trying to push the items on either side of our target
3326 * into the adjacent leaves. If all goes well we can avoid the double split
3329 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3330 struct btrfs_root *root,
3331 struct btrfs_path *path,
3338 int space_needed = data_size;
3340 slot = path->slots[0];
3341 if (slot < btrfs_header_nritems(path->nodes[0]))
3342 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3345 * try to push all the items after our slot into the
3348 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
3355 nritems = btrfs_header_nritems(path->nodes[0]);
3357 * our goal is to get our slot at the start or end of a leaf. If
3358 * we've done so we're done
3360 if (path->slots[0] == 0 || path->slots[0] == nritems)
3363 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3366 /* try to push all the items before our slot into the next leaf */
3367 slot = path->slots[0];
3368 space_needed = data_size;
3370 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3371 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
3384 * split the path's leaf in two, making sure there is at least data_size
3385 * available for the resulting leaf level of the path.
3387 * returns 0 if all went well and < 0 on failure.
3389 static noinline int split_leaf(struct btrfs_trans_handle *trans,
3390 struct btrfs_root *root,
3391 const struct btrfs_key *ins_key,
3392 struct btrfs_path *path, int data_size,
3395 struct btrfs_disk_key disk_key;
3396 struct extent_buffer *l;
3400 struct extent_buffer *right;
3401 struct btrfs_fs_info *fs_info = root->fs_info;
3405 int num_doubles = 0;
3406 int tried_avoid_double = 0;
3409 slot = path->slots[0];
3410 if (extend && data_size + btrfs_item_size(l, slot) +
3411 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
3414 /* first try to make some room by pushing left and right */
3415 if (data_size && path->nodes[1]) {
3416 int space_needed = data_size;
3418 if (slot < btrfs_header_nritems(l))
3419 space_needed -= btrfs_leaf_free_space(l);
3421 wret = push_leaf_right(trans, root, path, space_needed,
3422 space_needed, 0, 0);
3426 space_needed = data_size;
3428 space_needed -= btrfs_leaf_free_space(l);
3429 wret = push_leaf_left(trans, root, path, space_needed,
3430 space_needed, 0, (u32)-1);
3436 /* did the pushes work? */
3437 if (btrfs_leaf_free_space(l) >= data_size)
3441 if (!path->nodes[1]) {
3442 ret = insert_new_root(trans, root, path, 1);
3449 slot = path->slots[0];
3450 nritems = btrfs_header_nritems(l);
3451 mid = (nritems + 1) / 2;
3455 leaf_space_used(l, mid, nritems - mid) + data_size >
3456 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3457 if (slot >= nritems) {
3461 if (mid != nritems &&
3462 leaf_space_used(l, mid, nritems - mid) +
3463 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3464 if (data_size && !tried_avoid_double)
3465 goto push_for_double;
3471 if (leaf_space_used(l, 0, mid) + data_size >
3472 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3473 if (!extend && data_size && slot == 0) {
3475 } else if ((extend || !data_size) && slot == 0) {
3479 if (mid != nritems &&
3480 leaf_space_used(l, mid, nritems - mid) +
3481 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3482 if (data_size && !tried_avoid_double)
3483 goto push_for_double;
3491 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3493 btrfs_item_key(l, &disk_key, mid);
3496 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
3497 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
3498 * subclasses, which is 8 at the time of this patch, and we've maxed it
3499 * out. In the future we could add a
3500 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
3501 * use BTRFS_NESTING_NEW_ROOT.
3503 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3504 &disk_key, 0, l->start, 0,
3505 num_doubles ? BTRFS_NESTING_NEW_ROOT :
3506 BTRFS_NESTING_SPLIT);
3508 return PTR_ERR(right);
3510 root_add_used(root, fs_info->nodesize);
3514 btrfs_set_header_nritems(right, 0);
3515 insert_ptr(trans, path, &disk_key,
3516 right->start, path->slots[1] + 1, 1);
3517 btrfs_tree_unlock(path->nodes[0]);
3518 free_extent_buffer(path->nodes[0]);
3519 path->nodes[0] = right;
3521 path->slots[1] += 1;
3523 btrfs_set_header_nritems(right, 0);
3524 insert_ptr(trans, path, &disk_key,
3525 right->start, path->slots[1], 1);
3526 btrfs_tree_unlock(path->nodes[0]);
3527 free_extent_buffer(path->nodes[0]);
3528 path->nodes[0] = right;
3530 if (path->slots[1] == 0)
3531 fixup_low_keys(path, &disk_key, 1);
3534 * We create a new leaf 'right' for the required ins_len and
3535 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
3536 * the content of ins_len to 'right'.
3541 copy_for_split(trans, path, l, right, slot, mid, nritems);
3544 BUG_ON(num_doubles != 0);
3552 push_for_double_split(trans, root, path, data_size);
3553 tried_avoid_double = 1;
3554 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3559 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3560 struct btrfs_root *root,
3561 struct btrfs_path *path, int ins_len)
3563 struct btrfs_key key;
3564 struct extent_buffer *leaf;
3565 struct btrfs_file_extent_item *fi;
3570 leaf = path->nodes[0];
3571 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3573 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3574 key.type != BTRFS_EXTENT_CSUM_KEY);
3576 if (btrfs_leaf_free_space(leaf) >= ins_len)
3579 item_size = btrfs_item_size(leaf, path->slots[0]);
3580 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3581 fi = btrfs_item_ptr(leaf, path->slots[0],
3582 struct btrfs_file_extent_item);
3583 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3585 btrfs_release_path(path);
3587 path->keep_locks = 1;
3588 path->search_for_split = 1;
3589 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3590 path->search_for_split = 0;
3597 leaf = path->nodes[0];
3598 /* if our item isn't there, return now */
3599 if (item_size != btrfs_item_size(leaf, path->slots[0]))
3602 /* the leaf has changed, it now has room. return now */
3603 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
3606 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3607 fi = btrfs_item_ptr(leaf, path->slots[0],
3608 struct btrfs_file_extent_item);
3609 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3613 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3617 path->keep_locks = 0;
3618 btrfs_unlock_up_safe(path, 1);
3621 path->keep_locks = 0;
3625 static noinline int split_item(struct btrfs_path *path,
3626 const struct btrfs_key *new_key,
3627 unsigned long split_offset)
3629 struct extent_buffer *leaf;
3630 int orig_slot, slot;
3635 struct btrfs_disk_key disk_key;
3637 leaf = path->nodes[0];
3638 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
3640 orig_slot = path->slots[0];
3641 orig_offset = btrfs_item_offset(leaf, path->slots[0]);
3642 item_size = btrfs_item_size(leaf, path->slots[0]);
3644 buf = kmalloc(item_size, GFP_NOFS);
3648 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3649 path->slots[0]), item_size);
3651 slot = path->slots[0] + 1;
3652 nritems = btrfs_header_nritems(leaf);
3653 if (slot != nritems) {
3654 /* shift the items */
3655 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3656 btrfs_item_nr_offset(slot),
3657 (nritems - slot) * sizeof(struct btrfs_item));
3660 btrfs_cpu_key_to_disk(&disk_key, new_key);
3661 btrfs_set_item_key(leaf, &disk_key, slot);
3663 btrfs_set_item_offset(leaf, slot, orig_offset);
3664 btrfs_set_item_size(leaf, slot, item_size - split_offset);
3666 btrfs_set_item_offset(leaf, orig_slot,
3667 orig_offset + item_size - split_offset);
3668 btrfs_set_item_size(leaf, orig_slot, split_offset);
3670 btrfs_set_header_nritems(leaf, nritems + 1);
3672 /* write the data for the start of the original item */
3673 write_extent_buffer(leaf, buf,
3674 btrfs_item_ptr_offset(leaf, path->slots[0]),
3677 /* write the data for the new item */
3678 write_extent_buffer(leaf, buf + split_offset,
3679 btrfs_item_ptr_offset(leaf, slot),
3680 item_size - split_offset);
3681 btrfs_mark_buffer_dirty(leaf);
3683 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
3689 * This function splits a single item into two items,
3690 * giving 'new_key' to the new item and splitting the
3691 * old one at split_offset (from the start of the item).
3693 * The path may be released by this operation. After
3694 * the split, the path is pointing to the old item. The
3695 * new item is going to be in the same node as the old one.
3697 * Note, the item being split must be smaller enough to live alone on
3698 * a tree block with room for one extra struct btrfs_item
3700 * This allows us to split the item in place, keeping a lock on the
3701 * leaf the entire time.
3703 int btrfs_split_item(struct btrfs_trans_handle *trans,
3704 struct btrfs_root *root,
3705 struct btrfs_path *path,
3706 const struct btrfs_key *new_key,
3707 unsigned long split_offset)
3710 ret = setup_leaf_for_split(trans, root, path,
3711 sizeof(struct btrfs_item));
3715 ret = split_item(path, new_key, split_offset);
3720 * make the item pointed to by the path smaller. new_size indicates
3721 * how small to make it, and from_end tells us if we just chop bytes
3722 * off the end of the item or if we shift the item to chop bytes off
3725 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
3728 struct extent_buffer *leaf;
3730 unsigned int data_end;
3731 unsigned int old_data_start;
3732 unsigned int old_size;
3733 unsigned int size_diff;
3735 struct btrfs_map_token token;
3737 leaf = path->nodes[0];
3738 slot = path->slots[0];
3740 old_size = btrfs_item_size(leaf, slot);
3741 if (old_size == new_size)
3744 nritems = btrfs_header_nritems(leaf);
3745 data_end = leaf_data_end(leaf);
3747 old_data_start = btrfs_item_offset(leaf, slot);
3749 size_diff = old_size - new_size;
3752 BUG_ON(slot >= nritems);
3755 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3757 /* first correct the data pointers */
3758 btrfs_init_map_token(&token, leaf);
3759 for (i = slot; i < nritems; i++) {
3762 ioff = btrfs_token_item_offset(&token, i);
3763 btrfs_set_token_item_offset(&token, i, ioff + size_diff);
3766 /* shift the data */
3768 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3769 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
3770 data_end, old_data_start + new_size - data_end);
3772 struct btrfs_disk_key disk_key;
3775 btrfs_item_key(leaf, &disk_key, slot);
3777 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3779 struct btrfs_file_extent_item *fi;
3781 fi = btrfs_item_ptr(leaf, slot,
3782 struct btrfs_file_extent_item);
3783 fi = (struct btrfs_file_extent_item *)(
3784 (unsigned long)fi - size_diff);
3786 if (btrfs_file_extent_type(leaf, fi) ==
3787 BTRFS_FILE_EXTENT_INLINE) {
3788 ptr = btrfs_item_ptr_offset(leaf, slot);
3789 memmove_extent_buffer(leaf, ptr,
3791 BTRFS_FILE_EXTENT_INLINE_DATA_START);
3795 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3796 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
3797 data_end, old_data_start - data_end);
3799 offset = btrfs_disk_key_offset(&disk_key);
3800 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3801 btrfs_set_item_key(leaf, &disk_key, slot);
3803 fixup_low_keys(path, &disk_key, 1);
3806 btrfs_set_item_size(leaf, slot, new_size);
3807 btrfs_mark_buffer_dirty(leaf);
3809 if (btrfs_leaf_free_space(leaf) < 0) {
3810 btrfs_print_leaf(leaf);
3816 * make the item pointed to by the path bigger, data_size is the added size.
3818 void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
3821 struct extent_buffer *leaf;
3823 unsigned int data_end;
3824 unsigned int old_data;
3825 unsigned int old_size;
3827 struct btrfs_map_token token;
3829 leaf = path->nodes[0];
3831 nritems = btrfs_header_nritems(leaf);
3832 data_end = leaf_data_end(leaf);
3834 if (btrfs_leaf_free_space(leaf) < data_size) {
3835 btrfs_print_leaf(leaf);
3838 slot = path->slots[0];
3839 old_data = btrfs_item_data_end(leaf, slot);
3842 if (slot >= nritems) {
3843 btrfs_print_leaf(leaf);
3844 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
3850 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3852 /* first correct the data pointers */
3853 btrfs_init_map_token(&token, leaf);
3854 for (i = slot; i < nritems; i++) {
3857 ioff = btrfs_token_item_offset(&token, i);
3858 btrfs_set_token_item_offset(&token, i, ioff - data_size);
3861 /* shift the data */
3862 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3863 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
3864 data_end, old_data - data_end);
3866 data_end = old_data;
3867 old_size = btrfs_item_size(leaf, slot);
3868 btrfs_set_item_size(leaf, slot, old_size + data_size);
3869 btrfs_mark_buffer_dirty(leaf);
3871 if (btrfs_leaf_free_space(leaf) < 0) {
3872 btrfs_print_leaf(leaf);
3878 * setup_items_for_insert - Helper called before inserting one or more items
3879 * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
3880 * in a function that doesn't call btrfs_search_slot
3882 * @root: root we are inserting items to
3883 * @path: points to the leaf/slot where we are going to insert new items
3884 * @batch: information about the batch of items to insert
3886 static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
3887 const struct btrfs_item_batch *batch)
3889 struct btrfs_fs_info *fs_info = root->fs_info;
3892 unsigned int data_end;
3893 struct btrfs_disk_key disk_key;
3894 struct extent_buffer *leaf;
3896 struct btrfs_map_token token;
3900 * Before anything else, update keys in the parent and other ancestors
3901 * if needed, then release the write locks on them, so that other tasks
3902 * can use them while we modify the leaf.
3904 if (path->slots[0] == 0) {
3905 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[0]);
3906 fixup_low_keys(path, &disk_key, 1);
3908 btrfs_unlock_up_safe(path, 1);
3910 leaf = path->nodes[0];
3911 slot = path->slots[0];
3913 nritems = btrfs_header_nritems(leaf);
3914 data_end = leaf_data_end(leaf);
3915 total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
3917 if (btrfs_leaf_free_space(leaf) < total_size) {
3918 btrfs_print_leaf(leaf);
3919 btrfs_crit(fs_info, "not enough freespace need %u have %d",
3920 total_size, btrfs_leaf_free_space(leaf));
3924 btrfs_init_map_token(&token, leaf);
3925 if (slot != nritems) {
3926 unsigned int old_data = btrfs_item_data_end(leaf, slot);
3928 if (old_data < data_end) {
3929 btrfs_print_leaf(leaf);
3931 "item at slot %d with data offset %u beyond data end of leaf %u",
3932 slot, old_data, data_end);
3936 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3938 /* first correct the data pointers */
3939 for (i = slot; i < nritems; i++) {
3942 ioff = btrfs_token_item_offset(&token, i);
3943 btrfs_set_token_item_offset(&token, i,
3944 ioff - batch->total_data_size);
3946 /* shift the items */
3947 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + batch->nr),
3948 btrfs_item_nr_offset(slot),
3949 (nritems - slot) * sizeof(struct btrfs_item));
3951 /* shift the data */
3952 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3953 data_end - batch->total_data_size,
3954 BTRFS_LEAF_DATA_OFFSET + data_end,
3955 old_data - data_end);
3956 data_end = old_data;
3959 /* setup the item for the new data */
3960 for (i = 0; i < batch->nr; i++) {
3961 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[i]);
3962 btrfs_set_item_key(leaf, &disk_key, slot + i);
3963 data_end -= batch->data_sizes[i];
3964 btrfs_set_token_item_offset(&token, slot + i, data_end);
3965 btrfs_set_token_item_size(&token, slot + i, batch->data_sizes[i]);
3968 btrfs_set_header_nritems(leaf, nritems + batch->nr);
3969 btrfs_mark_buffer_dirty(leaf);
3971 if (btrfs_leaf_free_space(leaf) < 0) {
3972 btrfs_print_leaf(leaf);
3978 * Insert a new item into a leaf.
3980 * @root: The root of the btree.
3981 * @path: A path pointing to the target leaf and slot.
3982 * @key: The key of the new item.
3983 * @data_size: The size of the data associated with the new key.
3985 void btrfs_setup_item_for_insert(struct btrfs_root *root,
3986 struct btrfs_path *path,
3987 const struct btrfs_key *key,
3990 struct btrfs_item_batch batch;
3993 batch.data_sizes = &data_size;
3994 batch.total_data_size = data_size;
3997 setup_items_for_insert(root, path, &batch);
4001 * Given a key and some data, insert items into the tree.
4002 * This does all the path init required, making room in the tree if needed.
4004 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4005 struct btrfs_root *root,
4006 struct btrfs_path *path,
4007 const struct btrfs_item_batch *batch)
4013 total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
4014 ret = btrfs_search_slot(trans, root, &batch->keys[0], path, total_size, 1);
4020 slot = path->slots[0];
4023 setup_items_for_insert(root, path, batch);
4028 * Given a key and some data, insert an item into the tree.
4029 * This does all the path init required, making room in the tree if needed.
4031 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4032 const struct btrfs_key *cpu_key, void *data,
4036 struct btrfs_path *path;
4037 struct extent_buffer *leaf;
4040 path = btrfs_alloc_path();
4043 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4045 leaf = path->nodes[0];
4046 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4047 write_extent_buffer(leaf, data, ptr, data_size);
4048 btrfs_mark_buffer_dirty(leaf);
4050 btrfs_free_path(path);
4055 * This function duplicates an item, giving 'new_key' to the new item.
4056 * It guarantees both items live in the same tree leaf and the new item is
4057 * contiguous with the original item.
4059 * This allows us to split a file extent in place, keeping a lock on the leaf
4062 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4063 struct btrfs_root *root,
4064 struct btrfs_path *path,
4065 const struct btrfs_key *new_key)
4067 struct extent_buffer *leaf;
4071 leaf = path->nodes[0];
4072 item_size = btrfs_item_size(leaf, path->slots[0]);
4073 ret = setup_leaf_for_split(trans, root, path,
4074 item_size + sizeof(struct btrfs_item));
4079 btrfs_setup_item_for_insert(root, path, new_key, item_size);
4080 leaf = path->nodes[0];
4081 memcpy_extent_buffer(leaf,
4082 btrfs_item_ptr_offset(leaf, path->slots[0]),
4083 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4089 * delete the pointer from a given node.
4091 * the tree should have been previously balanced so the deletion does not
4094 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4095 int level, int slot)
4097 struct extent_buffer *parent = path->nodes[level];
4101 nritems = btrfs_header_nritems(parent);
4102 if (slot != nritems - 1) {
4104 ret = btrfs_tree_mod_log_insert_move(parent, slot,
4105 slot + 1, nritems - slot - 1);
4108 memmove_extent_buffer(parent,
4109 btrfs_node_key_ptr_offset(slot),
4110 btrfs_node_key_ptr_offset(slot + 1),
4111 sizeof(struct btrfs_key_ptr) *
4112 (nritems - slot - 1));
4114 ret = btrfs_tree_mod_log_insert_key(parent, slot,
4115 BTRFS_MOD_LOG_KEY_REMOVE, GFP_NOFS);
4120 btrfs_set_header_nritems(parent, nritems);
4121 if (nritems == 0 && parent == root->node) {
4122 BUG_ON(btrfs_header_level(root->node) != 1);
4123 /* just turn the root into a leaf and break */
4124 btrfs_set_header_level(root->node, 0);
4125 } else if (slot == 0) {
4126 struct btrfs_disk_key disk_key;
4128 btrfs_node_key(parent, &disk_key, 0);
4129 fixup_low_keys(path, &disk_key, level + 1);
4131 btrfs_mark_buffer_dirty(parent);
4135 * a helper function to delete the leaf pointed to by path->slots[1] and
4138 * This deletes the pointer in path->nodes[1] and frees the leaf
4139 * block extent. zero is returned if it all worked out, < 0 otherwise.
4141 * The path must have already been setup for deleting the leaf, including
4142 * all the proper balancing. path->nodes[1] must be locked.
4144 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4145 struct btrfs_root *root,
4146 struct btrfs_path *path,
4147 struct extent_buffer *leaf)
4149 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4150 del_ptr(root, path, 1, path->slots[1]);
4153 * btrfs_free_extent is expensive, we want to make sure we
4154 * aren't holding any locks when we call it
4156 btrfs_unlock_up_safe(path, 0);
4158 root_sub_used(root, leaf->len);
4160 atomic_inc(&leaf->refs);
4161 btrfs_free_tree_block(trans, btrfs_root_id(root), leaf, 0, 1);
4162 free_extent_buffer_stale(leaf);
4165 * delete the item at the leaf level in path. If that empties
4166 * the leaf, remove it from the tree
4168 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4169 struct btrfs_path *path, int slot, int nr)
4171 struct btrfs_fs_info *fs_info = root->fs_info;
4172 struct extent_buffer *leaf;
4180 leaf = path->nodes[0];
4181 last_off = btrfs_item_offset(leaf, slot + nr - 1);
4183 for (i = 0; i < nr; i++)
4184 dsize += btrfs_item_size(leaf, slot + i);
4186 nritems = btrfs_header_nritems(leaf);
4188 if (slot + nr != nritems) {
4189 int data_end = leaf_data_end(leaf);
4190 struct btrfs_map_token token;
4192 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4194 BTRFS_LEAF_DATA_OFFSET + data_end,
4195 last_off - data_end);
4197 btrfs_init_map_token(&token, leaf);
4198 for (i = slot + nr; i < nritems; i++) {
4201 ioff = btrfs_token_item_offset(&token, i);
4202 btrfs_set_token_item_offset(&token, i, ioff + dsize);
4205 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4206 btrfs_item_nr_offset(slot + nr),
4207 sizeof(struct btrfs_item) *
4208 (nritems - slot - nr));
4210 btrfs_set_header_nritems(leaf, nritems - nr);
4213 /* delete the leaf if we've emptied it */
4215 if (leaf == root->node) {
4216 btrfs_set_header_level(leaf, 0);
4218 btrfs_clean_tree_block(leaf);
4219 btrfs_del_leaf(trans, root, path, leaf);
4222 int used = leaf_space_used(leaf, 0, nritems);
4224 struct btrfs_disk_key disk_key;
4226 btrfs_item_key(leaf, &disk_key, 0);
4227 fixup_low_keys(path, &disk_key, 1);
4230 /* delete the leaf if it is mostly empty */
4231 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
4232 /* push_leaf_left fixes the path.
4233 * make sure the path still points to our leaf
4234 * for possible call to del_ptr below
4236 slot = path->slots[1];
4237 atomic_inc(&leaf->refs);
4239 wret = push_leaf_left(trans, root, path, 1, 1,
4241 if (wret < 0 && wret != -ENOSPC)
4244 if (path->nodes[0] == leaf &&
4245 btrfs_header_nritems(leaf)) {
4246 wret = push_leaf_right(trans, root, path, 1,
4248 if (wret < 0 && wret != -ENOSPC)
4252 if (btrfs_header_nritems(leaf) == 0) {
4253 path->slots[1] = slot;
4254 btrfs_del_leaf(trans, root, path, leaf);
4255 free_extent_buffer(leaf);
4258 /* if we're still in the path, make sure
4259 * we're dirty. Otherwise, one of the
4260 * push_leaf functions must have already
4261 * dirtied this buffer
4263 if (path->nodes[0] == leaf)
4264 btrfs_mark_buffer_dirty(leaf);
4265 free_extent_buffer(leaf);
4268 btrfs_mark_buffer_dirty(leaf);
4275 * search the tree again to find a leaf with lesser keys
4276 * returns 0 if it found something or 1 if there are no lesser leaves.
4277 * returns < 0 on io errors.
4279 * This may release the path, and so you may lose any locks held at the
4282 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4284 struct btrfs_key key;
4285 struct btrfs_disk_key found_key;
4288 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
4290 if (key.offset > 0) {
4292 } else if (key.type > 0) {
4294 key.offset = (u64)-1;
4295 } else if (key.objectid > 0) {
4298 key.offset = (u64)-1;
4303 btrfs_release_path(path);
4304 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4307 btrfs_item_key(path->nodes[0], &found_key, 0);
4308 ret = comp_keys(&found_key, &key);
4310 * We might have had an item with the previous key in the tree right
4311 * before we released our path. And after we released our path, that
4312 * item might have been pushed to the first slot (0) of the leaf we
4313 * were holding due to a tree balance. Alternatively, an item with the
4314 * previous key can exist as the only element of a leaf (big fat item).
4315 * Therefore account for these 2 cases, so that our callers (like
4316 * btrfs_previous_item) don't miss an existing item with a key matching
4317 * the previous key we computed above.
4325 * A helper function to walk down the tree starting at min_key, and looking
4326 * for nodes or leaves that are have a minimum transaction id.
4327 * This is used by the btree defrag code, and tree logging
4329 * This does not cow, but it does stuff the starting key it finds back
4330 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4331 * key and get a writable path.
4333 * This honors path->lowest_level to prevent descent past a given level
4336 * min_trans indicates the oldest transaction that you are interested
4337 * in walking through. Any nodes or leaves older than min_trans are
4338 * skipped over (without reading them).
4340 * returns zero if something useful was found, < 0 on error and 1 if there
4341 * was nothing in the tree that matched the search criteria.
4343 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4344 struct btrfs_path *path,
4347 struct extent_buffer *cur;
4348 struct btrfs_key found_key;
4354 int keep_locks = path->keep_locks;
4356 path->keep_locks = 1;
4358 cur = btrfs_read_lock_root_node(root);
4359 level = btrfs_header_level(cur);
4360 WARN_ON(path->nodes[level]);
4361 path->nodes[level] = cur;
4362 path->locks[level] = BTRFS_READ_LOCK;
4364 if (btrfs_header_generation(cur) < min_trans) {
4369 nritems = btrfs_header_nritems(cur);
4370 level = btrfs_header_level(cur);
4371 sret = btrfs_bin_search(cur, min_key, &slot);
4377 /* at the lowest level, we're done, setup the path and exit */
4378 if (level == path->lowest_level) {
4379 if (slot >= nritems)
4382 path->slots[level] = slot;
4383 btrfs_item_key_to_cpu(cur, &found_key, slot);
4386 if (sret && slot > 0)
4389 * check this node pointer against the min_trans parameters.
4390 * If it is too old, skip to the next one.
4392 while (slot < nritems) {
4395 gen = btrfs_node_ptr_generation(cur, slot);
4396 if (gen < min_trans) {
4404 * we didn't find a candidate key in this node, walk forward
4405 * and find another one
4407 if (slot >= nritems) {
4408 path->slots[level] = slot;
4409 sret = btrfs_find_next_key(root, path, min_key, level,
4412 btrfs_release_path(path);
4418 /* save our key for returning back */
4419 btrfs_node_key_to_cpu(cur, &found_key, slot);
4420 path->slots[level] = slot;
4421 if (level == path->lowest_level) {
4425 cur = btrfs_read_node_slot(cur, slot);
4431 btrfs_tree_read_lock(cur);
4433 path->locks[level - 1] = BTRFS_READ_LOCK;
4434 path->nodes[level - 1] = cur;
4435 unlock_up(path, level, 1, 0, NULL);
4438 path->keep_locks = keep_locks;
4440 btrfs_unlock_up_safe(path, path->lowest_level + 1);
4441 memcpy(min_key, &found_key, sizeof(found_key));
4447 * this is similar to btrfs_next_leaf, but does not try to preserve
4448 * and fixup the path. It looks for and returns the next key in the
4449 * tree based on the current path and the min_trans parameters.
4451 * 0 is returned if another key is found, < 0 if there are any errors
4452 * and 1 is returned if there are no higher keys in the tree
4454 * path->keep_locks should be set to 1 on the search made before
4455 * calling this function.
4457 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4458 struct btrfs_key *key, int level, u64 min_trans)
4461 struct extent_buffer *c;
4463 WARN_ON(!path->keep_locks && !path->skip_locking);
4464 while (level < BTRFS_MAX_LEVEL) {
4465 if (!path->nodes[level])
4468 slot = path->slots[level] + 1;
4469 c = path->nodes[level];
4471 if (slot >= btrfs_header_nritems(c)) {
4474 struct btrfs_key cur_key;
4475 if (level + 1 >= BTRFS_MAX_LEVEL ||
4476 !path->nodes[level + 1])
4479 if (path->locks[level + 1] || path->skip_locking) {
4484 slot = btrfs_header_nritems(c) - 1;
4486 btrfs_item_key_to_cpu(c, &cur_key, slot);
4488 btrfs_node_key_to_cpu(c, &cur_key, slot);
4490 orig_lowest = path->lowest_level;
4491 btrfs_release_path(path);
4492 path->lowest_level = level;
4493 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4495 path->lowest_level = orig_lowest;
4499 c = path->nodes[level];
4500 slot = path->slots[level];
4507 btrfs_item_key_to_cpu(c, key, slot);
4509 u64 gen = btrfs_node_ptr_generation(c, slot);
4511 if (gen < min_trans) {
4515 btrfs_node_key_to_cpu(c, key, slot);
4522 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
4527 struct extent_buffer *c;
4528 struct extent_buffer *next;
4529 struct btrfs_fs_info *fs_info = root->fs_info;
4530 struct btrfs_key key;
4531 bool need_commit_sem = false;
4536 nritems = btrfs_header_nritems(path->nodes[0]);
4540 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4544 btrfs_release_path(path);
4546 path->keep_locks = 1;
4549 ret = btrfs_search_old_slot(root, &key, path, time_seq);
4551 if (path->need_commit_sem) {
4552 path->need_commit_sem = 0;
4553 need_commit_sem = true;
4554 down_read(&fs_info->commit_root_sem);
4556 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4558 path->keep_locks = 0;
4563 nritems = btrfs_header_nritems(path->nodes[0]);
4565 * by releasing the path above we dropped all our locks. A balance
4566 * could have added more items next to the key that used to be
4567 * at the very end of the block. So, check again here and
4568 * advance the path if there are now more items available.
4570 if (nritems > 0 && path->slots[0] < nritems - 1) {
4577 * So the above check misses one case:
4578 * - after releasing the path above, someone has removed the item that
4579 * used to be at the very end of the block, and balance between leafs
4580 * gets another one with bigger key.offset to replace it.
4582 * This one should be returned as well, or we can get leaf corruption
4583 * later(esp. in __btrfs_drop_extents()).
4585 * And a bit more explanation about this check,
4586 * with ret > 0, the key isn't found, the path points to the slot
4587 * where it should be inserted, so the path->slots[0] item must be the
4590 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
4595 while (level < BTRFS_MAX_LEVEL) {
4596 if (!path->nodes[level]) {
4601 slot = path->slots[level] + 1;
4602 c = path->nodes[level];
4603 if (slot >= btrfs_header_nritems(c)) {
4605 if (level == BTRFS_MAX_LEVEL) {
4614 * Our current level is where we're going to start from, and to
4615 * make sure lockdep doesn't complain we need to drop our locks
4616 * and nodes from 0 to our current level.
4618 for (i = 0; i < level; i++) {
4619 if (path->locks[level]) {
4620 btrfs_tree_read_unlock(path->nodes[i]);
4623 free_extent_buffer(path->nodes[i]);
4624 path->nodes[i] = NULL;
4628 ret = read_block_for_search(root, path, &next, level,
4634 btrfs_release_path(path);
4638 if (!path->skip_locking) {
4639 ret = btrfs_try_tree_read_lock(next);
4640 if (!ret && time_seq) {
4642 * If we don't get the lock, we may be racing
4643 * with push_leaf_left, holding that lock while
4644 * itself waiting for the leaf we've currently
4645 * locked. To solve this situation, we give up
4646 * on our lock and cycle.
4648 free_extent_buffer(next);
4649 btrfs_release_path(path);
4654 btrfs_tree_read_lock(next);
4658 path->slots[level] = slot;
4661 path->nodes[level] = next;
4662 path->slots[level] = 0;
4663 if (!path->skip_locking)
4664 path->locks[level] = BTRFS_READ_LOCK;
4668 ret = read_block_for_search(root, path, &next, level,
4674 btrfs_release_path(path);
4678 if (!path->skip_locking)
4679 btrfs_tree_read_lock(next);
4683 unlock_up(path, 0, 1, 0, NULL);
4684 if (need_commit_sem) {
4687 path->need_commit_sem = 1;
4688 ret2 = finish_need_commit_sem_search(path);
4689 up_read(&fs_info->commit_root_sem);
4698 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4699 * searching until it gets past min_objectid or finds an item of 'type'
4701 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4703 int btrfs_previous_item(struct btrfs_root *root,
4704 struct btrfs_path *path, u64 min_objectid,
4707 struct btrfs_key found_key;
4708 struct extent_buffer *leaf;
4713 if (path->slots[0] == 0) {
4714 ret = btrfs_prev_leaf(root, path);
4720 leaf = path->nodes[0];
4721 nritems = btrfs_header_nritems(leaf);
4724 if (path->slots[0] == nritems)
4727 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4728 if (found_key.objectid < min_objectid)
4730 if (found_key.type == type)
4732 if (found_key.objectid == min_objectid &&
4733 found_key.type < type)
4740 * search in extent tree to find a previous Metadata/Data extent item with
4743 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4745 int btrfs_previous_extent_item(struct btrfs_root *root,
4746 struct btrfs_path *path, u64 min_objectid)
4748 struct btrfs_key found_key;
4749 struct extent_buffer *leaf;
4754 if (path->slots[0] == 0) {
4755 ret = btrfs_prev_leaf(root, path);
4761 leaf = path->nodes[0];
4762 nritems = btrfs_header_nritems(leaf);
4765 if (path->slots[0] == nritems)
4768 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4769 if (found_key.objectid < min_objectid)
4771 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4772 found_key.type == BTRFS_METADATA_ITEM_KEY)
4774 if (found_key.objectid == min_objectid &&
4775 found_key.type < BTRFS_EXTENT_ITEM_KEY)