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_trans_handle *trans, struct btrfs_root *root,
41 struct btrfs_path *path, int level, int slot,
43 static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44 struct extent_buffer *eb);
45 struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46 u32 blocksize, u64 parent_transid,
48 struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize,
52 struct btrfs_path *btrfs_alloc_path(void)
54 struct btrfs_path *path;
55 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
63 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
67 if (!p->nodes[i] || !p->locks[i])
69 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70 if (p->locks[i] == BTRFS_READ_LOCK)
71 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72 else if (p->locks[i] == BTRFS_WRITE_LOCK)
73 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
78 * reset all the locked nodes in the patch to spinning locks.
80 * held is used to keep lockdep happy, when lockdep is enabled
81 * we set held to a blocking lock before we go around and
82 * retake all the spinlocks in the path. You can safely use NULL
85 noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
86 struct extent_buffer *held, int held_rw)
90 #ifdef CONFIG_DEBUG_LOCK_ALLOC
91 /* lockdep really cares that we take all of these spinlocks
92 * in the right order. If any of the locks in the path are not
93 * currently blocking, it is going to complain. So, make really
94 * really sure by forcing the path to blocking before we clear
98 btrfs_set_lock_blocking_rw(held, held_rw);
99 if (held_rw == BTRFS_WRITE_LOCK)
100 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101 else if (held_rw == BTRFS_READ_LOCK)
102 held_rw = BTRFS_READ_LOCK_BLOCKING;
104 btrfs_set_path_blocking(p);
107 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
108 if (p->nodes[i] && p->locks[i]) {
109 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111 p->locks[i] = BTRFS_WRITE_LOCK;
112 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113 p->locks[i] = BTRFS_READ_LOCK;
117 #ifdef CONFIG_DEBUG_LOCK_ALLOC
119 btrfs_clear_lock_blocking_rw(held, held_rw);
123 /* this also releases the path */
124 void btrfs_free_path(struct btrfs_path *p)
128 btrfs_release_path(p);
129 kmem_cache_free(btrfs_path_cachep, p);
133 * path release drops references on the extent buffers in the path
134 * and it drops any locks held by this path
136 * It is safe to call this on paths that no locks or extent buffers held.
138 noinline void btrfs_release_path(struct btrfs_path *p)
142 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
147 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
150 free_extent_buffer(p->nodes[i]);
156 * safely gets a reference on the root node of a tree. A lock
157 * is not taken, so a concurrent writer may put a different node
158 * at the root of the tree. See btrfs_lock_root_node for the
161 * The extent buffer returned by this has a reference taken, so
162 * it won't disappear. It may stop being the root of the tree
163 * at any time because there are no locks held.
165 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
167 struct extent_buffer *eb;
171 eb = rcu_dereference(root->node);
174 * RCU really hurts here, we could free up the root node because
175 * it was cow'ed but we may not get the new root node yet so do
176 * the inc_not_zero dance and if it doesn't work then
177 * synchronize_rcu and try again.
179 if (atomic_inc_not_zero(&eb->refs)) {
189 /* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
193 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
195 struct extent_buffer *eb;
198 eb = btrfs_root_node(root);
200 if (eb == root->node)
202 btrfs_tree_unlock(eb);
203 free_extent_buffer(eb);
208 /* loop around taking references on and locking the root node of the
209 * tree until you end up with a lock on the root. A locked buffer
210 * is returned, with a reference held.
212 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
214 struct extent_buffer *eb;
217 eb = btrfs_root_node(root);
218 btrfs_tree_read_lock(eb);
219 if (eb == root->node)
221 btrfs_tree_read_unlock(eb);
222 free_extent_buffer(eb);
227 /* cowonly root (everything not a reference counted cow subvolume), just get
228 * put onto a simple dirty list. transaction.c walks this to make sure they
229 * get properly updated on disk.
231 static void add_root_to_dirty_list(struct btrfs_root *root)
233 spin_lock(&root->fs_info->trans_lock);
234 if (root->track_dirty && list_empty(&root->dirty_list)) {
235 list_add(&root->dirty_list,
236 &root->fs_info->dirty_cowonly_roots);
238 spin_unlock(&root->fs_info->trans_lock);
242 * used by snapshot creation to make a copy of a root for a tree with
243 * a given objectid. The buffer with the new root node is returned in
244 * cow_ret, and this func returns zero on success or a negative error code.
246 int btrfs_copy_root(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *buf,
249 struct extent_buffer **cow_ret, u64 new_root_objectid)
251 struct extent_buffer *cow;
254 struct btrfs_disk_key disk_key;
256 WARN_ON(root->ref_cows && trans->transid !=
257 root->fs_info->running_transaction->transid);
258 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
260 level = btrfs_header_level(buf);
262 btrfs_item_key(buf, &disk_key, 0);
264 btrfs_node_key(buf, &disk_key, 0);
266 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
267 new_root_objectid, &disk_key, level,
272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
273 btrfs_set_header_bytenr(cow, cow->start);
274 btrfs_set_header_generation(cow, trans->transid);
275 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
277 BTRFS_HEADER_FLAG_RELOC);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
281 btrfs_set_header_owner(cow, new_root_objectid);
283 write_extent_buffer(cow, root->fs_info->fsid,
284 (unsigned long)btrfs_header_fsid(cow),
287 WARN_ON(btrfs_header_generation(buf) > trans->transid);
288 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
289 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
291 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
296 btrfs_mark_buffer_dirty(cow);
305 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
308 MOD_LOG_ROOT_REPLACE,
311 struct tree_mod_move {
316 struct tree_mod_root {
321 struct tree_mod_elem {
323 u64 index; /* shifted logical */
324 struct seq_list elem;
327 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
330 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
333 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334 struct btrfs_disk_key key;
337 /* this is used for op == MOD_LOG_MOVE_KEYS */
338 struct tree_mod_move move;
340 /* this is used for op == MOD_LOG_ROOT_REPLACE */
341 struct tree_mod_root old_root;
345 __get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
347 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
348 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
351 void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
355 spin_lock(&fs_info->tree_mod_seq_lock);
356 __get_tree_mod_seq(fs_info, elem);
357 spin_unlock(&fs_info->tree_mod_seq_lock);
360 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
361 struct seq_list *elem)
363 struct rb_root *tm_root;
364 struct rb_node *node;
365 struct rb_node *next;
366 struct seq_list *cur_elem;
367 struct tree_mod_elem *tm;
368 u64 min_seq = (u64)-1;
369 u64 seq_putting = elem->seq;
374 BUG_ON(!(elem->flags & 1));
375 spin_lock(&fs_info->tree_mod_seq_lock);
376 list_del(&elem->list);
378 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
379 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
380 if (seq_putting > cur_elem->seq) {
382 * blocker with lower sequence number exists, we
383 * cannot remove anything from the log
387 min_seq = cur_elem->seq;
392 * anything that's lower than the lowest existing (read: blocked)
393 * sequence number can be removed from the tree.
395 write_lock(&fs_info->tree_mod_log_lock);
396 tm_root = &fs_info->tree_mod_log;
397 for (node = rb_first(tm_root); node; node = next) {
398 next = rb_next(node);
399 tm = container_of(node, struct tree_mod_elem, node);
400 if (tm->elem.seq > min_seq)
402 rb_erase(node, tm_root);
403 list_del(&tm->elem.list);
406 write_unlock(&fs_info->tree_mod_log_lock);
408 spin_unlock(&fs_info->tree_mod_seq_lock);
412 * key order of the log:
415 * the index is the shifted logical of the *new* root node for root replace
416 * operations, or the shifted logical of the affected block for all other
420 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
422 struct rb_root *tm_root;
423 struct rb_node **new;
424 struct rb_node *parent = NULL;
425 struct tree_mod_elem *cur;
428 BUG_ON(!tm || !tm->elem.seq);
430 write_lock(&fs_info->tree_mod_log_lock);
431 tm_root = &fs_info->tree_mod_log;
432 new = &tm_root->rb_node;
434 cur = container_of(*new, struct tree_mod_elem, node);
436 if (cur->index < tm->index)
437 new = &((*new)->rb_left);
438 else if (cur->index > tm->index)
439 new = &((*new)->rb_right);
440 else if (cur->elem.seq < tm->elem.seq)
441 new = &((*new)->rb_left);
442 else if (cur->elem.seq > tm->elem.seq)
443 new = &((*new)->rb_right);
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
454 write_unlock(&fs_info->tree_mod_log_lock);
458 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
459 struct extent_buffer *eb) {
461 if (list_empty(&(fs_info)->tree_mod_seq_list))
465 if (btrfs_header_level(eb) == 0)
470 static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
471 struct tree_mod_elem **tm_ret)
473 struct tree_mod_elem *tm;
476 if (tree_mod_dont_log(fs_info, NULL))
479 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
484 spin_lock(&fs_info->tree_mod_seq_lock);
485 if (list_empty(&fs_info->tree_mod_seq_list)) {
487 * someone emptied the list while we were waiting for the lock.
488 * we must not add to the list, because no blocker exists. items
489 * are removed from the list only when the existing blocker is
490 * removed from the list.
495 __get_tree_mod_seq(fs_info, &tm->elem);
498 spin_unlock(&fs_info->tree_mod_seq_lock);
504 tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
505 struct extent_buffer *eb, int slot,
506 enum mod_log_op op, gfp_t flags)
508 struct tree_mod_elem *tm;
511 ret = tree_mod_alloc(fs_info, flags, &tm);
515 tm->index = eb->start >> PAGE_CACHE_SHIFT;
516 if (op != MOD_LOG_KEY_ADD) {
517 btrfs_node_key(eb, &tm->key, slot);
518 tm->blockptr = btrfs_node_blockptr(eb, slot);
522 tm->generation = btrfs_node_ptr_generation(eb, slot);
524 return __tree_mod_log_insert(fs_info, tm);
528 tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
529 int slot, enum mod_log_op op)
531 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
535 tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
536 struct extent_buffer *eb, int dst_slot, int src_slot,
537 int nr_items, gfp_t flags)
539 struct tree_mod_elem *tm;
543 if (tree_mod_dont_log(fs_info, eb))
546 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
547 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
548 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
552 ret = tree_mod_alloc(fs_info, flags, &tm);
556 tm->index = eb->start >> PAGE_CACHE_SHIFT;
558 tm->move.dst_slot = dst_slot;
559 tm->move.nr_items = nr_items;
560 tm->op = MOD_LOG_MOVE_KEYS;
562 return __tree_mod_log_insert(fs_info, tm);
566 tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
567 struct extent_buffer *old_root,
568 struct extent_buffer *new_root, gfp_t flags)
570 struct tree_mod_elem *tm;
573 ret = tree_mod_alloc(fs_info, flags, &tm);
577 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
578 tm->old_root.logical = old_root->start;
579 tm->old_root.level = btrfs_header_level(old_root);
580 tm->generation = btrfs_header_generation(old_root);
581 tm->op = MOD_LOG_ROOT_REPLACE;
583 return __tree_mod_log_insert(fs_info, tm);
586 static struct tree_mod_elem *
587 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
590 struct rb_root *tm_root;
591 struct rb_node *node;
592 struct tree_mod_elem *cur = NULL;
593 struct tree_mod_elem *found = NULL;
594 u64 index = start >> PAGE_CACHE_SHIFT;
596 read_lock(&fs_info->tree_mod_log_lock);
597 tm_root = &fs_info->tree_mod_log;
598 node = tm_root->rb_node;
600 cur = container_of(node, struct tree_mod_elem, node);
601 if (cur->index < index) {
602 node = node->rb_left;
603 } else if (cur->index > index) {
604 node = node->rb_right;
605 } else if (cur->elem.seq < min_seq) {
606 node = node->rb_left;
607 } else if (!smallest) {
608 /* we want the node with the highest seq */
610 BUG_ON(found->elem.seq > cur->elem.seq);
612 node = node->rb_left;
613 } else if (cur->elem.seq > min_seq) {
614 /* we want the node with the smallest seq */
616 BUG_ON(found->elem.seq < cur->elem.seq);
618 node = node->rb_right;
624 read_unlock(&fs_info->tree_mod_log_lock);
630 * this returns the element from the log with the smallest time sequence
631 * value that's in the log (the oldest log item). any element with a time
632 * sequence lower than min_seq will be ignored.
634 static struct tree_mod_elem *
635 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
638 return __tree_mod_log_search(fs_info, start, min_seq, 1);
642 * this returns the element from the log with the largest time sequence
643 * value that's in the log (the most recent log item). any element with
644 * a time sequence lower than min_seq will be ignored.
646 static struct tree_mod_elem *
647 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
649 return __tree_mod_log_search(fs_info, start, min_seq, 0);
653 tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
654 struct extent_buffer *src, unsigned long dst_offset,
655 unsigned long src_offset, int nr_items)
660 if (tree_mod_dont_log(fs_info, NULL))
663 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
666 /* speed this up by single seq for all operations? */
667 for (i = 0; i < nr_items; i++) {
668 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
671 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
678 tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
679 int dst_offset, int src_offset, int nr_items)
682 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
688 tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
689 struct extent_buffer *eb,
690 struct btrfs_disk_key *disk_key, int slot, int atomic)
694 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
696 atomic ? GFP_ATOMIC : GFP_NOFS);
700 static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
701 struct extent_buffer *eb)
707 if (tree_mod_dont_log(fs_info, eb))
710 nritems = btrfs_header_nritems(eb);
711 for (i = nritems - 1; i >= 0; i--) {
712 ret = tree_mod_log_insert_key(fs_info, eb, i,
713 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
719 tree_mod_log_set_root_pointer(struct btrfs_root *root,
720 struct extent_buffer *new_root_node)
723 tree_mod_log_free_eb(root->fs_info, root->node);
724 ret = tree_mod_log_insert_root(root->fs_info, root->node,
725 new_root_node, GFP_NOFS);
730 * check if the tree block can be shared by multiple trees
732 int btrfs_block_can_be_shared(struct btrfs_root *root,
733 struct extent_buffer *buf)
736 * Tree blocks not in refernece counted trees and tree roots
737 * are never shared. If a block was allocated after the last
738 * snapshot and the block was not allocated by tree relocation,
739 * we know the block is not shared.
741 if (root->ref_cows &&
742 buf != root->node && buf != root->commit_root &&
743 (btrfs_header_generation(buf) <=
744 btrfs_root_last_snapshot(&root->root_item) ||
745 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
747 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
748 if (root->ref_cows &&
749 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
755 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root,
757 struct extent_buffer *buf,
758 struct extent_buffer *cow,
768 * Backrefs update rules:
770 * Always use full backrefs for extent pointers in tree block
771 * allocated by tree relocation.
773 * If a shared tree block is no longer referenced by its owner
774 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
775 * use full backrefs for extent pointers in tree block.
777 * If a tree block is been relocating
778 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
779 * use full backrefs for extent pointers in tree block.
780 * The reason for this is some operations (such as drop tree)
781 * are only allowed for blocks use full backrefs.
784 if (btrfs_block_can_be_shared(root, buf)) {
785 ret = btrfs_lookup_extent_info(trans, root, buf->start,
786 buf->len, &refs, &flags);
791 btrfs_std_error(root->fs_info, ret);
796 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
797 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
798 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
803 owner = btrfs_header_owner(buf);
804 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
805 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
808 if ((owner == root->root_key.objectid ||
809 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
810 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
811 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
812 BUG_ON(ret); /* -ENOMEM */
814 if (root->root_key.objectid ==
815 BTRFS_TREE_RELOC_OBJECTID) {
816 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
817 BUG_ON(ret); /* -ENOMEM */
818 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
819 BUG_ON(ret); /* -ENOMEM */
821 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
824 if (root->root_key.objectid ==
825 BTRFS_TREE_RELOC_OBJECTID)
826 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
828 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
829 BUG_ON(ret); /* -ENOMEM */
831 if (new_flags != 0) {
832 ret = btrfs_set_disk_extent_flags(trans, root,
840 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
841 if (root->root_key.objectid ==
842 BTRFS_TREE_RELOC_OBJECTID)
843 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
845 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
846 BUG_ON(ret); /* -ENOMEM */
847 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
848 BUG_ON(ret); /* -ENOMEM */
851 * don't log freeing in case we're freeing the root node, this
852 * is done by tree_mod_log_set_root_pointer later
854 if (buf != root->node && btrfs_header_level(buf) != 0)
855 tree_mod_log_free_eb(root->fs_info, buf);
856 clean_tree_block(trans, root, buf);
863 * does the dirty work in cow of a single block. The parent block (if
864 * supplied) is updated to point to the new cow copy. The new buffer is marked
865 * dirty and returned locked. If you modify the block it needs to be marked
868 * search_start -- an allocation hint for the new block
870 * empty_size -- a hint that you plan on doing more cow. This is the size in
871 * bytes the allocator should try to find free next to the block it returns.
872 * This is just a hint and may be ignored by the allocator.
874 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
875 struct btrfs_root *root,
876 struct extent_buffer *buf,
877 struct extent_buffer *parent, int parent_slot,
878 struct extent_buffer **cow_ret,
879 u64 search_start, u64 empty_size)
881 struct btrfs_disk_key disk_key;
882 struct extent_buffer *cow;
891 btrfs_assert_tree_locked(buf);
893 WARN_ON(root->ref_cows && trans->transid !=
894 root->fs_info->running_transaction->transid);
895 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
897 level = btrfs_header_level(buf);
900 btrfs_item_key(buf, &disk_key, 0);
902 btrfs_node_key(buf, &disk_key, 0);
904 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
906 parent_start = parent->start;
912 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
913 root->root_key.objectid, &disk_key,
914 level, search_start, empty_size);
918 /* cow is set to blocking by btrfs_init_new_buffer */
920 copy_extent_buffer(cow, buf, 0, 0, cow->len);
921 btrfs_set_header_bytenr(cow, cow->start);
922 btrfs_set_header_generation(cow, trans->transid);
923 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
924 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
925 BTRFS_HEADER_FLAG_RELOC);
926 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
927 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
929 btrfs_set_header_owner(cow, root->root_key.objectid);
931 write_extent_buffer(cow, root->fs_info->fsid,
932 (unsigned long)btrfs_header_fsid(cow),
935 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
937 btrfs_abort_transaction(trans, root, ret);
942 btrfs_reloc_cow_block(trans, root, buf, cow);
944 if (buf == root->node) {
945 WARN_ON(parent && parent != buf);
946 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
947 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
948 parent_start = buf->start;
952 extent_buffer_get(cow);
953 tree_mod_log_set_root_pointer(root, cow);
954 rcu_assign_pointer(root->node, cow);
956 btrfs_free_tree_block(trans, root, buf, parent_start,
958 free_extent_buffer(buf);
959 add_root_to_dirty_list(root);
961 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
962 parent_start = parent->start;
966 WARN_ON(trans->transid != btrfs_header_generation(parent));
967 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
968 MOD_LOG_KEY_REPLACE);
969 btrfs_set_node_blockptr(parent, parent_slot,
971 btrfs_set_node_ptr_generation(parent, parent_slot,
973 btrfs_mark_buffer_dirty(parent);
974 btrfs_free_tree_block(trans, root, buf, parent_start,
978 btrfs_tree_unlock(buf);
979 free_extent_buffer_stale(buf);
980 btrfs_mark_buffer_dirty(cow);
986 * returns the logical address of the oldest predecessor of the given root.
987 * entries older than time_seq are ignored.
989 static struct tree_mod_elem *
990 __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
991 struct btrfs_root *root, u64 time_seq)
993 struct tree_mod_elem *tm;
994 struct tree_mod_elem *found = NULL;
995 u64 root_logical = root->node->start;
1002 * the very last operation that's logged for a root is the replacement
1003 * operation (if it is replaced at all). this has the index of the *new*
1004 * root, making it the very first operation that's logged for this root.
1007 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1012 * we must have key remove operations in the log before the
1013 * replace operation.
1017 if (tm->op != MOD_LOG_ROOT_REPLACE)
1021 root_logical = tm->old_root.logical;
1022 BUG_ON(root_logical == root->node->start);
1026 /* if there's no old root to return, return what we found instead */
1034 * tm is a pointer to the first operation to rewind within eb. then, all
1035 * previous operations will be rewinded (until we reach something older than
1039 __tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1040 struct tree_mod_elem *first_tm)
1043 struct rb_node *next;
1044 struct tree_mod_elem *tm = first_tm;
1045 unsigned long o_dst;
1046 unsigned long o_src;
1047 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1049 n = btrfs_header_nritems(eb);
1050 while (tm && tm->elem.seq >= time_seq) {
1052 * all the operations are recorded with the operator used for
1053 * the modification. as we're going backwards, we do the
1054 * opposite of each operation here.
1057 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1058 BUG_ON(tm->slot < n);
1059 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1060 case MOD_LOG_KEY_REMOVE:
1061 btrfs_set_node_key(eb, &tm->key, tm->slot);
1062 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1063 btrfs_set_node_ptr_generation(eb, tm->slot,
1067 case MOD_LOG_KEY_REPLACE:
1068 BUG_ON(tm->slot >= n);
1069 btrfs_set_node_key(eb, &tm->key, tm->slot);
1070 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1071 btrfs_set_node_ptr_generation(eb, tm->slot,
1074 case MOD_LOG_KEY_ADD:
1075 if (tm->slot != n - 1) {
1076 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1077 o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
1078 memmove_extent_buffer(eb, o_dst, o_src, p_size);
1082 case MOD_LOG_MOVE_KEYS:
1083 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1084 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1085 memmove_extent_buffer(eb, o_dst, o_src,
1086 tm->move.nr_items * p_size);
1088 case MOD_LOG_ROOT_REPLACE:
1090 * this operation is special. for roots, this must be
1091 * handled explicitly before rewinding.
1092 * for non-roots, this operation may exist if the node
1093 * was a root: root A -> child B; then A gets empty and
1094 * B is promoted to the new root. in the mod log, we'll
1095 * have a root-replace operation for B, a tree block
1096 * that is no root. we simply ignore that operation.
1100 next = rb_next(&tm->node);
1103 tm = container_of(next, struct tree_mod_elem, node);
1104 if (tm->index != first_tm->index)
1107 btrfs_set_header_nritems(eb, n);
1110 static struct extent_buffer *
1111 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1114 struct extent_buffer *eb_rewin;
1115 struct tree_mod_elem *tm;
1120 if (btrfs_header_level(eb) == 0)
1123 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1127 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1128 BUG_ON(tm->slot != 0);
1129 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1130 fs_info->tree_root->nodesize);
1132 btrfs_set_header_bytenr(eb_rewin, eb->start);
1133 btrfs_set_header_backref_rev(eb_rewin,
1134 btrfs_header_backref_rev(eb));
1135 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1136 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1138 eb_rewin = btrfs_clone_extent_buffer(eb);
1142 extent_buffer_get(eb_rewin);
1143 free_extent_buffer(eb);
1145 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1151 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1152 * value. If there are no changes, the current root->root_node is returned. If
1153 * anything changed in between, there's a fresh buffer allocated on which the
1154 * rewind operations are done. In any case, the returned buffer is read locked.
1155 * Returns NULL on error (with no locks held).
1157 static inline struct extent_buffer *
1158 get_old_root(struct btrfs_root *root, u64 time_seq)
1160 struct tree_mod_elem *tm;
1161 struct extent_buffer *eb;
1162 struct tree_mod_root *old_root = NULL;
1166 eb = btrfs_read_lock_root_node(root);
1167 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1171 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1172 old_root = &tm->old_root;
1173 old_generation = tm->generation;
1174 logical = old_root->logical;
1176 logical = root->node->start;
1179 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
1181 * there was an item in the log when __tree_mod_log_oldest_root
1182 * returned. this one must not go away, because the time_seq passed to
1183 * us must be blocking its removal.
1188 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1191 eb = btrfs_clone_extent_buffer(root->node);
1192 btrfs_tree_read_unlock(root->node);
1193 free_extent_buffer(root->node);
1196 btrfs_tree_read_lock(eb);
1198 btrfs_set_header_bytenr(eb, eb->start);
1199 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1200 btrfs_set_header_owner(eb, root->root_key.objectid);
1201 btrfs_set_header_level(eb, old_root->level);
1202 btrfs_set_header_generation(eb, old_generation);
1204 __tree_mod_log_rewind(eb, time_seq, tm);
1205 extent_buffer_get(eb);
1210 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1211 struct btrfs_root *root,
1212 struct extent_buffer *buf)
1214 /* ensure we can see the force_cow */
1218 * We do not need to cow a block if
1219 * 1) this block is not created or changed in this transaction;
1220 * 2) this block does not belong to TREE_RELOC tree;
1221 * 3) the root is not forced COW.
1223 * What is forced COW:
1224 * when we create snapshot during commiting the transaction,
1225 * after we've finished coping src root, we must COW the shared
1226 * block to ensure the metadata consistency.
1228 if (btrfs_header_generation(buf) == trans->transid &&
1229 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1230 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1231 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1238 * cows a single block, see __btrfs_cow_block for the real work.
1239 * This version of it has extra checks so that a block isn't cow'd more than
1240 * once per transaction, as long as it hasn't been written yet
1242 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1243 struct btrfs_root *root, struct extent_buffer *buf,
1244 struct extent_buffer *parent, int parent_slot,
1245 struct extent_buffer **cow_ret)
1250 if (trans->transaction != root->fs_info->running_transaction) {
1251 printk(KERN_CRIT "trans %llu running %llu\n",
1252 (unsigned long long)trans->transid,
1253 (unsigned long long)
1254 root->fs_info->running_transaction->transid);
1257 if (trans->transid != root->fs_info->generation) {
1258 printk(KERN_CRIT "trans %llu running %llu\n",
1259 (unsigned long long)trans->transid,
1260 (unsigned long long)root->fs_info->generation);
1264 if (!should_cow_block(trans, root, buf)) {
1269 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
1272 btrfs_set_lock_blocking(parent);
1273 btrfs_set_lock_blocking(buf);
1275 ret = __btrfs_cow_block(trans, root, buf, parent,
1276 parent_slot, cow_ret, search_start, 0);
1278 trace_btrfs_cow_block(root, buf, *cow_ret);
1284 * helper function for defrag to decide if two blocks pointed to by a
1285 * node are actually close by
1287 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1289 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1291 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1297 * compare two keys in a memcmp fashion
1299 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1301 struct btrfs_key k1;
1303 btrfs_disk_key_to_cpu(&k1, disk);
1305 return btrfs_comp_cpu_keys(&k1, k2);
1309 * same as comp_keys only with two btrfs_key's
1311 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
1313 if (k1->objectid > k2->objectid)
1315 if (k1->objectid < k2->objectid)
1317 if (k1->type > k2->type)
1319 if (k1->type < k2->type)
1321 if (k1->offset > k2->offset)
1323 if (k1->offset < k2->offset)
1329 * this is used by the defrag code to go through all the
1330 * leaves pointed to by a node and reallocate them so that
1331 * disk order is close to key order
1333 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1334 struct btrfs_root *root, struct extent_buffer *parent,
1335 int start_slot, int cache_only, u64 *last_ret,
1336 struct btrfs_key *progress)
1338 struct extent_buffer *cur;
1341 u64 search_start = *last_ret;
1351 int progress_passed = 0;
1352 struct btrfs_disk_key disk_key;
1354 parent_level = btrfs_header_level(parent);
1355 if (cache_only && parent_level != 1)
1358 if (trans->transaction != root->fs_info->running_transaction)
1360 if (trans->transid != root->fs_info->generation)
1363 parent_nritems = btrfs_header_nritems(parent);
1364 blocksize = btrfs_level_size(root, parent_level - 1);
1365 end_slot = parent_nritems;
1367 if (parent_nritems == 1)
1370 btrfs_set_lock_blocking(parent);
1372 for (i = start_slot; i < end_slot; i++) {
1375 btrfs_node_key(parent, &disk_key, i);
1376 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1379 progress_passed = 1;
1380 blocknr = btrfs_node_blockptr(parent, i);
1381 gen = btrfs_node_ptr_generation(parent, i);
1382 if (last_block == 0)
1383 last_block = blocknr;
1386 other = btrfs_node_blockptr(parent, i - 1);
1387 close = close_blocks(blocknr, other, blocksize);
1389 if (!close && i < end_slot - 2) {
1390 other = btrfs_node_blockptr(parent, i + 1);
1391 close = close_blocks(blocknr, other, blocksize);
1394 last_block = blocknr;
1398 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1400 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1403 if (!cur || !uptodate) {
1405 free_extent_buffer(cur);
1409 cur = read_tree_block(root, blocknr,
1413 } else if (!uptodate) {
1414 err = btrfs_read_buffer(cur, gen);
1416 free_extent_buffer(cur);
1421 if (search_start == 0)
1422 search_start = last_block;
1424 btrfs_tree_lock(cur);
1425 btrfs_set_lock_blocking(cur);
1426 err = __btrfs_cow_block(trans, root, cur, parent, i,
1429 (end_slot - i) * blocksize));
1431 btrfs_tree_unlock(cur);
1432 free_extent_buffer(cur);
1435 search_start = cur->start;
1436 last_block = cur->start;
1437 *last_ret = search_start;
1438 btrfs_tree_unlock(cur);
1439 free_extent_buffer(cur);
1445 * The leaf data grows from end-to-front in the node.
1446 * this returns the address of the start of the last item,
1447 * which is the stop of the leaf data stack
1449 static inline unsigned int leaf_data_end(struct btrfs_root *root,
1450 struct extent_buffer *leaf)
1452 u32 nr = btrfs_header_nritems(leaf);
1454 return BTRFS_LEAF_DATA_SIZE(root);
1455 return btrfs_item_offset_nr(leaf, nr - 1);
1460 * search for key in the extent_buffer. The items start at offset p,
1461 * and they are item_size apart. There are 'max' items in p.
1463 * the slot in the array is returned via slot, and it points to
1464 * the place where you would insert key if it is not found in
1467 * slot may point to max if the key is bigger than all of the keys
1469 static noinline int generic_bin_search(struct extent_buffer *eb,
1471 int item_size, struct btrfs_key *key,
1478 struct btrfs_disk_key *tmp = NULL;
1479 struct btrfs_disk_key unaligned;
1480 unsigned long offset;
1482 unsigned long map_start = 0;
1483 unsigned long map_len = 0;
1486 while (low < high) {
1487 mid = (low + high) / 2;
1488 offset = p + mid * item_size;
1490 if (!kaddr || offset < map_start ||
1491 (offset + sizeof(struct btrfs_disk_key)) >
1492 map_start + map_len) {
1494 err = map_private_extent_buffer(eb, offset,
1495 sizeof(struct btrfs_disk_key),
1496 &kaddr, &map_start, &map_len);
1499 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1502 read_extent_buffer(eb, &unaligned,
1503 offset, sizeof(unaligned));
1508 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1511 ret = comp_keys(tmp, key);
1527 * simple bin_search frontend that does the right thing for
1530 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1531 int level, int *slot)
1534 return generic_bin_search(eb,
1535 offsetof(struct btrfs_leaf, items),
1536 sizeof(struct btrfs_item),
1537 key, btrfs_header_nritems(eb),
1540 return generic_bin_search(eb,
1541 offsetof(struct btrfs_node, ptrs),
1542 sizeof(struct btrfs_key_ptr),
1543 key, btrfs_header_nritems(eb),
1547 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1548 int level, int *slot)
1550 return bin_search(eb, key, level, slot);
1553 static void root_add_used(struct btrfs_root *root, u32 size)
1555 spin_lock(&root->accounting_lock);
1556 btrfs_set_root_used(&root->root_item,
1557 btrfs_root_used(&root->root_item) + size);
1558 spin_unlock(&root->accounting_lock);
1561 static void root_sub_used(struct btrfs_root *root, u32 size)
1563 spin_lock(&root->accounting_lock);
1564 btrfs_set_root_used(&root->root_item,
1565 btrfs_root_used(&root->root_item) - size);
1566 spin_unlock(&root->accounting_lock);
1569 /* given a node and slot number, this reads the blocks it points to. The
1570 * extent buffer is returned with a reference taken (but unlocked).
1571 * NULL is returned on error.
1573 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
1574 struct extent_buffer *parent, int slot)
1576 int level = btrfs_header_level(parent);
1579 if (slot >= btrfs_header_nritems(parent))
1584 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
1585 btrfs_level_size(root, level - 1),
1586 btrfs_node_ptr_generation(parent, slot));
1590 * node level balancing, used to make sure nodes are in proper order for
1591 * item deletion. We balance from the top down, so we have to make sure
1592 * that a deletion won't leave an node completely empty later on.
1594 static noinline int balance_level(struct btrfs_trans_handle *trans,
1595 struct btrfs_root *root,
1596 struct btrfs_path *path, int level)
1598 struct extent_buffer *right = NULL;
1599 struct extent_buffer *mid;
1600 struct extent_buffer *left = NULL;
1601 struct extent_buffer *parent = NULL;
1605 int orig_slot = path->slots[level];
1611 mid = path->nodes[level];
1613 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1614 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1615 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1617 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1619 if (level < BTRFS_MAX_LEVEL - 1) {
1620 parent = path->nodes[level + 1];
1621 pslot = path->slots[level + 1];
1625 * deal with the case where there is only one pointer in the root
1626 * by promoting the node below to a root
1629 struct extent_buffer *child;
1631 if (btrfs_header_nritems(mid) != 1)
1634 /* promote the child to a root */
1635 child = read_node_slot(root, mid, 0);
1638 btrfs_std_error(root->fs_info, ret);
1642 btrfs_tree_lock(child);
1643 btrfs_set_lock_blocking(child);
1644 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1646 btrfs_tree_unlock(child);
1647 free_extent_buffer(child);
1651 tree_mod_log_set_root_pointer(root, child);
1652 rcu_assign_pointer(root->node, child);
1654 add_root_to_dirty_list(root);
1655 btrfs_tree_unlock(child);
1657 path->locks[level] = 0;
1658 path->nodes[level] = NULL;
1659 clean_tree_block(trans, root, mid);
1660 btrfs_tree_unlock(mid);
1661 /* once for the path */
1662 free_extent_buffer(mid);
1664 root_sub_used(root, mid->len);
1665 btrfs_free_tree_block(trans, root, mid, 0, 1);
1666 /* once for the root ptr */
1667 free_extent_buffer_stale(mid);
1670 if (btrfs_header_nritems(mid) >
1671 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
1674 left = read_node_slot(root, parent, pslot - 1);
1676 btrfs_tree_lock(left);
1677 btrfs_set_lock_blocking(left);
1678 wret = btrfs_cow_block(trans, root, left,
1679 parent, pslot - 1, &left);
1685 right = read_node_slot(root, parent, pslot + 1);
1687 btrfs_tree_lock(right);
1688 btrfs_set_lock_blocking(right);
1689 wret = btrfs_cow_block(trans, root, right,
1690 parent, pslot + 1, &right);
1697 /* first, try to make some room in the middle buffer */
1699 orig_slot += btrfs_header_nritems(left);
1700 wret = push_node_left(trans, root, left, mid, 1);
1706 * then try to empty the right most buffer into the middle
1709 wret = push_node_left(trans, root, mid, right, 1);
1710 if (wret < 0 && wret != -ENOSPC)
1712 if (btrfs_header_nritems(right) == 0) {
1713 clean_tree_block(trans, root, right);
1714 btrfs_tree_unlock(right);
1715 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
1716 root_sub_used(root, right->len);
1717 btrfs_free_tree_block(trans, root, right, 0, 1);
1718 free_extent_buffer_stale(right);
1721 struct btrfs_disk_key right_key;
1722 btrfs_node_key(right, &right_key, 0);
1723 tree_mod_log_set_node_key(root->fs_info, parent,
1724 &right_key, pslot + 1, 0);
1725 btrfs_set_node_key(parent, &right_key, pslot + 1);
1726 btrfs_mark_buffer_dirty(parent);
1729 if (btrfs_header_nritems(mid) == 1) {
1731 * we're not allowed to leave a node with one item in the
1732 * tree during a delete. A deletion from lower in the tree
1733 * could try to delete the only pointer in this node.
1734 * So, pull some keys from the left.
1735 * There has to be a left pointer at this point because
1736 * otherwise we would have pulled some pointers from the
1741 btrfs_std_error(root->fs_info, ret);
1744 wret = balance_node_right(trans, root, mid, left);
1750 wret = push_node_left(trans, root, left, mid, 1);
1756 if (btrfs_header_nritems(mid) == 0) {
1757 clean_tree_block(trans, root, mid);
1758 btrfs_tree_unlock(mid);
1759 del_ptr(trans, root, path, level + 1, pslot, 1);
1760 root_sub_used(root, mid->len);
1761 btrfs_free_tree_block(trans, root, mid, 0, 1);
1762 free_extent_buffer_stale(mid);
1765 /* update the parent key to reflect our changes */
1766 struct btrfs_disk_key mid_key;
1767 btrfs_node_key(mid, &mid_key, 0);
1768 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1770 btrfs_set_node_key(parent, &mid_key, pslot);
1771 btrfs_mark_buffer_dirty(parent);
1774 /* update the path */
1776 if (btrfs_header_nritems(left) > orig_slot) {
1777 extent_buffer_get(left);
1778 /* left was locked after cow */
1779 path->nodes[level] = left;
1780 path->slots[level + 1] -= 1;
1781 path->slots[level] = orig_slot;
1783 btrfs_tree_unlock(mid);
1784 free_extent_buffer(mid);
1787 orig_slot -= btrfs_header_nritems(left);
1788 path->slots[level] = orig_slot;
1791 /* double check we haven't messed things up */
1793 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1797 btrfs_tree_unlock(right);
1798 free_extent_buffer(right);
1801 if (path->nodes[level] != left)
1802 btrfs_tree_unlock(left);
1803 free_extent_buffer(left);
1808 /* Node balancing for insertion. Here we only split or push nodes around
1809 * when they are completely full. This is also done top down, so we
1810 * have to be pessimistic.
1812 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1813 struct btrfs_root *root,
1814 struct btrfs_path *path, int level)
1816 struct extent_buffer *right = NULL;
1817 struct extent_buffer *mid;
1818 struct extent_buffer *left = NULL;
1819 struct extent_buffer *parent = NULL;
1823 int orig_slot = path->slots[level];
1828 mid = path->nodes[level];
1829 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1831 if (level < BTRFS_MAX_LEVEL - 1) {
1832 parent = path->nodes[level + 1];
1833 pslot = path->slots[level + 1];
1839 left = read_node_slot(root, parent, pslot - 1);
1841 /* first, try to make some room in the middle buffer */
1845 btrfs_tree_lock(left);
1846 btrfs_set_lock_blocking(left);
1848 left_nr = btrfs_header_nritems(left);
1849 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1852 ret = btrfs_cow_block(trans, root, left, parent,
1857 wret = push_node_left(trans, root,
1864 struct btrfs_disk_key disk_key;
1865 orig_slot += left_nr;
1866 btrfs_node_key(mid, &disk_key, 0);
1867 tree_mod_log_set_node_key(root->fs_info, parent,
1868 &disk_key, pslot, 0);
1869 btrfs_set_node_key(parent, &disk_key, pslot);
1870 btrfs_mark_buffer_dirty(parent);
1871 if (btrfs_header_nritems(left) > orig_slot) {
1872 path->nodes[level] = left;
1873 path->slots[level + 1] -= 1;
1874 path->slots[level] = orig_slot;
1875 btrfs_tree_unlock(mid);
1876 free_extent_buffer(mid);
1879 btrfs_header_nritems(left);
1880 path->slots[level] = orig_slot;
1881 btrfs_tree_unlock(left);
1882 free_extent_buffer(left);
1886 btrfs_tree_unlock(left);
1887 free_extent_buffer(left);
1889 right = read_node_slot(root, parent, pslot + 1);
1892 * then try to empty the right most buffer into the middle
1897 btrfs_tree_lock(right);
1898 btrfs_set_lock_blocking(right);
1900 right_nr = btrfs_header_nritems(right);
1901 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1904 ret = btrfs_cow_block(trans, root, right,
1910 wret = balance_node_right(trans, root,
1917 struct btrfs_disk_key disk_key;
1919 btrfs_node_key(right, &disk_key, 0);
1920 tree_mod_log_set_node_key(root->fs_info, parent,
1921 &disk_key, pslot + 1, 0);
1922 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1923 btrfs_mark_buffer_dirty(parent);
1925 if (btrfs_header_nritems(mid) <= orig_slot) {
1926 path->nodes[level] = right;
1927 path->slots[level + 1] += 1;
1928 path->slots[level] = orig_slot -
1929 btrfs_header_nritems(mid);
1930 btrfs_tree_unlock(mid);
1931 free_extent_buffer(mid);
1933 btrfs_tree_unlock(right);
1934 free_extent_buffer(right);
1938 btrfs_tree_unlock(right);
1939 free_extent_buffer(right);
1945 * readahead one full node of leaves, finding things that are close
1946 * to the block in 'slot', and triggering ra on them.
1948 static void reada_for_search(struct btrfs_root *root,
1949 struct btrfs_path *path,
1950 int level, int slot, u64 objectid)
1952 struct extent_buffer *node;
1953 struct btrfs_disk_key disk_key;
1959 int direction = path->reada;
1960 struct extent_buffer *eb;
1968 if (!path->nodes[level])
1971 node = path->nodes[level];
1973 search = btrfs_node_blockptr(node, slot);
1974 blocksize = btrfs_level_size(root, level - 1);
1975 eb = btrfs_find_tree_block(root, search, blocksize);
1977 free_extent_buffer(eb);
1983 nritems = btrfs_header_nritems(node);
1987 if (direction < 0) {
1991 } else if (direction > 0) {
1996 if (path->reada < 0 && objectid) {
1997 btrfs_node_key(node, &disk_key, nr);
1998 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2001 search = btrfs_node_blockptr(node, nr);
2002 if ((search <= target && target - search <= 65536) ||
2003 (search > target && search - target <= 65536)) {
2004 gen = btrfs_node_ptr_generation(node, nr);
2005 readahead_tree_block(root, search, blocksize, gen);
2009 if ((nread > 65536 || nscan > 32))
2015 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2018 static noinline int reada_for_balance(struct btrfs_root *root,
2019 struct btrfs_path *path, int level)
2023 struct extent_buffer *parent;
2024 struct extent_buffer *eb;
2031 parent = path->nodes[level + 1];
2035 nritems = btrfs_header_nritems(parent);
2036 slot = path->slots[level + 1];
2037 blocksize = btrfs_level_size(root, level);
2040 block1 = btrfs_node_blockptr(parent, slot - 1);
2041 gen = btrfs_node_ptr_generation(parent, slot - 1);
2042 eb = btrfs_find_tree_block(root, block1, blocksize);
2044 * if we get -eagain from btrfs_buffer_uptodate, we
2045 * don't want to return eagain here. That will loop
2048 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2050 free_extent_buffer(eb);
2052 if (slot + 1 < nritems) {
2053 block2 = btrfs_node_blockptr(parent, slot + 1);
2054 gen = btrfs_node_ptr_generation(parent, slot + 1);
2055 eb = btrfs_find_tree_block(root, block2, blocksize);
2056 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2058 free_extent_buffer(eb);
2060 if (block1 || block2) {
2063 /* release the whole path */
2064 btrfs_release_path(path);
2066 /* read the blocks */
2068 readahead_tree_block(root, block1, blocksize, 0);
2070 readahead_tree_block(root, block2, blocksize, 0);
2073 eb = read_tree_block(root, block1, blocksize, 0);
2074 free_extent_buffer(eb);
2077 eb = read_tree_block(root, block2, blocksize, 0);
2078 free_extent_buffer(eb);
2086 * when we walk down the tree, it is usually safe to unlock the higher layers
2087 * in the tree. The exceptions are when our path goes through slot 0, because
2088 * operations on the tree might require changing key pointers higher up in the
2091 * callers might also have set path->keep_locks, which tells this code to keep
2092 * the lock if the path points to the last slot in the block. This is part of
2093 * walking through the tree, and selecting the next slot in the higher block.
2095 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2096 * if lowest_unlock is 1, level 0 won't be unlocked
2098 static noinline void unlock_up(struct btrfs_path *path, int level,
2099 int lowest_unlock, int min_write_lock_level,
2100 int *write_lock_level)
2103 int skip_level = level;
2105 struct extent_buffer *t;
2107 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2108 if (!path->nodes[i])
2110 if (!path->locks[i])
2112 if (!no_skips && path->slots[i] == 0) {
2116 if (!no_skips && path->keep_locks) {
2119 nritems = btrfs_header_nritems(t);
2120 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2125 if (skip_level < i && i >= lowest_unlock)
2129 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
2130 btrfs_tree_unlock_rw(t, path->locks[i]);
2132 if (write_lock_level &&
2133 i > min_write_lock_level &&
2134 i <= *write_lock_level) {
2135 *write_lock_level = i - 1;
2142 * This releases any locks held in the path starting at level and
2143 * going all the way up to the root.
2145 * btrfs_search_slot will keep the lock held on higher nodes in a few
2146 * corner cases, such as COW of the block at slot zero in the node. This
2147 * ignores those rules, and it should only be called when there are no
2148 * more updates to be done higher up in the tree.
2150 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2154 if (path->keep_locks)
2157 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2158 if (!path->nodes[i])
2160 if (!path->locks[i])
2162 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2168 * helper function for btrfs_search_slot. The goal is to find a block
2169 * in cache without setting the path to blocking. If we find the block
2170 * we return zero and the path is unchanged.
2172 * If we can't find the block, we set the path blocking and do some
2173 * reada. -EAGAIN is returned and the search must be repeated.
2176 read_block_for_search(struct btrfs_trans_handle *trans,
2177 struct btrfs_root *root, struct btrfs_path *p,
2178 struct extent_buffer **eb_ret, int level, int slot,
2179 struct btrfs_key *key, u64 time_seq)
2184 struct extent_buffer *b = *eb_ret;
2185 struct extent_buffer *tmp;
2188 blocknr = btrfs_node_blockptr(b, slot);
2189 gen = btrfs_node_ptr_generation(b, slot);
2190 blocksize = btrfs_level_size(root, level - 1);
2192 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
2194 /* first we do an atomic uptodate check */
2195 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2196 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2198 * we found an up to date block without
2205 /* the pages were up to date, but we failed
2206 * the generation number check. Do a full
2207 * read for the generation number that is correct.
2208 * We must do this without dropping locks so
2209 * we can trust our generation number
2211 free_extent_buffer(tmp);
2212 btrfs_set_path_blocking(p);
2214 /* now we're allowed to do a blocking uptodate check */
2215 tmp = read_tree_block(root, blocknr, blocksize, gen);
2216 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
2220 free_extent_buffer(tmp);
2221 btrfs_release_path(p);
2227 * reduce lock contention at high levels
2228 * of the btree by dropping locks before
2229 * we read. Don't release the lock on the current
2230 * level because we need to walk this node to figure
2231 * out which blocks to read.
2233 btrfs_unlock_up_safe(p, level + 1);
2234 btrfs_set_path_blocking(p);
2236 free_extent_buffer(tmp);
2238 reada_for_search(root, p, level, slot, key->objectid);
2240 btrfs_release_path(p);
2243 tmp = read_tree_block(root, blocknr, blocksize, 0);
2246 * If the read above didn't mark this buffer up to date,
2247 * it will never end up being up to date. Set ret to EIO now
2248 * and give up so that our caller doesn't loop forever
2251 if (!btrfs_buffer_uptodate(tmp, 0, 0))
2253 free_extent_buffer(tmp);
2259 * helper function for btrfs_search_slot. This does all of the checks
2260 * for node-level blocks and does any balancing required based on
2263 * If no extra work was required, zero is returned. If we had to
2264 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2268 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2269 struct btrfs_root *root, struct btrfs_path *p,
2270 struct extent_buffer *b, int level, int ins_len,
2271 int *write_lock_level)
2274 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2275 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2278 if (*write_lock_level < level + 1) {
2279 *write_lock_level = level + 1;
2280 btrfs_release_path(p);
2284 sret = reada_for_balance(root, p, level);
2288 btrfs_set_path_blocking(p);
2289 sret = split_node(trans, root, p, level);
2290 btrfs_clear_path_blocking(p, NULL, 0);
2297 b = p->nodes[level];
2298 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2299 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
2302 if (*write_lock_level < level + 1) {
2303 *write_lock_level = level + 1;
2304 btrfs_release_path(p);
2308 sret = reada_for_balance(root, p, level);
2312 btrfs_set_path_blocking(p);
2313 sret = balance_level(trans, root, p, level);
2314 btrfs_clear_path_blocking(p, NULL, 0);
2320 b = p->nodes[level];
2322 btrfs_release_path(p);
2325 BUG_ON(btrfs_header_nritems(b) == 1);
2336 * look for key in the tree. path is filled in with nodes along the way
2337 * if key is found, we return zero and you can find the item in the leaf
2338 * level of the path (level 0)
2340 * If the key isn't found, the path points to the slot where it should
2341 * be inserted, and 1 is returned. If there are other errors during the
2342 * search a negative error number is returned.
2344 * if ins_len > 0, nodes and leaves will be split as we walk down the
2345 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2348 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2349 *root, struct btrfs_key *key, struct btrfs_path *p, int
2352 struct extent_buffer *b;
2357 int lowest_unlock = 1;
2359 /* everything at write_lock_level or lower must be write locked */
2360 int write_lock_level = 0;
2361 u8 lowest_level = 0;
2362 int min_write_lock_level;
2364 lowest_level = p->lowest_level;
2365 WARN_ON(lowest_level && ins_len > 0);
2366 WARN_ON(p->nodes[0] != NULL);
2371 /* when we are removing items, we might have to go up to level
2372 * two as we update tree pointers Make sure we keep write
2373 * for those levels as well
2375 write_lock_level = 2;
2376 } else if (ins_len > 0) {
2378 * for inserting items, make sure we have a write lock on
2379 * level 1 so we can update keys
2381 write_lock_level = 1;
2385 write_lock_level = -1;
2387 if (cow && (p->keep_locks || p->lowest_level))
2388 write_lock_level = BTRFS_MAX_LEVEL;
2390 min_write_lock_level = write_lock_level;
2394 * we try very hard to do read locks on the root
2396 root_lock = BTRFS_READ_LOCK;
2398 if (p->search_commit_root) {
2400 * the commit roots are read only
2401 * so we always do read locks
2403 b = root->commit_root;
2404 extent_buffer_get(b);
2405 level = btrfs_header_level(b);
2406 if (!p->skip_locking)
2407 btrfs_tree_read_lock(b);
2409 if (p->skip_locking) {
2410 b = btrfs_root_node(root);
2411 level = btrfs_header_level(b);
2413 /* we don't know the level of the root node
2414 * until we actually have it read locked
2416 b = btrfs_read_lock_root_node(root);
2417 level = btrfs_header_level(b);
2418 if (level <= write_lock_level) {
2419 /* whoops, must trade for write lock */
2420 btrfs_tree_read_unlock(b);
2421 free_extent_buffer(b);
2422 b = btrfs_lock_root_node(root);
2423 root_lock = BTRFS_WRITE_LOCK;
2425 /* the level might have changed, check again */
2426 level = btrfs_header_level(b);
2430 p->nodes[level] = b;
2431 if (!p->skip_locking)
2432 p->locks[level] = root_lock;
2435 level = btrfs_header_level(b);
2438 * setup the path here so we can release it under lock
2439 * contention with the cow code
2443 * if we don't really need to cow this block
2444 * then we don't want to set the path blocking,
2445 * so we test it here
2447 if (!should_cow_block(trans, root, b))
2450 btrfs_set_path_blocking(p);
2453 * must have write locks on this node and the
2456 if (level + 1 > write_lock_level) {
2457 write_lock_level = level + 1;
2458 btrfs_release_path(p);
2462 err = btrfs_cow_block(trans, root, b,
2463 p->nodes[level + 1],
2464 p->slots[level + 1], &b);
2471 BUG_ON(!cow && ins_len);
2473 p->nodes[level] = b;
2474 btrfs_clear_path_blocking(p, NULL, 0);
2477 * we have a lock on b and as long as we aren't changing
2478 * the tree, there is no way to for the items in b to change.
2479 * It is safe to drop the lock on our parent before we
2480 * go through the expensive btree search on b.
2482 * If cow is true, then we might be changing slot zero,
2483 * which may require changing the parent. So, we can't
2484 * drop the lock until after we know which slot we're
2488 btrfs_unlock_up_safe(p, level + 1);
2490 ret = bin_search(b, key, level, &slot);
2494 if (ret && slot > 0) {
2498 p->slots[level] = slot;
2499 err = setup_nodes_for_search(trans, root, p, b, level,
2500 ins_len, &write_lock_level);
2507 b = p->nodes[level];
2508 slot = p->slots[level];
2511 * slot 0 is special, if we change the key
2512 * we have to update the parent pointer
2513 * which means we must have a write lock
2516 if (slot == 0 && cow &&
2517 write_lock_level < level + 1) {
2518 write_lock_level = level + 1;
2519 btrfs_release_path(p);
2523 unlock_up(p, level, lowest_unlock,
2524 min_write_lock_level, &write_lock_level);
2526 if (level == lowest_level) {
2532 err = read_block_for_search(trans, root, p,
2533 &b, level, slot, key, 0);
2541 if (!p->skip_locking) {
2542 level = btrfs_header_level(b);
2543 if (level <= write_lock_level) {
2544 err = btrfs_try_tree_write_lock(b);
2546 btrfs_set_path_blocking(p);
2548 btrfs_clear_path_blocking(p, b,
2551 p->locks[level] = BTRFS_WRITE_LOCK;
2553 err = btrfs_try_tree_read_lock(b);
2555 btrfs_set_path_blocking(p);
2556 btrfs_tree_read_lock(b);
2557 btrfs_clear_path_blocking(p, b,
2560 p->locks[level] = BTRFS_READ_LOCK;
2562 p->nodes[level] = b;
2565 p->slots[level] = slot;
2567 btrfs_leaf_free_space(root, b) < ins_len) {
2568 if (write_lock_level < 1) {
2569 write_lock_level = 1;
2570 btrfs_release_path(p);
2574 btrfs_set_path_blocking(p);
2575 err = split_leaf(trans, root, key,
2576 p, ins_len, ret == 0);
2577 btrfs_clear_path_blocking(p, NULL, 0);
2585 if (!p->search_for_split)
2586 unlock_up(p, level, lowest_unlock,
2587 min_write_lock_level, &write_lock_level);
2594 * we don't really know what they plan on doing with the path
2595 * from here on, so for now just mark it as blocking
2597 if (!p->leave_spinning)
2598 btrfs_set_path_blocking(p);
2600 btrfs_release_path(p);
2605 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2606 * current state of the tree together with the operations recorded in the tree
2607 * modification log to search for the key in a previous version of this tree, as
2608 * denoted by the time_seq parameter.
2610 * Naturally, there is no support for insert, delete or cow operations.
2612 * The resulting path and return value will be set up as if we called
2613 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2615 int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2616 struct btrfs_path *p, u64 time_seq)
2618 struct extent_buffer *b;
2623 int lowest_unlock = 1;
2624 u8 lowest_level = 0;
2626 lowest_level = p->lowest_level;
2627 WARN_ON(p->nodes[0] != NULL);
2629 if (p->search_commit_root) {
2631 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2635 b = get_old_root(root, time_seq);
2636 level = btrfs_header_level(b);
2637 p->locks[level] = BTRFS_READ_LOCK;
2640 level = btrfs_header_level(b);
2641 p->nodes[level] = b;
2642 btrfs_clear_path_blocking(p, NULL, 0);
2645 * we have a lock on b and as long as we aren't changing
2646 * the tree, there is no way to for the items in b to change.
2647 * It is safe to drop the lock on our parent before we
2648 * go through the expensive btree search on b.
2650 btrfs_unlock_up_safe(p, level + 1);
2652 ret = bin_search(b, key, level, &slot);
2656 if (ret && slot > 0) {
2660 p->slots[level] = slot;
2661 unlock_up(p, level, lowest_unlock, 0, NULL);
2663 if (level == lowest_level) {
2669 err = read_block_for_search(NULL, root, p, &b, level,
2670 slot, key, time_seq);
2678 level = btrfs_header_level(b);
2679 err = btrfs_try_tree_read_lock(b);
2681 btrfs_set_path_blocking(p);
2682 btrfs_tree_read_lock(b);
2683 btrfs_clear_path_blocking(p, b,
2686 p->locks[level] = BTRFS_READ_LOCK;
2687 p->nodes[level] = b;
2688 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2689 if (b != p->nodes[level]) {
2690 btrfs_tree_unlock_rw(p->nodes[level],
2692 p->locks[level] = 0;
2693 p->nodes[level] = b;
2696 p->slots[level] = slot;
2697 unlock_up(p, level, lowest_unlock, 0, NULL);
2703 if (!p->leave_spinning)
2704 btrfs_set_path_blocking(p);
2706 btrfs_release_path(p);
2712 * adjust the pointers going up the tree, starting at level
2713 * making sure the right key of each node is points to 'key'.
2714 * This is used after shifting pointers to the left, so it stops
2715 * fixing up pointers when a given leaf/node is not in slot 0 of the
2719 static void fixup_low_keys(struct btrfs_trans_handle *trans,
2720 struct btrfs_root *root, struct btrfs_path *path,
2721 struct btrfs_disk_key *key, int level)
2724 struct extent_buffer *t;
2726 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2727 int tslot = path->slots[i];
2728 if (!path->nodes[i])
2731 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
2732 btrfs_set_node_key(t, key, tslot);
2733 btrfs_mark_buffer_dirty(path->nodes[i]);
2742 * This function isn't completely safe. It's the caller's responsibility
2743 * that the new key won't break the order
2745 void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2746 struct btrfs_root *root, struct btrfs_path *path,
2747 struct btrfs_key *new_key)
2749 struct btrfs_disk_key disk_key;
2750 struct extent_buffer *eb;
2753 eb = path->nodes[0];
2754 slot = path->slots[0];
2756 btrfs_item_key(eb, &disk_key, slot - 1);
2757 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
2759 if (slot < btrfs_header_nritems(eb) - 1) {
2760 btrfs_item_key(eb, &disk_key, slot + 1);
2761 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
2764 btrfs_cpu_key_to_disk(&disk_key, new_key);
2765 btrfs_set_item_key(eb, &disk_key, slot);
2766 btrfs_mark_buffer_dirty(eb);
2768 fixup_low_keys(trans, root, path, &disk_key, 1);
2772 * try to push data from one node into the next node left in the
2775 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2776 * error, and > 0 if there was no room in the left hand block.
2778 static int push_node_left(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root, struct extent_buffer *dst,
2780 struct extent_buffer *src, int empty)
2787 src_nritems = btrfs_header_nritems(src);
2788 dst_nritems = btrfs_header_nritems(dst);
2789 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2790 WARN_ON(btrfs_header_generation(src) != trans->transid);
2791 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2793 if (!empty && src_nritems <= 8)
2796 if (push_items <= 0)
2800 push_items = min(src_nritems, push_items);
2801 if (push_items < src_nritems) {
2802 /* leave at least 8 pointers in the node if
2803 * we aren't going to empty it
2805 if (src_nritems - push_items < 8) {
2806 if (push_items <= 8)
2812 push_items = min(src_nritems - 8, push_items);
2814 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2816 copy_extent_buffer(dst, src,
2817 btrfs_node_key_ptr_offset(dst_nritems),
2818 btrfs_node_key_ptr_offset(0),
2819 push_items * sizeof(struct btrfs_key_ptr));
2821 if (push_items < src_nritems) {
2822 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2823 src_nritems - push_items);
2824 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2825 btrfs_node_key_ptr_offset(push_items),
2826 (src_nritems - push_items) *
2827 sizeof(struct btrfs_key_ptr));
2829 btrfs_set_header_nritems(src, src_nritems - push_items);
2830 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2831 btrfs_mark_buffer_dirty(src);
2832 btrfs_mark_buffer_dirty(dst);
2838 * try to push data from one node into the next node right in the
2841 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2842 * error, and > 0 if there was no room in the right hand block.
2844 * this will only push up to 1/2 the contents of the left node over
2846 static int balance_node_right(struct btrfs_trans_handle *trans,
2847 struct btrfs_root *root,
2848 struct extent_buffer *dst,
2849 struct extent_buffer *src)
2857 WARN_ON(btrfs_header_generation(src) != trans->transid);
2858 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2860 src_nritems = btrfs_header_nritems(src);
2861 dst_nritems = btrfs_header_nritems(dst);
2862 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2863 if (push_items <= 0)
2866 if (src_nritems < 4)
2869 max_push = src_nritems / 2 + 1;
2870 /* don't try to empty the node */
2871 if (max_push >= src_nritems)
2874 if (max_push < push_items)
2875 push_items = max_push;
2877 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
2878 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2879 btrfs_node_key_ptr_offset(0),
2881 sizeof(struct btrfs_key_ptr));
2883 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2884 src_nritems - push_items, push_items);
2885 copy_extent_buffer(dst, src,
2886 btrfs_node_key_ptr_offset(0),
2887 btrfs_node_key_ptr_offset(src_nritems - push_items),
2888 push_items * sizeof(struct btrfs_key_ptr));
2890 btrfs_set_header_nritems(src, src_nritems - push_items);
2891 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2893 btrfs_mark_buffer_dirty(src);
2894 btrfs_mark_buffer_dirty(dst);
2900 * helper function to insert a new root level in the tree.
2901 * A new node is allocated, and a single item is inserted to
2902 * point to the existing root
2904 * returns zero on success or < 0 on failure.
2906 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2907 struct btrfs_root *root,
2908 struct btrfs_path *path, int level)
2911 struct extent_buffer *lower;
2912 struct extent_buffer *c;
2913 struct extent_buffer *old;
2914 struct btrfs_disk_key lower_key;
2916 BUG_ON(path->nodes[level]);
2917 BUG_ON(path->nodes[level-1] != root->node);
2919 lower = path->nodes[level-1];
2921 btrfs_item_key(lower, &lower_key, 0);
2923 btrfs_node_key(lower, &lower_key, 0);
2925 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2926 root->root_key.objectid, &lower_key,
2927 level, root->node->start, 0);
2931 root_add_used(root, root->nodesize);
2933 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
2934 btrfs_set_header_nritems(c, 1);
2935 btrfs_set_header_level(c, level);
2936 btrfs_set_header_bytenr(c, c->start);
2937 btrfs_set_header_generation(c, trans->transid);
2938 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
2939 btrfs_set_header_owner(c, root->root_key.objectid);
2941 write_extent_buffer(c, root->fs_info->fsid,
2942 (unsigned long)btrfs_header_fsid(c),
2945 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2946 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2949 btrfs_set_node_key(c, &lower_key, 0);
2950 btrfs_set_node_blockptr(c, 0, lower->start);
2951 lower_gen = btrfs_header_generation(lower);
2952 WARN_ON(lower_gen != trans->transid);
2954 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2956 btrfs_mark_buffer_dirty(c);
2959 tree_mod_log_set_root_pointer(root, c);
2960 rcu_assign_pointer(root->node, c);
2962 /* the super has an extra ref to root->node */
2963 free_extent_buffer(old);
2965 add_root_to_dirty_list(root);
2966 extent_buffer_get(c);
2967 path->nodes[level] = c;
2968 path->locks[level] = BTRFS_WRITE_LOCK;
2969 path->slots[level] = 0;
2974 * worker function to insert a single pointer in a node.
2975 * the node should have enough room for the pointer already
2977 * slot and level indicate where you want the key to go, and
2978 * blocknr is the block the key points to.
2980 static void insert_ptr(struct btrfs_trans_handle *trans,
2981 struct btrfs_root *root, struct btrfs_path *path,
2982 struct btrfs_disk_key *key, u64 bytenr,
2983 int slot, int level, int tree_mod_log)
2985 struct extent_buffer *lower;
2989 BUG_ON(!path->nodes[level]);
2990 btrfs_assert_tree_locked(path->nodes[level]);
2991 lower = path->nodes[level];
2992 nritems = btrfs_header_nritems(lower);
2993 BUG_ON(slot > nritems);
2994 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
2995 if (slot != nritems) {
2996 if (tree_mod_log && level)
2997 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2998 slot, nritems - slot);
2999 memmove_extent_buffer(lower,
3000 btrfs_node_key_ptr_offset(slot + 1),
3001 btrfs_node_key_ptr_offset(slot),
3002 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3004 if (tree_mod_log && level) {
3005 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3009 btrfs_set_node_key(lower, key, slot);
3010 btrfs_set_node_blockptr(lower, slot, bytenr);
3011 WARN_ON(trans->transid == 0);
3012 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3013 btrfs_set_header_nritems(lower, nritems + 1);
3014 btrfs_mark_buffer_dirty(lower);
3018 * split the node at the specified level in path in two.
3019 * The path is corrected to point to the appropriate node after the split
3021 * Before splitting this tries to make some room in the node by pushing
3022 * left and right, if either one works, it returns right away.
3024 * returns 0 on success and < 0 on failure
3026 static noinline int split_node(struct btrfs_trans_handle *trans,
3027 struct btrfs_root *root,
3028 struct btrfs_path *path, int level)
3030 struct extent_buffer *c;
3031 struct extent_buffer *split;
3032 struct btrfs_disk_key disk_key;
3037 c = path->nodes[level];
3038 WARN_ON(btrfs_header_generation(c) != trans->transid);
3039 if (c == root->node) {
3040 /* trying to split the root, lets make a new one */
3041 ret = insert_new_root(trans, root, path, level + 1);
3045 ret = push_nodes_for_insert(trans, root, path, level);
3046 c = path->nodes[level];
3047 if (!ret && btrfs_header_nritems(c) <
3048 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
3054 c_nritems = btrfs_header_nritems(c);
3055 mid = (c_nritems + 1) / 2;
3056 btrfs_node_key(c, &disk_key, mid);
3058 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
3059 root->root_key.objectid,
3060 &disk_key, level, c->start, 0);
3062 return PTR_ERR(split);
3064 root_add_used(root, root->nodesize);
3066 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
3067 btrfs_set_header_level(split, btrfs_header_level(c));
3068 btrfs_set_header_bytenr(split, split->start);
3069 btrfs_set_header_generation(split, trans->transid);
3070 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
3071 btrfs_set_header_owner(split, root->root_key.objectid);
3072 write_extent_buffer(split, root->fs_info->fsid,
3073 (unsigned long)btrfs_header_fsid(split),
3075 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3076 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3079 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
3080 copy_extent_buffer(split, c,
3081 btrfs_node_key_ptr_offset(0),
3082 btrfs_node_key_ptr_offset(mid),
3083 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3084 btrfs_set_header_nritems(split, c_nritems - mid);
3085 btrfs_set_header_nritems(c, mid);
3088 btrfs_mark_buffer_dirty(c);
3089 btrfs_mark_buffer_dirty(split);
3091 insert_ptr(trans, root, path, &disk_key, split->start,
3092 path->slots[level + 1] + 1, level + 1, 1);
3094 if (path->slots[level] >= mid) {
3095 path->slots[level] -= mid;
3096 btrfs_tree_unlock(c);
3097 free_extent_buffer(c);
3098 path->nodes[level] = split;
3099 path->slots[level + 1] += 1;
3101 btrfs_tree_unlock(split);
3102 free_extent_buffer(split);
3108 * how many bytes are required to store the items in a leaf. start
3109 * and nr indicate which items in the leaf to check. This totals up the
3110 * space used both by the item structs and the item data
3112 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3115 int nritems = btrfs_header_nritems(l);
3116 int end = min(nritems, start + nr) - 1;
3120 data_len = btrfs_item_end_nr(l, start);
3121 data_len = data_len - btrfs_item_offset_nr(l, end);
3122 data_len += sizeof(struct btrfs_item) * nr;
3123 WARN_ON(data_len < 0);
3128 * The space between the end of the leaf items and
3129 * the start of the leaf data. IOW, how much room
3130 * the leaf has left for both items and data
3132 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
3133 struct extent_buffer *leaf)
3135 int nritems = btrfs_header_nritems(leaf);
3137 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3139 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3140 "used %d nritems %d\n",
3141 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
3142 leaf_space_used(leaf, 0, nritems), nritems);
3148 * min slot controls the lowest index we're willing to push to the
3149 * right. We'll push up to and including min_slot, but no lower
3151 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3152 struct btrfs_root *root,
3153 struct btrfs_path *path,
3154 int data_size, int empty,
3155 struct extent_buffer *right,
3156 int free_space, u32 left_nritems,
3159 struct extent_buffer *left = path->nodes[0];
3160 struct extent_buffer *upper = path->nodes[1];
3161 struct btrfs_map_token token;
3162 struct btrfs_disk_key disk_key;
3167 struct btrfs_item *item;
3173 btrfs_init_map_token(&token);
3178 nr = max_t(u32, 1, min_slot);
3180 if (path->slots[0] >= left_nritems)
3181 push_space += data_size;
3183 slot = path->slots[1];
3184 i = left_nritems - 1;
3186 item = btrfs_item_nr(left, i);
3188 if (!empty && push_items > 0) {
3189 if (path->slots[0] > i)
3191 if (path->slots[0] == i) {
3192 int space = btrfs_leaf_free_space(root, left);
3193 if (space + push_space * 2 > free_space)
3198 if (path->slots[0] == i)
3199 push_space += data_size;
3201 this_item_size = btrfs_item_size(left, item);
3202 if (this_item_size + sizeof(*item) + push_space > free_space)
3206 push_space += this_item_size + sizeof(*item);
3212 if (push_items == 0)
3215 if (!empty && push_items == left_nritems)
3218 /* push left to right */
3219 right_nritems = btrfs_header_nritems(right);
3221 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3222 push_space -= leaf_data_end(root, left);
3224 /* make room in the right data area */
3225 data_end = leaf_data_end(root, right);
3226 memmove_extent_buffer(right,
3227 btrfs_leaf_data(right) + data_end - push_space,
3228 btrfs_leaf_data(right) + data_end,
3229 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3231 /* copy from the left data area */
3232 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
3233 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3234 btrfs_leaf_data(left) + leaf_data_end(root, left),
3237 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3238 btrfs_item_nr_offset(0),
3239 right_nritems * sizeof(struct btrfs_item));
3241 /* copy the items from left to right */
3242 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3243 btrfs_item_nr_offset(left_nritems - push_items),
3244 push_items * sizeof(struct btrfs_item));
3246 /* update the item pointers */
3247 right_nritems += push_items;
3248 btrfs_set_header_nritems(right, right_nritems);
3249 push_space = BTRFS_LEAF_DATA_SIZE(root);
3250 for (i = 0; i < right_nritems; i++) {
3251 item = btrfs_item_nr(right, i);
3252 push_space -= btrfs_token_item_size(right, item, &token);
3253 btrfs_set_token_item_offset(right, item, push_space, &token);
3256 left_nritems -= push_items;
3257 btrfs_set_header_nritems(left, left_nritems);
3260 btrfs_mark_buffer_dirty(left);
3262 clean_tree_block(trans, root, left);
3264 btrfs_mark_buffer_dirty(right);
3266 btrfs_item_key(right, &disk_key, 0);
3267 btrfs_set_node_key(upper, &disk_key, slot + 1);
3268 btrfs_mark_buffer_dirty(upper);
3270 /* then fixup the leaf pointer in the path */
3271 if (path->slots[0] >= left_nritems) {
3272 path->slots[0] -= left_nritems;
3273 if (btrfs_header_nritems(path->nodes[0]) == 0)
3274 clean_tree_block(trans, root, path->nodes[0]);
3275 btrfs_tree_unlock(path->nodes[0]);
3276 free_extent_buffer(path->nodes[0]);
3277 path->nodes[0] = right;
3278 path->slots[1] += 1;
3280 btrfs_tree_unlock(right);
3281 free_extent_buffer(right);
3286 btrfs_tree_unlock(right);
3287 free_extent_buffer(right);
3292 * push some data in the path leaf to the right, trying to free up at
3293 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3295 * returns 1 if the push failed because the other node didn't have enough
3296 * room, 0 if everything worked out and < 0 if there were major errors.
3298 * this will push starting from min_slot to the end of the leaf. It won't
3299 * push any slot lower than min_slot
3301 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3302 *root, struct btrfs_path *path,
3303 int min_data_size, int data_size,
3304 int empty, u32 min_slot)
3306 struct extent_buffer *left = path->nodes[0];
3307 struct extent_buffer *right;
3308 struct extent_buffer *upper;
3314 if (!path->nodes[1])
3317 slot = path->slots[1];
3318 upper = path->nodes[1];
3319 if (slot >= btrfs_header_nritems(upper) - 1)
3322 btrfs_assert_tree_locked(path->nodes[1]);
3324 right = read_node_slot(root, upper, slot + 1);
3328 btrfs_tree_lock(right);
3329 btrfs_set_lock_blocking(right);
3331 free_space = btrfs_leaf_free_space(root, right);
3332 if (free_space < data_size)
3335 /* cow and double check */
3336 ret = btrfs_cow_block(trans, root, right, upper,
3341 free_space = btrfs_leaf_free_space(root, right);
3342 if (free_space < data_size)
3345 left_nritems = btrfs_header_nritems(left);
3346 if (left_nritems == 0)
3349 return __push_leaf_right(trans, root, path, min_data_size, empty,
3350 right, free_space, left_nritems, min_slot);
3352 btrfs_tree_unlock(right);
3353 free_extent_buffer(right);
3358 * push some data in the path leaf to the left, trying to free up at
3359 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3361 * max_slot can put a limit on how far into the leaf we'll push items. The
3362 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3365 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3366 struct btrfs_root *root,
3367 struct btrfs_path *path, int data_size,
3368 int empty, struct extent_buffer *left,
3369 int free_space, u32 right_nritems,
3372 struct btrfs_disk_key disk_key;
3373 struct extent_buffer *right = path->nodes[0];
3377 struct btrfs_item *item;
3378 u32 old_left_nritems;
3382 u32 old_left_item_size;
3383 struct btrfs_map_token token;
3385 btrfs_init_map_token(&token);
3388 nr = min(right_nritems, max_slot);
3390 nr = min(right_nritems - 1, max_slot);
3392 for (i = 0; i < nr; i++) {
3393 item = btrfs_item_nr(right, i);
3395 if (!empty && push_items > 0) {
3396 if (path->slots[0] < i)
3398 if (path->slots[0] == i) {
3399 int space = btrfs_leaf_free_space(root, right);
3400 if (space + push_space * 2 > free_space)
3405 if (path->slots[0] == i)
3406 push_space += data_size;
3408 this_item_size = btrfs_item_size(right, item);
3409 if (this_item_size + sizeof(*item) + push_space > free_space)
3413 push_space += this_item_size + sizeof(*item);
3416 if (push_items == 0) {
3420 if (!empty && push_items == btrfs_header_nritems(right))
3423 /* push data from right to left */
3424 copy_extent_buffer(left, right,
3425 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3426 btrfs_item_nr_offset(0),
3427 push_items * sizeof(struct btrfs_item));
3429 push_space = BTRFS_LEAF_DATA_SIZE(root) -
3430 btrfs_item_offset_nr(right, push_items - 1);
3432 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
3433 leaf_data_end(root, left) - push_space,
3434 btrfs_leaf_data(right) +
3435 btrfs_item_offset_nr(right, push_items - 1),
3437 old_left_nritems = btrfs_header_nritems(left);
3438 BUG_ON(old_left_nritems <= 0);
3440 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3441 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3444 item = btrfs_item_nr(left, i);
3446 ioff = btrfs_token_item_offset(left, item, &token);
3447 btrfs_set_token_item_offset(left, item,
3448 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3451 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3453 /* fixup right node */
3454 if (push_items > right_nritems) {
3455 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3460 if (push_items < right_nritems) {
3461 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3462 leaf_data_end(root, right);
3463 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3464 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3465 btrfs_leaf_data(right) +
3466 leaf_data_end(root, right), push_space);
3468 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3469 btrfs_item_nr_offset(push_items),
3470 (btrfs_header_nritems(right) - push_items) *
3471 sizeof(struct btrfs_item));
3473 right_nritems -= push_items;
3474 btrfs_set_header_nritems(right, right_nritems);
3475 push_space = BTRFS_LEAF_DATA_SIZE(root);
3476 for (i = 0; i < right_nritems; i++) {
3477 item = btrfs_item_nr(right, i);
3479 push_space = push_space - btrfs_token_item_size(right,
3481 btrfs_set_token_item_offset(right, item, push_space, &token);
3484 btrfs_mark_buffer_dirty(left);
3486 btrfs_mark_buffer_dirty(right);
3488 clean_tree_block(trans, root, right);
3490 btrfs_item_key(right, &disk_key, 0);
3491 fixup_low_keys(trans, root, path, &disk_key, 1);
3493 /* then fixup the leaf pointer in the path */
3494 if (path->slots[0] < push_items) {
3495 path->slots[0] += old_left_nritems;
3496 btrfs_tree_unlock(path->nodes[0]);
3497 free_extent_buffer(path->nodes[0]);
3498 path->nodes[0] = left;
3499 path->slots[1] -= 1;
3501 btrfs_tree_unlock(left);
3502 free_extent_buffer(left);
3503 path->slots[0] -= push_items;
3505 BUG_ON(path->slots[0] < 0);
3508 btrfs_tree_unlock(left);
3509 free_extent_buffer(left);
3514 * push some data in the path leaf to the left, trying to free up at
3515 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3517 * max_slot can put a limit on how far into the leaf we'll push items. The
3518 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3521 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3522 *root, struct btrfs_path *path, int min_data_size,
3523 int data_size, int empty, u32 max_slot)
3525 struct extent_buffer *right = path->nodes[0];
3526 struct extent_buffer *left;
3532 slot = path->slots[1];
3535 if (!path->nodes[1])
3538 right_nritems = btrfs_header_nritems(right);
3539 if (right_nritems == 0)
3542 btrfs_assert_tree_locked(path->nodes[1]);
3544 left = read_node_slot(root, path->nodes[1], slot - 1);
3548 btrfs_tree_lock(left);
3549 btrfs_set_lock_blocking(left);
3551 free_space = btrfs_leaf_free_space(root, left);
3552 if (free_space < data_size) {
3557 /* cow and double check */
3558 ret = btrfs_cow_block(trans, root, left,
3559 path->nodes[1], slot - 1, &left);
3561 /* we hit -ENOSPC, but it isn't fatal here */
3567 free_space = btrfs_leaf_free_space(root, left);
3568 if (free_space < data_size) {
3573 return __push_leaf_left(trans, root, path, min_data_size,
3574 empty, left, free_space, right_nritems,
3577 btrfs_tree_unlock(left);
3578 free_extent_buffer(left);
3583 * split the path's leaf in two, making sure there is at least data_size
3584 * available for the resulting leaf level of the path.
3586 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3587 struct btrfs_root *root,
3588 struct btrfs_path *path,
3589 struct extent_buffer *l,
3590 struct extent_buffer *right,
3591 int slot, int mid, int nritems)
3596 struct btrfs_disk_key disk_key;
3597 struct btrfs_map_token token;
3599 btrfs_init_map_token(&token);
3601 nritems = nritems - mid;
3602 btrfs_set_header_nritems(right, nritems);
3603 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3605 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3606 btrfs_item_nr_offset(mid),
3607 nritems * sizeof(struct btrfs_item));
3609 copy_extent_buffer(right, l,
3610 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3611 data_copy_size, btrfs_leaf_data(l) +
3612 leaf_data_end(root, l), data_copy_size);
3614 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3615 btrfs_item_end_nr(l, mid);
3617 for (i = 0; i < nritems; i++) {
3618 struct btrfs_item *item = btrfs_item_nr(right, i);
3621 ioff = btrfs_token_item_offset(right, item, &token);
3622 btrfs_set_token_item_offset(right, item,
3623 ioff + rt_data_off, &token);
3626 btrfs_set_header_nritems(l, mid);
3627 btrfs_item_key(right, &disk_key, 0);
3628 insert_ptr(trans, root, path, &disk_key, right->start,
3629 path->slots[1] + 1, 1, 0);
3631 btrfs_mark_buffer_dirty(right);
3632 btrfs_mark_buffer_dirty(l);
3633 BUG_ON(path->slots[0] != slot);
3636 btrfs_tree_unlock(path->nodes[0]);
3637 free_extent_buffer(path->nodes[0]);
3638 path->nodes[0] = right;
3639 path->slots[0] -= mid;
3640 path->slots[1] += 1;
3642 btrfs_tree_unlock(right);
3643 free_extent_buffer(right);
3646 BUG_ON(path->slots[0] < 0);
3650 * double splits happen when we need to insert a big item in the middle
3651 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3652 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3655 * We avoid this by trying to push the items on either side of our target
3656 * into the adjacent leaves. If all goes well we can avoid the double split
3659 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3660 struct btrfs_root *root,
3661 struct btrfs_path *path,
3669 slot = path->slots[0];
3672 * try to push all the items after our slot into the
3675 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3682 nritems = btrfs_header_nritems(path->nodes[0]);
3684 * our goal is to get our slot at the start or end of a leaf. If
3685 * we've done so we're done
3687 if (path->slots[0] == 0 || path->slots[0] == nritems)
3690 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3693 /* try to push all the items before our slot into the next leaf */
3694 slot = path->slots[0];
3695 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3708 * split the path's leaf in two, making sure there is at least data_size
3709 * available for the resulting leaf level of the path.
3711 * returns 0 if all went well and < 0 on failure.
3713 static noinline int split_leaf(struct btrfs_trans_handle *trans,
3714 struct btrfs_root *root,
3715 struct btrfs_key *ins_key,
3716 struct btrfs_path *path, int data_size,
3719 struct btrfs_disk_key disk_key;
3720 struct extent_buffer *l;
3724 struct extent_buffer *right;
3728 int num_doubles = 0;
3729 int tried_avoid_double = 0;
3732 slot = path->slots[0];
3733 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3734 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3737 /* first try to make some room by pushing left and right */
3739 wret = push_leaf_right(trans, root, path, data_size,
3744 wret = push_leaf_left(trans, root, path, data_size,
3745 data_size, 0, (u32)-1);
3751 /* did the pushes work? */
3752 if (btrfs_leaf_free_space(root, l) >= data_size)
3756 if (!path->nodes[1]) {
3757 ret = insert_new_root(trans, root, path, 1);
3764 slot = path->slots[0];
3765 nritems = btrfs_header_nritems(l);
3766 mid = (nritems + 1) / 2;
3770 leaf_space_used(l, mid, nritems - mid) + data_size >
3771 BTRFS_LEAF_DATA_SIZE(root)) {
3772 if (slot >= nritems) {
3776 if (mid != nritems &&
3777 leaf_space_used(l, mid, nritems - mid) +
3778 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
3779 if (data_size && !tried_avoid_double)
3780 goto push_for_double;
3786 if (leaf_space_used(l, 0, mid) + data_size >
3787 BTRFS_LEAF_DATA_SIZE(root)) {
3788 if (!extend && data_size && slot == 0) {
3790 } else if ((extend || !data_size) && slot == 0) {
3794 if (mid != nritems &&
3795 leaf_space_used(l, mid, nritems - mid) +
3796 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
3797 if (data_size && !tried_avoid_double)
3798 goto push_for_double;
3806 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3808 btrfs_item_key(l, &disk_key, mid);
3810 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
3811 root->root_key.objectid,
3812 &disk_key, 0, l->start, 0);
3814 return PTR_ERR(right);
3816 root_add_used(root, root->leafsize);
3818 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3819 btrfs_set_header_bytenr(right, right->start);
3820 btrfs_set_header_generation(right, trans->transid);
3821 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
3822 btrfs_set_header_owner(right, root->root_key.objectid);
3823 btrfs_set_header_level(right, 0);
3824 write_extent_buffer(right, root->fs_info->fsid,
3825 (unsigned long)btrfs_header_fsid(right),
3828 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3829 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3834 btrfs_set_header_nritems(right, 0);
3835 insert_ptr(trans, root, path, &disk_key, right->start,
3836 path->slots[1] + 1, 1, 0);
3837 btrfs_tree_unlock(path->nodes[0]);
3838 free_extent_buffer(path->nodes[0]);
3839 path->nodes[0] = right;
3841 path->slots[1] += 1;
3843 btrfs_set_header_nritems(right, 0);
3844 insert_ptr(trans, root, path, &disk_key, right->start,
3845 path->slots[1], 1, 0);
3846 btrfs_tree_unlock(path->nodes[0]);
3847 free_extent_buffer(path->nodes[0]);
3848 path->nodes[0] = right;
3850 if (path->slots[1] == 0)
3851 fixup_low_keys(trans, root, path,
3854 btrfs_mark_buffer_dirty(right);
3858 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3861 BUG_ON(num_doubles != 0);
3869 push_for_double_split(trans, root, path, data_size);
3870 tried_avoid_double = 1;
3871 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3876 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3877 struct btrfs_root *root,
3878 struct btrfs_path *path, int ins_len)
3880 struct btrfs_key key;
3881 struct extent_buffer *leaf;
3882 struct btrfs_file_extent_item *fi;
3887 leaf = path->nodes[0];
3888 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3890 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3891 key.type != BTRFS_EXTENT_CSUM_KEY);
3893 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3896 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3897 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3898 fi = btrfs_item_ptr(leaf, path->slots[0],
3899 struct btrfs_file_extent_item);
3900 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3902 btrfs_release_path(path);
3904 path->keep_locks = 1;
3905 path->search_for_split = 1;
3906 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3907 path->search_for_split = 0;
3912 leaf = path->nodes[0];
3913 /* if our item isn't there or got smaller, return now */
3914 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3917 /* the leaf has changed, it now has room. return now */
3918 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3921 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3922 fi = btrfs_item_ptr(leaf, path->slots[0],
3923 struct btrfs_file_extent_item);
3924 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3928 btrfs_set_path_blocking(path);
3929 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3933 path->keep_locks = 0;
3934 btrfs_unlock_up_safe(path, 1);
3937 path->keep_locks = 0;
3941 static noinline int split_item(struct btrfs_trans_handle *trans,
3942 struct btrfs_root *root,
3943 struct btrfs_path *path,
3944 struct btrfs_key *new_key,
3945 unsigned long split_offset)
3947 struct extent_buffer *leaf;
3948 struct btrfs_item *item;
3949 struct btrfs_item *new_item;
3955 struct btrfs_disk_key disk_key;
3957 leaf = path->nodes[0];
3958 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3960 btrfs_set_path_blocking(path);
3962 item = btrfs_item_nr(leaf, path->slots[0]);
3963 orig_offset = btrfs_item_offset(leaf, item);
3964 item_size = btrfs_item_size(leaf, item);
3966 buf = kmalloc(item_size, GFP_NOFS);
3970 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3971 path->slots[0]), item_size);
3973 slot = path->slots[0] + 1;
3974 nritems = btrfs_header_nritems(leaf);
3975 if (slot != nritems) {
3976 /* shift the items */
3977 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3978 btrfs_item_nr_offset(slot),
3979 (nritems - slot) * sizeof(struct btrfs_item));
3982 btrfs_cpu_key_to_disk(&disk_key, new_key);
3983 btrfs_set_item_key(leaf, &disk_key, slot);
3985 new_item = btrfs_item_nr(leaf, slot);
3987 btrfs_set_item_offset(leaf, new_item, orig_offset);
3988 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3990 btrfs_set_item_offset(leaf, item,
3991 orig_offset + item_size - split_offset);
3992 btrfs_set_item_size(leaf, item, split_offset);
3994 btrfs_set_header_nritems(leaf, nritems + 1);
3996 /* write the data for the start of the original item */
3997 write_extent_buffer(leaf, buf,
3998 btrfs_item_ptr_offset(leaf, path->slots[0]),
4001 /* write the data for the new item */
4002 write_extent_buffer(leaf, buf + split_offset,
4003 btrfs_item_ptr_offset(leaf, slot),
4004 item_size - split_offset);
4005 btrfs_mark_buffer_dirty(leaf);
4007 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
4013 * This function splits a single item into two items,
4014 * giving 'new_key' to the new item and splitting the
4015 * old one at split_offset (from the start of the item).
4017 * The path may be released by this operation. After
4018 * the split, the path is pointing to the old item. The
4019 * new item is going to be in the same node as the old one.
4021 * Note, the item being split must be smaller enough to live alone on
4022 * a tree block with room for one extra struct btrfs_item
4024 * This allows us to split the item in place, keeping a lock on the
4025 * leaf the entire time.
4027 int btrfs_split_item(struct btrfs_trans_handle *trans,
4028 struct btrfs_root *root,
4029 struct btrfs_path *path,
4030 struct btrfs_key *new_key,
4031 unsigned long split_offset)
4034 ret = setup_leaf_for_split(trans, root, path,
4035 sizeof(struct btrfs_item));
4039 ret = split_item(trans, root, path, new_key, split_offset);
4044 * This function duplicate a item, giving 'new_key' to the new item.
4045 * It guarantees both items live in the same tree leaf and the new item
4046 * is contiguous with the original item.
4048 * This allows us to split file extent in place, keeping a lock on the
4049 * leaf the entire time.
4051 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4052 struct btrfs_root *root,
4053 struct btrfs_path *path,
4054 struct btrfs_key *new_key)
4056 struct extent_buffer *leaf;
4060 leaf = path->nodes[0];
4061 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4062 ret = setup_leaf_for_split(trans, root, path,
4063 item_size + sizeof(struct btrfs_item));
4068 setup_items_for_insert(trans, root, path, new_key, &item_size,
4069 item_size, item_size +
4070 sizeof(struct btrfs_item), 1);
4071 leaf = path->nodes[0];
4072 memcpy_extent_buffer(leaf,
4073 btrfs_item_ptr_offset(leaf, path->slots[0]),
4074 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4080 * make the item pointed to by the path smaller. new_size indicates
4081 * how small to make it, and from_end tells us if we just chop bytes
4082 * off the end of the item or if we shift the item to chop bytes off
4085 void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4086 struct btrfs_root *root,
4087 struct btrfs_path *path,
4088 u32 new_size, int from_end)
4091 struct extent_buffer *leaf;
4092 struct btrfs_item *item;
4094 unsigned int data_end;
4095 unsigned int old_data_start;
4096 unsigned int old_size;
4097 unsigned int size_diff;
4099 struct btrfs_map_token token;
4101 btrfs_init_map_token(&token);
4103 leaf = path->nodes[0];
4104 slot = path->slots[0];
4106 old_size = btrfs_item_size_nr(leaf, slot);
4107 if (old_size == new_size)
4110 nritems = btrfs_header_nritems(leaf);
4111 data_end = leaf_data_end(root, leaf);
4113 old_data_start = btrfs_item_offset_nr(leaf, slot);
4115 size_diff = old_size - new_size;
4118 BUG_ON(slot >= nritems);
4121 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4123 /* first correct the data pointers */
4124 for (i = slot; i < nritems; i++) {
4126 item = btrfs_item_nr(leaf, i);
4128 ioff = btrfs_token_item_offset(leaf, item, &token);
4129 btrfs_set_token_item_offset(leaf, item,
4130 ioff + size_diff, &token);
4133 /* shift the data */
4135 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4136 data_end + size_diff, btrfs_leaf_data(leaf) +
4137 data_end, old_data_start + new_size - data_end);
4139 struct btrfs_disk_key disk_key;
4142 btrfs_item_key(leaf, &disk_key, slot);
4144 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4146 struct btrfs_file_extent_item *fi;
4148 fi = btrfs_item_ptr(leaf, slot,
4149 struct btrfs_file_extent_item);
4150 fi = (struct btrfs_file_extent_item *)(
4151 (unsigned long)fi - size_diff);
4153 if (btrfs_file_extent_type(leaf, fi) ==
4154 BTRFS_FILE_EXTENT_INLINE) {
4155 ptr = btrfs_item_ptr_offset(leaf, slot);
4156 memmove_extent_buffer(leaf, ptr,
4158 offsetof(struct btrfs_file_extent_item,
4163 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4164 data_end + size_diff, btrfs_leaf_data(leaf) +
4165 data_end, old_data_start - data_end);
4167 offset = btrfs_disk_key_offset(&disk_key);
4168 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4169 btrfs_set_item_key(leaf, &disk_key, slot);
4171 fixup_low_keys(trans, root, path, &disk_key, 1);
4174 item = btrfs_item_nr(leaf, slot);
4175 btrfs_set_item_size(leaf, item, new_size);
4176 btrfs_mark_buffer_dirty(leaf);
4178 if (btrfs_leaf_free_space(root, leaf) < 0) {
4179 btrfs_print_leaf(root, leaf);
4185 * make the item pointed to by the path bigger, data_size is the new size.
4187 void btrfs_extend_item(struct btrfs_trans_handle *trans,
4188 struct btrfs_root *root, struct btrfs_path *path,
4192 struct extent_buffer *leaf;
4193 struct btrfs_item *item;
4195 unsigned int data_end;
4196 unsigned int old_data;
4197 unsigned int old_size;
4199 struct btrfs_map_token token;
4201 btrfs_init_map_token(&token);
4203 leaf = path->nodes[0];
4205 nritems = btrfs_header_nritems(leaf);
4206 data_end = leaf_data_end(root, leaf);
4208 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4209 btrfs_print_leaf(root, leaf);
4212 slot = path->slots[0];
4213 old_data = btrfs_item_end_nr(leaf, slot);
4216 if (slot >= nritems) {
4217 btrfs_print_leaf(root, leaf);
4218 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4224 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4226 /* first correct the data pointers */
4227 for (i = slot; i < nritems; i++) {
4229 item = btrfs_item_nr(leaf, i);
4231 ioff = btrfs_token_item_offset(leaf, item, &token);
4232 btrfs_set_token_item_offset(leaf, item,
4233 ioff - data_size, &token);
4236 /* shift the data */
4237 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4238 data_end - data_size, btrfs_leaf_data(leaf) +
4239 data_end, old_data - data_end);
4241 data_end = old_data;
4242 old_size = btrfs_item_size_nr(leaf, slot);
4243 item = btrfs_item_nr(leaf, slot);
4244 btrfs_set_item_size(leaf, item, old_size + data_size);
4245 btrfs_mark_buffer_dirty(leaf);
4247 if (btrfs_leaf_free_space(root, leaf) < 0) {
4248 btrfs_print_leaf(root, leaf);
4254 * Given a key and some data, insert items into the tree.
4255 * This does all the path init required, making room in the tree if needed.
4256 * Returns the number of keys that were inserted.
4258 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4259 struct btrfs_root *root,
4260 struct btrfs_path *path,
4261 struct btrfs_key *cpu_key, u32 *data_size,
4264 struct extent_buffer *leaf;
4265 struct btrfs_item *item;
4272 unsigned int data_end;
4273 struct btrfs_disk_key disk_key;
4274 struct btrfs_key found_key;
4275 struct btrfs_map_token token;
4277 btrfs_init_map_token(&token);
4279 for (i = 0; i < nr; i++) {
4280 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4281 BTRFS_LEAF_DATA_SIZE(root)) {
4285 total_data += data_size[i];
4286 total_size += data_size[i] + sizeof(struct btrfs_item);
4290 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4296 leaf = path->nodes[0];
4298 nritems = btrfs_header_nritems(leaf);
4299 data_end = leaf_data_end(root, leaf);
4301 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4302 for (i = nr; i >= 0; i--) {
4303 total_data -= data_size[i];
4304 total_size -= data_size[i] + sizeof(struct btrfs_item);
4305 if (total_size < btrfs_leaf_free_space(root, leaf))
4311 slot = path->slots[0];
4314 if (slot != nritems) {
4315 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4317 item = btrfs_item_nr(leaf, slot);
4318 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4320 /* figure out how many keys we can insert in here */
4321 total_data = data_size[0];
4322 for (i = 1; i < nr; i++) {
4323 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
4325 total_data += data_size[i];
4329 if (old_data < data_end) {
4330 btrfs_print_leaf(root, leaf);
4331 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
4332 slot, old_data, data_end);
4336 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4338 /* first correct the data pointers */
4339 for (i = slot; i < nritems; i++) {
4342 item = btrfs_item_nr(leaf, i);
4343 ioff = btrfs_token_item_offset(leaf, item, &token);
4344 btrfs_set_token_item_offset(leaf, item,
4345 ioff - total_data, &token);
4347 /* shift the items */
4348 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4349 btrfs_item_nr_offset(slot),
4350 (nritems - slot) * sizeof(struct btrfs_item));
4352 /* shift the data */
4353 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4354 data_end - total_data, btrfs_leaf_data(leaf) +
4355 data_end, old_data - data_end);
4356 data_end = old_data;
4359 * this sucks but it has to be done, if we are inserting at
4360 * the end of the leaf only insert 1 of the items, since we
4361 * have no way of knowing whats on the next leaf and we'd have
4362 * to drop our current locks to figure it out
4367 /* setup the item for the new data */
4368 for (i = 0; i < nr; i++) {
4369 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4370 btrfs_set_item_key(leaf, &disk_key, slot + i);
4371 item = btrfs_item_nr(leaf, slot + i);
4372 btrfs_set_token_item_offset(leaf, item,
4373 data_end - data_size[i], &token);
4374 data_end -= data_size[i];
4375 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4377 btrfs_set_header_nritems(leaf, nritems + nr);
4378 btrfs_mark_buffer_dirty(leaf);
4382 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4383 fixup_low_keys(trans, root, path, &disk_key, 1);
4386 if (btrfs_leaf_free_space(root, leaf) < 0) {
4387 btrfs_print_leaf(root, leaf);
4397 * this is a helper for btrfs_insert_empty_items, the main goal here is
4398 * to save stack depth by doing the bulk of the work in a function
4399 * that doesn't call btrfs_search_slot
4401 void setup_items_for_insert(struct btrfs_trans_handle *trans,
4402 struct btrfs_root *root, struct btrfs_path *path,
4403 struct btrfs_key *cpu_key, u32 *data_size,
4404 u32 total_data, u32 total_size, int nr)
4406 struct btrfs_item *item;
4409 unsigned int data_end;
4410 struct btrfs_disk_key disk_key;
4411 struct extent_buffer *leaf;
4413 struct btrfs_map_token token;
4415 btrfs_init_map_token(&token);
4417 leaf = path->nodes[0];
4418 slot = path->slots[0];
4420 nritems = btrfs_header_nritems(leaf);
4421 data_end = leaf_data_end(root, leaf);
4423 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4424 btrfs_print_leaf(root, leaf);
4425 printk(KERN_CRIT "not enough freespace need %u have %d\n",
4426 total_size, btrfs_leaf_free_space(root, leaf));
4430 if (slot != nritems) {
4431 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4433 if (old_data < data_end) {
4434 btrfs_print_leaf(root, leaf);
4435 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
4436 slot, old_data, data_end);
4440 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4442 /* first correct the data pointers */
4443 for (i = slot; i < nritems; i++) {
4446 item = btrfs_item_nr(leaf, i);
4447 ioff = btrfs_token_item_offset(leaf, item, &token);
4448 btrfs_set_token_item_offset(leaf, item,
4449 ioff - total_data, &token);
4451 /* shift the items */
4452 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4453 btrfs_item_nr_offset(slot),
4454 (nritems - slot) * sizeof(struct btrfs_item));
4456 /* shift the data */
4457 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4458 data_end - total_data, btrfs_leaf_data(leaf) +
4459 data_end, old_data - data_end);
4460 data_end = old_data;
4463 /* setup the item for the new data */
4464 for (i = 0; i < nr; i++) {
4465 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4466 btrfs_set_item_key(leaf, &disk_key, slot + i);
4467 item = btrfs_item_nr(leaf, slot + i);
4468 btrfs_set_token_item_offset(leaf, item,
4469 data_end - data_size[i], &token);
4470 data_end -= data_size[i];
4471 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4474 btrfs_set_header_nritems(leaf, nritems + nr);
4477 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4478 fixup_low_keys(trans, root, path, &disk_key, 1);
4480 btrfs_unlock_up_safe(path, 1);
4481 btrfs_mark_buffer_dirty(leaf);
4483 if (btrfs_leaf_free_space(root, leaf) < 0) {
4484 btrfs_print_leaf(root, leaf);
4490 * Given a key and some data, insert items into the tree.
4491 * This does all the path init required, making room in the tree if needed.
4493 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4494 struct btrfs_root *root,
4495 struct btrfs_path *path,
4496 struct btrfs_key *cpu_key, u32 *data_size,
4505 for (i = 0; i < nr; i++)
4506 total_data += data_size[i];
4508 total_size = total_data + (nr * sizeof(struct btrfs_item));
4509 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4515 slot = path->slots[0];
4518 setup_items_for_insert(trans, root, path, cpu_key, data_size,
4519 total_data, total_size, nr);
4524 * Given a key and some data, insert an item into the tree.
4525 * This does all the path init required, making room in the tree if needed.
4527 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4528 *root, struct btrfs_key *cpu_key, void *data, u32
4532 struct btrfs_path *path;
4533 struct extent_buffer *leaf;
4536 path = btrfs_alloc_path();
4539 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4541 leaf = path->nodes[0];
4542 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4543 write_extent_buffer(leaf, data, ptr, data_size);
4544 btrfs_mark_buffer_dirty(leaf);
4546 btrfs_free_path(path);
4551 * delete the pointer from a given node.
4553 * the tree should have been previously balanced so the deletion does not
4556 static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4557 struct btrfs_path *path, int level, int slot,
4560 struct extent_buffer *parent = path->nodes[level];
4564 nritems = btrfs_header_nritems(parent);
4565 if (slot != nritems - 1) {
4566 if (tree_mod_log && level)
4567 tree_mod_log_eb_move(root->fs_info, parent, slot,
4568 slot + 1, nritems - slot - 1);
4569 memmove_extent_buffer(parent,
4570 btrfs_node_key_ptr_offset(slot),
4571 btrfs_node_key_ptr_offset(slot + 1),
4572 sizeof(struct btrfs_key_ptr) *
4573 (nritems - slot - 1));
4574 } else if (tree_mod_log && level) {
4575 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4576 MOD_LOG_KEY_REMOVE);
4581 btrfs_set_header_nritems(parent, nritems);
4582 if (nritems == 0 && parent == root->node) {
4583 BUG_ON(btrfs_header_level(root->node) != 1);
4584 /* just turn the root into a leaf and break */
4585 btrfs_set_header_level(root->node, 0);
4586 } else if (slot == 0) {
4587 struct btrfs_disk_key disk_key;
4589 btrfs_node_key(parent, &disk_key, 0);
4590 fixup_low_keys(trans, root, path, &disk_key, level + 1);
4592 btrfs_mark_buffer_dirty(parent);
4596 * a helper function to delete the leaf pointed to by path->slots[1] and
4599 * This deletes the pointer in path->nodes[1] and frees the leaf
4600 * block extent. zero is returned if it all worked out, < 0 otherwise.
4602 * The path must have already been setup for deleting the leaf, including
4603 * all the proper balancing. path->nodes[1] must be locked.
4605 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4606 struct btrfs_root *root,
4607 struct btrfs_path *path,
4608 struct extent_buffer *leaf)
4610 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4611 del_ptr(trans, root, path, 1, path->slots[1], 1);
4614 * btrfs_free_extent is expensive, we want to make sure we
4615 * aren't holding any locks when we call it
4617 btrfs_unlock_up_safe(path, 0);
4619 root_sub_used(root, leaf->len);
4621 extent_buffer_get(leaf);
4622 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4623 free_extent_buffer_stale(leaf);
4626 * delete the item at the leaf level in path. If that empties
4627 * the leaf, remove it from the tree
4629 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4630 struct btrfs_path *path, int slot, int nr)
4632 struct extent_buffer *leaf;
4633 struct btrfs_item *item;
4640 struct btrfs_map_token token;
4642 btrfs_init_map_token(&token);
4644 leaf = path->nodes[0];
4645 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4647 for (i = 0; i < nr; i++)
4648 dsize += btrfs_item_size_nr(leaf, slot + i);
4650 nritems = btrfs_header_nritems(leaf);
4652 if (slot + nr != nritems) {
4653 int data_end = leaf_data_end(root, leaf);
4655 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4657 btrfs_leaf_data(leaf) + data_end,
4658 last_off - data_end);
4660 for (i = slot + nr; i < nritems; i++) {
4663 item = btrfs_item_nr(leaf, i);
4664 ioff = btrfs_token_item_offset(leaf, item, &token);
4665 btrfs_set_token_item_offset(leaf, item,
4666 ioff + dsize, &token);
4669 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4670 btrfs_item_nr_offset(slot + nr),
4671 sizeof(struct btrfs_item) *
4672 (nritems - slot - nr));
4674 btrfs_set_header_nritems(leaf, nritems - nr);
4677 /* delete the leaf if we've emptied it */
4679 if (leaf == root->node) {
4680 btrfs_set_header_level(leaf, 0);
4682 btrfs_set_path_blocking(path);
4683 clean_tree_block(trans, root, leaf);
4684 btrfs_del_leaf(trans, root, path, leaf);
4687 int used = leaf_space_used(leaf, 0, nritems);
4689 struct btrfs_disk_key disk_key;
4691 btrfs_item_key(leaf, &disk_key, 0);
4692 fixup_low_keys(trans, root, path, &disk_key, 1);
4695 /* delete the leaf if it is mostly empty */
4696 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
4697 /* push_leaf_left fixes the path.
4698 * make sure the path still points to our leaf
4699 * for possible call to del_ptr below
4701 slot = path->slots[1];
4702 extent_buffer_get(leaf);
4704 btrfs_set_path_blocking(path);
4705 wret = push_leaf_left(trans, root, path, 1, 1,
4707 if (wret < 0 && wret != -ENOSPC)
4710 if (path->nodes[0] == leaf &&
4711 btrfs_header_nritems(leaf)) {
4712 wret = push_leaf_right(trans, root, path, 1,
4714 if (wret < 0 && wret != -ENOSPC)
4718 if (btrfs_header_nritems(leaf) == 0) {
4719 path->slots[1] = slot;
4720 btrfs_del_leaf(trans, root, path, leaf);
4721 free_extent_buffer(leaf);
4724 /* if we're still in the path, make sure
4725 * we're dirty. Otherwise, one of the
4726 * push_leaf functions must have already
4727 * dirtied this buffer
4729 if (path->nodes[0] == leaf)
4730 btrfs_mark_buffer_dirty(leaf);
4731 free_extent_buffer(leaf);
4734 btrfs_mark_buffer_dirty(leaf);
4741 * search the tree again to find a leaf with lesser keys
4742 * returns 0 if it found something or 1 if there are no lesser leaves.
4743 * returns < 0 on io errors.
4745 * This may release the path, and so you may lose any locks held at the
4748 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4750 struct btrfs_key key;
4751 struct btrfs_disk_key found_key;
4754 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
4758 else if (key.type > 0)
4760 else if (key.objectid > 0)
4765 btrfs_release_path(path);
4766 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4769 btrfs_item_key(path->nodes[0], &found_key, 0);
4770 ret = comp_keys(&found_key, &key);
4777 * A helper function to walk down the tree starting at min_key, and looking
4778 * for nodes or leaves that are either in cache or have a minimum
4779 * transaction id. This is used by the btree defrag code, and tree logging
4781 * This does not cow, but it does stuff the starting key it finds back
4782 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4783 * key and get a writable path.
4785 * This does lock as it descends, and path->keep_locks should be set
4786 * to 1 by the caller.
4788 * This honors path->lowest_level to prevent descent past a given level
4791 * min_trans indicates the oldest transaction that you are interested
4792 * in walking through. Any nodes or leaves older than min_trans are
4793 * skipped over (without reading them).
4795 * returns zero if something useful was found, < 0 on error and 1 if there
4796 * was nothing in the tree that matched the search criteria.
4798 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4799 struct btrfs_key *max_key,
4800 struct btrfs_path *path, int cache_only,
4803 struct extent_buffer *cur;
4804 struct btrfs_key found_key;
4811 WARN_ON(!path->keep_locks);
4813 cur = btrfs_read_lock_root_node(root);
4814 level = btrfs_header_level(cur);
4815 WARN_ON(path->nodes[level]);
4816 path->nodes[level] = cur;
4817 path->locks[level] = BTRFS_READ_LOCK;
4819 if (btrfs_header_generation(cur) < min_trans) {
4824 nritems = btrfs_header_nritems(cur);
4825 level = btrfs_header_level(cur);
4826 sret = bin_search(cur, min_key, level, &slot);
4828 /* at the lowest level, we're done, setup the path and exit */
4829 if (level == path->lowest_level) {
4830 if (slot >= nritems)
4833 path->slots[level] = slot;
4834 btrfs_item_key_to_cpu(cur, &found_key, slot);
4837 if (sret && slot > 0)
4840 * check this node pointer against the cache_only and
4841 * min_trans parameters. If it isn't in cache or is too
4842 * old, skip to the next one.
4844 while (slot < nritems) {
4847 struct extent_buffer *tmp;
4848 struct btrfs_disk_key disk_key;
4850 blockptr = btrfs_node_blockptr(cur, slot);
4851 gen = btrfs_node_ptr_generation(cur, slot);
4852 if (gen < min_trans) {
4860 btrfs_node_key(cur, &disk_key, slot);
4861 if (comp_keys(&disk_key, max_key) >= 0) {
4867 tmp = btrfs_find_tree_block(root, blockptr,
4868 btrfs_level_size(root, level - 1));
4870 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
4871 free_extent_buffer(tmp);
4875 free_extent_buffer(tmp);
4880 * we didn't find a candidate key in this node, walk forward
4881 * and find another one
4883 if (slot >= nritems) {
4884 path->slots[level] = slot;
4885 btrfs_set_path_blocking(path);
4886 sret = btrfs_find_next_key(root, path, min_key, level,
4887 cache_only, min_trans);
4889 btrfs_release_path(path);
4895 /* save our key for returning back */
4896 btrfs_node_key_to_cpu(cur, &found_key, slot);
4897 path->slots[level] = slot;
4898 if (level == path->lowest_level) {
4900 unlock_up(path, level, 1, 0, NULL);
4903 btrfs_set_path_blocking(path);
4904 cur = read_node_slot(root, cur, slot);
4905 BUG_ON(!cur); /* -ENOMEM */
4907 btrfs_tree_read_lock(cur);
4909 path->locks[level - 1] = BTRFS_READ_LOCK;
4910 path->nodes[level - 1] = cur;
4911 unlock_up(path, level, 1, 0, NULL);
4912 btrfs_clear_path_blocking(path, NULL, 0);
4916 memcpy(min_key, &found_key, sizeof(found_key));
4917 btrfs_set_path_blocking(path);
4922 * this is similar to btrfs_next_leaf, but does not try to preserve
4923 * and fixup the path. It looks for and returns the next key in the
4924 * tree based on the current path and the cache_only and min_trans
4927 * 0 is returned if another key is found, < 0 if there are any errors
4928 * and 1 is returned if there are no higher keys in the tree
4930 * path->keep_locks should be set to 1 on the search made before
4931 * calling this function.
4933 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4934 struct btrfs_key *key, int level,
4935 int cache_only, u64 min_trans)
4938 struct extent_buffer *c;
4940 WARN_ON(!path->keep_locks);
4941 while (level < BTRFS_MAX_LEVEL) {
4942 if (!path->nodes[level])
4945 slot = path->slots[level] + 1;
4946 c = path->nodes[level];
4948 if (slot >= btrfs_header_nritems(c)) {
4951 struct btrfs_key cur_key;
4952 if (level + 1 >= BTRFS_MAX_LEVEL ||
4953 !path->nodes[level + 1])
4956 if (path->locks[level + 1]) {
4961 slot = btrfs_header_nritems(c) - 1;
4963 btrfs_item_key_to_cpu(c, &cur_key, slot);
4965 btrfs_node_key_to_cpu(c, &cur_key, slot);
4967 orig_lowest = path->lowest_level;
4968 btrfs_release_path(path);
4969 path->lowest_level = level;
4970 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4972 path->lowest_level = orig_lowest;
4976 c = path->nodes[level];
4977 slot = path->slots[level];
4984 btrfs_item_key_to_cpu(c, key, slot);
4986 u64 blockptr = btrfs_node_blockptr(c, slot);
4987 u64 gen = btrfs_node_ptr_generation(c, slot);
4990 struct extent_buffer *cur;
4991 cur = btrfs_find_tree_block(root, blockptr,
4992 btrfs_level_size(root, level - 1));
4994 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
4997 free_extent_buffer(cur);
5000 free_extent_buffer(cur);
5002 if (gen < min_trans) {
5006 btrfs_node_key_to_cpu(c, key, slot);
5014 * search the tree again to find a leaf with greater keys
5015 * returns 0 if it found something or 1 if there are no greater leaves.
5016 * returns < 0 on io errors.
5018 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5022 struct extent_buffer *c;
5023 struct extent_buffer *next;
5024 struct btrfs_key key;
5027 int old_spinning = path->leave_spinning;
5028 int next_rw_lock = 0;
5030 nritems = btrfs_header_nritems(path->nodes[0]);
5034 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5039 btrfs_release_path(path);
5041 path->keep_locks = 1;
5042 path->leave_spinning = 1;
5044 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5045 path->keep_locks = 0;
5050 nritems = btrfs_header_nritems(path->nodes[0]);
5052 * by releasing the path above we dropped all our locks. A balance
5053 * could have added more items next to the key that used to be
5054 * at the very end of the block. So, check again here and
5055 * advance the path if there are now more items available.
5057 if (nritems > 0 && path->slots[0] < nritems - 1) {
5064 while (level < BTRFS_MAX_LEVEL) {
5065 if (!path->nodes[level]) {
5070 slot = path->slots[level] + 1;
5071 c = path->nodes[level];
5072 if (slot >= btrfs_header_nritems(c)) {
5074 if (level == BTRFS_MAX_LEVEL) {
5082 btrfs_tree_unlock_rw(next, next_rw_lock);
5083 free_extent_buffer(next);
5087 next_rw_lock = path->locks[level];
5088 ret = read_block_for_search(NULL, root, path, &next, level,
5094 btrfs_release_path(path);
5098 if (!path->skip_locking) {
5099 ret = btrfs_try_tree_read_lock(next);
5101 btrfs_set_path_blocking(path);
5102 btrfs_tree_read_lock(next);
5103 btrfs_clear_path_blocking(path, next,
5106 next_rw_lock = BTRFS_READ_LOCK;
5110 path->slots[level] = slot;
5113 c = path->nodes[level];
5114 if (path->locks[level])
5115 btrfs_tree_unlock_rw(c, path->locks[level]);
5117 free_extent_buffer(c);
5118 path->nodes[level] = next;
5119 path->slots[level] = 0;
5120 if (!path->skip_locking)
5121 path->locks[level] = next_rw_lock;
5125 ret = read_block_for_search(NULL, root, path, &next, level,
5131 btrfs_release_path(path);
5135 if (!path->skip_locking) {
5136 ret = btrfs_try_tree_read_lock(next);
5138 btrfs_set_path_blocking(path);
5139 btrfs_tree_read_lock(next);
5140 btrfs_clear_path_blocking(path, next,
5143 next_rw_lock = BTRFS_READ_LOCK;
5148 unlock_up(path, 0, 1, 0, NULL);
5149 path->leave_spinning = old_spinning;
5151 btrfs_set_path_blocking(path);
5157 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5158 * searching until it gets past min_objectid or finds an item of 'type'
5160 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5162 int btrfs_previous_item(struct btrfs_root *root,
5163 struct btrfs_path *path, u64 min_objectid,
5166 struct btrfs_key found_key;
5167 struct extent_buffer *leaf;
5172 if (path->slots[0] == 0) {
5173 btrfs_set_path_blocking(path);
5174 ret = btrfs_prev_leaf(root, path);
5180 leaf = path->nodes[0];
5181 nritems = btrfs_header_nritems(leaf);
5184 if (path->slots[0] == nritems)
5187 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5188 if (found_key.objectid < min_objectid)
5190 if (found_key.type == type)
5192 if (found_key.objectid == min_objectid &&
5193 found_key.type < type)