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"
19 #include "tree-checker.h"
21 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
22 *root, struct btrfs_path *path, int level);
23 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
24 const struct btrfs_key *ins_key, struct btrfs_path *path,
25 int data_size, int extend);
26 static int push_node_left(struct btrfs_trans_handle *trans,
27 struct extent_buffer *dst,
28 struct extent_buffer *src, int empty);
29 static int balance_node_right(struct btrfs_trans_handle *trans,
30 struct extent_buffer *dst_buf,
31 struct extent_buffer *src_buf);
32 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
35 static const struct btrfs_csums {
38 const char driver[12];
40 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
41 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
42 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
43 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
44 .driver = "blake2b-256" },
47 int btrfs_super_csum_size(const struct btrfs_super_block *s)
49 u16 t = btrfs_super_csum_type(s);
51 * csum type is validated at mount time
53 return btrfs_csums[t].size;
56 const char *btrfs_super_csum_name(u16 csum_type)
58 /* csum type is validated at mount time */
59 return btrfs_csums[csum_type].name;
63 * Return driver name if defined, otherwise the name that's also a valid driver
66 const char *btrfs_super_csum_driver(u16 csum_type)
68 /* csum type is validated at mount time */
69 return btrfs_csums[csum_type].driver[0] ?
70 btrfs_csums[csum_type].driver :
71 btrfs_csums[csum_type].name;
74 size_t __attribute_const__ btrfs_get_num_csums(void)
76 return ARRAY_SIZE(btrfs_csums);
79 struct btrfs_path *btrfs_alloc_path(void)
81 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
84 /* this also releases the path */
85 void btrfs_free_path(struct btrfs_path *p)
89 btrfs_release_path(p);
90 kmem_cache_free(btrfs_path_cachep, p);
94 * path release drops references on the extent buffers in the path
95 * and it drops any locks held by this path
97 * It is safe to call this on paths that no locks or extent buffers held.
99 noinline void btrfs_release_path(struct btrfs_path *p)
103 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
108 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
111 free_extent_buffer(p->nodes[i]);
117 * safely gets a reference on the root node of a tree. A lock
118 * is not taken, so a concurrent writer may put a different node
119 * at the root of the tree. See btrfs_lock_root_node for the
122 * The extent buffer returned by this has a reference taken, so
123 * it won't disappear. It may stop being the root of the tree
124 * at any time because there are no locks held.
126 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
128 struct extent_buffer *eb;
132 eb = rcu_dereference(root->node);
135 * RCU really hurts here, we could free up the root node because
136 * it was COWed but we may not get the new root node yet so do
137 * the inc_not_zero dance and if it doesn't work then
138 * synchronize_rcu and try again.
140 if (atomic_inc_not_zero(&eb->refs)) {
151 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
152 * just get put onto a simple dirty list. Transaction walks this list to make
153 * sure they get properly updated on disk.
155 static void add_root_to_dirty_list(struct btrfs_root *root)
157 struct btrfs_fs_info *fs_info = root->fs_info;
159 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
160 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
163 spin_lock(&fs_info->trans_lock);
164 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
165 /* Want the extent tree to be the last on the list */
166 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
167 list_move_tail(&root->dirty_list,
168 &fs_info->dirty_cowonly_roots);
170 list_move(&root->dirty_list,
171 &fs_info->dirty_cowonly_roots);
173 spin_unlock(&fs_info->trans_lock);
177 * used by snapshot creation to make a copy of a root for a tree with
178 * a given objectid. The buffer with the new root node is returned in
179 * cow_ret, and this func returns zero on success or a negative error code.
181 int btrfs_copy_root(struct btrfs_trans_handle *trans,
182 struct btrfs_root *root,
183 struct extent_buffer *buf,
184 struct extent_buffer **cow_ret, u64 new_root_objectid)
186 struct btrfs_fs_info *fs_info = root->fs_info;
187 struct extent_buffer *cow;
190 struct btrfs_disk_key disk_key;
192 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
193 trans->transid != fs_info->running_transaction->transid);
194 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
195 trans->transid != root->last_trans);
197 level = btrfs_header_level(buf);
199 btrfs_item_key(buf, &disk_key, 0);
201 btrfs_node_key(buf, &disk_key, 0);
203 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
204 &disk_key, level, buf->start, 0,
205 BTRFS_NESTING_NEW_ROOT);
209 copy_extent_buffer_full(cow, buf);
210 btrfs_set_header_bytenr(cow, cow->start);
211 btrfs_set_header_generation(cow, trans->transid);
212 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
213 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
214 BTRFS_HEADER_FLAG_RELOC);
215 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
216 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
218 btrfs_set_header_owner(cow, new_root_objectid);
220 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
222 WARN_ON(btrfs_header_generation(buf) > trans->transid);
223 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
224 ret = btrfs_inc_ref(trans, root, cow, 1);
226 ret = btrfs_inc_ref(trans, root, cow, 0);
228 btrfs_tree_unlock(cow);
229 free_extent_buffer(cow);
230 btrfs_abort_transaction(trans, ret);
234 btrfs_mark_buffer_dirty(cow);
240 * check if the tree block can be shared by multiple trees
242 int btrfs_block_can_be_shared(struct btrfs_root *root,
243 struct extent_buffer *buf)
246 * Tree blocks not in shareable trees and tree roots are never shared.
247 * If a block was allocated after the last snapshot and the block was
248 * not allocated by tree relocation, we know the block is not shared.
250 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
251 buf != root->node && buf != root->commit_root &&
252 (btrfs_header_generation(buf) <=
253 btrfs_root_last_snapshot(&root->root_item) ||
254 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
260 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
261 struct btrfs_root *root,
262 struct extent_buffer *buf,
263 struct extent_buffer *cow,
266 struct btrfs_fs_info *fs_info = root->fs_info;
274 * Backrefs update rules:
276 * Always use full backrefs for extent pointers in tree block
277 * allocated by tree relocation.
279 * If a shared tree block is no longer referenced by its owner
280 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
281 * use full backrefs for extent pointers in tree block.
283 * If a tree block is been relocating
284 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
285 * use full backrefs for extent pointers in tree block.
286 * The reason for this is some operations (such as drop tree)
287 * are only allowed for blocks use full backrefs.
290 if (btrfs_block_can_be_shared(root, buf)) {
291 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
292 btrfs_header_level(buf), 1,
298 btrfs_handle_fs_error(fs_info, ret, NULL);
303 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
304 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
305 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
310 owner = btrfs_header_owner(buf);
311 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
312 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
315 if ((owner == root->root_key.objectid ||
316 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
317 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
318 ret = btrfs_inc_ref(trans, root, buf, 1);
322 if (root->root_key.objectid ==
323 BTRFS_TREE_RELOC_OBJECTID) {
324 ret = btrfs_dec_ref(trans, root, buf, 0);
327 ret = btrfs_inc_ref(trans, root, cow, 1);
331 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
334 if (root->root_key.objectid ==
335 BTRFS_TREE_RELOC_OBJECTID)
336 ret = btrfs_inc_ref(trans, root, cow, 1);
338 ret = btrfs_inc_ref(trans, root, cow, 0);
342 if (new_flags != 0) {
343 int level = btrfs_header_level(buf);
345 ret = btrfs_set_disk_extent_flags(trans, buf,
351 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
352 if (root->root_key.objectid ==
353 BTRFS_TREE_RELOC_OBJECTID)
354 ret = btrfs_inc_ref(trans, root, cow, 1);
356 ret = btrfs_inc_ref(trans, root, cow, 0);
359 ret = btrfs_dec_ref(trans, root, buf, 1);
363 btrfs_clean_tree_block(buf);
370 * does the dirty work in cow of a single block. The parent block (if
371 * supplied) is updated to point to the new cow copy. The new buffer is marked
372 * dirty and returned locked. If you modify the block it needs to be marked
375 * search_start -- an allocation hint for the new block
377 * empty_size -- a hint that you plan on doing more cow. This is the size in
378 * bytes the allocator should try to find free next to the block it returns.
379 * This is just a hint and may be ignored by the allocator.
381 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
382 struct btrfs_root *root,
383 struct extent_buffer *buf,
384 struct extent_buffer *parent, int parent_slot,
385 struct extent_buffer **cow_ret,
386 u64 search_start, u64 empty_size,
387 enum btrfs_lock_nesting nest)
389 struct btrfs_fs_info *fs_info = root->fs_info;
390 struct btrfs_disk_key disk_key;
391 struct extent_buffer *cow;
395 u64 parent_start = 0;
400 btrfs_assert_tree_write_locked(buf);
402 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
403 trans->transid != fs_info->running_transaction->transid);
404 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
405 trans->transid != root->last_trans);
407 level = btrfs_header_level(buf);
410 btrfs_item_key(buf, &disk_key, 0);
412 btrfs_node_key(buf, &disk_key, 0);
414 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
415 parent_start = parent->start;
417 cow = btrfs_alloc_tree_block(trans, root, parent_start,
418 root->root_key.objectid, &disk_key, level,
419 search_start, empty_size, nest);
423 /* cow is set to blocking by btrfs_init_new_buffer */
425 copy_extent_buffer_full(cow, buf);
426 btrfs_set_header_bytenr(cow, cow->start);
427 btrfs_set_header_generation(cow, trans->transid);
428 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
429 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
430 BTRFS_HEADER_FLAG_RELOC);
431 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
432 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
434 btrfs_set_header_owner(cow, root->root_key.objectid);
436 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
438 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
440 btrfs_tree_unlock(cow);
441 free_extent_buffer(cow);
442 btrfs_abort_transaction(trans, ret);
446 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
447 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
449 btrfs_tree_unlock(cow);
450 free_extent_buffer(cow);
451 btrfs_abort_transaction(trans, ret);
456 if (buf == root->node) {
457 WARN_ON(parent && parent != buf);
458 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
459 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
460 parent_start = buf->start;
462 atomic_inc(&cow->refs);
463 ret = btrfs_tree_mod_log_insert_root(root->node, cow, true);
465 rcu_assign_pointer(root->node, cow);
467 btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
468 parent_start, last_ref);
469 free_extent_buffer(buf);
470 add_root_to_dirty_list(root);
472 WARN_ON(trans->transid != btrfs_header_generation(parent));
473 btrfs_tree_mod_log_insert_key(parent, parent_slot,
474 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
475 btrfs_set_node_blockptr(parent, parent_slot,
477 btrfs_set_node_ptr_generation(parent, parent_slot,
479 btrfs_mark_buffer_dirty(parent);
481 ret = btrfs_tree_mod_log_free_eb(buf);
483 btrfs_tree_unlock(cow);
484 free_extent_buffer(cow);
485 btrfs_abort_transaction(trans, ret);
489 btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
490 parent_start, last_ref);
493 btrfs_tree_unlock(buf);
494 free_extent_buffer_stale(buf);
495 btrfs_mark_buffer_dirty(cow);
500 static inline int should_cow_block(struct btrfs_trans_handle *trans,
501 struct btrfs_root *root,
502 struct extent_buffer *buf)
504 if (btrfs_is_testing(root->fs_info))
507 /* Ensure we can see the FORCE_COW bit */
508 smp_mb__before_atomic();
511 * We do not need to cow a block if
512 * 1) this block is not created or changed in this transaction;
513 * 2) this block does not belong to TREE_RELOC tree;
514 * 3) the root is not forced COW.
516 * What is forced COW:
517 * when we create snapshot during committing the transaction,
518 * after we've finished copying src root, we must COW the shared
519 * block to ensure the metadata consistency.
521 if (btrfs_header_generation(buf) == trans->transid &&
522 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
523 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
524 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
525 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
531 * cows a single block, see __btrfs_cow_block for the real work.
532 * This version of it has extra checks so that a block isn't COWed more than
533 * once per transaction, as long as it hasn't been written yet
535 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
536 struct btrfs_root *root, struct extent_buffer *buf,
537 struct extent_buffer *parent, int parent_slot,
538 struct extent_buffer **cow_ret,
539 enum btrfs_lock_nesting nest)
541 struct btrfs_fs_info *fs_info = root->fs_info;
545 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
547 "COW'ing blocks on a fs root that's being dropped");
549 if (trans->transaction != fs_info->running_transaction)
550 WARN(1, KERN_CRIT "trans %llu running %llu\n",
552 fs_info->running_transaction->transid);
554 if (trans->transid != fs_info->generation)
555 WARN(1, KERN_CRIT "trans %llu running %llu\n",
556 trans->transid, fs_info->generation);
558 if (!should_cow_block(trans, root, buf)) {
563 search_start = buf->start & ~((u64)SZ_1G - 1);
566 * Before CoWing this block for later modification, check if it's
567 * the subtree root and do the delayed subtree trace if needed.
569 * Also We don't care about the error, as it's handled internally.
571 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
572 ret = __btrfs_cow_block(trans, root, buf, parent,
573 parent_slot, cow_ret, search_start, 0, nest);
575 trace_btrfs_cow_block(root, buf, *cow_ret);
579 ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
582 * helper function for defrag to decide if two blocks pointed to by a
583 * node are actually close by
585 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
587 if (blocknr < other && other - (blocknr + blocksize) < 32768)
589 if (blocknr > other && blocknr - (other + blocksize) < 32768)
594 #ifdef __LITTLE_ENDIAN
597 * Compare two keys, on little-endian the disk order is same as CPU order and
598 * we can avoid the conversion.
600 static int comp_keys(const struct btrfs_disk_key *disk_key,
601 const struct btrfs_key *k2)
603 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
605 return btrfs_comp_cpu_keys(k1, k2);
611 * compare two keys in a memcmp fashion
613 static int comp_keys(const struct btrfs_disk_key *disk,
614 const struct btrfs_key *k2)
618 btrfs_disk_key_to_cpu(&k1, disk);
620 return btrfs_comp_cpu_keys(&k1, k2);
625 * same as comp_keys only with two btrfs_key's
627 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
629 if (k1->objectid > k2->objectid)
631 if (k1->objectid < k2->objectid)
633 if (k1->type > k2->type)
635 if (k1->type < k2->type)
637 if (k1->offset > k2->offset)
639 if (k1->offset < k2->offset)
645 * this is used by the defrag code to go through all the
646 * leaves pointed to by a node and reallocate them so that
647 * disk order is close to key order
649 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root, struct extent_buffer *parent,
651 int start_slot, u64 *last_ret,
652 struct btrfs_key *progress)
654 struct btrfs_fs_info *fs_info = root->fs_info;
655 struct extent_buffer *cur;
657 u64 search_start = *last_ret;
665 int progress_passed = 0;
666 struct btrfs_disk_key disk_key;
668 WARN_ON(trans->transaction != fs_info->running_transaction);
669 WARN_ON(trans->transid != fs_info->generation);
671 parent_nritems = btrfs_header_nritems(parent);
672 blocksize = fs_info->nodesize;
673 end_slot = parent_nritems - 1;
675 if (parent_nritems <= 1)
678 for (i = start_slot; i <= end_slot; i++) {
681 btrfs_node_key(parent, &disk_key, i);
682 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
686 blocknr = btrfs_node_blockptr(parent, i);
688 last_block = blocknr;
691 other = btrfs_node_blockptr(parent, i - 1);
692 close = close_blocks(blocknr, other, blocksize);
694 if (!close && i < end_slot) {
695 other = btrfs_node_blockptr(parent, i + 1);
696 close = close_blocks(blocknr, other, blocksize);
699 last_block = blocknr;
703 cur = btrfs_read_node_slot(parent, i);
706 if (search_start == 0)
707 search_start = last_block;
709 btrfs_tree_lock(cur);
710 err = __btrfs_cow_block(trans, root, cur, parent, i,
713 (end_slot - i) * blocksize),
716 btrfs_tree_unlock(cur);
717 free_extent_buffer(cur);
720 search_start = cur->start;
721 last_block = cur->start;
722 *last_ret = search_start;
723 btrfs_tree_unlock(cur);
724 free_extent_buffer(cur);
730 * Search for a key in the given extent_buffer.
732 * The lower boundary for the search is specified by the slot number @low. Use a
733 * value of 0 to search over the whole extent buffer.
735 * The slot in the extent buffer is returned via @slot. If the key exists in the
736 * extent buffer, then @slot will point to the slot where the key is, otherwise
737 * it points to the slot where you would insert the key.
739 * Slot may point to the total number of items (i.e. one position beyond the last
740 * key) if the key is bigger than the last key in the extent buffer.
742 static noinline int generic_bin_search(struct extent_buffer *eb, int low,
743 const struct btrfs_key *key, int *slot)
747 int high = btrfs_header_nritems(eb);
749 const int key_size = sizeof(struct btrfs_disk_key);
752 btrfs_err(eb->fs_info,
753 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
754 __func__, low, high, eb->start,
755 btrfs_header_owner(eb), btrfs_header_level(eb));
759 if (btrfs_header_level(eb) == 0) {
760 p = offsetof(struct btrfs_leaf, items);
761 item_size = sizeof(struct btrfs_item);
763 p = offsetof(struct btrfs_node, ptrs);
764 item_size = sizeof(struct btrfs_key_ptr);
769 unsigned long offset;
770 struct btrfs_disk_key *tmp;
771 struct btrfs_disk_key unaligned;
774 mid = (low + high) / 2;
775 offset = p + mid * item_size;
776 oip = offset_in_page(offset);
778 if (oip + key_size <= PAGE_SIZE) {
779 const unsigned long idx = get_eb_page_index(offset);
780 char *kaddr = page_address(eb->pages[idx]);
782 oip = get_eb_offset_in_page(eb, offset);
783 tmp = (struct btrfs_disk_key *)(kaddr + oip);
785 read_extent_buffer(eb, &unaligned, offset, key_size);
789 ret = comp_keys(tmp, key);
805 * Simple binary search on an extent buffer. Works for both leaves and nodes, and
806 * always searches over the whole range of keys (slot 0 to slot 'nritems - 1').
808 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
811 return generic_bin_search(eb, 0, key, slot);
814 static void root_add_used(struct btrfs_root *root, u32 size)
816 spin_lock(&root->accounting_lock);
817 btrfs_set_root_used(&root->root_item,
818 btrfs_root_used(&root->root_item) + size);
819 spin_unlock(&root->accounting_lock);
822 static void root_sub_used(struct btrfs_root *root, u32 size)
824 spin_lock(&root->accounting_lock);
825 btrfs_set_root_used(&root->root_item,
826 btrfs_root_used(&root->root_item) - size);
827 spin_unlock(&root->accounting_lock);
830 /* given a node and slot number, this reads the blocks it points to. The
831 * extent buffer is returned with a reference taken (but unlocked).
833 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
836 int level = btrfs_header_level(parent);
837 struct extent_buffer *eb;
838 struct btrfs_key first_key;
840 if (slot < 0 || slot >= btrfs_header_nritems(parent))
841 return ERR_PTR(-ENOENT);
845 btrfs_node_key_to_cpu(parent, &first_key, slot);
846 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
847 btrfs_header_owner(parent),
848 btrfs_node_ptr_generation(parent, slot),
849 level - 1, &first_key);
852 if (!extent_buffer_uptodate(eb)) {
853 free_extent_buffer(eb);
854 return ERR_PTR(-EIO);
861 * node level balancing, used to make sure nodes are in proper order for
862 * item deletion. We balance from the top down, so we have to make sure
863 * that a deletion won't leave an node completely empty later on.
865 static noinline int balance_level(struct btrfs_trans_handle *trans,
866 struct btrfs_root *root,
867 struct btrfs_path *path, int level)
869 struct btrfs_fs_info *fs_info = root->fs_info;
870 struct extent_buffer *right = NULL;
871 struct extent_buffer *mid;
872 struct extent_buffer *left = NULL;
873 struct extent_buffer *parent = NULL;
877 int orig_slot = path->slots[level];
882 mid = path->nodes[level];
884 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK);
885 WARN_ON(btrfs_header_generation(mid) != trans->transid);
887 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
889 if (level < BTRFS_MAX_LEVEL - 1) {
890 parent = path->nodes[level + 1];
891 pslot = path->slots[level + 1];
895 * deal with the case where there is only one pointer in the root
896 * by promoting the node below to a root
899 struct extent_buffer *child;
901 if (btrfs_header_nritems(mid) != 1)
904 /* promote the child to a root */
905 child = btrfs_read_node_slot(mid, 0);
907 ret = PTR_ERR(child);
908 btrfs_handle_fs_error(fs_info, ret, NULL);
912 btrfs_tree_lock(child);
913 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
916 btrfs_tree_unlock(child);
917 free_extent_buffer(child);
921 ret = btrfs_tree_mod_log_insert_root(root->node, child, true);
923 rcu_assign_pointer(root->node, child);
925 add_root_to_dirty_list(root);
926 btrfs_tree_unlock(child);
928 path->locks[level] = 0;
929 path->nodes[level] = NULL;
930 btrfs_clean_tree_block(mid);
931 btrfs_tree_unlock(mid);
932 /* once for the path */
933 free_extent_buffer(mid);
935 root_sub_used(root, mid->len);
936 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
937 /* once for the root ptr */
938 free_extent_buffer_stale(mid);
941 if (btrfs_header_nritems(mid) >
942 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
945 left = btrfs_read_node_slot(parent, pslot - 1);
950 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
951 wret = btrfs_cow_block(trans, root, left,
952 parent, pslot - 1, &left,
953 BTRFS_NESTING_LEFT_COW);
960 right = btrfs_read_node_slot(parent, pslot + 1);
965 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
966 wret = btrfs_cow_block(trans, root, right,
967 parent, pslot + 1, &right,
968 BTRFS_NESTING_RIGHT_COW);
975 /* first, try to make some room in the middle buffer */
977 orig_slot += btrfs_header_nritems(left);
978 wret = push_node_left(trans, left, mid, 1);
984 * then try to empty the right most buffer into the middle
987 wret = push_node_left(trans, mid, right, 1);
988 if (wret < 0 && wret != -ENOSPC)
990 if (btrfs_header_nritems(right) == 0) {
991 btrfs_clean_tree_block(right);
992 btrfs_tree_unlock(right);
993 del_ptr(root, path, level + 1, pslot + 1);
994 root_sub_used(root, right->len);
995 btrfs_free_tree_block(trans, btrfs_root_id(root), right,
997 free_extent_buffer_stale(right);
1000 struct btrfs_disk_key right_key;
1001 btrfs_node_key(right, &right_key, 0);
1002 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1003 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1005 btrfs_set_node_key(parent, &right_key, pslot + 1);
1006 btrfs_mark_buffer_dirty(parent);
1009 if (btrfs_header_nritems(mid) == 1) {
1011 * we're not allowed to leave a node with one item in the
1012 * tree during a delete. A deletion from lower in the tree
1013 * could try to delete the only pointer in this node.
1014 * So, pull some keys from the left.
1015 * There has to be a left pointer at this point because
1016 * otherwise we would have pulled some pointers from the
1021 btrfs_handle_fs_error(fs_info, ret, NULL);
1024 wret = balance_node_right(trans, mid, left);
1030 wret = push_node_left(trans, left, mid, 1);
1036 if (btrfs_header_nritems(mid) == 0) {
1037 btrfs_clean_tree_block(mid);
1038 btrfs_tree_unlock(mid);
1039 del_ptr(root, path, level + 1, pslot);
1040 root_sub_used(root, mid->len);
1041 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1042 free_extent_buffer_stale(mid);
1045 /* update the parent key to reflect our changes */
1046 struct btrfs_disk_key mid_key;
1047 btrfs_node_key(mid, &mid_key, 0);
1048 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1049 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1051 btrfs_set_node_key(parent, &mid_key, pslot);
1052 btrfs_mark_buffer_dirty(parent);
1055 /* update the path */
1057 if (btrfs_header_nritems(left) > orig_slot) {
1058 atomic_inc(&left->refs);
1059 /* left was locked after cow */
1060 path->nodes[level] = left;
1061 path->slots[level + 1] -= 1;
1062 path->slots[level] = orig_slot;
1064 btrfs_tree_unlock(mid);
1065 free_extent_buffer(mid);
1068 orig_slot -= btrfs_header_nritems(left);
1069 path->slots[level] = orig_slot;
1072 /* double check we haven't messed things up */
1074 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1078 btrfs_tree_unlock(right);
1079 free_extent_buffer(right);
1082 if (path->nodes[level] != left)
1083 btrfs_tree_unlock(left);
1084 free_extent_buffer(left);
1089 /* Node balancing for insertion. Here we only split or push nodes around
1090 * when they are completely full. This is also done top down, so we
1091 * have to be pessimistic.
1093 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1094 struct btrfs_root *root,
1095 struct btrfs_path *path, int level)
1097 struct btrfs_fs_info *fs_info = root->fs_info;
1098 struct extent_buffer *right = NULL;
1099 struct extent_buffer *mid;
1100 struct extent_buffer *left = NULL;
1101 struct extent_buffer *parent = NULL;
1105 int orig_slot = path->slots[level];
1110 mid = path->nodes[level];
1111 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1113 if (level < BTRFS_MAX_LEVEL - 1) {
1114 parent = path->nodes[level + 1];
1115 pslot = path->slots[level + 1];
1121 left = btrfs_read_node_slot(parent, pslot - 1);
1125 /* first, try to make some room in the middle buffer */
1129 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
1131 left_nr = btrfs_header_nritems(left);
1132 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1135 ret = btrfs_cow_block(trans, root, left, parent,
1137 BTRFS_NESTING_LEFT_COW);
1141 wret = push_node_left(trans, left, mid, 0);
1147 struct btrfs_disk_key disk_key;
1148 orig_slot += left_nr;
1149 btrfs_node_key(mid, &disk_key, 0);
1150 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1151 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1153 btrfs_set_node_key(parent, &disk_key, pslot);
1154 btrfs_mark_buffer_dirty(parent);
1155 if (btrfs_header_nritems(left) > orig_slot) {
1156 path->nodes[level] = left;
1157 path->slots[level + 1] -= 1;
1158 path->slots[level] = orig_slot;
1159 btrfs_tree_unlock(mid);
1160 free_extent_buffer(mid);
1163 btrfs_header_nritems(left);
1164 path->slots[level] = orig_slot;
1165 btrfs_tree_unlock(left);
1166 free_extent_buffer(left);
1170 btrfs_tree_unlock(left);
1171 free_extent_buffer(left);
1173 right = btrfs_read_node_slot(parent, pslot + 1);
1178 * then try to empty the right most buffer into the middle
1183 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1185 right_nr = btrfs_header_nritems(right);
1186 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1189 ret = btrfs_cow_block(trans, root, right,
1191 &right, BTRFS_NESTING_RIGHT_COW);
1195 wret = balance_node_right(trans, right, mid);
1201 struct btrfs_disk_key disk_key;
1203 btrfs_node_key(right, &disk_key, 0);
1204 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1205 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1207 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1208 btrfs_mark_buffer_dirty(parent);
1210 if (btrfs_header_nritems(mid) <= orig_slot) {
1211 path->nodes[level] = right;
1212 path->slots[level + 1] += 1;
1213 path->slots[level] = orig_slot -
1214 btrfs_header_nritems(mid);
1215 btrfs_tree_unlock(mid);
1216 free_extent_buffer(mid);
1218 btrfs_tree_unlock(right);
1219 free_extent_buffer(right);
1223 btrfs_tree_unlock(right);
1224 free_extent_buffer(right);
1230 * readahead one full node of leaves, finding things that are close
1231 * to the block in 'slot', and triggering ra on them.
1233 static void reada_for_search(struct btrfs_fs_info *fs_info,
1234 struct btrfs_path *path,
1235 int level, int slot, u64 objectid)
1237 struct extent_buffer *node;
1238 struct btrfs_disk_key disk_key;
1248 if (level != 1 && path->reada != READA_FORWARD_ALWAYS)
1251 if (!path->nodes[level])
1254 node = path->nodes[level];
1257 * Since the time between visiting leaves is much shorter than the time
1258 * between visiting nodes, limit read ahead of nodes to 1, to avoid too
1259 * much IO at once (possibly random).
1261 if (path->reada == READA_FORWARD_ALWAYS) {
1263 nread_max = node->fs_info->nodesize;
1265 nread_max = SZ_128K;
1270 search = btrfs_node_blockptr(node, slot);
1271 blocksize = fs_info->nodesize;
1272 if (path->reada != READA_FORWARD_ALWAYS) {
1273 struct extent_buffer *eb;
1275 eb = find_extent_buffer(fs_info, search);
1277 free_extent_buffer(eb);
1284 nritems = btrfs_header_nritems(node);
1288 if (path->reada == READA_BACK) {
1292 } else if (path->reada == READA_FORWARD ||
1293 path->reada == READA_FORWARD_ALWAYS) {
1298 if (path->reada == READA_BACK && objectid) {
1299 btrfs_node_key(node, &disk_key, nr);
1300 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1303 search = btrfs_node_blockptr(node, nr);
1304 if (path->reada == READA_FORWARD_ALWAYS ||
1305 (search <= target && target - search <= 65536) ||
1306 (search > target && search - target <= 65536)) {
1307 btrfs_readahead_node_child(node, nr);
1311 if (nread > nread_max || nscan > 32)
1316 static noinline void reada_for_balance(struct btrfs_path *path, int level)
1318 struct extent_buffer *parent;
1322 parent = path->nodes[level + 1];
1326 nritems = btrfs_header_nritems(parent);
1327 slot = path->slots[level + 1];
1330 btrfs_readahead_node_child(parent, slot - 1);
1331 if (slot + 1 < nritems)
1332 btrfs_readahead_node_child(parent, slot + 1);
1337 * when we walk down the tree, it is usually safe to unlock the higher layers
1338 * in the tree. The exceptions are when our path goes through slot 0, because
1339 * operations on the tree might require changing key pointers higher up in the
1342 * callers might also have set path->keep_locks, which tells this code to keep
1343 * the lock if the path points to the last slot in the block. This is part of
1344 * walking through the tree, and selecting the next slot in the higher block.
1346 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1347 * if lowest_unlock is 1, level 0 won't be unlocked
1349 static noinline void unlock_up(struct btrfs_path *path, int level,
1350 int lowest_unlock, int min_write_lock_level,
1351 int *write_lock_level)
1354 int skip_level = level;
1355 bool check_skip = true;
1357 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1358 if (!path->nodes[i])
1360 if (!path->locks[i])
1364 if (path->slots[i] == 0) {
1369 if (path->keep_locks) {
1372 nritems = btrfs_header_nritems(path->nodes[i]);
1373 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1380 if (i >= lowest_unlock && i > skip_level) {
1382 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1384 if (write_lock_level &&
1385 i > min_write_lock_level &&
1386 i <= *write_lock_level) {
1387 *write_lock_level = i - 1;
1394 * Helper function for btrfs_search_slot() and other functions that do a search
1395 * on a btree. The goal is to find a tree block in the cache (the radix tree at
1396 * fs_info->buffer_radix), but if we can't find it, or it's not up to date, read
1397 * its pages from disk.
1399 * Returns -EAGAIN, with the path unlocked, if the caller needs to repeat the
1400 * whole btree search, starting again from the current root node.
1403 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
1404 struct extent_buffer **eb_ret, int level, int slot,
1405 const struct btrfs_key *key)
1407 struct btrfs_fs_info *fs_info = root->fs_info;
1410 struct extent_buffer *tmp;
1411 struct btrfs_key first_key;
1416 unlock_up = ((level + 1 < BTRFS_MAX_LEVEL) && p->locks[level + 1]);
1417 blocknr = btrfs_node_blockptr(*eb_ret, slot);
1418 gen = btrfs_node_ptr_generation(*eb_ret, slot);
1419 parent_level = btrfs_header_level(*eb_ret);
1420 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
1423 * If we need to read an extent buffer from disk and we are holding locks
1424 * on upper level nodes, we unlock all the upper nodes before reading the
1425 * extent buffer, and then return -EAGAIN to the caller as it needs to
1426 * restart the search. We don't release the lock on the current level
1427 * because we need to walk this node to figure out which blocks to read.
1429 tmp = find_extent_buffer(fs_info, blocknr);
1431 if (p->reada == READA_FORWARD_ALWAYS)
1432 reada_for_search(fs_info, p, level, slot, key->objectid);
1434 /* first we do an atomic uptodate check */
1435 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
1437 * Do extra check for first_key, eb can be stale due to
1438 * being cached, read from scrub, or have multiple
1439 * parents (shared tree blocks).
1441 if (btrfs_verify_level_key(tmp,
1442 parent_level - 1, &first_key, gen)) {
1443 free_extent_buffer(tmp);
1451 btrfs_unlock_up_safe(p, level + 1);
1453 /* now we're allowed to do a blocking uptodate check */
1454 ret = btrfs_read_extent_buffer(tmp, gen, parent_level - 1, &first_key);
1456 free_extent_buffer(tmp);
1457 btrfs_release_path(p);
1460 if (btrfs_check_eb_owner(tmp, root->root_key.objectid)) {
1461 free_extent_buffer(tmp);
1462 btrfs_release_path(p);
1473 btrfs_unlock_up_safe(p, level + 1);
1479 if (p->reada != READA_NONE)
1480 reada_for_search(fs_info, p, level, slot, key->objectid);
1482 tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid,
1483 gen, parent_level - 1, &first_key);
1485 btrfs_release_path(p);
1486 return PTR_ERR(tmp);
1489 * If the read above didn't mark this buffer up to date,
1490 * it will never end up being up to date. Set ret to EIO now
1491 * and give up so that our caller doesn't loop forever
1494 if (!extent_buffer_uptodate(tmp))
1501 free_extent_buffer(tmp);
1502 btrfs_release_path(p);
1509 * helper function for btrfs_search_slot. This does all of the checks
1510 * for node-level blocks and does any balancing required based on
1513 * If no extra work was required, zero is returned. If we had to
1514 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1518 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1519 struct btrfs_root *root, struct btrfs_path *p,
1520 struct extent_buffer *b, int level, int ins_len,
1521 int *write_lock_level)
1523 struct btrfs_fs_info *fs_info = root->fs_info;
1526 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1527 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1529 if (*write_lock_level < level + 1) {
1530 *write_lock_level = level + 1;
1531 btrfs_release_path(p);
1535 reada_for_balance(p, level);
1536 ret = split_node(trans, root, p, level);
1538 b = p->nodes[level];
1539 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1540 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
1542 if (*write_lock_level < level + 1) {
1543 *write_lock_level = level + 1;
1544 btrfs_release_path(p);
1548 reada_for_balance(p, level);
1549 ret = balance_level(trans, root, p, level);
1553 b = p->nodes[level];
1555 btrfs_release_path(p);
1558 BUG_ON(btrfs_header_nritems(b) == 1);
1563 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
1564 u64 iobjectid, u64 ioff, u8 key_type,
1565 struct btrfs_key *found_key)
1568 struct btrfs_key key;
1569 struct extent_buffer *eb;
1574 key.type = key_type;
1575 key.objectid = iobjectid;
1578 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1582 eb = path->nodes[0];
1583 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1584 ret = btrfs_next_leaf(fs_root, path);
1587 eb = path->nodes[0];
1590 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1591 if (found_key->type != key.type ||
1592 found_key->objectid != key.objectid)
1598 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
1599 struct btrfs_path *p,
1600 int write_lock_level)
1602 struct extent_buffer *b;
1606 if (p->search_commit_root) {
1607 b = root->commit_root;
1608 atomic_inc(&b->refs);
1609 level = btrfs_header_level(b);
1611 * Ensure that all callers have set skip_locking when
1612 * p->search_commit_root = 1.
1614 ASSERT(p->skip_locking == 1);
1619 if (p->skip_locking) {
1620 b = btrfs_root_node(root);
1621 level = btrfs_header_level(b);
1625 /* We try very hard to do read locks on the root */
1626 root_lock = BTRFS_READ_LOCK;
1629 * If the level is set to maximum, we can skip trying to get the read
1632 if (write_lock_level < BTRFS_MAX_LEVEL) {
1634 * We don't know the level of the root node until we actually
1635 * have it read locked
1637 b = btrfs_read_lock_root_node(root);
1638 level = btrfs_header_level(b);
1639 if (level > write_lock_level)
1642 /* Whoops, must trade for write lock */
1643 btrfs_tree_read_unlock(b);
1644 free_extent_buffer(b);
1647 b = btrfs_lock_root_node(root);
1648 root_lock = BTRFS_WRITE_LOCK;
1650 /* The level might have changed, check again */
1651 level = btrfs_header_level(b);
1655 * The root may have failed to write out at some point, and thus is no
1656 * longer valid, return an error in this case.
1658 if (!extent_buffer_uptodate(b)) {
1660 btrfs_tree_unlock_rw(b, root_lock);
1661 free_extent_buffer(b);
1662 return ERR_PTR(-EIO);
1665 p->nodes[level] = b;
1666 if (!p->skip_locking)
1667 p->locks[level] = root_lock;
1669 * Callers are responsible for dropping b's references.
1675 * Replace the extent buffer at the lowest level of the path with a cloned
1676 * version. The purpose is to be able to use it safely, after releasing the
1677 * commit root semaphore, even if relocation is happening in parallel, the
1678 * transaction used for relocation is committed and the extent buffer is
1679 * reallocated in the next transaction.
1681 * This is used in a context where the caller does not prevent transaction
1682 * commits from happening, either by holding a transaction handle or holding
1683 * some lock, while it's doing searches through a commit root.
1684 * At the moment it's only used for send operations.
1686 static int finish_need_commit_sem_search(struct btrfs_path *path)
1688 const int i = path->lowest_level;
1689 const int slot = path->slots[i];
1690 struct extent_buffer *lowest = path->nodes[i];
1691 struct extent_buffer *clone;
1693 ASSERT(path->need_commit_sem);
1698 lockdep_assert_held_read(&lowest->fs_info->commit_root_sem);
1700 clone = btrfs_clone_extent_buffer(lowest);
1704 btrfs_release_path(path);
1705 path->nodes[i] = clone;
1706 path->slots[i] = slot;
1711 static inline int search_for_key_slot(struct extent_buffer *eb,
1712 int search_low_slot,
1713 const struct btrfs_key *key,
1718 * If a previous call to btrfs_bin_search() on a parent node returned an
1719 * exact match (prev_cmp == 0), we can safely assume the target key will
1720 * always be at slot 0 on lower levels, since each key pointer
1721 * (struct btrfs_key_ptr) refers to the lowest key accessible from the
1722 * subtree it points to. Thus we can skip searching lower levels.
1724 if (prev_cmp == 0) {
1729 return generic_bin_search(eb, search_low_slot, key, slot);
1732 static int search_leaf(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root,
1734 const struct btrfs_key *key,
1735 struct btrfs_path *path,
1739 struct extent_buffer *leaf = path->nodes[0];
1740 int leaf_free_space = -1;
1741 int search_low_slot = 0;
1743 bool do_bin_search = true;
1746 * If we are doing an insertion, the leaf has enough free space and the
1747 * destination slot for the key is not slot 0, then we can unlock our
1748 * write lock on the parent, and any other upper nodes, before doing the
1749 * binary search on the leaf (with search_for_key_slot()), allowing other
1750 * tasks to lock the parent and any other upper nodes.
1754 * Cache the leaf free space, since we will need it later and it
1755 * will not change until then.
1757 leaf_free_space = btrfs_leaf_free_space(leaf);
1760 * !path->locks[1] means we have a single node tree, the leaf is
1761 * the root of the tree.
1763 if (path->locks[1] && leaf_free_space >= ins_len) {
1764 struct btrfs_disk_key first_key;
1766 ASSERT(btrfs_header_nritems(leaf) > 0);
1767 btrfs_item_key(leaf, &first_key, 0);
1770 * Doing the extra comparison with the first key is cheap,
1771 * taking into account that the first key is very likely
1772 * already in a cache line because it immediately follows
1773 * the extent buffer's header and we have recently accessed
1774 * the header's level field.
1776 ret = comp_keys(&first_key, key);
1779 * The first key is smaller than the key we want
1780 * to insert, so we are safe to unlock all upper
1781 * nodes and we have to do the binary search.
1783 * We do use btrfs_unlock_up_safe() and not
1784 * unlock_up() because the later does not unlock
1785 * nodes with a slot of 0 - we can safely unlock
1786 * any node even if its slot is 0 since in this
1787 * case the key does not end up at slot 0 of the
1788 * leaf and there's no need to split the leaf.
1790 btrfs_unlock_up_safe(path, 1);
1791 search_low_slot = 1;
1794 * The first key is >= then the key we want to
1795 * insert, so we can skip the binary search as
1796 * the target key will be at slot 0.
1798 * We can not unlock upper nodes when the key is
1799 * less than the first key, because we will need
1800 * to update the key at slot 0 of the parent node
1801 * and possibly of other upper nodes too.
1802 * If the key matches the first key, then we can
1803 * unlock all the upper nodes, using
1804 * btrfs_unlock_up_safe() instead of unlock_up()
1808 btrfs_unlock_up_safe(path, 1);
1810 * ret is already 0 or 1, matching the result of
1811 * a btrfs_bin_search() call, so there is no need
1814 do_bin_search = false;
1820 if (do_bin_search) {
1821 ret = search_for_key_slot(leaf, search_low_slot, key,
1822 prev_cmp, &path->slots[0]);
1829 * Item key already exists. In this case, if we are allowed to
1830 * insert the item (for example, in dir_item case, item key
1831 * collision is allowed), it will be merged with the original
1832 * item. Only the item size grows, no new btrfs item will be
1833 * added. If search_for_extension is not set, ins_len already
1834 * accounts the size btrfs_item, deduct it here so leaf space
1835 * check will be correct.
1837 if (ret == 0 && !path->search_for_extension) {
1838 ASSERT(ins_len >= sizeof(struct btrfs_item));
1839 ins_len -= sizeof(struct btrfs_item);
1842 ASSERT(leaf_free_space >= 0);
1844 if (leaf_free_space < ins_len) {
1847 err = split_leaf(trans, root, key, path, ins_len,
1850 if (WARN_ON(err > 0))
1861 * btrfs_search_slot - look for a key in a tree and perform necessary
1862 * modifications to preserve tree invariants.
1864 * @trans: Handle of transaction, used when modifying the tree
1865 * @p: Holds all btree nodes along the search path
1866 * @root: The root node of the tree
1867 * @key: The key we are looking for
1868 * @ins_len: Indicates purpose of search:
1869 * >0 for inserts it's size of item inserted (*)
1871 * 0 for plain searches, not modifying the tree
1873 * (*) If size of item inserted doesn't include
1874 * sizeof(struct btrfs_item), then p->search_for_extension must
1876 * @cow: boolean should CoW operations be performed. Must always be 1
1877 * when modifying the tree.
1879 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
1880 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
1882 * If @key is found, 0 is returned and you can find the item in the leaf level
1883 * of the path (level 0)
1885 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
1886 * points to the slot where it should be inserted
1888 * If an error is encountered while searching the tree a negative error number
1891 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1892 const struct btrfs_key *key, struct btrfs_path *p,
1893 int ins_len, int cow)
1895 struct btrfs_fs_info *fs_info = root->fs_info;
1896 struct extent_buffer *b;
1901 int lowest_unlock = 1;
1902 /* everything at write_lock_level or lower must be write locked */
1903 int write_lock_level = 0;
1904 u8 lowest_level = 0;
1905 int min_write_lock_level;
1908 lowest_level = p->lowest_level;
1909 WARN_ON(lowest_level && ins_len > 0);
1910 WARN_ON(p->nodes[0] != NULL);
1911 BUG_ON(!cow && ins_len);
1916 /* when we are removing items, we might have to go up to level
1917 * two as we update tree pointers Make sure we keep write
1918 * for those levels as well
1920 write_lock_level = 2;
1921 } else if (ins_len > 0) {
1923 * for inserting items, make sure we have a write lock on
1924 * level 1 so we can update keys
1926 write_lock_level = 1;
1930 write_lock_level = -1;
1932 if (cow && (p->keep_locks || p->lowest_level))
1933 write_lock_level = BTRFS_MAX_LEVEL;
1935 min_write_lock_level = write_lock_level;
1937 if (p->need_commit_sem) {
1938 ASSERT(p->search_commit_root);
1939 down_read(&fs_info->commit_root_sem);
1944 b = btrfs_search_slot_get_root(root, p, write_lock_level);
1953 level = btrfs_header_level(b);
1956 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
1959 * if we don't really need to cow this block
1960 * then we don't want to set the path blocking,
1961 * so we test it here
1963 if (!should_cow_block(trans, root, b))
1967 * must have write locks on this node and the
1970 if (level > write_lock_level ||
1971 (level + 1 > write_lock_level &&
1972 level + 1 < BTRFS_MAX_LEVEL &&
1973 p->nodes[level + 1])) {
1974 write_lock_level = level + 1;
1975 btrfs_release_path(p);
1980 err = btrfs_cow_block(trans, root, b, NULL, 0,
1984 err = btrfs_cow_block(trans, root, b,
1985 p->nodes[level + 1],
1986 p->slots[level + 1], &b,
1994 p->nodes[level] = b;
1997 * we have a lock on b and as long as we aren't changing
1998 * the tree, there is no way to for the items in b to change.
1999 * It is safe to drop the lock on our parent before we
2000 * go through the expensive btree search on b.
2002 * If we're inserting or deleting (ins_len != 0), then we might
2003 * be changing slot zero, which may require changing the parent.
2004 * So, we can't drop the lock until after we know which slot
2005 * we're operating on.
2007 if (!ins_len && !p->keep_locks) {
2010 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2011 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2018 ASSERT(write_lock_level >= 1);
2020 ret = search_leaf(trans, root, key, p, ins_len, prev_cmp);
2021 if (!p->search_for_split)
2022 unlock_up(p, level, lowest_unlock,
2023 min_write_lock_level, NULL);
2027 ret = search_for_key_slot(b, 0, key, prev_cmp, &slot);
2032 if (ret && slot > 0) {
2036 p->slots[level] = slot;
2037 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2045 b = p->nodes[level];
2046 slot = p->slots[level];
2049 * Slot 0 is special, if we change the key we have to update
2050 * the parent pointer which means we must have a write lock on
2053 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2054 write_lock_level = level + 1;
2055 btrfs_release_path(p);
2059 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2062 if (level == lowest_level) {
2068 err = read_block_for_search(root, p, &b, level, slot, key);
2076 if (!p->skip_locking) {
2077 level = btrfs_header_level(b);
2078 if (level <= write_lock_level) {
2080 p->locks[level] = BTRFS_WRITE_LOCK;
2082 btrfs_tree_read_lock(b);
2083 p->locks[level] = BTRFS_READ_LOCK;
2085 p->nodes[level] = b;
2090 if (ret < 0 && !p->skip_release_on_error)
2091 btrfs_release_path(p);
2093 if (p->need_commit_sem) {
2096 ret2 = finish_need_commit_sem_search(p);
2097 up_read(&fs_info->commit_root_sem);
2104 ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
2107 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2108 * current state of the tree together with the operations recorded in the tree
2109 * modification log to search for the key in a previous version of this tree, as
2110 * denoted by the time_seq parameter.
2112 * Naturally, there is no support for insert, delete or cow operations.
2114 * The resulting path and return value will be set up as if we called
2115 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2117 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2118 struct btrfs_path *p, u64 time_seq)
2120 struct btrfs_fs_info *fs_info = root->fs_info;
2121 struct extent_buffer *b;
2126 int lowest_unlock = 1;
2127 u8 lowest_level = 0;
2129 lowest_level = p->lowest_level;
2130 WARN_ON(p->nodes[0] != NULL);
2132 if (p->search_commit_root) {
2134 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2138 b = btrfs_get_old_root(root, time_seq);
2143 level = btrfs_header_level(b);
2144 p->locks[level] = BTRFS_READ_LOCK;
2149 level = btrfs_header_level(b);
2150 p->nodes[level] = b;
2153 * we have a lock on b and as long as we aren't changing
2154 * the tree, there is no way to for the items in b to change.
2155 * It is safe to drop the lock on our parent before we
2156 * go through the expensive btree search on b.
2158 btrfs_unlock_up_safe(p, level + 1);
2160 ret = btrfs_bin_search(b, key, &slot);
2165 p->slots[level] = slot;
2166 unlock_up(p, level, lowest_unlock, 0, NULL);
2170 if (ret && slot > 0) {
2174 p->slots[level] = slot;
2175 unlock_up(p, level, lowest_unlock, 0, NULL);
2177 if (level == lowest_level) {
2183 err = read_block_for_search(root, p, &b, level, slot, key);
2191 level = btrfs_header_level(b);
2192 btrfs_tree_read_lock(b);
2193 b = btrfs_tree_mod_log_rewind(fs_info, p, b, time_seq);
2198 p->locks[level] = BTRFS_READ_LOCK;
2199 p->nodes[level] = b;
2204 btrfs_release_path(p);
2210 * helper to use instead of search slot if no exact match is needed but
2211 * instead the next or previous item should be returned.
2212 * When find_higher is true, the next higher item is returned, the next lower
2214 * When return_any and find_higher are both true, and no higher item is found,
2215 * return the next lower instead.
2216 * When return_any is true and find_higher is false, and no lower item is found,
2217 * return the next higher instead.
2218 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2221 int btrfs_search_slot_for_read(struct btrfs_root *root,
2222 const struct btrfs_key *key,
2223 struct btrfs_path *p, int find_higher,
2227 struct extent_buffer *leaf;
2230 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2234 * a return value of 1 means the path is at the position where the
2235 * item should be inserted. Normally this is the next bigger item,
2236 * but in case the previous item is the last in a leaf, path points
2237 * to the first free slot in the previous leaf, i.e. at an invalid
2243 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2244 ret = btrfs_next_leaf(root, p);
2250 * no higher item found, return the next
2255 btrfs_release_path(p);
2259 if (p->slots[0] == 0) {
2260 ret = btrfs_prev_leaf(root, p);
2265 if (p->slots[0] == btrfs_header_nritems(leaf))
2272 * no lower item found, return the next
2277 btrfs_release_path(p);
2287 * Execute search and call btrfs_previous_item to traverse backwards if the item
2290 * Return 0 if found, 1 if not found and < 0 if error.
2292 int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
2293 struct btrfs_path *path)
2297 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2299 ret = btrfs_previous_item(root, path, key->objectid, key->type);
2302 btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]);
2308 * Search for a valid slot for the given path.
2310 * @root: The root node of the tree.
2311 * @key: Will contain a valid item if found.
2312 * @path: The starting point to validate the slot.
2314 * Return: 0 if the item is valid
2318 int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
2319 struct btrfs_path *path)
2323 const int slot = path->slots[0];
2324 const struct extent_buffer *leaf = path->nodes[0];
2326 /* This is where we start walking the path. */
2327 if (slot >= btrfs_header_nritems(leaf)) {
2329 * If we've reached the last slot in this leaf we need
2330 * to go to the next leaf and reset the path.
2332 ret = btrfs_next_leaf(root, path);
2337 /* Store the found, valid item in @key. */
2338 btrfs_item_key_to_cpu(leaf, key, slot);
2345 * adjust the pointers going up the tree, starting at level
2346 * making sure the right key of each node is points to 'key'.
2347 * This is used after shifting pointers to the left, so it stops
2348 * fixing up pointers when a given leaf/node is not in slot 0 of the
2352 static void fixup_low_keys(struct btrfs_path *path,
2353 struct btrfs_disk_key *key, int level)
2356 struct extent_buffer *t;
2359 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2360 int tslot = path->slots[i];
2362 if (!path->nodes[i])
2365 ret = btrfs_tree_mod_log_insert_key(t, tslot,
2366 BTRFS_MOD_LOG_KEY_REPLACE, GFP_ATOMIC);
2368 btrfs_set_node_key(t, key, tslot);
2369 btrfs_mark_buffer_dirty(path->nodes[i]);
2378 * This function isn't completely safe. It's the caller's responsibility
2379 * that the new key won't break the order
2381 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2382 struct btrfs_path *path,
2383 const struct btrfs_key *new_key)
2385 struct btrfs_disk_key disk_key;
2386 struct extent_buffer *eb;
2389 eb = path->nodes[0];
2390 slot = path->slots[0];
2392 btrfs_item_key(eb, &disk_key, slot - 1);
2393 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
2395 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2396 slot, btrfs_disk_key_objectid(&disk_key),
2397 btrfs_disk_key_type(&disk_key),
2398 btrfs_disk_key_offset(&disk_key),
2399 new_key->objectid, new_key->type,
2401 btrfs_print_leaf(eb);
2405 if (slot < btrfs_header_nritems(eb) - 1) {
2406 btrfs_item_key(eb, &disk_key, slot + 1);
2407 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
2409 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2410 slot, btrfs_disk_key_objectid(&disk_key),
2411 btrfs_disk_key_type(&disk_key),
2412 btrfs_disk_key_offset(&disk_key),
2413 new_key->objectid, new_key->type,
2415 btrfs_print_leaf(eb);
2420 btrfs_cpu_key_to_disk(&disk_key, new_key);
2421 btrfs_set_item_key(eb, &disk_key, slot);
2422 btrfs_mark_buffer_dirty(eb);
2424 fixup_low_keys(path, &disk_key, 1);
2428 * Check key order of two sibling extent buffers.
2430 * Return true if something is wrong.
2431 * Return false if everything is fine.
2433 * Tree-checker only works inside one tree block, thus the following
2434 * corruption can not be detected by tree-checker:
2436 * Leaf @left | Leaf @right
2437 * --------------------------------------------------------------
2438 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
2440 * Key f6 in leaf @left itself is valid, but not valid when the next
2441 * key in leaf @right is 7.
2442 * This can only be checked at tree block merge time.
2443 * And since tree checker has ensured all key order in each tree block
2444 * is correct, we only need to bother the last key of @left and the first
2447 static bool check_sibling_keys(struct extent_buffer *left,
2448 struct extent_buffer *right)
2450 struct btrfs_key left_last;
2451 struct btrfs_key right_first;
2452 int level = btrfs_header_level(left);
2453 int nr_left = btrfs_header_nritems(left);
2454 int nr_right = btrfs_header_nritems(right);
2456 /* No key to check in one of the tree blocks */
2457 if (!nr_left || !nr_right)
2461 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
2462 btrfs_node_key_to_cpu(right, &right_first, 0);
2464 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
2465 btrfs_item_key_to_cpu(right, &right_first, 0);
2468 if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
2469 btrfs_crit(left->fs_info,
2470 "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
2471 left_last.objectid, left_last.type,
2472 left_last.offset, right_first.objectid,
2473 right_first.type, right_first.offset);
2480 * try to push data from one node into the next node left in the
2483 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2484 * error, and > 0 if there was no room in the left hand block.
2486 static int push_node_left(struct btrfs_trans_handle *trans,
2487 struct extent_buffer *dst,
2488 struct extent_buffer *src, int empty)
2490 struct btrfs_fs_info *fs_info = trans->fs_info;
2496 src_nritems = btrfs_header_nritems(src);
2497 dst_nritems = btrfs_header_nritems(dst);
2498 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2499 WARN_ON(btrfs_header_generation(src) != trans->transid);
2500 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2502 if (!empty && src_nritems <= 8)
2505 if (push_items <= 0)
2509 push_items = min(src_nritems, push_items);
2510 if (push_items < src_nritems) {
2511 /* leave at least 8 pointers in the node if
2512 * we aren't going to empty it
2514 if (src_nritems - push_items < 8) {
2515 if (push_items <= 8)
2521 push_items = min(src_nritems - 8, push_items);
2523 /* dst is the left eb, src is the middle eb */
2524 if (check_sibling_keys(dst, src)) {
2526 btrfs_abort_transaction(trans, ret);
2529 ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
2531 btrfs_abort_transaction(trans, ret);
2534 copy_extent_buffer(dst, src,
2535 btrfs_node_key_ptr_offset(dst_nritems),
2536 btrfs_node_key_ptr_offset(0),
2537 push_items * sizeof(struct btrfs_key_ptr));
2539 if (push_items < src_nritems) {
2541 * Don't call btrfs_tree_mod_log_insert_move() here, key removal
2542 * was already fully logged by btrfs_tree_mod_log_eb_copy() above.
2544 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2545 btrfs_node_key_ptr_offset(push_items),
2546 (src_nritems - push_items) *
2547 sizeof(struct btrfs_key_ptr));
2549 btrfs_set_header_nritems(src, src_nritems - push_items);
2550 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2551 btrfs_mark_buffer_dirty(src);
2552 btrfs_mark_buffer_dirty(dst);
2558 * try to push data from one node into the next node right in the
2561 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2562 * error, and > 0 if there was no room in the right hand block.
2564 * this will only push up to 1/2 the contents of the left node over
2566 static int balance_node_right(struct btrfs_trans_handle *trans,
2567 struct extent_buffer *dst,
2568 struct extent_buffer *src)
2570 struct btrfs_fs_info *fs_info = trans->fs_info;
2577 WARN_ON(btrfs_header_generation(src) != trans->transid);
2578 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2580 src_nritems = btrfs_header_nritems(src);
2581 dst_nritems = btrfs_header_nritems(dst);
2582 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2583 if (push_items <= 0)
2586 if (src_nritems < 4)
2589 max_push = src_nritems / 2 + 1;
2590 /* don't try to empty the node */
2591 if (max_push >= src_nritems)
2594 if (max_push < push_items)
2595 push_items = max_push;
2597 /* dst is the right eb, src is the middle eb */
2598 if (check_sibling_keys(src, dst)) {
2600 btrfs_abort_transaction(trans, ret);
2603 ret = btrfs_tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
2605 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2606 btrfs_node_key_ptr_offset(0),
2608 sizeof(struct btrfs_key_ptr));
2610 ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
2613 btrfs_abort_transaction(trans, ret);
2616 copy_extent_buffer(dst, src,
2617 btrfs_node_key_ptr_offset(0),
2618 btrfs_node_key_ptr_offset(src_nritems - push_items),
2619 push_items * sizeof(struct btrfs_key_ptr));
2621 btrfs_set_header_nritems(src, src_nritems - push_items);
2622 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2624 btrfs_mark_buffer_dirty(src);
2625 btrfs_mark_buffer_dirty(dst);
2631 * helper function to insert a new root level in the tree.
2632 * A new node is allocated, and a single item is inserted to
2633 * point to the existing root
2635 * returns zero on success or < 0 on failure.
2637 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2638 struct btrfs_root *root,
2639 struct btrfs_path *path, int level)
2641 struct btrfs_fs_info *fs_info = root->fs_info;
2643 struct extent_buffer *lower;
2644 struct extent_buffer *c;
2645 struct extent_buffer *old;
2646 struct btrfs_disk_key lower_key;
2649 BUG_ON(path->nodes[level]);
2650 BUG_ON(path->nodes[level-1] != root->node);
2652 lower = path->nodes[level-1];
2654 btrfs_item_key(lower, &lower_key, 0);
2656 btrfs_node_key(lower, &lower_key, 0);
2658 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
2659 &lower_key, level, root->node->start, 0,
2660 BTRFS_NESTING_NEW_ROOT);
2664 root_add_used(root, fs_info->nodesize);
2666 btrfs_set_header_nritems(c, 1);
2667 btrfs_set_node_key(c, &lower_key, 0);
2668 btrfs_set_node_blockptr(c, 0, lower->start);
2669 lower_gen = btrfs_header_generation(lower);
2670 WARN_ON(lower_gen != trans->transid);
2672 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2674 btrfs_mark_buffer_dirty(c);
2677 ret = btrfs_tree_mod_log_insert_root(root->node, c, false);
2679 rcu_assign_pointer(root->node, c);
2681 /* the super has an extra ref to root->node */
2682 free_extent_buffer(old);
2684 add_root_to_dirty_list(root);
2685 atomic_inc(&c->refs);
2686 path->nodes[level] = c;
2687 path->locks[level] = BTRFS_WRITE_LOCK;
2688 path->slots[level] = 0;
2693 * worker function to insert a single pointer in a node.
2694 * the node should have enough room for the pointer already
2696 * slot and level indicate where you want the key to go, and
2697 * blocknr is the block the key points to.
2699 static void insert_ptr(struct btrfs_trans_handle *trans,
2700 struct btrfs_path *path,
2701 struct btrfs_disk_key *key, u64 bytenr,
2702 int slot, int level)
2704 struct extent_buffer *lower;
2708 BUG_ON(!path->nodes[level]);
2709 btrfs_assert_tree_write_locked(path->nodes[level]);
2710 lower = path->nodes[level];
2711 nritems = btrfs_header_nritems(lower);
2712 BUG_ON(slot > nritems);
2713 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
2714 if (slot != nritems) {
2716 ret = btrfs_tree_mod_log_insert_move(lower, slot + 1,
2717 slot, nritems - slot);
2720 memmove_extent_buffer(lower,
2721 btrfs_node_key_ptr_offset(slot + 1),
2722 btrfs_node_key_ptr_offset(slot),
2723 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2726 ret = btrfs_tree_mod_log_insert_key(lower, slot,
2727 BTRFS_MOD_LOG_KEY_ADD, GFP_NOFS);
2730 btrfs_set_node_key(lower, key, slot);
2731 btrfs_set_node_blockptr(lower, slot, bytenr);
2732 WARN_ON(trans->transid == 0);
2733 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2734 btrfs_set_header_nritems(lower, nritems + 1);
2735 btrfs_mark_buffer_dirty(lower);
2739 * split the node at the specified level in path in two.
2740 * The path is corrected to point to the appropriate node after the split
2742 * Before splitting this tries to make some room in the node by pushing
2743 * left and right, if either one works, it returns right away.
2745 * returns 0 on success and < 0 on failure
2747 static noinline int split_node(struct btrfs_trans_handle *trans,
2748 struct btrfs_root *root,
2749 struct btrfs_path *path, int level)
2751 struct btrfs_fs_info *fs_info = root->fs_info;
2752 struct extent_buffer *c;
2753 struct extent_buffer *split;
2754 struct btrfs_disk_key disk_key;
2759 c = path->nodes[level];
2760 WARN_ON(btrfs_header_generation(c) != trans->transid);
2761 if (c == root->node) {
2763 * trying to split the root, lets make a new one
2765 * tree mod log: We don't log_removal old root in
2766 * insert_new_root, because that root buffer will be kept as a
2767 * normal node. We are going to log removal of half of the
2768 * elements below with btrfs_tree_mod_log_eb_copy(). We're
2769 * holding a tree lock on the buffer, which is why we cannot
2770 * race with other tree_mod_log users.
2772 ret = insert_new_root(trans, root, path, level + 1);
2776 ret = push_nodes_for_insert(trans, root, path, level);
2777 c = path->nodes[level];
2778 if (!ret && btrfs_header_nritems(c) <
2779 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
2785 c_nritems = btrfs_header_nritems(c);
2786 mid = (c_nritems + 1) / 2;
2787 btrfs_node_key(c, &disk_key, mid);
2789 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
2790 &disk_key, level, c->start, 0,
2791 BTRFS_NESTING_SPLIT);
2793 return PTR_ERR(split);
2795 root_add_used(root, fs_info->nodesize);
2796 ASSERT(btrfs_header_level(c) == level);
2798 ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
2800 btrfs_abort_transaction(trans, ret);
2803 copy_extent_buffer(split, c,
2804 btrfs_node_key_ptr_offset(0),
2805 btrfs_node_key_ptr_offset(mid),
2806 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2807 btrfs_set_header_nritems(split, c_nritems - mid);
2808 btrfs_set_header_nritems(c, mid);
2810 btrfs_mark_buffer_dirty(c);
2811 btrfs_mark_buffer_dirty(split);
2813 insert_ptr(trans, path, &disk_key, split->start,
2814 path->slots[level + 1] + 1, level + 1);
2816 if (path->slots[level] >= mid) {
2817 path->slots[level] -= mid;
2818 btrfs_tree_unlock(c);
2819 free_extent_buffer(c);
2820 path->nodes[level] = split;
2821 path->slots[level + 1] += 1;
2823 btrfs_tree_unlock(split);
2824 free_extent_buffer(split);
2830 * how many bytes are required to store the items in a leaf. start
2831 * and nr indicate which items in the leaf to check. This totals up the
2832 * space used both by the item structs and the item data
2834 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2837 int nritems = btrfs_header_nritems(l);
2838 int end = min(nritems, start + nr) - 1;
2842 data_len = btrfs_item_offset(l, start) + btrfs_item_size(l, start);
2843 data_len = data_len - btrfs_item_offset(l, end);
2844 data_len += sizeof(struct btrfs_item) * nr;
2845 WARN_ON(data_len < 0);
2850 * The space between the end of the leaf items and
2851 * the start of the leaf data. IOW, how much room
2852 * the leaf has left for both items and data
2854 noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
2856 struct btrfs_fs_info *fs_info = leaf->fs_info;
2857 int nritems = btrfs_header_nritems(leaf);
2860 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
2863 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
2865 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
2866 leaf_space_used(leaf, 0, nritems), nritems);
2872 * min slot controls the lowest index we're willing to push to the
2873 * right. We'll push up to and including min_slot, but no lower
2875 static noinline int __push_leaf_right(struct btrfs_path *path,
2876 int data_size, int empty,
2877 struct extent_buffer *right,
2878 int free_space, u32 left_nritems,
2881 struct btrfs_fs_info *fs_info = right->fs_info;
2882 struct extent_buffer *left = path->nodes[0];
2883 struct extent_buffer *upper = path->nodes[1];
2884 struct btrfs_map_token token;
2885 struct btrfs_disk_key disk_key;
2898 nr = max_t(u32, 1, min_slot);
2900 if (path->slots[0] >= left_nritems)
2901 push_space += data_size;
2903 slot = path->slots[1];
2904 i = left_nritems - 1;
2906 if (!empty && push_items > 0) {
2907 if (path->slots[0] > i)
2909 if (path->slots[0] == i) {
2910 int space = btrfs_leaf_free_space(left);
2912 if (space + push_space * 2 > free_space)
2917 if (path->slots[0] == i)
2918 push_space += data_size;
2920 this_item_size = btrfs_item_size(left, i);
2921 if (this_item_size + sizeof(struct btrfs_item) +
2922 push_space > free_space)
2926 push_space += this_item_size + sizeof(struct btrfs_item);
2932 if (push_items == 0)
2935 WARN_ON(!empty && push_items == left_nritems);
2937 /* push left to right */
2938 right_nritems = btrfs_header_nritems(right);
2940 push_space = btrfs_item_data_end(left, left_nritems - push_items);
2941 push_space -= leaf_data_end(left);
2943 /* make room in the right data area */
2944 data_end = leaf_data_end(right);
2945 memmove_extent_buffer(right,
2946 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
2947 BTRFS_LEAF_DATA_OFFSET + data_end,
2948 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
2950 /* copy from the left data area */
2951 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
2952 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
2953 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
2956 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2957 btrfs_item_nr_offset(0),
2958 right_nritems * sizeof(struct btrfs_item));
2960 /* copy the items from left to right */
2961 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2962 btrfs_item_nr_offset(left_nritems - push_items),
2963 push_items * sizeof(struct btrfs_item));
2965 /* update the item pointers */
2966 btrfs_init_map_token(&token, right);
2967 right_nritems += push_items;
2968 btrfs_set_header_nritems(right, right_nritems);
2969 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
2970 for (i = 0; i < right_nritems; i++) {
2971 push_space -= btrfs_token_item_size(&token, i);
2972 btrfs_set_token_item_offset(&token, i, push_space);
2975 left_nritems -= push_items;
2976 btrfs_set_header_nritems(left, left_nritems);
2979 btrfs_mark_buffer_dirty(left);
2981 btrfs_clean_tree_block(left);
2983 btrfs_mark_buffer_dirty(right);
2985 btrfs_item_key(right, &disk_key, 0);
2986 btrfs_set_node_key(upper, &disk_key, slot + 1);
2987 btrfs_mark_buffer_dirty(upper);
2989 /* then fixup the leaf pointer in the path */
2990 if (path->slots[0] >= left_nritems) {
2991 path->slots[0] -= left_nritems;
2992 if (btrfs_header_nritems(path->nodes[0]) == 0)
2993 btrfs_clean_tree_block(path->nodes[0]);
2994 btrfs_tree_unlock(path->nodes[0]);
2995 free_extent_buffer(path->nodes[0]);
2996 path->nodes[0] = right;
2997 path->slots[1] += 1;
2999 btrfs_tree_unlock(right);
3000 free_extent_buffer(right);
3005 btrfs_tree_unlock(right);
3006 free_extent_buffer(right);
3011 * push some data in the path leaf to the right, trying to free up at
3012 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3014 * returns 1 if the push failed because the other node didn't have enough
3015 * room, 0 if everything worked out and < 0 if there were major errors.
3017 * this will push starting from min_slot to the end of the leaf. It won't
3018 * push any slot lower than min_slot
3020 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3021 *root, struct btrfs_path *path,
3022 int min_data_size, int data_size,
3023 int empty, u32 min_slot)
3025 struct extent_buffer *left = path->nodes[0];
3026 struct extent_buffer *right;
3027 struct extent_buffer *upper;
3033 if (!path->nodes[1])
3036 slot = path->slots[1];
3037 upper = path->nodes[1];
3038 if (slot >= btrfs_header_nritems(upper) - 1)
3041 btrfs_assert_tree_write_locked(path->nodes[1]);
3043 right = btrfs_read_node_slot(upper, slot + 1);
3045 * slot + 1 is not valid or we fail to read the right node,
3046 * no big deal, just return.
3051 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
3053 free_space = btrfs_leaf_free_space(right);
3054 if (free_space < data_size)
3057 ret = btrfs_cow_block(trans, root, right, upper,
3058 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
3062 left_nritems = btrfs_header_nritems(left);
3063 if (left_nritems == 0)
3066 if (check_sibling_keys(left, right)) {
3068 btrfs_tree_unlock(right);
3069 free_extent_buffer(right);
3072 if (path->slots[0] == left_nritems && !empty) {
3073 /* Key greater than all keys in the leaf, right neighbor has
3074 * enough room for it and we're not emptying our leaf to delete
3075 * it, therefore use right neighbor to insert the new item and
3076 * no need to touch/dirty our left leaf. */
3077 btrfs_tree_unlock(left);
3078 free_extent_buffer(left);
3079 path->nodes[0] = right;
3085 return __push_leaf_right(path, min_data_size, empty,
3086 right, free_space, left_nritems, min_slot);
3088 btrfs_tree_unlock(right);
3089 free_extent_buffer(right);
3094 * push some data in the path leaf to the left, trying to free up at
3095 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3097 * max_slot can put a limit on how far into the leaf we'll push items. The
3098 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3101 static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
3102 int empty, struct extent_buffer *left,
3103 int free_space, u32 right_nritems,
3106 struct btrfs_fs_info *fs_info = left->fs_info;
3107 struct btrfs_disk_key disk_key;
3108 struct extent_buffer *right = path->nodes[0];
3112 u32 old_left_nritems;
3116 u32 old_left_item_size;
3117 struct btrfs_map_token token;
3120 nr = min(right_nritems, max_slot);
3122 nr = min(right_nritems - 1, max_slot);
3124 for (i = 0; i < nr; i++) {
3125 if (!empty && push_items > 0) {
3126 if (path->slots[0] < i)
3128 if (path->slots[0] == i) {
3129 int space = btrfs_leaf_free_space(right);
3131 if (space + push_space * 2 > free_space)
3136 if (path->slots[0] == i)
3137 push_space += data_size;
3139 this_item_size = btrfs_item_size(right, i);
3140 if (this_item_size + sizeof(struct btrfs_item) + push_space >
3145 push_space += this_item_size + sizeof(struct btrfs_item);
3148 if (push_items == 0) {
3152 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3154 /* push data from right to left */
3155 copy_extent_buffer(left, right,
3156 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3157 btrfs_item_nr_offset(0),
3158 push_items * sizeof(struct btrfs_item));
3160 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3161 btrfs_item_offset(right, push_items - 1);
3163 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3164 leaf_data_end(left) - push_space,
3165 BTRFS_LEAF_DATA_OFFSET +
3166 btrfs_item_offset(right, push_items - 1),
3168 old_left_nritems = btrfs_header_nritems(left);
3169 BUG_ON(old_left_nritems <= 0);
3171 btrfs_init_map_token(&token, left);
3172 old_left_item_size = btrfs_item_offset(left, old_left_nritems - 1);
3173 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3176 ioff = btrfs_token_item_offset(&token, i);
3177 btrfs_set_token_item_offset(&token, i,
3178 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
3180 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3182 /* fixup right node */
3183 if (push_items > right_nritems)
3184 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3187 if (push_items < right_nritems) {
3188 push_space = btrfs_item_offset(right, push_items - 1) -
3189 leaf_data_end(right);
3190 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
3191 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3192 BTRFS_LEAF_DATA_OFFSET +
3193 leaf_data_end(right), push_space);
3195 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3196 btrfs_item_nr_offset(push_items),
3197 (btrfs_header_nritems(right) - push_items) *
3198 sizeof(struct btrfs_item));
3201 btrfs_init_map_token(&token, right);
3202 right_nritems -= push_items;
3203 btrfs_set_header_nritems(right, right_nritems);
3204 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3205 for (i = 0; i < right_nritems; i++) {
3206 push_space = push_space - btrfs_token_item_size(&token, i);
3207 btrfs_set_token_item_offset(&token, i, push_space);
3210 btrfs_mark_buffer_dirty(left);
3212 btrfs_mark_buffer_dirty(right);
3214 btrfs_clean_tree_block(right);
3216 btrfs_item_key(right, &disk_key, 0);
3217 fixup_low_keys(path, &disk_key, 1);
3219 /* then fixup the leaf pointer in the path */
3220 if (path->slots[0] < push_items) {
3221 path->slots[0] += old_left_nritems;
3222 btrfs_tree_unlock(path->nodes[0]);
3223 free_extent_buffer(path->nodes[0]);
3224 path->nodes[0] = left;
3225 path->slots[1] -= 1;
3227 btrfs_tree_unlock(left);
3228 free_extent_buffer(left);
3229 path->slots[0] -= push_items;
3231 BUG_ON(path->slots[0] < 0);
3234 btrfs_tree_unlock(left);
3235 free_extent_buffer(left);
3240 * push some data in the path leaf to the left, trying to free up at
3241 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3243 * max_slot can put a limit on how far into the leaf we'll push items. The
3244 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3247 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3248 *root, struct btrfs_path *path, int min_data_size,
3249 int data_size, int empty, u32 max_slot)
3251 struct extent_buffer *right = path->nodes[0];
3252 struct extent_buffer *left;
3258 slot = path->slots[1];
3261 if (!path->nodes[1])
3264 right_nritems = btrfs_header_nritems(right);
3265 if (right_nritems == 0)
3268 btrfs_assert_tree_write_locked(path->nodes[1]);
3270 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
3272 * slot - 1 is not valid or we fail to read the left node,
3273 * no big deal, just return.
3278 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
3280 free_space = btrfs_leaf_free_space(left);
3281 if (free_space < data_size) {
3286 ret = btrfs_cow_block(trans, root, left,
3287 path->nodes[1], slot - 1, &left,
3288 BTRFS_NESTING_LEFT_COW);
3290 /* we hit -ENOSPC, but it isn't fatal here */
3296 if (check_sibling_keys(left, right)) {
3300 return __push_leaf_left(path, min_data_size,
3301 empty, left, free_space, right_nritems,
3304 btrfs_tree_unlock(left);
3305 free_extent_buffer(left);
3310 * split the path's leaf in two, making sure there is at least data_size
3311 * available for the resulting leaf level of the path.
3313 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3314 struct btrfs_path *path,
3315 struct extent_buffer *l,
3316 struct extent_buffer *right,
3317 int slot, int mid, int nritems)
3319 struct btrfs_fs_info *fs_info = trans->fs_info;
3323 struct btrfs_disk_key disk_key;
3324 struct btrfs_map_token token;
3326 nritems = nritems - mid;
3327 btrfs_set_header_nritems(right, nritems);
3328 data_copy_size = btrfs_item_data_end(l, mid) - leaf_data_end(l);
3330 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3331 btrfs_item_nr_offset(mid),
3332 nritems * sizeof(struct btrfs_item));
3334 copy_extent_buffer(right, l,
3335 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
3336 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
3337 leaf_data_end(l), data_copy_size);
3339 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_data_end(l, mid);
3341 btrfs_init_map_token(&token, right);
3342 for (i = 0; i < nritems; i++) {
3345 ioff = btrfs_token_item_offset(&token, i);
3346 btrfs_set_token_item_offset(&token, i, ioff + rt_data_off);
3349 btrfs_set_header_nritems(l, mid);
3350 btrfs_item_key(right, &disk_key, 0);
3351 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
3353 btrfs_mark_buffer_dirty(right);
3354 btrfs_mark_buffer_dirty(l);
3355 BUG_ON(path->slots[0] != slot);
3358 btrfs_tree_unlock(path->nodes[0]);
3359 free_extent_buffer(path->nodes[0]);
3360 path->nodes[0] = right;
3361 path->slots[0] -= mid;
3362 path->slots[1] += 1;
3364 btrfs_tree_unlock(right);
3365 free_extent_buffer(right);
3368 BUG_ON(path->slots[0] < 0);
3372 * double splits happen when we need to insert a big item in the middle
3373 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3374 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3377 * We avoid this by trying to push the items on either side of our target
3378 * into the adjacent leaves. If all goes well we can avoid the double split
3381 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3382 struct btrfs_root *root,
3383 struct btrfs_path *path,
3390 int space_needed = data_size;
3392 slot = path->slots[0];
3393 if (slot < btrfs_header_nritems(path->nodes[0]))
3394 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3397 * try to push all the items after our slot into the
3400 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
3407 nritems = btrfs_header_nritems(path->nodes[0]);
3409 * our goal is to get our slot at the start or end of a leaf. If
3410 * we've done so we're done
3412 if (path->slots[0] == 0 || path->slots[0] == nritems)
3415 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3418 /* try to push all the items before our slot into the next leaf */
3419 slot = path->slots[0];
3420 space_needed = data_size;
3422 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3423 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
3436 * split the path's leaf in two, making sure there is at least data_size
3437 * available for the resulting leaf level of the path.
3439 * returns 0 if all went well and < 0 on failure.
3441 static noinline int split_leaf(struct btrfs_trans_handle *trans,
3442 struct btrfs_root *root,
3443 const struct btrfs_key *ins_key,
3444 struct btrfs_path *path, int data_size,
3447 struct btrfs_disk_key disk_key;
3448 struct extent_buffer *l;
3452 struct extent_buffer *right;
3453 struct btrfs_fs_info *fs_info = root->fs_info;
3457 int num_doubles = 0;
3458 int tried_avoid_double = 0;
3461 slot = path->slots[0];
3462 if (extend && data_size + btrfs_item_size(l, slot) +
3463 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
3466 /* first try to make some room by pushing left and right */
3467 if (data_size && path->nodes[1]) {
3468 int space_needed = data_size;
3470 if (slot < btrfs_header_nritems(l))
3471 space_needed -= btrfs_leaf_free_space(l);
3473 wret = push_leaf_right(trans, root, path, space_needed,
3474 space_needed, 0, 0);
3478 space_needed = data_size;
3480 space_needed -= btrfs_leaf_free_space(l);
3481 wret = push_leaf_left(trans, root, path, space_needed,
3482 space_needed, 0, (u32)-1);
3488 /* did the pushes work? */
3489 if (btrfs_leaf_free_space(l) >= data_size)
3493 if (!path->nodes[1]) {
3494 ret = insert_new_root(trans, root, path, 1);
3501 slot = path->slots[0];
3502 nritems = btrfs_header_nritems(l);
3503 mid = (nritems + 1) / 2;
3507 leaf_space_used(l, mid, nritems - mid) + data_size >
3508 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3509 if (slot >= nritems) {
3513 if (mid != nritems &&
3514 leaf_space_used(l, mid, nritems - mid) +
3515 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3516 if (data_size && !tried_avoid_double)
3517 goto push_for_double;
3523 if (leaf_space_used(l, 0, mid) + data_size >
3524 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3525 if (!extend && data_size && slot == 0) {
3527 } else if ((extend || !data_size) && slot == 0) {
3531 if (mid != nritems &&
3532 leaf_space_used(l, mid, nritems - mid) +
3533 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3534 if (data_size && !tried_avoid_double)
3535 goto push_for_double;
3543 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3545 btrfs_item_key(l, &disk_key, mid);
3548 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
3549 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
3550 * subclasses, which is 8 at the time of this patch, and we've maxed it
3551 * out. In the future we could add a
3552 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
3553 * use BTRFS_NESTING_NEW_ROOT.
3555 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3556 &disk_key, 0, l->start, 0,
3557 num_doubles ? BTRFS_NESTING_NEW_ROOT :
3558 BTRFS_NESTING_SPLIT);
3560 return PTR_ERR(right);
3562 root_add_used(root, fs_info->nodesize);
3566 btrfs_set_header_nritems(right, 0);
3567 insert_ptr(trans, path, &disk_key,
3568 right->start, path->slots[1] + 1, 1);
3569 btrfs_tree_unlock(path->nodes[0]);
3570 free_extent_buffer(path->nodes[0]);
3571 path->nodes[0] = right;
3573 path->slots[1] += 1;
3575 btrfs_set_header_nritems(right, 0);
3576 insert_ptr(trans, path, &disk_key,
3577 right->start, path->slots[1], 1);
3578 btrfs_tree_unlock(path->nodes[0]);
3579 free_extent_buffer(path->nodes[0]);
3580 path->nodes[0] = right;
3582 if (path->slots[1] == 0)
3583 fixup_low_keys(path, &disk_key, 1);
3586 * We create a new leaf 'right' for the required ins_len and
3587 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
3588 * the content of ins_len to 'right'.
3593 copy_for_split(trans, path, l, right, slot, mid, nritems);
3596 BUG_ON(num_doubles != 0);
3604 push_for_double_split(trans, root, path, data_size);
3605 tried_avoid_double = 1;
3606 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3611 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3612 struct btrfs_root *root,
3613 struct btrfs_path *path, int ins_len)
3615 struct btrfs_key key;
3616 struct extent_buffer *leaf;
3617 struct btrfs_file_extent_item *fi;
3622 leaf = path->nodes[0];
3623 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3625 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3626 key.type != BTRFS_EXTENT_CSUM_KEY);
3628 if (btrfs_leaf_free_space(leaf) >= ins_len)
3631 item_size = btrfs_item_size(leaf, path->slots[0]);
3632 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3633 fi = btrfs_item_ptr(leaf, path->slots[0],
3634 struct btrfs_file_extent_item);
3635 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3637 btrfs_release_path(path);
3639 path->keep_locks = 1;
3640 path->search_for_split = 1;
3641 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3642 path->search_for_split = 0;
3649 leaf = path->nodes[0];
3650 /* if our item isn't there, return now */
3651 if (item_size != btrfs_item_size(leaf, path->slots[0]))
3654 /* the leaf has changed, it now has room. return now */
3655 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
3658 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3659 fi = btrfs_item_ptr(leaf, path->slots[0],
3660 struct btrfs_file_extent_item);
3661 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3665 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3669 path->keep_locks = 0;
3670 btrfs_unlock_up_safe(path, 1);
3673 path->keep_locks = 0;
3677 static noinline int split_item(struct btrfs_path *path,
3678 const struct btrfs_key *new_key,
3679 unsigned long split_offset)
3681 struct extent_buffer *leaf;
3682 int orig_slot, slot;
3687 struct btrfs_disk_key disk_key;
3689 leaf = path->nodes[0];
3690 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
3692 orig_slot = path->slots[0];
3693 orig_offset = btrfs_item_offset(leaf, path->slots[0]);
3694 item_size = btrfs_item_size(leaf, path->slots[0]);
3696 buf = kmalloc(item_size, GFP_NOFS);
3700 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3701 path->slots[0]), item_size);
3703 slot = path->slots[0] + 1;
3704 nritems = btrfs_header_nritems(leaf);
3705 if (slot != nritems) {
3706 /* shift the items */
3707 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3708 btrfs_item_nr_offset(slot),
3709 (nritems - slot) * sizeof(struct btrfs_item));
3712 btrfs_cpu_key_to_disk(&disk_key, new_key);
3713 btrfs_set_item_key(leaf, &disk_key, slot);
3715 btrfs_set_item_offset(leaf, slot, orig_offset);
3716 btrfs_set_item_size(leaf, slot, item_size - split_offset);
3718 btrfs_set_item_offset(leaf, orig_slot,
3719 orig_offset + item_size - split_offset);
3720 btrfs_set_item_size(leaf, orig_slot, split_offset);
3722 btrfs_set_header_nritems(leaf, nritems + 1);
3724 /* write the data for the start of the original item */
3725 write_extent_buffer(leaf, buf,
3726 btrfs_item_ptr_offset(leaf, path->slots[0]),
3729 /* write the data for the new item */
3730 write_extent_buffer(leaf, buf + split_offset,
3731 btrfs_item_ptr_offset(leaf, slot),
3732 item_size - split_offset);
3733 btrfs_mark_buffer_dirty(leaf);
3735 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
3741 * This function splits a single item into two items,
3742 * giving 'new_key' to the new item and splitting the
3743 * old one at split_offset (from the start of the item).
3745 * The path may be released by this operation. After
3746 * the split, the path is pointing to the old item. The
3747 * new item is going to be in the same node as the old one.
3749 * Note, the item being split must be smaller enough to live alone on
3750 * a tree block with room for one extra struct btrfs_item
3752 * This allows us to split the item in place, keeping a lock on the
3753 * leaf the entire time.
3755 int btrfs_split_item(struct btrfs_trans_handle *trans,
3756 struct btrfs_root *root,
3757 struct btrfs_path *path,
3758 const struct btrfs_key *new_key,
3759 unsigned long split_offset)
3762 ret = setup_leaf_for_split(trans, root, path,
3763 sizeof(struct btrfs_item));
3767 ret = split_item(path, new_key, split_offset);
3772 * make the item pointed to by the path smaller. new_size indicates
3773 * how small to make it, and from_end tells us if we just chop bytes
3774 * off the end of the item or if we shift the item to chop bytes off
3777 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
3780 struct extent_buffer *leaf;
3782 unsigned int data_end;
3783 unsigned int old_data_start;
3784 unsigned int old_size;
3785 unsigned int size_diff;
3787 struct btrfs_map_token token;
3789 leaf = path->nodes[0];
3790 slot = path->slots[0];
3792 old_size = btrfs_item_size(leaf, slot);
3793 if (old_size == new_size)
3796 nritems = btrfs_header_nritems(leaf);
3797 data_end = leaf_data_end(leaf);
3799 old_data_start = btrfs_item_offset(leaf, slot);
3801 size_diff = old_size - new_size;
3804 BUG_ON(slot >= nritems);
3807 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3809 /* first correct the data pointers */
3810 btrfs_init_map_token(&token, leaf);
3811 for (i = slot; i < nritems; i++) {
3814 ioff = btrfs_token_item_offset(&token, i);
3815 btrfs_set_token_item_offset(&token, i, ioff + size_diff);
3818 /* shift the data */
3820 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3821 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
3822 data_end, old_data_start + new_size - data_end);
3824 struct btrfs_disk_key disk_key;
3827 btrfs_item_key(leaf, &disk_key, slot);
3829 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3831 struct btrfs_file_extent_item *fi;
3833 fi = btrfs_item_ptr(leaf, slot,
3834 struct btrfs_file_extent_item);
3835 fi = (struct btrfs_file_extent_item *)(
3836 (unsigned long)fi - size_diff);
3838 if (btrfs_file_extent_type(leaf, fi) ==
3839 BTRFS_FILE_EXTENT_INLINE) {
3840 ptr = btrfs_item_ptr_offset(leaf, slot);
3841 memmove_extent_buffer(leaf, ptr,
3843 BTRFS_FILE_EXTENT_INLINE_DATA_START);
3847 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3848 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
3849 data_end, old_data_start - data_end);
3851 offset = btrfs_disk_key_offset(&disk_key);
3852 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3853 btrfs_set_item_key(leaf, &disk_key, slot);
3855 fixup_low_keys(path, &disk_key, 1);
3858 btrfs_set_item_size(leaf, slot, new_size);
3859 btrfs_mark_buffer_dirty(leaf);
3861 if (btrfs_leaf_free_space(leaf) < 0) {
3862 btrfs_print_leaf(leaf);
3868 * make the item pointed to by the path bigger, data_size is the added size.
3870 void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
3873 struct extent_buffer *leaf;
3875 unsigned int data_end;
3876 unsigned int old_data;
3877 unsigned int old_size;
3879 struct btrfs_map_token token;
3881 leaf = path->nodes[0];
3883 nritems = btrfs_header_nritems(leaf);
3884 data_end = leaf_data_end(leaf);
3886 if (btrfs_leaf_free_space(leaf) < data_size) {
3887 btrfs_print_leaf(leaf);
3890 slot = path->slots[0];
3891 old_data = btrfs_item_data_end(leaf, slot);
3894 if (slot >= nritems) {
3895 btrfs_print_leaf(leaf);
3896 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
3902 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3904 /* first correct the data pointers */
3905 btrfs_init_map_token(&token, leaf);
3906 for (i = slot; i < nritems; i++) {
3909 ioff = btrfs_token_item_offset(&token, i);
3910 btrfs_set_token_item_offset(&token, i, ioff - data_size);
3913 /* shift the data */
3914 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3915 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
3916 data_end, old_data - data_end);
3918 data_end = old_data;
3919 old_size = btrfs_item_size(leaf, slot);
3920 btrfs_set_item_size(leaf, slot, old_size + data_size);
3921 btrfs_mark_buffer_dirty(leaf);
3923 if (btrfs_leaf_free_space(leaf) < 0) {
3924 btrfs_print_leaf(leaf);
3930 * setup_items_for_insert - Helper called before inserting one or more items
3931 * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
3932 * in a function that doesn't call btrfs_search_slot
3934 * @root: root we are inserting items to
3935 * @path: points to the leaf/slot where we are going to insert new items
3936 * @batch: information about the batch of items to insert
3938 static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
3939 const struct btrfs_item_batch *batch)
3941 struct btrfs_fs_info *fs_info = root->fs_info;
3944 unsigned int data_end;
3945 struct btrfs_disk_key disk_key;
3946 struct extent_buffer *leaf;
3948 struct btrfs_map_token token;
3952 * Before anything else, update keys in the parent and other ancestors
3953 * if needed, then release the write locks on them, so that other tasks
3954 * can use them while we modify the leaf.
3956 if (path->slots[0] == 0) {
3957 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[0]);
3958 fixup_low_keys(path, &disk_key, 1);
3960 btrfs_unlock_up_safe(path, 1);
3962 leaf = path->nodes[0];
3963 slot = path->slots[0];
3965 nritems = btrfs_header_nritems(leaf);
3966 data_end = leaf_data_end(leaf);
3967 total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
3969 if (btrfs_leaf_free_space(leaf) < total_size) {
3970 btrfs_print_leaf(leaf);
3971 btrfs_crit(fs_info, "not enough freespace need %u have %d",
3972 total_size, btrfs_leaf_free_space(leaf));
3976 btrfs_init_map_token(&token, leaf);
3977 if (slot != nritems) {
3978 unsigned int old_data = btrfs_item_data_end(leaf, slot);
3980 if (old_data < data_end) {
3981 btrfs_print_leaf(leaf);
3983 "item at slot %d with data offset %u beyond data end of leaf %u",
3984 slot, old_data, data_end);
3988 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3990 /* first correct the data pointers */
3991 for (i = slot; i < nritems; i++) {
3994 ioff = btrfs_token_item_offset(&token, i);
3995 btrfs_set_token_item_offset(&token, i,
3996 ioff - batch->total_data_size);
3998 /* shift the items */
3999 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + batch->nr),
4000 btrfs_item_nr_offset(slot),
4001 (nritems - slot) * sizeof(struct btrfs_item));
4003 /* shift the data */
4004 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4005 data_end - batch->total_data_size,
4006 BTRFS_LEAF_DATA_OFFSET + data_end,
4007 old_data - data_end);
4008 data_end = old_data;
4011 /* setup the item for the new data */
4012 for (i = 0; i < batch->nr; i++) {
4013 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[i]);
4014 btrfs_set_item_key(leaf, &disk_key, slot + i);
4015 data_end -= batch->data_sizes[i];
4016 btrfs_set_token_item_offset(&token, slot + i, data_end);
4017 btrfs_set_token_item_size(&token, slot + i, batch->data_sizes[i]);
4020 btrfs_set_header_nritems(leaf, nritems + batch->nr);
4021 btrfs_mark_buffer_dirty(leaf);
4023 if (btrfs_leaf_free_space(leaf) < 0) {
4024 btrfs_print_leaf(leaf);
4030 * Insert a new item into a leaf.
4032 * @root: The root of the btree.
4033 * @path: A path pointing to the target leaf and slot.
4034 * @key: The key of the new item.
4035 * @data_size: The size of the data associated with the new key.
4037 void btrfs_setup_item_for_insert(struct btrfs_root *root,
4038 struct btrfs_path *path,
4039 const struct btrfs_key *key,
4042 struct btrfs_item_batch batch;
4045 batch.data_sizes = &data_size;
4046 batch.total_data_size = data_size;
4049 setup_items_for_insert(root, path, &batch);
4053 * Given a key and some data, insert items into the tree.
4054 * This does all the path init required, making room in the tree if needed.
4056 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4057 struct btrfs_root *root,
4058 struct btrfs_path *path,
4059 const struct btrfs_item_batch *batch)
4065 total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
4066 ret = btrfs_search_slot(trans, root, &batch->keys[0], path, total_size, 1);
4072 slot = path->slots[0];
4075 setup_items_for_insert(root, path, batch);
4080 * Given a key and some data, insert an item into the tree.
4081 * This does all the path init required, making room in the tree if needed.
4083 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4084 const struct btrfs_key *cpu_key, void *data,
4088 struct btrfs_path *path;
4089 struct extent_buffer *leaf;
4092 path = btrfs_alloc_path();
4095 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4097 leaf = path->nodes[0];
4098 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4099 write_extent_buffer(leaf, data, ptr, data_size);
4100 btrfs_mark_buffer_dirty(leaf);
4102 btrfs_free_path(path);
4107 * This function duplicates an item, giving 'new_key' to the new item.
4108 * It guarantees both items live in the same tree leaf and the new item is
4109 * contiguous with the original item.
4111 * This allows us to split a file extent in place, keeping a lock on the leaf
4114 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4115 struct btrfs_root *root,
4116 struct btrfs_path *path,
4117 const struct btrfs_key *new_key)
4119 struct extent_buffer *leaf;
4123 leaf = path->nodes[0];
4124 item_size = btrfs_item_size(leaf, path->slots[0]);
4125 ret = setup_leaf_for_split(trans, root, path,
4126 item_size + sizeof(struct btrfs_item));
4131 btrfs_setup_item_for_insert(root, path, new_key, item_size);
4132 leaf = path->nodes[0];
4133 memcpy_extent_buffer(leaf,
4134 btrfs_item_ptr_offset(leaf, path->slots[0]),
4135 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4141 * delete the pointer from a given node.
4143 * the tree should have been previously balanced so the deletion does not
4146 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4147 int level, int slot)
4149 struct extent_buffer *parent = path->nodes[level];
4153 nritems = btrfs_header_nritems(parent);
4154 if (slot != nritems - 1) {
4156 ret = btrfs_tree_mod_log_insert_move(parent, slot,
4157 slot + 1, nritems - slot - 1);
4160 memmove_extent_buffer(parent,
4161 btrfs_node_key_ptr_offset(slot),
4162 btrfs_node_key_ptr_offset(slot + 1),
4163 sizeof(struct btrfs_key_ptr) *
4164 (nritems - slot - 1));
4166 ret = btrfs_tree_mod_log_insert_key(parent, slot,
4167 BTRFS_MOD_LOG_KEY_REMOVE, GFP_NOFS);
4172 btrfs_set_header_nritems(parent, nritems);
4173 if (nritems == 0 && parent == root->node) {
4174 BUG_ON(btrfs_header_level(root->node) != 1);
4175 /* just turn the root into a leaf and break */
4176 btrfs_set_header_level(root->node, 0);
4177 } else if (slot == 0) {
4178 struct btrfs_disk_key disk_key;
4180 btrfs_node_key(parent, &disk_key, 0);
4181 fixup_low_keys(path, &disk_key, level + 1);
4183 btrfs_mark_buffer_dirty(parent);
4187 * a helper function to delete the leaf pointed to by path->slots[1] and
4190 * This deletes the pointer in path->nodes[1] and frees the leaf
4191 * block extent. zero is returned if it all worked out, < 0 otherwise.
4193 * The path must have already been setup for deleting the leaf, including
4194 * all the proper balancing. path->nodes[1] must be locked.
4196 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4197 struct btrfs_root *root,
4198 struct btrfs_path *path,
4199 struct extent_buffer *leaf)
4201 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4202 del_ptr(root, path, 1, path->slots[1]);
4205 * btrfs_free_extent is expensive, we want to make sure we
4206 * aren't holding any locks when we call it
4208 btrfs_unlock_up_safe(path, 0);
4210 root_sub_used(root, leaf->len);
4212 atomic_inc(&leaf->refs);
4213 btrfs_free_tree_block(trans, btrfs_root_id(root), leaf, 0, 1);
4214 free_extent_buffer_stale(leaf);
4217 * delete the item at the leaf level in path. If that empties
4218 * the leaf, remove it from the tree
4220 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4221 struct btrfs_path *path, int slot, int nr)
4223 struct btrfs_fs_info *fs_info = root->fs_info;
4224 struct extent_buffer *leaf;
4229 leaf = path->nodes[0];
4230 nritems = btrfs_header_nritems(leaf);
4232 if (slot + nr != nritems) {
4233 const u32 last_off = btrfs_item_offset(leaf, slot + nr - 1);
4234 const int data_end = leaf_data_end(leaf);
4235 struct btrfs_map_token token;
4239 for (i = 0; i < nr; i++)
4240 dsize += btrfs_item_size(leaf, slot + i);
4242 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4244 BTRFS_LEAF_DATA_OFFSET + data_end,
4245 last_off - data_end);
4247 btrfs_init_map_token(&token, leaf);
4248 for (i = slot + nr; i < nritems; i++) {
4251 ioff = btrfs_token_item_offset(&token, i);
4252 btrfs_set_token_item_offset(&token, i, ioff + dsize);
4255 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4256 btrfs_item_nr_offset(slot + nr),
4257 sizeof(struct btrfs_item) *
4258 (nritems - slot - nr));
4260 btrfs_set_header_nritems(leaf, nritems - nr);
4263 /* delete the leaf if we've emptied it */
4265 if (leaf == root->node) {
4266 btrfs_set_header_level(leaf, 0);
4268 btrfs_clean_tree_block(leaf);
4269 btrfs_del_leaf(trans, root, path, leaf);
4272 int used = leaf_space_used(leaf, 0, nritems);
4274 struct btrfs_disk_key disk_key;
4276 btrfs_item_key(leaf, &disk_key, 0);
4277 fixup_low_keys(path, &disk_key, 1);
4281 * Try to delete the leaf if it is mostly empty. We do this by
4282 * trying to move all its items into its left and right neighbours.
4283 * If we can't move all the items, then we don't delete it - it's
4284 * not ideal, but future insertions might fill the leaf with more
4285 * items, or items from other leaves might be moved later into our
4286 * leaf due to deletions on those leaves.
4288 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
4291 /* push_leaf_left fixes the path.
4292 * make sure the path still points to our leaf
4293 * for possible call to del_ptr below
4295 slot = path->slots[1];
4296 atomic_inc(&leaf->refs);
4298 * We want to be able to at least push one item to the
4299 * left neighbour leaf, and that's the first item.
4301 min_push_space = sizeof(struct btrfs_item) +
4302 btrfs_item_size(leaf, 0);
4303 wret = push_leaf_left(trans, root, path, 0,
4304 min_push_space, 1, (u32)-1);
4305 if (wret < 0 && wret != -ENOSPC)
4308 if (path->nodes[0] == leaf &&
4309 btrfs_header_nritems(leaf)) {
4311 * If we were not able to push all items from our
4312 * leaf to its left neighbour, then attempt to
4313 * either push all the remaining items to the
4314 * right neighbour or none. There's no advantage
4315 * in pushing only some items, instead of all, as
4316 * it's pointless to end up with a leaf having
4317 * too few items while the neighbours can be full
4320 nritems = btrfs_header_nritems(leaf);
4321 min_push_space = leaf_space_used(leaf, 0, nritems);
4322 wret = push_leaf_right(trans, root, path, 0,
4323 min_push_space, 1, 0);
4324 if (wret < 0 && wret != -ENOSPC)
4328 if (btrfs_header_nritems(leaf) == 0) {
4329 path->slots[1] = slot;
4330 btrfs_del_leaf(trans, root, path, leaf);
4331 free_extent_buffer(leaf);
4334 /* if we're still in the path, make sure
4335 * we're dirty. Otherwise, one of the
4336 * push_leaf functions must have already
4337 * dirtied this buffer
4339 if (path->nodes[0] == leaf)
4340 btrfs_mark_buffer_dirty(leaf);
4341 free_extent_buffer(leaf);
4344 btrfs_mark_buffer_dirty(leaf);
4351 * search the tree again to find a leaf with lesser keys
4352 * returns 0 if it found something or 1 if there are no lesser leaves.
4353 * returns < 0 on io errors.
4355 * This may release the path, and so you may lose any locks held at the
4358 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4360 struct btrfs_key key;
4361 struct btrfs_disk_key found_key;
4364 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
4366 if (key.offset > 0) {
4368 } else if (key.type > 0) {
4370 key.offset = (u64)-1;
4371 } else if (key.objectid > 0) {
4374 key.offset = (u64)-1;
4379 btrfs_release_path(path);
4380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4383 btrfs_item_key(path->nodes[0], &found_key, 0);
4384 ret = comp_keys(&found_key, &key);
4386 * We might have had an item with the previous key in the tree right
4387 * before we released our path. And after we released our path, that
4388 * item might have been pushed to the first slot (0) of the leaf we
4389 * were holding due to a tree balance. Alternatively, an item with the
4390 * previous key can exist as the only element of a leaf (big fat item).
4391 * Therefore account for these 2 cases, so that our callers (like
4392 * btrfs_previous_item) don't miss an existing item with a key matching
4393 * the previous key we computed above.
4401 * A helper function to walk down the tree starting at min_key, and looking
4402 * for nodes or leaves that are have a minimum transaction id.
4403 * This is used by the btree defrag code, and tree logging
4405 * This does not cow, but it does stuff the starting key it finds back
4406 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4407 * key and get a writable path.
4409 * This honors path->lowest_level to prevent descent past a given level
4412 * min_trans indicates the oldest transaction that you are interested
4413 * in walking through. Any nodes or leaves older than min_trans are
4414 * skipped over (without reading them).
4416 * returns zero if something useful was found, < 0 on error and 1 if there
4417 * was nothing in the tree that matched the search criteria.
4419 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4420 struct btrfs_path *path,
4423 struct extent_buffer *cur;
4424 struct btrfs_key found_key;
4430 int keep_locks = path->keep_locks;
4432 path->keep_locks = 1;
4434 cur = btrfs_read_lock_root_node(root);
4435 level = btrfs_header_level(cur);
4436 WARN_ON(path->nodes[level]);
4437 path->nodes[level] = cur;
4438 path->locks[level] = BTRFS_READ_LOCK;
4440 if (btrfs_header_generation(cur) < min_trans) {
4445 nritems = btrfs_header_nritems(cur);
4446 level = btrfs_header_level(cur);
4447 sret = btrfs_bin_search(cur, min_key, &slot);
4453 /* at the lowest level, we're done, setup the path and exit */
4454 if (level == path->lowest_level) {
4455 if (slot >= nritems)
4458 path->slots[level] = slot;
4459 btrfs_item_key_to_cpu(cur, &found_key, slot);
4462 if (sret && slot > 0)
4465 * check this node pointer against the min_trans parameters.
4466 * If it is too old, skip to the next one.
4468 while (slot < nritems) {
4471 gen = btrfs_node_ptr_generation(cur, slot);
4472 if (gen < min_trans) {
4480 * we didn't find a candidate key in this node, walk forward
4481 * and find another one
4483 if (slot >= nritems) {
4484 path->slots[level] = slot;
4485 sret = btrfs_find_next_key(root, path, min_key, level,
4488 btrfs_release_path(path);
4494 /* save our key for returning back */
4495 btrfs_node_key_to_cpu(cur, &found_key, slot);
4496 path->slots[level] = slot;
4497 if (level == path->lowest_level) {
4501 cur = btrfs_read_node_slot(cur, slot);
4507 btrfs_tree_read_lock(cur);
4509 path->locks[level - 1] = BTRFS_READ_LOCK;
4510 path->nodes[level - 1] = cur;
4511 unlock_up(path, level, 1, 0, NULL);
4514 path->keep_locks = keep_locks;
4516 btrfs_unlock_up_safe(path, path->lowest_level + 1);
4517 memcpy(min_key, &found_key, sizeof(found_key));
4523 * this is similar to btrfs_next_leaf, but does not try to preserve
4524 * and fixup the path. It looks for and returns the next key in the
4525 * tree based on the current path and the min_trans parameters.
4527 * 0 is returned if another key is found, < 0 if there are any errors
4528 * and 1 is returned if there are no higher keys in the tree
4530 * path->keep_locks should be set to 1 on the search made before
4531 * calling this function.
4533 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4534 struct btrfs_key *key, int level, u64 min_trans)
4537 struct extent_buffer *c;
4539 WARN_ON(!path->keep_locks && !path->skip_locking);
4540 while (level < BTRFS_MAX_LEVEL) {
4541 if (!path->nodes[level])
4544 slot = path->slots[level] + 1;
4545 c = path->nodes[level];
4547 if (slot >= btrfs_header_nritems(c)) {
4550 struct btrfs_key cur_key;
4551 if (level + 1 >= BTRFS_MAX_LEVEL ||
4552 !path->nodes[level + 1])
4555 if (path->locks[level + 1] || path->skip_locking) {
4560 slot = btrfs_header_nritems(c) - 1;
4562 btrfs_item_key_to_cpu(c, &cur_key, slot);
4564 btrfs_node_key_to_cpu(c, &cur_key, slot);
4566 orig_lowest = path->lowest_level;
4567 btrfs_release_path(path);
4568 path->lowest_level = level;
4569 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4571 path->lowest_level = orig_lowest;
4575 c = path->nodes[level];
4576 slot = path->slots[level];
4583 btrfs_item_key_to_cpu(c, key, slot);
4585 u64 gen = btrfs_node_ptr_generation(c, slot);
4587 if (gen < min_trans) {
4591 btrfs_node_key_to_cpu(c, key, slot);
4598 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
4603 struct extent_buffer *c;
4604 struct extent_buffer *next;
4605 struct btrfs_fs_info *fs_info = root->fs_info;
4606 struct btrfs_key key;
4607 bool need_commit_sem = false;
4612 nritems = btrfs_header_nritems(path->nodes[0]);
4616 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4620 btrfs_release_path(path);
4622 path->keep_locks = 1;
4625 ret = btrfs_search_old_slot(root, &key, path, time_seq);
4627 if (path->need_commit_sem) {
4628 path->need_commit_sem = 0;
4629 need_commit_sem = true;
4630 down_read(&fs_info->commit_root_sem);
4632 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4634 path->keep_locks = 0;
4639 nritems = btrfs_header_nritems(path->nodes[0]);
4641 * by releasing the path above we dropped all our locks. A balance
4642 * could have added more items next to the key that used to be
4643 * at the very end of the block. So, check again here and
4644 * advance the path if there are now more items available.
4646 if (nritems > 0 && path->slots[0] < nritems - 1) {
4653 * So the above check misses one case:
4654 * - after releasing the path above, someone has removed the item that
4655 * used to be at the very end of the block, and balance between leafs
4656 * gets another one with bigger key.offset to replace it.
4658 * This one should be returned as well, or we can get leaf corruption
4659 * later(esp. in __btrfs_drop_extents()).
4661 * And a bit more explanation about this check,
4662 * with ret > 0, the key isn't found, the path points to the slot
4663 * where it should be inserted, so the path->slots[0] item must be the
4666 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
4671 while (level < BTRFS_MAX_LEVEL) {
4672 if (!path->nodes[level]) {
4677 slot = path->slots[level] + 1;
4678 c = path->nodes[level];
4679 if (slot >= btrfs_header_nritems(c)) {
4681 if (level == BTRFS_MAX_LEVEL) {
4690 * Our current level is where we're going to start from, and to
4691 * make sure lockdep doesn't complain we need to drop our locks
4692 * and nodes from 0 to our current level.
4694 for (i = 0; i < level; i++) {
4695 if (path->locks[level]) {
4696 btrfs_tree_read_unlock(path->nodes[i]);
4699 free_extent_buffer(path->nodes[i]);
4700 path->nodes[i] = NULL;
4704 ret = read_block_for_search(root, path, &next, level,
4710 btrfs_release_path(path);
4714 if (!path->skip_locking) {
4715 ret = btrfs_try_tree_read_lock(next);
4716 if (!ret && time_seq) {
4718 * If we don't get the lock, we may be racing
4719 * with push_leaf_left, holding that lock while
4720 * itself waiting for the leaf we've currently
4721 * locked. To solve this situation, we give up
4722 * on our lock and cycle.
4724 free_extent_buffer(next);
4725 btrfs_release_path(path);
4730 btrfs_tree_read_lock(next);
4734 path->slots[level] = slot;
4737 path->nodes[level] = next;
4738 path->slots[level] = 0;
4739 if (!path->skip_locking)
4740 path->locks[level] = BTRFS_READ_LOCK;
4744 ret = read_block_for_search(root, path, &next, level,
4750 btrfs_release_path(path);
4754 if (!path->skip_locking)
4755 btrfs_tree_read_lock(next);
4759 unlock_up(path, 0, 1, 0, NULL);
4760 if (need_commit_sem) {
4763 path->need_commit_sem = 1;
4764 ret2 = finish_need_commit_sem_search(path);
4765 up_read(&fs_info->commit_root_sem);
4774 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4775 * searching until it gets past min_objectid or finds an item of 'type'
4777 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4779 int btrfs_previous_item(struct btrfs_root *root,
4780 struct btrfs_path *path, u64 min_objectid,
4783 struct btrfs_key found_key;
4784 struct extent_buffer *leaf;
4789 if (path->slots[0] == 0) {
4790 ret = btrfs_prev_leaf(root, path);
4796 leaf = path->nodes[0];
4797 nritems = btrfs_header_nritems(leaf);
4800 if (path->slots[0] == nritems)
4803 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4804 if (found_key.objectid < min_objectid)
4806 if (found_key.type == type)
4808 if (found_key.objectid == min_objectid &&
4809 found_key.type < type)
4816 * search in extent tree to find a previous Metadata/Data extent item with
4819 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4821 int btrfs_previous_extent_item(struct btrfs_root *root,
4822 struct btrfs_path *path, u64 min_objectid)
4824 struct btrfs_key found_key;
4825 struct extent_buffer *leaf;
4830 if (path->slots[0] == 0) {
4831 ret = btrfs_prev_leaf(root, path);
4837 leaf = path->nodes[0];
4838 nritems = btrfs_header_nritems(leaf);
4841 if (path->slots[0] == nritems)
4844 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4845 if (found_key.objectid < min_objectid)
4847 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4848 found_key.type == BTRFS_METADATA_ITEM_KEY)
4850 if (found_key.objectid == min_objectid &&
4851 found_key.type < BTRFS_EXTENT_ITEM_KEY)