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);
1030 * tm is a pointer to the first operation to rewind within eb. then, all
1031 * previous operations will be rewinded (until we reach something older than
1035 __tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1036 struct tree_mod_elem *first_tm)
1039 struct rb_node *next;
1040 struct tree_mod_elem *tm = first_tm;
1041 unsigned long o_dst;
1042 unsigned long o_src;
1043 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1045 n = btrfs_header_nritems(eb);
1046 while (tm && tm->elem.seq >= time_seq) {
1048 * all the operations are recorded with the operator used for
1049 * the modification. as we're going backwards, we do the
1050 * opposite of each operation here.
1053 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1054 BUG_ON(tm->slot < n);
1055 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1056 case MOD_LOG_KEY_REMOVE:
1057 btrfs_set_node_key(eb, &tm->key, tm->slot);
1058 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1059 btrfs_set_node_ptr_generation(eb, tm->slot,
1063 case MOD_LOG_KEY_REPLACE:
1064 BUG_ON(tm->slot >= n);
1065 btrfs_set_node_key(eb, &tm->key, tm->slot);
1066 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1067 btrfs_set_node_ptr_generation(eb, tm->slot,
1070 case MOD_LOG_KEY_ADD:
1071 if (tm->slot != n - 1) {
1072 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1073 o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
1074 memmove_extent_buffer(eb, o_dst, o_src, p_size);
1078 case MOD_LOG_MOVE_KEYS:
1079 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1080 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1081 memmove_extent_buffer(eb, o_dst, o_src,
1082 tm->move.nr_items * p_size);
1084 case MOD_LOG_ROOT_REPLACE:
1086 * this operation is special. for roots, this must be
1087 * handled explicitly before rewinding.
1088 * for non-roots, this operation may exist if the node
1089 * was a root: root A -> child B; then A gets empty and
1090 * B is promoted to the new root. in the mod log, we'll
1091 * have a root-replace operation for B, a tree block
1092 * that is no root. we simply ignore that operation.
1096 next = rb_next(&tm->node);
1099 tm = container_of(next, struct tree_mod_elem, node);
1100 if (tm->index != first_tm->index)
1103 btrfs_set_header_nritems(eb, n);
1106 static struct extent_buffer *
1107 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1110 struct extent_buffer *eb_rewin;
1111 struct tree_mod_elem *tm;
1116 if (btrfs_header_level(eb) == 0)
1119 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1123 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1124 BUG_ON(tm->slot != 0);
1125 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1126 fs_info->tree_root->nodesize);
1128 btrfs_set_header_bytenr(eb_rewin, eb->start);
1129 btrfs_set_header_backref_rev(eb_rewin,
1130 btrfs_header_backref_rev(eb));
1131 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1132 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1134 eb_rewin = btrfs_clone_extent_buffer(eb);
1138 extent_buffer_get(eb_rewin);
1139 free_extent_buffer(eb);
1141 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1146 static inline struct extent_buffer *
1147 get_old_root(struct btrfs_root *root, u64 time_seq)
1149 struct tree_mod_elem *tm;
1150 struct extent_buffer *eb;
1151 struct tree_mod_root *old_root;
1154 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1158 old_root = &tm->old_root;
1159 old_generation = tm->generation;
1161 tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
1163 * there was an item in the log when __tree_mod_log_oldest_root
1164 * returned. this one must not go away, because the time_seq passed to
1165 * us must be blocking its removal.
1169 if (old_root->logical == root->node->start) {
1170 /* there are logged operations for the current root */
1171 eb = btrfs_clone_extent_buffer(root->node);
1173 /* there's a root replace operation for the current root */
1174 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1176 btrfs_set_header_bytenr(eb, eb->start);
1177 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1178 btrfs_set_header_owner(eb, root->root_key.objectid);
1182 btrfs_set_header_level(eb, old_root->level);
1183 btrfs_set_header_generation(eb, old_generation);
1184 __tree_mod_log_rewind(eb, time_seq, tm);
1189 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1190 struct btrfs_root *root,
1191 struct extent_buffer *buf)
1193 /* ensure we can see the force_cow */
1197 * We do not need to cow a block if
1198 * 1) this block is not created or changed in this transaction;
1199 * 2) this block does not belong to TREE_RELOC tree;
1200 * 3) the root is not forced COW.
1202 * What is forced COW:
1203 * when we create snapshot during commiting the transaction,
1204 * after we've finished coping src root, we must COW the shared
1205 * block to ensure the metadata consistency.
1207 if (btrfs_header_generation(buf) == trans->transid &&
1208 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1209 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1210 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1217 * cows a single block, see __btrfs_cow_block for the real work.
1218 * This version of it has extra checks so that a block isn't cow'd more than
1219 * once per transaction, as long as it hasn't been written yet
1221 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1222 struct btrfs_root *root, struct extent_buffer *buf,
1223 struct extent_buffer *parent, int parent_slot,
1224 struct extent_buffer **cow_ret)
1229 if (trans->transaction != root->fs_info->running_transaction) {
1230 printk(KERN_CRIT "trans %llu running %llu\n",
1231 (unsigned long long)trans->transid,
1232 (unsigned long long)
1233 root->fs_info->running_transaction->transid);
1236 if (trans->transid != root->fs_info->generation) {
1237 printk(KERN_CRIT "trans %llu running %llu\n",
1238 (unsigned long long)trans->transid,
1239 (unsigned long long)root->fs_info->generation);
1243 if (!should_cow_block(trans, root, buf)) {
1248 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
1251 btrfs_set_lock_blocking(parent);
1252 btrfs_set_lock_blocking(buf);
1254 ret = __btrfs_cow_block(trans, root, buf, parent,
1255 parent_slot, cow_ret, search_start, 0);
1257 trace_btrfs_cow_block(root, buf, *cow_ret);
1263 * helper function for defrag to decide if two blocks pointed to by a
1264 * node are actually close by
1266 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1268 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1270 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1276 * compare two keys in a memcmp fashion
1278 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1280 struct btrfs_key k1;
1282 btrfs_disk_key_to_cpu(&k1, disk);
1284 return btrfs_comp_cpu_keys(&k1, k2);
1288 * same as comp_keys only with two btrfs_key's
1290 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
1292 if (k1->objectid > k2->objectid)
1294 if (k1->objectid < k2->objectid)
1296 if (k1->type > k2->type)
1298 if (k1->type < k2->type)
1300 if (k1->offset > k2->offset)
1302 if (k1->offset < k2->offset)
1308 * this is used by the defrag code to go through all the
1309 * leaves pointed to by a node and reallocate them so that
1310 * disk order is close to key order
1312 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1313 struct btrfs_root *root, struct extent_buffer *parent,
1314 int start_slot, int cache_only, u64 *last_ret,
1315 struct btrfs_key *progress)
1317 struct extent_buffer *cur;
1320 u64 search_start = *last_ret;
1330 int progress_passed = 0;
1331 struct btrfs_disk_key disk_key;
1333 parent_level = btrfs_header_level(parent);
1334 if (cache_only && parent_level != 1)
1337 if (trans->transaction != root->fs_info->running_transaction)
1339 if (trans->transid != root->fs_info->generation)
1342 parent_nritems = btrfs_header_nritems(parent);
1343 blocksize = btrfs_level_size(root, parent_level - 1);
1344 end_slot = parent_nritems;
1346 if (parent_nritems == 1)
1349 btrfs_set_lock_blocking(parent);
1351 for (i = start_slot; i < end_slot; i++) {
1354 btrfs_node_key(parent, &disk_key, i);
1355 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1358 progress_passed = 1;
1359 blocknr = btrfs_node_blockptr(parent, i);
1360 gen = btrfs_node_ptr_generation(parent, i);
1361 if (last_block == 0)
1362 last_block = blocknr;
1365 other = btrfs_node_blockptr(parent, i - 1);
1366 close = close_blocks(blocknr, other, blocksize);
1368 if (!close && i < end_slot - 2) {
1369 other = btrfs_node_blockptr(parent, i + 1);
1370 close = close_blocks(blocknr, other, blocksize);
1373 last_block = blocknr;
1377 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1379 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1382 if (!cur || !uptodate) {
1384 free_extent_buffer(cur);
1388 cur = read_tree_block(root, blocknr,
1392 } else if (!uptodate) {
1393 err = btrfs_read_buffer(cur, gen);
1395 free_extent_buffer(cur);
1400 if (search_start == 0)
1401 search_start = last_block;
1403 btrfs_tree_lock(cur);
1404 btrfs_set_lock_blocking(cur);
1405 err = __btrfs_cow_block(trans, root, cur, parent, i,
1408 (end_slot - i) * blocksize));
1410 btrfs_tree_unlock(cur);
1411 free_extent_buffer(cur);
1414 search_start = cur->start;
1415 last_block = cur->start;
1416 *last_ret = search_start;
1417 btrfs_tree_unlock(cur);
1418 free_extent_buffer(cur);
1424 * The leaf data grows from end-to-front in the node.
1425 * this returns the address of the start of the last item,
1426 * which is the stop of the leaf data stack
1428 static inline unsigned int leaf_data_end(struct btrfs_root *root,
1429 struct extent_buffer *leaf)
1431 u32 nr = btrfs_header_nritems(leaf);
1433 return BTRFS_LEAF_DATA_SIZE(root);
1434 return btrfs_item_offset_nr(leaf, nr - 1);
1439 * search for key in the extent_buffer. The items start at offset p,
1440 * and they are item_size apart. There are 'max' items in p.
1442 * the slot in the array is returned via slot, and it points to
1443 * the place where you would insert key if it is not found in
1446 * slot may point to max if the key is bigger than all of the keys
1448 static noinline int generic_bin_search(struct extent_buffer *eb,
1450 int item_size, struct btrfs_key *key,
1457 struct btrfs_disk_key *tmp = NULL;
1458 struct btrfs_disk_key unaligned;
1459 unsigned long offset;
1461 unsigned long map_start = 0;
1462 unsigned long map_len = 0;
1465 while (low < high) {
1466 mid = (low + high) / 2;
1467 offset = p + mid * item_size;
1469 if (!kaddr || offset < map_start ||
1470 (offset + sizeof(struct btrfs_disk_key)) >
1471 map_start + map_len) {
1473 err = map_private_extent_buffer(eb, offset,
1474 sizeof(struct btrfs_disk_key),
1475 &kaddr, &map_start, &map_len);
1478 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1481 read_extent_buffer(eb, &unaligned,
1482 offset, sizeof(unaligned));
1487 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1490 ret = comp_keys(tmp, key);
1506 * simple bin_search frontend that does the right thing for
1509 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1510 int level, int *slot)
1513 return generic_bin_search(eb,
1514 offsetof(struct btrfs_leaf, items),
1515 sizeof(struct btrfs_item),
1516 key, btrfs_header_nritems(eb),
1519 return generic_bin_search(eb,
1520 offsetof(struct btrfs_node, ptrs),
1521 sizeof(struct btrfs_key_ptr),
1522 key, btrfs_header_nritems(eb),
1526 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1527 int level, int *slot)
1529 return bin_search(eb, key, level, slot);
1532 static void root_add_used(struct btrfs_root *root, u32 size)
1534 spin_lock(&root->accounting_lock);
1535 btrfs_set_root_used(&root->root_item,
1536 btrfs_root_used(&root->root_item) + size);
1537 spin_unlock(&root->accounting_lock);
1540 static void root_sub_used(struct btrfs_root *root, u32 size)
1542 spin_lock(&root->accounting_lock);
1543 btrfs_set_root_used(&root->root_item,
1544 btrfs_root_used(&root->root_item) - size);
1545 spin_unlock(&root->accounting_lock);
1548 /* given a node and slot number, this reads the blocks it points to. The
1549 * extent buffer is returned with a reference taken (but unlocked).
1550 * NULL is returned on error.
1552 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
1553 struct extent_buffer *parent, int slot)
1555 int level = btrfs_header_level(parent);
1558 if (slot >= btrfs_header_nritems(parent))
1563 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
1564 btrfs_level_size(root, level - 1),
1565 btrfs_node_ptr_generation(parent, slot));
1569 * node level balancing, used to make sure nodes are in proper order for
1570 * item deletion. We balance from the top down, so we have to make sure
1571 * that a deletion won't leave an node completely empty later on.
1573 static noinline int balance_level(struct btrfs_trans_handle *trans,
1574 struct btrfs_root *root,
1575 struct btrfs_path *path, int level)
1577 struct extent_buffer *right = NULL;
1578 struct extent_buffer *mid;
1579 struct extent_buffer *left = NULL;
1580 struct extent_buffer *parent = NULL;
1584 int orig_slot = path->slots[level];
1590 mid = path->nodes[level];
1592 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1593 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1594 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1596 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1598 if (level < BTRFS_MAX_LEVEL - 1) {
1599 parent = path->nodes[level + 1];
1600 pslot = path->slots[level + 1];
1604 * deal with the case where there is only one pointer in the root
1605 * by promoting the node below to a root
1608 struct extent_buffer *child;
1610 if (btrfs_header_nritems(mid) != 1)
1613 /* promote the child to a root */
1614 child = read_node_slot(root, mid, 0);
1617 btrfs_std_error(root->fs_info, ret);
1621 btrfs_tree_lock(child);
1622 btrfs_set_lock_blocking(child);
1623 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1625 btrfs_tree_unlock(child);
1626 free_extent_buffer(child);
1630 tree_mod_log_set_root_pointer(root, child);
1631 rcu_assign_pointer(root->node, child);
1633 add_root_to_dirty_list(root);
1634 btrfs_tree_unlock(child);
1636 path->locks[level] = 0;
1637 path->nodes[level] = NULL;
1638 clean_tree_block(trans, root, mid);
1639 btrfs_tree_unlock(mid);
1640 /* once for the path */
1641 free_extent_buffer(mid);
1643 root_sub_used(root, mid->len);
1644 btrfs_free_tree_block(trans, root, mid, 0, 1);
1645 /* once for the root ptr */
1646 free_extent_buffer_stale(mid);
1649 if (btrfs_header_nritems(mid) >
1650 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
1653 left = read_node_slot(root, parent, pslot - 1);
1655 btrfs_tree_lock(left);
1656 btrfs_set_lock_blocking(left);
1657 wret = btrfs_cow_block(trans, root, left,
1658 parent, pslot - 1, &left);
1664 right = read_node_slot(root, parent, pslot + 1);
1666 btrfs_tree_lock(right);
1667 btrfs_set_lock_blocking(right);
1668 wret = btrfs_cow_block(trans, root, right,
1669 parent, pslot + 1, &right);
1676 /* first, try to make some room in the middle buffer */
1678 orig_slot += btrfs_header_nritems(left);
1679 wret = push_node_left(trans, root, left, mid, 1);
1685 * then try to empty the right most buffer into the middle
1688 wret = push_node_left(trans, root, mid, right, 1);
1689 if (wret < 0 && wret != -ENOSPC)
1691 if (btrfs_header_nritems(right) == 0) {
1692 clean_tree_block(trans, root, right);
1693 btrfs_tree_unlock(right);
1694 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
1695 root_sub_used(root, right->len);
1696 btrfs_free_tree_block(trans, root, right, 0, 1);
1697 free_extent_buffer_stale(right);
1700 struct btrfs_disk_key right_key;
1701 btrfs_node_key(right, &right_key, 0);
1702 tree_mod_log_set_node_key(root->fs_info, parent,
1703 &right_key, pslot + 1, 0);
1704 btrfs_set_node_key(parent, &right_key, pslot + 1);
1705 btrfs_mark_buffer_dirty(parent);
1708 if (btrfs_header_nritems(mid) == 1) {
1710 * we're not allowed to leave a node with one item in the
1711 * tree during a delete. A deletion from lower in the tree
1712 * could try to delete the only pointer in this node.
1713 * So, pull some keys from the left.
1714 * There has to be a left pointer at this point because
1715 * otherwise we would have pulled some pointers from the
1720 btrfs_std_error(root->fs_info, ret);
1723 wret = balance_node_right(trans, root, mid, left);
1729 wret = push_node_left(trans, root, left, mid, 1);
1735 if (btrfs_header_nritems(mid) == 0) {
1736 clean_tree_block(trans, root, mid);
1737 btrfs_tree_unlock(mid);
1738 del_ptr(trans, root, path, level + 1, pslot, 1);
1739 root_sub_used(root, mid->len);
1740 btrfs_free_tree_block(trans, root, mid, 0, 1);
1741 free_extent_buffer_stale(mid);
1744 /* update the parent key to reflect our changes */
1745 struct btrfs_disk_key mid_key;
1746 btrfs_node_key(mid, &mid_key, 0);
1747 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1749 btrfs_set_node_key(parent, &mid_key, pslot);
1750 btrfs_mark_buffer_dirty(parent);
1753 /* update the path */
1755 if (btrfs_header_nritems(left) > orig_slot) {
1756 extent_buffer_get(left);
1757 /* left was locked after cow */
1758 path->nodes[level] = left;
1759 path->slots[level + 1] -= 1;
1760 path->slots[level] = orig_slot;
1762 btrfs_tree_unlock(mid);
1763 free_extent_buffer(mid);
1766 orig_slot -= btrfs_header_nritems(left);
1767 path->slots[level] = orig_slot;
1770 /* double check we haven't messed things up */
1772 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1776 btrfs_tree_unlock(right);
1777 free_extent_buffer(right);
1780 if (path->nodes[level] != left)
1781 btrfs_tree_unlock(left);
1782 free_extent_buffer(left);
1787 /* Node balancing for insertion. Here we only split or push nodes around
1788 * when they are completely full. This is also done top down, so we
1789 * have to be pessimistic.
1791 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1792 struct btrfs_root *root,
1793 struct btrfs_path *path, int level)
1795 struct extent_buffer *right = NULL;
1796 struct extent_buffer *mid;
1797 struct extent_buffer *left = NULL;
1798 struct extent_buffer *parent = NULL;
1802 int orig_slot = path->slots[level];
1807 mid = path->nodes[level];
1808 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1810 if (level < BTRFS_MAX_LEVEL - 1) {
1811 parent = path->nodes[level + 1];
1812 pslot = path->slots[level + 1];
1818 left = read_node_slot(root, parent, pslot - 1);
1820 /* first, try to make some room in the middle buffer */
1824 btrfs_tree_lock(left);
1825 btrfs_set_lock_blocking(left);
1827 left_nr = btrfs_header_nritems(left);
1828 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1831 ret = btrfs_cow_block(trans, root, left, parent,
1836 wret = push_node_left(trans, root,
1843 struct btrfs_disk_key disk_key;
1844 orig_slot += left_nr;
1845 btrfs_node_key(mid, &disk_key, 0);
1846 tree_mod_log_set_node_key(root->fs_info, parent,
1847 &disk_key, pslot, 0);
1848 btrfs_set_node_key(parent, &disk_key, pslot);
1849 btrfs_mark_buffer_dirty(parent);
1850 if (btrfs_header_nritems(left) > orig_slot) {
1851 path->nodes[level] = left;
1852 path->slots[level + 1] -= 1;
1853 path->slots[level] = orig_slot;
1854 btrfs_tree_unlock(mid);
1855 free_extent_buffer(mid);
1858 btrfs_header_nritems(left);
1859 path->slots[level] = orig_slot;
1860 btrfs_tree_unlock(left);
1861 free_extent_buffer(left);
1865 btrfs_tree_unlock(left);
1866 free_extent_buffer(left);
1868 right = read_node_slot(root, parent, pslot + 1);
1871 * then try to empty the right most buffer into the middle
1876 btrfs_tree_lock(right);
1877 btrfs_set_lock_blocking(right);
1879 right_nr = btrfs_header_nritems(right);
1880 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1883 ret = btrfs_cow_block(trans, root, right,
1889 wret = balance_node_right(trans, root,
1896 struct btrfs_disk_key disk_key;
1898 btrfs_node_key(right, &disk_key, 0);
1899 tree_mod_log_set_node_key(root->fs_info, parent,
1900 &disk_key, pslot + 1, 0);
1901 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1902 btrfs_mark_buffer_dirty(parent);
1904 if (btrfs_header_nritems(mid) <= orig_slot) {
1905 path->nodes[level] = right;
1906 path->slots[level + 1] += 1;
1907 path->slots[level] = orig_slot -
1908 btrfs_header_nritems(mid);
1909 btrfs_tree_unlock(mid);
1910 free_extent_buffer(mid);
1912 btrfs_tree_unlock(right);
1913 free_extent_buffer(right);
1917 btrfs_tree_unlock(right);
1918 free_extent_buffer(right);
1924 * readahead one full node of leaves, finding things that are close
1925 * to the block in 'slot', and triggering ra on them.
1927 static void reada_for_search(struct btrfs_root *root,
1928 struct btrfs_path *path,
1929 int level, int slot, u64 objectid)
1931 struct extent_buffer *node;
1932 struct btrfs_disk_key disk_key;
1938 int direction = path->reada;
1939 struct extent_buffer *eb;
1947 if (!path->nodes[level])
1950 node = path->nodes[level];
1952 search = btrfs_node_blockptr(node, slot);
1953 blocksize = btrfs_level_size(root, level - 1);
1954 eb = btrfs_find_tree_block(root, search, blocksize);
1956 free_extent_buffer(eb);
1962 nritems = btrfs_header_nritems(node);
1966 if (direction < 0) {
1970 } else if (direction > 0) {
1975 if (path->reada < 0 && objectid) {
1976 btrfs_node_key(node, &disk_key, nr);
1977 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1980 search = btrfs_node_blockptr(node, nr);
1981 if ((search <= target && target - search <= 65536) ||
1982 (search > target && search - target <= 65536)) {
1983 gen = btrfs_node_ptr_generation(node, nr);
1984 readahead_tree_block(root, search, blocksize, gen);
1988 if ((nread > 65536 || nscan > 32))
1994 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1997 static noinline int reada_for_balance(struct btrfs_root *root,
1998 struct btrfs_path *path, int level)
2002 struct extent_buffer *parent;
2003 struct extent_buffer *eb;
2010 parent = path->nodes[level + 1];
2014 nritems = btrfs_header_nritems(parent);
2015 slot = path->slots[level + 1];
2016 blocksize = btrfs_level_size(root, level);
2019 block1 = btrfs_node_blockptr(parent, slot - 1);
2020 gen = btrfs_node_ptr_generation(parent, slot - 1);
2021 eb = btrfs_find_tree_block(root, block1, blocksize);
2023 * if we get -eagain from btrfs_buffer_uptodate, we
2024 * don't want to return eagain here. That will loop
2027 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2029 free_extent_buffer(eb);
2031 if (slot + 1 < nritems) {
2032 block2 = btrfs_node_blockptr(parent, slot + 1);
2033 gen = btrfs_node_ptr_generation(parent, slot + 1);
2034 eb = btrfs_find_tree_block(root, block2, blocksize);
2035 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2037 free_extent_buffer(eb);
2039 if (block1 || block2) {
2042 /* release the whole path */
2043 btrfs_release_path(path);
2045 /* read the blocks */
2047 readahead_tree_block(root, block1, blocksize, 0);
2049 readahead_tree_block(root, block2, blocksize, 0);
2052 eb = read_tree_block(root, block1, blocksize, 0);
2053 free_extent_buffer(eb);
2056 eb = read_tree_block(root, block2, blocksize, 0);
2057 free_extent_buffer(eb);
2065 * when we walk down the tree, it is usually safe to unlock the higher layers
2066 * in the tree. The exceptions are when our path goes through slot 0, because
2067 * operations on the tree might require changing key pointers higher up in the
2070 * callers might also have set path->keep_locks, which tells this code to keep
2071 * the lock if the path points to the last slot in the block. This is part of
2072 * walking through the tree, and selecting the next slot in the higher block.
2074 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2075 * if lowest_unlock is 1, level 0 won't be unlocked
2077 static noinline void unlock_up(struct btrfs_path *path, int level,
2078 int lowest_unlock, int min_write_lock_level,
2079 int *write_lock_level)
2082 int skip_level = level;
2084 struct extent_buffer *t;
2086 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2087 if (!path->nodes[i])
2089 if (!path->locks[i])
2091 if (!no_skips && path->slots[i] == 0) {
2095 if (!no_skips && path->keep_locks) {
2098 nritems = btrfs_header_nritems(t);
2099 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2104 if (skip_level < i && i >= lowest_unlock)
2108 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
2109 btrfs_tree_unlock_rw(t, path->locks[i]);
2111 if (write_lock_level &&
2112 i > min_write_lock_level &&
2113 i <= *write_lock_level) {
2114 *write_lock_level = i - 1;
2121 * This releases any locks held in the path starting at level and
2122 * going all the way up to the root.
2124 * btrfs_search_slot will keep the lock held on higher nodes in a few
2125 * corner cases, such as COW of the block at slot zero in the node. This
2126 * ignores those rules, and it should only be called when there are no
2127 * more updates to be done higher up in the tree.
2129 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2133 if (path->keep_locks)
2136 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2137 if (!path->nodes[i])
2139 if (!path->locks[i])
2141 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2147 * helper function for btrfs_search_slot. The goal is to find a block
2148 * in cache without setting the path to blocking. If we find the block
2149 * we return zero and the path is unchanged.
2151 * If we can't find the block, we set the path blocking and do some
2152 * reada. -EAGAIN is returned and the search must be repeated.
2155 read_block_for_search(struct btrfs_trans_handle *trans,
2156 struct btrfs_root *root, struct btrfs_path *p,
2157 struct extent_buffer **eb_ret, int level, int slot,
2158 struct btrfs_key *key, u64 time_seq)
2163 struct extent_buffer *b = *eb_ret;
2164 struct extent_buffer *tmp;
2167 blocknr = btrfs_node_blockptr(b, slot);
2168 gen = btrfs_node_ptr_generation(b, slot);
2169 blocksize = btrfs_level_size(root, level - 1);
2171 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
2173 /* first we do an atomic uptodate check */
2174 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2175 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2177 * we found an up to date block without
2184 /* the pages were up to date, but we failed
2185 * the generation number check. Do a full
2186 * read for the generation number that is correct.
2187 * We must do this without dropping locks so
2188 * we can trust our generation number
2190 free_extent_buffer(tmp);
2191 btrfs_set_path_blocking(p);
2193 /* now we're allowed to do a blocking uptodate check */
2194 tmp = read_tree_block(root, blocknr, blocksize, gen);
2195 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
2199 free_extent_buffer(tmp);
2200 btrfs_release_path(p);
2206 * reduce lock contention at high levels
2207 * of the btree by dropping locks before
2208 * we read. Don't release the lock on the current
2209 * level because we need to walk this node to figure
2210 * out which blocks to read.
2212 btrfs_unlock_up_safe(p, level + 1);
2213 btrfs_set_path_blocking(p);
2215 free_extent_buffer(tmp);
2217 reada_for_search(root, p, level, slot, key->objectid);
2219 btrfs_release_path(p);
2222 tmp = read_tree_block(root, blocknr, blocksize, 0);
2225 * If the read above didn't mark this buffer up to date,
2226 * it will never end up being up to date. Set ret to EIO now
2227 * and give up so that our caller doesn't loop forever
2230 if (!btrfs_buffer_uptodate(tmp, 0, 0))
2232 free_extent_buffer(tmp);
2238 * helper function for btrfs_search_slot. This does all of the checks
2239 * for node-level blocks and does any balancing required based on
2242 * If no extra work was required, zero is returned. If we had to
2243 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2247 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2248 struct btrfs_root *root, struct btrfs_path *p,
2249 struct extent_buffer *b, int level, int ins_len,
2250 int *write_lock_level)
2253 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2254 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2257 if (*write_lock_level < level + 1) {
2258 *write_lock_level = level + 1;
2259 btrfs_release_path(p);
2263 sret = reada_for_balance(root, p, level);
2267 btrfs_set_path_blocking(p);
2268 sret = split_node(trans, root, p, level);
2269 btrfs_clear_path_blocking(p, NULL, 0);
2276 b = p->nodes[level];
2277 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2278 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
2281 if (*write_lock_level < level + 1) {
2282 *write_lock_level = level + 1;
2283 btrfs_release_path(p);
2287 sret = reada_for_balance(root, p, level);
2291 btrfs_set_path_blocking(p);
2292 sret = balance_level(trans, root, p, level);
2293 btrfs_clear_path_blocking(p, NULL, 0);
2299 b = p->nodes[level];
2301 btrfs_release_path(p);
2304 BUG_ON(btrfs_header_nritems(b) == 1);
2315 * look for key in the tree. path is filled in with nodes along the way
2316 * if key is found, we return zero and you can find the item in the leaf
2317 * level of the path (level 0)
2319 * If the key isn't found, the path points to the slot where it should
2320 * be inserted, and 1 is returned. If there are other errors during the
2321 * search a negative error number is returned.
2323 * if ins_len > 0, nodes and leaves will be split as we walk down the
2324 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2327 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2328 *root, struct btrfs_key *key, struct btrfs_path *p, int
2331 struct extent_buffer *b;
2336 int lowest_unlock = 1;
2338 /* everything at write_lock_level or lower must be write locked */
2339 int write_lock_level = 0;
2340 u8 lowest_level = 0;
2341 int min_write_lock_level;
2343 lowest_level = p->lowest_level;
2344 WARN_ON(lowest_level && ins_len > 0);
2345 WARN_ON(p->nodes[0] != NULL);
2350 /* when we are removing items, we might have to go up to level
2351 * two as we update tree pointers Make sure we keep write
2352 * for those levels as well
2354 write_lock_level = 2;
2355 } else if (ins_len > 0) {
2357 * for inserting items, make sure we have a write lock on
2358 * level 1 so we can update keys
2360 write_lock_level = 1;
2364 write_lock_level = -1;
2366 if (cow && (p->keep_locks || p->lowest_level))
2367 write_lock_level = BTRFS_MAX_LEVEL;
2369 min_write_lock_level = write_lock_level;
2373 * we try very hard to do read locks on the root
2375 root_lock = BTRFS_READ_LOCK;
2377 if (p->search_commit_root) {
2379 * the commit roots are read only
2380 * so we always do read locks
2382 b = root->commit_root;
2383 extent_buffer_get(b);
2384 level = btrfs_header_level(b);
2385 if (!p->skip_locking)
2386 btrfs_tree_read_lock(b);
2388 if (p->skip_locking) {
2389 b = btrfs_root_node(root);
2390 level = btrfs_header_level(b);
2392 /* we don't know the level of the root node
2393 * until we actually have it read locked
2395 b = btrfs_read_lock_root_node(root);
2396 level = btrfs_header_level(b);
2397 if (level <= write_lock_level) {
2398 /* whoops, must trade for write lock */
2399 btrfs_tree_read_unlock(b);
2400 free_extent_buffer(b);
2401 b = btrfs_lock_root_node(root);
2402 root_lock = BTRFS_WRITE_LOCK;
2404 /* the level might have changed, check again */
2405 level = btrfs_header_level(b);
2409 p->nodes[level] = b;
2410 if (!p->skip_locking)
2411 p->locks[level] = root_lock;
2414 level = btrfs_header_level(b);
2417 * setup the path here so we can release it under lock
2418 * contention with the cow code
2422 * if we don't really need to cow this block
2423 * then we don't want to set the path blocking,
2424 * so we test it here
2426 if (!should_cow_block(trans, root, b))
2429 btrfs_set_path_blocking(p);
2432 * must have write locks on this node and the
2435 if (level + 1 > write_lock_level) {
2436 write_lock_level = level + 1;
2437 btrfs_release_path(p);
2441 err = btrfs_cow_block(trans, root, b,
2442 p->nodes[level + 1],
2443 p->slots[level + 1], &b);
2450 BUG_ON(!cow && ins_len);
2452 p->nodes[level] = b;
2453 btrfs_clear_path_blocking(p, NULL, 0);
2456 * we have a lock on b and as long as we aren't changing
2457 * the tree, there is no way to for the items in b to change.
2458 * It is safe to drop the lock on our parent before we
2459 * go through the expensive btree search on b.
2461 * If cow is true, then we might be changing slot zero,
2462 * which may require changing the parent. So, we can't
2463 * drop the lock until after we know which slot we're
2467 btrfs_unlock_up_safe(p, level + 1);
2469 ret = bin_search(b, key, level, &slot);
2473 if (ret && slot > 0) {
2477 p->slots[level] = slot;
2478 err = setup_nodes_for_search(trans, root, p, b, level,
2479 ins_len, &write_lock_level);
2486 b = p->nodes[level];
2487 slot = p->slots[level];
2490 * slot 0 is special, if we change the key
2491 * we have to update the parent pointer
2492 * which means we must have a write lock
2495 if (slot == 0 && cow &&
2496 write_lock_level < level + 1) {
2497 write_lock_level = level + 1;
2498 btrfs_release_path(p);
2502 unlock_up(p, level, lowest_unlock,
2503 min_write_lock_level, &write_lock_level);
2505 if (level == lowest_level) {
2511 err = read_block_for_search(trans, root, p,
2512 &b, level, slot, key, 0);
2520 if (!p->skip_locking) {
2521 level = btrfs_header_level(b);
2522 if (level <= write_lock_level) {
2523 err = btrfs_try_tree_write_lock(b);
2525 btrfs_set_path_blocking(p);
2527 btrfs_clear_path_blocking(p, b,
2530 p->locks[level] = BTRFS_WRITE_LOCK;
2532 err = btrfs_try_tree_read_lock(b);
2534 btrfs_set_path_blocking(p);
2535 btrfs_tree_read_lock(b);
2536 btrfs_clear_path_blocking(p, b,
2539 p->locks[level] = BTRFS_READ_LOCK;
2541 p->nodes[level] = b;
2544 p->slots[level] = slot;
2546 btrfs_leaf_free_space(root, b) < ins_len) {
2547 if (write_lock_level < 1) {
2548 write_lock_level = 1;
2549 btrfs_release_path(p);
2553 btrfs_set_path_blocking(p);
2554 err = split_leaf(trans, root, key,
2555 p, ins_len, ret == 0);
2556 btrfs_clear_path_blocking(p, NULL, 0);
2564 if (!p->search_for_split)
2565 unlock_up(p, level, lowest_unlock,
2566 min_write_lock_level, &write_lock_level);
2573 * we don't really know what they plan on doing with the path
2574 * from here on, so for now just mark it as blocking
2576 if (!p->leave_spinning)
2577 btrfs_set_path_blocking(p);
2579 btrfs_release_path(p);
2584 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2585 * current state of the tree together with the operations recorded in the tree
2586 * modification log to search for the key in a previous version of this tree, as
2587 * denoted by the time_seq parameter.
2589 * Naturally, there is no support for insert, delete or cow operations.
2591 * The resulting path and return value will be set up as if we called
2592 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2594 int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2595 struct btrfs_path *p, u64 time_seq)
2597 struct extent_buffer *b;
2602 int lowest_unlock = 1;
2603 u8 lowest_level = 0;
2605 lowest_level = p->lowest_level;
2606 WARN_ON(p->nodes[0] != NULL);
2608 if (p->search_commit_root) {
2610 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2614 b = get_old_root(root, time_seq);
2615 extent_buffer_get(b);
2616 level = btrfs_header_level(b);
2617 btrfs_tree_read_lock(b);
2618 p->locks[level] = BTRFS_READ_LOCK;
2621 level = btrfs_header_level(b);
2622 p->nodes[level] = b;
2623 btrfs_clear_path_blocking(p, NULL, 0);
2626 * we have a lock on b and as long as we aren't changing
2627 * the tree, there is no way to for the items in b to change.
2628 * It is safe to drop the lock on our parent before we
2629 * go through the expensive btree search on b.
2631 btrfs_unlock_up_safe(p, level + 1);
2633 ret = bin_search(b, key, level, &slot);
2637 if (ret && slot > 0) {
2641 p->slots[level] = slot;
2642 unlock_up(p, level, lowest_unlock, 0, NULL);
2644 if (level == lowest_level) {
2650 err = read_block_for_search(NULL, root, p, &b, level,
2651 slot, key, time_seq);
2659 level = btrfs_header_level(b);
2660 err = btrfs_try_tree_read_lock(b);
2662 btrfs_set_path_blocking(p);
2663 btrfs_tree_read_lock(b);
2664 btrfs_clear_path_blocking(p, b,
2667 p->locks[level] = BTRFS_READ_LOCK;
2668 p->nodes[level] = b;
2669 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2670 if (b != p->nodes[level]) {
2671 btrfs_tree_unlock_rw(p->nodes[level],
2673 p->locks[level] = 0;
2674 p->nodes[level] = b;
2677 p->slots[level] = slot;
2678 unlock_up(p, level, lowest_unlock, 0, NULL);
2684 if (!p->leave_spinning)
2685 btrfs_set_path_blocking(p);
2687 btrfs_release_path(p);
2693 * adjust the pointers going up the tree, starting at level
2694 * making sure the right key of each node is points to 'key'.
2695 * This is used after shifting pointers to the left, so it stops
2696 * fixing up pointers when a given leaf/node is not in slot 0 of the
2700 static void fixup_low_keys(struct btrfs_trans_handle *trans,
2701 struct btrfs_root *root, struct btrfs_path *path,
2702 struct btrfs_disk_key *key, int level)
2705 struct extent_buffer *t;
2707 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2708 int tslot = path->slots[i];
2709 if (!path->nodes[i])
2712 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
2713 btrfs_set_node_key(t, key, tslot);
2714 btrfs_mark_buffer_dirty(path->nodes[i]);
2723 * This function isn't completely safe. It's the caller's responsibility
2724 * that the new key won't break the order
2726 void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2727 struct btrfs_root *root, struct btrfs_path *path,
2728 struct btrfs_key *new_key)
2730 struct btrfs_disk_key disk_key;
2731 struct extent_buffer *eb;
2734 eb = path->nodes[0];
2735 slot = path->slots[0];
2737 btrfs_item_key(eb, &disk_key, slot - 1);
2738 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
2740 if (slot < btrfs_header_nritems(eb) - 1) {
2741 btrfs_item_key(eb, &disk_key, slot + 1);
2742 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
2745 btrfs_cpu_key_to_disk(&disk_key, new_key);
2746 btrfs_set_item_key(eb, &disk_key, slot);
2747 btrfs_mark_buffer_dirty(eb);
2749 fixup_low_keys(trans, root, path, &disk_key, 1);
2753 * try to push data from one node into the next node left in the
2756 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2757 * error, and > 0 if there was no room in the left hand block.
2759 static int push_node_left(struct btrfs_trans_handle *trans,
2760 struct btrfs_root *root, struct extent_buffer *dst,
2761 struct extent_buffer *src, int empty)
2768 src_nritems = btrfs_header_nritems(src);
2769 dst_nritems = btrfs_header_nritems(dst);
2770 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2771 WARN_ON(btrfs_header_generation(src) != trans->transid);
2772 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2774 if (!empty && src_nritems <= 8)
2777 if (push_items <= 0)
2781 push_items = min(src_nritems, push_items);
2782 if (push_items < src_nritems) {
2783 /* leave at least 8 pointers in the node if
2784 * we aren't going to empty it
2786 if (src_nritems - push_items < 8) {
2787 if (push_items <= 8)
2793 push_items = min(src_nritems - 8, push_items);
2795 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2797 copy_extent_buffer(dst, src,
2798 btrfs_node_key_ptr_offset(dst_nritems),
2799 btrfs_node_key_ptr_offset(0),
2800 push_items * sizeof(struct btrfs_key_ptr));
2802 if (push_items < src_nritems) {
2803 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2804 src_nritems - push_items);
2805 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2806 btrfs_node_key_ptr_offset(push_items),
2807 (src_nritems - push_items) *
2808 sizeof(struct btrfs_key_ptr));
2810 btrfs_set_header_nritems(src, src_nritems - push_items);
2811 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2812 btrfs_mark_buffer_dirty(src);
2813 btrfs_mark_buffer_dirty(dst);
2819 * try to push data from one node into the next node right in the
2822 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2823 * error, and > 0 if there was no room in the right hand block.
2825 * this will only push up to 1/2 the contents of the left node over
2827 static int balance_node_right(struct btrfs_trans_handle *trans,
2828 struct btrfs_root *root,
2829 struct extent_buffer *dst,
2830 struct extent_buffer *src)
2838 WARN_ON(btrfs_header_generation(src) != trans->transid);
2839 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2841 src_nritems = btrfs_header_nritems(src);
2842 dst_nritems = btrfs_header_nritems(dst);
2843 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2844 if (push_items <= 0)
2847 if (src_nritems < 4)
2850 max_push = src_nritems / 2 + 1;
2851 /* don't try to empty the node */
2852 if (max_push >= src_nritems)
2855 if (max_push < push_items)
2856 push_items = max_push;
2858 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
2859 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2860 btrfs_node_key_ptr_offset(0),
2862 sizeof(struct btrfs_key_ptr));
2864 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2865 src_nritems - push_items, push_items);
2866 copy_extent_buffer(dst, src,
2867 btrfs_node_key_ptr_offset(0),
2868 btrfs_node_key_ptr_offset(src_nritems - push_items),
2869 push_items * sizeof(struct btrfs_key_ptr));
2871 btrfs_set_header_nritems(src, src_nritems - push_items);
2872 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2874 btrfs_mark_buffer_dirty(src);
2875 btrfs_mark_buffer_dirty(dst);
2881 * helper function to insert a new root level in the tree.
2882 * A new node is allocated, and a single item is inserted to
2883 * point to the existing root
2885 * returns zero on success or < 0 on failure.
2887 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2888 struct btrfs_root *root,
2889 struct btrfs_path *path, int level)
2892 struct extent_buffer *lower;
2893 struct extent_buffer *c;
2894 struct extent_buffer *old;
2895 struct btrfs_disk_key lower_key;
2897 BUG_ON(path->nodes[level]);
2898 BUG_ON(path->nodes[level-1] != root->node);
2900 lower = path->nodes[level-1];
2902 btrfs_item_key(lower, &lower_key, 0);
2904 btrfs_node_key(lower, &lower_key, 0);
2906 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2907 root->root_key.objectid, &lower_key,
2908 level, root->node->start, 0);
2912 root_add_used(root, root->nodesize);
2914 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
2915 btrfs_set_header_nritems(c, 1);
2916 btrfs_set_header_level(c, level);
2917 btrfs_set_header_bytenr(c, c->start);
2918 btrfs_set_header_generation(c, trans->transid);
2919 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
2920 btrfs_set_header_owner(c, root->root_key.objectid);
2922 write_extent_buffer(c, root->fs_info->fsid,
2923 (unsigned long)btrfs_header_fsid(c),
2926 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2927 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2930 btrfs_set_node_key(c, &lower_key, 0);
2931 btrfs_set_node_blockptr(c, 0, lower->start);
2932 lower_gen = btrfs_header_generation(lower);
2933 WARN_ON(lower_gen != trans->transid);
2935 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2937 btrfs_mark_buffer_dirty(c);
2940 tree_mod_log_set_root_pointer(root, c);
2941 rcu_assign_pointer(root->node, c);
2943 /* the super has an extra ref to root->node */
2944 free_extent_buffer(old);
2946 add_root_to_dirty_list(root);
2947 extent_buffer_get(c);
2948 path->nodes[level] = c;
2949 path->locks[level] = BTRFS_WRITE_LOCK;
2950 path->slots[level] = 0;
2955 * worker function to insert a single pointer in a node.
2956 * the node should have enough room for the pointer already
2958 * slot and level indicate where you want the key to go, and
2959 * blocknr is the block the key points to.
2961 static void insert_ptr(struct btrfs_trans_handle *trans,
2962 struct btrfs_root *root, struct btrfs_path *path,
2963 struct btrfs_disk_key *key, u64 bytenr,
2964 int slot, int level, int tree_mod_log)
2966 struct extent_buffer *lower;
2970 BUG_ON(!path->nodes[level]);
2971 btrfs_assert_tree_locked(path->nodes[level]);
2972 lower = path->nodes[level];
2973 nritems = btrfs_header_nritems(lower);
2974 BUG_ON(slot > nritems);
2975 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
2976 if (slot != nritems) {
2977 if (tree_mod_log && level)
2978 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2979 slot, nritems - slot);
2980 memmove_extent_buffer(lower,
2981 btrfs_node_key_ptr_offset(slot + 1),
2982 btrfs_node_key_ptr_offset(slot),
2983 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2985 if (tree_mod_log && level) {
2986 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2990 btrfs_set_node_key(lower, key, slot);
2991 btrfs_set_node_blockptr(lower, slot, bytenr);
2992 WARN_ON(trans->transid == 0);
2993 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2994 btrfs_set_header_nritems(lower, nritems + 1);
2995 btrfs_mark_buffer_dirty(lower);
2999 * split the node at the specified level in path in two.
3000 * The path is corrected to point to the appropriate node after the split
3002 * Before splitting this tries to make some room in the node by pushing
3003 * left and right, if either one works, it returns right away.
3005 * returns 0 on success and < 0 on failure
3007 static noinline int split_node(struct btrfs_trans_handle *trans,
3008 struct btrfs_root *root,
3009 struct btrfs_path *path, int level)
3011 struct extent_buffer *c;
3012 struct extent_buffer *split;
3013 struct btrfs_disk_key disk_key;
3018 c = path->nodes[level];
3019 WARN_ON(btrfs_header_generation(c) != trans->transid);
3020 if (c == root->node) {
3021 /* trying to split the root, lets make a new one */
3022 ret = insert_new_root(trans, root, path, level + 1);
3026 ret = push_nodes_for_insert(trans, root, path, level);
3027 c = path->nodes[level];
3028 if (!ret && btrfs_header_nritems(c) <
3029 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
3035 c_nritems = btrfs_header_nritems(c);
3036 mid = (c_nritems + 1) / 2;
3037 btrfs_node_key(c, &disk_key, mid);
3039 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
3040 root->root_key.objectid,
3041 &disk_key, level, c->start, 0);
3043 return PTR_ERR(split);
3045 root_add_used(root, root->nodesize);
3047 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
3048 btrfs_set_header_level(split, btrfs_header_level(c));
3049 btrfs_set_header_bytenr(split, split->start);
3050 btrfs_set_header_generation(split, trans->transid);
3051 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
3052 btrfs_set_header_owner(split, root->root_key.objectid);
3053 write_extent_buffer(split, root->fs_info->fsid,
3054 (unsigned long)btrfs_header_fsid(split),
3056 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3057 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3060 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
3061 copy_extent_buffer(split, c,
3062 btrfs_node_key_ptr_offset(0),
3063 btrfs_node_key_ptr_offset(mid),
3064 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3065 btrfs_set_header_nritems(split, c_nritems - mid);
3066 btrfs_set_header_nritems(c, mid);
3069 btrfs_mark_buffer_dirty(c);
3070 btrfs_mark_buffer_dirty(split);
3072 insert_ptr(trans, root, path, &disk_key, split->start,
3073 path->slots[level + 1] + 1, level + 1, 1);
3075 if (path->slots[level] >= mid) {
3076 path->slots[level] -= mid;
3077 btrfs_tree_unlock(c);
3078 free_extent_buffer(c);
3079 path->nodes[level] = split;
3080 path->slots[level + 1] += 1;
3082 btrfs_tree_unlock(split);
3083 free_extent_buffer(split);
3089 * how many bytes are required to store the items in a leaf. start
3090 * and nr indicate which items in the leaf to check. This totals up the
3091 * space used both by the item structs and the item data
3093 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3096 int nritems = btrfs_header_nritems(l);
3097 int end = min(nritems, start + nr) - 1;
3101 data_len = btrfs_item_end_nr(l, start);
3102 data_len = data_len - btrfs_item_offset_nr(l, end);
3103 data_len += sizeof(struct btrfs_item) * nr;
3104 WARN_ON(data_len < 0);
3109 * The space between the end of the leaf items and
3110 * the start of the leaf data. IOW, how much room
3111 * the leaf has left for both items and data
3113 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
3114 struct extent_buffer *leaf)
3116 int nritems = btrfs_header_nritems(leaf);
3118 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3120 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3121 "used %d nritems %d\n",
3122 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
3123 leaf_space_used(leaf, 0, nritems), nritems);
3129 * min slot controls the lowest index we're willing to push to the
3130 * right. We'll push up to and including min_slot, but no lower
3132 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3133 struct btrfs_root *root,
3134 struct btrfs_path *path,
3135 int data_size, int empty,
3136 struct extent_buffer *right,
3137 int free_space, u32 left_nritems,
3140 struct extent_buffer *left = path->nodes[0];
3141 struct extent_buffer *upper = path->nodes[1];
3142 struct btrfs_map_token token;
3143 struct btrfs_disk_key disk_key;
3148 struct btrfs_item *item;
3154 btrfs_init_map_token(&token);
3159 nr = max_t(u32, 1, min_slot);
3161 if (path->slots[0] >= left_nritems)
3162 push_space += data_size;
3164 slot = path->slots[1];
3165 i = left_nritems - 1;
3167 item = btrfs_item_nr(left, i);
3169 if (!empty && push_items > 0) {
3170 if (path->slots[0] > i)
3172 if (path->slots[0] == i) {
3173 int space = btrfs_leaf_free_space(root, left);
3174 if (space + push_space * 2 > free_space)
3179 if (path->slots[0] == i)
3180 push_space += data_size;
3182 this_item_size = btrfs_item_size(left, item);
3183 if (this_item_size + sizeof(*item) + push_space > free_space)
3187 push_space += this_item_size + sizeof(*item);
3193 if (push_items == 0)
3196 if (!empty && push_items == left_nritems)
3199 /* push left to right */
3200 right_nritems = btrfs_header_nritems(right);
3202 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3203 push_space -= leaf_data_end(root, left);
3205 /* make room in the right data area */
3206 data_end = leaf_data_end(root, right);
3207 memmove_extent_buffer(right,
3208 btrfs_leaf_data(right) + data_end - push_space,
3209 btrfs_leaf_data(right) + data_end,
3210 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3212 /* copy from the left data area */
3213 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
3214 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3215 btrfs_leaf_data(left) + leaf_data_end(root, left),
3218 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3219 btrfs_item_nr_offset(0),
3220 right_nritems * sizeof(struct btrfs_item));
3222 /* copy the items from left to right */
3223 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3224 btrfs_item_nr_offset(left_nritems - push_items),
3225 push_items * sizeof(struct btrfs_item));
3227 /* update the item pointers */
3228 right_nritems += push_items;
3229 btrfs_set_header_nritems(right, right_nritems);
3230 push_space = BTRFS_LEAF_DATA_SIZE(root);
3231 for (i = 0; i < right_nritems; i++) {
3232 item = btrfs_item_nr(right, i);
3233 push_space -= btrfs_token_item_size(right, item, &token);
3234 btrfs_set_token_item_offset(right, item, push_space, &token);
3237 left_nritems -= push_items;
3238 btrfs_set_header_nritems(left, left_nritems);
3241 btrfs_mark_buffer_dirty(left);
3243 clean_tree_block(trans, root, left);
3245 btrfs_mark_buffer_dirty(right);
3247 btrfs_item_key(right, &disk_key, 0);
3248 btrfs_set_node_key(upper, &disk_key, slot + 1);
3249 btrfs_mark_buffer_dirty(upper);
3251 /* then fixup the leaf pointer in the path */
3252 if (path->slots[0] >= left_nritems) {
3253 path->slots[0] -= left_nritems;
3254 if (btrfs_header_nritems(path->nodes[0]) == 0)
3255 clean_tree_block(trans, root, path->nodes[0]);
3256 btrfs_tree_unlock(path->nodes[0]);
3257 free_extent_buffer(path->nodes[0]);
3258 path->nodes[0] = right;
3259 path->slots[1] += 1;
3261 btrfs_tree_unlock(right);
3262 free_extent_buffer(right);
3267 btrfs_tree_unlock(right);
3268 free_extent_buffer(right);
3273 * push some data in the path leaf to the right, trying to free up at
3274 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3276 * returns 1 if the push failed because the other node didn't have enough
3277 * room, 0 if everything worked out and < 0 if there were major errors.
3279 * this will push starting from min_slot to the end of the leaf. It won't
3280 * push any slot lower than min_slot
3282 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3283 *root, struct btrfs_path *path,
3284 int min_data_size, int data_size,
3285 int empty, u32 min_slot)
3287 struct extent_buffer *left = path->nodes[0];
3288 struct extent_buffer *right;
3289 struct extent_buffer *upper;
3295 if (!path->nodes[1])
3298 slot = path->slots[1];
3299 upper = path->nodes[1];
3300 if (slot >= btrfs_header_nritems(upper) - 1)
3303 btrfs_assert_tree_locked(path->nodes[1]);
3305 right = read_node_slot(root, upper, slot + 1);
3309 btrfs_tree_lock(right);
3310 btrfs_set_lock_blocking(right);
3312 free_space = btrfs_leaf_free_space(root, right);
3313 if (free_space < data_size)
3316 /* cow and double check */
3317 ret = btrfs_cow_block(trans, root, right, upper,
3322 free_space = btrfs_leaf_free_space(root, right);
3323 if (free_space < data_size)
3326 left_nritems = btrfs_header_nritems(left);
3327 if (left_nritems == 0)
3330 return __push_leaf_right(trans, root, path, min_data_size, empty,
3331 right, free_space, left_nritems, min_slot);
3333 btrfs_tree_unlock(right);
3334 free_extent_buffer(right);
3339 * push some data in the path leaf to the left, trying to free up at
3340 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3342 * max_slot can put a limit on how far into the leaf we'll push items. The
3343 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3346 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3347 struct btrfs_root *root,
3348 struct btrfs_path *path, int data_size,
3349 int empty, struct extent_buffer *left,
3350 int free_space, u32 right_nritems,
3353 struct btrfs_disk_key disk_key;
3354 struct extent_buffer *right = path->nodes[0];
3358 struct btrfs_item *item;
3359 u32 old_left_nritems;
3363 u32 old_left_item_size;
3364 struct btrfs_map_token token;
3366 btrfs_init_map_token(&token);
3369 nr = min(right_nritems, max_slot);
3371 nr = min(right_nritems - 1, max_slot);
3373 for (i = 0; i < nr; i++) {
3374 item = btrfs_item_nr(right, i);
3376 if (!empty && push_items > 0) {
3377 if (path->slots[0] < i)
3379 if (path->slots[0] == i) {
3380 int space = btrfs_leaf_free_space(root, right);
3381 if (space + push_space * 2 > free_space)
3386 if (path->slots[0] == i)
3387 push_space += data_size;
3389 this_item_size = btrfs_item_size(right, item);
3390 if (this_item_size + sizeof(*item) + push_space > free_space)
3394 push_space += this_item_size + sizeof(*item);
3397 if (push_items == 0) {
3401 if (!empty && push_items == btrfs_header_nritems(right))
3404 /* push data from right to left */
3405 copy_extent_buffer(left, right,
3406 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3407 btrfs_item_nr_offset(0),
3408 push_items * sizeof(struct btrfs_item));
3410 push_space = BTRFS_LEAF_DATA_SIZE(root) -
3411 btrfs_item_offset_nr(right, push_items - 1);
3413 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
3414 leaf_data_end(root, left) - push_space,
3415 btrfs_leaf_data(right) +
3416 btrfs_item_offset_nr(right, push_items - 1),
3418 old_left_nritems = btrfs_header_nritems(left);
3419 BUG_ON(old_left_nritems <= 0);
3421 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3422 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3425 item = btrfs_item_nr(left, i);
3427 ioff = btrfs_token_item_offset(left, item, &token);
3428 btrfs_set_token_item_offset(left, item,
3429 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3432 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3434 /* fixup right node */
3435 if (push_items > right_nritems) {
3436 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3441 if (push_items < right_nritems) {
3442 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3443 leaf_data_end(root, right);
3444 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3445 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3446 btrfs_leaf_data(right) +
3447 leaf_data_end(root, right), push_space);
3449 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3450 btrfs_item_nr_offset(push_items),
3451 (btrfs_header_nritems(right) - push_items) *
3452 sizeof(struct btrfs_item));
3454 right_nritems -= push_items;
3455 btrfs_set_header_nritems(right, right_nritems);
3456 push_space = BTRFS_LEAF_DATA_SIZE(root);
3457 for (i = 0; i < right_nritems; i++) {
3458 item = btrfs_item_nr(right, i);
3460 push_space = push_space - btrfs_token_item_size(right,
3462 btrfs_set_token_item_offset(right, item, push_space, &token);
3465 btrfs_mark_buffer_dirty(left);
3467 btrfs_mark_buffer_dirty(right);
3469 clean_tree_block(trans, root, right);
3471 btrfs_item_key(right, &disk_key, 0);
3472 fixup_low_keys(trans, root, path, &disk_key, 1);
3474 /* then fixup the leaf pointer in the path */
3475 if (path->slots[0] < push_items) {
3476 path->slots[0] += old_left_nritems;
3477 btrfs_tree_unlock(path->nodes[0]);
3478 free_extent_buffer(path->nodes[0]);
3479 path->nodes[0] = left;
3480 path->slots[1] -= 1;
3482 btrfs_tree_unlock(left);
3483 free_extent_buffer(left);
3484 path->slots[0] -= push_items;
3486 BUG_ON(path->slots[0] < 0);
3489 btrfs_tree_unlock(left);
3490 free_extent_buffer(left);
3495 * push some data in the path leaf to the left, trying to free up at
3496 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3498 * max_slot can put a limit on how far into the leaf we'll push items. The
3499 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3502 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3503 *root, struct btrfs_path *path, int min_data_size,
3504 int data_size, int empty, u32 max_slot)
3506 struct extent_buffer *right = path->nodes[0];
3507 struct extent_buffer *left;
3513 slot = path->slots[1];
3516 if (!path->nodes[1])
3519 right_nritems = btrfs_header_nritems(right);
3520 if (right_nritems == 0)
3523 btrfs_assert_tree_locked(path->nodes[1]);
3525 left = read_node_slot(root, path->nodes[1], slot - 1);
3529 btrfs_tree_lock(left);
3530 btrfs_set_lock_blocking(left);
3532 free_space = btrfs_leaf_free_space(root, left);
3533 if (free_space < data_size) {
3538 /* cow and double check */
3539 ret = btrfs_cow_block(trans, root, left,
3540 path->nodes[1], slot - 1, &left);
3542 /* we hit -ENOSPC, but it isn't fatal here */
3548 free_space = btrfs_leaf_free_space(root, left);
3549 if (free_space < data_size) {
3554 return __push_leaf_left(trans, root, path, min_data_size,
3555 empty, left, free_space, right_nritems,
3558 btrfs_tree_unlock(left);
3559 free_extent_buffer(left);
3564 * split the path's leaf in two, making sure there is at least data_size
3565 * available for the resulting leaf level of the path.
3567 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3568 struct btrfs_root *root,
3569 struct btrfs_path *path,
3570 struct extent_buffer *l,
3571 struct extent_buffer *right,
3572 int slot, int mid, int nritems)
3577 struct btrfs_disk_key disk_key;
3578 struct btrfs_map_token token;
3580 btrfs_init_map_token(&token);
3582 nritems = nritems - mid;
3583 btrfs_set_header_nritems(right, nritems);
3584 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3586 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3587 btrfs_item_nr_offset(mid),
3588 nritems * sizeof(struct btrfs_item));
3590 copy_extent_buffer(right, l,
3591 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3592 data_copy_size, btrfs_leaf_data(l) +
3593 leaf_data_end(root, l), data_copy_size);
3595 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3596 btrfs_item_end_nr(l, mid);
3598 for (i = 0; i < nritems; i++) {
3599 struct btrfs_item *item = btrfs_item_nr(right, i);
3602 ioff = btrfs_token_item_offset(right, item, &token);
3603 btrfs_set_token_item_offset(right, item,
3604 ioff + rt_data_off, &token);
3607 btrfs_set_header_nritems(l, mid);
3608 btrfs_item_key(right, &disk_key, 0);
3609 insert_ptr(trans, root, path, &disk_key, right->start,
3610 path->slots[1] + 1, 1, 0);
3612 btrfs_mark_buffer_dirty(right);
3613 btrfs_mark_buffer_dirty(l);
3614 BUG_ON(path->slots[0] != slot);
3617 btrfs_tree_unlock(path->nodes[0]);
3618 free_extent_buffer(path->nodes[0]);
3619 path->nodes[0] = right;
3620 path->slots[0] -= mid;
3621 path->slots[1] += 1;
3623 btrfs_tree_unlock(right);
3624 free_extent_buffer(right);
3627 BUG_ON(path->slots[0] < 0);
3631 * double splits happen when we need to insert a big item in the middle
3632 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3633 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3636 * We avoid this by trying to push the items on either side of our target
3637 * into the adjacent leaves. If all goes well we can avoid the double split
3640 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3641 struct btrfs_root *root,
3642 struct btrfs_path *path,
3650 slot = path->slots[0];
3653 * try to push all the items after our slot into the
3656 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3663 nritems = btrfs_header_nritems(path->nodes[0]);
3665 * our goal is to get our slot at the start or end of a leaf. If
3666 * we've done so we're done
3668 if (path->slots[0] == 0 || path->slots[0] == nritems)
3671 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3674 /* try to push all the items before our slot into the next leaf */
3675 slot = path->slots[0];
3676 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3689 * split the path's leaf in two, making sure there is at least data_size
3690 * available for the resulting leaf level of the path.
3692 * returns 0 if all went well and < 0 on failure.
3694 static noinline int split_leaf(struct btrfs_trans_handle *trans,
3695 struct btrfs_root *root,
3696 struct btrfs_key *ins_key,
3697 struct btrfs_path *path, int data_size,
3700 struct btrfs_disk_key disk_key;
3701 struct extent_buffer *l;
3705 struct extent_buffer *right;
3709 int num_doubles = 0;
3710 int tried_avoid_double = 0;
3713 slot = path->slots[0];
3714 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3715 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3718 /* first try to make some room by pushing left and right */
3720 wret = push_leaf_right(trans, root, path, data_size,
3725 wret = push_leaf_left(trans, root, path, data_size,
3726 data_size, 0, (u32)-1);
3732 /* did the pushes work? */
3733 if (btrfs_leaf_free_space(root, l) >= data_size)
3737 if (!path->nodes[1]) {
3738 ret = insert_new_root(trans, root, path, 1);
3745 slot = path->slots[0];
3746 nritems = btrfs_header_nritems(l);
3747 mid = (nritems + 1) / 2;
3751 leaf_space_used(l, mid, nritems - mid) + data_size >
3752 BTRFS_LEAF_DATA_SIZE(root)) {
3753 if (slot >= nritems) {
3757 if (mid != nritems &&
3758 leaf_space_used(l, mid, nritems - mid) +
3759 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
3760 if (data_size && !tried_avoid_double)
3761 goto push_for_double;
3767 if (leaf_space_used(l, 0, mid) + data_size >
3768 BTRFS_LEAF_DATA_SIZE(root)) {
3769 if (!extend && data_size && slot == 0) {
3771 } else if ((extend || !data_size) && slot == 0) {
3775 if (mid != nritems &&
3776 leaf_space_used(l, mid, nritems - mid) +
3777 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
3778 if (data_size && !tried_avoid_double)
3779 goto push_for_double;
3787 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3789 btrfs_item_key(l, &disk_key, mid);
3791 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
3792 root->root_key.objectid,
3793 &disk_key, 0, l->start, 0);
3795 return PTR_ERR(right);
3797 root_add_used(root, root->leafsize);
3799 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3800 btrfs_set_header_bytenr(right, right->start);
3801 btrfs_set_header_generation(right, trans->transid);
3802 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
3803 btrfs_set_header_owner(right, root->root_key.objectid);
3804 btrfs_set_header_level(right, 0);
3805 write_extent_buffer(right, root->fs_info->fsid,
3806 (unsigned long)btrfs_header_fsid(right),
3809 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3810 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3815 btrfs_set_header_nritems(right, 0);
3816 insert_ptr(trans, root, path, &disk_key, right->start,
3817 path->slots[1] + 1, 1, 0);
3818 btrfs_tree_unlock(path->nodes[0]);
3819 free_extent_buffer(path->nodes[0]);
3820 path->nodes[0] = right;
3822 path->slots[1] += 1;
3824 btrfs_set_header_nritems(right, 0);
3825 insert_ptr(trans, root, path, &disk_key, right->start,
3826 path->slots[1], 1, 0);
3827 btrfs_tree_unlock(path->nodes[0]);
3828 free_extent_buffer(path->nodes[0]);
3829 path->nodes[0] = right;
3831 if (path->slots[1] == 0)
3832 fixup_low_keys(trans, root, path,
3835 btrfs_mark_buffer_dirty(right);
3839 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3842 BUG_ON(num_doubles != 0);
3850 push_for_double_split(trans, root, path, data_size);
3851 tried_avoid_double = 1;
3852 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3857 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3858 struct btrfs_root *root,
3859 struct btrfs_path *path, int ins_len)
3861 struct btrfs_key key;
3862 struct extent_buffer *leaf;
3863 struct btrfs_file_extent_item *fi;
3868 leaf = path->nodes[0];
3869 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3871 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3872 key.type != BTRFS_EXTENT_CSUM_KEY);
3874 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3877 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3878 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3879 fi = btrfs_item_ptr(leaf, path->slots[0],
3880 struct btrfs_file_extent_item);
3881 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3883 btrfs_release_path(path);
3885 path->keep_locks = 1;
3886 path->search_for_split = 1;
3887 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3888 path->search_for_split = 0;
3893 leaf = path->nodes[0];
3894 /* if our item isn't there or got smaller, return now */
3895 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3898 /* the leaf has changed, it now has room. return now */
3899 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3902 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3903 fi = btrfs_item_ptr(leaf, path->slots[0],
3904 struct btrfs_file_extent_item);
3905 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3909 btrfs_set_path_blocking(path);
3910 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3914 path->keep_locks = 0;
3915 btrfs_unlock_up_safe(path, 1);
3918 path->keep_locks = 0;
3922 static noinline int split_item(struct btrfs_trans_handle *trans,
3923 struct btrfs_root *root,
3924 struct btrfs_path *path,
3925 struct btrfs_key *new_key,
3926 unsigned long split_offset)
3928 struct extent_buffer *leaf;
3929 struct btrfs_item *item;
3930 struct btrfs_item *new_item;
3936 struct btrfs_disk_key disk_key;
3938 leaf = path->nodes[0];
3939 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3941 btrfs_set_path_blocking(path);
3943 item = btrfs_item_nr(leaf, path->slots[0]);
3944 orig_offset = btrfs_item_offset(leaf, item);
3945 item_size = btrfs_item_size(leaf, item);
3947 buf = kmalloc(item_size, GFP_NOFS);
3951 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3952 path->slots[0]), item_size);
3954 slot = path->slots[0] + 1;
3955 nritems = btrfs_header_nritems(leaf);
3956 if (slot != nritems) {
3957 /* shift the items */
3958 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3959 btrfs_item_nr_offset(slot),
3960 (nritems - slot) * sizeof(struct btrfs_item));
3963 btrfs_cpu_key_to_disk(&disk_key, new_key);
3964 btrfs_set_item_key(leaf, &disk_key, slot);
3966 new_item = btrfs_item_nr(leaf, slot);
3968 btrfs_set_item_offset(leaf, new_item, orig_offset);
3969 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3971 btrfs_set_item_offset(leaf, item,
3972 orig_offset + item_size - split_offset);
3973 btrfs_set_item_size(leaf, item, split_offset);
3975 btrfs_set_header_nritems(leaf, nritems + 1);
3977 /* write the data for the start of the original item */
3978 write_extent_buffer(leaf, buf,
3979 btrfs_item_ptr_offset(leaf, path->slots[0]),
3982 /* write the data for the new item */
3983 write_extent_buffer(leaf, buf + split_offset,
3984 btrfs_item_ptr_offset(leaf, slot),
3985 item_size - split_offset);
3986 btrfs_mark_buffer_dirty(leaf);
3988 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3994 * This function splits a single item into two items,
3995 * giving 'new_key' to the new item and splitting the
3996 * old one at split_offset (from the start of the item).
3998 * The path may be released by this operation. After
3999 * the split, the path is pointing to the old item. The
4000 * new item is going to be in the same node as the old one.
4002 * Note, the item being split must be smaller enough to live alone on
4003 * a tree block with room for one extra struct btrfs_item
4005 * This allows us to split the item in place, keeping a lock on the
4006 * leaf the entire time.
4008 int btrfs_split_item(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root,
4010 struct btrfs_path *path,
4011 struct btrfs_key *new_key,
4012 unsigned long split_offset)
4015 ret = setup_leaf_for_split(trans, root, path,
4016 sizeof(struct btrfs_item));
4020 ret = split_item(trans, root, path, new_key, split_offset);
4025 * This function duplicate a item, giving 'new_key' to the new item.
4026 * It guarantees both items live in the same tree leaf and the new item
4027 * is contiguous with the original item.
4029 * This allows us to split file extent in place, keeping a lock on the
4030 * leaf the entire time.
4032 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4033 struct btrfs_root *root,
4034 struct btrfs_path *path,
4035 struct btrfs_key *new_key)
4037 struct extent_buffer *leaf;
4041 leaf = path->nodes[0];
4042 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4043 ret = setup_leaf_for_split(trans, root, path,
4044 item_size + sizeof(struct btrfs_item));
4049 setup_items_for_insert(trans, root, path, new_key, &item_size,
4050 item_size, item_size +
4051 sizeof(struct btrfs_item), 1);
4052 leaf = path->nodes[0];
4053 memcpy_extent_buffer(leaf,
4054 btrfs_item_ptr_offset(leaf, path->slots[0]),
4055 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4061 * make the item pointed to by the path smaller. new_size indicates
4062 * how small to make it, and from_end tells us if we just chop bytes
4063 * off the end of the item or if we shift the item to chop bytes off
4066 void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4067 struct btrfs_root *root,
4068 struct btrfs_path *path,
4069 u32 new_size, int from_end)
4072 struct extent_buffer *leaf;
4073 struct btrfs_item *item;
4075 unsigned int data_end;
4076 unsigned int old_data_start;
4077 unsigned int old_size;
4078 unsigned int size_diff;
4080 struct btrfs_map_token token;
4082 btrfs_init_map_token(&token);
4084 leaf = path->nodes[0];
4085 slot = path->slots[0];
4087 old_size = btrfs_item_size_nr(leaf, slot);
4088 if (old_size == new_size)
4091 nritems = btrfs_header_nritems(leaf);
4092 data_end = leaf_data_end(root, leaf);
4094 old_data_start = btrfs_item_offset_nr(leaf, slot);
4096 size_diff = old_size - new_size;
4099 BUG_ON(slot >= nritems);
4102 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4104 /* first correct the data pointers */
4105 for (i = slot; i < nritems; i++) {
4107 item = btrfs_item_nr(leaf, i);
4109 ioff = btrfs_token_item_offset(leaf, item, &token);
4110 btrfs_set_token_item_offset(leaf, item,
4111 ioff + size_diff, &token);
4114 /* shift the data */
4116 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4117 data_end + size_diff, btrfs_leaf_data(leaf) +
4118 data_end, old_data_start + new_size - data_end);
4120 struct btrfs_disk_key disk_key;
4123 btrfs_item_key(leaf, &disk_key, slot);
4125 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4127 struct btrfs_file_extent_item *fi;
4129 fi = btrfs_item_ptr(leaf, slot,
4130 struct btrfs_file_extent_item);
4131 fi = (struct btrfs_file_extent_item *)(
4132 (unsigned long)fi - size_diff);
4134 if (btrfs_file_extent_type(leaf, fi) ==
4135 BTRFS_FILE_EXTENT_INLINE) {
4136 ptr = btrfs_item_ptr_offset(leaf, slot);
4137 memmove_extent_buffer(leaf, ptr,
4139 offsetof(struct btrfs_file_extent_item,
4144 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4145 data_end + size_diff, btrfs_leaf_data(leaf) +
4146 data_end, old_data_start - data_end);
4148 offset = btrfs_disk_key_offset(&disk_key);
4149 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4150 btrfs_set_item_key(leaf, &disk_key, slot);
4152 fixup_low_keys(trans, root, path, &disk_key, 1);
4155 item = btrfs_item_nr(leaf, slot);
4156 btrfs_set_item_size(leaf, item, new_size);
4157 btrfs_mark_buffer_dirty(leaf);
4159 if (btrfs_leaf_free_space(root, leaf) < 0) {
4160 btrfs_print_leaf(root, leaf);
4166 * make the item pointed to by the path bigger, data_size is the new size.
4168 void btrfs_extend_item(struct btrfs_trans_handle *trans,
4169 struct btrfs_root *root, struct btrfs_path *path,
4173 struct extent_buffer *leaf;
4174 struct btrfs_item *item;
4176 unsigned int data_end;
4177 unsigned int old_data;
4178 unsigned int old_size;
4180 struct btrfs_map_token token;
4182 btrfs_init_map_token(&token);
4184 leaf = path->nodes[0];
4186 nritems = btrfs_header_nritems(leaf);
4187 data_end = leaf_data_end(root, leaf);
4189 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4190 btrfs_print_leaf(root, leaf);
4193 slot = path->slots[0];
4194 old_data = btrfs_item_end_nr(leaf, slot);
4197 if (slot >= nritems) {
4198 btrfs_print_leaf(root, leaf);
4199 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4205 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4207 /* first correct the data pointers */
4208 for (i = slot; i < nritems; i++) {
4210 item = btrfs_item_nr(leaf, i);
4212 ioff = btrfs_token_item_offset(leaf, item, &token);
4213 btrfs_set_token_item_offset(leaf, item,
4214 ioff - data_size, &token);
4217 /* shift the data */
4218 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4219 data_end - data_size, btrfs_leaf_data(leaf) +
4220 data_end, old_data - data_end);
4222 data_end = old_data;
4223 old_size = btrfs_item_size_nr(leaf, slot);
4224 item = btrfs_item_nr(leaf, slot);
4225 btrfs_set_item_size(leaf, item, old_size + data_size);
4226 btrfs_mark_buffer_dirty(leaf);
4228 if (btrfs_leaf_free_space(root, leaf) < 0) {
4229 btrfs_print_leaf(root, leaf);
4235 * Given a key and some data, insert items into the tree.
4236 * This does all the path init required, making room in the tree if needed.
4237 * Returns the number of keys that were inserted.
4239 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4240 struct btrfs_root *root,
4241 struct btrfs_path *path,
4242 struct btrfs_key *cpu_key, u32 *data_size,
4245 struct extent_buffer *leaf;
4246 struct btrfs_item *item;
4253 unsigned int data_end;
4254 struct btrfs_disk_key disk_key;
4255 struct btrfs_key found_key;
4256 struct btrfs_map_token token;
4258 btrfs_init_map_token(&token);
4260 for (i = 0; i < nr; i++) {
4261 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4262 BTRFS_LEAF_DATA_SIZE(root)) {
4266 total_data += data_size[i];
4267 total_size += data_size[i] + sizeof(struct btrfs_item);
4271 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4277 leaf = path->nodes[0];
4279 nritems = btrfs_header_nritems(leaf);
4280 data_end = leaf_data_end(root, leaf);
4282 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4283 for (i = nr; i >= 0; i--) {
4284 total_data -= data_size[i];
4285 total_size -= data_size[i] + sizeof(struct btrfs_item);
4286 if (total_size < btrfs_leaf_free_space(root, leaf))
4292 slot = path->slots[0];
4295 if (slot != nritems) {
4296 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4298 item = btrfs_item_nr(leaf, slot);
4299 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4301 /* figure out how many keys we can insert in here */
4302 total_data = data_size[0];
4303 for (i = 1; i < nr; i++) {
4304 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
4306 total_data += data_size[i];
4310 if (old_data < data_end) {
4311 btrfs_print_leaf(root, leaf);
4312 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
4313 slot, old_data, data_end);
4317 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4319 /* first correct the data pointers */
4320 for (i = slot; i < nritems; i++) {
4323 item = btrfs_item_nr(leaf, i);
4324 ioff = btrfs_token_item_offset(leaf, item, &token);
4325 btrfs_set_token_item_offset(leaf, item,
4326 ioff - total_data, &token);
4328 /* shift the items */
4329 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4330 btrfs_item_nr_offset(slot),
4331 (nritems - slot) * sizeof(struct btrfs_item));
4333 /* shift the data */
4334 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4335 data_end - total_data, btrfs_leaf_data(leaf) +
4336 data_end, old_data - data_end);
4337 data_end = old_data;
4340 * this sucks but it has to be done, if we are inserting at
4341 * the end of the leaf only insert 1 of the items, since we
4342 * have no way of knowing whats on the next leaf and we'd have
4343 * to drop our current locks to figure it out
4348 /* setup the item for the new data */
4349 for (i = 0; i < nr; i++) {
4350 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4351 btrfs_set_item_key(leaf, &disk_key, slot + i);
4352 item = btrfs_item_nr(leaf, slot + i);
4353 btrfs_set_token_item_offset(leaf, item,
4354 data_end - data_size[i], &token);
4355 data_end -= data_size[i];
4356 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4358 btrfs_set_header_nritems(leaf, nritems + nr);
4359 btrfs_mark_buffer_dirty(leaf);
4363 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4364 fixup_low_keys(trans, root, path, &disk_key, 1);
4367 if (btrfs_leaf_free_space(root, leaf) < 0) {
4368 btrfs_print_leaf(root, leaf);
4378 * this is a helper for btrfs_insert_empty_items, the main goal here is
4379 * to save stack depth by doing the bulk of the work in a function
4380 * that doesn't call btrfs_search_slot
4382 void setup_items_for_insert(struct btrfs_trans_handle *trans,
4383 struct btrfs_root *root, struct btrfs_path *path,
4384 struct btrfs_key *cpu_key, u32 *data_size,
4385 u32 total_data, u32 total_size, int nr)
4387 struct btrfs_item *item;
4390 unsigned int data_end;
4391 struct btrfs_disk_key disk_key;
4392 struct extent_buffer *leaf;
4394 struct btrfs_map_token token;
4396 btrfs_init_map_token(&token);
4398 leaf = path->nodes[0];
4399 slot = path->slots[0];
4401 nritems = btrfs_header_nritems(leaf);
4402 data_end = leaf_data_end(root, leaf);
4404 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4405 btrfs_print_leaf(root, leaf);
4406 printk(KERN_CRIT "not enough freespace need %u have %d\n",
4407 total_size, btrfs_leaf_free_space(root, leaf));
4411 if (slot != nritems) {
4412 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4414 if (old_data < data_end) {
4415 btrfs_print_leaf(root, leaf);
4416 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
4417 slot, old_data, data_end);
4421 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4423 /* first correct the data pointers */
4424 for (i = slot; i < nritems; i++) {
4427 item = btrfs_item_nr(leaf, i);
4428 ioff = btrfs_token_item_offset(leaf, item, &token);
4429 btrfs_set_token_item_offset(leaf, item,
4430 ioff - total_data, &token);
4432 /* shift the items */
4433 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4434 btrfs_item_nr_offset(slot),
4435 (nritems - slot) * sizeof(struct btrfs_item));
4437 /* shift the data */
4438 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4439 data_end - total_data, btrfs_leaf_data(leaf) +
4440 data_end, old_data - data_end);
4441 data_end = old_data;
4444 /* setup the item for the new data */
4445 for (i = 0; i < nr; i++) {
4446 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4447 btrfs_set_item_key(leaf, &disk_key, slot + i);
4448 item = btrfs_item_nr(leaf, slot + i);
4449 btrfs_set_token_item_offset(leaf, item,
4450 data_end - data_size[i], &token);
4451 data_end -= data_size[i];
4452 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4455 btrfs_set_header_nritems(leaf, nritems + nr);
4458 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4459 fixup_low_keys(trans, root, path, &disk_key, 1);
4461 btrfs_unlock_up_safe(path, 1);
4462 btrfs_mark_buffer_dirty(leaf);
4464 if (btrfs_leaf_free_space(root, leaf) < 0) {
4465 btrfs_print_leaf(root, leaf);
4471 * Given a key and some data, insert items into the tree.
4472 * This does all the path init required, making room in the tree if needed.
4474 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4475 struct btrfs_root *root,
4476 struct btrfs_path *path,
4477 struct btrfs_key *cpu_key, u32 *data_size,
4486 for (i = 0; i < nr; i++)
4487 total_data += data_size[i];
4489 total_size = total_data + (nr * sizeof(struct btrfs_item));
4490 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4496 slot = path->slots[0];
4499 setup_items_for_insert(trans, root, path, cpu_key, data_size,
4500 total_data, total_size, nr);
4505 * Given a key and some data, insert an item into the tree.
4506 * This does all the path init required, making room in the tree if needed.
4508 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4509 *root, struct btrfs_key *cpu_key, void *data, u32
4513 struct btrfs_path *path;
4514 struct extent_buffer *leaf;
4517 path = btrfs_alloc_path();
4520 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4522 leaf = path->nodes[0];
4523 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4524 write_extent_buffer(leaf, data, ptr, data_size);
4525 btrfs_mark_buffer_dirty(leaf);
4527 btrfs_free_path(path);
4532 * delete the pointer from a given node.
4534 * the tree should have been previously balanced so the deletion does not
4537 static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4538 struct btrfs_path *path, int level, int slot,
4541 struct extent_buffer *parent = path->nodes[level];
4545 nritems = btrfs_header_nritems(parent);
4546 if (slot != nritems - 1) {
4547 if (tree_mod_log && level)
4548 tree_mod_log_eb_move(root->fs_info, parent, slot,
4549 slot + 1, nritems - slot - 1);
4550 memmove_extent_buffer(parent,
4551 btrfs_node_key_ptr_offset(slot),
4552 btrfs_node_key_ptr_offset(slot + 1),
4553 sizeof(struct btrfs_key_ptr) *
4554 (nritems - slot - 1));
4555 } else if (tree_mod_log && level) {
4556 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4557 MOD_LOG_KEY_REMOVE);
4562 btrfs_set_header_nritems(parent, nritems);
4563 if (nritems == 0 && parent == root->node) {
4564 BUG_ON(btrfs_header_level(root->node) != 1);
4565 /* just turn the root into a leaf and break */
4566 btrfs_set_header_level(root->node, 0);
4567 } else if (slot == 0) {
4568 struct btrfs_disk_key disk_key;
4570 btrfs_node_key(parent, &disk_key, 0);
4571 fixup_low_keys(trans, root, path, &disk_key, level + 1);
4573 btrfs_mark_buffer_dirty(parent);
4577 * a helper function to delete the leaf pointed to by path->slots[1] and
4580 * This deletes the pointer in path->nodes[1] and frees the leaf
4581 * block extent. zero is returned if it all worked out, < 0 otherwise.
4583 * The path must have already been setup for deleting the leaf, including
4584 * all the proper balancing. path->nodes[1] must be locked.
4586 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4587 struct btrfs_root *root,
4588 struct btrfs_path *path,
4589 struct extent_buffer *leaf)
4591 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4592 del_ptr(trans, root, path, 1, path->slots[1], 1);
4595 * btrfs_free_extent is expensive, we want to make sure we
4596 * aren't holding any locks when we call it
4598 btrfs_unlock_up_safe(path, 0);
4600 root_sub_used(root, leaf->len);
4602 extent_buffer_get(leaf);
4603 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4604 free_extent_buffer_stale(leaf);
4607 * delete the item at the leaf level in path. If that empties
4608 * the leaf, remove it from the tree
4610 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4611 struct btrfs_path *path, int slot, int nr)
4613 struct extent_buffer *leaf;
4614 struct btrfs_item *item;
4621 struct btrfs_map_token token;
4623 btrfs_init_map_token(&token);
4625 leaf = path->nodes[0];
4626 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4628 for (i = 0; i < nr; i++)
4629 dsize += btrfs_item_size_nr(leaf, slot + i);
4631 nritems = btrfs_header_nritems(leaf);
4633 if (slot + nr != nritems) {
4634 int data_end = leaf_data_end(root, leaf);
4636 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4638 btrfs_leaf_data(leaf) + data_end,
4639 last_off - data_end);
4641 for (i = slot + nr; i < nritems; i++) {
4644 item = btrfs_item_nr(leaf, i);
4645 ioff = btrfs_token_item_offset(leaf, item, &token);
4646 btrfs_set_token_item_offset(leaf, item,
4647 ioff + dsize, &token);
4650 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4651 btrfs_item_nr_offset(slot + nr),
4652 sizeof(struct btrfs_item) *
4653 (nritems - slot - nr));
4655 btrfs_set_header_nritems(leaf, nritems - nr);
4658 /* delete the leaf if we've emptied it */
4660 if (leaf == root->node) {
4661 btrfs_set_header_level(leaf, 0);
4663 btrfs_set_path_blocking(path);
4664 clean_tree_block(trans, root, leaf);
4665 btrfs_del_leaf(trans, root, path, leaf);
4668 int used = leaf_space_used(leaf, 0, nritems);
4670 struct btrfs_disk_key disk_key;
4672 btrfs_item_key(leaf, &disk_key, 0);
4673 fixup_low_keys(trans, root, path, &disk_key, 1);
4676 /* delete the leaf if it is mostly empty */
4677 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
4678 /* push_leaf_left fixes the path.
4679 * make sure the path still points to our leaf
4680 * for possible call to del_ptr below
4682 slot = path->slots[1];
4683 extent_buffer_get(leaf);
4685 btrfs_set_path_blocking(path);
4686 wret = push_leaf_left(trans, root, path, 1, 1,
4688 if (wret < 0 && wret != -ENOSPC)
4691 if (path->nodes[0] == leaf &&
4692 btrfs_header_nritems(leaf)) {
4693 wret = push_leaf_right(trans, root, path, 1,
4695 if (wret < 0 && wret != -ENOSPC)
4699 if (btrfs_header_nritems(leaf) == 0) {
4700 path->slots[1] = slot;
4701 btrfs_del_leaf(trans, root, path, leaf);
4702 free_extent_buffer(leaf);
4705 /* if we're still in the path, make sure
4706 * we're dirty. Otherwise, one of the
4707 * push_leaf functions must have already
4708 * dirtied this buffer
4710 if (path->nodes[0] == leaf)
4711 btrfs_mark_buffer_dirty(leaf);
4712 free_extent_buffer(leaf);
4715 btrfs_mark_buffer_dirty(leaf);
4722 * search the tree again to find a leaf with lesser keys
4723 * returns 0 if it found something or 1 if there are no lesser leaves.
4724 * returns < 0 on io errors.
4726 * This may release the path, and so you may lose any locks held at the
4729 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4731 struct btrfs_key key;
4732 struct btrfs_disk_key found_key;
4735 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
4739 else if (key.type > 0)
4741 else if (key.objectid > 0)
4746 btrfs_release_path(path);
4747 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4750 btrfs_item_key(path->nodes[0], &found_key, 0);
4751 ret = comp_keys(&found_key, &key);
4758 * A helper function to walk down the tree starting at min_key, and looking
4759 * for nodes or leaves that are either in cache or have a minimum
4760 * transaction id. This is used by the btree defrag code, and tree logging
4762 * This does not cow, but it does stuff the starting key it finds back
4763 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4764 * key and get a writable path.
4766 * This does lock as it descends, and path->keep_locks should be set
4767 * to 1 by the caller.
4769 * This honors path->lowest_level to prevent descent past a given level
4772 * min_trans indicates the oldest transaction that you are interested
4773 * in walking through. Any nodes or leaves older than min_trans are
4774 * skipped over (without reading them).
4776 * returns zero if something useful was found, < 0 on error and 1 if there
4777 * was nothing in the tree that matched the search criteria.
4779 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4780 struct btrfs_key *max_key,
4781 struct btrfs_path *path, int cache_only,
4784 struct extent_buffer *cur;
4785 struct btrfs_key found_key;
4792 WARN_ON(!path->keep_locks);
4794 cur = btrfs_read_lock_root_node(root);
4795 level = btrfs_header_level(cur);
4796 WARN_ON(path->nodes[level]);
4797 path->nodes[level] = cur;
4798 path->locks[level] = BTRFS_READ_LOCK;
4800 if (btrfs_header_generation(cur) < min_trans) {
4805 nritems = btrfs_header_nritems(cur);
4806 level = btrfs_header_level(cur);
4807 sret = bin_search(cur, min_key, level, &slot);
4809 /* at the lowest level, we're done, setup the path and exit */
4810 if (level == path->lowest_level) {
4811 if (slot >= nritems)
4814 path->slots[level] = slot;
4815 btrfs_item_key_to_cpu(cur, &found_key, slot);
4818 if (sret && slot > 0)
4821 * check this node pointer against the cache_only and
4822 * min_trans parameters. If it isn't in cache or is too
4823 * old, skip to the next one.
4825 while (slot < nritems) {
4828 struct extent_buffer *tmp;
4829 struct btrfs_disk_key disk_key;
4831 blockptr = btrfs_node_blockptr(cur, slot);
4832 gen = btrfs_node_ptr_generation(cur, slot);
4833 if (gen < min_trans) {
4841 btrfs_node_key(cur, &disk_key, slot);
4842 if (comp_keys(&disk_key, max_key) >= 0) {
4848 tmp = btrfs_find_tree_block(root, blockptr,
4849 btrfs_level_size(root, level - 1));
4851 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
4852 free_extent_buffer(tmp);
4856 free_extent_buffer(tmp);
4861 * we didn't find a candidate key in this node, walk forward
4862 * and find another one
4864 if (slot >= nritems) {
4865 path->slots[level] = slot;
4866 btrfs_set_path_blocking(path);
4867 sret = btrfs_find_next_key(root, path, min_key, level,
4868 cache_only, min_trans);
4870 btrfs_release_path(path);
4876 /* save our key for returning back */
4877 btrfs_node_key_to_cpu(cur, &found_key, slot);
4878 path->slots[level] = slot;
4879 if (level == path->lowest_level) {
4881 unlock_up(path, level, 1, 0, NULL);
4884 btrfs_set_path_blocking(path);
4885 cur = read_node_slot(root, cur, slot);
4886 BUG_ON(!cur); /* -ENOMEM */
4888 btrfs_tree_read_lock(cur);
4890 path->locks[level - 1] = BTRFS_READ_LOCK;
4891 path->nodes[level - 1] = cur;
4892 unlock_up(path, level, 1, 0, NULL);
4893 btrfs_clear_path_blocking(path, NULL, 0);
4897 memcpy(min_key, &found_key, sizeof(found_key));
4898 btrfs_set_path_blocking(path);
4903 * this is similar to btrfs_next_leaf, but does not try to preserve
4904 * and fixup the path. It looks for and returns the next key in the
4905 * tree based on the current path and the cache_only and min_trans
4908 * 0 is returned if another key is found, < 0 if there are any errors
4909 * and 1 is returned if there are no higher keys in the tree
4911 * path->keep_locks should be set to 1 on the search made before
4912 * calling this function.
4914 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4915 struct btrfs_key *key, int level,
4916 int cache_only, u64 min_trans)
4919 struct extent_buffer *c;
4921 WARN_ON(!path->keep_locks);
4922 while (level < BTRFS_MAX_LEVEL) {
4923 if (!path->nodes[level])
4926 slot = path->slots[level] + 1;
4927 c = path->nodes[level];
4929 if (slot >= btrfs_header_nritems(c)) {
4932 struct btrfs_key cur_key;
4933 if (level + 1 >= BTRFS_MAX_LEVEL ||
4934 !path->nodes[level + 1])
4937 if (path->locks[level + 1]) {
4942 slot = btrfs_header_nritems(c) - 1;
4944 btrfs_item_key_to_cpu(c, &cur_key, slot);
4946 btrfs_node_key_to_cpu(c, &cur_key, slot);
4948 orig_lowest = path->lowest_level;
4949 btrfs_release_path(path);
4950 path->lowest_level = level;
4951 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4953 path->lowest_level = orig_lowest;
4957 c = path->nodes[level];
4958 slot = path->slots[level];
4965 btrfs_item_key_to_cpu(c, key, slot);
4967 u64 blockptr = btrfs_node_blockptr(c, slot);
4968 u64 gen = btrfs_node_ptr_generation(c, slot);
4971 struct extent_buffer *cur;
4972 cur = btrfs_find_tree_block(root, blockptr,
4973 btrfs_level_size(root, level - 1));
4975 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
4978 free_extent_buffer(cur);
4981 free_extent_buffer(cur);
4983 if (gen < min_trans) {
4987 btrfs_node_key_to_cpu(c, key, slot);
4995 * search the tree again to find a leaf with greater keys
4996 * returns 0 if it found something or 1 if there are no greater leaves.
4997 * returns < 0 on io errors.
4999 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5003 struct extent_buffer *c;
5004 struct extent_buffer *next;
5005 struct btrfs_key key;
5008 int old_spinning = path->leave_spinning;
5009 int next_rw_lock = 0;
5011 nritems = btrfs_header_nritems(path->nodes[0]);
5015 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5020 btrfs_release_path(path);
5022 path->keep_locks = 1;
5023 path->leave_spinning = 1;
5025 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5026 path->keep_locks = 0;
5031 nritems = btrfs_header_nritems(path->nodes[0]);
5033 * by releasing the path above we dropped all our locks. A balance
5034 * could have added more items next to the key that used to be
5035 * at the very end of the block. So, check again here and
5036 * advance the path if there are now more items available.
5038 if (nritems > 0 && path->slots[0] < nritems - 1) {
5045 while (level < BTRFS_MAX_LEVEL) {
5046 if (!path->nodes[level]) {
5051 slot = path->slots[level] + 1;
5052 c = path->nodes[level];
5053 if (slot >= btrfs_header_nritems(c)) {
5055 if (level == BTRFS_MAX_LEVEL) {
5063 btrfs_tree_unlock_rw(next, next_rw_lock);
5064 free_extent_buffer(next);
5068 next_rw_lock = path->locks[level];
5069 ret = read_block_for_search(NULL, root, path, &next, level,
5075 btrfs_release_path(path);
5079 if (!path->skip_locking) {
5080 ret = btrfs_try_tree_read_lock(next);
5082 btrfs_set_path_blocking(path);
5083 btrfs_tree_read_lock(next);
5084 btrfs_clear_path_blocking(path, next,
5087 next_rw_lock = BTRFS_READ_LOCK;
5091 path->slots[level] = slot;
5094 c = path->nodes[level];
5095 if (path->locks[level])
5096 btrfs_tree_unlock_rw(c, path->locks[level]);
5098 free_extent_buffer(c);
5099 path->nodes[level] = next;
5100 path->slots[level] = 0;
5101 if (!path->skip_locking)
5102 path->locks[level] = next_rw_lock;
5106 ret = read_block_for_search(NULL, root, path, &next, level,
5112 btrfs_release_path(path);
5116 if (!path->skip_locking) {
5117 ret = btrfs_try_tree_read_lock(next);
5119 btrfs_set_path_blocking(path);
5120 btrfs_tree_read_lock(next);
5121 btrfs_clear_path_blocking(path, next,
5124 next_rw_lock = BTRFS_READ_LOCK;
5129 unlock_up(path, 0, 1, 0, NULL);
5130 path->leave_spinning = old_spinning;
5132 btrfs_set_path_blocking(path);
5138 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5139 * searching until it gets past min_objectid or finds an item of 'type'
5141 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5143 int btrfs_previous_item(struct btrfs_root *root,
5144 struct btrfs_path *path, u64 min_objectid,
5147 struct btrfs_key found_key;
5148 struct extent_buffer *leaf;
5153 if (path->slots[0] == 0) {
5154 btrfs_set_path_blocking(path);
5155 ret = btrfs_prev_leaf(root, path);
5161 leaf = path->nodes[0];
5162 nritems = btrfs_header_nritems(leaf);
5165 if (path->slots[0] == nritems)
5168 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5169 if (found_key.objectid < min_objectid)
5171 if (found_key.type == type)
5173 if (found_key.objectid == min_objectid &&
5174 found_key.type < type)