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>
14 #include "transaction.h"
15 #include "print-tree.h"
19 #include "tree-mod-log.h"
20 #include "tree-checker.h"
22 #include "accessors.h"
23 #include "extent-tree.h"
24 #include "relocation.h"
25 #include "file-item.h"
27 static struct kmem_cache *btrfs_path_cachep;
29 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
30 *root, struct btrfs_path *path, int level);
31 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
32 const struct btrfs_key *ins_key, struct btrfs_path *path,
33 int data_size, int extend);
34 static int push_node_left(struct btrfs_trans_handle *trans,
35 struct extent_buffer *dst,
36 struct extent_buffer *src, int empty);
37 static int balance_node_right(struct btrfs_trans_handle *trans,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
41 static const struct btrfs_csums {
44 const char driver[12];
46 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
47 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
48 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
49 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
50 .driver = "blake2b-256" },
54 * The leaf data grows from end-to-front in the node. this returns the address
55 * of the start of the last item, which is the stop of the leaf data stack.
57 static unsigned int leaf_data_end(const struct extent_buffer *leaf)
59 u32 nr = btrfs_header_nritems(leaf);
62 return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
63 return btrfs_item_offset(leaf, nr - 1);
67 * Move data in a @leaf (using memmove, safe for overlapping ranges).
69 * @leaf: leaf that we're doing a memmove on
70 * @dst_offset: item data offset we're moving to
71 * @src_offset: item data offset were' moving from
72 * @len: length of the data we're moving
74 * Wrapper around memmove_extent_buffer() that takes into account the header on
75 * the leaf. The btrfs_item offset's start directly after the header, so we
76 * have to adjust any offsets to account for the header in the leaf. This
77 * handles that math to simplify the callers.
79 static inline void memmove_leaf_data(const struct extent_buffer *leaf,
80 unsigned long dst_offset,
81 unsigned long src_offset,
84 memmove_extent_buffer(leaf, btrfs_item_nr_offset(leaf, 0) + dst_offset,
85 btrfs_item_nr_offset(leaf, 0) + src_offset, len);
89 * Copy item data from @src into @dst at the given @offset.
91 * @dst: destination leaf that we're copying into
92 * @src: source leaf that we're copying from
93 * @dst_offset: item data offset we're copying to
94 * @src_offset: item data offset were' copying from
95 * @len: length of the data we're copying
97 * Wrapper around copy_extent_buffer() that takes into account the header on
98 * the leaf. The btrfs_item offset's start directly after the header, so we
99 * have to adjust any offsets to account for the header in the leaf. This
100 * handles that math to simplify the callers.
102 static inline void copy_leaf_data(const struct extent_buffer *dst,
103 const struct extent_buffer *src,
104 unsigned long dst_offset,
105 unsigned long src_offset, unsigned long len)
107 copy_extent_buffer(dst, src, btrfs_item_nr_offset(dst, 0) + dst_offset,
108 btrfs_item_nr_offset(src, 0) + src_offset, len);
112 * Move items in a @leaf (using memmove).
114 * @dst: destination leaf for the items
115 * @dst_item: the item nr we're copying into
116 * @src_item: the item nr we're copying from
117 * @nr_items: the number of items to copy
119 * Wrapper around memmove_extent_buffer() that does the math to get the
120 * appropriate offsets into the leaf from the item numbers.
122 static inline void memmove_leaf_items(const struct extent_buffer *leaf,
123 int dst_item, int src_item, int nr_items)
125 memmove_extent_buffer(leaf, btrfs_item_nr_offset(leaf, dst_item),
126 btrfs_item_nr_offset(leaf, src_item),
127 nr_items * sizeof(struct btrfs_item));
131 * Copy items from @src into @dst at the given @offset.
133 * @dst: destination leaf for the items
134 * @src: source leaf for the items
135 * @dst_item: the item nr we're copying into
136 * @src_item: the item nr we're copying from
137 * @nr_items: the number of items to copy
139 * Wrapper around copy_extent_buffer() that does the math to get the
140 * appropriate offsets into the leaf from the item numbers.
142 static inline void copy_leaf_items(const struct extent_buffer *dst,
143 const struct extent_buffer *src,
144 int dst_item, int src_item, int nr_items)
146 copy_extent_buffer(dst, src, btrfs_item_nr_offset(dst, dst_item),
147 btrfs_item_nr_offset(src, src_item),
148 nr_items * sizeof(struct btrfs_item));
151 /* This exists for btrfs-progs usages. */
152 u16 btrfs_csum_type_size(u16 type)
154 return btrfs_csums[type].size;
157 int btrfs_super_csum_size(const struct btrfs_super_block *s)
159 u16 t = btrfs_super_csum_type(s);
161 * csum type is validated at mount time
163 return btrfs_csum_type_size(t);
166 const char *btrfs_super_csum_name(u16 csum_type)
168 /* csum type is validated at mount time */
169 return btrfs_csums[csum_type].name;
173 * Return driver name if defined, otherwise the name that's also a valid driver
176 const char *btrfs_super_csum_driver(u16 csum_type)
178 /* csum type is validated at mount time */
179 return btrfs_csums[csum_type].driver[0] ?
180 btrfs_csums[csum_type].driver :
181 btrfs_csums[csum_type].name;
184 size_t __attribute_const__ btrfs_get_num_csums(void)
186 return ARRAY_SIZE(btrfs_csums);
189 struct btrfs_path *btrfs_alloc_path(void)
193 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
196 /* this also releases the path */
197 void btrfs_free_path(struct btrfs_path *p)
201 btrfs_release_path(p);
202 kmem_cache_free(btrfs_path_cachep, p);
206 * path release drops references on the extent buffers in the path
207 * and it drops any locks held by this path
209 * It is safe to call this on paths that no locks or extent buffers held.
211 noinline void btrfs_release_path(struct btrfs_path *p)
215 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
220 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
223 free_extent_buffer(p->nodes[i]);
229 * We want the transaction abort to print stack trace only for errors where the
230 * cause could be a bug, eg. due to ENOSPC, and not for common errors that are
231 * caused by external factors.
233 bool __cold abort_should_print_stack(int errno)
245 * safely gets a reference on the root node of a tree. A lock
246 * is not taken, so a concurrent writer may put a different node
247 * at the root of the tree. See btrfs_lock_root_node for the
250 * The extent buffer returned by this has a reference taken, so
251 * it won't disappear. It may stop being the root of the tree
252 * at any time because there are no locks held.
254 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
256 struct extent_buffer *eb;
260 eb = rcu_dereference(root->node);
263 * RCU really hurts here, we could free up the root node because
264 * it was COWed but we may not get the new root node yet so do
265 * the inc_not_zero dance and if it doesn't work then
266 * synchronize_rcu and try again.
268 if (atomic_inc_not_zero(&eb->refs)) {
279 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
280 * just get put onto a simple dirty list. Transaction walks this list to make
281 * sure they get properly updated on disk.
283 static void add_root_to_dirty_list(struct btrfs_root *root)
285 struct btrfs_fs_info *fs_info = root->fs_info;
287 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
288 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
291 spin_lock(&fs_info->trans_lock);
292 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
293 /* Want the extent tree to be the last on the list */
294 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
295 list_move_tail(&root->dirty_list,
296 &fs_info->dirty_cowonly_roots);
298 list_move(&root->dirty_list,
299 &fs_info->dirty_cowonly_roots);
301 spin_unlock(&fs_info->trans_lock);
305 * used by snapshot creation to make a copy of a root for a tree with
306 * a given objectid. The buffer with the new root node is returned in
307 * cow_ret, and this func returns zero on success or a negative error code.
309 int btrfs_copy_root(struct btrfs_trans_handle *trans,
310 struct btrfs_root *root,
311 struct extent_buffer *buf,
312 struct extent_buffer **cow_ret, u64 new_root_objectid)
314 struct btrfs_fs_info *fs_info = root->fs_info;
315 struct extent_buffer *cow;
318 struct btrfs_disk_key disk_key;
320 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
321 trans->transid != fs_info->running_transaction->transid);
322 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
323 trans->transid != root->last_trans);
325 level = btrfs_header_level(buf);
327 btrfs_item_key(buf, &disk_key, 0);
329 btrfs_node_key(buf, &disk_key, 0);
331 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
332 &disk_key, level, buf->start, 0,
333 BTRFS_NESTING_NEW_ROOT);
337 copy_extent_buffer_full(cow, buf);
338 btrfs_set_header_bytenr(cow, cow->start);
339 btrfs_set_header_generation(cow, trans->transid);
340 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
341 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
342 BTRFS_HEADER_FLAG_RELOC);
343 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
344 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
346 btrfs_set_header_owner(cow, new_root_objectid);
348 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
350 WARN_ON(btrfs_header_generation(buf) > trans->transid);
351 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
352 ret = btrfs_inc_ref(trans, root, cow, 1);
354 ret = btrfs_inc_ref(trans, root, cow, 0);
356 btrfs_tree_unlock(cow);
357 free_extent_buffer(cow);
358 btrfs_abort_transaction(trans, ret);
362 btrfs_mark_buffer_dirty(cow);
368 * check if the tree block can be shared by multiple trees
370 int btrfs_block_can_be_shared(struct btrfs_root *root,
371 struct extent_buffer *buf)
374 * Tree blocks not in shareable trees and tree roots are never shared.
375 * If a block was allocated after the last snapshot and the block was
376 * not allocated by tree relocation, we know the block is not shared.
378 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
379 buf != root->node && buf != root->commit_root &&
380 (btrfs_header_generation(buf) <=
381 btrfs_root_last_snapshot(&root->root_item) ||
382 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
388 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
389 struct btrfs_root *root,
390 struct extent_buffer *buf,
391 struct extent_buffer *cow,
394 struct btrfs_fs_info *fs_info = root->fs_info;
402 * Backrefs update rules:
404 * Always use full backrefs for extent pointers in tree block
405 * allocated by tree relocation.
407 * If a shared tree block is no longer referenced by its owner
408 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
409 * use full backrefs for extent pointers in tree block.
411 * If a tree block is been relocating
412 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
413 * use full backrefs for extent pointers in tree block.
414 * The reason for this is some operations (such as drop tree)
415 * are only allowed for blocks use full backrefs.
418 if (btrfs_block_can_be_shared(root, buf)) {
419 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
420 btrfs_header_level(buf), 1,
424 if (unlikely(refs == 0)) {
426 "found 0 references for tree block at bytenr %llu level %d root %llu",
427 buf->start, btrfs_header_level(buf),
428 btrfs_root_id(root));
430 btrfs_abort_transaction(trans, ret);
435 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
436 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
437 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
442 owner = btrfs_header_owner(buf);
443 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
444 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
447 if ((owner == root->root_key.objectid ||
448 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
449 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
450 ret = btrfs_inc_ref(trans, root, buf, 1);
454 if (root->root_key.objectid ==
455 BTRFS_TREE_RELOC_OBJECTID) {
456 ret = btrfs_dec_ref(trans, root, buf, 0);
459 ret = btrfs_inc_ref(trans, root, cow, 1);
463 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
466 if (root->root_key.objectid ==
467 BTRFS_TREE_RELOC_OBJECTID)
468 ret = btrfs_inc_ref(trans, root, cow, 1);
470 ret = btrfs_inc_ref(trans, root, cow, 0);
474 if (new_flags != 0) {
475 ret = btrfs_set_disk_extent_flags(trans, buf, new_flags);
480 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
481 if (root->root_key.objectid ==
482 BTRFS_TREE_RELOC_OBJECTID)
483 ret = btrfs_inc_ref(trans, root, cow, 1);
485 ret = btrfs_inc_ref(trans, root, cow, 0);
488 ret = btrfs_dec_ref(trans, root, buf, 1);
492 btrfs_clear_buffer_dirty(trans, buf);
499 * does the dirty work in cow of a single block. The parent block (if
500 * supplied) is updated to point to the new cow copy. The new buffer is marked
501 * dirty and returned locked. If you modify the block it needs to be marked
504 * search_start -- an allocation hint for the new block
506 * empty_size -- a hint that you plan on doing more cow. This is the size in
507 * bytes the allocator should try to find free next to the block it returns.
508 * This is just a hint and may be ignored by the allocator.
510 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root,
512 struct extent_buffer *buf,
513 struct extent_buffer *parent, int parent_slot,
514 struct extent_buffer **cow_ret,
515 u64 search_start, u64 empty_size,
516 enum btrfs_lock_nesting nest)
518 struct btrfs_fs_info *fs_info = root->fs_info;
519 struct btrfs_disk_key disk_key;
520 struct extent_buffer *cow;
524 u64 parent_start = 0;
529 btrfs_assert_tree_write_locked(buf);
531 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
532 trans->transid != fs_info->running_transaction->transid);
533 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
534 trans->transid != root->last_trans);
536 level = btrfs_header_level(buf);
539 btrfs_item_key(buf, &disk_key, 0);
541 btrfs_node_key(buf, &disk_key, 0);
543 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
544 parent_start = parent->start;
546 cow = btrfs_alloc_tree_block(trans, root, parent_start,
547 root->root_key.objectid, &disk_key, level,
548 search_start, empty_size, nest);
552 /* cow is set to blocking by btrfs_init_new_buffer */
554 copy_extent_buffer_full(cow, buf);
555 btrfs_set_header_bytenr(cow, cow->start);
556 btrfs_set_header_generation(cow, trans->transid);
557 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
558 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
559 BTRFS_HEADER_FLAG_RELOC);
560 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
561 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
563 btrfs_set_header_owner(cow, root->root_key.objectid);
565 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
567 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
569 btrfs_tree_unlock(cow);
570 free_extent_buffer(cow);
571 btrfs_abort_transaction(trans, ret);
575 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
576 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
578 btrfs_tree_unlock(cow);
579 free_extent_buffer(cow);
580 btrfs_abort_transaction(trans, ret);
585 if (buf == root->node) {
586 WARN_ON(parent && parent != buf);
587 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
588 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
589 parent_start = buf->start;
591 ret = btrfs_tree_mod_log_insert_root(root->node, cow, true);
593 btrfs_tree_unlock(cow);
594 free_extent_buffer(cow);
595 btrfs_abort_transaction(trans, ret);
598 atomic_inc(&cow->refs);
599 rcu_assign_pointer(root->node, cow);
601 btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
602 parent_start, last_ref);
603 free_extent_buffer(buf);
604 add_root_to_dirty_list(root);
606 WARN_ON(trans->transid != btrfs_header_generation(parent));
607 ret = btrfs_tree_mod_log_insert_key(parent, parent_slot,
608 BTRFS_MOD_LOG_KEY_REPLACE);
610 btrfs_tree_unlock(cow);
611 free_extent_buffer(cow);
612 btrfs_abort_transaction(trans, ret);
615 btrfs_set_node_blockptr(parent, parent_slot,
617 btrfs_set_node_ptr_generation(parent, parent_slot,
619 btrfs_mark_buffer_dirty(parent);
621 ret = btrfs_tree_mod_log_free_eb(buf);
623 btrfs_tree_unlock(cow);
624 free_extent_buffer(cow);
625 btrfs_abort_transaction(trans, ret);
629 btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
630 parent_start, last_ref);
633 btrfs_tree_unlock(buf);
634 free_extent_buffer_stale(buf);
635 btrfs_mark_buffer_dirty(cow);
640 static inline int should_cow_block(struct btrfs_trans_handle *trans,
641 struct btrfs_root *root,
642 struct extent_buffer *buf)
644 if (btrfs_is_testing(root->fs_info))
647 /* Ensure we can see the FORCE_COW bit */
648 smp_mb__before_atomic();
651 * We do not need to cow a block if
652 * 1) this block is not created or changed in this transaction;
653 * 2) this block does not belong to TREE_RELOC tree;
654 * 3) the root is not forced COW.
656 * What is forced COW:
657 * when we create snapshot during committing the transaction,
658 * after we've finished copying src root, we must COW the shared
659 * block to ensure the metadata consistency.
661 if (btrfs_header_generation(buf) == trans->transid &&
662 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
663 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
664 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
665 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
671 * cows a single block, see __btrfs_cow_block for the real work.
672 * This version of it has extra checks so that a block isn't COWed more than
673 * once per transaction, as long as it hasn't been written yet
675 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
676 struct btrfs_root *root, struct extent_buffer *buf,
677 struct extent_buffer *parent, int parent_slot,
678 struct extent_buffer **cow_ret,
679 enum btrfs_lock_nesting nest)
681 struct btrfs_fs_info *fs_info = root->fs_info;
685 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
687 "COW'ing blocks on a fs root that's being dropped");
689 if (trans->transaction != fs_info->running_transaction)
690 WARN(1, KERN_CRIT "trans %llu running %llu\n",
692 fs_info->running_transaction->transid);
694 if (trans->transid != fs_info->generation)
695 WARN(1, KERN_CRIT "trans %llu running %llu\n",
696 trans->transid, fs_info->generation);
698 if (!should_cow_block(trans, root, buf)) {
703 search_start = buf->start & ~((u64)SZ_1G - 1);
706 * Before CoWing this block for later modification, check if it's
707 * the subtree root and do the delayed subtree trace if needed.
709 * Also We don't care about the error, as it's handled internally.
711 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
712 ret = __btrfs_cow_block(trans, root, buf, parent,
713 parent_slot, cow_ret, search_start, 0, nest);
715 trace_btrfs_cow_block(root, buf, *cow_ret);
719 ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
722 * helper function for defrag to decide if two blocks pointed to by a
723 * node are actually close by
725 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
727 if (blocknr < other && other - (blocknr + blocksize) < 32768)
729 if (blocknr > other && blocknr - (other + blocksize) < 32768)
734 #ifdef __LITTLE_ENDIAN
737 * Compare two keys, on little-endian the disk order is same as CPU order and
738 * we can avoid the conversion.
740 static int comp_keys(const struct btrfs_disk_key *disk_key,
741 const struct btrfs_key *k2)
743 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
745 return btrfs_comp_cpu_keys(k1, k2);
751 * compare two keys in a memcmp fashion
753 static int comp_keys(const struct btrfs_disk_key *disk,
754 const struct btrfs_key *k2)
758 btrfs_disk_key_to_cpu(&k1, disk);
760 return btrfs_comp_cpu_keys(&k1, k2);
765 * same as comp_keys only with two btrfs_key's
767 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
769 if (k1->objectid > k2->objectid)
771 if (k1->objectid < k2->objectid)
773 if (k1->type > k2->type)
775 if (k1->type < k2->type)
777 if (k1->offset > k2->offset)
779 if (k1->offset < k2->offset)
785 * this is used by the defrag code to go through all the
786 * leaves pointed to by a node and reallocate them so that
787 * disk order is close to key order
789 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
790 struct btrfs_root *root, struct extent_buffer *parent,
791 int start_slot, u64 *last_ret,
792 struct btrfs_key *progress)
794 struct btrfs_fs_info *fs_info = root->fs_info;
795 struct extent_buffer *cur;
797 u64 search_start = *last_ret;
805 int progress_passed = 0;
806 struct btrfs_disk_key disk_key;
808 WARN_ON(trans->transaction != fs_info->running_transaction);
809 WARN_ON(trans->transid != fs_info->generation);
811 parent_nritems = btrfs_header_nritems(parent);
812 blocksize = fs_info->nodesize;
813 end_slot = parent_nritems - 1;
815 if (parent_nritems <= 1)
818 for (i = start_slot; i <= end_slot; i++) {
821 btrfs_node_key(parent, &disk_key, i);
822 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
826 blocknr = btrfs_node_blockptr(parent, i);
828 last_block = blocknr;
831 other = btrfs_node_blockptr(parent, i - 1);
832 close = close_blocks(blocknr, other, blocksize);
834 if (!close && i < end_slot) {
835 other = btrfs_node_blockptr(parent, i + 1);
836 close = close_blocks(blocknr, other, blocksize);
839 last_block = blocknr;
843 cur = btrfs_read_node_slot(parent, i);
846 if (search_start == 0)
847 search_start = last_block;
849 btrfs_tree_lock(cur);
850 err = __btrfs_cow_block(trans, root, cur, parent, i,
853 (end_slot - i) * blocksize),
856 btrfs_tree_unlock(cur);
857 free_extent_buffer(cur);
860 search_start = cur->start;
861 last_block = cur->start;
862 *last_ret = search_start;
863 btrfs_tree_unlock(cur);
864 free_extent_buffer(cur);
870 * Search for a key in the given extent_buffer.
872 * The lower boundary for the search is specified by the slot number @first_slot.
873 * Use a value of 0 to search over the whole extent buffer. Works for both
876 * The slot in the extent buffer is returned via @slot. If the key exists in the
877 * extent buffer, then @slot will point to the slot where the key is, otherwise
878 * it points to the slot where you would insert the key.
880 * Slot may point to the total number of items (i.e. one position beyond the last
881 * key) if the key is bigger than the last key in the extent buffer.
883 int btrfs_bin_search(struct extent_buffer *eb, int first_slot,
884 const struct btrfs_key *key, int *slot)
889 * Use unsigned types for the low and high slots, so that we get a more
890 * efficient division in the search loop below.
892 u32 low = first_slot;
893 u32 high = btrfs_header_nritems(eb);
895 const int key_size = sizeof(struct btrfs_disk_key);
897 if (unlikely(low > high)) {
898 btrfs_err(eb->fs_info,
899 "%s: low (%u) > high (%u) eb %llu owner %llu level %d",
900 __func__, low, high, eb->start,
901 btrfs_header_owner(eb), btrfs_header_level(eb));
905 if (btrfs_header_level(eb) == 0) {
906 p = offsetof(struct btrfs_leaf, items);
907 item_size = sizeof(struct btrfs_item);
909 p = offsetof(struct btrfs_node, ptrs);
910 item_size = sizeof(struct btrfs_key_ptr);
915 unsigned long offset;
916 struct btrfs_disk_key *tmp;
917 struct btrfs_disk_key unaligned;
920 mid = (low + high) / 2;
921 offset = p + mid * item_size;
922 oip = offset_in_page(offset);
924 if (oip + key_size <= PAGE_SIZE) {
925 const unsigned long idx = get_eb_page_index(offset);
926 char *kaddr = page_address(eb->pages[idx]);
928 oip = get_eb_offset_in_page(eb, offset);
929 tmp = (struct btrfs_disk_key *)(kaddr + oip);
931 read_extent_buffer(eb, &unaligned, offset, key_size);
935 ret = comp_keys(tmp, key);
950 static void root_add_used(struct btrfs_root *root, u32 size)
952 spin_lock(&root->accounting_lock);
953 btrfs_set_root_used(&root->root_item,
954 btrfs_root_used(&root->root_item) + size);
955 spin_unlock(&root->accounting_lock);
958 static void root_sub_used(struct btrfs_root *root, u32 size)
960 spin_lock(&root->accounting_lock);
961 btrfs_set_root_used(&root->root_item,
962 btrfs_root_used(&root->root_item) - size);
963 spin_unlock(&root->accounting_lock);
966 /* given a node and slot number, this reads the blocks it points to. The
967 * extent buffer is returned with a reference taken (but unlocked).
969 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
972 int level = btrfs_header_level(parent);
973 struct btrfs_tree_parent_check check = { 0 };
974 struct extent_buffer *eb;
976 if (slot < 0 || slot >= btrfs_header_nritems(parent))
977 return ERR_PTR(-ENOENT);
981 check.level = level - 1;
982 check.transid = btrfs_node_ptr_generation(parent, slot);
983 check.owner_root = btrfs_header_owner(parent);
984 check.has_first_key = true;
985 btrfs_node_key_to_cpu(parent, &check.first_key, slot);
987 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
991 if (!extent_buffer_uptodate(eb)) {
992 free_extent_buffer(eb);
993 return ERR_PTR(-EIO);
1000 * node level balancing, used to make sure nodes are in proper order for
1001 * item deletion. We balance from the top down, so we have to make sure
1002 * that a deletion won't leave an node completely empty later on.
1004 static noinline int balance_level(struct btrfs_trans_handle *trans,
1005 struct btrfs_root *root,
1006 struct btrfs_path *path, int level)
1008 struct btrfs_fs_info *fs_info = root->fs_info;
1009 struct extent_buffer *right = NULL;
1010 struct extent_buffer *mid;
1011 struct extent_buffer *left = NULL;
1012 struct extent_buffer *parent = NULL;
1016 int orig_slot = path->slots[level];
1021 mid = path->nodes[level];
1023 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK);
1024 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1026 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1028 if (level < BTRFS_MAX_LEVEL - 1) {
1029 parent = path->nodes[level + 1];
1030 pslot = path->slots[level + 1];
1034 * deal with the case where there is only one pointer in the root
1035 * by promoting the node below to a root
1038 struct extent_buffer *child;
1040 if (btrfs_header_nritems(mid) != 1)
1043 /* promote the child to a root */
1044 child = btrfs_read_node_slot(mid, 0);
1045 if (IS_ERR(child)) {
1046 ret = PTR_ERR(child);
1050 btrfs_tree_lock(child);
1051 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
1054 btrfs_tree_unlock(child);
1055 free_extent_buffer(child);
1059 ret = btrfs_tree_mod_log_insert_root(root->node, child, true);
1061 btrfs_tree_unlock(child);
1062 free_extent_buffer(child);
1063 btrfs_abort_transaction(trans, ret);
1066 rcu_assign_pointer(root->node, child);
1068 add_root_to_dirty_list(root);
1069 btrfs_tree_unlock(child);
1071 path->locks[level] = 0;
1072 path->nodes[level] = NULL;
1073 btrfs_clear_buffer_dirty(trans, mid);
1074 btrfs_tree_unlock(mid);
1075 /* once for the path */
1076 free_extent_buffer(mid);
1078 root_sub_used(root, mid->len);
1079 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1080 /* once for the root ptr */
1081 free_extent_buffer_stale(mid);
1084 if (btrfs_header_nritems(mid) >
1085 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1089 left = btrfs_read_node_slot(parent, pslot - 1);
1091 ret = PTR_ERR(left);
1096 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
1097 wret = btrfs_cow_block(trans, root, left,
1098 parent, pslot - 1, &left,
1099 BTRFS_NESTING_LEFT_COW);
1106 if (pslot + 1 < btrfs_header_nritems(parent)) {
1107 right = btrfs_read_node_slot(parent, pslot + 1);
1108 if (IS_ERR(right)) {
1109 ret = PTR_ERR(right);
1114 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1115 wret = btrfs_cow_block(trans, root, right,
1116 parent, pslot + 1, &right,
1117 BTRFS_NESTING_RIGHT_COW);
1124 /* first, try to make some room in the middle buffer */
1126 orig_slot += btrfs_header_nritems(left);
1127 wret = push_node_left(trans, left, mid, 1);
1133 * then try to empty the right most buffer into the middle
1136 wret = push_node_left(trans, mid, right, 1);
1137 if (wret < 0 && wret != -ENOSPC)
1139 if (btrfs_header_nritems(right) == 0) {
1140 btrfs_clear_buffer_dirty(trans, right);
1141 btrfs_tree_unlock(right);
1142 ret = btrfs_del_ptr(trans, root, path, level + 1, pslot + 1);
1144 free_extent_buffer_stale(right);
1148 root_sub_used(root, right->len);
1149 btrfs_free_tree_block(trans, btrfs_root_id(root), right,
1151 free_extent_buffer_stale(right);
1154 struct btrfs_disk_key right_key;
1155 btrfs_node_key(right, &right_key, 0);
1156 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1157 BTRFS_MOD_LOG_KEY_REPLACE);
1159 btrfs_abort_transaction(trans, ret);
1162 btrfs_set_node_key(parent, &right_key, pslot + 1);
1163 btrfs_mark_buffer_dirty(parent);
1166 if (btrfs_header_nritems(mid) == 1) {
1168 * we're not allowed to leave a node with one item in the
1169 * tree during a delete. A deletion from lower in the tree
1170 * could try to delete the only pointer in this node.
1171 * So, pull some keys from the left.
1172 * There has to be a left pointer at this point because
1173 * otherwise we would have pulled some pointers from the
1176 if (unlikely(!left)) {
1178 "missing left child when middle child only has 1 item, parent bytenr %llu level %d mid bytenr %llu root %llu",
1179 parent->start, btrfs_header_level(parent),
1180 mid->start, btrfs_root_id(root));
1182 btrfs_abort_transaction(trans, ret);
1185 wret = balance_node_right(trans, mid, left);
1191 wret = push_node_left(trans, left, mid, 1);
1197 if (btrfs_header_nritems(mid) == 0) {
1198 btrfs_clear_buffer_dirty(trans, mid);
1199 btrfs_tree_unlock(mid);
1200 ret = btrfs_del_ptr(trans, root, path, level + 1, pslot);
1202 free_extent_buffer_stale(mid);
1206 root_sub_used(root, mid->len);
1207 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1208 free_extent_buffer_stale(mid);
1211 /* update the parent key to reflect our changes */
1212 struct btrfs_disk_key mid_key;
1213 btrfs_node_key(mid, &mid_key, 0);
1214 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1215 BTRFS_MOD_LOG_KEY_REPLACE);
1217 btrfs_abort_transaction(trans, ret);
1220 btrfs_set_node_key(parent, &mid_key, pslot);
1221 btrfs_mark_buffer_dirty(parent);
1224 /* update the path */
1226 if (btrfs_header_nritems(left) > orig_slot) {
1227 atomic_inc(&left->refs);
1228 /* left was locked after cow */
1229 path->nodes[level] = left;
1230 path->slots[level + 1] -= 1;
1231 path->slots[level] = orig_slot;
1233 btrfs_tree_unlock(mid);
1234 free_extent_buffer(mid);
1237 orig_slot -= btrfs_header_nritems(left);
1238 path->slots[level] = orig_slot;
1241 /* double check we haven't messed things up */
1243 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1247 btrfs_tree_unlock(right);
1248 free_extent_buffer(right);
1251 if (path->nodes[level] != left)
1252 btrfs_tree_unlock(left);
1253 free_extent_buffer(left);
1258 /* Node balancing for insertion. Here we only split or push nodes around
1259 * when they are completely full. This is also done top down, so we
1260 * have to be pessimistic.
1262 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1263 struct btrfs_root *root,
1264 struct btrfs_path *path, int level)
1266 struct btrfs_fs_info *fs_info = root->fs_info;
1267 struct extent_buffer *right = NULL;
1268 struct extent_buffer *mid;
1269 struct extent_buffer *left = NULL;
1270 struct extent_buffer *parent = NULL;
1274 int orig_slot = path->slots[level];
1279 mid = path->nodes[level];
1280 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1282 if (level < BTRFS_MAX_LEVEL - 1) {
1283 parent = path->nodes[level + 1];
1284 pslot = path->slots[level + 1];
1290 /* first, try to make some room in the middle buffer */
1294 left = btrfs_read_node_slot(parent, pslot - 1);
1296 return PTR_ERR(left);
1298 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
1300 left_nr = btrfs_header_nritems(left);
1301 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1304 ret = btrfs_cow_block(trans, root, left, parent,
1306 BTRFS_NESTING_LEFT_COW);
1310 wret = push_node_left(trans, left, mid, 0);
1316 struct btrfs_disk_key disk_key;
1317 orig_slot += left_nr;
1318 btrfs_node_key(mid, &disk_key, 0);
1319 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1320 BTRFS_MOD_LOG_KEY_REPLACE);
1322 btrfs_tree_unlock(left);
1323 free_extent_buffer(left);
1324 btrfs_abort_transaction(trans, ret);
1327 btrfs_set_node_key(parent, &disk_key, pslot);
1328 btrfs_mark_buffer_dirty(parent);
1329 if (btrfs_header_nritems(left) > orig_slot) {
1330 path->nodes[level] = left;
1331 path->slots[level + 1] -= 1;
1332 path->slots[level] = orig_slot;
1333 btrfs_tree_unlock(mid);
1334 free_extent_buffer(mid);
1337 btrfs_header_nritems(left);
1338 path->slots[level] = orig_slot;
1339 btrfs_tree_unlock(left);
1340 free_extent_buffer(left);
1344 btrfs_tree_unlock(left);
1345 free_extent_buffer(left);
1349 * then try to empty the right most buffer into the middle
1351 if (pslot + 1 < btrfs_header_nritems(parent)) {
1354 right = btrfs_read_node_slot(parent, pslot + 1);
1356 return PTR_ERR(right);
1358 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1360 right_nr = btrfs_header_nritems(right);
1361 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1364 ret = btrfs_cow_block(trans, root, right,
1366 &right, BTRFS_NESTING_RIGHT_COW);
1370 wret = balance_node_right(trans, right, mid);
1376 struct btrfs_disk_key disk_key;
1378 btrfs_node_key(right, &disk_key, 0);
1379 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1380 BTRFS_MOD_LOG_KEY_REPLACE);
1382 btrfs_tree_unlock(right);
1383 free_extent_buffer(right);
1384 btrfs_abort_transaction(trans, ret);
1387 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1388 btrfs_mark_buffer_dirty(parent);
1390 if (btrfs_header_nritems(mid) <= orig_slot) {
1391 path->nodes[level] = right;
1392 path->slots[level + 1] += 1;
1393 path->slots[level] = orig_slot -
1394 btrfs_header_nritems(mid);
1395 btrfs_tree_unlock(mid);
1396 free_extent_buffer(mid);
1398 btrfs_tree_unlock(right);
1399 free_extent_buffer(right);
1403 btrfs_tree_unlock(right);
1404 free_extent_buffer(right);
1410 * readahead one full node of leaves, finding things that are close
1411 * to the block in 'slot', and triggering ra on them.
1413 static void reada_for_search(struct btrfs_fs_info *fs_info,
1414 struct btrfs_path *path,
1415 int level, int slot, u64 objectid)
1417 struct extent_buffer *node;
1418 struct btrfs_disk_key disk_key;
1428 if (level != 1 && path->reada != READA_FORWARD_ALWAYS)
1431 if (!path->nodes[level])
1434 node = path->nodes[level];
1437 * Since the time between visiting leaves is much shorter than the time
1438 * between visiting nodes, limit read ahead of nodes to 1, to avoid too
1439 * much IO at once (possibly random).
1441 if (path->reada == READA_FORWARD_ALWAYS) {
1443 nread_max = node->fs_info->nodesize;
1445 nread_max = SZ_128K;
1450 search = btrfs_node_blockptr(node, slot);
1451 blocksize = fs_info->nodesize;
1452 if (path->reada != READA_FORWARD_ALWAYS) {
1453 struct extent_buffer *eb;
1455 eb = find_extent_buffer(fs_info, search);
1457 free_extent_buffer(eb);
1464 nritems = btrfs_header_nritems(node);
1468 if (path->reada == READA_BACK) {
1472 } else if (path->reada == READA_FORWARD ||
1473 path->reada == READA_FORWARD_ALWAYS) {
1478 if (path->reada == READA_BACK && objectid) {
1479 btrfs_node_key(node, &disk_key, nr);
1480 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1483 search = btrfs_node_blockptr(node, nr);
1484 if (path->reada == READA_FORWARD_ALWAYS ||
1485 (search <= target && target - search <= 65536) ||
1486 (search > target && search - target <= 65536)) {
1487 btrfs_readahead_node_child(node, nr);
1491 if (nread > nread_max || nscan > 32)
1496 static noinline void reada_for_balance(struct btrfs_path *path, int level)
1498 struct extent_buffer *parent;
1502 parent = path->nodes[level + 1];
1506 nritems = btrfs_header_nritems(parent);
1507 slot = path->slots[level + 1];
1510 btrfs_readahead_node_child(parent, slot - 1);
1511 if (slot + 1 < nritems)
1512 btrfs_readahead_node_child(parent, slot + 1);
1517 * when we walk down the tree, it is usually safe to unlock the higher layers
1518 * in the tree. The exceptions are when our path goes through slot 0, because
1519 * operations on the tree might require changing key pointers higher up in the
1522 * callers might also have set path->keep_locks, which tells this code to keep
1523 * the lock if the path points to the last slot in the block. This is part of
1524 * walking through the tree, and selecting the next slot in the higher block.
1526 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1527 * if lowest_unlock is 1, level 0 won't be unlocked
1529 static noinline void unlock_up(struct btrfs_path *path, int level,
1530 int lowest_unlock, int min_write_lock_level,
1531 int *write_lock_level)
1534 int skip_level = level;
1535 bool check_skip = true;
1537 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1538 if (!path->nodes[i])
1540 if (!path->locks[i])
1544 if (path->slots[i] == 0) {
1549 if (path->keep_locks) {
1552 nritems = btrfs_header_nritems(path->nodes[i]);
1553 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1560 if (i >= lowest_unlock && i > skip_level) {
1562 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1564 if (write_lock_level &&
1565 i > min_write_lock_level &&
1566 i <= *write_lock_level) {
1567 *write_lock_level = i - 1;
1574 * Helper function for btrfs_search_slot() and other functions that do a search
1575 * on a btree. The goal is to find a tree block in the cache (the radix tree at
1576 * fs_info->buffer_radix), but if we can't find it, or it's not up to date, read
1577 * its pages from disk.
1579 * Returns -EAGAIN, with the path unlocked, if the caller needs to repeat the
1580 * whole btree search, starting again from the current root node.
1583 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
1584 struct extent_buffer **eb_ret, int level, int slot,
1585 const struct btrfs_key *key)
1587 struct btrfs_fs_info *fs_info = root->fs_info;
1588 struct btrfs_tree_parent_check check = { 0 };
1591 struct extent_buffer *tmp;
1596 unlock_up = ((level + 1 < BTRFS_MAX_LEVEL) && p->locks[level + 1]);
1597 blocknr = btrfs_node_blockptr(*eb_ret, slot);
1598 gen = btrfs_node_ptr_generation(*eb_ret, slot);
1599 parent_level = btrfs_header_level(*eb_ret);
1600 btrfs_node_key_to_cpu(*eb_ret, &check.first_key, slot);
1601 check.has_first_key = true;
1602 check.level = parent_level - 1;
1603 check.transid = gen;
1604 check.owner_root = root->root_key.objectid;
1607 * If we need to read an extent buffer from disk and we are holding locks
1608 * on upper level nodes, we unlock all the upper nodes before reading the
1609 * extent buffer, and then return -EAGAIN to the caller as it needs to
1610 * restart the search. We don't release the lock on the current level
1611 * because we need to walk this node to figure out which blocks to read.
1613 tmp = find_extent_buffer(fs_info, blocknr);
1615 if (p->reada == READA_FORWARD_ALWAYS)
1616 reada_for_search(fs_info, p, level, slot, key->objectid);
1618 /* first we do an atomic uptodate check */
1619 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
1621 * Do extra check for first_key, eb can be stale due to
1622 * being cached, read from scrub, or have multiple
1623 * parents (shared tree blocks).
1625 if (btrfs_verify_level_key(tmp,
1626 parent_level - 1, &check.first_key, gen)) {
1627 free_extent_buffer(tmp);
1635 free_extent_buffer(tmp);
1640 btrfs_unlock_up_safe(p, level + 1);
1642 /* now we're allowed to do a blocking uptodate check */
1643 ret = btrfs_read_extent_buffer(tmp, &check);
1645 free_extent_buffer(tmp);
1646 btrfs_release_path(p);
1649 if (btrfs_check_eb_owner(tmp, root->root_key.objectid)) {
1650 free_extent_buffer(tmp);
1651 btrfs_release_path(p);
1659 } else if (p->nowait) {
1664 btrfs_unlock_up_safe(p, level + 1);
1670 if (p->reada != READA_NONE)
1671 reada_for_search(fs_info, p, level, slot, key->objectid);
1673 tmp = read_tree_block(fs_info, blocknr, &check);
1675 btrfs_release_path(p);
1676 return PTR_ERR(tmp);
1679 * If the read above didn't mark this buffer up to date,
1680 * it will never end up being up to date. Set ret to EIO now
1681 * and give up so that our caller doesn't loop forever
1684 if (!extent_buffer_uptodate(tmp))
1691 free_extent_buffer(tmp);
1692 btrfs_release_path(p);
1699 * helper function for btrfs_search_slot. This does all of the checks
1700 * for node-level blocks and does any balancing required based on
1703 * If no extra work was required, zero is returned. If we had to
1704 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1708 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1709 struct btrfs_root *root, struct btrfs_path *p,
1710 struct extent_buffer *b, int level, int ins_len,
1711 int *write_lock_level)
1713 struct btrfs_fs_info *fs_info = root->fs_info;
1716 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1717 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1719 if (*write_lock_level < level + 1) {
1720 *write_lock_level = level + 1;
1721 btrfs_release_path(p);
1725 reada_for_balance(p, level);
1726 ret = split_node(trans, root, p, level);
1728 b = p->nodes[level];
1729 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1730 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
1732 if (*write_lock_level < level + 1) {
1733 *write_lock_level = level + 1;
1734 btrfs_release_path(p);
1738 reada_for_balance(p, level);
1739 ret = balance_level(trans, root, p, level);
1743 b = p->nodes[level];
1745 btrfs_release_path(p);
1748 BUG_ON(btrfs_header_nritems(b) == 1);
1753 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
1754 u64 iobjectid, u64 ioff, u8 key_type,
1755 struct btrfs_key *found_key)
1758 struct btrfs_key key;
1759 struct extent_buffer *eb;
1764 key.type = key_type;
1765 key.objectid = iobjectid;
1768 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1772 eb = path->nodes[0];
1773 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1774 ret = btrfs_next_leaf(fs_root, path);
1777 eb = path->nodes[0];
1780 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1781 if (found_key->type != key.type ||
1782 found_key->objectid != key.objectid)
1788 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
1789 struct btrfs_path *p,
1790 int write_lock_level)
1792 struct extent_buffer *b;
1796 if (p->search_commit_root) {
1797 b = root->commit_root;
1798 atomic_inc(&b->refs);
1799 level = btrfs_header_level(b);
1801 * Ensure that all callers have set skip_locking when
1802 * p->search_commit_root = 1.
1804 ASSERT(p->skip_locking == 1);
1809 if (p->skip_locking) {
1810 b = btrfs_root_node(root);
1811 level = btrfs_header_level(b);
1815 /* We try very hard to do read locks on the root */
1816 root_lock = BTRFS_READ_LOCK;
1819 * If the level is set to maximum, we can skip trying to get the read
1822 if (write_lock_level < BTRFS_MAX_LEVEL) {
1824 * We don't know the level of the root node until we actually
1825 * have it read locked
1828 b = btrfs_try_read_lock_root_node(root);
1832 b = btrfs_read_lock_root_node(root);
1834 level = btrfs_header_level(b);
1835 if (level > write_lock_level)
1838 /* Whoops, must trade for write lock */
1839 btrfs_tree_read_unlock(b);
1840 free_extent_buffer(b);
1843 b = btrfs_lock_root_node(root);
1844 root_lock = BTRFS_WRITE_LOCK;
1846 /* The level might have changed, check again */
1847 level = btrfs_header_level(b);
1851 * The root may have failed to write out at some point, and thus is no
1852 * longer valid, return an error in this case.
1854 if (!extent_buffer_uptodate(b)) {
1856 btrfs_tree_unlock_rw(b, root_lock);
1857 free_extent_buffer(b);
1858 return ERR_PTR(-EIO);
1861 p->nodes[level] = b;
1862 if (!p->skip_locking)
1863 p->locks[level] = root_lock;
1865 * Callers are responsible for dropping b's references.
1871 * Replace the extent buffer at the lowest level of the path with a cloned
1872 * version. The purpose is to be able to use it safely, after releasing the
1873 * commit root semaphore, even if relocation is happening in parallel, the
1874 * transaction used for relocation is committed and the extent buffer is
1875 * reallocated in the next transaction.
1877 * This is used in a context where the caller does not prevent transaction
1878 * commits from happening, either by holding a transaction handle or holding
1879 * some lock, while it's doing searches through a commit root.
1880 * At the moment it's only used for send operations.
1882 static int finish_need_commit_sem_search(struct btrfs_path *path)
1884 const int i = path->lowest_level;
1885 const int slot = path->slots[i];
1886 struct extent_buffer *lowest = path->nodes[i];
1887 struct extent_buffer *clone;
1889 ASSERT(path->need_commit_sem);
1894 lockdep_assert_held_read(&lowest->fs_info->commit_root_sem);
1896 clone = btrfs_clone_extent_buffer(lowest);
1900 btrfs_release_path(path);
1901 path->nodes[i] = clone;
1902 path->slots[i] = slot;
1907 static inline int search_for_key_slot(struct extent_buffer *eb,
1908 int search_low_slot,
1909 const struct btrfs_key *key,
1914 * If a previous call to btrfs_bin_search() on a parent node returned an
1915 * exact match (prev_cmp == 0), we can safely assume the target key will
1916 * always be at slot 0 on lower levels, since each key pointer
1917 * (struct btrfs_key_ptr) refers to the lowest key accessible from the
1918 * subtree it points to. Thus we can skip searching lower levels.
1920 if (prev_cmp == 0) {
1925 return btrfs_bin_search(eb, search_low_slot, key, slot);
1928 static int search_leaf(struct btrfs_trans_handle *trans,
1929 struct btrfs_root *root,
1930 const struct btrfs_key *key,
1931 struct btrfs_path *path,
1935 struct extent_buffer *leaf = path->nodes[0];
1936 int leaf_free_space = -1;
1937 int search_low_slot = 0;
1939 bool do_bin_search = true;
1942 * If we are doing an insertion, the leaf has enough free space and the
1943 * destination slot for the key is not slot 0, then we can unlock our
1944 * write lock on the parent, and any other upper nodes, before doing the
1945 * binary search on the leaf (with search_for_key_slot()), allowing other
1946 * tasks to lock the parent and any other upper nodes.
1950 * Cache the leaf free space, since we will need it later and it
1951 * will not change until then.
1953 leaf_free_space = btrfs_leaf_free_space(leaf);
1956 * !path->locks[1] means we have a single node tree, the leaf is
1957 * the root of the tree.
1959 if (path->locks[1] && leaf_free_space >= ins_len) {
1960 struct btrfs_disk_key first_key;
1962 ASSERT(btrfs_header_nritems(leaf) > 0);
1963 btrfs_item_key(leaf, &first_key, 0);
1966 * Doing the extra comparison with the first key is cheap,
1967 * taking into account that the first key is very likely
1968 * already in a cache line because it immediately follows
1969 * the extent buffer's header and we have recently accessed
1970 * the header's level field.
1972 ret = comp_keys(&first_key, key);
1975 * The first key is smaller than the key we want
1976 * to insert, so we are safe to unlock all upper
1977 * nodes and we have to do the binary search.
1979 * We do use btrfs_unlock_up_safe() and not
1980 * unlock_up() because the later does not unlock
1981 * nodes with a slot of 0 - we can safely unlock
1982 * any node even if its slot is 0 since in this
1983 * case the key does not end up at slot 0 of the
1984 * leaf and there's no need to split the leaf.
1986 btrfs_unlock_up_safe(path, 1);
1987 search_low_slot = 1;
1990 * The first key is >= then the key we want to
1991 * insert, so we can skip the binary search as
1992 * the target key will be at slot 0.
1994 * We can not unlock upper nodes when the key is
1995 * less than the first key, because we will need
1996 * to update the key at slot 0 of the parent node
1997 * and possibly of other upper nodes too.
1998 * If the key matches the first key, then we can
1999 * unlock all the upper nodes, using
2000 * btrfs_unlock_up_safe() instead of unlock_up()
2004 btrfs_unlock_up_safe(path, 1);
2006 * ret is already 0 or 1, matching the result of
2007 * a btrfs_bin_search() call, so there is no need
2010 do_bin_search = false;
2016 if (do_bin_search) {
2017 ret = search_for_key_slot(leaf, search_low_slot, key,
2018 prev_cmp, &path->slots[0]);
2025 * Item key already exists. In this case, if we are allowed to
2026 * insert the item (for example, in dir_item case, item key
2027 * collision is allowed), it will be merged with the original
2028 * item. Only the item size grows, no new btrfs item will be
2029 * added. If search_for_extension is not set, ins_len already
2030 * accounts the size btrfs_item, deduct it here so leaf space
2031 * check will be correct.
2033 if (ret == 0 && !path->search_for_extension) {
2034 ASSERT(ins_len >= sizeof(struct btrfs_item));
2035 ins_len -= sizeof(struct btrfs_item);
2038 ASSERT(leaf_free_space >= 0);
2040 if (leaf_free_space < ins_len) {
2043 err = split_leaf(trans, root, key, path, ins_len,
2046 if (WARN_ON(err > 0))
2057 * btrfs_search_slot - look for a key in a tree and perform necessary
2058 * modifications to preserve tree invariants.
2060 * @trans: Handle of transaction, used when modifying the tree
2061 * @p: Holds all btree nodes along the search path
2062 * @root: The root node of the tree
2063 * @key: The key we are looking for
2064 * @ins_len: Indicates purpose of search:
2065 * >0 for inserts it's size of item inserted (*)
2067 * 0 for plain searches, not modifying the tree
2069 * (*) If size of item inserted doesn't include
2070 * sizeof(struct btrfs_item), then p->search_for_extension must
2072 * @cow: boolean should CoW operations be performed. Must always be 1
2073 * when modifying the tree.
2075 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2076 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2078 * If @key is found, 0 is returned and you can find the item in the leaf level
2079 * of the path (level 0)
2081 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2082 * points to the slot where it should be inserted
2084 * If an error is encountered while searching the tree a negative error number
2087 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2088 const struct btrfs_key *key, struct btrfs_path *p,
2089 int ins_len, int cow)
2091 struct btrfs_fs_info *fs_info = root->fs_info;
2092 struct extent_buffer *b;
2097 int lowest_unlock = 1;
2098 /* everything at write_lock_level or lower must be write locked */
2099 int write_lock_level = 0;
2100 u8 lowest_level = 0;
2101 int min_write_lock_level;
2106 lowest_level = p->lowest_level;
2107 WARN_ON(lowest_level && ins_len > 0);
2108 WARN_ON(p->nodes[0] != NULL);
2109 BUG_ON(!cow && ins_len);
2112 * For now only allow nowait for read only operations. There's no
2113 * strict reason why we can't, we just only need it for reads so it's
2114 * only implemented for reads.
2116 ASSERT(!p->nowait || !cow);
2121 /* when we are removing items, we might have to go up to level
2122 * two as we update tree pointers Make sure we keep write
2123 * for those levels as well
2125 write_lock_level = 2;
2126 } else if (ins_len > 0) {
2128 * for inserting items, make sure we have a write lock on
2129 * level 1 so we can update keys
2131 write_lock_level = 1;
2135 write_lock_level = -1;
2137 if (cow && (p->keep_locks || p->lowest_level))
2138 write_lock_level = BTRFS_MAX_LEVEL;
2140 min_write_lock_level = write_lock_level;
2142 if (p->need_commit_sem) {
2143 ASSERT(p->search_commit_root);
2145 if (!down_read_trylock(&fs_info->commit_root_sem))
2148 down_read(&fs_info->commit_root_sem);
2154 b = btrfs_search_slot_get_root(root, p, write_lock_level);
2163 level = btrfs_header_level(b);
2166 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2169 * if we don't really need to cow this block
2170 * then we don't want to set the path blocking,
2171 * so we test it here
2173 if (!should_cow_block(trans, root, b))
2177 * must have write locks on this node and the
2180 if (level > write_lock_level ||
2181 (level + 1 > write_lock_level &&
2182 level + 1 < BTRFS_MAX_LEVEL &&
2183 p->nodes[level + 1])) {
2184 write_lock_level = level + 1;
2185 btrfs_release_path(p);
2190 err = btrfs_cow_block(trans, root, b, NULL, 0,
2194 err = btrfs_cow_block(trans, root, b,
2195 p->nodes[level + 1],
2196 p->slots[level + 1], &b,
2204 p->nodes[level] = b;
2207 * we have a lock on b and as long as we aren't changing
2208 * the tree, there is no way to for the items in b to change.
2209 * It is safe to drop the lock on our parent before we
2210 * go through the expensive btree search on b.
2212 * If we're inserting or deleting (ins_len != 0), then we might
2213 * be changing slot zero, which may require changing the parent.
2214 * So, we can't drop the lock until after we know which slot
2215 * we're operating on.
2217 if (!ins_len && !p->keep_locks) {
2220 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2221 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2228 ASSERT(write_lock_level >= 1);
2230 ret = search_leaf(trans, root, key, p, ins_len, prev_cmp);
2231 if (!p->search_for_split)
2232 unlock_up(p, level, lowest_unlock,
2233 min_write_lock_level, NULL);
2237 ret = search_for_key_slot(b, 0, key, prev_cmp, &slot);
2242 if (ret && slot > 0) {
2246 p->slots[level] = slot;
2247 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2255 b = p->nodes[level];
2256 slot = p->slots[level];
2259 * Slot 0 is special, if we change the key we have to update
2260 * the parent pointer which means we must have a write lock on
2263 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2264 write_lock_level = level + 1;
2265 btrfs_release_path(p);
2269 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2272 if (level == lowest_level) {
2278 err = read_block_for_search(root, p, &b, level, slot, key);
2286 if (!p->skip_locking) {
2287 level = btrfs_header_level(b);
2289 btrfs_maybe_reset_lockdep_class(root, b);
2291 if (level <= write_lock_level) {
2293 p->locks[level] = BTRFS_WRITE_LOCK;
2296 if (!btrfs_try_tree_read_lock(b)) {
2297 free_extent_buffer(b);
2302 btrfs_tree_read_lock(b);
2304 p->locks[level] = BTRFS_READ_LOCK;
2306 p->nodes[level] = b;
2311 if (ret < 0 && !p->skip_release_on_error)
2312 btrfs_release_path(p);
2314 if (p->need_commit_sem) {
2317 ret2 = finish_need_commit_sem_search(p);
2318 up_read(&fs_info->commit_root_sem);
2325 ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
2328 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2329 * current state of the tree together with the operations recorded in the tree
2330 * modification log to search for the key in a previous version of this tree, as
2331 * denoted by the time_seq parameter.
2333 * Naturally, there is no support for insert, delete or cow operations.
2335 * The resulting path and return value will be set up as if we called
2336 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2338 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2339 struct btrfs_path *p, u64 time_seq)
2341 struct btrfs_fs_info *fs_info = root->fs_info;
2342 struct extent_buffer *b;
2347 int lowest_unlock = 1;
2348 u8 lowest_level = 0;
2350 lowest_level = p->lowest_level;
2351 WARN_ON(p->nodes[0] != NULL);
2354 if (p->search_commit_root) {
2356 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2360 b = btrfs_get_old_root(root, time_seq);
2365 level = btrfs_header_level(b);
2366 p->locks[level] = BTRFS_READ_LOCK;
2371 level = btrfs_header_level(b);
2372 p->nodes[level] = b;
2375 * we have a lock on b and as long as we aren't changing
2376 * the tree, there is no way to for the items in b to change.
2377 * It is safe to drop the lock on our parent before we
2378 * go through the expensive btree search on b.
2380 btrfs_unlock_up_safe(p, level + 1);
2382 ret = btrfs_bin_search(b, 0, key, &slot);
2387 p->slots[level] = slot;
2388 unlock_up(p, level, lowest_unlock, 0, NULL);
2392 if (ret && slot > 0) {
2396 p->slots[level] = slot;
2397 unlock_up(p, level, lowest_unlock, 0, NULL);
2399 if (level == lowest_level) {
2405 err = read_block_for_search(root, p, &b, level, slot, key);
2413 level = btrfs_header_level(b);
2414 btrfs_tree_read_lock(b);
2415 b = btrfs_tree_mod_log_rewind(fs_info, p, b, time_seq);
2420 p->locks[level] = BTRFS_READ_LOCK;
2421 p->nodes[level] = b;
2426 btrfs_release_path(p);
2432 * Search the tree again to find a leaf with smaller keys.
2433 * Returns 0 if it found something.
2434 * Returns 1 if there are no smaller keys.
2435 * Returns < 0 on error.
2437 * This may release the path, and so you may lose any locks held at the
2440 static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2442 struct btrfs_key key;
2443 struct btrfs_key orig_key;
2444 struct btrfs_disk_key found_key;
2447 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
2450 if (key.offset > 0) {
2452 } else if (key.type > 0) {
2454 key.offset = (u64)-1;
2455 } else if (key.objectid > 0) {
2458 key.offset = (u64)-1;
2463 btrfs_release_path(path);
2464 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2469 * Previous key not found. Even if we were at slot 0 of the leaf we had
2470 * before releasing the path and calling btrfs_search_slot(), we now may
2471 * be in a slot pointing to the same original key - this can happen if
2472 * after we released the path, one of more items were moved from a
2473 * sibling leaf into the front of the leaf we had due to an insertion
2474 * (see push_leaf_right()).
2475 * If we hit this case and our slot is > 0 and just decrement the slot
2476 * so that the caller does not process the same key again, which may or
2477 * may not break the caller, depending on its logic.
2479 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
2480 btrfs_item_key(path->nodes[0], &found_key, path->slots[0]);
2481 ret = comp_keys(&found_key, &orig_key);
2483 if (path->slots[0] > 0) {
2488 * At slot 0, same key as before, it means orig_key is
2489 * the lowest, leftmost, key in the tree. We're done.
2495 btrfs_item_key(path->nodes[0], &found_key, 0);
2496 ret = comp_keys(&found_key, &key);
2498 * We might have had an item with the previous key in the tree right
2499 * before we released our path. And after we released our path, that
2500 * item might have been pushed to the first slot (0) of the leaf we
2501 * were holding due to a tree balance. Alternatively, an item with the
2502 * previous key can exist as the only element of a leaf (big fat item).
2503 * Therefore account for these 2 cases, so that our callers (like
2504 * btrfs_previous_item) don't miss an existing item with a key matching
2505 * the previous key we computed above.
2513 * helper to use instead of search slot if no exact match is needed but
2514 * instead the next or previous item should be returned.
2515 * When find_higher is true, the next higher item is returned, the next lower
2517 * When return_any and find_higher are both true, and no higher item is found,
2518 * return the next lower instead.
2519 * When return_any is true and find_higher is false, and no lower item is found,
2520 * return the next higher instead.
2521 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2524 int btrfs_search_slot_for_read(struct btrfs_root *root,
2525 const struct btrfs_key *key,
2526 struct btrfs_path *p, int find_higher,
2530 struct extent_buffer *leaf;
2533 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2537 * a return value of 1 means the path is at the position where the
2538 * item should be inserted. Normally this is the next bigger item,
2539 * but in case the previous item is the last in a leaf, path points
2540 * to the first free slot in the previous leaf, i.e. at an invalid
2546 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2547 ret = btrfs_next_leaf(root, p);
2553 * no higher item found, return the next
2558 btrfs_release_path(p);
2562 if (p->slots[0] == 0) {
2563 ret = btrfs_prev_leaf(root, p);
2568 if (p->slots[0] == btrfs_header_nritems(leaf))
2575 * no lower item found, return the next
2580 btrfs_release_path(p);
2590 * Execute search and call btrfs_previous_item to traverse backwards if the item
2593 * Return 0 if found, 1 if not found and < 0 if error.
2595 int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
2596 struct btrfs_path *path)
2600 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2602 ret = btrfs_previous_item(root, path, key->objectid, key->type);
2605 btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]);
2611 * Search for a valid slot for the given path.
2613 * @root: The root node of the tree.
2614 * @key: Will contain a valid item if found.
2615 * @path: The starting point to validate the slot.
2617 * Return: 0 if the item is valid
2621 int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
2622 struct btrfs_path *path)
2624 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2627 ret = btrfs_next_leaf(root, path);
2632 btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]);
2637 * adjust the pointers going up the tree, starting at level
2638 * making sure the right key of each node is points to 'key'.
2639 * This is used after shifting pointers to the left, so it stops
2640 * fixing up pointers when a given leaf/node is not in slot 0 of the
2644 static void fixup_low_keys(struct btrfs_path *path,
2645 struct btrfs_disk_key *key, int level)
2648 struct extent_buffer *t;
2651 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2652 int tslot = path->slots[i];
2654 if (!path->nodes[i])
2657 ret = btrfs_tree_mod_log_insert_key(t, tslot,
2658 BTRFS_MOD_LOG_KEY_REPLACE);
2660 btrfs_set_node_key(t, key, tslot);
2661 btrfs_mark_buffer_dirty(path->nodes[i]);
2670 * This function isn't completely safe. It's the caller's responsibility
2671 * that the new key won't break the order
2673 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2674 struct btrfs_path *path,
2675 const struct btrfs_key *new_key)
2677 struct btrfs_disk_key disk_key;
2678 struct extent_buffer *eb;
2681 eb = path->nodes[0];
2682 slot = path->slots[0];
2684 btrfs_item_key(eb, &disk_key, slot - 1);
2685 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
2686 btrfs_print_leaf(eb);
2688 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2689 slot, btrfs_disk_key_objectid(&disk_key),
2690 btrfs_disk_key_type(&disk_key),
2691 btrfs_disk_key_offset(&disk_key),
2692 new_key->objectid, new_key->type,
2697 if (slot < btrfs_header_nritems(eb) - 1) {
2698 btrfs_item_key(eb, &disk_key, slot + 1);
2699 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
2700 btrfs_print_leaf(eb);
2702 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2703 slot, btrfs_disk_key_objectid(&disk_key),
2704 btrfs_disk_key_type(&disk_key),
2705 btrfs_disk_key_offset(&disk_key),
2706 new_key->objectid, new_key->type,
2712 btrfs_cpu_key_to_disk(&disk_key, new_key);
2713 btrfs_set_item_key(eb, &disk_key, slot);
2714 btrfs_mark_buffer_dirty(eb);
2716 fixup_low_keys(path, &disk_key, 1);
2720 * Check key order of two sibling extent buffers.
2722 * Return true if something is wrong.
2723 * Return false if everything is fine.
2725 * Tree-checker only works inside one tree block, thus the following
2726 * corruption can not be detected by tree-checker:
2728 * Leaf @left | Leaf @right
2729 * --------------------------------------------------------------
2730 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
2732 * Key f6 in leaf @left itself is valid, but not valid when the next
2733 * key in leaf @right is 7.
2734 * This can only be checked at tree block merge time.
2735 * And since tree checker has ensured all key order in each tree block
2736 * is correct, we only need to bother the last key of @left and the first
2739 static bool check_sibling_keys(struct extent_buffer *left,
2740 struct extent_buffer *right)
2742 struct btrfs_key left_last;
2743 struct btrfs_key right_first;
2744 int level = btrfs_header_level(left);
2745 int nr_left = btrfs_header_nritems(left);
2746 int nr_right = btrfs_header_nritems(right);
2748 /* No key to check in one of the tree blocks */
2749 if (!nr_left || !nr_right)
2753 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
2754 btrfs_node_key_to_cpu(right, &right_first, 0);
2756 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
2757 btrfs_item_key_to_cpu(right, &right_first, 0);
2760 if (unlikely(btrfs_comp_cpu_keys(&left_last, &right_first) >= 0)) {
2761 btrfs_crit(left->fs_info, "left extent buffer:");
2762 btrfs_print_tree(left, false);
2763 btrfs_crit(left->fs_info, "right extent buffer:");
2764 btrfs_print_tree(right, false);
2765 btrfs_crit(left->fs_info,
2766 "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
2767 left_last.objectid, left_last.type,
2768 left_last.offset, right_first.objectid,
2769 right_first.type, right_first.offset);
2776 * try to push data from one node into the next node left in the
2779 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2780 * error, and > 0 if there was no room in the left hand block.
2782 static int push_node_left(struct btrfs_trans_handle *trans,
2783 struct extent_buffer *dst,
2784 struct extent_buffer *src, int empty)
2786 struct btrfs_fs_info *fs_info = trans->fs_info;
2792 src_nritems = btrfs_header_nritems(src);
2793 dst_nritems = btrfs_header_nritems(dst);
2794 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2795 WARN_ON(btrfs_header_generation(src) != trans->transid);
2796 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2798 if (!empty && src_nritems <= 8)
2801 if (push_items <= 0)
2805 push_items = min(src_nritems, push_items);
2806 if (push_items < src_nritems) {
2807 /* leave at least 8 pointers in the node if
2808 * we aren't going to empty it
2810 if (src_nritems - push_items < 8) {
2811 if (push_items <= 8)
2817 push_items = min(src_nritems - 8, push_items);
2819 /* dst is the left eb, src is the middle eb */
2820 if (check_sibling_keys(dst, src)) {
2822 btrfs_abort_transaction(trans, ret);
2825 ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
2827 btrfs_abort_transaction(trans, ret);
2830 copy_extent_buffer(dst, src,
2831 btrfs_node_key_ptr_offset(dst, dst_nritems),
2832 btrfs_node_key_ptr_offset(src, 0),
2833 push_items * sizeof(struct btrfs_key_ptr));
2835 if (push_items < src_nritems) {
2837 * btrfs_tree_mod_log_eb_copy handles logging the move, so we
2838 * don't need to do an explicit tree mod log operation for it.
2840 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(src, 0),
2841 btrfs_node_key_ptr_offset(src, push_items),
2842 (src_nritems - push_items) *
2843 sizeof(struct btrfs_key_ptr));
2845 btrfs_set_header_nritems(src, src_nritems - push_items);
2846 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2847 btrfs_mark_buffer_dirty(src);
2848 btrfs_mark_buffer_dirty(dst);
2854 * try to push data from one node into the next node right in the
2857 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2858 * error, and > 0 if there was no room in the right hand block.
2860 * this will only push up to 1/2 the contents of the left node over
2862 static int balance_node_right(struct btrfs_trans_handle *trans,
2863 struct extent_buffer *dst,
2864 struct extent_buffer *src)
2866 struct btrfs_fs_info *fs_info = trans->fs_info;
2873 WARN_ON(btrfs_header_generation(src) != trans->transid);
2874 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2876 src_nritems = btrfs_header_nritems(src);
2877 dst_nritems = btrfs_header_nritems(dst);
2878 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2879 if (push_items <= 0)
2882 if (src_nritems < 4)
2885 max_push = src_nritems / 2 + 1;
2886 /* don't try to empty the node */
2887 if (max_push >= src_nritems)
2890 if (max_push < push_items)
2891 push_items = max_push;
2893 /* dst is the right eb, src is the middle eb */
2894 if (check_sibling_keys(src, dst)) {
2896 btrfs_abort_transaction(trans, ret);
2901 * btrfs_tree_mod_log_eb_copy handles logging the move, so we don't
2902 * need to do an explicit tree mod log operation for it.
2904 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(dst, push_items),
2905 btrfs_node_key_ptr_offset(dst, 0),
2907 sizeof(struct btrfs_key_ptr));
2909 ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
2912 btrfs_abort_transaction(trans, ret);
2915 copy_extent_buffer(dst, src,
2916 btrfs_node_key_ptr_offset(dst, 0),
2917 btrfs_node_key_ptr_offset(src, src_nritems - push_items),
2918 push_items * sizeof(struct btrfs_key_ptr));
2920 btrfs_set_header_nritems(src, src_nritems - push_items);
2921 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2923 btrfs_mark_buffer_dirty(src);
2924 btrfs_mark_buffer_dirty(dst);
2930 * helper function to insert a new root level in the tree.
2931 * A new node is allocated, and a single item is inserted to
2932 * point to the existing root
2934 * returns zero on success or < 0 on failure.
2936 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2937 struct btrfs_root *root,
2938 struct btrfs_path *path, int level)
2940 struct btrfs_fs_info *fs_info = root->fs_info;
2942 struct extent_buffer *lower;
2943 struct extent_buffer *c;
2944 struct extent_buffer *old;
2945 struct btrfs_disk_key lower_key;
2948 BUG_ON(path->nodes[level]);
2949 BUG_ON(path->nodes[level-1] != root->node);
2951 lower = path->nodes[level-1];
2953 btrfs_item_key(lower, &lower_key, 0);
2955 btrfs_node_key(lower, &lower_key, 0);
2957 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
2958 &lower_key, level, root->node->start, 0,
2959 BTRFS_NESTING_NEW_ROOT);
2963 root_add_used(root, fs_info->nodesize);
2965 btrfs_set_header_nritems(c, 1);
2966 btrfs_set_node_key(c, &lower_key, 0);
2967 btrfs_set_node_blockptr(c, 0, lower->start);
2968 lower_gen = btrfs_header_generation(lower);
2969 WARN_ON(lower_gen != trans->transid);
2971 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2973 btrfs_mark_buffer_dirty(c);
2976 ret = btrfs_tree_mod_log_insert_root(root->node, c, false);
2978 btrfs_free_tree_block(trans, btrfs_root_id(root), c, 0, 1);
2979 btrfs_tree_unlock(c);
2980 free_extent_buffer(c);
2983 rcu_assign_pointer(root->node, c);
2985 /* the super has an extra ref to root->node */
2986 free_extent_buffer(old);
2988 add_root_to_dirty_list(root);
2989 atomic_inc(&c->refs);
2990 path->nodes[level] = c;
2991 path->locks[level] = BTRFS_WRITE_LOCK;
2992 path->slots[level] = 0;
2997 * worker function to insert a single pointer in a node.
2998 * the node should have enough room for the pointer already
3000 * slot and level indicate where you want the key to go, and
3001 * blocknr is the block the key points to.
3003 static int insert_ptr(struct btrfs_trans_handle *trans,
3004 struct btrfs_path *path,
3005 struct btrfs_disk_key *key, u64 bytenr,
3006 int slot, int level)
3008 struct extent_buffer *lower;
3012 BUG_ON(!path->nodes[level]);
3013 btrfs_assert_tree_write_locked(path->nodes[level]);
3014 lower = path->nodes[level];
3015 nritems = btrfs_header_nritems(lower);
3016 BUG_ON(slot > nritems);
3017 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
3018 if (slot != nritems) {
3020 ret = btrfs_tree_mod_log_insert_move(lower, slot + 1,
3021 slot, nritems - slot);
3023 btrfs_abort_transaction(trans, ret);
3027 memmove_extent_buffer(lower,
3028 btrfs_node_key_ptr_offset(lower, slot + 1),
3029 btrfs_node_key_ptr_offset(lower, slot),
3030 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3033 ret = btrfs_tree_mod_log_insert_key(lower, slot,
3034 BTRFS_MOD_LOG_KEY_ADD);
3036 btrfs_abort_transaction(trans, ret);
3040 btrfs_set_node_key(lower, key, slot);
3041 btrfs_set_node_blockptr(lower, slot, bytenr);
3042 WARN_ON(trans->transid == 0);
3043 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3044 btrfs_set_header_nritems(lower, nritems + 1);
3045 btrfs_mark_buffer_dirty(lower);
3051 * split the node at the specified level in path in two.
3052 * The path is corrected to point to the appropriate node after the split
3054 * Before splitting this tries to make some room in the node by pushing
3055 * left and right, if either one works, it returns right away.
3057 * returns 0 on success and < 0 on failure
3059 static noinline int split_node(struct btrfs_trans_handle *trans,
3060 struct btrfs_root *root,
3061 struct btrfs_path *path, int level)
3063 struct btrfs_fs_info *fs_info = root->fs_info;
3064 struct extent_buffer *c;
3065 struct extent_buffer *split;
3066 struct btrfs_disk_key disk_key;
3071 c = path->nodes[level];
3072 WARN_ON(btrfs_header_generation(c) != trans->transid);
3073 if (c == root->node) {
3075 * trying to split the root, lets make a new one
3077 * tree mod log: We don't log_removal old root in
3078 * insert_new_root, because that root buffer will be kept as a
3079 * normal node. We are going to log removal of half of the
3080 * elements below with btrfs_tree_mod_log_eb_copy(). We're
3081 * holding a tree lock on the buffer, which is why we cannot
3082 * race with other tree_mod_log users.
3084 ret = insert_new_root(trans, root, path, level + 1);
3088 ret = push_nodes_for_insert(trans, root, path, level);
3089 c = path->nodes[level];
3090 if (!ret && btrfs_header_nritems(c) <
3091 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3097 c_nritems = btrfs_header_nritems(c);
3098 mid = (c_nritems + 1) / 2;
3099 btrfs_node_key(c, &disk_key, mid);
3101 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3102 &disk_key, level, c->start, 0,
3103 BTRFS_NESTING_SPLIT);
3105 return PTR_ERR(split);
3107 root_add_used(root, fs_info->nodesize);
3108 ASSERT(btrfs_header_level(c) == level);
3110 ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
3112 btrfs_tree_unlock(split);
3113 free_extent_buffer(split);
3114 btrfs_abort_transaction(trans, ret);
3117 copy_extent_buffer(split, c,
3118 btrfs_node_key_ptr_offset(split, 0),
3119 btrfs_node_key_ptr_offset(c, mid),
3120 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3121 btrfs_set_header_nritems(split, c_nritems - mid);
3122 btrfs_set_header_nritems(c, mid);
3124 btrfs_mark_buffer_dirty(c);
3125 btrfs_mark_buffer_dirty(split);
3127 ret = insert_ptr(trans, path, &disk_key, split->start,
3128 path->slots[level + 1] + 1, level + 1);
3130 btrfs_tree_unlock(split);
3131 free_extent_buffer(split);
3135 if (path->slots[level] >= mid) {
3136 path->slots[level] -= mid;
3137 btrfs_tree_unlock(c);
3138 free_extent_buffer(c);
3139 path->nodes[level] = split;
3140 path->slots[level + 1] += 1;
3142 btrfs_tree_unlock(split);
3143 free_extent_buffer(split);
3149 * how many bytes are required to store the items in a leaf. start
3150 * and nr indicate which items in the leaf to check. This totals up the
3151 * space used both by the item structs and the item data
3153 static int leaf_space_used(const struct extent_buffer *l, int start, int nr)
3156 int nritems = btrfs_header_nritems(l);
3157 int end = min(nritems, start + nr) - 1;
3161 data_len = btrfs_item_offset(l, start) + btrfs_item_size(l, start);
3162 data_len = data_len - btrfs_item_offset(l, end);
3163 data_len += sizeof(struct btrfs_item) * nr;
3164 WARN_ON(data_len < 0);
3169 * The space between the end of the leaf items and
3170 * the start of the leaf data. IOW, how much room
3171 * the leaf has left for both items and data
3173 int btrfs_leaf_free_space(const struct extent_buffer *leaf)
3175 struct btrfs_fs_info *fs_info = leaf->fs_info;
3176 int nritems = btrfs_header_nritems(leaf);
3179 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3182 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3184 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3185 leaf_space_used(leaf, 0, nritems), nritems);
3191 * min slot controls the lowest index we're willing to push to the
3192 * right. We'll push up to and including min_slot, but no lower
3194 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3195 struct btrfs_path *path,
3196 int data_size, int empty,
3197 struct extent_buffer *right,
3198 int free_space, u32 left_nritems,
3201 struct btrfs_fs_info *fs_info = right->fs_info;
3202 struct extent_buffer *left = path->nodes[0];
3203 struct extent_buffer *upper = path->nodes[1];
3204 struct btrfs_map_token token;
3205 struct btrfs_disk_key disk_key;
3218 nr = max_t(u32, 1, min_slot);
3220 if (path->slots[0] >= left_nritems)
3221 push_space += data_size;
3223 slot = path->slots[1];
3224 i = left_nritems - 1;
3226 if (!empty && push_items > 0) {
3227 if (path->slots[0] > i)
3229 if (path->slots[0] == i) {
3230 int space = btrfs_leaf_free_space(left);
3232 if (space + push_space * 2 > free_space)
3237 if (path->slots[0] == i)
3238 push_space += data_size;
3240 this_item_size = btrfs_item_size(left, i);
3241 if (this_item_size + sizeof(struct btrfs_item) +
3242 push_space > free_space)
3246 push_space += this_item_size + sizeof(struct btrfs_item);
3252 if (push_items == 0)
3255 WARN_ON(!empty && push_items == left_nritems);
3257 /* push left to right */
3258 right_nritems = btrfs_header_nritems(right);
3260 push_space = btrfs_item_data_end(left, left_nritems - push_items);
3261 push_space -= leaf_data_end(left);
3263 /* make room in the right data area */
3264 data_end = leaf_data_end(right);
3265 memmove_leaf_data(right, data_end - push_space, data_end,
3266 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3268 /* copy from the left data area */
3269 copy_leaf_data(right, left, BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3270 leaf_data_end(left), push_space);
3272 memmove_leaf_items(right, push_items, 0, right_nritems);
3274 /* copy the items from left to right */
3275 copy_leaf_items(right, left, 0, left_nritems - push_items, push_items);
3277 /* update the item pointers */
3278 btrfs_init_map_token(&token, right);
3279 right_nritems += push_items;
3280 btrfs_set_header_nritems(right, right_nritems);
3281 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3282 for (i = 0; i < right_nritems; i++) {
3283 push_space -= btrfs_token_item_size(&token, i);
3284 btrfs_set_token_item_offset(&token, i, push_space);
3287 left_nritems -= push_items;
3288 btrfs_set_header_nritems(left, left_nritems);
3291 btrfs_mark_buffer_dirty(left);
3293 btrfs_clear_buffer_dirty(trans, left);
3295 btrfs_mark_buffer_dirty(right);
3297 btrfs_item_key(right, &disk_key, 0);
3298 btrfs_set_node_key(upper, &disk_key, slot + 1);
3299 btrfs_mark_buffer_dirty(upper);
3301 /* then fixup the leaf pointer in the path */
3302 if (path->slots[0] >= left_nritems) {
3303 path->slots[0] -= left_nritems;
3304 if (btrfs_header_nritems(path->nodes[0]) == 0)
3305 btrfs_clear_buffer_dirty(trans, path->nodes[0]);
3306 btrfs_tree_unlock(path->nodes[0]);
3307 free_extent_buffer(path->nodes[0]);
3308 path->nodes[0] = right;
3309 path->slots[1] += 1;
3311 btrfs_tree_unlock(right);
3312 free_extent_buffer(right);
3317 btrfs_tree_unlock(right);
3318 free_extent_buffer(right);
3323 * push some data in the path leaf to the right, trying to free up at
3324 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3326 * returns 1 if the push failed because the other node didn't have enough
3327 * room, 0 if everything worked out and < 0 if there were major errors.
3329 * this will push starting from min_slot to the end of the leaf. It won't
3330 * push any slot lower than min_slot
3332 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3333 *root, struct btrfs_path *path,
3334 int min_data_size, int data_size,
3335 int empty, u32 min_slot)
3337 struct extent_buffer *left = path->nodes[0];
3338 struct extent_buffer *right;
3339 struct extent_buffer *upper;
3345 if (!path->nodes[1])
3348 slot = path->slots[1];
3349 upper = path->nodes[1];
3350 if (slot >= btrfs_header_nritems(upper) - 1)
3353 btrfs_assert_tree_write_locked(path->nodes[1]);
3355 right = btrfs_read_node_slot(upper, slot + 1);
3357 return PTR_ERR(right);
3359 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
3361 free_space = btrfs_leaf_free_space(right);
3362 if (free_space < data_size)
3365 ret = btrfs_cow_block(trans, root, right, upper,
3366 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
3370 left_nritems = btrfs_header_nritems(left);
3371 if (left_nritems == 0)
3374 if (check_sibling_keys(left, right)) {
3376 btrfs_abort_transaction(trans, ret);
3377 btrfs_tree_unlock(right);
3378 free_extent_buffer(right);
3381 if (path->slots[0] == left_nritems && !empty) {
3382 /* Key greater than all keys in the leaf, right neighbor has
3383 * enough room for it and we're not emptying our leaf to delete
3384 * it, therefore use right neighbor to insert the new item and
3385 * no need to touch/dirty our left leaf. */
3386 btrfs_tree_unlock(left);
3387 free_extent_buffer(left);
3388 path->nodes[0] = right;
3394 return __push_leaf_right(trans, path, min_data_size, empty, right,
3395 free_space, left_nritems, min_slot);
3397 btrfs_tree_unlock(right);
3398 free_extent_buffer(right);
3403 * push some data in the path leaf to the left, trying to free up at
3404 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3406 * max_slot can put a limit on how far into the leaf we'll push items. The
3407 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3410 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3411 struct btrfs_path *path, int data_size,
3412 int empty, struct extent_buffer *left,
3413 int free_space, u32 right_nritems,
3416 struct btrfs_fs_info *fs_info = left->fs_info;
3417 struct btrfs_disk_key disk_key;
3418 struct extent_buffer *right = path->nodes[0];
3422 u32 old_left_nritems;
3426 u32 old_left_item_size;
3427 struct btrfs_map_token token;
3430 nr = min(right_nritems, max_slot);
3432 nr = min(right_nritems - 1, max_slot);
3434 for (i = 0; i < nr; i++) {
3435 if (!empty && push_items > 0) {
3436 if (path->slots[0] < i)
3438 if (path->slots[0] == i) {
3439 int space = btrfs_leaf_free_space(right);
3441 if (space + push_space * 2 > free_space)
3446 if (path->slots[0] == i)
3447 push_space += data_size;
3449 this_item_size = btrfs_item_size(right, i);
3450 if (this_item_size + sizeof(struct btrfs_item) + push_space >
3455 push_space += this_item_size + sizeof(struct btrfs_item);
3458 if (push_items == 0) {
3462 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3464 /* push data from right to left */
3465 copy_leaf_items(left, right, btrfs_header_nritems(left), 0, push_items);
3467 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3468 btrfs_item_offset(right, push_items - 1);
3470 copy_leaf_data(left, right, leaf_data_end(left) - push_space,
3471 btrfs_item_offset(right, push_items - 1), push_space);
3472 old_left_nritems = btrfs_header_nritems(left);
3473 BUG_ON(old_left_nritems <= 0);
3475 btrfs_init_map_token(&token, left);
3476 old_left_item_size = btrfs_item_offset(left, old_left_nritems - 1);
3477 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3480 ioff = btrfs_token_item_offset(&token, i);
3481 btrfs_set_token_item_offset(&token, i,
3482 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
3484 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3486 /* fixup right node */
3487 if (push_items > right_nritems)
3488 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3491 if (push_items < right_nritems) {
3492 push_space = btrfs_item_offset(right, push_items - 1) -
3493 leaf_data_end(right);
3494 memmove_leaf_data(right,
3495 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3496 leaf_data_end(right), push_space);
3498 memmove_leaf_items(right, 0, push_items,
3499 btrfs_header_nritems(right) - push_items);
3502 btrfs_init_map_token(&token, right);
3503 right_nritems -= push_items;
3504 btrfs_set_header_nritems(right, right_nritems);
3505 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3506 for (i = 0; i < right_nritems; i++) {
3507 push_space = push_space - btrfs_token_item_size(&token, i);
3508 btrfs_set_token_item_offset(&token, i, push_space);
3511 btrfs_mark_buffer_dirty(left);
3513 btrfs_mark_buffer_dirty(right);
3515 btrfs_clear_buffer_dirty(trans, right);
3517 btrfs_item_key(right, &disk_key, 0);
3518 fixup_low_keys(path, &disk_key, 1);
3520 /* then fixup the leaf pointer in the path */
3521 if (path->slots[0] < push_items) {
3522 path->slots[0] += old_left_nritems;
3523 btrfs_tree_unlock(path->nodes[0]);
3524 free_extent_buffer(path->nodes[0]);
3525 path->nodes[0] = left;
3526 path->slots[1] -= 1;
3528 btrfs_tree_unlock(left);
3529 free_extent_buffer(left);
3530 path->slots[0] -= push_items;
3532 BUG_ON(path->slots[0] < 0);
3535 btrfs_tree_unlock(left);
3536 free_extent_buffer(left);
3541 * push some data in the path leaf to the left, trying to free up at
3542 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3544 * max_slot can put a limit on how far into the leaf we'll push items. The
3545 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3548 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3549 *root, struct btrfs_path *path, int min_data_size,
3550 int data_size, int empty, u32 max_slot)
3552 struct extent_buffer *right = path->nodes[0];
3553 struct extent_buffer *left;
3559 slot = path->slots[1];
3562 if (!path->nodes[1])
3565 right_nritems = btrfs_header_nritems(right);
3566 if (right_nritems == 0)
3569 btrfs_assert_tree_write_locked(path->nodes[1]);
3571 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
3573 return PTR_ERR(left);
3575 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
3577 free_space = btrfs_leaf_free_space(left);
3578 if (free_space < data_size) {
3583 ret = btrfs_cow_block(trans, root, left,
3584 path->nodes[1], slot - 1, &left,
3585 BTRFS_NESTING_LEFT_COW);
3587 /* we hit -ENOSPC, but it isn't fatal here */
3593 if (check_sibling_keys(left, right)) {
3595 btrfs_abort_transaction(trans, ret);
3598 return __push_leaf_left(trans, path, min_data_size, empty, left,
3599 free_space, right_nritems, max_slot);
3601 btrfs_tree_unlock(left);
3602 free_extent_buffer(left);
3607 * split the path's leaf in two, making sure there is at least data_size
3608 * available for the resulting leaf level of the path.
3610 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
3611 struct btrfs_path *path,
3612 struct extent_buffer *l,
3613 struct extent_buffer *right,
3614 int slot, int mid, int nritems)
3616 struct btrfs_fs_info *fs_info = trans->fs_info;
3621 struct btrfs_disk_key disk_key;
3622 struct btrfs_map_token token;
3624 nritems = nritems - mid;
3625 btrfs_set_header_nritems(right, nritems);
3626 data_copy_size = btrfs_item_data_end(l, mid) - leaf_data_end(l);
3628 copy_leaf_items(right, l, 0, mid, nritems);
3630 copy_leaf_data(right, l, BTRFS_LEAF_DATA_SIZE(fs_info) - data_copy_size,
3631 leaf_data_end(l), data_copy_size);
3633 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_data_end(l, mid);
3635 btrfs_init_map_token(&token, right);
3636 for (i = 0; i < nritems; i++) {
3639 ioff = btrfs_token_item_offset(&token, i);
3640 btrfs_set_token_item_offset(&token, i, ioff + rt_data_off);
3643 btrfs_set_header_nritems(l, mid);
3644 btrfs_item_key(right, &disk_key, 0);
3645 ret = insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
3649 btrfs_mark_buffer_dirty(right);
3650 btrfs_mark_buffer_dirty(l);
3651 BUG_ON(path->slots[0] != slot);
3654 btrfs_tree_unlock(path->nodes[0]);
3655 free_extent_buffer(path->nodes[0]);
3656 path->nodes[0] = right;
3657 path->slots[0] -= mid;
3658 path->slots[1] += 1;
3660 btrfs_tree_unlock(right);
3661 free_extent_buffer(right);
3664 BUG_ON(path->slots[0] < 0);
3670 * double splits happen when we need to insert a big item in the middle
3671 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3672 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3675 * We avoid this by trying to push the items on either side of our target
3676 * into the adjacent leaves. If all goes well we can avoid the double split
3679 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3680 struct btrfs_root *root,
3681 struct btrfs_path *path,
3688 int space_needed = data_size;
3690 slot = path->slots[0];
3691 if (slot < btrfs_header_nritems(path->nodes[0]))
3692 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3695 * try to push all the items after our slot into the
3698 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
3705 nritems = btrfs_header_nritems(path->nodes[0]);
3707 * our goal is to get our slot at the start or end of a leaf. If
3708 * we've done so we're done
3710 if (path->slots[0] == 0 || path->slots[0] == nritems)
3713 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3716 /* try to push all the items before our slot into the next leaf */
3717 slot = path->slots[0];
3718 space_needed = data_size;
3720 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3721 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
3734 * split the path's leaf in two, making sure there is at least data_size
3735 * available for the resulting leaf level of the path.
3737 * returns 0 if all went well and < 0 on failure.
3739 static noinline int split_leaf(struct btrfs_trans_handle *trans,
3740 struct btrfs_root *root,
3741 const struct btrfs_key *ins_key,
3742 struct btrfs_path *path, int data_size,
3745 struct btrfs_disk_key disk_key;
3746 struct extent_buffer *l;
3750 struct extent_buffer *right;
3751 struct btrfs_fs_info *fs_info = root->fs_info;
3755 int num_doubles = 0;
3756 int tried_avoid_double = 0;
3759 slot = path->slots[0];
3760 if (extend && data_size + btrfs_item_size(l, slot) +
3761 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
3764 /* first try to make some room by pushing left and right */
3765 if (data_size && path->nodes[1]) {
3766 int space_needed = data_size;
3768 if (slot < btrfs_header_nritems(l))
3769 space_needed -= btrfs_leaf_free_space(l);
3771 wret = push_leaf_right(trans, root, path, space_needed,
3772 space_needed, 0, 0);
3776 space_needed = data_size;
3778 space_needed -= btrfs_leaf_free_space(l);
3779 wret = push_leaf_left(trans, root, path, space_needed,
3780 space_needed, 0, (u32)-1);
3786 /* did the pushes work? */
3787 if (btrfs_leaf_free_space(l) >= data_size)
3791 if (!path->nodes[1]) {
3792 ret = insert_new_root(trans, root, path, 1);
3799 slot = path->slots[0];
3800 nritems = btrfs_header_nritems(l);
3801 mid = (nritems + 1) / 2;
3805 leaf_space_used(l, mid, nritems - mid) + data_size >
3806 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3807 if (slot >= nritems) {
3811 if (mid != nritems &&
3812 leaf_space_used(l, mid, nritems - mid) +
3813 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3814 if (data_size && !tried_avoid_double)
3815 goto push_for_double;
3821 if (leaf_space_used(l, 0, mid) + data_size >
3822 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3823 if (!extend && data_size && slot == 0) {
3825 } else if ((extend || !data_size) && slot == 0) {
3829 if (mid != nritems &&
3830 leaf_space_used(l, mid, nritems - mid) +
3831 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3832 if (data_size && !tried_avoid_double)
3833 goto push_for_double;
3841 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3843 btrfs_item_key(l, &disk_key, mid);
3846 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
3847 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
3848 * subclasses, which is 8 at the time of this patch, and we've maxed it
3849 * out. In the future we could add a
3850 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
3851 * use BTRFS_NESTING_NEW_ROOT.
3853 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3854 &disk_key, 0, l->start, 0,
3855 num_doubles ? BTRFS_NESTING_NEW_ROOT :
3856 BTRFS_NESTING_SPLIT);
3858 return PTR_ERR(right);
3860 root_add_used(root, fs_info->nodesize);
3864 btrfs_set_header_nritems(right, 0);
3865 ret = insert_ptr(trans, path, &disk_key,
3866 right->start, path->slots[1] + 1, 1);
3868 btrfs_tree_unlock(right);
3869 free_extent_buffer(right);
3872 btrfs_tree_unlock(path->nodes[0]);
3873 free_extent_buffer(path->nodes[0]);
3874 path->nodes[0] = right;
3876 path->slots[1] += 1;
3878 btrfs_set_header_nritems(right, 0);
3879 ret = insert_ptr(trans, path, &disk_key,
3880 right->start, path->slots[1], 1);
3882 btrfs_tree_unlock(right);
3883 free_extent_buffer(right);
3886 btrfs_tree_unlock(path->nodes[0]);
3887 free_extent_buffer(path->nodes[0]);
3888 path->nodes[0] = right;
3890 if (path->slots[1] == 0)
3891 fixup_low_keys(path, &disk_key, 1);
3894 * We create a new leaf 'right' for the required ins_len and
3895 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
3896 * the content of ins_len to 'right'.
3901 ret = copy_for_split(trans, path, l, right, slot, mid, nritems);
3903 btrfs_tree_unlock(right);
3904 free_extent_buffer(right);
3909 BUG_ON(num_doubles != 0);
3917 push_for_double_split(trans, root, path, data_size);
3918 tried_avoid_double = 1;
3919 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3924 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3925 struct btrfs_root *root,
3926 struct btrfs_path *path, int ins_len)
3928 struct btrfs_key key;
3929 struct extent_buffer *leaf;
3930 struct btrfs_file_extent_item *fi;
3935 leaf = path->nodes[0];
3936 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3938 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3939 key.type != BTRFS_EXTENT_CSUM_KEY);
3941 if (btrfs_leaf_free_space(leaf) >= ins_len)
3944 item_size = btrfs_item_size(leaf, path->slots[0]);
3945 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3946 fi = btrfs_item_ptr(leaf, path->slots[0],
3947 struct btrfs_file_extent_item);
3948 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3950 btrfs_release_path(path);
3952 path->keep_locks = 1;
3953 path->search_for_split = 1;
3954 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3955 path->search_for_split = 0;
3962 leaf = path->nodes[0];
3963 /* if our item isn't there, return now */
3964 if (item_size != btrfs_item_size(leaf, path->slots[0]))
3967 /* the leaf has changed, it now has room. return now */
3968 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
3971 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3972 fi = btrfs_item_ptr(leaf, path->slots[0],
3973 struct btrfs_file_extent_item);
3974 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3978 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3982 path->keep_locks = 0;
3983 btrfs_unlock_up_safe(path, 1);
3986 path->keep_locks = 0;
3990 static noinline int split_item(struct btrfs_path *path,
3991 const struct btrfs_key *new_key,
3992 unsigned long split_offset)
3994 struct extent_buffer *leaf;
3995 int orig_slot, slot;
4000 struct btrfs_disk_key disk_key;
4002 leaf = path->nodes[0];
4004 * Shouldn't happen because the caller must have previously called
4005 * setup_leaf_for_split() to make room for the new item in the leaf.
4007 if (WARN_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)))
4010 orig_slot = path->slots[0];
4011 orig_offset = btrfs_item_offset(leaf, path->slots[0]);
4012 item_size = btrfs_item_size(leaf, path->slots[0]);
4014 buf = kmalloc(item_size, GFP_NOFS);
4018 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4019 path->slots[0]), item_size);
4021 slot = path->slots[0] + 1;
4022 nritems = btrfs_header_nritems(leaf);
4023 if (slot != nritems) {
4024 /* shift the items */
4025 memmove_leaf_items(leaf, slot + 1, slot, nritems - slot);
4028 btrfs_cpu_key_to_disk(&disk_key, new_key);
4029 btrfs_set_item_key(leaf, &disk_key, slot);
4031 btrfs_set_item_offset(leaf, slot, orig_offset);
4032 btrfs_set_item_size(leaf, slot, item_size - split_offset);
4034 btrfs_set_item_offset(leaf, orig_slot,
4035 orig_offset + item_size - split_offset);
4036 btrfs_set_item_size(leaf, orig_slot, split_offset);
4038 btrfs_set_header_nritems(leaf, nritems + 1);
4040 /* write the data for the start of the original item */
4041 write_extent_buffer(leaf, buf,
4042 btrfs_item_ptr_offset(leaf, path->slots[0]),
4045 /* write the data for the new item */
4046 write_extent_buffer(leaf, buf + split_offset,
4047 btrfs_item_ptr_offset(leaf, slot),
4048 item_size - split_offset);
4049 btrfs_mark_buffer_dirty(leaf);
4051 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
4057 * This function splits a single item into two items,
4058 * giving 'new_key' to the new item and splitting the
4059 * old one at split_offset (from the start of the item).
4061 * The path may be released by this operation. After
4062 * the split, the path is pointing to the old item. The
4063 * new item is going to be in the same node as the old one.
4065 * Note, the item being split must be smaller enough to live alone on
4066 * a tree block with room for one extra struct btrfs_item
4068 * This allows us to split the item in place, keeping a lock on the
4069 * leaf the entire time.
4071 int btrfs_split_item(struct btrfs_trans_handle *trans,
4072 struct btrfs_root *root,
4073 struct btrfs_path *path,
4074 const struct btrfs_key *new_key,
4075 unsigned long split_offset)
4078 ret = setup_leaf_for_split(trans, root, path,
4079 sizeof(struct btrfs_item));
4083 ret = split_item(path, new_key, split_offset);
4088 * make the item pointed to by the path smaller. new_size indicates
4089 * how small to make it, and from_end tells us if we just chop bytes
4090 * off the end of the item or if we shift the item to chop bytes off
4093 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
4096 struct extent_buffer *leaf;
4098 unsigned int data_end;
4099 unsigned int old_data_start;
4100 unsigned int old_size;
4101 unsigned int size_diff;
4103 struct btrfs_map_token token;
4105 leaf = path->nodes[0];
4106 slot = path->slots[0];
4108 old_size = btrfs_item_size(leaf, slot);
4109 if (old_size == new_size)
4112 nritems = btrfs_header_nritems(leaf);
4113 data_end = leaf_data_end(leaf);
4115 old_data_start = btrfs_item_offset(leaf, slot);
4117 size_diff = old_size - new_size;
4120 BUG_ON(slot >= nritems);
4123 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4125 /* first correct the data pointers */
4126 btrfs_init_map_token(&token, leaf);
4127 for (i = slot; i < nritems; i++) {
4130 ioff = btrfs_token_item_offset(&token, i);
4131 btrfs_set_token_item_offset(&token, i, ioff + size_diff);
4134 /* shift the data */
4136 memmove_leaf_data(leaf, data_end + size_diff, data_end,
4137 old_data_start + new_size - data_end);
4139 struct btrfs_disk_key disk_key;
4142 btrfs_item_key(leaf, &disk_key, slot);
4144 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4146 struct btrfs_file_extent_item *fi;
4148 fi = btrfs_item_ptr(leaf, slot,
4149 struct btrfs_file_extent_item);
4150 fi = (struct btrfs_file_extent_item *)(
4151 (unsigned long)fi - size_diff);
4153 if (btrfs_file_extent_type(leaf, fi) ==
4154 BTRFS_FILE_EXTENT_INLINE) {
4155 ptr = btrfs_item_ptr_offset(leaf, slot);
4156 memmove_extent_buffer(leaf, ptr,
4158 BTRFS_FILE_EXTENT_INLINE_DATA_START);
4162 memmove_leaf_data(leaf, data_end + size_diff, data_end,
4163 old_data_start - data_end);
4165 offset = btrfs_disk_key_offset(&disk_key);
4166 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4167 btrfs_set_item_key(leaf, &disk_key, slot);
4169 fixup_low_keys(path, &disk_key, 1);
4172 btrfs_set_item_size(leaf, slot, new_size);
4173 btrfs_mark_buffer_dirty(leaf);
4175 if (btrfs_leaf_free_space(leaf) < 0) {
4176 btrfs_print_leaf(leaf);
4182 * make the item pointed to by the path bigger, data_size is the added size.
4184 void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
4187 struct extent_buffer *leaf;
4189 unsigned int data_end;
4190 unsigned int old_data;
4191 unsigned int old_size;
4193 struct btrfs_map_token token;
4195 leaf = path->nodes[0];
4197 nritems = btrfs_header_nritems(leaf);
4198 data_end = leaf_data_end(leaf);
4200 if (btrfs_leaf_free_space(leaf) < data_size) {
4201 btrfs_print_leaf(leaf);
4204 slot = path->slots[0];
4205 old_data = btrfs_item_data_end(leaf, slot);
4208 if (slot >= nritems) {
4209 btrfs_print_leaf(leaf);
4210 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
4216 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4218 /* first correct the data pointers */
4219 btrfs_init_map_token(&token, leaf);
4220 for (i = slot; i < nritems; i++) {
4223 ioff = btrfs_token_item_offset(&token, i);
4224 btrfs_set_token_item_offset(&token, i, ioff - data_size);
4227 /* shift the data */
4228 memmove_leaf_data(leaf, data_end - data_size, data_end,
4229 old_data - data_end);
4231 data_end = old_data;
4232 old_size = btrfs_item_size(leaf, slot);
4233 btrfs_set_item_size(leaf, slot, old_size + data_size);
4234 btrfs_mark_buffer_dirty(leaf);
4236 if (btrfs_leaf_free_space(leaf) < 0) {
4237 btrfs_print_leaf(leaf);
4243 * Make space in the node before inserting one or more items.
4245 * @root: root we are inserting items to
4246 * @path: points to the leaf/slot where we are going to insert new items
4247 * @batch: information about the batch of items to insert
4249 * Main purpose is to save stack depth by doing the bulk of the work in a
4250 * function that doesn't call btrfs_search_slot
4252 static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4253 const struct btrfs_item_batch *batch)
4255 struct btrfs_fs_info *fs_info = root->fs_info;
4258 unsigned int data_end;
4259 struct btrfs_disk_key disk_key;
4260 struct extent_buffer *leaf;
4262 struct btrfs_map_token token;
4266 * Before anything else, update keys in the parent and other ancestors
4267 * if needed, then release the write locks on them, so that other tasks
4268 * can use them while we modify the leaf.
4270 if (path->slots[0] == 0) {
4271 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[0]);
4272 fixup_low_keys(path, &disk_key, 1);
4274 btrfs_unlock_up_safe(path, 1);
4276 leaf = path->nodes[0];
4277 slot = path->slots[0];
4279 nritems = btrfs_header_nritems(leaf);
4280 data_end = leaf_data_end(leaf);
4281 total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
4283 if (btrfs_leaf_free_space(leaf) < total_size) {
4284 btrfs_print_leaf(leaf);
4285 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4286 total_size, btrfs_leaf_free_space(leaf));
4290 btrfs_init_map_token(&token, leaf);
4291 if (slot != nritems) {
4292 unsigned int old_data = btrfs_item_data_end(leaf, slot);
4294 if (old_data < data_end) {
4295 btrfs_print_leaf(leaf);
4297 "item at slot %d with data offset %u beyond data end of leaf %u",
4298 slot, old_data, data_end);
4302 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4304 /* first correct the data pointers */
4305 for (i = slot; i < nritems; i++) {
4308 ioff = btrfs_token_item_offset(&token, i);
4309 btrfs_set_token_item_offset(&token, i,
4310 ioff - batch->total_data_size);
4312 /* shift the items */
4313 memmove_leaf_items(leaf, slot + batch->nr, slot, nritems - slot);
4315 /* shift the data */
4316 memmove_leaf_data(leaf, data_end - batch->total_data_size,
4317 data_end, old_data - data_end);
4318 data_end = old_data;
4321 /* setup the item for the new data */
4322 for (i = 0; i < batch->nr; i++) {
4323 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[i]);
4324 btrfs_set_item_key(leaf, &disk_key, slot + i);
4325 data_end -= batch->data_sizes[i];
4326 btrfs_set_token_item_offset(&token, slot + i, data_end);
4327 btrfs_set_token_item_size(&token, slot + i, batch->data_sizes[i]);
4330 btrfs_set_header_nritems(leaf, nritems + batch->nr);
4331 btrfs_mark_buffer_dirty(leaf);
4333 if (btrfs_leaf_free_space(leaf) < 0) {
4334 btrfs_print_leaf(leaf);
4340 * Insert a new item into a leaf.
4342 * @root: The root of the btree.
4343 * @path: A path pointing to the target leaf and slot.
4344 * @key: The key of the new item.
4345 * @data_size: The size of the data associated with the new key.
4347 void btrfs_setup_item_for_insert(struct btrfs_root *root,
4348 struct btrfs_path *path,
4349 const struct btrfs_key *key,
4352 struct btrfs_item_batch batch;
4355 batch.data_sizes = &data_size;
4356 batch.total_data_size = data_size;
4359 setup_items_for_insert(root, path, &batch);
4363 * Given a key and some data, insert items into the tree.
4364 * This does all the path init required, making room in the tree if needed.
4366 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4367 struct btrfs_root *root,
4368 struct btrfs_path *path,
4369 const struct btrfs_item_batch *batch)
4375 total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
4376 ret = btrfs_search_slot(trans, root, &batch->keys[0], path, total_size, 1);
4382 slot = path->slots[0];
4385 setup_items_for_insert(root, path, batch);
4390 * Given a key and some data, insert an item into the tree.
4391 * This does all the path init required, making room in the tree if needed.
4393 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4394 const struct btrfs_key *cpu_key, void *data,
4398 struct btrfs_path *path;
4399 struct extent_buffer *leaf;
4402 path = btrfs_alloc_path();
4405 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4407 leaf = path->nodes[0];
4408 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4409 write_extent_buffer(leaf, data, ptr, data_size);
4410 btrfs_mark_buffer_dirty(leaf);
4412 btrfs_free_path(path);
4417 * This function duplicates an item, giving 'new_key' to the new item.
4418 * It guarantees both items live in the same tree leaf and the new item is
4419 * contiguous with the original item.
4421 * This allows us to split a file extent in place, keeping a lock on the leaf
4424 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4425 struct btrfs_root *root,
4426 struct btrfs_path *path,
4427 const struct btrfs_key *new_key)
4429 struct extent_buffer *leaf;
4433 leaf = path->nodes[0];
4434 item_size = btrfs_item_size(leaf, path->slots[0]);
4435 ret = setup_leaf_for_split(trans, root, path,
4436 item_size + sizeof(struct btrfs_item));
4441 btrfs_setup_item_for_insert(root, path, new_key, item_size);
4442 leaf = path->nodes[0];
4443 memcpy_extent_buffer(leaf,
4444 btrfs_item_ptr_offset(leaf, path->slots[0]),
4445 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4451 * delete the pointer from a given node.
4453 * the tree should have been previously balanced so the deletion does not
4456 * This is exported for use inside btrfs-progs, don't un-export it.
4458 int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4459 struct btrfs_path *path, int level, int slot)
4461 struct extent_buffer *parent = path->nodes[level];
4465 nritems = btrfs_header_nritems(parent);
4466 if (slot != nritems - 1) {
4468 ret = btrfs_tree_mod_log_insert_move(parent, slot,
4469 slot + 1, nritems - slot - 1);
4471 btrfs_abort_transaction(trans, ret);
4475 memmove_extent_buffer(parent,
4476 btrfs_node_key_ptr_offset(parent, slot),
4477 btrfs_node_key_ptr_offset(parent, slot + 1),
4478 sizeof(struct btrfs_key_ptr) *
4479 (nritems - slot - 1));
4481 ret = btrfs_tree_mod_log_insert_key(parent, slot,
4482 BTRFS_MOD_LOG_KEY_REMOVE);
4484 btrfs_abort_transaction(trans, ret);
4490 btrfs_set_header_nritems(parent, nritems);
4491 if (nritems == 0 && parent == root->node) {
4492 BUG_ON(btrfs_header_level(root->node) != 1);
4493 /* just turn the root into a leaf and break */
4494 btrfs_set_header_level(root->node, 0);
4495 } else if (slot == 0) {
4496 struct btrfs_disk_key disk_key;
4498 btrfs_node_key(parent, &disk_key, 0);
4499 fixup_low_keys(path, &disk_key, level + 1);
4501 btrfs_mark_buffer_dirty(parent);
4506 * a helper function to delete the leaf pointed to by path->slots[1] and
4509 * This deletes the pointer in path->nodes[1] and frees the leaf
4510 * block extent. zero is returned if it all worked out, < 0 otherwise.
4512 * The path must have already been setup for deleting the leaf, including
4513 * all the proper balancing. path->nodes[1] must be locked.
4515 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
4516 struct btrfs_root *root,
4517 struct btrfs_path *path,
4518 struct extent_buffer *leaf)
4522 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4523 ret = btrfs_del_ptr(trans, root, path, 1, path->slots[1]);
4528 * btrfs_free_extent is expensive, we want to make sure we
4529 * aren't holding any locks when we call it
4531 btrfs_unlock_up_safe(path, 0);
4533 root_sub_used(root, leaf->len);
4535 atomic_inc(&leaf->refs);
4536 btrfs_free_tree_block(trans, btrfs_root_id(root), leaf, 0, 1);
4537 free_extent_buffer_stale(leaf);
4541 * delete the item at the leaf level in path. If that empties
4542 * the leaf, remove it from the tree
4544 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4545 struct btrfs_path *path, int slot, int nr)
4547 struct btrfs_fs_info *fs_info = root->fs_info;
4548 struct extent_buffer *leaf;
4553 leaf = path->nodes[0];
4554 nritems = btrfs_header_nritems(leaf);
4556 if (slot + nr != nritems) {
4557 const u32 last_off = btrfs_item_offset(leaf, slot + nr - 1);
4558 const int data_end = leaf_data_end(leaf);
4559 struct btrfs_map_token token;
4563 for (i = 0; i < nr; i++)
4564 dsize += btrfs_item_size(leaf, slot + i);
4566 memmove_leaf_data(leaf, data_end + dsize, data_end,
4567 last_off - data_end);
4569 btrfs_init_map_token(&token, leaf);
4570 for (i = slot + nr; i < nritems; i++) {
4573 ioff = btrfs_token_item_offset(&token, i);
4574 btrfs_set_token_item_offset(&token, i, ioff + dsize);
4577 memmove_leaf_items(leaf, slot, slot + nr, nritems - slot - nr);
4579 btrfs_set_header_nritems(leaf, nritems - nr);
4582 /* delete the leaf if we've emptied it */
4584 if (leaf == root->node) {
4585 btrfs_set_header_level(leaf, 0);
4587 btrfs_clear_buffer_dirty(trans, leaf);
4588 ret = btrfs_del_leaf(trans, root, path, leaf);
4593 int used = leaf_space_used(leaf, 0, nritems);
4595 struct btrfs_disk_key disk_key;
4597 btrfs_item_key(leaf, &disk_key, 0);
4598 fixup_low_keys(path, &disk_key, 1);
4602 * Try to delete the leaf if it is mostly empty. We do this by
4603 * trying to move all its items into its left and right neighbours.
4604 * If we can't move all the items, then we don't delete it - it's
4605 * not ideal, but future insertions might fill the leaf with more
4606 * items, or items from other leaves might be moved later into our
4607 * leaf due to deletions on those leaves.
4609 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
4612 /* push_leaf_left fixes the path.
4613 * make sure the path still points to our leaf
4614 * for possible call to btrfs_del_ptr below
4616 slot = path->slots[1];
4617 atomic_inc(&leaf->refs);
4619 * We want to be able to at least push one item to the
4620 * left neighbour leaf, and that's the first item.
4622 min_push_space = sizeof(struct btrfs_item) +
4623 btrfs_item_size(leaf, 0);
4624 wret = push_leaf_left(trans, root, path, 0,
4625 min_push_space, 1, (u32)-1);
4626 if (wret < 0 && wret != -ENOSPC)
4629 if (path->nodes[0] == leaf &&
4630 btrfs_header_nritems(leaf)) {
4632 * If we were not able to push all items from our
4633 * leaf to its left neighbour, then attempt to
4634 * either push all the remaining items to the
4635 * right neighbour or none. There's no advantage
4636 * in pushing only some items, instead of all, as
4637 * it's pointless to end up with a leaf having
4638 * too few items while the neighbours can be full
4641 nritems = btrfs_header_nritems(leaf);
4642 min_push_space = leaf_space_used(leaf, 0, nritems);
4643 wret = push_leaf_right(trans, root, path, 0,
4644 min_push_space, 1, 0);
4645 if (wret < 0 && wret != -ENOSPC)
4649 if (btrfs_header_nritems(leaf) == 0) {
4650 path->slots[1] = slot;
4651 ret = btrfs_del_leaf(trans, root, path, leaf);
4654 free_extent_buffer(leaf);
4657 /* if we're still in the path, make sure
4658 * we're dirty. Otherwise, one of the
4659 * push_leaf functions must have already
4660 * dirtied this buffer
4662 if (path->nodes[0] == leaf)
4663 btrfs_mark_buffer_dirty(leaf);
4664 free_extent_buffer(leaf);
4667 btrfs_mark_buffer_dirty(leaf);
4674 * A helper function to walk down the tree starting at min_key, and looking
4675 * for nodes or leaves that are have a minimum transaction id.
4676 * This is used by the btree defrag code, and tree logging
4678 * This does not cow, but it does stuff the starting key it finds back
4679 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4680 * key and get a writable path.
4682 * This honors path->lowest_level to prevent descent past a given level
4685 * min_trans indicates the oldest transaction that you are interested
4686 * in walking through. Any nodes or leaves older than min_trans are
4687 * skipped over (without reading them).
4689 * returns zero if something useful was found, < 0 on error and 1 if there
4690 * was nothing in the tree that matched the search criteria.
4692 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4693 struct btrfs_path *path,
4696 struct extent_buffer *cur;
4697 struct btrfs_key found_key;
4703 int keep_locks = path->keep_locks;
4705 ASSERT(!path->nowait);
4706 path->keep_locks = 1;
4708 cur = btrfs_read_lock_root_node(root);
4709 level = btrfs_header_level(cur);
4710 WARN_ON(path->nodes[level]);
4711 path->nodes[level] = cur;
4712 path->locks[level] = BTRFS_READ_LOCK;
4714 if (btrfs_header_generation(cur) < min_trans) {
4719 nritems = btrfs_header_nritems(cur);
4720 level = btrfs_header_level(cur);
4721 sret = btrfs_bin_search(cur, 0, min_key, &slot);
4727 /* at the lowest level, we're done, setup the path and exit */
4728 if (level == path->lowest_level) {
4729 if (slot >= nritems)
4732 path->slots[level] = slot;
4733 btrfs_item_key_to_cpu(cur, &found_key, slot);
4736 if (sret && slot > 0)
4739 * check this node pointer against the min_trans parameters.
4740 * If it is too old, skip to the next one.
4742 while (slot < nritems) {
4745 gen = btrfs_node_ptr_generation(cur, slot);
4746 if (gen < min_trans) {
4754 * we didn't find a candidate key in this node, walk forward
4755 * and find another one
4757 if (slot >= nritems) {
4758 path->slots[level] = slot;
4759 sret = btrfs_find_next_key(root, path, min_key, level,
4762 btrfs_release_path(path);
4768 /* save our key for returning back */
4769 btrfs_node_key_to_cpu(cur, &found_key, slot);
4770 path->slots[level] = slot;
4771 if (level == path->lowest_level) {
4775 cur = btrfs_read_node_slot(cur, slot);
4781 btrfs_tree_read_lock(cur);
4783 path->locks[level - 1] = BTRFS_READ_LOCK;
4784 path->nodes[level - 1] = cur;
4785 unlock_up(path, level, 1, 0, NULL);
4788 path->keep_locks = keep_locks;
4790 btrfs_unlock_up_safe(path, path->lowest_level + 1);
4791 memcpy(min_key, &found_key, sizeof(found_key));
4797 * this is similar to btrfs_next_leaf, but does not try to preserve
4798 * and fixup the path. It looks for and returns the next key in the
4799 * tree based on the current path and the min_trans parameters.
4801 * 0 is returned if another key is found, < 0 if there are any errors
4802 * and 1 is returned if there are no higher keys in the tree
4804 * path->keep_locks should be set to 1 on the search made before
4805 * calling this function.
4807 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4808 struct btrfs_key *key, int level, u64 min_trans)
4811 struct extent_buffer *c;
4813 WARN_ON(!path->keep_locks && !path->skip_locking);
4814 while (level < BTRFS_MAX_LEVEL) {
4815 if (!path->nodes[level])
4818 slot = path->slots[level] + 1;
4819 c = path->nodes[level];
4821 if (slot >= btrfs_header_nritems(c)) {
4824 struct btrfs_key cur_key;
4825 if (level + 1 >= BTRFS_MAX_LEVEL ||
4826 !path->nodes[level + 1])
4829 if (path->locks[level + 1] || path->skip_locking) {
4834 slot = btrfs_header_nritems(c) - 1;
4836 btrfs_item_key_to_cpu(c, &cur_key, slot);
4838 btrfs_node_key_to_cpu(c, &cur_key, slot);
4840 orig_lowest = path->lowest_level;
4841 btrfs_release_path(path);
4842 path->lowest_level = level;
4843 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4845 path->lowest_level = orig_lowest;
4849 c = path->nodes[level];
4850 slot = path->slots[level];
4857 btrfs_item_key_to_cpu(c, key, slot);
4859 u64 gen = btrfs_node_ptr_generation(c, slot);
4861 if (gen < min_trans) {
4865 btrfs_node_key_to_cpu(c, key, slot);
4872 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
4877 struct extent_buffer *c;
4878 struct extent_buffer *next;
4879 struct btrfs_fs_info *fs_info = root->fs_info;
4880 struct btrfs_key key;
4881 bool need_commit_sem = false;
4887 * The nowait semantics are used only for write paths, where we don't
4888 * use the tree mod log and sequence numbers.
4891 ASSERT(!path->nowait);
4893 nritems = btrfs_header_nritems(path->nodes[0]);
4897 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4901 btrfs_release_path(path);
4903 path->keep_locks = 1;
4906 ret = btrfs_search_old_slot(root, &key, path, time_seq);
4908 if (path->need_commit_sem) {
4909 path->need_commit_sem = 0;
4910 need_commit_sem = true;
4912 if (!down_read_trylock(&fs_info->commit_root_sem)) {
4917 down_read(&fs_info->commit_root_sem);
4920 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4922 path->keep_locks = 0;
4927 nritems = btrfs_header_nritems(path->nodes[0]);
4929 * by releasing the path above we dropped all our locks. A balance
4930 * could have added more items next to the key that used to be
4931 * at the very end of the block. So, check again here and
4932 * advance the path if there are now more items available.
4934 if (nritems > 0 && path->slots[0] < nritems - 1) {
4941 * So the above check misses one case:
4942 * - after releasing the path above, someone has removed the item that
4943 * used to be at the very end of the block, and balance between leafs
4944 * gets another one with bigger key.offset to replace it.
4946 * This one should be returned as well, or we can get leaf corruption
4947 * later(esp. in __btrfs_drop_extents()).
4949 * And a bit more explanation about this check,
4950 * with ret > 0, the key isn't found, the path points to the slot
4951 * where it should be inserted, so the path->slots[0] item must be the
4954 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
4959 while (level < BTRFS_MAX_LEVEL) {
4960 if (!path->nodes[level]) {
4965 slot = path->slots[level] + 1;
4966 c = path->nodes[level];
4967 if (slot >= btrfs_header_nritems(c)) {
4969 if (level == BTRFS_MAX_LEVEL) {
4978 * Our current level is where we're going to start from, and to
4979 * make sure lockdep doesn't complain we need to drop our locks
4980 * and nodes from 0 to our current level.
4982 for (i = 0; i < level; i++) {
4983 if (path->locks[level]) {
4984 btrfs_tree_read_unlock(path->nodes[i]);
4987 free_extent_buffer(path->nodes[i]);
4988 path->nodes[i] = NULL;
4992 ret = read_block_for_search(root, path, &next, level,
4994 if (ret == -EAGAIN && !path->nowait)
4998 btrfs_release_path(path);
5002 if (!path->skip_locking) {
5003 ret = btrfs_try_tree_read_lock(next);
5004 if (!ret && path->nowait) {
5008 if (!ret && time_seq) {
5010 * If we don't get the lock, we may be racing
5011 * with push_leaf_left, holding that lock while
5012 * itself waiting for the leaf we've currently
5013 * locked. To solve this situation, we give up
5014 * on our lock and cycle.
5016 free_extent_buffer(next);
5017 btrfs_release_path(path);
5022 btrfs_tree_read_lock(next);
5026 path->slots[level] = slot;
5029 path->nodes[level] = next;
5030 path->slots[level] = 0;
5031 if (!path->skip_locking)
5032 path->locks[level] = BTRFS_READ_LOCK;
5036 ret = read_block_for_search(root, path, &next, level,
5038 if (ret == -EAGAIN && !path->nowait)
5042 btrfs_release_path(path);
5046 if (!path->skip_locking) {
5048 if (!btrfs_try_tree_read_lock(next)) {
5053 btrfs_tree_read_lock(next);
5059 unlock_up(path, 0, 1, 0, NULL);
5060 if (need_commit_sem) {
5063 path->need_commit_sem = 1;
5064 ret2 = finish_need_commit_sem_search(path);
5065 up_read(&fs_info->commit_root_sem);
5073 int btrfs_next_old_item(struct btrfs_root *root, struct btrfs_path *path, u64 time_seq)
5076 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0]))
5077 return btrfs_next_old_leaf(root, path, time_seq);
5082 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5083 * searching until it gets past min_objectid or finds an item of 'type'
5085 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5087 int btrfs_previous_item(struct btrfs_root *root,
5088 struct btrfs_path *path, u64 min_objectid,
5091 struct btrfs_key found_key;
5092 struct extent_buffer *leaf;
5097 if (path->slots[0] == 0) {
5098 ret = btrfs_prev_leaf(root, path);
5104 leaf = path->nodes[0];
5105 nritems = btrfs_header_nritems(leaf);
5108 if (path->slots[0] == nritems)
5111 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5112 if (found_key.objectid < min_objectid)
5114 if (found_key.type == type)
5116 if (found_key.objectid == min_objectid &&
5117 found_key.type < type)
5124 * search in extent tree to find a previous Metadata/Data extent item with
5127 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5129 int btrfs_previous_extent_item(struct btrfs_root *root,
5130 struct btrfs_path *path, u64 min_objectid)
5132 struct btrfs_key found_key;
5133 struct extent_buffer *leaf;
5138 if (path->slots[0] == 0) {
5139 ret = btrfs_prev_leaf(root, path);
5145 leaf = path->nodes[0];
5146 nritems = btrfs_header_nritems(leaf);
5149 if (path->slots[0] == nritems)
5152 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5153 if (found_key.objectid < min_objectid)
5155 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5156 found_key.type == BTRFS_METADATA_ITEM_KEY)
5158 if (found_key.objectid == min_objectid &&
5159 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5165 int __init btrfs_ctree_init(void)
5167 btrfs_path_cachep = kmem_cache_create("btrfs_path",
5168 sizeof(struct btrfs_path), 0,
5169 SLAB_MEM_SPREAD, NULL);
5170 if (!btrfs_path_cachep)
5175 void __cold btrfs_ctree_exit(void)
5177 kmem_cache_destroy(btrfs_path_cachep);