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>
12 #include "transaction.h"
13 #include "print-tree.h"
17 #include "tree-mod-log.h"
19 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
20 *root, struct btrfs_path *path, int level);
21 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
22 const struct btrfs_key *ins_key, struct btrfs_path *path,
23 int data_size, int extend);
24 static int push_node_left(struct btrfs_trans_handle *trans,
25 struct extent_buffer *dst,
26 struct extent_buffer *src, int empty);
27 static int balance_node_right(struct btrfs_trans_handle *trans,
28 struct extent_buffer *dst_buf,
29 struct extent_buffer *src_buf);
30 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
33 static const struct btrfs_csums {
36 const char driver[12];
38 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
39 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
40 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
41 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
42 .driver = "blake2b-256" },
45 int btrfs_super_csum_size(const struct btrfs_super_block *s)
47 u16 t = btrfs_super_csum_type(s);
49 * csum type is validated at mount time
51 return btrfs_csums[t].size;
54 const char *btrfs_super_csum_name(u16 csum_type)
56 /* csum type is validated at mount time */
57 return btrfs_csums[csum_type].name;
61 * Return driver name if defined, otherwise the name that's also a valid driver
64 const char *btrfs_super_csum_driver(u16 csum_type)
66 /* csum type is validated at mount time */
67 return btrfs_csums[csum_type].driver[0] ?
68 btrfs_csums[csum_type].driver :
69 btrfs_csums[csum_type].name;
72 size_t __attribute_const__ btrfs_get_num_csums(void)
74 return ARRAY_SIZE(btrfs_csums);
77 struct btrfs_path *btrfs_alloc_path(void)
79 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
82 /* this also releases the path */
83 void btrfs_free_path(struct btrfs_path *p)
87 btrfs_release_path(p);
88 kmem_cache_free(btrfs_path_cachep, p);
92 * path release drops references on the extent buffers in the path
93 * and it drops any locks held by this path
95 * It is safe to call this on paths that no locks or extent buffers held.
97 noinline void btrfs_release_path(struct btrfs_path *p)
101 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
106 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
109 free_extent_buffer(p->nodes[i]);
115 * safely gets a reference on the root node of a tree. A lock
116 * is not taken, so a concurrent writer may put a different node
117 * at the root of the tree. See btrfs_lock_root_node for the
120 * The extent buffer returned by this has a reference taken, so
121 * it won't disappear. It may stop being the root of the tree
122 * at any time because there are no locks held.
124 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
126 struct extent_buffer *eb;
130 eb = rcu_dereference(root->node);
133 * RCU really hurts here, we could free up the root node because
134 * it was COWed but we may not get the new root node yet so do
135 * the inc_not_zero dance and if it doesn't work then
136 * synchronize_rcu and try again.
138 if (atomic_inc_not_zero(&eb->refs)) {
149 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
150 * just get put onto a simple dirty list. Transaction walks this list to make
151 * sure they get properly updated on disk.
153 static void add_root_to_dirty_list(struct btrfs_root *root)
155 struct btrfs_fs_info *fs_info = root->fs_info;
157 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
158 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
161 spin_lock(&fs_info->trans_lock);
162 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
163 /* Want the extent tree to be the last on the list */
164 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
165 list_move_tail(&root->dirty_list,
166 &fs_info->dirty_cowonly_roots);
168 list_move(&root->dirty_list,
169 &fs_info->dirty_cowonly_roots);
171 spin_unlock(&fs_info->trans_lock);
175 * used by snapshot creation to make a copy of a root for a tree with
176 * a given objectid. The buffer with the new root node is returned in
177 * cow_ret, and this func returns zero on success or a negative error code.
179 int btrfs_copy_root(struct btrfs_trans_handle *trans,
180 struct btrfs_root *root,
181 struct extent_buffer *buf,
182 struct extent_buffer **cow_ret, u64 new_root_objectid)
184 struct btrfs_fs_info *fs_info = root->fs_info;
185 struct extent_buffer *cow;
188 struct btrfs_disk_key disk_key;
190 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
191 trans->transid != fs_info->running_transaction->transid);
192 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
193 trans->transid != root->last_trans);
195 level = btrfs_header_level(buf);
197 btrfs_item_key(buf, &disk_key, 0);
199 btrfs_node_key(buf, &disk_key, 0);
201 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
202 &disk_key, level, buf->start, 0,
203 BTRFS_NESTING_NEW_ROOT);
207 copy_extent_buffer_full(cow, buf);
208 btrfs_set_header_bytenr(cow, cow->start);
209 btrfs_set_header_generation(cow, trans->transid);
210 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
211 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
212 BTRFS_HEADER_FLAG_RELOC);
213 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
214 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
216 btrfs_set_header_owner(cow, new_root_objectid);
218 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
220 WARN_ON(btrfs_header_generation(buf) > trans->transid);
221 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
222 ret = btrfs_inc_ref(trans, root, cow, 1);
224 ret = btrfs_inc_ref(trans, root, cow, 0);
226 btrfs_tree_unlock(cow);
227 free_extent_buffer(cow);
228 btrfs_abort_transaction(trans, ret);
232 btrfs_mark_buffer_dirty(cow);
238 * check if the tree block can be shared by multiple trees
240 int btrfs_block_can_be_shared(struct btrfs_root *root,
241 struct extent_buffer *buf)
244 * Tree blocks not in shareable trees and tree roots are never shared.
245 * If a block was allocated after the last snapshot and the block was
246 * not allocated by tree relocation, we know the block is not shared.
248 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
249 buf != root->node && buf != root->commit_root &&
250 (btrfs_header_generation(buf) <=
251 btrfs_root_last_snapshot(&root->root_item) ||
252 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
258 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
259 struct btrfs_root *root,
260 struct extent_buffer *buf,
261 struct extent_buffer *cow,
264 struct btrfs_fs_info *fs_info = root->fs_info;
272 * Backrefs update rules:
274 * Always use full backrefs for extent pointers in tree block
275 * allocated by tree relocation.
277 * If a shared tree block is no longer referenced by its owner
278 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
279 * use full backrefs for extent pointers in tree block.
281 * If a tree block is been relocating
282 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
283 * use full backrefs for extent pointers in tree block.
284 * The reason for this is some operations (such as drop tree)
285 * are only allowed for blocks use full backrefs.
288 if (btrfs_block_can_be_shared(root, buf)) {
289 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
290 btrfs_header_level(buf), 1,
296 btrfs_handle_fs_error(fs_info, ret, NULL);
301 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
302 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
303 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
308 owner = btrfs_header_owner(buf);
309 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
310 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
313 if ((owner == root->root_key.objectid ||
314 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
315 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
316 ret = btrfs_inc_ref(trans, root, buf, 1);
320 if (root->root_key.objectid ==
321 BTRFS_TREE_RELOC_OBJECTID) {
322 ret = btrfs_dec_ref(trans, root, buf, 0);
325 ret = btrfs_inc_ref(trans, root, cow, 1);
329 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
332 if (root->root_key.objectid ==
333 BTRFS_TREE_RELOC_OBJECTID)
334 ret = btrfs_inc_ref(trans, root, cow, 1);
336 ret = btrfs_inc_ref(trans, root, cow, 0);
340 if (new_flags != 0) {
341 int level = btrfs_header_level(buf);
343 ret = btrfs_set_disk_extent_flags(trans, buf,
344 new_flags, level, 0);
349 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
350 if (root->root_key.objectid ==
351 BTRFS_TREE_RELOC_OBJECTID)
352 ret = btrfs_inc_ref(trans, root, cow, 1);
354 ret = btrfs_inc_ref(trans, root, cow, 0);
357 ret = btrfs_dec_ref(trans, root, buf, 1);
361 btrfs_clean_tree_block(buf);
367 static struct extent_buffer *alloc_tree_block_no_bg_flush(
368 struct btrfs_trans_handle *trans,
369 struct btrfs_root *root,
371 const struct btrfs_disk_key *disk_key,
375 enum btrfs_lock_nesting nest)
377 struct btrfs_fs_info *fs_info = root->fs_info;
378 struct extent_buffer *ret;
381 * If we are COWing a node/leaf from the extent, chunk, device or free
382 * space trees, make sure that we do not finish block group creation of
383 * pending block groups. We do this to avoid a deadlock.
384 * COWing can result in allocation of a new chunk, and flushing pending
385 * block groups (btrfs_create_pending_block_groups()) can be triggered
386 * when finishing allocation of a new chunk. Creation of a pending block
387 * group modifies the extent, chunk, device and free space trees,
388 * therefore we could deadlock with ourselves since we are holding a
389 * lock on an extent buffer that btrfs_create_pending_block_groups() may
391 * For similar reasons, we also need to delay flushing pending block
392 * groups when splitting a leaf or node, from one of those trees, since
393 * we are holding a write lock on it and its parent or when inserting a
394 * new root node for one of those trees.
396 if (root == fs_info->extent_root ||
397 root == fs_info->chunk_root ||
398 root == fs_info->dev_root ||
399 root == fs_info->free_space_root)
400 trans->can_flush_pending_bgs = false;
402 ret = btrfs_alloc_tree_block(trans, root, parent_start,
403 root->root_key.objectid, disk_key, level,
404 hint, empty_size, nest);
405 trans->can_flush_pending_bgs = true;
411 * does the dirty work in cow of a single block. The parent block (if
412 * supplied) is updated to point to the new cow copy. The new buffer is marked
413 * dirty and returned locked. If you modify the block it needs to be marked
416 * search_start -- an allocation hint for the new block
418 * empty_size -- a hint that you plan on doing more cow. This is the size in
419 * bytes the allocator should try to find free next to the block it returns.
420 * This is just a hint and may be ignored by the allocator.
422 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
423 struct btrfs_root *root,
424 struct extent_buffer *buf,
425 struct extent_buffer *parent, int parent_slot,
426 struct extent_buffer **cow_ret,
427 u64 search_start, u64 empty_size,
428 enum btrfs_lock_nesting nest)
430 struct btrfs_fs_info *fs_info = root->fs_info;
431 struct btrfs_disk_key disk_key;
432 struct extent_buffer *cow;
436 u64 parent_start = 0;
441 btrfs_assert_tree_locked(buf);
443 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
444 trans->transid != fs_info->running_transaction->transid);
445 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
446 trans->transid != root->last_trans);
448 level = btrfs_header_level(buf);
451 btrfs_item_key(buf, &disk_key, 0);
453 btrfs_node_key(buf, &disk_key, 0);
455 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
456 parent_start = parent->start;
458 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
459 level, search_start, empty_size, nest);
463 /* cow is set to blocking by btrfs_init_new_buffer */
465 copy_extent_buffer_full(cow, buf);
466 btrfs_set_header_bytenr(cow, cow->start);
467 btrfs_set_header_generation(cow, trans->transid);
468 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
469 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
470 BTRFS_HEADER_FLAG_RELOC);
471 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
472 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
474 btrfs_set_header_owner(cow, root->root_key.objectid);
476 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
478 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
480 btrfs_tree_unlock(cow);
481 free_extent_buffer(cow);
482 btrfs_abort_transaction(trans, ret);
486 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
487 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
489 btrfs_tree_unlock(cow);
490 free_extent_buffer(cow);
491 btrfs_abort_transaction(trans, ret);
496 if (buf == root->node) {
497 WARN_ON(parent && parent != buf);
498 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
499 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
500 parent_start = buf->start;
502 atomic_inc(&cow->refs);
503 ret = btrfs_tree_mod_log_insert_root(root->node, cow, true);
505 rcu_assign_pointer(root->node, cow);
507 btrfs_free_tree_block(trans, root, buf, parent_start,
509 free_extent_buffer(buf);
510 add_root_to_dirty_list(root);
512 WARN_ON(trans->transid != btrfs_header_generation(parent));
513 btrfs_tree_mod_log_insert_key(parent, parent_slot,
514 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
515 btrfs_set_node_blockptr(parent, parent_slot,
517 btrfs_set_node_ptr_generation(parent, parent_slot,
519 btrfs_mark_buffer_dirty(parent);
521 ret = btrfs_tree_mod_log_free_eb(buf);
523 btrfs_tree_unlock(cow);
524 free_extent_buffer(cow);
525 btrfs_abort_transaction(trans, ret);
529 btrfs_free_tree_block(trans, root, buf, parent_start,
533 btrfs_tree_unlock(buf);
534 free_extent_buffer_stale(buf);
535 btrfs_mark_buffer_dirty(cow);
540 static inline int should_cow_block(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root,
542 struct extent_buffer *buf)
544 if (btrfs_is_testing(root->fs_info))
547 /* Ensure we can see the FORCE_COW bit */
548 smp_mb__before_atomic();
551 * We do not need to cow a block if
552 * 1) this block is not created or changed in this transaction;
553 * 2) this block does not belong to TREE_RELOC tree;
554 * 3) the root is not forced COW.
556 * What is forced COW:
557 * when we create snapshot during committing the transaction,
558 * after we've finished copying src root, we must COW the shared
559 * block to ensure the metadata consistency.
561 if (btrfs_header_generation(buf) == trans->transid &&
562 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
563 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
564 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
565 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
571 * cows a single block, see __btrfs_cow_block for the real work.
572 * This version of it has extra checks so that a block isn't COWed more than
573 * once per transaction, as long as it hasn't been written yet
575 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
576 struct btrfs_root *root, struct extent_buffer *buf,
577 struct extent_buffer *parent, int parent_slot,
578 struct extent_buffer **cow_ret,
579 enum btrfs_lock_nesting nest)
581 struct btrfs_fs_info *fs_info = root->fs_info;
585 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
587 "COW'ing blocks on a fs root that's being dropped");
589 if (trans->transaction != fs_info->running_transaction)
590 WARN(1, KERN_CRIT "trans %llu running %llu\n",
592 fs_info->running_transaction->transid);
594 if (trans->transid != fs_info->generation)
595 WARN(1, KERN_CRIT "trans %llu running %llu\n",
596 trans->transid, fs_info->generation);
598 if (!should_cow_block(trans, root, buf)) {
604 search_start = buf->start & ~((u64)SZ_1G - 1);
607 * Before CoWing this block for later modification, check if it's
608 * the subtree root and do the delayed subtree trace if needed.
610 * Also We don't care about the error, as it's handled internally.
612 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
613 ret = __btrfs_cow_block(trans, root, buf, parent,
614 parent_slot, cow_ret, search_start, 0, nest);
616 trace_btrfs_cow_block(root, buf, *cow_ret);
620 ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
623 * helper function for defrag to decide if two blocks pointed to by a
624 * node are actually close by
626 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
628 if (blocknr < other && other - (blocknr + blocksize) < 32768)
630 if (blocknr > other && blocknr - (other + blocksize) < 32768)
635 #ifdef __LITTLE_ENDIAN
638 * Compare two keys, on little-endian the disk order is same as CPU order and
639 * we can avoid the conversion.
641 static int comp_keys(const struct btrfs_disk_key *disk_key,
642 const struct btrfs_key *k2)
644 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
646 return btrfs_comp_cpu_keys(k1, k2);
652 * compare two keys in a memcmp fashion
654 static int comp_keys(const struct btrfs_disk_key *disk,
655 const struct btrfs_key *k2)
659 btrfs_disk_key_to_cpu(&k1, disk);
661 return btrfs_comp_cpu_keys(&k1, k2);
666 * same as comp_keys only with two btrfs_key's
668 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
670 if (k1->objectid > k2->objectid)
672 if (k1->objectid < k2->objectid)
674 if (k1->type > k2->type)
676 if (k1->type < k2->type)
678 if (k1->offset > k2->offset)
680 if (k1->offset < k2->offset)
686 * this is used by the defrag code to go through all the
687 * leaves pointed to by a node and reallocate them so that
688 * disk order is close to key order
690 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
691 struct btrfs_root *root, struct extent_buffer *parent,
692 int start_slot, u64 *last_ret,
693 struct btrfs_key *progress)
695 struct btrfs_fs_info *fs_info = root->fs_info;
696 struct extent_buffer *cur;
698 u64 search_start = *last_ret;
706 int progress_passed = 0;
707 struct btrfs_disk_key disk_key;
709 WARN_ON(trans->transaction != fs_info->running_transaction);
710 WARN_ON(trans->transid != fs_info->generation);
712 parent_nritems = btrfs_header_nritems(parent);
713 blocksize = fs_info->nodesize;
714 end_slot = parent_nritems - 1;
716 if (parent_nritems <= 1)
719 for (i = start_slot; i <= end_slot; i++) {
722 btrfs_node_key(parent, &disk_key, i);
723 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
727 blocknr = btrfs_node_blockptr(parent, i);
729 last_block = blocknr;
732 other = btrfs_node_blockptr(parent, i - 1);
733 close = close_blocks(blocknr, other, blocksize);
735 if (!close && i < end_slot) {
736 other = btrfs_node_blockptr(parent, i + 1);
737 close = close_blocks(blocknr, other, blocksize);
740 last_block = blocknr;
744 cur = btrfs_read_node_slot(parent, i);
747 if (search_start == 0)
748 search_start = last_block;
750 btrfs_tree_lock(cur);
751 err = __btrfs_cow_block(trans, root, cur, parent, i,
754 (end_slot - i) * blocksize),
757 btrfs_tree_unlock(cur);
758 free_extent_buffer(cur);
761 search_start = cur->start;
762 last_block = cur->start;
763 *last_ret = search_start;
764 btrfs_tree_unlock(cur);
765 free_extent_buffer(cur);
771 * search for key in the extent_buffer. The items start at offset p,
772 * and they are item_size apart. There are 'max' items in p.
774 * the slot in the array is returned via slot, and it points to
775 * the place where you would insert key if it is not found in
778 * slot may point to max if the key is bigger than all of the keys
780 static noinline int generic_bin_search(struct extent_buffer *eb,
781 unsigned long p, int item_size,
782 const struct btrfs_key *key,
788 const int key_size = sizeof(struct btrfs_disk_key);
791 btrfs_err(eb->fs_info,
792 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
793 __func__, low, high, eb->start,
794 btrfs_header_owner(eb), btrfs_header_level(eb));
800 unsigned long offset;
801 struct btrfs_disk_key *tmp;
802 struct btrfs_disk_key unaligned;
805 mid = (low + high) / 2;
806 offset = p + mid * item_size;
807 oip = offset_in_page(offset);
809 if (oip + key_size <= PAGE_SIZE) {
810 const unsigned long idx = get_eb_page_index(offset);
811 char *kaddr = page_address(eb->pages[idx]);
813 oip = get_eb_offset_in_page(eb, offset);
814 tmp = (struct btrfs_disk_key *)(kaddr + oip);
816 read_extent_buffer(eb, &unaligned, offset, key_size);
820 ret = comp_keys(tmp, key);
836 * simple bin_search frontend that does the right thing for
839 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
842 if (btrfs_header_level(eb) == 0)
843 return generic_bin_search(eb,
844 offsetof(struct btrfs_leaf, items),
845 sizeof(struct btrfs_item),
846 key, btrfs_header_nritems(eb),
849 return generic_bin_search(eb,
850 offsetof(struct btrfs_node, ptrs),
851 sizeof(struct btrfs_key_ptr),
852 key, btrfs_header_nritems(eb),
856 static void root_add_used(struct btrfs_root *root, u32 size)
858 spin_lock(&root->accounting_lock);
859 btrfs_set_root_used(&root->root_item,
860 btrfs_root_used(&root->root_item) + size);
861 spin_unlock(&root->accounting_lock);
864 static void root_sub_used(struct btrfs_root *root, u32 size)
866 spin_lock(&root->accounting_lock);
867 btrfs_set_root_used(&root->root_item,
868 btrfs_root_used(&root->root_item) - size);
869 spin_unlock(&root->accounting_lock);
872 /* given a node and slot number, this reads the blocks it points to. The
873 * extent buffer is returned with a reference taken (but unlocked).
875 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
878 int level = btrfs_header_level(parent);
879 struct extent_buffer *eb;
880 struct btrfs_key first_key;
882 if (slot < 0 || slot >= btrfs_header_nritems(parent))
883 return ERR_PTR(-ENOENT);
887 btrfs_node_key_to_cpu(parent, &first_key, slot);
888 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
889 btrfs_header_owner(parent),
890 btrfs_node_ptr_generation(parent, slot),
891 level - 1, &first_key);
892 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
893 free_extent_buffer(eb);
901 * node level balancing, used to make sure nodes are in proper order for
902 * item deletion. We balance from the top down, so we have to make sure
903 * that a deletion won't leave an node completely empty later on.
905 static noinline int balance_level(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root,
907 struct btrfs_path *path, int level)
909 struct btrfs_fs_info *fs_info = root->fs_info;
910 struct extent_buffer *right = NULL;
911 struct extent_buffer *mid;
912 struct extent_buffer *left = NULL;
913 struct extent_buffer *parent = NULL;
917 int orig_slot = path->slots[level];
922 mid = path->nodes[level];
924 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK);
925 WARN_ON(btrfs_header_generation(mid) != trans->transid);
927 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
929 if (level < BTRFS_MAX_LEVEL - 1) {
930 parent = path->nodes[level + 1];
931 pslot = path->slots[level + 1];
935 * deal with the case where there is only one pointer in the root
936 * by promoting the node below to a root
939 struct extent_buffer *child;
941 if (btrfs_header_nritems(mid) != 1)
944 /* promote the child to a root */
945 child = btrfs_read_node_slot(mid, 0);
947 ret = PTR_ERR(child);
948 btrfs_handle_fs_error(fs_info, ret, NULL);
952 btrfs_tree_lock(child);
953 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
956 btrfs_tree_unlock(child);
957 free_extent_buffer(child);
961 ret = btrfs_tree_mod_log_insert_root(root->node, child, true);
963 rcu_assign_pointer(root->node, child);
965 add_root_to_dirty_list(root);
966 btrfs_tree_unlock(child);
968 path->locks[level] = 0;
969 path->nodes[level] = NULL;
970 btrfs_clean_tree_block(mid);
971 btrfs_tree_unlock(mid);
972 /* once for the path */
973 free_extent_buffer(mid);
975 root_sub_used(root, mid->len);
976 btrfs_free_tree_block(trans, root, mid, 0, 1);
977 /* once for the root ptr */
978 free_extent_buffer_stale(mid);
981 if (btrfs_header_nritems(mid) >
982 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
985 left = btrfs_read_node_slot(parent, pslot - 1);
990 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
991 wret = btrfs_cow_block(trans, root, left,
992 parent, pslot - 1, &left,
993 BTRFS_NESTING_LEFT_COW);
1000 right = btrfs_read_node_slot(parent, pslot + 1);
1005 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1006 wret = btrfs_cow_block(trans, root, right,
1007 parent, pslot + 1, &right,
1008 BTRFS_NESTING_RIGHT_COW);
1015 /* first, try to make some room in the middle buffer */
1017 orig_slot += btrfs_header_nritems(left);
1018 wret = push_node_left(trans, left, mid, 1);
1024 * then try to empty the right most buffer into the middle
1027 wret = push_node_left(trans, mid, right, 1);
1028 if (wret < 0 && wret != -ENOSPC)
1030 if (btrfs_header_nritems(right) == 0) {
1031 btrfs_clean_tree_block(right);
1032 btrfs_tree_unlock(right);
1033 del_ptr(root, path, level + 1, pslot + 1);
1034 root_sub_used(root, right->len);
1035 btrfs_free_tree_block(trans, root, right, 0, 1);
1036 free_extent_buffer_stale(right);
1039 struct btrfs_disk_key right_key;
1040 btrfs_node_key(right, &right_key, 0);
1041 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1042 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1044 btrfs_set_node_key(parent, &right_key, pslot + 1);
1045 btrfs_mark_buffer_dirty(parent);
1048 if (btrfs_header_nritems(mid) == 1) {
1050 * we're not allowed to leave a node with one item in the
1051 * tree during a delete. A deletion from lower in the tree
1052 * could try to delete the only pointer in this node.
1053 * So, pull some keys from the left.
1054 * There has to be a left pointer at this point because
1055 * otherwise we would have pulled some pointers from the
1060 btrfs_handle_fs_error(fs_info, ret, NULL);
1063 wret = balance_node_right(trans, mid, left);
1069 wret = push_node_left(trans, left, mid, 1);
1075 if (btrfs_header_nritems(mid) == 0) {
1076 btrfs_clean_tree_block(mid);
1077 btrfs_tree_unlock(mid);
1078 del_ptr(root, path, level + 1, pslot);
1079 root_sub_used(root, mid->len);
1080 btrfs_free_tree_block(trans, root, mid, 0, 1);
1081 free_extent_buffer_stale(mid);
1084 /* update the parent key to reflect our changes */
1085 struct btrfs_disk_key mid_key;
1086 btrfs_node_key(mid, &mid_key, 0);
1087 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1088 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1090 btrfs_set_node_key(parent, &mid_key, pslot);
1091 btrfs_mark_buffer_dirty(parent);
1094 /* update the path */
1096 if (btrfs_header_nritems(left) > orig_slot) {
1097 atomic_inc(&left->refs);
1098 /* left was locked after cow */
1099 path->nodes[level] = left;
1100 path->slots[level + 1] -= 1;
1101 path->slots[level] = orig_slot;
1103 btrfs_tree_unlock(mid);
1104 free_extent_buffer(mid);
1107 orig_slot -= btrfs_header_nritems(left);
1108 path->slots[level] = orig_slot;
1111 /* double check we haven't messed things up */
1113 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1117 btrfs_tree_unlock(right);
1118 free_extent_buffer(right);
1121 if (path->nodes[level] != left)
1122 btrfs_tree_unlock(left);
1123 free_extent_buffer(left);
1128 /* Node balancing for insertion. Here we only split or push nodes around
1129 * when they are completely full. This is also done top down, so we
1130 * have to be pessimistic.
1132 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1133 struct btrfs_root *root,
1134 struct btrfs_path *path, int level)
1136 struct btrfs_fs_info *fs_info = root->fs_info;
1137 struct extent_buffer *right = NULL;
1138 struct extent_buffer *mid;
1139 struct extent_buffer *left = NULL;
1140 struct extent_buffer *parent = NULL;
1144 int orig_slot = path->slots[level];
1149 mid = path->nodes[level];
1150 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1152 if (level < BTRFS_MAX_LEVEL - 1) {
1153 parent = path->nodes[level + 1];
1154 pslot = path->slots[level + 1];
1160 left = btrfs_read_node_slot(parent, pslot - 1);
1164 /* first, try to make some room in the middle buffer */
1168 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
1170 left_nr = btrfs_header_nritems(left);
1171 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1174 ret = btrfs_cow_block(trans, root, left, parent,
1176 BTRFS_NESTING_LEFT_COW);
1180 wret = push_node_left(trans, left, mid, 0);
1186 struct btrfs_disk_key disk_key;
1187 orig_slot += left_nr;
1188 btrfs_node_key(mid, &disk_key, 0);
1189 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1190 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1192 btrfs_set_node_key(parent, &disk_key, pslot);
1193 btrfs_mark_buffer_dirty(parent);
1194 if (btrfs_header_nritems(left) > orig_slot) {
1195 path->nodes[level] = left;
1196 path->slots[level + 1] -= 1;
1197 path->slots[level] = orig_slot;
1198 btrfs_tree_unlock(mid);
1199 free_extent_buffer(mid);
1202 btrfs_header_nritems(left);
1203 path->slots[level] = orig_slot;
1204 btrfs_tree_unlock(left);
1205 free_extent_buffer(left);
1209 btrfs_tree_unlock(left);
1210 free_extent_buffer(left);
1212 right = btrfs_read_node_slot(parent, pslot + 1);
1217 * then try to empty the right most buffer into the middle
1222 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1224 right_nr = btrfs_header_nritems(right);
1225 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
1228 ret = btrfs_cow_block(trans, root, right,
1230 &right, BTRFS_NESTING_RIGHT_COW);
1234 wret = balance_node_right(trans, right, mid);
1240 struct btrfs_disk_key disk_key;
1242 btrfs_node_key(right, &disk_key, 0);
1243 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1244 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
1246 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1247 btrfs_mark_buffer_dirty(parent);
1249 if (btrfs_header_nritems(mid) <= orig_slot) {
1250 path->nodes[level] = right;
1251 path->slots[level + 1] += 1;
1252 path->slots[level] = orig_slot -
1253 btrfs_header_nritems(mid);
1254 btrfs_tree_unlock(mid);
1255 free_extent_buffer(mid);
1257 btrfs_tree_unlock(right);
1258 free_extent_buffer(right);
1262 btrfs_tree_unlock(right);
1263 free_extent_buffer(right);
1269 * readahead one full node of leaves, finding things that are close
1270 * to the block in 'slot', and triggering ra on them.
1272 static void reada_for_search(struct btrfs_fs_info *fs_info,
1273 struct btrfs_path *path,
1274 int level, int slot, u64 objectid)
1276 struct extent_buffer *node;
1277 struct btrfs_disk_key disk_key;
1283 struct extent_buffer *eb;
1288 if (level != 1 && path->reada != READA_FORWARD_ALWAYS)
1291 if (!path->nodes[level])
1294 node = path->nodes[level];
1297 * Since the time between visiting leaves is much shorter than the time
1298 * between visiting nodes, limit read ahead of nodes to 1, to avoid too
1299 * much IO at once (possibly random).
1301 if (path->reada == READA_FORWARD_ALWAYS) {
1303 nread_max = node->fs_info->nodesize;
1305 nread_max = SZ_128K;
1310 search = btrfs_node_blockptr(node, slot);
1311 blocksize = fs_info->nodesize;
1312 eb = find_extent_buffer(fs_info, search);
1314 free_extent_buffer(eb);
1320 nritems = btrfs_header_nritems(node);
1324 if (path->reada == READA_BACK) {
1328 } else if (path->reada == READA_FORWARD ||
1329 path->reada == READA_FORWARD_ALWAYS) {
1334 if (path->reada == READA_BACK && objectid) {
1335 btrfs_node_key(node, &disk_key, nr);
1336 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1339 search = btrfs_node_blockptr(node, nr);
1340 if (path->reada == READA_FORWARD_ALWAYS ||
1341 (search <= target && target - search <= 65536) ||
1342 (search > target && search - target <= 65536)) {
1343 btrfs_readahead_node_child(node, nr);
1347 if (nread > nread_max || nscan > 32)
1352 static noinline void reada_for_balance(struct btrfs_path *path, int level)
1354 struct extent_buffer *parent;
1358 parent = path->nodes[level + 1];
1362 nritems = btrfs_header_nritems(parent);
1363 slot = path->slots[level + 1];
1366 btrfs_readahead_node_child(parent, slot - 1);
1367 if (slot + 1 < nritems)
1368 btrfs_readahead_node_child(parent, slot + 1);
1373 * when we walk down the tree, it is usually safe to unlock the higher layers
1374 * in the tree. The exceptions are when our path goes through slot 0, because
1375 * operations on the tree might require changing key pointers higher up in the
1378 * callers might also have set path->keep_locks, which tells this code to keep
1379 * the lock if the path points to the last slot in the block. This is part of
1380 * walking through the tree, and selecting the next slot in the higher block.
1382 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1383 * if lowest_unlock is 1, level 0 won't be unlocked
1385 static noinline void unlock_up(struct btrfs_path *path, int level,
1386 int lowest_unlock, int min_write_lock_level,
1387 int *write_lock_level)
1390 int skip_level = level;
1392 struct extent_buffer *t;
1394 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1395 if (!path->nodes[i])
1397 if (!path->locks[i])
1399 if (!no_skips && path->slots[i] == 0) {
1403 if (!no_skips && path->keep_locks) {
1406 nritems = btrfs_header_nritems(t);
1407 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1412 if (skip_level < i && i >= lowest_unlock)
1416 if (i >= lowest_unlock && i > skip_level) {
1417 btrfs_tree_unlock_rw(t, path->locks[i]);
1419 if (write_lock_level &&
1420 i > min_write_lock_level &&
1421 i <= *write_lock_level) {
1422 *write_lock_level = i - 1;
1429 * helper function for btrfs_search_slot. The goal is to find a block
1430 * in cache without setting the path to blocking. If we find the block
1431 * we return zero and the path is unchanged.
1433 * If we can't find the block, we set the path blocking and do some
1434 * reada. -EAGAIN is returned and the search must be repeated.
1437 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
1438 struct extent_buffer **eb_ret, int level, int slot,
1439 const struct btrfs_key *key)
1441 struct btrfs_fs_info *fs_info = root->fs_info;
1444 struct extent_buffer *tmp;
1445 struct btrfs_key first_key;
1449 blocknr = btrfs_node_blockptr(*eb_ret, slot);
1450 gen = btrfs_node_ptr_generation(*eb_ret, slot);
1451 parent_level = btrfs_header_level(*eb_ret);
1452 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
1454 tmp = find_extent_buffer(fs_info, blocknr);
1456 if (p->reada == READA_FORWARD_ALWAYS)
1457 reada_for_search(fs_info, p, level, slot, key->objectid);
1459 /* first we do an atomic uptodate check */
1460 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
1462 * Do extra check for first_key, eb can be stale due to
1463 * being cached, read from scrub, or have multiple
1464 * parents (shared tree blocks).
1466 if (btrfs_verify_level_key(tmp,
1467 parent_level - 1, &first_key, gen)) {
1468 free_extent_buffer(tmp);
1475 /* now we're allowed to do a blocking uptodate check */
1476 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
1481 free_extent_buffer(tmp);
1482 btrfs_release_path(p);
1487 * reduce lock contention at high levels
1488 * of the btree by dropping locks before
1489 * we read. Don't release the lock on the current
1490 * level because we need to walk this node to figure
1491 * out which blocks to read.
1493 btrfs_unlock_up_safe(p, level + 1);
1495 if (p->reada != READA_NONE)
1496 reada_for_search(fs_info, p, level, slot, key->objectid);
1499 tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid,
1500 gen, parent_level - 1, &first_key);
1503 * If the read above didn't mark this buffer up to date,
1504 * it will never end up being up to date. Set ret to EIO now
1505 * and give up so that our caller doesn't loop forever
1508 if (!extent_buffer_uptodate(tmp))
1510 free_extent_buffer(tmp);
1515 btrfs_release_path(p);
1520 * helper function for btrfs_search_slot. This does all of the checks
1521 * for node-level blocks and does any balancing required based on
1524 * If no extra work was required, zero is returned. If we had to
1525 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1529 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1530 struct btrfs_root *root, struct btrfs_path *p,
1531 struct extent_buffer *b, int level, int ins_len,
1532 int *write_lock_level)
1534 struct btrfs_fs_info *fs_info = root->fs_info;
1537 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1538 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1540 if (*write_lock_level < level + 1) {
1541 *write_lock_level = level + 1;
1542 btrfs_release_path(p);
1546 reada_for_balance(p, level);
1547 ret = split_node(trans, root, p, level);
1549 b = p->nodes[level];
1550 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1551 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
1553 if (*write_lock_level < level + 1) {
1554 *write_lock_level = level + 1;
1555 btrfs_release_path(p);
1559 reada_for_balance(p, level);
1560 ret = balance_level(trans, root, p, level);
1564 b = p->nodes[level];
1566 btrfs_release_path(p);
1569 BUG_ON(btrfs_header_nritems(b) == 1);
1574 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
1575 u64 iobjectid, u64 ioff, u8 key_type,
1576 struct btrfs_key *found_key)
1579 struct btrfs_key key;
1580 struct extent_buffer *eb;
1585 key.type = key_type;
1586 key.objectid = iobjectid;
1589 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1593 eb = path->nodes[0];
1594 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1595 ret = btrfs_next_leaf(fs_root, path);
1598 eb = path->nodes[0];
1601 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1602 if (found_key->type != key.type ||
1603 found_key->objectid != key.objectid)
1609 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
1610 struct btrfs_path *p,
1611 int write_lock_level)
1613 struct btrfs_fs_info *fs_info = root->fs_info;
1614 struct extent_buffer *b;
1618 /* We try very hard to do read locks on the root */
1619 root_lock = BTRFS_READ_LOCK;
1621 if (p->search_commit_root) {
1623 * The commit roots are read only so we always do read locks,
1624 * and we always must hold the commit_root_sem when doing
1625 * searches on them, the only exception is send where we don't
1626 * want to block transaction commits for a long time, so
1627 * we need to clone the commit root in order to avoid races
1628 * with transaction commits that create a snapshot of one of
1629 * the roots used by a send operation.
1631 if (p->need_commit_sem) {
1632 down_read(&fs_info->commit_root_sem);
1633 b = btrfs_clone_extent_buffer(root->commit_root);
1634 up_read(&fs_info->commit_root_sem);
1636 return ERR_PTR(-ENOMEM);
1639 b = root->commit_root;
1640 atomic_inc(&b->refs);
1642 level = btrfs_header_level(b);
1644 * Ensure that all callers have set skip_locking when
1645 * p->search_commit_root = 1.
1647 ASSERT(p->skip_locking == 1);
1652 if (p->skip_locking) {
1653 b = btrfs_root_node(root);
1654 level = btrfs_header_level(b);
1659 * If the level is set to maximum, we can skip trying to get the read
1662 if (write_lock_level < BTRFS_MAX_LEVEL) {
1664 * We don't know the level of the root node until we actually
1665 * have it read locked
1667 b = btrfs_read_lock_root_node(root);
1668 level = btrfs_header_level(b);
1669 if (level > write_lock_level)
1672 /* Whoops, must trade for write lock */
1673 btrfs_tree_read_unlock(b);
1674 free_extent_buffer(b);
1677 b = btrfs_lock_root_node(root);
1678 root_lock = BTRFS_WRITE_LOCK;
1680 /* The level might have changed, check again */
1681 level = btrfs_header_level(b);
1684 p->nodes[level] = b;
1685 if (!p->skip_locking)
1686 p->locks[level] = root_lock;
1688 * Callers are responsible for dropping b's references.
1695 * btrfs_search_slot - look for a key in a tree and perform necessary
1696 * modifications to preserve tree invariants.
1698 * @trans: Handle of transaction, used when modifying the tree
1699 * @p: Holds all btree nodes along the search path
1700 * @root: The root node of the tree
1701 * @key: The key we are looking for
1702 * @ins_len: Indicates purpose of search:
1703 * >0 for inserts it's size of item inserted (*)
1705 * 0 for plain searches, not modifying the tree
1707 * (*) If size of item inserted doesn't include
1708 * sizeof(struct btrfs_item), then p->search_for_extension must
1710 * @cow: boolean should CoW operations be performed. Must always be 1
1711 * when modifying the tree.
1713 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
1714 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
1716 * If @key is found, 0 is returned and you can find the item in the leaf level
1717 * of the path (level 0)
1719 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
1720 * points to the slot where it should be inserted
1722 * If an error is encountered while searching the tree a negative error number
1725 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1726 const struct btrfs_key *key, struct btrfs_path *p,
1727 int ins_len, int cow)
1729 struct extent_buffer *b;
1734 int lowest_unlock = 1;
1735 /* everything at write_lock_level or lower must be write locked */
1736 int write_lock_level = 0;
1737 u8 lowest_level = 0;
1738 int min_write_lock_level;
1741 lowest_level = p->lowest_level;
1742 WARN_ON(lowest_level && ins_len > 0);
1743 WARN_ON(p->nodes[0] != NULL);
1744 BUG_ON(!cow && ins_len);
1749 /* when we are removing items, we might have to go up to level
1750 * two as we update tree pointers Make sure we keep write
1751 * for those levels as well
1753 write_lock_level = 2;
1754 } else if (ins_len > 0) {
1756 * for inserting items, make sure we have a write lock on
1757 * level 1 so we can update keys
1759 write_lock_level = 1;
1763 write_lock_level = -1;
1765 if (cow && (p->keep_locks || p->lowest_level))
1766 write_lock_level = BTRFS_MAX_LEVEL;
1768 min_write_lock_level = write_lock_level;
1772 b = btrfs_search_slot_get_root(root, p, write_lock_level);
1781 level = btrfs_header_level(b);
1784 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
1787 * if we don't really need to cow this block
1788 * then we don't want to set the path blocking,
1789 * so we test it here
1791 if (!should_cow_block(trans, root, b)) {
1792 trans->dirty = true;
1797 * must have write locks on this node and the
1800 if (level > write_lock_level ||
1801 (level + 1 > write_lock_level &&
1802 level + 1 < BTRFS_MAX_LEVEL &&
1803 p->nodes[level + 1])) {
1804 write_lock_level = level + 1;
1805 btrfs_release_path(p);
1810 err = btrfs_cow_block(trans, root, b, NULL, 0,
1814 err = btrfs_cow_block(trans, root, b,
1815 p->nodes[level + 1],
1816 p->slots[level + 1], &b,
1824 p->nodes[level] = b;
1826 * Leave path with blocking locks to avoid massive
1827 * lock context switch, this is made on purpose.
1831 * we have a lock on b and as long as we aren't changing
1832 * the tree, there is no way to for the items in b to change.
1833 * It is safe to drop the lock on our parent before we
1834 * go through the expensive btree search on b.
1836 * If we're inserting or deleting (ins_len != 0), then we might
1837 * be changing slot zero, which may require changing the parent.
1838 * So, we can't drop the lock until after we know which slot
1839 * we're operating on.
1841 if (!ins_len && !p->keep_locks) {
1844 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
1845 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
1851 * If btrfs_bin_search returns an exact match (prev_cmp == 0)
1852 * we can safely assume the target key will always be in slot 0
1853 * on lower levels due to the invariants BTRFS' btree provides,
1854 * namely that a btrfs_key_ptr entry always points to the
1855 * lowest key in the child node, thus we can skip searching
1858 if (prev_cmp == 0) {
1862 ret = btrfs_bin_search(b, key, &slot);
1869 p->slots[level] = slot;
1871 * Item key already exists. In this case, if we are
1872 * allowed to insert the item (for example, in dir_item
1873 * case, item key collision is allowed), it will be
1874 * merged with the original item. Only the item size
1875 * grows, no new btrfs item will be added. If
1876 * search_for_extension is not set, ins_len already
1877 * accounts the size btrfs_item, deduct it here so leaf
1878 * space check will be correct.
1880 if (ret == 0 && ins_len > 0 && !p->search_for_extension) {
1881 ASSERT(ins_len >= sizeof(struct btrfs_item));
1882 ins_len -= sizeof(struct btrfs_item);
1885 btrfs_leaf_free_space(b) < ins_len) {
1886 if (write_lock_level < 1) {
1887 write_lock_level = 1;
1888 btrfs_release_path(p);
1892 err = split_leaf(trans, root, key,
1893 p, ins_len, ret == 0);
1901 if (!p->search_for_split)
1902 unlock_up(p, level, lowest_unlock,
1903 min_write_lock_level, NULL);
1906 if (ret && slot > 0) {
1910 p->slots[level] = slot;
1911 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
1919 b = p->nodes[level];
1920 slot = p->slots[level];
1923 * Slot 0 is special, if we change the key we have to update
1924 * the parent pointer which means we must have a write lock on
1927 if (slot == 0 && ins_len && write_lock_level < level + 1) {
1928 write_lock_level = level + 1;
1929 btrfs_release_path(p);
1933 unlock_up(p, level, lowest_unlock, min_write_lock_level,
1936 if (level == lowest_level) {
1942 err = read_block_for_search(root, p, &b, level, slot, key);
1950 if (!p->skip_locking) {
1951 level = btrfs_header_level(b);
1952 if (level <= write_lock_level) {
1954 p->locks[level] = BTRFS_WRITE_LOCK;
1956 btrfs_tree_read_lock(b);
1957 p->locks[level] = BTRFS_READ_LOCK;
1959 p->nodes[level] = b;
1964 if (ret < 0 && !p->skip_release_on_error)
1965 btrfs_release_path(p);
1968 ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
1971 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
1972 * current state of the tree together with the operations recorded in the tree
1973 * modification log to search for the key in a previous version of this tree, as
1974 * denoted by the time_seq parameter.
1976 * Naturally, there is no support for insert, delete or cow operations.
1978 * The resulting path and return value will be set up as if we called
1979 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
1981 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
1982 struct btrfs_path *p, u64 time_seq)
1984 struct btrfs_fs_info *fs_info = root->fs_info;
1985 struct extent_buffer *b;
1990 int lowest_unlock = 1;
1991 u8 lowest_level = 0;
1993 lowest_level = p->lowest_level;
1994 WARN_ON(p->nodes[0] != NULL);
1996 if (p->search_commit_root) {
1998 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2002 b = btrfs_get_old_root(root, time_seq);
2007 level = btrfs_header_level(b);
2008 p->locks[level] = BTRFS_READ_LOCK;
2013 level = btrfs_header_level(b);
2014 p->nodes[level] = b;
2017 * we have a lock on b and as long as we aren't changing
2018 * the tree, there is no way to for the items in b to change.
2019 * It is safe to drop the lock on our parent before we
2020 * go through the expensive btree search on b.
2022 btrfs_unlock_up_safe(p, level + 1);
2024 ret = btrfs_bin_search(b, key, &slot);
2029 p->slots[level] = slot;
2030 unlock_up(p, level, lowest_unlock, 0, NULL);
2034 if (ret && slot > 0) {
2038 p->slots[level] = slot;
2039 unlock_up(p, level, lowest_unlock, 0, NULL);
2041 if (level == lowest_level) {
2047 err = read_block_for_search(root, p, &b, level, slot, key);
2055 level = btrfs_header_level(b);
2056 btrfs_tree_read_lock(b);
2057 b = btrfs_tree_mod_log_rewind(fs_info, p, b, time_seq);
2062 p->locks[level] = BTRFS_READ_LOCK;
2063 p->nodes[level] = b;
2068 btrfs_release_path(p);
2074 * helper to use instead of search slot if no exact match is needed but
2075 * instead the next or previous item should be returned.
2076 * When find_higher is true, the next higher item is returned, the next lower
2078 * When return_any and find_higher are both true, and no higher item is found,
2079 * return the next lower instead.
2080 * When return_any is true and find_higher is false, and no lower item is found,
2081 * return the next higher instead.
2082 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2085 int btrfs_search_slot_for_read(struct btrfs_root *root,
2086 const struct btrfs_key *key,
2087 struct btrfs_path *p, int find_higher,
2091 struct extent_buffer *leaf;
2094 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2098 * a return value of 1 means the path is at the position where the
2099 * item should be inserted. Normally this is the next bigger item,
2100 * but in case the previous item is the last in a leaf, path points
2101 * to the first free slot in the previous leaf, i.e. at an invalid
2107 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2108 ret = btrfs_next_leaf(root, p);
2114 * no higher item found, return the next
2119 btrfs_release_path(p);
2123 if (p->slots[0] == 0) {
2124 ret = btrfs_prev_leaf(root, p);
2129 if (p->slots[0] == btrfs_header_nritems(leaf))
2136 * no lower item found, return the next
2141 btrfs_release_path(p);
2151 * adjust the pointers going up the tree, starting at level
2152 * making sure the right key of each node is points to 'key'.
2153 * This is used after shifting pointers to the left, so it stops
2154 * fixing up pointers when a given leaf/node is not in slot 0 of the
2158 static void fixup_low_keys(struct btrfs_path *path,
2159 struct btrfs_disk_key *key, int level)
2162 struct extent_buffer *t;
2165 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2166 int tslot = path->slots[i];
2168 if (!path->nodes[i])
2171 ret = btrfs_tree_mod_log_insert_key(t, tslot,
2172 BTRFS_MOD_LOG_KEY_REPLACE, GFP_ATOMIC);
2174 btrfs_set_node_key(t, key, tslot);
2175 btrfs_mark_buffer_dirty(path->nodes[i]);
2184 * This function isn't completely safe. It's the caller's responsibility
2185 * that the new key won't break the order
2187 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2188 struct btrfs_path *path,
2189 const struct btrfs_key *new_key)
2191 struct btrfs_disk_key disk_key;
2192 struct extent_buffer *eb;
2195 eb = path->nodes[0];
2196 slot = path->slots[0];
2198 btrfs_item_key(eb, &disk_key, slot - 1);
2199 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
2201 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2202 slot, btrfs_disk_key_objectid(&disk_key),
2203 btrfs_disk_key_type(&disk_key),
2204 btrfs_disk_key_offset(&disk_key),
2205 new_key->objectid, new_key->type,
2207 btrfs_print_leaf(eb);
2211 if (slot < btrfs_header_nritems(eb) - 1) {
2212 btrfs_item_key(eb, &disk_key, slot + 1);
2213 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
2215 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2216 slot, btrfs_disk_key_objectid(&disk_key),
2217 btrfs_disk_key_type(&disk_key),
2218 btrfs_disk_key_offset(&disk_key),
2219 new_key->objectid, new_key->type,
2221 btrfs_print_leaf(eb);
2226 btrfs_cpu_key_to_disk(&disk_key, new_key);
2227 btrfs_set_item_key(eb, &disk_key, slot);
2228 btrfs_mark_buffer_dirty(eb);
2230 fixup_low_keys(path, &disk_key, 1);
2234 * Check key order of two sibling extent buffers.
2236 * Return true if something is wrong.
2237 * Return false if everything is fine.
2239 * Tree-checker only works inside one tree block, thus the following
2240 * corruption can not be detected by tree-checker:
2242 * Leaf @left | Leaf @right
2243 * --------------------------------------------------------------
2244 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
2246 * Key f6 in leaf @left itself is valid, but not valid when the next
2247 * key in leaf @right is 7.
2248 * This can only be checked at tree block merge time.
2249 * And since tree checker has ensured all key order in each tree block
2250 * is correct, we only need to bother the last key of @left and the first
2253 static bool check_sibling_keys(struct extent_buffer *left,
2254 struct extent_buffer *right)
2256 struct btrfs_key left_last;
2257 struct btrfs_key right_first;
2258 int level = btrfs_header_level(left);
2259 int nr_left = btrfs_header_nritems(left);
2260 int nr_right = btrfs_header_nritems(right);
2262 /* No key to check in one of the tree blocks */
2263 if (!nr_left || !nr_right)
2267 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
2268 btrfs_node_key_to_cpu(right, &right_first, 0);
2270 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
2271 btrfs_item_key_to_cpu(right, &right_first, 0);
2274 if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
2275 btrfs_crit(left->fs_info,
2276 "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
2277 left_last.objectid, left_last.type,
2278 left_last.offset, right_first.objectid,
2279 right_first.type, right_first.offset);
2286 * try to push data from one node into the next node left in the
2289 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2290 * error, and > 0 if there was no room in the left hand block.
2292 static int push_node_left(struct btrfs_trans_handle *trans,
2293 struct extent_buffer *dst,
2294 struct extent_buffer *src, int empty)
2296 struct btrfs_fs_info *fs_info = trans->fs_info;
2302 src_nritems = btrfs_header_nritems(src);
2303 dst_nritems = btrfs_header_nritems(dst);
2304 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2305 WARN_ON(btrfs_header_generation(src) != trans->transid);
2306 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2308 if (!empty && src_nritems <= 8)
2311 if (push_items <= 0)
2315 push_items = min(src_nritems, push_items);
2316 if (push_items < src_nritems) {
2317 /* leave at least 8 pointers in the node if
2318 * we aren't going to empty it
2320 if (src_nritems - push_items < 8) {
2321 if (push_items <= 8)
2327 push_items = min(src_nritems - 8, push_items);
2329 /* dst is the left eb, src is the middle eb */
2330 if (check_sibling_keys(dst, src)) {
2332 btrfs_abort_transaction(trans, ret);
2335 ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
2337 btrfs_abort_transaction(trans, ret);
2340 copy_extent_buffer(dst, src,
2341 btrfs_node_key_ptr_offset(dst_nritems),
2342 btrfs_node_key_ptr_offset(0),
2343 push_items * sizeof(struct btrfs_key_ptr));
2345 if (push_items < src_nritems) {
2347 * Don't call btrfs_tree_mod_log_insert_move() here, key removal
2348 * was already fully logged by btrfs_tree_mod_log_eb_copy() above.
2350 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2351 btrfs_node_key_ptr_offset(push_items),
2352 (src_nritems - push_items) *
2353 sizeof(struct btrfs_key_ptr));
2355 btrfs_set_header_nritems(src, src_nritems - push_items);
2356 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2357 btrfs_mark_buffer_dirty(src);
2358 btrfs_mark_buffer_dirty(dst);
2364 * try to push data from one node into the next node right in the
2367 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2368 * error, and > 0 if there was no room in the right hand block.
2370 * this will only push up to 1/2 the contents of the left node over
2372 static int balance_node_right(struct btrfs_trans_handle *trans,
2373 struct extent_buffer *dst,
2374 struct extent_buffer *src)
2376 struct btrfs_fs_info *fs_info = trans->fs_info;
2383 WARN_ON(btrfs_header_generation(src) != trans->transid);
2384 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2386 src_nritems = btrfs_header_nritems(src);
2387 dst_nritems = btrfs_header_nritems(dst);
2388 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2389 if (push_items <= 0)
2392 if (src_nritems < 4)
2395 max_push = src_nritems / 2 + 1;
2396 /* don't try to empty the node */
2397 if (max_push >= src_nritems)
2400 if (max_push < push_items)
2401 push_items = max_push;
2403 /* dst is the right eb, src is the middle eb */
2404 if (check_sibling_keys(src, dst)) {
2406 btrfs_abort_transaction(trans, ret);
2409 ret = btrfs_tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
2411 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2412 btrfs_node_key_ptr_offset(0),
2414 sizeof(struct btrfs_key_ptr));
2416 ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
2419 btrfs_abort_transaction(trans, ret);
2422 copy_extent_buffer(dst, src,
2423 btrfs_node_key_ptr_offset(0),
2424 btrfs_node_key_ptr_offset(src_nritems - push_items),
2425 push_items * sizeof(struct btrfs_key_ptr));
2427 btrfs_set_header_nritems(src, src_nritems - push_items);
2428 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2430 btrfs_mark_buffer_dirty(src);
2431 btrfs_mark_buffer_dirty(dst);
2437 * helper function to insert a new root level in the tree.
2438 * A new node is allocated, and a single item is inserted to
2439 * point to the existing root
2441 * returns zero on success or < 0 on failure.
2443 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2444 struct btrfs_root *root,
2445 struct btrfs_path *path, int level)
2447 struct btrfs_fs_info *fs_info = root->fs_info;
2449 struct extent_buffer *lower;
2450 struct extent_buffer *c;
2451 struct extent_buffer *old;
2452 struct btrfs_disk_key lower_key;
2455 BUG_ON(path->nodes[level]);
2456 BUG_ON(path->nodes[level-1] != root->node);
2458 lower = path->nodes[level-1];
2460 btrfs_item_key(lower, &lower_key, 0);
2462 btrfs_node_key(lower, &lower_key, 0);
2464 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
2465 root->node->start, 0,
2466 BTRFS_NESTING_NEW_ROOT);
2470 root_add_used(root, fs_info->nodesize);
2472 btrfs_set_header_nritems(c, 1);
2473 btrfs_set_node_key(c, &lower_key, 0);
2474 btrfs_set_node_blockptr(c, 0, lower->start);
2475 lower_gen = btrfs_header_generation(lower);
2476 WARN_ON(lower_gen != trans->transid);
2478 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2480 btrfs_mark_buffer_dirty(c);
2483 ret = btrfs_tree_mod_log_insert_root(root->node, c, false);
2485 rcu_assign_pointer(root->node, c);
2487 /* the super has an extra ref to root->node */
2488 free_extent_buffer(old);
2490 add_root_to_dirty_list(root);
2491 atomic_inc(&c->refs);
2492 path->nodes[level] = c;
2493 path->locks[level] = BTRFS_WRITE_LOCK;
2494 path->slots[level] = 0;
2499 * worker function to insert a single pointer in a node.
2500 * the node should have enough room for the pointer already
2502 * slot and level indicate where you want the key to go, and
2503 * blocknr is the block the key points to.
2505 static void insert_ptr(struct btrfs_trans_handle *trans,
2506 struct btrfs_path *path,
2507 struct btrfs_disk_key *key, u64 bytenr,
2508 int slot, int level)
2510 struct extent_buffer *lower;
2514 BUG_ON(!path->nodes[level]);
2515 btrfs_assert_tree_locked(path->nodes[level]);
2516 lower = path->nodes[level];
2517 nritems = btrfs_header_nritems(lower);
2518 BUG_ON(slot > nritems);
2519 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
2520 if (slot != nritems) {
2522 ret = btrfs_tree_mod_log_insert_move(lower, slot + 1,
2523 slot, nritems - slot);
2526 memmove_extent_buffer(lower,
2527 btrfs_node_key_ptr_offset(slot + 1),
2528 btrfs_node_key_ptr_offset(slot),
2529 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2532 ret = btrfs_tree_mod_log_insert_key(lower, slot,
2533 BTRFS_MOD_LOG_KEY_ADD, GFP_NOFS);
2536 btrfs_set_node_key(lower, key, slot);
2537 btrfs_set_node_blockptr(lower, slot, bytenr);
2538 WARN_ON(trans->transid == 0);
2539 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2540 btrfs_set_header_nritems(lower, nritems + 1);
2541 btrfs_mark_buffer_dirty(lower);
2545 * split the node at the specified level in path in two.
2546 * The path is corrected to point to the appropriate node after the split
2548 * Before splitting this tries to make some room in the node by pushing
2549 * left and right, if either one works, it returns right away.
2551 * returns 0 on success and < 0 on failure
2553 static noinline int split_node(struct btrfs_trans_handle *trans,
2554 struct btrfs_root *root,
2555 struct btrfs_path *path, int level)
2557 struct btrfs_fs_info *fs_info = root->fs_info;
2558 struct extent_buffer *c;
2559 struct extent_buffer *split;
2560 struct btrfs_disk_key disk_key;
2565 c = path->nodes[level];
2566 WARN_ON(btrfs_header_generation(c) != trans->transid);
2567 if (c == root->node) {
2569 * trying to split the root, lets make a new one
2571 * tree mod log: We don't log_removal old root in
2572 * insert_new_root, because that root buffer will be kept as a
2573 * normal node. We are going to log removal of half of the
2574 * elements below with btrfs_tree_mod_log_eb_copy(). We're
2575 * holding a tree lock on the buffer, which is why we cannot
2576 * race with other tree_mod_log users.
2578 ret = insert_new_root(trans, root, path, level + 1);
2582 ret = push_nodes_for_insert(trans, root, path, level);
2583 c = path->nodes[level];
2584 if (!ret && btrfs_header_nritems(c) <
2585 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
2591 c_nritems = btrfs_header_nritems(c);
2592 mid = (c_nritems + 1) / 2;
2593 btrfs_node_key(c, &disk_key, mid);
2595 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
2596 c->start, 0, BTRFS_NESTING_SPLIT);
2598 return PTR_ERR(split);
2600 root_add_used(root, fs_info->nodesize);
2601 ASSERT(btrfs_header_level(c) == level);
2603 ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
2605 btrfs_abort_transaction(trans, ret);
2608 copy_extent_buffer(split, c,
2609 btrfs_node_key_ptr_offset(0),
2610 btrfs_node_key_ptr_offset(mid),
2611 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2612 btrfs_set_header_nritems(split, c_nritems - mid);
2613 btrfs_set_header_nritems(c, mid);
2615 btrfs_mark_buffer_dirty(c);
2616 btrfs_mark_buffer_dirty(split);
2618 insert_ptr(trans, path, &disk_key, split->start,
2619 path->slots[level + 1] + 1, level + 1);
2621 if (path->slots[level] >= mid) {
2622 path->slots[level] -= mid;
2623 btrfs_tree_unlock(c);
2624 free_extent_buffer(c);
2625 path->nodes[level] = split;
2626 path->slots[level + 1] += 1;
2628 btrfs_tree_unlock(split);
2629 free_extent_buffer(split);
2635 * how many bytes are required to store the items in a leaf. start
2636 * and nr indicate which items in the leaf to check. This totals up the
2637 * space used both by the item structs and the item data
2639 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2641 struct btrfs_item *start_item;
2642 struct btrfs_item *end_item;
2644 int nritems = btrfs_header_nritems(l);
2645 int end = min(nritems, start + nr) - 1;
2649 start_item = btrfs_item_nr(start);
2650 end_item = btrfs_item_nr(end);
2651 data_len = btrfs_item_offset(l, start_item) +
2652 btrfs_item_size(l, start_item);
2653 data_len = data_len - btrfs_item_offset(l, end_item);
2654 data_len += sizeof(struct btrfs_item) * nr;
2655 WARN_ON(data_len < 0);
2660 * The space between the end of the leaf items and
2661 * the start of the leaf data. IOW, how much room
2662 * the leaf has left for both items and data
2664 noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
2666 struct btrfs_fs_info *fs_info = leaf->fs_info;
2667 int nritems = btrfs_header_nritems(leaf);
2670 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
2673 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
2675 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
2676 leaf_space_used(leaf, 0, nritems), nritems);
2682 * min slot controls the lowest index we're willing to push to the
2683 * right. We'll push up to and including min_slot, but no lower
2685 static noinline int __push_leaf_right(struct btrfs_path *path,
2686 int data_size, int empty,
2687 struct extent_buffer *right,
2688 int free_space, u32 left_nritems,
2691 struct btrfs_fs_info *fs_info = right->fs_info;
2692 struct extent_buffer *left = path->nodes[0];
2693 struct extent_buffer *upper = path->nodes[1];
2694 struct btrfs_map_token token;
2695 struct btrfs_disk_key disk_key;
2700 struct btrfs_item *item;
2709 nr = max_t(u32, 1, min_slot);
2711 if (path->slots[0] >= left_nritems)
2712 push_space += data_size;
2714 slot = path->slots[1];
2715 i = left_nritems - 1;
2717 item = btrfs_item_nr(i);
2719 if (!empty && push_items > 0) {
2720 if (path->slots[0] > i)
2722 if (path->slots[0] == i) {
2723 int space = btrfs_leaf_free_space(left);
2725 if (space + push_space * 2 > free_space)
2730 if (path->slots[0] == i)
2731 push_space += data_size;
2733 this_item_size = btrfs_item_size(left, item);
2734 if (this_item_size + sizeof(*item) + push_space > free_space)
2738 push_space += this_item_size + sizeof(*item);
2744 if (push_items == 0)
2747 WARN_ON(!empty && push_items == left_nritems);
2749 /* push left to right */
2750 right_nritems = btrfs_header_nritems(right);
2752 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2753 push_space -= leaf_data_end(left);
2755 /* make room in the right data area */
2756 data_end = leaf_data_end(right);
2757 memmove_extent_buffer(right,
2758 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
2759 BTRFS_LEAF_DATA_OFFSET + data_end,
2760 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
2762 /* copy from the left data area */
2763 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
2764 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
2765 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
2768 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2769 btrfs_item_nr_offset(0),
2770 right_nritems * sizeof(struct btrfs_item));
2772 /* copy the items from left to right */
2773 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2774 btrfs_item_nr_offset(left_nritems - push_items),
2775 push_items * sizeof(struct btrfs_item));
2777 /* update the item pointers */
2778 btrfs_init_map_token(&token, right);
2779 right_nritems += push_items;
2780 btrfs_set_header_nritems(right, right_nritems);
2781 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
2782 for (i = 0; i < right_nritems; i++) {
2783 item = btrfs_item_nr(i);
2784 push_space -= btrfs_token_item_size(&token, item);
2785 btrfs_set_token_item_offset(&token, item, push_space);
2788 left_nritems -= push_items;
2789 btrfs_set_header_nritems(left, left_nritems);
2792 btrfs_mark_buffer_dirty(left);
2794 btrfs_clean_tree_block(left);
2796 btrfs_mark_buffer_dirty(right);
2798 btrfs_item_key(right, &disk_key, 0);
2799 btrfs_set_node_key(upper, &disk_key, slot + 1);
2800 btrfs_mark_buffer_dirty(upper);
2802 /* then fixup the leaf pointer in the path */
2803 if (path->slots[0] >= left_nritems) {
2804 path->slots[0] -= left_nritems;
2805 if (btrfs_header_nritems(path->nodes[0]) == 0)
2806 btrfs_clean_tree_block(path->nodes[0]);
2807 btrfs_tree_unlock(path->nodes[0]);
2808 free_extent_buffer(path->nodes[0]);
2809 path->nodes[0] = right;
2810 path->slots[1] += 1;
2812 btrfs_tree_unlock(right);
2813 free_extent_buffer(right);
2818 btrfs_tree_unlock(right);
2819 free_extent_buffer(right);
2824 * push some data in the path leaf to the right, trying to free up at
2825 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2827 * returns 1 if the push failed because the other node didn't have enough
2828 * room, 0 if everything worked out and < 0 if there were major errors.
2830 * this will push starting from min_slot to the end of the leaf. It won't
2831 * push any slot lower than min_slot
2833 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2834 *root, struct btrfs_path *path,
2835 int min_data_size, int data_size,
2836 int empty, u32 min_slot)
2838 struct extent_buffer *left = path->nodes[0];
2839 struct extent_buffer *right;
2840 struct extent_buffer *upper;
2846 if (!path->nodes[1])
2849 slot = path->slots[1];
2850 upper = path->nodes[1];
2851 if (slot >= btrfs_header_nritems(upper) - 1)
2854 btrfs_assert_tree_locked(path->nodes[1]);
2856 right = btrfs_read_node_slot(upper, slot + 1);
2858 * slot + 1 is not valid or we fail to read the right node,
2859 * no big deal, just return.
2864 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
2866 free_space = btrfs_leaf_free_space(right);
2867 if (free_space < data_size)
2870 /* cow and double check */
2871 ret = btrfs_cow_block(trans, root, right, upper,
2872 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
2876 free_space = btrfs_leaf_free_space(right);
2877 if (free_space < data_size)
2880 left_nritems = btrfs_header_nritems(left);
2881 if (left_nritems == 0)
2884 if (check_sibling_keys(left, right)) {
2886 btrfs_tree_unlock(right);
2887 free_extent_buffer(right);
2890 if (path->slots[0] == left_nritems && !empty) {
2891 /* Key greater than all keys in the leaf, right neighbor has
2892 * enough room for it and we're not emptying our leaf to delete
2893 * it, therefore use right neighbor to insert the new item and
2894 * no need to touch/dirty our left leaf. */
2895 btrfs_tree_unlock(left);
2896 free_extent_buffer(left);
2897 path->nodes[0] = right;
2903 return __push_leaf_right(path, min_data_size, empty,
2904 right, free_space, left_nritems, min_slot);
2906 btrfs_tree_unlock(right);
2907 free_extent_buffer(right);
2912 * push some data in the path leaf to the left, trying to free up at
2913 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2915 * max_slot can put a limit on how far into the leaf we'll push items. The
2916 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2919 static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
2920 int empty, struct extent_buffer *left,
2921 int free_space, u32 right_nritems,
2924 struct btrfs_fs_info *fs_info = left->fs_info;
2925 struct btrfs_disk_key disk_key;
2926 struct extent_buffer *right = path->nodes[0];
2930 struct btrfs_item *item;
2931 u32 old_left_nritems;
2935 u32 old_left_item_size;
2936 struct btrfs_map_token token;
2939 nr = min(right_nritems, max_slot);
2941 nr = min(right_nritems - 1, max_slot);
2943 for (i = 0; i < nr; i++) {
2944 item = btrfs_item_nr(i);
2946 if (!empty && push_items > 0) {
2947 if (path->slots[0] < i)
2949 if (path->slots[0] == i) {
2950 int space = btrfs_leaf_free_space(right);
2952 if (space + push_space * 2 > free_space)
2957 if (path->slots[0] == i)
2958 push_space += data_size;
2960 this_item_size = btrfs_item_size(right, item);
2961 if (this_item_size + sizeof(*item) + push_space > free_space)
2965 push_space += this_item_size + sizeof(*item);
2968 if (push_items == 0) {
2972 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
2974 /* push data from right to left */
2975 copy_extent_buffer(left, right,
2976 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2977 btrfs_item_nr_offset(0),
2978 push_items * sizeof(struct btrfs_item));
2980 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
2981 btrfs_item_offset_nr(right, push_items - 1);
2983 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
2984 leaf_data_end(left) - push_space,
2985 BTRFS_LEAF_DATA_OFFSET +
2986 btrfs_item_offset_nr(right, push_items - 1),
2988 old_left_nritems = btrfs_header_nritems(left);
2989 BUG_ON(old_left_nritems <= 0);
2991 btrfs_init_map_token(&token, left);
2992 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2993 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2996 item = btrfs_item_nr(i);
2998 ioff = btrfs_token_item_offset(&token, item);
2999 btrfs_set_token_item_offset(&token, item,
3000 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
3002 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3004 /* fixup right node */
3005 if (push_items > right_nritems)
3006 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3009 if (push_items < right_nritems) {
3010 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3011 leaf_data_end(right);
3012 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
3013 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3014 BTRFS_LEAF_DATA_OFFSET +
3015 leaf_data_end(right), push_space);
3017 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3018 btrfs_item_nr_offset(push_items),
3019 (btrfs_header_nritems(right) - push_items) *
3020 sizeof(struct btrfs_item));
3023 btrfs_init_map_token(&token, right);
3024 right_nritems -= push_items;
3025 btrfs_set_header_nritems(right, right_nritems);
3026 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3027 for (i = 0; i < right_nritems; i++) {
3028 item = btrfs_item_nr(i);
3030 push_space = push_space - btrfs_token_item_size(&token, item);
3031 btrfs_set_token_item_offset(&token, item, push_space);
3034 btrfs_mark_buffer_dirty(left);
3036 btrfs_mark_buffer_dirty(right);
3038 btrfs_clean_tree_block(right);
3040 btrfs_item_key(right, &disk_key, 0);
3041 fixup_low_keys(path, &disk_key, 1);
3043 /* then fixup the leaf pointer in the path */
3044 if (path->slots[0] < push_items) {
3045 path->slots[0] += old_left_nritems;
3046 btrfs_tree_unlock(path->nodes[0]);
3047 free_extent_buffer(path->nodes[0]);
3048 path->nodes[0] = left;
3049 path->slots[1] -= 1;
3051 btrfs_tree_unlock(left);
3052 free_extent_buffer(left);
3053 path->slots[0] -= push_items;
3055 BUG_ON(path->slots[0] < 0);
3058 btrfs_tree_unlock(left);
3059 free_extent_buffer(left);
3064 * push some data in the path leaf to the left, trying to free up at
3065 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3067 * max_slot can put a limit on how far into the leaf we'll push items. The
3068 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3071 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3072 *root, struct btrfs_path *path, int min_data_size,
3073 int data_size, int empty, u32 max_slot)
3075 struct extent_buffer *right = path->nodes[0];
3076 struct extent_buffer *left;
3082 slot = path->slots[1];
3085 if (!path->nodes[1])
3088 right_nritems = btrfs_header_nritems(right);
3089 if (right_nritems == 0)
3092 btrfs_assert_tree_locked(path->nodes[1]);
3094 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
3096 * slot - 1 is not valid or we fail to read the left node,
3097 * no big deal, just return.
3102 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
3104 free_space = btrfs_leaf_free_space(left);
3105 if (free_space < data_size) {
3110 /* cow and double check */
3111 ret = btrfs_cow_block(trans, root, left,
3112 path->nodes[1], slot - 1, &left,
3113 BTRFS_NESTING_LEFT_COW);
3115 /* we hit -ENOSPC, but it isn't fatal here */
3121 free_space = btrfs_leaf_free_space(left);
3122 if (free_space < data_size) {
3127 if (check_sibling_keys(left, right)) {
3131 return __push_leaf_left(path, min_data_size,
3132 empty, left, free_space, right_nritems,
3135 btrfs_tree_unlock(left);
3136 free_extent_buffer(left);
3141 * split the path's leaf in two, making sure there is at least data_size
3142 * available for the resulting leaf level of the path.
3144 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3145 struct btrfs_path *path,
3146 struct extent_buffer *l,
3147 struct extent_buffer *right,
3148 int slot, int mid, int nritems)
3150 struct btrfs_fs_info *fs_info = trans->fs_info;
3154 struct btrfs_disk_key disk_key;
3155 struct btrfs_map_token token;
3157 nritems = nritems - mid;
3158 btrfs_set_header_nritems(right, nritems);
3159 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
3161 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3162 btrfs_item_nr_offset(mid),
3163 nritems * sizeof(struct btrfs_item));
3165 copy_extent_buffer(right, l,
3166 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
3167 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
3168 leaf_data_end(l), data_copy_size);
3170 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
3172 btrfs_init_map_token(&token, right);
3173 for (i = 0; i < nritems; i++) {
3174 struct btrfs_item *item = btrfs_item_nr(i);
3177 ioff = btrfs_token_item_offset(&token, item);
3178 btrfs_set_token_item_offset(&token, item, ioff + rt_data_off);
3181 btrfs_set_header_nritems(l, mid);
3182 btrfs_item_key(right, &disk_key, 0);
3183 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
3185 btrfs_mark_buffer_dirty(right);
3186 btrfs_mark_buffer_dirty(l);
3187 BUG_ON(path->slots[0] != slot);
3190 btrfs_tree_unlock(path->nodes[0]);
3191 free_extent_buffer(path->nodes[0]);
3192 path->nodes[0] = right;
3193 path->slots[0] -= mid;
3194 path->slots[1] += 1;
3196 btrfs_tree_unlock(right);
3197 free_extent_buffer(right);
3200 BUG_ON(path->slots[0] < 0);
3204 * double splits happen when we need to insert a big item in the middle
3205 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3206 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3209 * We avoid this by trying to push the items on either side of our target
3210 * into the adjacent leaves. If all goes well we can avoid the double split
3213 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3214 struct btrfs_root *root,
3215 struct btrfs_path *path,
3222 int space_needed = data_size;
3224 slot = path->slots[0];
3225 if (slot < btrfs_header_nritems(path->nodes[0]))
3226 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3229 * try to push all the items after our slot into the
3232 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
3239 nritems = btrfs_header_nritems(path->nodes[0]);
3241 * our goal is to get our slot at the start or end of a leaf. If
3242 * we've done so we're done
3244 if (path->slots[0] == 0 || path->slots[0] == nritems)
3247 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3250 /* try to push all the items before our slot into the next leaf */
3251 slot = path->slots[0];
3252 space_needed = data_size;
3254 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
3255 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
3268 * split the path's leaf in two, making sure there is at least data_size
3269 * available for the resulting leaf level of the path.
3271 * returns 0 if all went well and < 0 on failure.
3273 static noinline int split_leaf(struct btrfs_trans_handle *trans,
3274 struct btrfs_root *root,
3275 const struct btrfs_key *ins_key,
3276 struct btrfs_path *path, int data_size,
3279 struct btrfs_disk_key disk_key;
3280 struct extent_buffer *l;
3284 struct extent_buffer *right;
3285 struct btrfs_fs_info *fs_info = root->fs_info;
3289 int num_doubles = 0;
3290 int tried_avoid_double = 0;
3293 slot = path->slots[0];
3294 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3295 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
3298 /* first try to make some room by pushing left and right */
3299 if (data_size && path->nodes[1]) {
3300 int space_needed = data_size;
3302 if (slot < btrfs_header_nritems(l))
3303 space_needed -= btrfs_leaf_free_space(l);
3305 wret = push_leaf_right(trans, root, path, space_needed,
3306 space_needed, 0, 0);
3310 space_needed = data_size;
3312 space_needed -= btrfs_leaf_free_space(l);
3313 wret = push_leaf_left(trans, root, path, space_needed,
3314 space_needed, 0, (u32)-1);
3320 /* did the pushes work? */
3321 if (btrfs_leaf_free_space(l) >= data_size)
3325 if (!path->nodes[1]) {
3326 ret = insert_new_root(trans, root, path, 1);
3333 slot = path->slots[0];
3334 nritems = btrfs_header_nritems(l);
3335 mid = (nritems + 1) / 2;
3339 leaf_space_used(l, mid, nritems - mid) + data_size >
3340 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3341 if (slot >= nritems) {
3345 if (mid != nritems &&
3346 leaf_space_used(l, mid, nritems - mid) +
3347 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3348 if (data_size && !tried_avoid_double)
3349 goto push_for_double;
3355 if (leaf_space_used(l, 0, mid) + data_size >
3356 BTRFS_LEAF_DATA_SIZE(fs_info)) {
3357 if (!extend && data_size && slot == 0) {
3359 } else if ((extend || !data_size) && slot == 0) {
3363 if (mid != nritems &&
3364 leaf_space_used(l, mid, nritems - mid) +
3365 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
3366 if (data_size && !tried_avoid_double)
3367 goto push_for_double;
3375 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3377 btrfs_item_key(l, &disk_key, mid);
3380 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
3381 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
3382 * subclasses, which is 8 at the time of this patch, and we've maxed it
3383 * out. In the future we could add a
3384 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
3385 * use BTRFS_NESTING_NEW_ROOT.
3387 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
3388 l->start, 0, num_doubles ?
3389 BTRFS_NESTING_NEW_ROOT :
3390 BTRFS_NESTING_SPLIT);
3392 return PTR_ERR(right);
3394 root_add_used(root, fs_info->nodesize);
3398 btrfs_set_header_nritems(right, 0);
3399 insert_ptr(trans, path, &disk_key,
3400 right->start, path->slots[1] + 1, 1);
3401 btrfs_tree_unlock(path->nodes[0]);
3402 free_extent_buffer(path->nodes[0]);
3403 path->nodes[0] = right;
3405 path->slots[1] += 1;
3407 btrfs_set_header_nritems(right, 0);
3408 insert_ptr(trans, path, &disk_key,
3409 right->start, path->slots[1], 1);
3410 btrfs_tree_unlock(path->nodes[0]);
3411 free_extent_buffer(path->nodes[0]);
3412 path->nodes[0] = right;
3414 if (path->slots[1] == 0)
3415 fixup_low_keys(path, &disk_key, 1);
3418 * We create a new leaf 'right' for the required ins_len and
3419 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
3420 * the content of ins_len to 'right'.
3425 copy_for_split(trans, path, l, right, slot, mid, nritems);
3428 BUG_ON(num_doubles != 0);
3436 push_for_double_split(trans, root, path, data_size);
3437 tried_avoid_double = 1;
3438 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
3443 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3444 struct btrfs_root *root,
3445 struct btrfs_path *path, int ins_len)
3447 struct btrfs_key key;
3448 struct extent_buffer *leaf;
3449 struct btrfs_file_extent_item *fi;
3454 leaf = path->nodes[0];
3455 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3457 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3458 key.type != BTRFS_EXTENT_CSUM_KEY);
3460 if (btrfs_leaf_free_space(leaf) >= ins_len)
3463 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3464 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3465 fi = btrfs_item_ptr(leaf, path->slots[0],
3466 struct btrfs_file_extent_item);
3467 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3469 btrfs_release_path(path);
3471 path->keep_locks = 1;
3472 path->search_for_split = 1;
3473 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3474 path->search_for_split = 0;
3481 leaf = path->nodes[0];
3482 /* if our item isn't there, return now */
3483 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3486 /* the leaf has changed, it now has room. return now */
3487 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
3490 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3491 fi = btrfs_item_ptr(leaf, path->slots[0],
3492 struct btrfs_file_extent_item);
3493 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3497 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3501 path->keep_locks = 0;
3502 btrfs_unlock_up_safe(path, 1);
3505 path->keep_locks = 0;
3509 static noinline int split_item(struct btrfs_path *path,
3510 const struct btrfs_key *new_key,
3511 unsigned long split_offset)
3513 struct extent_buffer *leaf;
3514 struct btrfs_item *item;
3515 struct btrfs_item *new_item;
3521 struct btrfs_disk_key disk_key;
3523 leaf = path->nodes[0];
3524 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
3526 item = btrfs_item_nr(path->slots[0]);
3527 orig_offset = btrfs_item_offset(leaf, item);
3528 item_size = btrfs_item_size(leaf, item);
3530 buf = kmalloc(item_size, GFP_NOFS);
3534 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3535 path->slots[0]), item_size);
3537 slot = path->slots[0] + 1;
3538 nritems = btrfs_header_nritems(leaf);
3539 if (slot != nritems) {
3540 /* shift the items */
3541 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3542 btrfs_item_nr_offset(slot),
3543 (nritems - slot) * sizeof(struct btrfs_item));
3546 btrfs_cpu_key_to_disk(&disk_key, new_key);
3547 btrfs_set_item_key(leaf, &disk_key, slot);
3549 new_item = btrfs_item_nr(slot);
3551 btrfs_set_item_offset(leaf, new_item, orig_offset);
3552 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3554 btrfs_set_item_offset(leaf, item,
3555 orig_offset + item_size - split_offset);
3556 btrfs_set_item_size(leaf, item, split_offset);
3558 btrfs_set_header_nritems(leaf, nritems + 1);
3560 /* write the data for the start of the original item */
3561 write_extent_buffer(leaf, buf,
3562 btrfs_item_ptr_offset(leaf, path->slots[0]),
3565 /* write the data for the new item */
3566 write_extent_buffer(leaf, buf + split_offset,
3567 btrfs_item_ptr_offset(leaf, slot),
3568 item_size - split_offset);
3569 btrfs_mark_buffer_dirty(leaf);
3571 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
3577 * This function splits a single item into two items,
3578 * giving 'new_key' to the new item and splitting the
3579 * old one at split_offset (from the start of the item).
3581 * The path may be released by this operation. After
3582 * the split, the path is pointing to the old item. The
3583 * new item is going to be in the same node as the old one.
3585 * Note, the item being split must be smaller enough to live alone on
3586 * a tree block with room for one extra struct btrfs_item
3588 * This allows us to split the item in place, keeping a lock on the
3589 * leaf the entire time.
3591 int btrfs_split_item(struct btrfs_trans_handle *trans,
3592 struct btrfs_root *root,
3593 struct btrfs_path *path,
3594 const struct btrfs_key *new_key,
3595 unsigned long split_offset)
3598 ret = setup_leaf_for_split(trans, root, path,
3599 sizeof(struct btrfs_item));
3603 ret = split_item(path, new_key, split_offset);
3608 * This function duplicate a item, giving 'new_key' to the new item.
3609 * It guarantees both items live in the same tree leaf and the new item
3610 * is contiguous with the original item.
3612 * This allows us to split file extent in place, keeping a lock on the
3613 * leaf the entire time.
3615 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3616 struct btrfs_root *root,
3617 struct btrfs_path *path,
3618 const struct btrfs_key *new_key)
3620 struct extent_buffer *leaf;
3624 leaf = path->nodes[0];
3625 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3626 ret = setup_leaf_for_split(trans, root, path,
3627 item_size + sizeof(struct btrfs_item));
3632 setup_items_for_insert(root, path, new_key, &item_size, 1);
3633 leaf = path->nodes[0];
3634 memcpy_extent_buffer(leaf,
3635 btrfs_item_ptr_offset(leaf, path->slots[0]),
3636 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3642 * make the item pointed to by the path smaller. new_size indicates
3643 * how small to make it, and from_end tells us if we just chop bytes
3644 * off the end of the item or if we shift the item to chop bytes off
3647 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
3650 struct extent_buffer *leaf;
3651 struct btrfs_item *item;
3653 unsigned int data_end;
3654 unsigned int old_data_start;
3655 unsigned int old_size;
3656 unsigned int size_diff;
3658 struct btrfs_map_token token;
3660 leaf = path->nodes[0];
3661 slot = path->slots[0];
3663 old_size = btrfs_item_size_nr(leaf, slot);
3664 if (old_size == new_size)
3667 nritems = btrfs_header_nritems(leaf);
3668 data_end = leaf_data_end(leaf);
3670 old_data_start = btrfs_item_offset_nr(leaf, slot);
3672 size_diff = old_size - new_size;
3675 BUG_ON(slot >= nritems);
3678 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3680 /* first correct the data pointers */
3681 btrfs_init_map_token(&token, leaf);
3682 for (i = slot; i < nritems; i++) {
3684 item = btrfs_item_nr(i);
3686 ioff = btrfs_token_item_offset(&token, item);
3687 btrfs_set_token_item_offset(&token, item, ioff + size_diff);
3690 /* shift the data */
3692 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3693 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
3694 data_end, old_data_start + new_size - data_end);
3696 struct btrfs_disk_key disk_key;
3699 btrfs_item_key(leaf, &disk_key, slot);
3701 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3703 struct btrfs_file_extent_item *fi;
3705 fi = btrfs_item_ptr(leaf, slot,
3706 struct btrfs_file_extent_item);
3707 fi = (struct btrfs_file_extent_item *)(
3708 (unsigned long)fi - size_diff);
3710 if (btrfs_file_extent_type(leaf, fi) ==
3711 BTRFS_FILE_EXTENT_INLINE) {
3712 ptr = btrfs_item_ptr_offset(leaf, slot);
3713 memmove_extent_buffer(leaf, ptr,
3715 BTRFS_FILE_EXTENT_INLINE_DATA_START);
3719 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3720 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
3721 data_end, old_data_start - data_end);
3723 offset = btrfs_disk_key_offset(&disk_key);
3724 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3725 btrfs_set_item_key(leaf, &disk_key, slot);
3727 fixup_low_keys(path, &disk_key, 1);
3730 item = btrfs_item_nr(slot);
3731 btrfs_set_item_size(leaf, item, new_size);
3732 btrfs_mark_buffer_dirty(leaf);
3734 if (btrfs_leaf_free_space(leaf) < 0) {
3735 btrfs_print_leaf(leaf);
3741 * make the item pointed to by the path bigger, data_size is the added size.
3743 void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
3746 struct extent_buffer *leaf;
3747 struct btrfs_item *item;
3749 unsigned int data_end;
3750 unsigned int old_data;
3751 unsigned int old_size;
3753 struct btrfs_map_token token;
3755 leaf = path->nodes[0];
3757 nritems = btrfs_header_nritems(leaf);
3758 data_end = leaf_data_end(leaf);
3760 if (btrfs_leaf_free_space(leaf) < data_size) {
3761 btrfs_print_leaf(leaf);
3764 slot = path->slots[0];
3765 old_data = btrfs_item_end_nr(leaf, slot);
3768 if (slot >= nritems) {
3769 btrfs_print_leaf(leaf);
3770 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
3776 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3778 /* first correct the data pointers */
3779 btrfs_init_map_token(&token, leaf);
3780 for (i = slot; i < nritems; i++) {
3782 item = btrfs_item_nr(i);
3784 ioff = btrfs_token_item_offset(&token, item);
3785 btrfs_set_token_item_offset(&token, item, ioff - data_size);
3788 /* shift the data */
3789 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3790 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
3791 data_end, old_data - data_end);
3793 data_end = old_data;
3794 old_size = btrfs_item_size_nr(leaf, slot);
3795 item = btrfs_item_nr(slot);
3796 btrfs_set_item_size(leaf, item, old_size + data_size);
3797 btrfs_mark_buffer_dirty(leaf);
3799 if (btrfs_leaf_free_space(leaf) < 0) {
3800 btrfs_print_leaf(leaf);
3806 * setup_items_for_insert - Helper called before inserting one or more items
3807 * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
3808 * in a function that doesn't call btrfs_search_slot
3810 * @root: root we are inserting items to
3811 * @path: points to the leaf/slot where we are going to insert new items
3812 * @cpu_key: array of keys for items to be inserted
3813 * @data_size: size of the body of each item we are going to insert
3814 * @nr: size of @cpu_key/@data_size arrays
3816 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
3817 const struct btrfs_key *cpu_key, u32 *data_size,
3820 struct btrfs_fs_info *fs_info = root->fs_info;
3821 struct btrfs_item *item;
3824 unsigned int data_end;
3825 struct btrfs_disk_key disk_key;
3826 struct extent_buffer *leaf;
3828 struct btrfs_map_token token;
3832 for (i = 0; i < nr; i++)
3833 total_data += data_size[i];
3834 total_size = total_data + (nr * sizeof(struct btrfs_item));
3836 if (path->slots[0] == 0) {
3837 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3838 fixup_low_keys(path, &disk_key, 1);
3840 btrfs_unlock_up_safe(path, 1);
3842 leaf = path->nodes[0];
3843 slot = path->slots[0];
3845 nritems = btrfs_header_nritems(leaf);
3846 data_end = leaf_data_end(leaf);
3848 if (btrfs_leaf_free_space(leaf) < total_size) {
3849 btrfs_print_leaf(leaf);
3850 btrfs_crit(fs_info, "not enough freespace need %u have %d",
3851 total_size, btrfs_leaf_free_space(leaf));
3855 btrfs_init_map_token(&token, leaf);
3856 if (slot != nritems) {
3857 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3859 if (old_data < data_end) {
3860 btrfs_print_leaf(leaf);
3862 "item at slot %d with data offset %u beyond data end of leaf %u",
3863 slot, old_data, data_end);
3867 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3869 /* first correct the data pointers */
3870 for (i = slot; i < nritems; i++) {
3873 item = btrfs_item_nr(i);
3874 ioff = btrfs_token_item_offset(&token, item);
3875 btrfs_set_token_item_offset(&token, item,
3878 /* shift the items */
3879 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3880 btrfs_item_nr_offset(slot),
3881 (nritems - slot) * sizeof(struct btrfs_item));
3883 /* shift the data */
3884 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3885 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
3886 data_end, old_data - data_end);
3887 data_end = old_data;
3890 /* setup the item for the new data */
3891 for (i = 0; i < nr; i++) {
3892 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3893 btrfs_set_item_key(leaf, &disk_key, slot + i);
3894 item = btrfs_item_nr(slot + i);
3895 data_end -= data_size[i];
3896 btrfs_set_token_item_offset(&token, item, data_end);
3897 btrfs_set_token_item_size(&token, item, data_size[i]);
3900 btrfs_set_header_nritems(leaf, nritems + nr);
3901 btrfs_mark_buffer_dirty(leaf);
3903 if (btrfs_leaf_free_space(leaf) < 0) {
3904 btrfs_print_leaf(leaf);
3910 * Given a key and some data, insert items into the tree.
3911 * This does all the path init required, making room in the tree if needed.
3913 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3914 struct btrfs_root *root,
3915 struct btrfs_path *path,
3916 const struct btrfs_key *cpu_key, u32 *data_size,
3925 for (i = 0; i < nr; i++)
3926 total_data += data_size[i];
3928 total_size = total_data + (nr * sizeof(struct btrfs_item));
3929 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3935 slot = path->slots[0];
3938 setup_items_for_insert(root, path, cpu_key, data_size, nr);
3943 * Given a key and some data, insert an item into the tree.
3944 * This does all the path init required, making room in the tree if needed.
3946 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3947 const struct btrfs_key *cpu_key, void *data,
3951 struct btrfs_path *path;
3952 struct extent_buffer *leaf;
3955 path = btrfs_alloc_path();
3958 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3960 leaf = path->nodes[0];
3961 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3962 write_extent_buffer(leaf, data, ptr, data_size);
3963 btrfs_mark_buffer_dirty(leaf);
3965 btrfs_free_path(path);
3970 * delete the pointer from a given node.
3972 * the tree should have been previously balanced so the deletion does not
3975 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
3976 int level, int slot)
3978 struct extent_buffer *parent = path->nodes[level];
3982 nritems = btrfs_header_nritems(parent);
3983 if (slot != nritems - 1) {
3985 ret = btrfs_tree_mod_log_insert_move(parent, slot,
3986 slot + 1, nritems - slot - 1);
3989 memmove_extent_buffer(parent,
3990 btrfs_node_key_ptr_offset(slot),
3991 btrfs_node_key_ptr_offset(slot + 1),
3992 sizeof(struct btrfs_key_ptr) *
3993 (nritems - slot - 1));
3995 ret = btrfs_tree_mod_log_insert_key(parent, slot,
3996 BTRFS_MOD_LOG_KEY_REMOVE, GFP_NOFS);
4001 btrfs_set_header_nritems(parent, nritems);
4002 if (nritems == 0 && parent == root->node) {
4003 BUG_ON(btrfs_header_level(root->node) != 1);
4004 /* just turn the root into a leaf and break */
4005 btrfs_set_header_level(root->node, 0);
4006 } else if (slot == 0) {
4007 struct btrfs_disk_key disk_key;
4009 btrfs_node_key(parent, &disk_key, 0);
4010 fixup_low_keys(path, &disk_key, level + 1);
4012 btrfs_mark_buffer_dirty(parent);
4016 * a helper function to delete the leaf pointed to by path->slots[1] and
4019 * This deletes the pointer in path->nodes[1] and frees the leaf
4020 * block extent. zero is returned if it all worked out, < 0 otherwise.
4022 * The path must have already been setup for deleting the leaf, including
4023 * all the proper balancing. path->nodes[1] must be locked.
4025 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4026 struct btrfs_root *root,
4027 struct btrfs_path *path,
4028 struct extent_buffer *leaf)
4030 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4031 del_ptr(root, path, 1, path->slots[1]);
4034 * btrfs_free_extent is expensive, we want to make sure we
4035 * aren't holding any locks when we call it
4037 btrfs_unlock_up_safe(path, 0);
4039 root_sub_used(root, leaf->len);
4041 atomic_inc(&leaf->refs);
4042 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4043 free_extent_buffer_stale(leaf);
4046 * delete the item at the leaf level in path. If that empties
4047 * the leaf, remove it from the tree
4049 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4050 struct btrfs_path *path, int slot, int nr)
4052 struct btrfs_fs_info *fs_info = root->fs_info;
4053 struct extent_buffer *leaf;
4054 struct btrfs_item *item;
4062 leaf = path->nodes[0];
4063 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4065 for (i = 0; i < nr; i++)
4066 dsize += btrfs_item_size_nr(leaf, slot + i);
4068 nritems = btrfs_header_nritems(leaf);
4070 if (slot + nr != nritems) {
4071 int data_end = leaf_data_end(leaf);
4072 struct btrfs_map_token token;
4074 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4076 BTRFS_LEAF_DATA_OFFSET + data_end,
4077 last_off - data_end);
4079 btrfs_init_map_token(&token, leaf);
4080 for (i = slot + nr; i < nritems; i++) {
4083 item = btrfs_item_nr(i);
4084 ioff = btrfs_token_item_offset(&token, item);
4085 btrfs_set_token_item_offset(&token, item, ioff + dsize);
4088 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4089 btrfs_item_nr_offset(slot + nr),
4090 sizeof(struct btrfs_item) *
4091 (nritems - slot - nr));
4093 btrfs_set_header_nritems(leaf, nritems - nr);
4096 /* delete the leaf if we've emptied it */
4098 if (leaf == root->node) {
4099 btrfs_set_header_level(leaf, 0);
4101 btrfs_clean_tree_block(leaf);
4102 btrfs_del_leaf(trans, root, path, leaf);
4105 int used = leaf_space_used(leaf, 0, nritems);
4107 struct btrfs_disk_key disk_key;
4109 btrfs_item_key(leaf, &disk_key, 0);
4110 fixup_low_keys(path, &disk_key, 1);
4113 /* delete the leaf if it is mostly empty */
4114 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
4115 /* push_leaf_left fixes the path.
4116 * make sure the path still points to our leaf
4117 * for possible call to del_ptr below
4119 slot = path->slots[1];
4120 atomic_inc(&leaf->refs);
4122 wret = push_leaf_left(trans, root, path, 1, 1,
4124 if (wret < 0 && wret != -ENOSPC)
4127 if (path->nodes[0] == leaf &&
4128 btrfs_header_nritems(leaf)) {
4129 wret = push_leaf_right(trans, root, path, 1,
4131 if (wret < 0 && wret != -ENOSPC)
4135 if (btrfs_header_nritems(leaf) == 0) {
4136 path->slots[1] = slot;
4137 btrfs_del_leaf(trans, root, path, leaf);
4138 free_extent_buffer(leaf);
4141 /* if we're still in the path, make sure
4142 * we're dirty. Otherwise, one of the
4143 * push_leaf functions must have already
4144 * dirtied this buffer
4146 if (path->nodes[0] == leaf)
4147 btrfs_mark_buffer_dirty(leaf);
4148 free_extent_buffer(leaf);
4151 btrfs_mark_buffer_dirty(leaf);
4158 * search the tree again to find a leaf with lesser keys
4159 * returns 0 if it found something or 1 if there are no lesser leaves.
4160 * returns < 0 on io errors.
4162 * This may release the path, and so you may lose any locks held at the
4165 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4167 struct btrfs_key key;
4168 struct btrfs_disk_key found_key;
4171 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
4173 if (key.offset > 0) {
4175 } else if (key.type > 0) {
4177 key.offset = (u64)-1;
4178 } else if (key.objectid > 0) {
4181 key.offset = (u64)-1;
4186 btrfs_release_path(path);
4187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4190 btrfs_item_key(path->nodes[0], &found_key, 0);
4191 ret = comp_keys(&found_key, &key);
4193 * We might have had an item with the previous key in the tree right
4194 * before we released our path. And after we released our path, that
4195 * item might have been pushed to the first slot (0) of the leaf we
4196 * were holding due to a tree balance. Alternatively, an item with the
4197 * previous key can exist as the only element of a leaf (big fat item).
4198 * Therefore account for these 2 cases, so that our callers (like
4199 * btrfs_previous_item) don't miss an existing item with a key matching
4200 * the previous key we computed above.
4208 * A helper function to walk down the tree starting at min_key, and looking
4209 * for nodes or leaves that are have a minimum transaction id.
4210 * This is used by the btree defrag code, and tree logging
4212 * This does not cow, but it does stuff the starting key it finds back
4213 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4214 * key and get a writable path.
4216 * This honors path->lowest_level to prevent descent past a given level
4219 * min_trans indicates the oldest transaction that you are interested
4220 * in walking through. Any nodes or leaves older than min_trans are
4221 * skipped over (without reading them).
4223 * returns zero if something useful was found, < 0 on error and 1 if there
4224 * was nothing in the tree that matched the search criteria.
4226 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4227 struct btrfs_path *path,
4230 struct extent_buffer *cur;
4231 struct btrfs_key found_key;
4237 int keep_locks = path->keep_locks;
4239 path->keep_locks = 1;
4241 cur = btrfs_read_lock_root_node(root);
4242 level = btrfs_header_level(cur);
4243 WARN_ON(path->nodes[level]);
4244 path->nodes[level] = cur;
4245 path->locks[level] = BTRFS_READ_LOCK;
4247 if (btrfs_header_generation(cur) < min_trans) {
4252 nritems = btrfs_header_nritems(cur);
4253 level = btrfs_header_level(cur);
4254 sret = btrfs_bin_search(cur, min_key, &slot);
4260 /* at the lowest level, we're done, setup the path and exit */
4261 if (level == path->lowest_level) {
4262 if (slot >= nritems)
4265 path->slots[level] = slot;
4266 btrfs_item_key_to_cpu(cur, &found_key, slot);
4269 if (sret && slot > 0)
4272 * check this node pointer against the min_trans parameters.
4273 * If it is too old, skip to the next one.
4275 while (slot < nritems) {
4278 gen = btrfs_node_ptr_generation(cur, slot);
4279 if (gen < min_trans) {
4287 * we didn't find a candidate key in this node, walk forward
4288 * and find another one
4290 if (slot >= nritems) {
4291 path->slots[level] = slot;
4292 sret = btrfs_find_next_key(root, path, min_key, level,
4295 btrfs_release_path(path);
4301 /* save our key for returning back */
4302 btrfs_node_key_to_cpu(cur, &found_key, slot);
4303 path->slots[level] = slot;
4304 if (level == path->lowest_level) {
4308 cur = btrfs_read_node_slot(cur, slot);
4314 btrfs_tree_read_lock(cur);
4316 path->locks[level - 1] = BTRFS_READ_LOCK;
4317 path->nodes[level - 1] = cur;
4318 unlock_up(path, level, 1, 0, NULL);
4321 path->keep_locks = keep_locks;
4323 btrfs_unlock_up_safe(path, path->lowest_level + 1);
4324 memcpy(min_key, &found_key, sizeof(found_key));
4330 * this is similar to btrfs_next_leaf, but does not try to preserve
4331 * and fixup the path. It looks for and returns the next key in the
4332 * tree based on the current path and the min_trans parameters.
4334 * 0 is returned if another key is found, < 0 if there are any errors
4335 * and 1 is returned if there are no higher keys in the tree
4337 * path->keep_locks should be set to 1 on the search made before
4338 * calling this function.
4340 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4341 struct btrfs_key *key, int level, u64 min_trans)
4344 struct extent_buffer *c;
4346 WARN_ON(!path->keep_locks && !path->skip_locking);
4347 while (level < BTRFS_MAX_LEVEL) {
4348 if (!path->nodes[level])
4351 slot = path->slots[level] + 1;
4352 c = path->nodes[level];
4354 if (slot >= btrfs_header_nritems(c)) {
4357 struct btrfs_key cur_key;
4358 if (level + 1 >= BTRFS_MAX_LEVEL ||
4359 !path->nodes[level + 1])
4362 if (path->locks[level + 1] || path->skip_locking) {
4367 slot = btrfs_header_nritems(c) - 1;
4369 btrfs_item_key_to_cpu(c, &cur_key, slot);
4371 btrfs_node_key_to_cpu(c, &cur_key, slot);
4373 orig_lowest = path->lowest_level;
4374 btrfs_release_path(path);
4375 path->lowest_level = level;
4376 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4378 path->lowest_level = orig_lowest;
4382 c = path->nodes[level];
4383 slot = path->slots[level];
4390 btrfs_item_key_to_cpu(c, key, slot);
4392 u64 gen = btrfs_node_ptr_generation(c, slot);
4394 if (gen < min_trans) {
4398 btrfs_node_key_to_cpu(c, key, slot);
4406 * search the tree again to find a leaf with greater keys
4407 * returns 0 if it found something or 1 if there are no greater leaves.
4408 * returns < 0 on io errors.
4410 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4412 return btrfs_next_old_leaf(root, path, 0);
4415 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
4420 struct extent_buffer *c;
4421 struct extent_buffer *next;
4422 struct btrfs_key key;
4427 nritems = btrfs_header_nritems(path->nodes[0]);
4431 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4435 btrfs_release_path(path);
4437 path->keep_locks = 1;
4440 ret = btrfs_search_old_slot(root, &key, path, time_seq);
4442 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4443 path->keep_locks = 0;
4448 nritems = btrfs_header_nritems(path->nodes[0]);
4450 * by releasing the path above we dropped all our locks. A balance
4451 * could have added more items next to the key that used to be
4452 * at the very end of the block. So, check again here and
4453 * advance the path if there are now more items available.
4455 if (nritems > 0 && path->slots[0] < nritems - 1) {
4462 * So the above check misses one case:
4463 * - after releasing the path above, someone has removed the item that
4464 * used to be at the very end of the block, and balance between leafs
4465 * gets another one with bigger key.offset to replace it.
4467 * This one should be returned as well, or we can get leaf corruption
4468 * later(esp. in __btrfs_drop_extents()).
4470 * And a bit more explanation about this check,
4471 * with ret > 0, the key isn't found, the path points to the slot
4472 * where it should be inserted, so the path->slots[0] item must be the
4475 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
4480 while (level < BTRFS_MAX_LEVEL) {
4481 if (!path->nodes[level]) {
4486 slot = path->slots[level] + 1;
4487 c = path->nodes[level];
4488 if (slot >= btrfs_header_nritems(c)) {
4490 if (level == BTRFS_MAX_LEVEL) {
4499 * Our current level is where we're going to start from, and to
4500 * make sure lockdep doesn't complain we need to drop our locks
4501 * and nodes from 0 to our current level.
4503 for (i = 0; i < level; i++) {
4504 if (path->locks[level]) {
4505 btrfs_tree_read_unlock(path->nodes[i]);
4508 free_extent_buffer(path->nodes[i]);
4509 path->nodes[i] = NULL;
4513 ret = read_block_for_search(root, path, &next, level,
4519 btrfs_release_path(path);
4523 if (!path->skip_locking) {
4524 ret = btrfs_try_tree_read_lock(next);
4525 if (!ret && time_seq) {
4527 * If we don't get the lock, we may be racing
4528 * with push_leaf_left, holding that lock while
4529 * itself waiting for the leaf we've currently
4530 * locked. To solve this situation, we give up
4531 * on our lock and cycle.
4533 free_extent_buffer(next);
4534 btrfs_release_path(path);
4539 btrfs_tree_read_lock(next);
4543 path->slots[level] = slot;
4546 path->nodes[level] = next;
4547 path->slots[level] = 0;
4548 if (!path->skip_locking)
4549 path->locks[level] = BTRFS_READ_LOCK;
4553 ret = read_block_for_search(root, path, &next, level,
4559 btrfs_release_path(path);
4563 if (!path->skip_locking)
4564 btrfs_tree_read_lock(next);
4568 unlock_up(path, 0, 1, 0, NULL);
4574 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4575 * searching until it gets past min_objectid or finds an item of 'type'
4577 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4579 int btrfs_previous_item(struct btrfs_root *root,
4580 struct btrfs_path *path, u64 min_objectid,
4583 struct btrfs_key found_key;
4584 struct extent_buffer *leaf;
4589 if (path->slots[0] == 0) {
4590 ret = btrfs_prev_leaf(root, path);
4596 leaf = path->nodes[0];
4597 nritems = btrfs_header_nritems(leaf);
4600 if (path->slots[0] == nritems)
4603 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4604 if (found_key.objectid < min_objectid)
4606 if (found_key.type == type)
4608 if (found_key.objectid == min_objectid &&
4609 found_key.type < type)
4616 * search in extent tree to find a previous Metadata/Data extent item with
4619 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4621 int btrfs_previous_extent_item(struct btrfs_root *root,
4622 struct btrfs_path *path, u64 min_objectid)
4624 struct btrfs_key found_key;
4625 struct extent_buffer *leaf;
4630 if (path->slots[0] == 0) {
4631 ret = btrfs_prev_leaf(root, path);
4637 leaf = path->nodes[0];
4638 nritems = btrfs_header_nritems(leaf);
4641 if (path->slots[0] == nritems)
4644 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4645 if (found_key.objectid < min_objectid)
4647 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4648 found_key.type == BTRFS_METADATA_ITEM_KEY)
4650 if (found_key.objectid == min_objectid &&
4651 found_key.type < BTRFS_EXTENT_ITEM_KEY)