2 * Copyright (C) 2007,2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/rbtree.h>
24 #include "transaction.h"
25 #include "print-tree.h"
28 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
31 *root, struct btrfs_key *ins_key,
32 struct btrfs_path *path, int data_size, int extend);
33 static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
35 struct extent_buffer *src, int empty);
36 static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
40 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
42 static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
43 struct extent_buffer *eb);
45 struct btrfs_path *btrfs_alloc_path(void)
47 struct btrfs_path *path;
48 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
53 * set all locked nodes in the path to blocking locks. This should
54 * be done before scheduling
56 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
59 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
60 if (!p->nodes[i] || !p->locks[i])
62 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
63 if (p->locks[i] == BTRFS_READ_LOCK)
64 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
65 else if (p->locks[i] == BTRFS_WRITE_LOCK)
66 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
71 * reset all the locked nodes in the patch to spinning locks.
73 * held is used to keep lockdep happy, when lockdep is enabled
74 * we set held to a blocking lock before we go around and
75 * retake all the spinlocks in the path. You can safely use NULL
78 noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
79 struct extent_buffer *held, int held_rw)
83 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84 /* lockdep really cares that we take all of these spinlocks
85 * in the right order. If any of the locks in the path are not
86 * currently blocking, it is going to complain. So, make really
87 * really sure by forcing the path to blocking before we clear
91 btrfs_set_lock_blocking_rw(held, held_rw);
92 if (held_rw == BTRFS_WRITE_LOCK)
93 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
94 else if (held_rw == BTRFS_READ_LOCK)
95 held_rw = BTRFS_READ_LOCK_BLOCKING;
97 btrfs_set_path_blocking(p);
100 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
101 if (p->nodes[i] && p->locks[i]) {
102 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
103 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
104 p->locks[i] = BTRFS_WRITE_LOCK;
105 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
106 p->locks[i] = BTRFS_READ_LOCK;
110 #ifdef CONFIG_DEBUG_LOCK_ALLOC
112 btrfs_clear_lock_blocking_rw(held, held_rw);
116 /* this also releases the path */
117 void btrfs_free_path(struct btrfs_path *p)
121 btrfs_release_path(p);
122 kmem_cache_free(btrfs_path_cachep, p);
126 * path release drops references on the extent buffers in the path
127 * and it drops any locks held by this path
129 * It is safe to call this on paths that no locks or extent buffers held.
131 noinline void btrfs_release_path(struct btrfs_path *p)
135 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
140 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
143 free_extent_buffer(p->nodes[i]);
149 * safely gets a reference on the root node of a tree. A lock
150 * is not taken, so a concurrent writer may put a different node
151 * at the root of the tree. See btrfs_lock_root_node for the
154 * The extent buffer returned by this has a reference taken, so
155 * it won't disappear. It may stop being the root of the tree
156 * at any time because there are no locks held.
158 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
160 struct extent_buffer *eb;
164 eb = rcu_dereference(root->node);
167 * RCU really hurts here, we could free up the root node because
168 * it was cow'ed but we may not get the new root node yet so do
169 * the inc_not_zero dance and if it doesn't work then
170 * synchronize_rcu and try again.
172 if (atomic_inc_not_zero(&eb->refs)) {
182 /* loop around taking references on and locking the root node of the
183 * tree until you end up with a lock on the root. A locked buffer
184 * is returned, with a reference held.
186 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
188 struct extent_buffer *eb;
191 eb = btrfs_root_node(root);
193 if (eb == root->node)
195 btrfs_tree_unlock(eb);
196 free_extent_buffer(eb);
201 /* loop around taking references on and locking the root node of the
202 * tree until you end up with a lock on the root. A locked buffer
203 * is returned, with a reference held.
205 static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
207 struct extent_buffer *eb;
210 eb = btrfs_root_node(root);
211 btrfs_tree_read_lock(eb);
212 if (eb == root->node)
214 btrfs_tree_read_unlock(eb);
215 free_extent_buffer(eb);
220 /* cowonly root (everything not a reference counted cow subvolume), just get
221 * put onto a simple dirty list. transaction.c walks this to make sure they
222 * get properly updated on disk.
224 static void add_root_to_dirty_list(struct btrfs_root *root)
226 spin_lock(&root->fs_info->trans_lock);
227 if (test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state) &&
228 list_empty(&root->dirty_list)) {
229 list_add(&root->dirty_list,
230 &root->fs_info->dirty_cowonly_roots);
232 spin_unlock(&root->fs_info->trans_lock);
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
240 int btrfs_copy_root(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *buf,
243 struct extent_buffer **cow_ret, u64 new_root_objectid)
245 struct extent_buffer *cow;
248 struct btrfs_disk_key disk_key;
250 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
251 trans->transid != root->fs_info->running_transaction->transid);
252 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
253 trans->transid != root->last_trans);
255 level = btrfs_header_level(buf);
257 btrfs_item_key(buf, &disk_key, 0);
259 btrfs_node_key(buf, &disk_key, 0);
261 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
262 new_root_objectid, &disk_key, level,
267 copy_extent_buffer(cow, buf, 0, 0, cow->len);
268 btrfs_set_header_bytenr(cow, cow->start);
269 btrfs_set_header_generation(cow, trans->transid);
270 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
271 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
272 BTRFS_HEADER_FLAG_RELOC);
273 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
274 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
276 btrfs_set_header_owner(cow, new_root_objectid);
278 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
281 WARN_ON(btrfs_header_generation(buf) > trans->transid);
282 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
283 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
285 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
290 btrfs_mark_buffer_dirty(cow);
299 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
300 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
302 MOD_LOG_ROOT_REPLACE,
305 struct tree_mod_move {
310 struct tree_mod_root {
315 struct tree_mod_elem {
317 u64 index; /* shifted logical */
321 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
324 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
327 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
328 struct btrfs_disk_key key;
331 /* this is used for op == MOD_LOG_MOVE_KEYS */
332 struct tree_mod_move move;
334 /* this is used for op == MOD_LOG_ROOT_REPLACE */
335 struct tree_mod_root old_root;
338 static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
340 read_lock(&fs_info->tree_mod_log_lock);
343 static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
345 read_unlock(&fs_info->tree_mod_log_lock);
348 static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
350 write_lock(&fs_info->tree_mod_log_lock);
353 static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
355 write_unlock(&fs_info->tree_mod_log_lock);
359 * Pull a new tree mod seq number for our operation.
361 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
363 return atomic64_inc_return(&fs_info->tree_mod_seq);
367 * This adds a new blocker to the tree mod log's blocker list if the @elem
368 * passed does not already have a sequence number set. So when a caller expects
369 * to record tree modifications, it should ensure to set elem->seq to zero
370 * before calling btrfs_get_tree_mod_seq.
371 * Returns a fresh, unused tree log modification sequence number, even if no new
374 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
375 struct seq_list *elem)
377 tree_mod_log_write_lock(fs_info);
378 spin_lock(&fs_info->tree_mod_seq_lock);
380 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
381 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
383 spin_unlock(&fs_info->tree_mod_seq_lock);
384 tree_mod_log_write_unlock(fs_info);
389 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
390 struct seq_list *elem)
392 struct rb_root *tm_root;
393 struct rb_node *node;
394 struct rb_node *next;
395 struct seq_list *cur_elem;
396 struct tree_mod_elem *tm;
397 u64 min_seq = (u64)-1;
398 u64 seq_putting = elem->seq;
403 spin_lock(&fs_info->tree_mod_seq_lock);
404 list_del(&elem->list);
407 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
408 if (cur_elem->seq < min_seq) {
409 if (seq_putting > cur_elem->seq) {
411 * blocker with lower sequence number exists, we
412 * cannot remove anything from the log
414 spin_unlock(&fs_info->tree_mod_seq_lock);
417 min_seq = cur_elem->seq;
420 spin_unlock(&fs_info->tree_mod_seq_lock);
423 * anything that's lower than the lowest existing (read: blocked)
424 * sequence number can be removed from the tree.
426 tree_mod_log_write_lock(fs_info);
427 tm_root = &fs_info->tree_mod_log;
428 for (node = rb_first(tm_root); node; node = next) {
429 next = rb_next(node);
430 tm = container_of(node, struct tree_mod_elem, node);
431 if (tm->seq > min_seq)
433 rb_erase(node, tm_root);
436 tree_mod_log_write_unlock(fs_info);
440 * key order of the log:
443 * the index is the shifted logical of the *new* root node for root replace
444 * operations, or the shifted logical of the affected block for all other
447 * Note: must be called with write lock (tree_mod_log_write_lock).
450 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
452 struct rb_root *tm_root;
453 struct rb_node **new;
454 struct rb_node *parent = NULL;
455 struct tree_mod_elem *cur;
459 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
461 tm_root = &fs_info->tree_mod_log;
462 new = &tm_root->rb_node;
464 cur = container_of(*new, struct tree_mod_elem, node);
466 if (cur->index < tm->index)
467 new = &((*new)->rb_left);
468 else if (cur->index > tm->index)
469 new = &((*new)->rb_right);
470 else if (cur->seq < tm->seq)
471 new = &((*new)->rb_left);
472 else if (cur->seq > tm->seq)
473 new = &((*new)->rb_right);
478 rb_link_node(&tm->node, parent, new);
479 rb_insert_color(&tm->node, tm_root);
484 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
485 * returns zero with the tree_mod_log_lock acquired. The caller must hold
486 * this until all tree mod log insertions are recorded in the rb tree and then
487 * call tree_mod_log_write_unlock() to release.
489 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
490 struct extent_buffer *eb) {
492 if (list_empty(&(fs_info)->tree_mod_seq_list))
494 if (eb && btrfs_header_level(eb) == 0)
497 tree_mod_log_write_lock(fs_info);
498 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
499 tree_mod_log_write_unlock(fs_info);
506 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
507 static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
508 struct extent_buffer *eb)
511 if (list_empty(&(fs_info)->tree_mod_seq_list))
513 if (eb && btrfs_header_level(eb) == 0)
519 static struct tree_mod_elem *
520 alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
521 enum mod_log_op op, gfp_t flags)
523 struct tree_mod_elem *tm;
525 tm = kzalloc(sizeof(*tm), flags);
529 tm->index = eb->start >> PAGE_CACHE_SHIFT;
530 if (op != MOD_LOG_KEY_ADD) {
531 btrfs_node_key(eb, &tm->key, slot);
532 tm->blockptr = btrfs_node_blockptr(eb, slot);
536 tm->generation = btrfs_node_ptr_generation(eb, slot);
537 RB_CLEAR_NODE(&tm->node);
543 tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
544 struct extent_buffer *eb, int slot,
545 enum mod_log_op op, gfp_t flags)
547 struct tree_mod_elem *tm;
550 if (!tree_mod_need_log(fs_info, eb))
553 tm = alloc_tree_mod_elem(eb, slot, op, flags);
557 if (tree_mod_dont_log(fs_info, eb)) {
562 ret = __tree_mod_log_insert(fs_info, tm);
563 tree_mod_log_write_unlock(fs_info);
571 tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
572 struct extent_buffer *eb, int dst_slot, int src_slot,
573 int nr_items, gfp_t flags)
575 struct tree_mod_elem *tm = NULL;
576 struct tree_mod_elem **tm_list = NULL;
581 if (!tree_mod_need_log(fs_info, eb))
584 tm_list = kzalloc(nr_items * sizeof(struct tree_mod_elem *), flags);
588 tm = kzalloc(sizeof(*tm), flags);
594 tm->index = eb->start >> PAGE_CACHE_SHIFT;
596 tm->move.dst_slot = dst_slot;
597 tm->move.nr_items = nr_items;
598 tm->op = MOD_LOG_MOVE_KEYS;
600 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
601 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
602 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
609 if (tree_mod_dont_log(fs_info, eb))
614 * When we override something during the move, we log these removals.
615 * This can only happen when we move towards the beginning of the
616 * buffer, i.e. dst_slot < src_slot.
618 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
619 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
624 ret = __tree_mod_log_insert(fs_info, tm);
627 tree_mod_log_write_unlock(fs_info);
632 for (i = 0; i < nr_items; i++) {
633 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
634 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
638 tree_mod_log_write_unlock(fs_info);
646 __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
647 struct tree_mod_elem **tm_list,
653 for (i = nritems - 1; i >= 0; i--) {
654 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
656 for (j = nritems - 1; j > i; j--)
657 rb_erase(&tm_list[j]->node,
658 &fs_info->tree_mod_log);
667 tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
668 struct extent_buffer *old_root,
669 struct extent_buffer *new_root, gfp_t flags,
672 struct tree_mod_elem *tm = NULL;
673 struct tree_mod_elem **tm_list = NULL;
678 if (!tree_mod_need_log(fs_info, NULL))
681 if (log_removal && btrfs_header_level(old_root) > 0) {
682 nritems = btrfs_header_nritems(old_root);
683 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
689 for (i = 0; i < nritems; i++) {
690 tm_list[i] = alloc_tree_mod_elem(old_root, i,
691 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
699 tm = kzalloc(sizeof(*tm), flags);
705 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
706 tm->old_root.logical = old_root->start;
707 tm->old_root.level = btrfs_header_level(old_root);
708 tm->generation = btrfs_header_generation(old_root);
709 tm->op = MOD_LOG_ROOT_REPLACE;
711 if (tree_mod_dont_log(fs_info, NULL))
715 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
717 ret = __tree_mod_log_insert(fs_info, tm);
719 tree_mod_log_write_unlock(fs_info);
728 for (i = 0; i < nritems; i++)
737 static struct tree_mod_elem *
738 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
741 struct rb_root *tm_root;
742 struct rb_node *node;
743 struct tree_mod_elem *cur = NULL;
744 struct tree_mod_elem *found = NULL;
745 u64 index = start >> PAGE_CACHE_SHIFT;
747 tree_mod_log_read_lock(fs_info);
748 tm_root = &fs_info->tree_mod_log;
749 node = tm_root->rb_node;
751 cur = container_of(node, struct tree_mod_elem, node);
752 if (cur->index < index) {
753 node = node->rb_left;
754 } else if (cur->index > index) {
755 node = node->rb_right;
756 } else if (cur->seq < min_seq) {
757 node = node->rb_left;
758 } else if (!smallest) {
759 /* we want the node with the highest seq */
761 BUG_ON(found->seq > cur->seq);
763 node = node->rb_left;
764 } else if (cur->seq > min_seq) {
765 /* we want the node with the smallest seq */
767 BUG_ON(found->seq < cur->seq);
769 node = node->rb_right;
775 tree_mod_log_read_unlock(fs_info);
781 * this returns the element from the log with the smallest time sequence
782 * value that's in the log (the oldest log item). any element with a time
783 * sequence lower than min_seq will be ignored.
785 static struct tree_mod_elem *
786 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
789 return __tree_mod_log_search(fs_info, start, min_seq, 1);
793 * this returns the element from the log with the largest time sequence
794 * value that's in the log (the most recent log item). any element with
795 * a time sequence lower than min_seq will be ignored.
797 static struct tree_mod_elem *
798 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
800 return __tree_mod_log_search(fs_info, start, min_seq, 0);
804 tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
805 struct extent_buffer *src, unsigned long dst_offset,
806 unsigned long src_offset, int nr_items)
809 struct tree_mod_elem **tm_list = NULL;
810 struct tree_mod_elem **tm_list_add, **tm_list_rem;
814 if (!tree_mod_need_log(fs_info, NULL))
817 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
820 tm_list = kzalloc(nr_items * 2 * sizeof(struct tree_mod_elem *),
825 tm_list_add = tm_list;
826 tm_list_rem = tm_list + nr_items;
827 for (i = 0; i < nr_items; i++) {
828 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
829 MOD_LOG_KEY_REMOVE, GFP_NOFS);
830 if (!tm_list_rem[i]) {
835 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
836 MOD_LOG_KEY_ADD, GFP_NOFS);
837 if (!tm_list_add[i]) {
843 if (tree_mod_dont_log(fs_info, NULL))
847 for (i = 0; i < nr_items; i++) {
848 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
851 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
856 tree_mod_log_write_unlock(fs_info);
862 for (i = 0; i < nr_items * 2; i++) {
863 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
864 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
868 tree_mod_log_write_unlock(fs_info);
875 tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
876 int dst_offset, int src_offset, int nr_items)
879 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
885 tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
886 struct extent_buffer *eb, int slot, int atomic)
890 ret = tree_mod_log_insert_key(fs_info, eb, slot,
892 atomic ? GFP_ATOMIC : GFP_NOFS);
897 tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
899 struct tree_mod_elem **tm_list = NULL;
904 if (btrfs_header_level(eb) == 0)
907 if (!tree_mod_need_log(fs_info, NULL))
910 nritems = btrfs_header_nritems(eb);
911 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
916 for (i = 0; i < nritems; i++) {
917 tm_list[i] = alloc_tree_mod_elem(eb, i,
918 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
925 if (tree_mod_dont_log(fs_info, eb))
928 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
929 tree_mod_log_write_unlock(fs_info);
937 for (i = 0; i < nritems; i++)
945 tree_mod_log_set_root_pointer(struct btrfs_root *root,
946 struct extent_buffer *new_root_node,
950 ret = tree_mod_log_insert_root(root->fs_info, root->node,
951 new_root_node, GFP_NOFS, log_removal);
956 * check if the tree block can be shared by multiple trees
958 int btrfs_block_can_be_shared(struct btrfs_root *root,
959 struct extent_buffer *buf)
962 * Tree blocks not in refernece counted trees and tree roots
963 * are never shared. If a block was allocated after the last
964 * snapshot and the block was not allocated by tree relocation,
965 * we know the block is not shared.
967 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
968 buf != root->node && buf != root->commit_root &&
969 (btrfs_header_generation(buf) <=
970 btrfs_root_last_snapshot(&root->root_item) ||
971 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
973 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
974 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
975 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
981 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
982 struct btrfs_root *root,
983 struct extent_buffer *buf,
984 struct extent_buffer *cow,
994 * Backrefs update rules:
996 * Always use full backrefs for extent pointers in tree block
997 * allocated by tree relocation.
999 * If a shared tree block is no longer referenced by its owner
1000 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
1001 * use full backrefs for extent pointers in tree block.
1003 * If a tree block is been relocating
1004 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
1005 * use full backrefs for extent pointers in tree block.
1006 * The reason for this is some operations (such as drop tree)
1007 * are only allowed for blocks use full backrefs.
1010 if (btrfs_block_can_be_shared(root, buf)) {
1011 ret = btrfs_lookup_extent_info(trans, root, buf->start,
1012 btrfs_header_level(buf), 1,
1018 btrfs_std_error(root->fs_info, ret);
1023 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1024 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1025 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1030 owner = btrfs_header_owner(buf);
1031 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1032 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1035 if ((owner == root->root_key.objectid ||
1036 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1037 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
1038 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
1039 BUG_ON(ret); /* -ENOMEM */
1041 if (root->root_key.objectid ==
1042 BTRFS_TREE_RELOC_OBJECTID) {
1043 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
1044 BUG_ON(ret); /* -ENOMEM */
1045 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
1046 BUG_ON(ret); /* -ENOMEM */
1048 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1051 if (root->root_key.objectid ==
1052 BTRFS_TREE_RELOC_OBJECTID)
1053 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
1055 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
1056 BUG_ON(ret); /* -ENOMEM */
1058 if (new_flags != 0) {
1059 int level = btrfs_header_level(buf);
1061 ret = btrfs_set_disk_extent_flags(trans, root,
1064 new_flags, level, 0);
1069 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1070 if (root->root_key.objectid ==
1071 BTRFS_TREE_RELOC_OBJECTID)
1072 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
1074 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
1075 BUG_ON(ret); /* -ENOMEM */
1076 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
1077 BUG_ON(ret); /* -ENOMEM */
1079 clean_tree_block(trans, root, buf);
1086 * does the dirty work in cow of a single block. The parent block (if
1087 * supplied) is updated to point to the new cow copy. The new buffer is marked
1088 * dirty and returned locked. If you modify the block it needs to be marked
1091 * search_start -- an allocation hint for the new block
1093 * empty_size -- a hint that you plan on doing more cow. This is the size in
1094 * bytes the allocator should try to find free next to the block it returns.
1095 * This is just a hint and may be ignored by the allocator.
1097 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1098 struct btrfs_root *root,
1099 struct extent_buffer *buf,
1100 struct extent_buffer *parent, int parent_slot,
1101 struct extent_buffer **cow_ret,
1102 u64 search_start, u64 empty_size)
1104 struct btrfs_disk_key disk_key;
1105 struct extent_buffer *cow;
1108 int unlock_orig = 0;
1111 if (*cow_ret == buf)
1114 btrfs_assert_tree_locked(buf);
1116 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1117 trans->transid != root->fs_info->running_transaction->transid);
1118 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1119 trans->transid != root->last_trans);
1121 level = btrfs_header_level(buf);
1124 btrfs_item_key(buf, &disk_key, 0);
1126 btrfs_node_key(buf, &disk_key, 0);
1128 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1130 parent_start = parent->start;
1136 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1137 root->root_key.objectid, &disk_key,
1138 level, search_start, empty_size);
1140 return PTR_ERR(cow);
1142 /* cow is set to blocking by btrfs_init_new_buffer */
1144 copy_extent_buffer(cow, buf, 0, 0, cow->len);
1145 btrfs_set_header_bytenr(cow, cow->start);
1146 btrfs_set_header_generation(cow, trans->transid);
1147 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1148 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1149 BTRFS_HEADER_FLAG_RELOC);
1150 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1151 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1153 btrfs_set_header_owner(cow, root->root_key.objectid);
1155 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
1158 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1160 btrfs_abort_transaction(trans, root, ret);
1164 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
1165 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1170 if (buf == root->node) {
1171 WARN_ON(parent && parent != buf);
1172 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1173 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1174 parent_start = buf->start;
1178 extent_buffer_get(cow);
1179 tree_mod_log_set_root_pointer(root, cow, 1);
1180 rcu_assign_pointer(root->node, cow);
1182 btrfs_free_tree_block(trans, root, buf, parent_start,
1184 free_extent_buffer(buf);
1185 add_root_to_dirty_list(root);
1187 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1188 parent_start = parent->start;
1192 WARN_ON(trans->transid != btrfs_header_generation(parent));
1193 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1194 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1195 btrfs_set_node_blockptr(parent, parent_slot,
1197 btrfs_set_node_ptr_generation(parent, parent_slot,
1199 btrfs_mark_buffer_dirty(parent);
1201 ret = tree_mod_log_free_eb(root->fs_info, buf);
1203 btrfs_abort_transaction(trans, root, ret);
1207 btrfs_free_tree_block(trans, root, buf, parent_start,
1211 btrfs_tree_unlock(buf);
1212 free_extent_buffer_stale(buf);
1213 btrfs_mark_buffer_dirty(cow);
1219 * returns the logical address of the oldest predecessor of the given root.
1220 * entries older than time_seq are ignored.
1222 static struct tree_mod_elem *
1223 __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
1224 struct extent_buffer *eb_root, u64 time_seq)
1226 struct tree_mod_elem *tm;
1227 struct tree_mod_elem *found = NULL;
1228 u64 root_logical = eb_root->start;
1235 * the very last operation that's logged for a root is the replacement
1236 * operation (if it is replaced at all). this has the index of the *new*
1237 * root, making it the very first operation that's logged for this root.
1240 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1245 * if there are no tree operation for the oldest root, we simply
1246 * return it. this should only happen if that (old) root is at
1253 * if there's an operation that's not a root replacement, we
1254 * found the oldest version of our root. normally, we'll find a
1255 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1257 if (tm->op != MOD_LOG_ROOT_REPLACE)
1261 root_logical = tm->old_root.logical;
1265 /* if there's no old root to return, return what we found instead */
1273 * tm is a pointer to the first operation to rewind within eb. then, all
1274 * previous operations will be rewinded (until we reach something older than
1278 __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1279 u64 time_seq, struct tree_mod_elem *first_tm)
1282 struct rb_node *next;
1283 struct tree_mod_elem *tm = first_tm;
1284 unsigned long o_dst;
1285 unsigned long o_src;
1286 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1288 n = btrfs_header_nritems(eb);
1289 tree_mod_log_read_lock(fs_info);
1290 while (tm && tm->seq >= time_seq) {
1292 * all the operations are recorded with the operator used for
1293 * the modification. as we're going backwards, we do the
1294 * opposite of each operation here.
1297 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1298 BUG_ON(tm->slot < n);
1300 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1301 case MOD_LOG_KEY_REMOVE:
1302 btrfs_set_node_key(eb, &tm->key, tm->slot);
1303 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1304 btrfs_set_node_ptr_generation(eb, tm->slot,
1308 case MOD_LOG_KEY_REPLACE:
1309 BUG_ON(tm->slot >= n);
1310 btrfs_set_node_key(eb, &tm->key, tm->slot);
1311 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1312 btrfs_set_node_ptr_generation(eb, tm->slot,
1315 case MOD_LOG_KEY_ADD:
1316 /* if a move operation is needed it's in the log */
1319 case MOD_LOG_MOVE_KEYS:
1320 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1321 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1322 memmove_extent_buffer(eb, o_dst, o_src,
1323 tm->move.nr_items * p_size);
1325 case MOD_LOG_ROOT_REPLACE:
1327 * this operation is special. for roots, this must be
1328 * handled explicitly before rewinding.
1329 * for non-roots, this operation may exist if the node
1330 * was a root: root A -> child B; then A gets empty and
1331 * B is promoted to the new root. in the mod log, we'll
1332 * have a root-replace operation for B, a tree block
1333 * that is no root. we simply ignore that operation.
1337 next = rb_next(&tm->node);
1340 tm = container_of(next, struct tree_mod_elem, node);
1341 if (tm->index != first_tm->index)
1344 tree_mod_log_read_unlock(fs_info);
1345 btrfs_set_header_nritems(eb, n);
1349 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1350 * is returned. If rewind operations happen, a fresh buffer is returned. The
1351 * returned buffer is always read-locked. If the returned buffer is not the
1352 * input buffer, the lock on the input buffer is released and the input buffer
1353 * is freed (its refcount is decremented).
1355 static struct extent_buffer *
1356 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1357 struct extent_buffer *eb, u64 time_seq)
1359 struct extent_buffer *eb_rewin;
1360 struct tree_mod_elem *tm;
1365 if (btrfs_header_level(eb) == 0)
1368 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1372 btrfs_set_path_blocking(path);
1373 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1375 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1376 BUG_ON(tm->slot != 0);
1377 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1378 fs_info->tree_root->nodesize);
1380 btrfs_tree_read_unlock_blocking(eb);
1381 free_extent_buffer(eb);
1384 btrfs_set_header_bytenr(eb_rewin, eb->start);
1385 btrfs_set_header_backref_rev(eb_rewin,
1386 btrfs_header_backref_rev(eb));
1387 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1388 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1390 eb_rewin = btrfs_clone_extent_buffer(eb);
1392 btrfs_tree_read_unlock_blocking(eb);
1393 free_extent_buffer(eb);
1398 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1399 btrfs_tree_read_unlock_blocking(eb);
1400 free_extent_buffer(eb);
1402 extent_buffer_get(eb_rewin);
1403 btrfs_tree_read_lock(eb_rewin);
1404 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
1405 WARN_ON(btrfs_header_nritems(eb_rewin) >
1406 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
1412 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1413 * value. If there are no changes, the current root->root_node is returned. If
1414 * anything changed in between, there's a fresh buffer allocated on which the
1415 * rewind operations are done. In any case, the returned buffer is read locked.
1416 * Returns NULL on error (with no locks held).
1418 static inline struct extent_buffer *
1419 get_old_root(struct btrfs_root *root, u64 time_seq)
1421 struct tree_mod_elem *tm;
1422 struct extent_buffer *eb = NULL;
1423 struct extent_buffer *eb_root;
1424 struct extent_buffer *old;
1425 struct tree_mod_root *old_root = NULL;
1426 u64 old_generation = 0;
1430 eb_root = btrfs_read_lock_root_node(root);
1431 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
1435 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1436 old_root = &tm->old_root;
1437 old_generation = tm->generation;
1438 logical = old_root->logical;
1440 logical = eb_root->start;
1443 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
1444 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1445 btrfs_tree_read_unlock(eb_root);
1446 free_extent_buffer(eb_root);
1447 blocksize = btrfs_level_size(root, old_root->level);
1448 old = read_tree_block(root, logical, blocksize, 0);
1449 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
1450 free_extent_buffer(old);
1451 btrfs_warn(root->fs_info,
1452 "failed to read tree block %llu from get_old_root", logical);
1454 eb = btrfs_clone_extent_buffer(old);
1455 free_extent_buffer(old);
1457 } else if (old_root) {
1458 btrfs_tree_read_unlock(eb_root);
1459 free_extent_buffer(eb_root);
1460 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
1462 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
1463 eb = btrfs_clone_extent_buffer(eb_root);
1464 btrfs_tree_read_unlock_blocking(eb_root);
1465 free_extent_buffer(eb_root);
1470 extent_buffer_get(eb);
1471 btrfs_tree_read_lock(eb);
1473 btrfs_set_header_bytenr(eb, eb->start);
1474 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1475 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
1476 btrfs_set_header_level(eb, old_root->level);
1477 btrfs_set_header_generation(eb, old_generation);
1480 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
1482 WARN_ON(btrfs_header_level(eb) != 0);
1483 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
1488 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1490 struct tree_mod_elem *tm;
1492 struct extent_buffer *eb_root = btrfs_root_node(root);
1494 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
1495 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1496 level = tm->old_root.level;
1498 level = btrfs_header_level(eb_root);
1500 free_extent_buffer(eb_root);
1505 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1506 struct btrfs_root *root,
1507 struct extent_buffer *buf)
1509 /* ensure we can see the force_cow */
1513 * We do not need to cow a block if
1514 * 1) this block is not created or changed in this transaction;
1515 * 2) this block does not belong to TREE_RELOC tree;
1516 * 3) the root is not forced COW.
1518 * What is forced COW:
1519 * when we create snapshot during commiting the transaction,
1520 * after we've finished coping src root, we must COW the shared
1521 * block to ensure the metadata consistency.
1523 if (btrfs_header_generation(buf) == trans->transid &&
1524 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1525 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1526 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1527 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
1533 * cows a single block, see __btrfs_cow_block for the real work.
1534 * This version of it has extra checks so that a block isn't cow'd more than
1535 * once per transaction, as long as it hasn't been written yet
1537 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *root, struct extent_buffer *buf,
1539 struct extent_buffer *parent, int parent_slot,
1540 struct extent_buffer **cow_ret)
1545 if (trans->transaction != root->fs_info->running_transaction)
1546 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1548 root->fs_info->running_transaction->transid);
1550 if (trans->transid != root->fs_info->generation)
1551 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1552 trans->transid, root->fs_info->generation);
1554 if (!should_cow_block(trans, root, buf)) {
1559 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
1562 btrfs_set_lock_blocking(parent);
1563 btrfs_set_lock_blocking(buf);
1565 ret = __btrfs_cow_block(trans, root, buf, parent,
1566 parent_slot, cow_ret, search_start, 0);
1568 trace_btrfs_cow_block(root, buf, *cow_ret);
1574 * helper function for defrag to decide if two blocks pointed to by a
1575 * node are actually close by
1577 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1579 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1581 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1587 * compare two keys in a memcmp fashion
1589 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1591 struct btrfs_key k1;
1593 btrfs_disk_key_to_cpu(&k1, disk);
1595 return btrfs_comp_cpu_keys(&k1, k2);
1599 * same as comp_keys only with two btrfs_key's
1601 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
1603 if (k1->objectid > k2->objectid)
1605 if (k1->objectid < k2->objectid)
1607 if (k1->type > k2->type)
1609 if (k1->type < k2->type)
1611 if (k1->offset > k2->offset)
1613 if (k1->offset < k2->offset)
1619 * this is used by the defrag code to go through all the
1620 * leaves pointed to by a node and reallocate them so that
1621 * disk order is close to key order
1623 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1624 struct btrfs_root *root, struct extent_buffer *parent,
1625 int start_slot, u64 *last_ret,
1626 struct btrfs_key *progress)
1628 struct extent_buffer *cur;
1631 u64 search_start = *last_ret;
1641 int progress_passed = 0;
1642 struct btrfs_disk_key disk_key;
1644 parent_level = btrfs_header_level(parent);
1646 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1647 WARN_ON(trans->transid != root->fs_info->generation);
1649 parent_nritems = btrfs_header_nritems(parent);
1650 blocksize = btrfs_level_size(root, parent_level - 1);
1651 end_slot = parent_nritems;
1653 if (parent_nritems == 1)
1656 btrfs_set_lock_blocking(parent);
1658 for (i = start_slot; i < end_slot; i++) {
1661 btrfs_node_key(parent, &disk_key, i);
1662 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1665 progress_passed = 1;
1666 blocknr = btrfs_node_blockptr(parent, i);
1667 gen = btrfs_node_ptr_generation(parent, i);
1668 if (last_block == 0)
1669 last_block = blocknr;
1672 other = btrfs_node_blockptr(parent, i - 1);
1673 close = close_blocks(blocknr, other, blocksize);
1675 if (!close && i < end_slot - 2) {
1676 other = btrfs_node_blockptr(parent, i + 1);
1677 close = close_blocks(blocknr, other, blocksize);
1680 last_block = blocknr;
1684 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1686 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1689 if (!cur || !uptodate) {
1691 cur = read_tree_block(root, blocknr,
1693 if (!cur || !extent_buffer_uptodate(cur)) {
1694 free_extent_buffer(cur);
1697 } else if (!uptodate) {
1698 err = btrfs_read_buffer(cur, gen);
1700 free_extent_buffer(cur);
1705 if (search_start == 0)
1706 search_start = last_block;
1708 btrfs_tree_lock(cur);
1709 btrfs_set_lock_blocking(cur);
1710 err = __btrfs_cow_block(trans, root, cur, parent, i,
1713 (end_slot - i) * blocksize));
1715 btrfs_tree_unlock(cur);
1716 free_extent_buffer(cur);
1719 search_start = cur->start;
1720 last_block = cur->start;
1721 *last_ret = search_start;
1722 btrfs_tree_unlock(cur);
1723 free_extent_buffer(cur);
1729 * The leaf data grows from end-to-front in the node.
1730 * this returns the address of the start of the last item,
1731 * which is the stop of the leaf data stack
1733 static inline unsigned int leaf_data_end(struct btrfs_root *root,
1734 struct extent_buffer *leaf)
1736 u32 nr = btrfs_header_nritems(leaf);
1738 return BTRFS_LEAF_DATA_SIZE(root);
1739 return btrfs_item_offset_nr(leaf, nr - 1);
1744 * search for key in the extent_buffer. The items start at offset p,
1745 * and they are item_size apart. There are 'max' items in p.
1747 * the slot in the array is returned via slot, and it points to
1748 * the place where you would insert key if it is not found in
1751 * slot may point to max if the key is bigger than all of the keys
1753 static noinline int generic_bin_search(struct extent_buffer *eb,
1755 int item_size, struct btrfs_key *key,
1762 struct btrfs_disk_key *tmp = NULL;
1763 struct btrfs_disk_key unaligned;
1764 unsigned long offset;
1766 unsigned long map_start = 0;
1767 unsigned long map_len = 0;
1770 while (low < high) {
1771 mid = (low + high) / 2;
1772 offset = p + mid * item_size;
1774 if (!kaddr || offset < map_start ||
1775 (offset + sizeof(struct btrfs_disk_key)) >
1776 map_start + map_len) {
1778 err = map_private_extent_buffer(eb, offset,
1779 sizeof(struct btrfs_disk_key),
1780 &kaddr, &map_start, &map_len);
1783 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1786 read_extent_buffer(eb, &unaligned,
1787 offset, sizeof(unaligned));
1792 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1795 ret = comp_keys(tmp, key);
1811 * simple bin_search frontend that does the right thing for
1814 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1815 int level, int *slot)
1818 return generic_bin_search(eb,
1819 offsetof(struct btrfs_leaf, items),
1820 sizeof(struct btrfs_item),
1821 key, btrfs_header_nritems(eb),
1824 return generic_bin_search(eb,
1825 offsetof(struct btrfs_node, ptrs),
1826 sizeof(struct btrfs_key_ptr),
1827 key, btrfs_header_nritems(eb),
1831 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1832 int level, int *slot)
1834 return bin_search(eb, key, level, slot);
1837 static void root_add_used(struct btrfs_root *root, u32 size)
1839 spin_lock(&root->accounting_lock);
1840 btrfs_set_root_used(&root->root_item,
1841 btrfs_root_used(&root->root_item) + size);
1842 spin_unlock(&root->accounting_lock);
1845 static void root_sub_used(struct btrfs_root *root, u32 size)
1847 spin_lock(&root->accounting_lock);
1848 btrfs_set_root_used(&root->root_item,
1849 btrfs_root_used(&root->root_item) - size);
1850 spin_unlock(&root->accounting_lock);
1853 /* given a node and slot number, this reads the blocks it points to. The
1854 * extent buffer is returned with a reference taken (but unlocked).
1855 * NULL is returned on error.
1857 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
1858 struct extent_buffer *parent, int slot)
1860 int level = btrfs_header_level(parent);
1861 struct extent_buffer *eb;
1865 if (slot >= btrfs_header_nritems(parent))
1870 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1871 btrfs_level_size(root, level - 1),
1872 btrfs_node_ptr_generation(parent, slot));
1873 if (eb && !extent_buffer_uptodate(eb)) {
1874 free_extent_buffer(eb);
1882 * node level balancing, used to make sure nodes are in proper order for
1883 * item deletion. We balance from the top down, so we have to make sure
1884 * that a deletion won't leave an node completely empty later on.
1886 static noinline int balance_level(struct btrfs_trans_handle *trans,
1887 struct btrfs_root *root,
1888 struct btrfs_path *path, int level)
1890 struct extent_buffer *right = NULL;
1891 struct extent_buffer *mid;
1892 struct extent_buffer *left = NULL;
1893 struct extent_buffer *parent = NULL;
1897 int orig_slot = path->slots[level];
1903 mid = path->nodes[level];
1905 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1906 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1907 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1909 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1911 if (level < BTRFS_MAX_LEVEL - 1) {
1912 parent = path->nodes[level + 1];
1913 pslot = path->slots[level + 1];
1917 * deal with the case where there is only one pointer in the root
1918 * by promoting the node below to a root
1921 struct extent_buffer *child;
1923 if (btrfs_header_nritems(mid) != 1)
1926 /* promote the child to a root */
1927 child = read_node_slot(root, mid, 0);
1930 btrfs_std_error(root->fs_info, ret);
1934 btrfs_tree_lock(child);
1935 btrfs_set_lock_blocking(child);
1936 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1938 btrfs_tree_unlock(child);
1939 free_extent_buffer(child);
1943 tree_mod_log_set_root_pointer(root, child, 1);
1944 rcu_assign_pointer(root->node, child);
1946 add_root_to_dirty_list(root);
1947 btrfs_tree_unlock(child);
1949 path->locks[level] = 0;
1950 path->nodes[level] = NULL;
1951 clean_tree_block(trans, root, mid);
1952 btrfs_tree_unlock(mid);
1953 /* once for the path */
1954 free_extent_buffer(mid);
1956 root_sub_used(root, mid->len);
1957 btrfs_free_tree_block(trans, root, mid, 0, 1);
1958 /* once for the root ptr */
1959 free_extent_buffer_stale(mid);
1962 if (btrfs_header_nritems(mid) >
1963 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
1966 left = read_node_slot(root, parent, pslot - 1);
1968 btrfs_tree_lock(left);
1969 btrfs_set_lock_blocking(left);
1970 wret = btrfs_cow_block(trans, root, left,
1971 parent, pslot - 1, &left);
1977 right = read_node_slot(root, parent, pslot + 1);
1979 btrfs_tree_lock(right);
1980 btrfs_set_lock_blocking(right);
1981 wret = btrfs_cow_block(trans, root, right,
1982 parent, pslot + 1, &right);
1989 /* first, try to make some room in the middle buffer */
1991 orig_slot += btrfs_header_nritems(left);
1992 wret = push_node_left(trans, root, left, mid, 1);
1998 * then try to empty the right most buffer into the middle
2001 wret = push_node_left(trans, root, mid, right, 1);
2002 if (wret < 0 && wret != -ENOSPC)
2004 if (btrfs_header_nritems(right) == 0) {
2005 clean_tree_block(trans, root, right);
2006 btrfs_tree_unlock(right);
2007 del_ptr(root, path, level + 1, pslot + 1);
2008 root_sub_used(root, right->len);
2009 btrfs_free_tree_block(trans, root, right, 0, 1);
2010 free_extent_buffer_stale(right);
2013 struct btrfs_disk_key right_key;
2014 btrfs_node_key(right, &right_key, 0);
2015 tree_mod_log_set_node_key(root->fs_info, parent,
2017 btrfs_set_node_key(parent, &right_key, pslot + 1);
2018 btrfs_mark_buffer_dirty(parent);
2021 if (btrfs_header_nritems(mid) == 1) {
2023 * we're not allowed to leave a node with one item in the
2024 * tree during a delete. A deletion from lower in the tree
2025 * could try to delete the only pointer in this node.
2026 * So, pull some keys from the left.
2027 * There has to be a left pointer at this point because
2028 * otherwise we would have pulled some pointers from the
2033 btrfs_std_error(root->fs_info, ret);
2036 wret = balance_node_right(trans, root, mid, left);
2042 wret = push_node_left(trans, root, left, mid, 1);
2048 if (btrfs_header_nritems(mid) == 0) {
2049 clean_tree_block(trans, root, mid);
2050 btrfs_tree_unlock(mid);
2051 del_ptr(root, path, level + 1, pslot);
2052 root_sub_used(root, mid->len);
2053 btrfs_free_tree_block(trans, root, mid, 0, 1);
2054 free_extent_buffer_stale(mid);
2057 /* update the parent key to reflect our changes */
2058 struct btrfs_disk_key mid_key;
2059 btrfs_node_key(mid, &mid_key, 0);
2060 tree_mod_log_set_node_key(root->fs_info, parent,
2062 btrfs_set_node_key(parent, &mid_key, pslot);
2063 btrfs_mark_buffer_dirty(parent);
2066 /* update the path */
2068 if (btrfs_header_nritems(left) > orig_slot) {
2069 extent_buffer_get(left);
2070 /* left was locked after cow */
2071 path->nodes[level] = left;
2072 path->slots[level + 1] -= 1;
2073 path->slots[level] = orig_slot;
2075 btrfs_tree_unlock(mid);
2076 free_extent_buffer(mid);
2079 orig_slot -= btrfs_header_nritems(left);
2080 path->slots[level] = orig_slot;
2083 /* double check we haven't messed things up */
2085 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
2089 btrfs_tree_unlock(right);
2090 free_extent_buffer(right);
2093 if (path->nodes[level] != left)
2094 btrfs_tree_unlock(left);
2095 free_extent_buffer(left);
2100 /* Node balancing for insertion. Here we only split or push nodes around
2101 * when they are completely full. This is also done top down, so we
2102 * have to be pessimistic.
2104 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2105 struct btrfs_root *root,
2106 struct btrfs_path *path, int level)
2108 struct extent_buffer *right = NULL;
2109 struct extent_buffer *mid;
2110 struct extent_buffer *left = NULL;
2111 struct extent_buffer *parent = NULL;
2115 int orig_slot = path->slots[level];
2120 mid = path->nodes[level];
2121 WARN_ON(btrfs_header_generation(mid) != trans->transid);
2123 if (level < BTRFS_MAX_LEVEL - 1) {
2124 parent = path->nodes[level + 1];
2125 pslot = path->slots[level + 1];
2131 left = read_node_slot(root, parent, pslot - 1);
2133 /* first, try to make some room in the middle buffer */
2137 btrfs_tree_lock(left);
2138 btrfs_set_lock_blocking(left);
2140 left_nr = btrfs_header_nritems(left);
2141 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2144 ret = btrfs_cow_block(trans, root, left, parent,
2149 wret = push_node_left(trans, root,
2156 struct btrfs_disk_key disk_key;
2157 orig_slot += left_nr;
2158 btrfs_node_key(mid, &disk_key, 0);
2159 tree_mod_log_set_node_key(root->fs_info, parent,
2161 btrfs_set_node_key(parent, &disk_key, pslot);
2162 btrfs_mark_buffer_dirty(parent);
2163 if (btrfs_header_nritems(left) > orig_slot) {
2164 path->nodes[level] = left;
2165 path->slots[level + 1] -= 1;
2166 path->slots[level] = orig_slot;
2167 btrfs_tree_unlock(mid);
2168 free_extent_buffer(mid);
2171 btrfs_header_nritems(left);
2172 path->slots[level] = orig_slot;
2173 btrfs_tree_unlock(left);
2174 free_extent_buffer(left);
2178 btrfs_tree_unlock(left);
2179 free_extent_buffer(left);
2181 right = read_node_slot(root, parent, pslot + 1);
2184 * then try to empty the right most buffer into the middle
2189 btrfs_tree_lock(right);
2190 btrfs_set_lock_blocking(right);
2192 right_nr = btrfs_header_nritems(right);
2193 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2196 ret = btrfs_cow_block(trans, root, right,
2202 wret = balance_node_right(trans, root,
2209 struct btrfs_disk_key disk_key;
2211 btrfs_node_key(right, &disk_key, 0);
2212 tree_mod_log_set_node_key(root->fs_info, parent,
2214 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2215 btrfs_mark_buffer_dirty(parent);
2217 if (btrfs_header_nritems(mid) <= orig_slot) {
2218 path->nodes[level] = right;
2219 path->slots[level + 1] += 1;
2220 path->slots[level] = orig_slot -
2221 btrfs_header_nritems(mid);
2222 btrfs_tree_unlock(mid);
2223 free_extent_buffer(mid);
2225 btrfs_tree_unlock(right);
2226 free_extent_buffer(right);
2230 btrfs_tree_unlock(right);
2231 free_extent_buffer(right);
2237 * readahead one full node of leaves, finding things that are close
2238 * to the block in 'slot', and triggering ra on them.
2240 static void reada_for_search(struct btrfs_root *root,
2241 struct btrfs_path *path,
2242 int level, int slot, u64 objectid)
2244 struct extent_buffer *node;
2245 struct btrfs_disk_key disk_key;
2251 int direction = path->reada;
2252 struct extent_buffer *eb;
2260 if (!path->nodes[level])
2263 node = path->nodes[level];
2265 search = btrfs_node_blockptr(node, slot);
2266 blocksize = btrfs_level_size(root, level - 1);
2267 eb = btrfs_find_tree_block(root, search, blocksize);
2269 free_extent_buffer(eb);
2275 nritems = btrfs_header_nritems(node);
2279 if (direction < 0) {
2283 } else if (direction > 0) {
2288 if (path->reada < 0 && objectid) {
2289 btrfs_node_key(node, &disk_key, nr);
2290 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2293 search = btrfs_node_blockptr(node, nr);
2294 if ((search <= target && target - search <= 65536) ||
2295 (search > target && search - target <= 65536)) {
2296 gen = btrfs_node_ptr_generation(node, nr);
2297 readahead_tree_block(root, search, blocksize, gen);
2301 if ((nread > 65536 || nscan > 32))
2306 static noinline void reada_for_balance(struct btrfs_root *root,
2307 struct btrfs_path *path, int level)
2311 struct extent_buffer *parent;
2312 struct extent_buffer *eb;
2318 parent = path->nodes[level + 1];
2322 nritems = btrfs_header_nritems(parent);
2323 slot = path->slots[level + 1];
2324 blocksize = btrfs_level_size(root, level);
2327 block1 = btrfs_node_blockptr(parent, slot - 1);
2328 gen = btrfs_node_ptr_generation(parent, slot - 1);
2329 eb = btrfs_find_tree_block(root, block1, blocksize);
2331 * if we get -eagain from btrfs_buffer_uptodate, we
2332 * don't want to return eagain here. That will loop
2335 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2337 free_extent_buffer(eb);
2339 if (slot + 1 < nritems) {
2340 block2 = btrfs_node_blockptr(parent, slot + 1);
2341 gen = btrfs_node_ptr_generation(parent, slot + 1);
2342 eb = btrfs_find_tree_block(root, block2, blocksize);
2343 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2345 free_extent_buffer(eb);
2349 readahead_tree_block(root, block1, blocksize, 0);
2351 readahead_tree_block(root, block2, blocksize, 0);
2356 * when we walk down the tree, it is usually safe to unlock the higher layers
2357 * in the tree. The exceptions are when our path goes through slot 0, because
2358 * operations on the tree might require changing key pointers higher up in the
2361 * callers might also have set path->keep_locks, which tells this code to keep
2362 * the lock if the path points to the last slot in the block. This is part of
2363 * walking through the tree, and selecting the next slot in the higher block.
2365 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2366 * if lowest_unlock is 1, level 0 won't be unlocked
2368 static noinline void unlock_up(struct btrfs_path *path, int level,
2369 int lowest_unlock, int min_write_lock_level,
2370 int *write_lock_level)
2373 int skip_level = level;
2375 struct extent_buffer *t;
2377 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2378 if (!path->nodes[i])
2380 if (!path->locks[i])
2382 if (!no_skips && path->slots[i] == 0) {
2386 if (!no_skips && path->keep_locks) {
2389 nritems = btrfs_header_nritems(t);
2390 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2395 if (skip_level < i && i >= lowest_unlock)
2399 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
2400 btrfs_tree_unlock_rw(t, path->locks[i]);
2402 if (write_lock_level &&
2403 i > min_write_lock_level &&
2404 i <= *write_lock_level) {
2405 *write_lock_level = i - 1;
2412 * This releases any locks held in the path starting at level and
2413 * going all the way up to the root.
2415 * btrfs_search_slot will keep the lock held on higher nodes in a few
2416 * corner cases, such as COW of the block at slot zero in the node. This
2417 * ignores those rules, and it should only be called when there are no
2418 * more updates to be done higher up in the tree.
2420 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2424 if (path->keep_locks)
2427 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2428 if (!path->nodes[i])
2430 if (!path->locks[i])
2432 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2438 * helper function for btrfs_search_slot. The goal is to find a block
2439 * in cache without setting the path to blocking. If we find the block
2440 * we return zero and the path is unchanged.
2442 * If we can't find the block, we set the path blocking and do some
2443 * reada. -EAGAIN is returned and the search must be repeated.
2446 read_block_for_search(struct btrfs_trans_handle *trans,
2447 struct btrfs_root *root, struct btrfs_path *p,
2448 struct extent_buffer **eb_ret, int level, int slot,
2449 struct btrfs_key *key, u64 time_seq)
2454 struct extent_buffer *b = *eb_ret;
2455 struct extent_buffer *tmp;
2458 blocknr = btrfs_node_blockptr(b, slot);
2459 gen = btrfs_node_ptr_generation(b, slot);
2460 blocksize = btrfs_level_size(root, level - 1);
2462 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
2464 /* first we do an atomic uptodate check */
2465 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2470 /* the pages were up to date, but we failed
2471 * the generation number check. Do a full
2472 * read for the generation number that is correct.
2473 * We must do this without dropping locks so
2474 * we can trust our generation number
2476 btrfs_set_path_blocking(p);
2478 /* now we're allowed to do a blocking uptodate check */
2479 ret = btrfs_read_buffer(tmp, gen);
2484 free_extent_buffer(tmp);
2485 btrfs_release_path(p);
2490 * reduce lock contention at high levels
2491 * of the btree by dropping locks before
2492 * we read. Don't release the lock on the current
2493 * level because we need to walk this node to figure
2494 * out which blocks to read.
2496 btrfs_unlock_up_safe(p, level + 1);
2497 btrfs_set_path_blocking(p);
2499 free_extent_buffer(tmp);
2501 reada_for_search(root, p, level, slot, key->objectid);
2503 btrfs_release_path(p);
2506 tmp = read_tree_block(root, blocknr, blocksize, 0);
2509 * If the read above didn't mark this buffer up to date,
2510 * it will never end up being up to date. Set ret to EIO now
2511 * and give up so that our caller doesn't loop forever
2514 if (!btrfs_buffer_uptodate(tmp, 0, 0))
2516 free_extent_buffer(tmp);
2522 * helper function for btrfs_search_slot. This does all of the checks
2523 * for node-level blocks and does any balancing required based on
2526 * If no extra work was required, zero is returned. If we had to
2527 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2531 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2532 struct btrfs_root *root, struct btrfs_path *p,
2533 struct extent_buffer *b, int level, int ins_len,
2534 int *write_lock_level)
2537 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2538 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2541 if (*write_lock_level < level + 1) {
2542 *write_lock_level = level + 1;
2543 btrfs_release_path(p);
2547 btrfs_set_path_blocking(p);
2548 reada_for_balance(root, p, level);
2549 sret = split_node(trans, root, p, level);
2550 btrfs_clear_path_blocking(p, NULL, 0);
2557 b = p->nodes[level];
2558 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2559 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
2562 if (*write_lock_level < level + 1) {
2563 *write_lock_level = level + 1;
2564 btrfs_release_path(p);
2568 btrfs_set_path_blocking(p);
2569 reada_for_balance(root, p, level);
2570 sret = balance_level(trans, root, p, level);
2571 btrfs_clear_path_blocking(p, NULL, 0);
2577 b = p->nodes[level];
2579 btrfs_release_path(p);
2582 BUG_ON(btrfs_header_nritems(b) == 1);
2592 static void key_search_validate(struct extent_buffer *b,
2593 struct btrfs_key *key,
2596 #ifdef CONFIG_BTRFS_ASSERT
2597 struct btrfs_disk_key disk_key;
2599 btrfs_cpu_key_to_disk(&disk_key, key);
2602 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2603 offsetof(struct btrfs_leaf, items[0].key),
2606 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2607 offsetof(struct btrfs_node, ptrs[0].key),
2612 static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2613 int level, int *prev_cmp, int *slot)
2615 if (*prev_cmp != 0) {
2616 *prev_cmp = bin_search(b, key, level, slot);
2620 key_search_validate(b, key, level);
2626 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
2627 u64 iobjectid, u64 ioff, u8 key_type,
2628 struct btrfs_key *found_key)
2631 struct btrfs_key key;
2632 struct extent_buffer *eb;
2633 struct btrfs_path *path;
2635 key.type = key_type;
2636 key.objectid = iobjectid;
2639 if (found_path == NULL) {
2640 path = btrfs_alloc_path();
2646 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
2647 if ((ret < 0) || (found_key == NULL)) {
2648 if (path != found_path)
2649 btrfs_free_path(path);
2653 eb = path->nodes[0];
2654 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2655 ret = btrfs_next_leaf(fs_root, path);
2658 eb = path->nodes[0];
2661 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2662 if (found_key->type != key.type ||
2663 found_key->objectid != key.objectid)
2670 * look for key in the tree. path is filled in with nodes along the way
2671 * if key is found, we return zero and you can find the item in the leaf
2672 * level of the path (level 0)
2674 * If the key isn't found, the path points to the slot where it should
2675 * be inserted, and 1 is returned. If there are other errors during the
2676 * search a negative error number is returned.
2678 * if ins_len > 0, nodes and leaves will be split as we walk down the
2679 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2682 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2683 *root, struct btrfs_key *key, struct btrfs_path *p, int
2686 struct extent_buffer *b;
2691 int lowest_unlock = 1;
2693 /* everything at write_lock_level or lower must be write locked */
2694 int write_lock_level = 0;
2695 u8 lowest_level = 0;
2696 int min_write_lock_level;
2699 lowest_level = p->lowest_level;
2700 WARN_ON(lowest_level && ins_len > 0);
2701 WARN_ON(p->nodes[0] != NULL);
2702 BUG_ON(!cow && ins_len);
2707 /* when we are removing items, we might have to go up to level
2708 * two as we update tree pointers Make sure we keep write
2709 * for those levels as well
2711 write_lock_level = 2;
2712 } else if (ins_len > 0) {
2714 * for inserting items, make sure we have a write lock on
2715 * level 1 so we can update keys
2717 write_lock_level = 1;
2721 write_lock_level = -1;
2723 if (cow && (p->keep_locks || p->lowest_level))
2724 write_lock_level = BTRFS_MAX_LEVEL;
2726 min_write_lock_level = write_lock_level;
2731 * we try very hard to do read locks on the root
2733 root_lock = BTRFS_READ_LOCK;
2735 if (p->search_commit_root) {
2737 * the commit roots are read only
2738 * so we always do read locks
2740 if (p->need_commit_sem)
2741 down_read(&root->fs_info->commit_root_sem);
2742 b = root->commit_root;
2743 extent_buffer_get(b);
2744 level = btrfs_header_level(b);
2745 if (p->need_commit_sem)
2746 up_read(&root->fs_info->commit_root_sem);
2747 if (!p->skip_locking)
2748 btrfs_tree_read_lock(b);
2750 if (p->skip_locking) {
2751 b = btrfs_root_node(root);
2752 level = btrfs_header_level(b);
2754 /* we don't know the level of the root node
2755 * until we actually have it read locked
2757 b = btrfs_read_lock_root_node(root);
2758 level = btrfs_header_level(b);
2759 if (level <= write_lock_level) {
2760 /* whoops, must trade for write lock */
2761 btrfs_tree_read_unlock(b);
2762 free_extent_buffer(b);
2763 b = btrfs_lock_root_node(root);
2764 root_lock = BTRFS_WRITE_LOCK;
2766 /* the level might have changed, check again */
2767 level = btrfs_header_level(b);
2771 p->nodes[level] = b;
2772 if (!p->skip_locking)
2773 p->locks[level] = root_lock;
2776 level = btrfs_header_level(b);
2779 * setup the path here so we can release it under lock
2780 * contention with the cow code
2784 * if we don't really need to cow this block
2785 * then we don't want to set the path blocking,
2786 * so we test it here
2788 if (!should_cow_block(trans, root, b))
2791 btrfs_set_path_blocking(p);
2794 * must have write locks on this node and the
2797 if (level > write_lock_level ||
2798 (level + 1 > write_lock_level &&
2799 level + 1 < BTRFS_MAX_LEVEL &&
2800 p->nodes[level + 1])) {
2801 write_lock_level = level + 1;
2802 btrfs_release_path(p);
2806 err = btrfs_cow_block(trans, root, b,
2807 p->nodes[level + 1],
2808 p->slots[level + 1], &b);
2815 p->nodes[level] = b;
2816 btrfs_clear_path_blocking(p, NULL, 0);
2819 * we have a lock on b and as long as we aren't changing
2820 * the tree, there is no way to for the items in b to change.
2821 * It is safe to drop the lock on our parent before we
2822 * go through the expensive btree search on b.
2824 * If we're inserting or deleting (ins_len != 0), then we might
2825 * be changing slot zero, which may require changing the parent.
2826 * So, we can't drop the lock until after we know which slot
2827 * we're operating on.
2829 if (!ins_len && !p->keep_locks) {
2832 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2833 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2838 ret = key_search(b, key, level, &prev_cmp, &slot);
2842 if (ret && slot > 0) {
2846 p->slots[level] = slot;
2847 err = setup_nodes_for_search(trans, root, p, b, level,
2848 ins_len, &write_lock_level);
2855 b = p->nodes[level];
2856 slot = p->slots[level];
2859 * slot 0 is special, if we change the key
2860 * we have to update the parent pointer
2861 * which means we must have a write lock
2864 if (slot == 0 && ins_len &&
2865 write_lock_level < level + 1) {
2866 write_lock_level = level + 1;
2867 btrfs_release_path(p);
2871 unlock_up(p, level, lowest_unlock,
2872 min_write_lock_level, &write_lock_level);
2874 if (level == lowest_level) {
2880 err = read_block_for_search(trans, root, p,
2881 &b, level, slot, key, 0);
2889 if (!p->skip_locking) {
2890 level = btrfs_header_level(b);
2891 if (level <= write_lock_level) {
2892 err = btrfs_try_tree_write_lock(b);
2894 btrfs_set_path_blocking(p);
2896 btrfs_clear_path_blocking(p, b,
2899 p->locks[level] = BTRFS_WRITE_LOCK;
2901 err = btrfs_try_tree_read_lock(b);
2903 btrfs_set_path_blocking(p);
2904 btrfs_tree_read_lock(b);
2905 btrfs_clear_path_blocking(p, b,
2908 p->locks[level] = BTRFS_READ_LOCK;
2910 p->nodes[level] = b;
2913 p->slots[level] = slot;
2915 btrfs_leaf_free_space(root, b) < ins_len) {
2916 if (write_lock_level < 1) {
2917 write_lock_level = 1;
2918 btrfs_release_path(p);
2922 btrfs_set_path_blocking(p);
2923 err = split_leaf(trans, root, key,
2924 p, ins_len, ret == 0);
2925 btrfs_clear_path_blocking(p, NULL, 0);
2933 if (!p->search_for_split)
2934 unlock_up(p, level, lowest_unlock,
2935 min_write_lock_level, &write_lock_level);
2942 * we don't really know what they plan on doing with the path
2943 * from here on, so for now just mark it as blocking
2945 if (!p->leave_spinning)
2946 btrfs_set_path_blocking(p);
2948 btrfs_release_path(p);
2953 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2954 * current state of the tree together with the operations recorded in the tree
2955 * modification log to search for the key in a previous version of this tree, as
2956 * denoted by the time_seq parameter.
2958 * Naturally, there is no support for insert, delete or cow operations.
2960 * The resulting path and return value will be set up as if we called
2961 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2963 int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2964 struct btrfs_path *p, u64 time_seq)
2966 struct extent_buffer *b;
2971 int lowest_unlock = 1;
2972 u8 lowest_level = 0;
2975 lowest_level = p->lowest_level;
2976 WARN_ON(p->nodes[0] != NULL);
2978 if (p->search_commit_root) {
2980 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2984 b = get_old_root(root, time_seq);
2985 level = btrfs_header_level(b);
2986 p->locks[level] = BTRFS_READ_LOCK;
2989 level = btrfs_header_level(b);
2990 p->nodes[level] = b;
2991 btrfs_clear_path_blocking(p, NULL, 0);
2994 * we have a lock on b and as long as we aren't changing
2995 * the tree, there is no way to for the items in b to change.
2996 * It is safe to drop the lock on our parent before we
2997 * go through the expensive btree search on b.
2999 btrfs_unlock_up_safe(p, level + 1);
3002 * Since we can unwind eb's we want to do a real search every
3006 ret = key_search(b, key, level, &prev_cmp, &slot);
3010 if (ret && slot > 0) {
3014 p->slots[level] = slot;
3015 unlock_up(p, level, lowest_unlock, 0, NULL);
3017 if (level == lowest_level) {
3023 err = read_block_for_search(NULL, root, p, &b, level,
3024 slot, key, time_seq);
3032 level = btrfs_header_level(b);
3033 err = btrfs_try_tree_read_lock(b);
3035 btrfs_set_path_blocking(p);
3036 btrfs_tree_read_lock(b);
3037 btrfs_clear_path_blocking(p, b,
3040 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
3045 p->locks[level] = BTRFS_READ_LOCK;
3046 p->nodes[level] = b;
3048 p->slots[level] = slot;
3049 unlock_up(p, level, lowest_unlock, 0, NULL);
3055 if (!p->leave_spinning)
3056 btrfs_set_path_blocking(p);
3058 btrfs_release_path(p);
3064 * helper to use instead of search slot if no exact match is needed but
3065 * instead the next or previous item should be returned.
3066 * When find_higher is true, the next higher item is returned, the next lower
3068 * When return_any and find_higher are both true, and no higher item is found,
3069 * return the next lower instead.
3070 * When return_any is true and find_higher is false, and no lower item is found,
3071 * return the next higher instead.
3072 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3075 int btrfs_search_slot_for_read(struct btrfs_root *root,
3076 struct btrfs_key *key, struct btrfs_path *p,
3077 int find_higher, int return_any)
3080 struct extent_buffer *leaf;
3083 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3087 * a return value of 1 means the path is at the position where the
3088 * item should be inserted. Normally this is the next bigger item,
3089 * but in case the previous item is the last in a leaf, path points
3090 * to the first free slot in the previous leaf, i.e. at an invalid
3096 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3097 ret = btrfs_next_leaf(root, p);
3103 * no higher item found, return the next
3108 btrfs_release_path(p);
3112 if (p->slots[0] == 0) {
3113 ret = btrfs_prev_leaf(root, p);
3118 if (p->slots[0] == btrfs_header_nritems(leaf))
3125 * no lower item found, return the next
3130 btrfs_release_path(p);
3140 * adjust the pointers going up the tree, starting at level
3141 * making sure the right key of each node is points to 'key'.
3142 * This is used after shifting pointers to the left, so it stops
3143 * fixing up pointers when a given leaf/node is not in slot 0 of the
3147 static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
3148 struct btrfs_disk_key *key, int level)
3151 struct extent_buffer *t;
3153 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
3154 int tslot = path->slots[i];
3155 if (!path->nodes[i])
3158 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
3159 btrfs_set_node_key(t, key, tslot);
3160 btrfs_mark_buffer_dirty(path->nodes[i]);
3169 * This function isn't completely safe. It's the caller's responsibility
3170 * that the new key won't break the order
3172 void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
3173 struct btrfs_key *new_key)
3175 struct btrfs_disk_key disk_key;
3176 struct extent_buffer *eb;
3179 eb = path->nodes[0];
3180 slot = path->slots[0];
3182 btrfs_item_key(eb, &disk_key, slot - 1);
3183 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
3185 if (slot < btrfs_header_nritems(eb) - 1) {
3186 btrfs_item_key(eb, &disk_key, slot + 1);
3187 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
3190 btrfs_cpu_key_to_disk(&disk_key, new_key);
3191 btrfs_set_item_key(eb, &disk_key, slot);
3192 btrfs_mark_buffer_dirty(eb);
3194 fixup_low_keys(root, path, &disk_key, 1);
3198 * try to push data from one node into the next node left in the
3201 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3202 * error, and > 0 if there was no room in the left hand block.
3204 static int push_node_left(struct btrfs_trans_handle *trans,
3205 struct btrfs_root *root, struct extent_buffer *dst,
3206 struct extent_buffer *src, int empty)
3213 src_nritems = btrfs_header_nritems(src);
3214 dst_nritems = btrfs_header_nritems(dst);
3215 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
3216 WARN_ON(btrfs_header_generation(src) != trans->transid);
3217 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3219 if (!empty && src_nritems <= 8)
3222 if (push_items <= 0)
3226 push_items = min(src_nritems, push_items);
3227 if (push_items < src_nritems) {
3228 /* leave at least 8 pointers in the node if
3229 * we aren't going to empty it
3231 if (src_nritems - push_items < 8) {
3232 if (push_items <= 8)
3238 push_items = min(src_nritems - 8, push_items);
3240 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3243 btrfs_abort_transaction(trans, root, ret);
3246 copy_extent_buffer(dst, src,
3247 btrfs_node_key_ptr_offset(dst_nritems),
3248 btrfs_node_key_ptr_offset(0),
3249 push_items * sizeof(struct btrfs_key_ptr));
3251 if (push_items < src_nritems) {
3253 * don't call tree_mod_log_eb_move here, key removal was already
3254 * fully logged by tree_mod_log_eb_copy above.
3256 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3257 btrfs_node_key_ptr_offset(push_items),
3258 (src_nritems - push_items) *
3259 sizeof(struct btrfs_key_ptr));
3261 btrfs_set_header_nritems(src, src_nritems - push_items);
3262 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3263 btrfs_mark_buffer_dirty(src);
3264 btrfs_mark_buffer_dirty(dst);
3270 * try to push data from one node into the next node right in the
3273 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3274 * error, and > 0 if there was no room in the right hand block.
3276 * this will only push up to 1/2 the contents of the left node over
3278 static int balance_node_right(struct btrfs_trans_handle *trans,
3279 struct btrfs_root *root,
3280 struct extent_buffer *dst,
3281 struct extent_buffer *src)
3289 WARN_ON(btrfs_header_generation(src) != trans->transid);
3290 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3292 src_nritems = btrfs_header_nritems(src);
3293 dst_nritems = btrfs_header_nritems(dst);
3294 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
3295 if (push_items <= 0)
3298 if (src_nritems < 4)
3301 max_push = src_nritems / 2 + 1;
3302 /* don't try to empty the node */
3303 if (max_push >= src_nritems)
3306 if (max_push < push_items)
3307 push_items = max_push;
3309 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
3310 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3311 btrfs_node_key_ptr_offset(0),
3313 sizeof(struct btrfs_key_ptr));
3315 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3316 src_nritems - push_items, push_items);
3318 btrfs_abort_transaction(trans, root, ret);
3321 copy_extent_buffer(dst, src,
3322 btrfs_node_key_ptr_offset(0),
3323 btrfs_node_key_ptr_offset(src_nritems - push_items),
3324 push_items * sizeof(struct btrfs_key_ptr));
3326 btrfs_set_header_nritems(src, src_nritems - push_items);
3327 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3329 btrfs_mark_buffer_dirty(src);
3330 btrfs_mark_buffer_dirty(dst);
3336 * helper function to insert a new root level in the tree.
3337 * A new node is allocated, and a single item is inserted to
3338 * point to the existing root
3340 * returns zero on success or < 0 on failure.
3342 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3343 struct btrfs_root *root,
3344 struct btrfs_path *path, int level)
3347 struct extent_buffer *lower;
3348 struct extent_buffer *c;
3349 struct extent_buffer *old;
3350 struct btrfs_disk_key lower_key;
3352 BUG_ON(path->nodes[level]);
3353 BUG_ON(path->nodes[level-1] != root->node);
3355 lower = path->nodes[level-1];
3357 btrfs_item_key(lower, &lower_key, 0);
3359 btrfs_node_key(lower, &lower_key, 0);
3361 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
3362 root->root_key.objectid, &lower_key,
3363 level, root->node->start, 0);
3367 root_add_used(root, root->nodesize);
3369 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
3370 btrfs_set_header_nritems(c, 1);
3371 btrfs_set_header_level(c, level);
3372 btrfs_set_header_bytenr(c, c->start);
3373 btrfs_set_header_generation(c, trans->transid);
3374 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
3375 btrfs_set_header_owner(c, root->root_key.objectid);
3377 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
3380 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3381 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
3383 btrfs_set_node_key(c, &lower_key, 0);
3384 btrfs_set_node_blockptr(c, 0, lower->start);
3385 lower_gen = btrfs_header_generation(lower);
3386 WARN_ON(lower_gen != trans->transid);
3388 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3390 btrfs_mark_buffer_dirty(c);
3393 tree_mod_log_set_root_pointer(root, c, 0);
3394 rcu_assign_pointer(root->node, c);
3396 /* the super has an extra ref to root->node */
3397 free_extent_buffer(old);
3399 add_root_to_dirty_list(root);
3400 extent_buffer_get(c);
3401 path->nodes[level] = c;
3402 path->locks[level] = BTRFS_WRITE_LOCK;
3403 path->slots[level] = 0;
3408 * worker function to insert a single pointer in a node.
3409 * the node should have enough room for the pointer already
3411 * slot and level indicate where you want the key to go, and
3412 * blocknr is the block the key points to.
3414 static void insert_ptr(struct btrfs_trans_handle *trans,
3415 struct btrfs_root *root, struct btrfs_path *path,
3416 struct btrfs_disk_key *key, u64 bytenr,
3417 int slot, int level)
3419 struct extent_buffer *lower;
3423 BUG_ON(!path->nodes[level]);
3424 btrfs_assert_tree_locked(path->nodes[level]);
3425 lower = path->nodes[level];
3426 nritems = btrfs_header_nritems(lower);
3427 BUG_ON(slot > nritems);
3428 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
3429 if (slot != nritems) {
3431 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3432 slot, nritems - slot);
3433 memmove_extent_buffer(lower,
3434 btrfs_node_key_ptr_offset(slot + 1),
3435 btrfs_node_key_ptr_offset(slot),
3436 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3439 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3440 MOD_LOG_KEY_ADD, GFP_NOFS);
3443 btrfs_set_node_key(lower, key, slot);
3444 btrfs_set_node_blockptr(lower, slot, bytenr);
3445 WARN_ON(trans->transid == 0);
3446 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3447 btrfs_set_header_nritems(lower, nritems + 1);
3448 btrfs_mark_buffer_dirty(lower);
3452 * split the node at the specified level in path in two.
3453 * The path is corrected to point to the appropriate node after the split
3455 * Before splitting this tries to make some room in the node by pushing
3456 * left and right, if either one works, it returns right away.
3458 * returns 0 on success and < 0 on failure
3460 static noinline int split_node(struct btrfs_trans_handle *trans,
3461 struct btrfs_root *root,
3462 struct btrfs_path *path, int level)
3464 struct extent_buffer *c;
3465 struct extent_buffer *split;
3466 struct btrfs_disk_key disk_key;
3471 c = path->nodes[level];
3472 WARN_ON(btrfs_header_generation(c) != trans->transid);
3473 if (c == root->node) {
3475 * trying to split the root, lets make a new one
3477 * tree mod log: We don't log_removal old root in
3478 * insert_new_root, because that root buffer will be kept as a
3479 * normal node. We are going to log removal of half of the
3480 * elements below with tree_mod_log_eb_copy. We're holding a
3481 * tree lock on the buffer, which is why we cannot race with
3482 * other tree_mod_log users.
3484 ret = insert_new_root(trans, root, path, level + 1);
3488 ret = push_nodes_for_insert(trans, root, path, level);
3489 c = path->nodes[level];
3490 if (!ret && btrfs_header_nritems(c) <
3491 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
3497 c_nritems = btrfs_header_nritems(c);
3498 mid = (c_nritems + 1) / 2;
3499 btrfs_node_key(c, &disk_key, mid);
3501 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
3502 root->root_key.objectid,
3503 &disk_key, level, c->start, 0);
3505 return PTR_ERR(split);
3507 root_add_used(root, root->nodesize);
3509 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
3510 btrfs_set_header_level(split, btrfs_header_level(c));
3511 btrfs_set_header_bytenr(split, split->start);
3512 btrfs_set_header_generation(split, trans->transid);
3513 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
3514 btrfs_set_header_owner(split, root->root_key.objectid);
3515 write_extent_buffer(split, root->fs_info->fsid,
3516 btrfs_header_fsid(), BTRFS_FSID_SIZE);
3517 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3518 btrfs_header_chunk_tree_uuid(split),
3521 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3522 mid, c_nritems - mid);
3524 btrfs_abort_transaction(trans, root, ret);
3527 copy_extent_buffer(split, c,
3528 btrfs_node_key_ptr_offset(0),
3529 btrfs_node_key_ptr_offset(mid),
3530 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3531 btrfs_set_header_nritems(split, c_nritems - mid);
3532 btrfs_set_header_nritems(c, mid);
3535 btrfs_mark_buffer_dirty(c);
3536 btrfs_mark_buffer_dirty(split);
3538 insert_ptr(trans, root, path, &disk_key, split->start,
3539 path->slots[level + 1] + 1, level + 1);
3541 if (path->slots[level] >= mid) {
3542 path->slots[level] -= mid;
3543 btrfs_tree_unlock(c);
3544 free_extent_buffer(c);
3545 path->nodes[level] = split;
3546 path->slots[level + 1] += 1;
3548 btrfs_tree_unlock(split);
3549 free_extent_buffer(split);
3555 * how many bytes are required to store the items in a leaf. start
3556 * and nr indicate which items in the leaf to check. This totals up the
3557 * space used both by the item structs and the item data
3559 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3561 struct btrfs_item *start_item;
3562 struct btrfs_item *end_item;
3563 struct btrfs_map_token token;
3565 int nritems = btrfs_header_nritems(l);
3566 int end = min(nritems, start + nr) - 1;
3570 btrfs_init_map_token(&token);
3571 start_item = btrfs_item_nr(start);
3572 end_item = btrfs_item_nr(end);
3573 data_len = btrfs_token_item_offset(l, start_item, &token) +
3574 btrfs_token_item_size(l, start_item, &token);
3575 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
3576 data_len += sizeof(struct btrfs_item) * nr;
3577 WARN_ON(data_len < 0);
3582 * The space between the end of the leaf items and
3583 * the start of the leaf data. IOW, how much room
3584 * the leaf has left for both items and data
3586 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
3587 struct extent_buffer *leaf)
3589 int nritems = btrfs_header_nritems(leaf);
3591 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3593 btrfs_crit(root->fs_info,
3594 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3595 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
3596 leaf_space_used(leaf, 0, nritems), nritems);
3602 * min slot controls the lowest index we're willing to push to the
3603 * right. We'll push up to and including min_slot, but no lower
3605 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3606 struct btrfs_root *root,
3607 struct btrfs_path *path,
3608 int data_size, int empty,
3609 struct extent_buffer *right,
3610 int free_space, u32 left_nritems,
3613 struct extent_buffer *left = path->nodes[0];
3614 struct extent_buffer *upper = path->nodes[1];
3615 struct btrfs_map_token token;
3616 struct btrfs_disk_key disk_key;
3621 struct btrfs_item *item;
3627 btrfs_init_map_token(&token);
3632 nr = max_t(u32, 1, min_slot);
3634 if (path->slots[0] >= left_nritems)
3635 push_space += data_size;
3637 slot = path->slots[1];
3638 i = left_nritems - 1;
3640 item = btrfs_item_nr(i);
3642 if (!empty && push_items > 0) {
3643 if (path->slots[0] > i)
3645 if (path->slots[0] == i) {
3646 int space = btrfs_leaf_free_space(root, left);
3647 if (space + push_space * 2 > free_space)
3652 if (path->slots[0] == i)
3653 push_space += data_size;
3655 this_item_size = btrfs_item_size(left, item);
3656 if (this_item_size + sizeof(*item) + push_space > free_space)
3660 push_space += this_item_size + sizeof(*item);
3666 if (push_items == 0)
3669 WARN_ON(!empty && push_items == left_nritems);
3671 /* push left to right */
3672 right_nritems = btrfs_header_nritems(right);
3674 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3675 push_space -= leaf_data_end(root, left);
3677 /* make room in the right data area */
3678 data_end = leaf_data_end(root, right);
3679 memmove_extent_buffer(right,
3680 btrfs_leaf_data(right) + data_end - push_space,
3681 btrfs_leaf_data(right) + data_end,
3682 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3684 /* copy from the left data area */
3685 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
3686 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3687 btrfs_leaf_data(left) + leaf_data_end(root, left),
3690 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3691 btrfs_item_nr_offset(0),
3692 right_nritems * sizeof(struct btrfs_item));
3694 /* copy the items from left to right */
3695 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3696 btrfs_item_nr_offset(left_nritems - push_items),
3697 push_items * sizeof(struct btrfs_item));
3699 /* update the item pointers */
3700 right_nritems += push_items;
3701 btrfs_set_header_nritems(right, right_nritems);
3702 push_space = BTRFS_LEAF_DATA_SIZE(root);
3703 for (i = 0; i < right_nritems; i++) {
3704 item = btrfs_item_nr(i);
3705 push_space -= btrfs_token_item_size(right, item, &token);
3706 btrfs_set_token_item_offset(right, item, push_space, &token);
3709 left_nritems -= push_items;
3710 btrfs_set_header_nritems(left, left_nritems);
3713 btrfs_mark_buffer_dirty(left);
3715 clean_tree_block(trans, root, left);
3717 btrfs_mark_buffer_dirty(right);
3719 btrfs_item_key(right, &disk_key, 0);
3720 btrfs_set_node_key(upper, &disk_key, slot + 1);
3721 btrfs_mark_buffer_dirty(upper);
3723 /* then fixup the leaf pointer in the path */
3724 if (path->slots[0] >= left_nritems) {
3725 path->slots[0] -= left_nritems;
3726 if (btrfs_header_nritems(path->nodes[0]) == 0)
3727 clean_tree_block(trans, root, path->nodes[0]);
3728 btrfs_tree_unlock(path->nodes[0]);
3729 free_extent_buffer(path->nodes[0]);
3730 path->nodes[0] = right;
3731 path->slots[1] += 1;
3733 btrfs_tree_unlock(right);
3734 free_extent_buffer(right);
3739 btrfs_tree_unlock(right);
3740 free_extent_buffer(right);
3745 * push some data in the path leaf to the right, trying to free up at
3746 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3748 * returns 1 if the push failed because the other node didn't have enough
3749 * room, 0 if everything worked out and < 0 if there were major errors.
3751 * this will push starting from min_slot to the end of the leaf. It won't
3752 * push any slot lower than min_slot
3754 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3755 *root, struct btrfs_path *path,
3756 int min_data_size, int data_size,
3757 int empty, u32 min_slot)
3759 struct extent_buffer *left = path->nodes[0];
3760 struct extent_buffer *right;
3761 struct extent_buffer *upper;
3767 if (!path->nodes[1])
3770 slot = path->slots[1];
3771 upper = path->nodes[1];
3772 if (slot >= btrfs_header_nritems(upper) - 1)
3775 btrfs_assert_tree_locked(path->nodes[1]);
3777 right = read_node_slot(root, upper, slot + 1);
3781 btrfs_tree_lock(right);
3782 btrfs_set_lock_blocking(right);
3784 free_space = btrfs_leaf_free_space(root, right);
3785 if (free_space < data_size)
3788 /* cow and double check */
3789 ret = btrfs_cow_block(trans, root, right, upper,
3794 free_space = btrfs_leaf_free_space(root, right);
3795 if (free_space < data_size)
3798 left_nritems = btrfs_header_nritems(left);
3799 if (left_nritems == 0)
3802 if (path->slots[0] == left_nritems && !empty) {
3803 /* Key greater than all keys in the leaf, right neighbor has
3804 * enough room for it and we're not emptying our leaf to delete
3805 * it, therefore use right neighbor to insert the new item and
3806 * no need to touch/dirty our left leaft. */
3807 btrfs_tree_unlock(left);
3808 free_extent_buffer(left);
3809 path->nodes[0] = right;
3815 return __push_leaf_right(trans, root, path, min_data_size, empty,
3816 right, free_space, left_nritems, min_slot);
3818 btrfs_tree_unlock(right);
3819 free_extent_buffer(right);
3824 * push some data in the path leaf to the left, trying to free up at
3825 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3827 * max_slot can put a limit on how far into the leaf we'll push items. The
3828 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3831 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3832 struct btrfs_root *root,
3833 struct btrfs_path *path, int data_size,
3834 int empty, struct extent_buffer *left,
3835 int free_space, u32 right_nritems,
3838 struct btrfs_disk_key disk_key;
3839 struct extent_buffer *right = path->nodes[0];
3843 struct btrfs_item *item;
3844 u32 old_left_nritems;
3848 u32 old_left_item_size;
3849 struct btrfs_map_token token;
3851 btrfs_init_map_token(&token);
3854 nr = min(right_nritems, max_slot);
3856 nr = min(right_nritems - 1, max_slot);
3858 for (i = 0; i < nr; i++) {
3859 item = btrfs_item_nr(i);
3861 if (!empty && push_items > 0) {
3862 if (path->slots[0] < i)
3864 if (path->slots[0] == i) {
3865 int space = btrfs_leaf_free_space(root, right);
3866 if (space + push_space * 2 > free_space)
3871 if (path->slots[0] == i)
3872 push_space += data_size;
3874 this_item_size = btrfs_item_size(right, item);
3875 if (this_item_size + sizeof(*item) + push_space > free_space)
3879 push_space += this_item_size + sizeof(*item);
3882 if (push_items == 0) {
3886 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3888 /* push data from right to left */
3889 copy_extent_buffer(left, right,
3890 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3891 btrfs_item_nr_offset(0),
3892 push_items * sizeof(struct btrfs_item));
3894 push_space = BTRFS_LEAF_DATA_SIZE(root) -
3895 btrfs_item_offset_nr(right, push_items - 1);
3897 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
3898 leaf_data_end(root, left) - push_space,
3899 btrfs_leaf_data(right) +
3900 btrfs_item_offset_nr(right, push_items - 1),
3902 old_left_nritems = btrfs_header_nritems(left);
3903 BUG_ON(old_left_nritems <= 0);
3905 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3906 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3909 item = btrfs_item_nr(i);
3911 ioff = btrfs_token_item_offset(left, item, &token);
3912 btrfs_set_token_item_offset(left, item,
3913 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3916 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3918 /* fixup right node */
3919 if (push_items > right_nritems)
3920 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3923 if (push_items < right_nritems) {
3924 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3925 leaf_data_end(root, right);
3926 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3927 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3928 btrfs_leaf_data(right) +
3929 leaf_data_end(root, right), push_space);
3931 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3932 btrfs_item_nr_offset(push_items),
3933 (btrfs_header_nritems(right) - push_items) *
3934 sizeof(struct btrfs_item));
3936 right_nritems -= push_items;
3937 btrfs_set_header_nritems(right, right_nritems);
3938 push_space = BTRFS_LEAF_DATA_SIZE(root);
3939 for (i = 0; i < right_nritems; i++) {
3940 item = btrfs_item_nr(i);
3942 push_space = push_space - btrfs_token_item_size(right,
3944 btrfs_set_token_item_offset(right, item, push_space, &token);
3947 btrfs_mark_buffer_dirty(left);
3949 btrfs_mark_buffer_dirty(right);
3951 clean_tree_block(trans, root, right);
3953 btrfs_item_key(right, &disk_key, 0);
3954 fixup_low_keys(root, path, &disk_key, 1);
3956 /* then fixup the leaf pointer in the path */
3957 if (path->slots[0] < push_items) {
3958 path->slots[0] += old_left_nritems;
3959 btrfs_tree_unlock(path->nodes[0]);
3960 free_extent_buffer(path->nodes[0]);
3961 path->nodes[0] = left;
3962 path->slots[1] -= 1;
3964 btrfs_tree_unlock(left);
3965 free_extent_buffer(left);
3966 path->slots[0] -= push_items;
3968 BUG_ON(path->slots[0] < 0);
3971 btrfs_tree_unlock(left);
3972 free_extent_buffer(left);
3977 * push some data in the path leaf to the left, trying to free up at
3978 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3980 * max_slot can put a limit on how far into the leaf we'll push items. The
3981 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3984 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3985 *root, struct btrfs_path *path, int min_data_size,
3986 int data_size, int empty, u32 max_slot)
3988 struct extent_buffer *right = path->nodes[0];
3989 struct extent_buffer *left;
3995 slot = path->slots[1];
3998 if (!path->nodes[1])
4001 right_nritems = btrfs_header_nritems(right);
4002 if (right_nritems == 0)
4005 btrfs_assert_tree_locked(path->nodes[1]);
4007 left = read_node_slot(root, path->nodes[1], slot - 1);
4011 btrfs_tree_lock(left);
4012 btrfs_set_lock_blocking(left);
4014 free_space = btrfs_leaf_free_space(root, left);
4015 if (free_space < data_size) {
4020 /* cow and double check */
4021 ret = btrfs_cow_block(trans, root, left,
4022 path->nodes[1], slot - 1, &left);
4024 /* we hit -ENOSPC, but it isn't fatal here */
4030 free_space = btrfs_leaf_free_space(root, left);
4031 if (free_space < data_size) {
4036 return __push_leaf_left(trans, root, path, min_data_size,
4037 empty, left, free_space, right_nritems,
4040 btrfs_tree_unlock(left);
4041 free_extent_buffer(left);
4046 * split the path's leaf in two, making sure there is at least data_size
4047 * available for the resulting leaf level of the path.
4049 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4050 struct btrfs_root *root,
4051 struct btrfs_path *path,
4052 struct extent_buffer *l,
4053 struct extent_buffer *right,
4054 int slot, int mid, int nritems)
4059 struct btrfs_disk_key disk_key;
4060 struct btrfs_map_token token;
4062 btrfs_init_map_token(&token);
4064 nritems = nritems - mid;
4065 btrfs_set_header_nritems(right, nritems);
4066 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4068 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4069 btrfs_item_nr_offset(mid),
4070 nritems * sizeof(struct btrfs_item));
4072 copy_extent_buffer(right, l,
4073 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4074 data_copy_size, btrfs_leaf_data(l) +
4075 leaf_data_end(root, l), data_copy_size);
4077 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4078 btrfs_item_end_nr(l, mid);
4080 for (i = 0; i < nritems; i++) {
4081 struct btrfs_item *item = btrfs_item_nr(i);
4084 ioff = btrfs_token_item_offset(right, item, &token);
4085 btrfs_set_token_item_offset(right, item,
4086 ioff + rt_data_off, &token);
4089 btrfs_set_header_nritems(l, mid);
4090 btrfs_item_key(right, &disk_key, 0);
4091 insert_ptr(trans, root, path, &disk_key, right->start,
4092 path->slots[1] + 1, 1);
4094 btrfs_mark_buffer_dirty(right);
4095 btrfs_mark_buffer_dirty(l);
4096 BUG_ON(path->slots[0] != slot);
4099 btrfs_tree_unlock(path->nodes[0]);
4100 free_extent_buffer(path->nodes[0]);
4101 path->nodes[0] = right;
4102 path->slots[0] -= mid;
4103 path->slots[1] += 1;
4105 btrfs_tree_unlock(right);
4106 free_extent_buffer(right);
4109 BUG_ON(path->slots[0] < 0);
4113 * double splits happen when we need to insert a big item in the middle
4114 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4115 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4118 * We avoid this by trying to push the items on either side of our target
4119 * into the adjacent leaves. If all goes well we can avoid the double split
4122 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4123 struct btrfs_root *root,
4124 struct btrfs_path *path,
4131 int space_needed = data_size;
4133 slot = path->slots[0];
4134 if (slot < btrfs_header_nritems(path->nodes[0]))
4135 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
4138 * try to push all the items after our slot into the
4141 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
4148 nritems = btrfs_header_nritems(path->nodes[0]);
4150 * our goal is to get our slot at the start or end of a leaf. If
4151 * we've done so we're done
4153 if (path->slots[0] == 0 || path->slots[0] == nritems)
4156 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4159 /* try to push all the items before our slot into the next leaf */
4160 slot = path->slots[0];
4161 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
4174 * split the path's leaf in two, making sure there is at least data_size
4175 * available for the resulting leaf level of the path.
4177 * returns 0 if all went well and < 0 on failure.
4179 static noinline int split_leaf(struct btrfs_trans_handle *trans,
4180 struct btrfs_root *root,
4181 struct btrfs_key *ins_key,
4182 struct btrfs_path *path, int data_size,
4185 struct btrfs_disk_key disk_key;
4186 struct extent_buffer *l;
4190 struct extent_buffer *right;
4194 int num_doubles = 0;
4195 int tried_avoid_double = 0;
4198 slot = path->slots[0];
4199 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4200 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4203 /* first try to make some room by pushing left and right */
4204 if (data_size && path->nodes[1]) {
4205 int space_needed = data_size;
4207 if (slot < btrfs_header_nritems(l))
4208 space_needed -= btrfs_leaf_free_space(root, l);
4210 wret = push_leaf_right(trans, root, path, space_needed,
4211 space_needed, 0, 0);
4215 wret = push_leaf_left(trans, root, path, space_needed,
4216 space_needed, 0, (u32)-1);
4222 /* did the pushes work? */
4223 if (btrfs_leaf_free_space(root, l) >= data_size)
4227 if (!path->nodes[1]) {
4228 ret = insert_new_root(trans, root, path, 1);
4235 slot = path->slots[0];
4236 nritems = btrfs_header_nritems(l);
4237 mid = (nritems + 1) / 2;
4241 leaf_space_used(l, mid, nritems - mid) + data_size >
4242 BTRFS_LEAF_DATA_SIZE(root)) {
4243 if (slot >= nritems) {
4247 if (mid != nritems &&
4248 leaf_space_used(l, mid, nritems - mid) +
4249 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
4250 if (data_size && !tried_avoid_double)
4251 goto push_for_double;
4257 if (leaf_space_used(l, 0, mid) + data_size >
4258 BTRFS_LEAF_DATA_SIZE(root)) {
4259 if (!extend && data_size && slot == 0) {
4261 } else if ((extend || !data_size) && slot == 0) {
4265 if (mid != nritems &&
4266 leaf_space_used(l, mid, nritems - mid) +
4267 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
4268 if (data_size && !tried_avoid_double)
4269 goto push_for_double;
4277 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4279 btrfs_item_key(l, &disk_key, mid);
4281 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
4282 root->root_key.objectid,
4283 &disk_key, 0, l->start, 0);
4285 return PTR_ERR(right);
4287 root_add_used(root, root->leafsize);
4289 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4290 btrfs_set_header_bytenr(right, right->start);
4291 btrfs_set_header_generation(right, trans->transid);
4292 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
4293 btrfs_set_header_owner(right, root->root_key.objectid);
4294 btrfs_set_header_level(right, 0);
4295 write_extent_buffer(right, root->fs_info->fsid,
4296 btrfs_header_fsid(), BTRFS_FSID_SIZE);
4298 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
4299 btrfs_header_chunk_tree_uuid(right),
4304 btrfs_set_header_nritems(right, 0);
4305 insert_ptr(trans, root, path, &disk_key, right->start,
4306 path->slots[1] + 1, 1);
4307 btrfs_tree_unlock(path->nodes[0]);
4308 free_extent_buffer(path->nodes[0]);
4309 path->nodes[0] = right;
4311 path->slots[1] += 1;
4313 btrfs_set_header_nritems(right, 0);
4314 insert_ptr(trans, root, path, &disk_key, right->start,
4316 btrfs_tree_unlock(path->nodes[0]);
4317 free_extent_buffer(path->nodes[0]);
4318 path->nodes[0] = right;
4320 if (path->slots[1] == 0)
4321 fixup_low_keys(root, path, &disk_key, 1);
4323 btrfs_mark_buffer_dirty(right);
4327 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
4330 BUG_ON(num_doubles != 0);
4338 push_for_double_split(trans, root, path, data_size);
4339 tried_avoid_double = 1;
4340 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4345 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4346 struct btrfs_root *root,
4347 struct btrfs_path *path, int ins_len)
4349 struct btrfs_key key;
4350 struct extent_buffer *leaf;
4351 struct btrfs_file_extent_item *fi;
4356 leaf = path->nodes[0];
4357 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4359 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4360 key.type != BTRFS_EXTENT_CSUM_KEY);
4362 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4365 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4366 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4367 fi = btrfs_item_ptr(leaf, path->slots[0],
4368 struct btrfs_file_extent_item);
4369 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4371 btrfs_release_path(path);
4373 path->keep_locks = 1;
4374 path->search_for_split = 1;
4375 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4376 path->search_for_split = 0;
4381 leaf = path->nodes[0];
4382 /* if our item isn't there or got smaller, return now */
4383 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4386 /* the leaf has changed, it now has room. return now */
4387 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4390 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4391 fi = btrfs_item_ptr(leaf, path->slots[0],
4392 struct btrfs_file_extent_item);
4393 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4397 btrfs_set_path_blocking(path);
4398 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4402 path->keep_locks = 0;
4403 btrfs_unlock_up_safe(path, 1);
4406 path->keep_locks = 0;
4410 static noinline int split_item(struct btrfs_trans_handle *trans,
4411 struct btrfs_root *root,
4412 struct btrfs_path *path,
4413 struct btrfs_key *new_key,
4414 unsigned long split_offset)
4416 struct extent_buffer *leaf;
4417 struct btrfs_item *item;
4418 struct btrfs_item *new_item;
4424 struct btrfs_disk_key disk_key;
4426 leaf = path->nodes[0];
4427 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4429 btrfs_set_path_blocking(path);
4431 item = btrfs_item_nr(path->slots[0]);
4432 orig_offset = btrfs_item_offset(leaf, item);
4433 item_size = btrfs_item_size(leaf, item);
4435 buf = kmalloc(item_size, GFP_NOFS);
4439 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4440 path->slots[0]), item_size);
4442 slot = path->slots[0] + 1;
4443 nritems = btrfs_header_nritems(leaf);
4444 if (slot != nritems) {
4445 /* shift the items */
4446 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4447 btrfs_item_nr_offset(slot),
4448 (nritems - slot) * sizeof(struct btrfs_item));
4451 btrfs_cpu_key_to_disk(&disk_key, new_key);
4452 btrfs_set_item_key(leaf, &disk_key, slot);
4454 new_item = btrfs_item_nr(slot);
4456 btrfs_set_item_offset(leaf, new_item, orig_offset);
4457 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4459 btrfs_set_item_offset(leaf, item,
4460 orig_offset + item_size - split_offset);
4461 btrfs_set_item_size(leaf, item, split_offset);
4463 btrfs_set_header_nritems(leaf, nritems + 1);
4465 /* write the data for the start of the original item */
4466 write_extent_buffer(leaf, buf,
4467 btrfs_item_ptr_offset(leaf, path->slots[0]),
4470 /* write the data for the new item */
4471 write_extent_buffer(leaf, buf + split_offset,
4472 btrfs_item_ptr_offset(leaf, slot),
4473 item_size - split_offset);
4474 btrfs_mark_buffer_dirty(leaf);
4476 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
4482 * This function splits a single item into two items,
4483 * giving 'new_key' to the new item and splitting the
4484 * old one at split_offset (from the start of the item).
4486 * The path may be released by this operation. After
4487 * the split, the path is pointing to the old item. The
4488 * new item is going to be in the same node as the old one.
4490 * Note, the item being split must be smaller enough to live alone on
4491 * a tree block with room for one extra struct btrfs_item
4493 * This allows us to split the item in place, keeping a lock on the
4494 * leaf the entire time.
4496 int btrfs_split_item(struct btrfs_trans_handle *trans,
4497 struct btrfs_root *root,
4498 struct btrfs_path *path,
4499 struct btrfs_key *new_key,
4500 unsigned long split_offset)
4503 ret = setup_leaf_for_split(trans, root, path,
4504 sizeof(struct btrfs_item));
4508 ret = split_item(trans, root, path, new_key, split_offset);
4513 * This function duplicate a item, giving 'new_key' to the new item.
4514 * It guarantees both items live in the same tree leaf and the new item
4515 * is contiguous with the original item.
4517 * This allows us to split file extent in place, keeping a lock on the
4518 * leaf the entire time.
4520 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4521 struct btrfs_root *root,
4522 struct btrfs_path *path,
4523 struct btrfs_key *new_key)
4525 struct extent_buffer *leaf;
4529 leaf = path->nodes[0];
4530 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4531 ret = setup_leaf_for_split(trans, root, path,
4532 item_size + sizeof(struct btrfs_item));
4537 setup_items_for_insert(root, path, new_key, &item_size,
4538 item_size, item_size +
4539 sizeof(struct btrfs_item), 1);
4540 leaf = path->nodes[0];
4541 memcpy_extent_buffer(leaf,
4542 btrfs_item_ptr_offset(leaf, path->slots[0]),
4543 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4549 * make the item pointed to by the path smaller. new_size indicates
4550 * how small to make it, and from_end tells us if we just chop bytes
4551 * off the end of the item or if we shift the item to chop bytes off
4554 void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
4555 u32 new_size, int from_end)
4558 struct extent_buffer *leaf;
4559 struct btrfs_item *item;
4561 unsigned int data_end;
4562 unsigned int old_data_start;
4563 unsigned int old_size;
4564 unsigned int size_diff;
4566 struct btrfs_map_token token;
4568 btrfs_init_map_token(&token);
4570 leaf = path->nodes[0];
4571 slot = path->slots[0];
4573 old_size = btrfs_item_size_nr(leaf, slot);
4574 if (old_size == new_size)
4577 nritems = btrfs_header_nritems(leaf);
4578 data_end = leaf_data_end(root, leaf);
4580 old_data_start = btrfs_item_offset_nr(leaf, slot);
4582 size_diff = old_size - new_size;
4585 BUG_ON(slot >= nritems);
4588 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4590 /* first correct the data pointers */
4591 for (i = slot; i < nritems; i++) {
4593 item = btrfs_item_nr(i);
4595 ioff = btrfs_token_item_offset(leaf, item, &token);
4596 btrfs_set_token_item_offset(leaf, item,
4597 ioff + size_diff, &token);
4600 /* shift the data */
4602 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4603 data_end + size_diff, btrfs_leaf_data(leaf) +
4604 data_end, old_data_start + new_size - data_end);
4606 struct btrfs_disk_key disk_key;
4609 btrfs_item_key(leaf, &disk_key, slot);
4611 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4613 struct btrfs_file_extent_item *fi;
4615 fi = btrfs_item_ptr(leaf, slot,
4616 struct btrfs_file_extent_item);
4617 fi = (struct btrfs_file_extent_item *)(
4618 (unsigned long)fi - size_diff);
4620 if (btrfs_file_extent_type(leaf, fi) ==
4621 BTRFS_FILE_EXTENT_INLINE) {
4622 ptr = btrfs_item_ptr_offset(leaf, slot);
4623 memmove_extent_buffer(leaf, ptr,
4625 offsetof(struct btrfs_file_extent_item,
4630 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4631 data_end + size_diff, btrfs_leaf_data(leaf) +
4632 data_end, old_data_start - data_end);
4634 offset = btrfs_disk_key_offset(&disk_key);
4635 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4636 btrfs_set_item_key(leaf, &disk_key, slot);
4638 fixup_low_keys(root, path, &disk_key, 1);
4641 item = btrfs_item_nr(slot);
4642 btrfs_set_item_size(leaf, item, new_size);
4643 btrfs_mark_buffer_dirty(leaf);
4645 if (btrfs_leaf_free_space(root, leaf) < 0) {
4646 btrfs_print_leaf(root, leaf);
4652 * make the item pointed to by the path bigger, data_size is the added size.
4654 void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
4658 struct extent_buffer *leaf;
4659 struct btrfs_item *item;
4661 unsigned int data_end;
4662 unsigned int old_data;
4663 unsigned int old_size;
4665 struct btrfs_map_token token;
4667 btrfs_init_map_token(&token);
4669 leaf = path->nodes[0];
4671 nritems = btrfs_header_nritems(leaf);
4672 data_end = leaf_data_end(root, leaf);
4674 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4675 btrfs_print_leaf(root, leaf);
4678 slot = path->slots[0];
4679 old_data = btrfs_item_end_nr(leaf, slot);
4682 if (slot >= nritems) {
4683 btrfs_print_leaf(root, leaf);
4684 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
4690 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4692 /* first correct the data pointers */
4693 for (i = slot; i < nritems; i++) {
4695 item = btrfs_item_nr(i);
4697 ioff = btrfs_token_item_offset(leaf, item, &token);
4698 btrfs_set_token_item_offset(leaf, item,
4699 ioff - data_size, &token);
4702 /* shift the data */
4703 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4704 data_end - data_size, btrfs_leaf_data(leaf) +
4705 data_end, old_data - data_end);
4707 data_end = old_data;
4708 old_size = btrfs_item_size_nr(leaf, slot);
4709 item = btrfs_item_nr(slot);
4710 btrfs_set_item_size(leaf, item, old_size + data_size);
4711 btrfs_mark_buffer_dirty(leaf);
4713 if (btrfs_leaf_free_space(root, leaf) < 0) {
4714 btrfs_print_leaf(root, leaf);
4720 * this is a helper for btrfs_insert_empty_items, the main goal here is
4721 * to save stack depth by doing the bulk of the work in a function
4722 * that doesn't call btrfs_search_slot
4724 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4725 struct btrfs_key *cpu_key, u32 *data_size,
4726 u32 total_data, u32 total_size, int nr)
4728 struct btrfs_item *item;
4731 unsigned int data_end;
4732 struct btrfs_disk_key disk_key;
4733 struct extent_buffer *leaf;
4735 struct btrfs_map_token token;
4737 btrfs_init_map_token(&token);
4739 leaf = path->nodes[0];
4740 slot = path->slots[0];
4742 nritems = btrfs_header_nritems(leaf);
4743 data_end = leaf_data_end(root, leaf);
4745 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4746 btrfs_print_leaf(root, leaf);
4747 btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
4748 total_size, btrfs_leaf_free_space(root, leaf));
4752 if (slot != nritems) {
4753 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4755 if (old_data < data_end) {
4756 btrfs_print_leaf(root, leaf);
4757 btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
4758 slot, old_data, data_end);
4762 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4764 /* first correct the data pointers */
4765 for (i = slot; i < nritems; i++) {
4768 item = btrfs_item_nr( i);
4769 ioff = btrfs_token_item_offset(leaf, item, &token);
4770 btrfs_set_token_item_offset(leaf, item,
4771 ioff - total_data, &token);
4773 /* shift the items */
4774 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4775 btrfs_item_nr_offset(slot),
4776 (nritems - slot) * sizeof(struct btrfs_item));
4778 /* shift the data */
4779 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4780 data_end - total_data, btrfs_leaf_data(leaf) +
4781 data_end, old_data - data_end);
4782 data_end = old_data;
4785 /* setup the item for the new data */
4786 for (i = 0; i < nr; i++) {
4787 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4788 btrfs_set_item_key(leaf, &disk_key, slot + i);
4789 item = btrfs_item_nr(slot + i);
4790 btrfs_set_token_item_offset(leaf, item,
4791 data_end - data_size[i], &token);
4792 data_end -= data_size[i];
4793 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4796 btrfs_set_header_nritems(leaf, nritems + nr);
4799 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4800 fixup_low_keys(root, path, &disk_key, 1);
4802 btrfs_unlock_up_safe(path, 1);
4803 btrfs_mark_buffer_dirty(leaf);
4805 if (btrfs_leaf_free_space(root, leaf) < 0) {
4806 btrfs_print_leaf(root, leaf);
4812 * Given a key and some data, insert items into the tree.
4813 * This does all the path init required, making room in the tree if needed.
4815 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4816 struct btrfs_root *root,
4817 struct btrfs_path *path,
4818 struct btrfs_key *cpu_key, u32 *data_size,
4827 for (i = 0; i < nr; i++)
4828 total_data += data_size[i];
4830 total_size = total_data + (nr * sizeof(struct btrfs_item));
4831 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4837 slot = path->slots[0];
4840 setup_items_for_insert(root, path, cpu_key, data_size,
4841 total_data, total_size, nr);
4846 * Given a key and some data, insert an item into the tree.
4847 * This does all the path init required, making room in the tree if needed.
4849 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4850 *root, struct btrfs_key *cpu_key, void *data, u32
4854 struct btrfs_path *path;
4855 struct extent_buffer *leaf;
4858 path = btrfs_alloc_path();
4861 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4863 leaf = path->nodes[0];
4864 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4865 write_extent_buffer(leaf, data, ptr, data_size);
4866 btrfs_mark_buffer_dirty(leaf);
4868 btrfs_free_path(path);
4873 * delete the pointer from a given node.
4875 * the tree should have been previously balanced so the deletion does not
4878 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4879 int level, int slot)
4881 struct extent_buffer *parent = path->nodes[level];
4885 nritems = btrfs_header_nritems(parent);
4886 if (slot != nritems - 1) {
4888 tree_mod_log_eb_move(root->fs_info, parent, slot,
4889 slot + 1, nritems - slot - 1);
4890 memmove_extent_buffer(parent,
4891 btrfs_node_key_ptr_offset(slot),
4892 btrfs_node_key_ptr_offset(slot + 1),
4893 sizeof(struct btrfs_key_ptr) *
4894 (nritems - slot - 1));
4896 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4897 MOD_LOG_KEY_REMOVE, GFP_NOFS);
4902 btrfs_set_header_nritems(parent, nritems);
4903 if (nritems == 0 && parent == root->node) {
4904 BUG_ON(btrfs_header_level(root->node) != 1);
4905 /* just turn the root into a leaf and break */
4906 btrfs_set_header_level(root->node, 0);
4907 } else if (slot == 0) {
4908 struct btrfs_disk_key disk_key;
4910 btrfs_node_key(parent, &disk_key, 0);
4911 fixup_low_keys(root, path, &disk_key, level + 1);
4913 btrfs_mark_buffer_dirty(parent);
4917 * a helper function to delete the leaf pointed to by path->slots[1] and
4920 * This deletes the pointer in path->nodes[1] and frees the leaf
4921 * block extent. zero is returned if it all worked out, < 0 otherwise.
4923 * The path must have already been setup for deleting the leaf, including
4924 * all the proper balancing. path->nodes[1] must be locked.
4926 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4927 struct btrfs_root *root,
4928 struct btrfs_path *path,
4929 struct extent_buffer *leaf)
4931 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4932 del_ptr(root, path, 1, path->slots[1]);
4935 * btrfs_free_extent is expensive, we want to make sure we
4936 * aren't holding any locks when we call it
4938 btrfs_unlock_up_safe(path, 0);
4940 root_sub_used(root, leaf->len);
4942 extent_buffer_get(leaf);
4943 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4944 free_extent_buffer_stale(leaf);
4947 * delete the item at the leaf level in path. If that empties
4948 * the leaf, remove it from the tree
4950 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4951 struct btrfs_path *path, int slot, int nr)
4953 struct extent_buffer *leaf;
4954 struct btrfs_item *item;
4961 struct btrfs_map_token token;
4963 btrfs_init_map_token(&token);
4965 leaf = path->nodes[0];
4966 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4968 for (i = 0; i < nr; i++)
4969 dsize += btrfs_item_size_nr(leaf, slot + i);
4971 nritems = btrfs_header_nritems(leaf);
4973 if (slot + nr != nritems) {
4974 int data_end = leaf_data_end(root, leaf);
4976 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4978 btrfs_leaf_data(leaf) + data_end,
4979 last_off - data_end);
4981 for (i = slot + nr; i < nritems; i++) {
4984 item = btrfs_item_nr(i);
4985 ioff = btrfs_token_item_offset(leaf, item, &token);
4986 btrfs_set_token_item_offset(leaf, item,
4987 ioff + dsize, &token);
4990 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4991 btrfs_item_nr_offset(slot + nr),
4992 sizeof(struct btrfs_item) *
4993 (nritems - slot - nr));
4995 btrfs_set_header_nritems(leaf, nritems - nr);
4998 /* delete the leaf if we've emptied it */
5000 if (leaf == root->node) {
5001 btrfs_set_header_level(leaf, 0);
5003 btrfs_set_path_blocking(path);
5004 clean_tree_block(trans, root, leaf);
5005 btrfs_del_leaf(trans, root, path, leaf);
5008 int used = leaf_space_used(leaf, 0, nritems);
5010 struct btrfs_disk_key disk_key;
5012 btrfs_item_key(leaf, &disk_key, 0);
5013 fixup_low_keys(root, path, &disk_key, 1);
5016 /* delete the leaf if it is mostly empty */
5017 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
5018 /* push_leaf_left fixes the path.
5019 * make sure the path still points to our leaf
5020 * for possible call to del_ptr below
5022 slot = path->slots[1];
5023 extent_buffer_get(leaf);
5025 btrfs_set_path_blocking(path);
5026 wret = push_leaf_left(trans, root, path, 1, 1,
5028 if (wret < 0 && wret != -ENOSPC)
5031 if (path->nodes[0] == leaf &&
5032 btrfs_header_nritems(leaf)) {
5033 wret = push_leaf_right(trans, root, path, 1,
5035 if (wret < 0 && wret != -ENOSPC)
5039 if (btrfs_header_nritems(leaf) == 0) {
5040 path->slots[1] = slot;
5041 btrfs_del_leaf(trans, root, path, leaf);
5042 free_extent_buffer(leaf);
5045 /* if we're still in the path, make sure
5046 * we're dirty. Otherwise, one of the
5047 * push_leaf functions must have already
5048 * dirtied this buffer
5050 if (path->nodes[0] == leaf)
5051 btrfs_mark_buffer_dirty(leaf);
5052 free_extent_buffer(leaf);
5055 btrfs_mark_buffer_dirty(leaf);
5062 * search the tree again to find a leaf with lesser keys
5063 * returns 0 if it found something or 1 if there are no lesser leaves.
5064 * returns < 0 on io errors.
5066 * This may release the path, and so you may lose any locks held at the
5069 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
5071 struct btrfs_key key;
5072 struct btrfs_disk_key found_key;
5075 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5077 if (key.offset > 0) {
5079 } else if (key.type > 0) {
5081 key.offset = (u64)-1;
5082 } else if (key.objectid > 0) {
5085 key.offset = (u64)-1;
5090 btrfs_release_path(path);
5091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5094 btrfs_item_key(path->nodes[0], &found_key, 0);
5095 ret = comp_keys(&found_key, &key);
5102 * A helper function to walk down the tree starting at min_key, and looking
5103 * for nodes or leaves that are have a minimum transaction id.
5104 * This is used by the btree defrag code, and tree logging
5106 * This does not cow, but it does stuff the starting key it finds back
5107 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5108 * key and get a writable path.
5110 * This does lock as it descends, and path->keep_locks should be set
5111 * to 1 by the caller.
5113 * This honors path->lowest_level to prevent descent past a given level
5116 * min_trans indicates the oldest transaction that you are interested
5117 * in walking through. Any nodes or leaves older than min_trans are
5118 * skipped over (without reading them).
5120 * returns zero if something useful was found, < 0 on error and 1 if there
5121 * was nothing in the tree that matched the search criteria.
5123 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5124 struct btrfs_path *path,
5127 struct extent_buffer *cur;
5128 struct btrfs_key found_key;
5135 WARN_ON(!path->keep_locks);
5137 cur = btrfs_read_lock_root_node(root);
5138 level = btrfs_header_level(cur);
5139 WARN_ON(path->nodes[level]);
5140 path->nodes[level] = cur;
5141 path->locks[level] = BTRFS_READ_LOCK;
5143 if (btrfs_header_generation(cur) < min_trans) {
5148 nritems = btrfs_header_nritems(cur);
5149 level = btrfs_header_level(cur);
5150 sret = bin_search(cur, min_key, level, &slot);
5152 /* at the lowest level, we're done, setup the path and exit */
5153 if (level == path->lowest_level) {
5154 if (slot >= nritems)
5157 path->slots[level] = slot;
5158 btrfs_item_key_to_cpu(cur, &found_key, slot);
5161 if (sret && slot > 0)
5164 * check this node pointer against the min_trans parameters.
5165 * If it is too old, old, skip to the next one.
5167 while (slot < nritems) {
5170 gen = btrfs_node_ptr_generation(cur, slot);
5171 if (gen < min_trans) {
5179 * we didn't find a candidate key in this node, walk forward
5180 * and find another one
5182 if (slot >= nritems) {
5183 path->slots[level] = slot;
5184 btrfs_set_path_blocking(path);
5185 sret = btrfs_find_next_key(root, path, min_key, level,
5188 btrfs_release_path(path);
5194 /* save our key for returning back */
5195 btrfs_node_key_to_cpu(cur, &found_key, slot);
5196 path->slots[level] = slot;
5197 if (level == path->lowest_level) {
5199 unlock_up(path, level, 1, 0, NULL);
5202 btrfs_set_path_blocking(path);
5203 cur = read_node_slot(root, cur, slot);
5204 BUG_ON(!cur); /* -ENOMEM */
5206 btrfs_tree_read_lock(cur);
5208 path->locks[level - 1] = BTRFS_READ_LOCK;
5209 path->nodes[level - 1] = cur;
5210 unlock_up(path, level, 1, 0, NULL);
5211 btrfs_clear_path_blocking(path, NULL, 0);
5215 memcpy(min_key, &found_key, sizeof(found_key));
5216 btrfs_set_path_blocking(path);
5220 static void tree_move_down(struct btrfs_root *root,
5221 struct btrfs_path *path,
5222 int *level, int root_level)
5224 BUG_ON(*level == 0);
5225 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5226 path->slots[*level]);
5227 path->slots[*level - 1] = 0;
5231 static int tree_move_next_or_upnext(struct btrfs_root *root,
5232 struct btrfs_path *path,
5233 int *level, int root_level)
5237 nritems = btrfs_header_nritems(path->nodes[*level]);
5239 path->slots[*level]++;
5241 while (path->slots[*level] >= nritems) {
5242 if (*level == root_level)
5246 path->slots[*level] = 0;
5247 free_extent_buffer(path->nodes[*level]);
5248 path->nodes[*level] = NULL;
5250 path->slots[*level]++;
5252 nritems = btrfs_header_nritems(path->nodes[*level]);
5259 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5262 static int tree_advance(struct btrfs_root *root,
5263 struct btrfs_path *path,
5264 int *level, int root_level,
5266 struct btrfs_key *key)
5270 if (*level == 0 || !allow_down) {
5271 ret = tree_move_next_or_upnext(root, path, level, root_level);
5273 tree_move_down(root, path, level, root_level);
5278 btrfs_item_key_to_cpu(path->nodes[*level], key,
5279 path->slots[*level]);
5281 btrfs_node_key_to_cpu(path->nodes[*level], key,
5282 path->slots[*level]);
5287 static int tree_compare_item(struct btrfs_root *left_root,
5288 struct btrfs_path *left_path,
5289 struct btrfs_path *right_path,
5294 unsigned long off1, off2;
5296 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5297 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5301 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5302 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5303 right_path->slots[0]);
5305 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5307 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5314 #define ADVANCE_ONLY_NEXT -1
5317 * This function compares two trees and calls the provided callback for
5318 * every changed/new/deleted item it finds.
5319 * If shared tree blocks are encountered, whole subtrees are skipped, making
5320 * the compare pretty fast on snapshotted subvolumes.
5322 * This currently works on commit roots only. As commit roots are read only,
5323 * we don't do any locking. The commit roots are protected with transactions.
5324 * Transactions are ended and rejoined when a commit is tried in between.
5326 * This function checks for modifications done to the trees while comparing.
5327 * If it detects a change, it aborts immediately.
5329 int btrfs_compare_trees(struct btrfs_root *left_root,
5330 struct btrfs_root *right_root,
5331 btrfs_changed_cb_t changed_cb, void *ctx)
5335 struct btrfs_path *left_path = NULL;
5336 struct btrfs_path *right_path = NULL;
5337 struct btrfs_key left_key;
5338 struct btrfs_key right_key;
5339 char *tmp_buf = NULL;
5340 int left_root_level;
5341 int right_root_level;
5344 int left_end_reached;
5345 int right_end_reached;
5353 left_path = btrfs_alloc_path();
5358 right_path = btrfs_alloc_path();
5364 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5370 left_path->search_commit_root = 1;
5371 left_path->skip_locking = 1;
5372 right_path->search_commit_root = 1;
5373 right_path->skip_locking = 1;
5376 * Strategy: Go to the first items of both trees. Then do
5378 * If both trees are at level 0
5379 * Compare keys of current items
5380 * If left < right treat left item as new, advance left tree
5382 * If left > right treat right item as deleted, advance right tree
5384 * If left == right do deep compare of items, treat as changed if
5385 * needed, advance both trees and repeat
5386 * If both trees are at the same level but not at level 0
5387 * Compare keys of current nodes/leafs
5388 * If left < right advance left tree and repeat
5389 * If left > right advance right tree and repeat
5390 * If left == right compare blockptrs of the next nodes/leafs
5391 * If they match advance both trees but stay at the same level
5393 * If they don't match advance both trees while allowing to go
5395 * If tree levels are different
5396 * Advance the tree that needs it and repeat
5398 * Advancing a tree means:
5399 * If we are at level 0, try to go to the next slot. If that's not
5400 * possible, go one level up and repeat. Stop when we found a level
5401 * where we could go to the next slot. We may at this point be on a
5404 * If we are not at level 0 and not on shared tree blocks, go one
5407 * If we are not at level 0 and on shared tree blocks, go one slot to
5408 * the right if possible or go up and right.
5411 down_read(&left_root->fs_info->commit_root_sem);
5412 left_level = btrfs_header_level(left_root->commit_root);
5413 left_root_level = left_level;
5414 left_path->nodes[left_level] = left_root->commit_root;
5415 extent_buffer_get(left_path->nodes[left_level]);
5417 right_level = btrfs_header_level(right_root->commit_root);
5418 right_root_level = right_level;
5419 right_path->nodes[right_level] = right_root->commit_root;
5420 extent_buffer_get(right_path->nodes[right_level]);
5421 up_read(&left_root->fs_info->commit_root_sem);
5423 if (left_level == 0)
5424 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5425 &left_key, left_path->slots[left_level]);
5427 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5428 &left_key, left_path->slots[left_level]);
5429 if (right_level == 0)
5430 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5431 &right_key, right_path->slots[right_level]);
5433 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5434 &right_key, right_path->slots[right_level]);
5436 left_end_reached = right_end_reached = 0;
5437 advance_left = advance_right = 0;
5440 if (advance_left && !left_end_reached) {
5441 ret = tree_advance(left_root, left_path, &left_level,
5443 advance_left != ADVANCE_ONLY_NEXT,
5446 left_end_reached = ADVANCE;
5449 if (advance_right && !right_end_reached) {
5450 ret = tree_advance(right_root, right_path, &right_level,
5452 advance_right != ADVANCE_ONLY_NEXT,
5455 right_end_reached = ADVANCE;
5459 if (left_end_reached && right_end_reached) {
5462 } else if (left_end_reached) {
5463 if (right_level == 0) {
5464 ret = changed_cb(left_root, right_root,
5465 left_path, right_path,
5467 BTRFS_COMPARE_TREE_DELETED,
5472 advance_right = ADVANCE;
5474 } else if (right_end_reached) {
5475 if (left_level == 0) {
5476 ret = changed_cb(left_root, right_root,
5477 left_path, right_path,
5479 BTRFS_COMPARE_TREE_NEW,
5484 advance_left = ADVANCE;
5488 if (left_level == 0 && right_level == 0) {
5489 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5491 ret = changed_cb(left_root, right_root,
5492 left_path, right_path,
5494 BTRFS_COMPARE_TREE_NEW,
5498 advance_left = ADVANCE;
5499 } else if (cmp > 0) {
5500 ret = changed_cb(left_root, right_root,
5501 left_path, right_path,
5503 BTRFS_COMPARE_TREE_DELETED,
5507 advance_right = ADVANCE;
5509 enum btrfs_compare_tree_result cmp;
5511 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
5512 ret = tree_compare_item(left_root, left_path,
5513 right_path, tmp_buf);
5515 cmp = BTRFS_COMPARE_TREE_CHANGED;
5517 cmp = BTRFS_COMPARE_TREE_SAME;
5518 ret = changed_cb(left_root, right_root,
5519 left_path, right_path,
5520 &left_key, cmp, ctx);
5523 advance_left = ADVANCE;
5524 advance_right = ADVANCE;
5526 } else if (left_level == right_level) {
5527 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5529 advance_left = ADVANCE;
5530 } else if (cmp > 0) {
5531 advance_right = ADVANCE;
5533 left_blockptr = btrfs_node_blockptr(
5534 left_path->nodes[left_level],
5535 left_path->slots[left_level]);
5536 right_blockptr = btrfs_node_blockptr(
5537 right_path->nodes[right_level],
5538 right_path->slots[right_level]);
5539 left_gen = btrfs_node_ptr_generation(
5540 left_path->nodes[left_level],
5541 left_path->slots[left_level]);
5542 right_gen = btrfs_node_ptr_generation(
5543 right_path->nodes[right_level],
5544 right_path->slots[right_level]);
5545 if (left_blockptr == right_blockptr &&
5546 left_gen == right_gen) {
5548 * As we're on a shared block, don't
5549 * allow to go deeper.
5551 advance_left = ADVANCE_ONLY_NEXT;
5552 advance_right = ADVANCE_ONLY_NEXT;
5554 advance_left = ADVANCE;
5555 advance_right = ADVANCE;
5558 } else if (left_level < right_level) {
5559 advance_right = ADVANCE;
5561 advance_left = ADVANCE;
5566 btrfs_free_path(left_path);
5567 btrfs_free_path(right_path);
5573 * this is similar to btrfs_next_leaf, but does not try to preserve
5574 * and fixup the path. It looks for and returns the next key in the
5575 * tree based on the current path and the min_trans parameters.
5577 * 0 is returned if another key is found, < 0 if there are any errors
5578 * and 1 is returned if there are no higher keys in the tree
5580 * path->keep_locks should be set to 1 on the search made before
5581 * calling this function.
5583 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5584 struct btrfs_key *key, int level, u64 min_trans)
5587 struct extent_buffer *c;
5589 WARN_ON(!path->keep_locks);
5590 while (level < BTRFS_MAX_LEVEL) {
5591 if (!path->nodes[level])
5594 slot = path->slots[level] + 1;
5595 c = path->nodes[level];
5597 if (slot >= btrfs_header_nritems(c)) {
5600 struct btrfs_key cur_key;
5601 if (level + 1 >= BTRFS_MAX_LEVEL ||
5602 !path->nodes[level + 1])
5605 if (path->locks[level + 1]) {
5610 slot = btrfs_header_nritems(c) - 1;
5612 btrfs_item_key_to_cpu(c, &cur_key, slot);
5614 btrfs_node_key_to_cpu(c, &cur_key, slot);
5616 orig_lowest = path->lowest_level;
5617 btrfs_release_path(path);
5618 path->lowest_level = level;
5619 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5621 path->lowest_level = orig_lowest;
5625 c = path->nodes[level];
5626 slot = path->slots[level];
5633 btrfs_item_key_to_cpu(c, key, slot);
5635 u64 gen = btrfs_node_ptr_generation(c, slot);
5637 if (gen < min_trans) {
5641 btrfs_node_key_to_cpu(c, key, slot);
5649 * search the tree again to find a leaf with greater keys
5650 * returns 0 if it found something or 1 if there are no greater leaves.
5651 * returns < 0 on io errors.
5653 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5655 return btrfs_next_old_leaf(root, path, 0);
5658 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5663 struct extent_buffer *c;
5664 struct extent_buffer *next;
5665 struct btrfs_key key;
5668 int old_spinning = path->leave_spinning;
5669 int next_rw_lock = 0;
5671 nritems = btrfs_header_nritems(path->nodes[0]);
5675 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5680 btrfs_release_path(path);
5682 path->keep_locks = 1;
5683 path->leave_spinning = 1;
5686 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5688 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5689 path->keep_locks = 0;
5694 nritems = btrfs_header_nritems(path->nodes[0]);
5696 * by releasing the path above we dropped all our locks. A balance
5697 * could have added more items next to the key that used to be
5698 * at the very end of the block. So, check again here and
5699 * advance the path if there are now more items available.
5701 if (nritems > 0 && path->slots[0] < nritems - 1) {
5708 while (level < BTRFS_MAX_LEVEL) {
5709 if (!path->nodes[level]) {
5714 slot = path->slots[level] + 1;
5715 c = path->nodes[level];
5716 if (slot >= btrfs_header_nritems(c)) {
5718 if (level == BTRFS_MAX_LEVEL) {
5726 btrfs_tree_unlock_rw(next, next_rw_lock);
5727 free_extent_buffer(next);
5731 next_rw_lock = path->locks[level];
5732 ret = read_block_for_search(NULL, root, path, &next, level,
5738 btrfs_release_path(path);
5742 if (!path->skip_locking) {
5743 ret = btrfs_try_tree_read_lock(next);
5744 if (!ret && time_seq) {
5746 * If we don't get the lock, we may be racing
5747 * with push_leaf_left, holding that lock while
5748 * itself waiting for the leaf we've currently
5749 * locked. To solve this situation, we give up
5750 * on our lock and cycle.
5752 free_extent_buffer(next);
5753 btrfs_release_path(path);
5758 btrfs_set_path_blocking(path);
5759 btrfs_tree_read_lock(next);
5760 btrfs_clear_path_blocking(path, next,
5763 next_rw_lock = BTRFS_READ_LOCK;
5767 path->slots[level] = slot;
5770 c = path->nodes[level];
5771 if (path->locks[level])
5772 btrfs_tree_unlock_rw(c, path->locks[level]);
5774 free_extent_buffer(c);
5775 path->nodes[level] = next;
5776 path->slots[level] = 0;
5777 if (!path->skip_locking)
5778 path->locks[level] = next_rw_lock;
5782 ret = read_block_for_search(NULL, root, path, &next, level,
5788 btrfs_release_path(path);
5792 if (!path->skip_locking) {
5793 ret = btrfs_try_tree_read_lock(next);
5795 btrfs_set_path_blocking(path);
5796 btrfs_tree_read_lock(next);
5797 btrfs_clear_path_blocking(path, next,
5800 next_rw_lock = BTRFS_READ_LOCK;
5805 unlock_up(path, 0, 1, 0, NULL);
5806 path->leave_spinning = old_spinning;
5808 btrfs_set_path_blocking(path);
5814 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5815 * searching until it gets past min_objectid or finds an item of 'type'
5817 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5819 int btrfs_previous_item(struct btrfs_root *root,
5820 struct btrfs_path *path, u64 min_objectid,
5823 struct btrfs_key found_key;
5824 struct extent_buffer *leaf;
5829 if (path->slots[0] == 0) {
5830 btrfs_set_path_blocking(path);
5831 ret = btrfs_prev_leaf(root, path);
5837 leaf = path->nodes[0];
5838 nritems = btrfs_header_nritems(leaf);
5841 if (path->slots[0] == nritems)
5844 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5845 if (found_key.objectid < min_objectid)
5847 if (found_key.type == type)
5849 if (found_key.objectid == min_objectid &&
5850 found_key.type < type)
5857 * search in extent tree to find a previous Metadata/Data extent item with
5860 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5862 int btrfs_previous_extent_item(struct btrfs_root *root,
5863 struct btrfs_path *path, u64 min_objectid)
5865 struct btrfs_key found_key;
5866 struct extent_buffer *leaf;
5871 if (path->slots[0] == 0) {
5872 btrfs_set_path_blocking(path);
5873 ret = btrfs_prev_leaf(root, path);
5879 leaf = path->nodes[0];
5880 nritems = btrfs_header_nritems(leaf);
5883 if (path->slots[0] == nritems)
5886 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5887 if (found_key.objectid < min_objectid)
5889 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5890 found_key.type == BTRFS_METADATA_ITEM_KEY)
5892 if (found_key.objectid == min_objectid &&
5893 found_key.type < BTRFS_EXTENT_ITEM_KEY)