2 * Copyright (C) 2007 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>
22 #include "transaction.h"
23 #include "print-tree.h"
26 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_key *ins_key,
30 struct btrfs_path *path, int data_size, int extend);
31 static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
33 struct extent_buffer *src, int empty);
34 static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
38 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
41 inline void btrfs_init_path(struct btrfs_path *p)
43 memset(p, 0, sizeof(*p));
46 struct btrfs_path *btrfs_alloc_path(void)
48 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
51 btrfs_init_path(path);
57 void btrfs_free_path(struct btrfs_path *p)
59 btrfs_release_path(NULL, p);
60 kmem_cache_free(btrfs_path_cachep, p);
63 void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
67 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
72 btrfs_tree_unlock(p->nodes[i]);
75 free_extent_buffer(p->nodes[i]);
80 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
82 struct extent_buffer *eb;
83 spin_lock(&root->node_lock);
85 extent_buffer_get(eb);
86 spin_unlock(&root->node_lock);
90 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
92 struct extent_buffer *eb;
95 eb = btrfs_root_node(root);
98 spin_lock(&root->node_lock);
99 if (eb == root->node) {
100 spin_unlock(&root->node_lock);
103 spin_unlock(&root->node_lock);
105 btrfs_tree_unlock(eb);
106 free_extent_buffer(eb);
111 static void add_root_to_dirty_list(struct btrfs_root *root)
113 if (root->track_dirty && list_empty(&root->dirty_list)) {
114 list_add(&root->dirty_list,
115 &root->fs_info->dirty_cowonly_roots);
119 int btrfs_copy_root(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root,
121 struct extent_buffer *buf,
122 struct extent_buffer **cow_ret, u64 new_root_objectid)
124 struct extent_buffer *cow;
128 struct btrfs_root *new_root;
130 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
134 memcpy(new_root, root, sizeof(*new_root));
135 new_root->root_key.objectid = new_root_objectid;
137 WARN_ON(root->ref_cows && trans->transid !=
138 root->fs_info->running_transaction->transid);
139 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
141 level = btrfs_header_level(buf);
142 nritems = btrfs_header_nritems(buf);
144 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
145 new_root_objectid, trans->transid,
146 level, buf->start, 0);
152 copy_extent_buffer(cow, buf, 0, 0, cow->len);
153 btrfs_set_header_bytenr(cow, cow->start);
154 btrfs_set_header_generation(cow, trans->transid);
155 btrfs_set_header_owner(cow, new_root_objectid);
156 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
158 WARN_ON(btrfs_header_generation(buf) > trans->transid);
159 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
165 btrfs_mark_buffer_dirty(cow);
170 int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans,
171 struct btrfs_root *root,
172 struct extent_buffer *buf,
173 struct extent_buffer *parent, int parent_slot,
174 struct extent_buffer **cow_ret,
175 u64 search_start, u64 empty_size,
179 struct extent_buffer *cow;
182 int different_trans = 0;
189 WARN_ON(!btrfs_tree_locked(buf));
192 parent_start = parent->start;
196 WARN_ON(root->ref_cows && trans->transid !=
197 root->fs_info->running_transaction->transid);
198 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
200 level = btrfs_header_level(buf);
201 nritems = btrfs_header_nritems(buf);
204 struct btrfs_key ins;
206 ins.objectid = prealloc_dest;
207 ins.offset = buf->len;
208 ins.type = BTRFS_EXTENT_ITEM_KEY;
210 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
211 root->root_key.objectid,
212 trans->transid, level, 0,
215 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
218 cow = btrfs_alloc_free_block(trans, root, buf->len,
220 root->root_key.objectid,
221 trans->transid, level,
222 search_start, empty_size);
227 copy_extent_buffer(cow, buf, 0, 0, cow->len);
228 btrfs_set_header_bytenr(cow, cow->start);
229 btrfs_set_header_generation(cow, trans->transid);
230 btrfs_set_header_owner(cow, root->root_key.objectid);
231 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
233 WARN_ON(btrfs_header_generation(buf) > trans->transid);
234 if (btrfs_header_generation(buf) != trans->transid) {
237 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
241 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
244 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
247 clean_tree_block(trans, root, buf);
250 if (buf == root->node) {
251 WARN_ON(parent && parent != buf);
253 spin_lock(&root->node_lock);
255 extent_buffer_get(cow);
256 spin_unlock(&root->node_lock);
258 if (buf != root->commit_root) {
259 btrfs_free_extent(trans, root, buf->start,
260 buf->len, buf->start,
261 root->root_key.objectid,
262 btrfs_header_generation(buf),
265 free_extent_buffer(buf);
266 add_root_to_dirty_list(root);
268 btrfs_set_node_blockptr(parent, parent_slot,
270 WARN_ON(trans->transid == 0);
271 btrfs_set_node_ptr_generation(parent, parent_slot,
273 btrfs_mark_buffer_dirty(parent);
274 WARN_ON(btrfs_header_generation(parent) != trans->transid);
275 btrfs_free_extent(trans, root, buf->start, buf->len,
276 parent_start, btrfs_header_owner(parent),
277 btrfs_header_generation(parent), 0, 0, 1);
280 btrfs_tree_unlock(buf);
281 free_extent_buffer(buf);
282 btrfs_mark_buffer_dirty(cow);
287 int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
288 struct btrfs_root *root, struct extent_buffer *buf,
289 struct extent_buffer *parent, int parent_slot,
290 struct extent_buffer **cow_ret, u64 prealloc_dest)
295 if (trans->transaction != root->fs_info->running_transaction) {
296 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
297 root->fs_info->running_transaction->transid);
300 if (trans->transid != root->fs_info->generation) {
301 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
302 root->fs_info->generation);
306 spin_lock(&root->fs_info->hash_lock);
307 if (btrfs_header_generation(buf) == trans->transid &&
308 btrfs_header_owner(buf) == root->root_key.objectid &&
309 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
311 spin_unlock(&root->fs_info->hash_lock);
312 WARN_ON(prealloc_dest);
315 spin_unlock(&root->fs_info->hash_lock);
316 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
317 ret = __btrfs_cow_block(trans, root, buf, parent,
318 parent_slot, cow_ret, search_start, 0,
323 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
325 if (blocknr < other && other - (blocknr + blocksize) < 32768)
327 if (blocknr > other && blocknr - (other + blocksize) < 32768)
333 * compare two keys in a memcmp fashion
335 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
339 btrfs_disk_key_to_cpu(&k1, disk);
341 if (k1.objectid > k2->objectid)
343 if (k1.objectid < k2->objectid)
345 if (k1.type > k2->type)
347 if (k1.type < k2->type)
349 if (k1.offset > k2->offset)
351 if (k1.offset < k2->offset)
357 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root, struct extent_buffer *parent,
359 int start_slot, int cache_only, u64 *last_ret,
360 struct btrfs_key *progress)
362 struct extent_buffer *cur;
365 u64 search_start = *last_ret;
375 int progress_passed = 0;
376 struct btrfs_disk_key disk_key;
378 parent_level = btrfs_header_level(parent);
379 if (cache_only && parent_level != 1)
382 if (trans->transaction != root->fs_info->running_transaction) {
383 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
384 root->fs_info->running_transaction->transid);
387 if (trans->transid != root->fs_info->generation) {
388 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
389 root->fs_info->generation);
393 parent_nritems = btrfs_header_nritems(parent);
394 blocksize = btrfs_level_size(root, parent_level - 1);
395 end_slot = parent_nritems;
397 if (parent_nritems == 1)
400 for (i = start_slot; i < end_slot; i++) {
403 if (!parent->map_token) {
404 map_extent_buffer(parent,
405 btrfs_node_key_ptr_offset(i),
406 sizeof(struct btrfs_key_ptr),
407 &parent->map_token, &parent->kaddr,
408 &parent->map_start, &parent->map_len,
411 btrfs_node_key(parent, &disk_key, i);
412 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
416 blocknr = btrfs_node_blockptr(parent, i);
417 gen = btrfs_node_ptr_generation(parent, i);
419 last_block = blocknr;
422 other = btrfs_node_blockptr(parent, i - 1);
423 close = close_blocks(blocknr, other, blocksize);
425 if (!close && i < end_slot - 2) {
426 other = btrfs_node_blockptr(parent, i + 1);
427 close = close_blocks(blocknr, other, blocksize);
430 last_block = blocknr;
433 if (parent->map_token) {
434 unmap_extent_buffer(parent, parent->map_token,
436 parent->map_token = NULL;
439 cur = btrfs_find_tree_block(root, blocknr, blocksize);
441 uptodate = btrfs_buffer_uptodate(cur, gen);
444 if (!cur || !uptodate) {
446 free_extent_buffer(cur);
450 cur = read_tree_block(root, blocknr,
452 } else if (!uptodate) {
453 btrfs_read_buffer(cur, gen);
456 if (search_start == 0)
457 search_start = last_block;
459 btrfs_tree_lock(cur);
460 err = __btrfs_cow_block(trans, root, cur, parent, i,
463 (end_slot - i) * blocksize), 0);
465 btrfs_tree_unlock(cur);
466 free_extent_buffer(cur);
469 search_start = cur->start;
470 last_block = cur->start;
471 *last_ret = search_start;
472 btrfs_tree_unlock(cur);
473 free_extent_buffer(cur);
475 if (parent->map_token) {
476 unmap_extent_buffer(parent, parent->map_token,
478 parent->map_token = NULL;
484 * The leaf data grows from end-to-front in the node.
485 * this returns the address of the start of the last item,
486 * which is the stop of the leaf data stack
488 static inline unsigned int leaf_data_end(struct btrfs_root *root,
489 struct extent_buffer *leaf)
491 u32 nr = btrfs_header_nritems(leaf);
493 return BTRFS_LEAF_DATA_SIZE(root);
494 return btrfs_item_offset_nr(leaf, nr - 1);
497 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
500 struct extent_buffer *parent = NULL;
501 struct extent_buffer *node = path->nodes[level];
502 struct btrfs_disk_key parent_key;
503 struct btrfs_disk_key node_key;
506 struct btrfs_key cpukey;
507 u32 nritems = btrfs_header_nritems(node);
509 if (path->nodes[level + 1])
510 parent = path->nodes[level + 1];
512 slot = path->slots[level];
513 BUG_ON(nritems == 0);
515 parent_slot = path->slots[level + 1];
516 btrfs_node_key(parent, &parent_key, parent_slot);
517 btrfs_node_key(node, &node_key, 0);
518 BUG_ON(memcmp(&parent_key, &node_key,
519 sizeof(struct btrfs_disk_key)));
520 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
521 btrfs_header_bytenr(node));
523 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
525 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
526 btrfs_node_key(node, &node_key, slot);
527 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
529 if (slot < nritems - 1) {
530 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
531 btrfs_node_key(node, &node_key, slot);
532 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
537 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
540 struct extent_buffer *leaf = path->nodes[level];
541 struct extent_buffer *parent = NULL;
543 struct btrfs_key cpukey;
544 struct btrfs_disk_key parent_key;
545 struct btrfs_disk_key leaf_key;
546 int slot = path->slots[0];
548 u32 nritems = btrfs_header_nritems(leaf);
550 if (path->nodes[level + 1])
551 parent = path->nodes[level + 1];
557 parent_slot = path->slots[level + 1];
558 btrfs_node_key(parent, &parent_key, parent_slot);
559 btrfs_item_key(leaf, &leaf_key, 0);
561 BUG_ON(memcmp(&parent_key, &leaf_key,
562 sizeof(struct btrfs_disk_key)));
563 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
564 btrfs_header_bytenr(leaf));
567 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
568 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
569 btrfs_item_key(leaf, &leaf_key, i);
570 if (comp_keys(&leaf_key, &cpukey) >= 0) {
571 btrfs_print_leaf(root, leaf);
572 printk("slot %d offset bad key\n", i);
575 if (btrfs_item_offset_nr(leaf, i) !=
576 btrfs_item_end_nr(leaf, i + 1)) {
577 btrfs_print_leaf(root, leaf);
578 printk("slot %d offset bad\n", i);
582 if (btrfs_item_offset_nr(leaf, i) +
583 btrfs_item_size_nr(leaf, i) !=
584 BTRFS_LEAF_DATA_SIZE(root)) {
585 btrfs_print_leaf(root, leaf);
586 printk("slot %d first offset bad\n", i);
592 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
593 btrfs_print_leaf(root, leaf);
594 printk("slot %d bad size \n", nritems - 1);
599 if (slot != 0 && slot < nritems - 1) {
600 btrfs_item_key(leaf, &leaf_key, slot);
601 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
602 if (comp_keys(&leaf_key, &cpukey) <= 0) {
603 btrfs_print_leaf(root, leaf);
604 printk("slot %d offset bad key\n", slot);
607 if (btrfs_item_offset_nr(leaf, slot - 1) !=
608 btrfs_item_end_nr(leaf, slot)) {
609 btrfs_print_leaf(root, leaf);
610 printk("slot %d offset bad\n", slot);
614 if (slot < nritems - 1) {
615 btrfs_item_key(leaf, &leaf_key, slot);
616 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
617 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
618 if (btrfs_item_offset_nr(leaf, slot) !=
619 btrfs_item_end_nr(leaf, slot + 1)) {
620 btrfs_print_leaf(root, leaf);
621 printk("slot %d offset bad\n", slot);
625 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
626 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
630 static int noinline check_block(struct btrfs_root *root,
631 struct btrfs_path *path, int level)
635 if (btrfs_header_level(path->nodes[level]) != level)
636 printk("warning: bad level %Lu wanted %d found %d\n",
637 path->nodes[level]->start, level,
638 btrfs_header_level(path->nodes[level]));
639 found_start = btrfs_header_bytenr(path->nodes[level]);
640 if (found_start != path->nodes[level]->start) {
641 printk("warning: bad bytentr %Lu found %Lu\n",
642 path->nodes[level]->start, found_start);
645 struct extent_buffer *buf = path->nodes[level];
647 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
648 (unsigned long)btrfs_header_fsid(buf),
650 printk("warning bad block %Lu\n", buf->start);
655 return check_leaf(root, path, level);
656 return check_node(root, path, level);
660 * search for key in the extent_buffer. The items start at offset p,
661 * and they are item_size apart. There are 'max' items in p.
663 * the slot in the array is returned via slot, and it points to
664 * the place where you would insert key if it is not found in
667 * slot may point to max if the key is bigger than all of the keys
669 static noinline int generic_bin_search(struct extent_buffer *eb,
671 int item_size, struct btrfs_key *key,
678 struct btrfs_disk_key *tmp = NULL;
679 struct btrfs_disk_key unaligned;
680 unsigned long offset;
681 char *map_token = NULL;
683 unsigned long map_start = 0;
684 unsigned long map_len = 0;
688 mid = (low + high) / 2;
689 offset = p + mid * item_size;
691 if (!map_token || offset < map_start ||
692 (offset + sizeof(struct btrfs_disk_key)) >
693 map_start + map_len) {
695 unmap_extent_buffer(eb, map_token, KM_USER0);
698 err = map_extent_buffer(eb, offset,
699 sizeof(struct btrfs_disk_key),
701 &map_start, &map_len, KM_USER0);
704 tmp = (struct btrfs_disk_key *)(kaddr + offset -
707 read_extent_buffer(eb, &unaligned,
708 offset, sizeof(unaligned));
713 tmp = (struct btrfs_disk_key *)(kaddr + offset -
716 ret = comp_keys(tmp, key);
725 unmap_extent_buffer(eb, map_token, KM_USER0);
731 unmap_extent_buffer(eb, map_token, KM_USER0);
736 * simple bin_search frontend that does the right thing for
739 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
740 int level, int *slot)
743 return generic_bin_search(eb,
744 offsetof(struct btrfs_leaf, items),
745 sizeof(struct btrfs_item),
746 key, btrfs_header_nritems(eb),
749 return generic_bin_search(eb,
750 offsetof(struct btrfs_node, ptrs),
751 sizeof(struct btrfs_key_ptr),
752 key, btrfs_header_nritems(eb),
758 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
759 struct extent_buffer *parent, int slot)
761 int level = btrfs_header_level(parent);
764 if (slot >= btrfs_header_nritems(parent))
769 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
770 btrfs_level_size(root, level - 1),
771 btrfs_node_ptr_generation(parent, slot));
774 static noinline int balance_level(struct btrfs_trans_handle *trans,
775 struct btrfs_root *root,
776 struct btrfs_path *path, int level)
778 struct extent_buffer *right = NULL;
779 struct extent_buffer *mid;
780 struct extent_buffer *left = NULL;
781 struct extent_buffer *parent = NULL;
785 int orig_slot = path->slots[level];
786 int err_on_enospc = 0;
792 mid = path->nodes[level];
793 WARN_ON(!path->locks[level]);
794 WARN_ON(btrfs_header_generation(mid) != trans->transid);
796 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
798 if (level < BTRFS_MAX_LEVEL - 1)
799 parent = path->nodes[level + 1];
800 pslot = path->slots[level + 1];
803 * deal with the case where there is only one pointer in the root
804 * by promoting the node below to a root
807 struct extent_buffer *child;
809 if (btrfs_header_nritems(mid) != 1)
812 /* promote the child to a root */
813 child = read_node_slot(root, mid, 0);
814 btrfs_tree_lock(child);
816 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
819 spin_lock(&root->node_lock);
821 spin_unlock(&root->node_lock);
823 ret = btrfs_update_extent_ref(trans, root, child->start,
824 mid->start, child->start,
825 root->root_key.objectid,
826 trans->transid, level - 1, 0);
829 add_root_to_dirty_list(root);
830 btrfs_tree_unlock(child);
831 path->locks[level] = 0;
832 path->nodes[level] = NULL;
833 clean_tree_block(trans, root, mid);
834 btrfs_tree_unlock(mid);
835 /* once for the path */
836 free_extent_buffer(mid);
837 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
838 mid->start, root->root_key.objectid,
839 btrfs_header_generation(mid), 0, 0, 1);
840 /* once for the root ptr */
841 free_extent_buffer(mid);
844 if (btrfs_header_nritems(mid) >
845 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
848 if (btrfs_header_nritems(mid) < 2)
851 left = read_node_slot(root, parent, pslot - 1);
853 btrfs_tree_lock(left);
854 wret = btrfs_cow_block(trans, root, left,
855 parent, pslot - 1, &left, 0);
861 right = read_node_slot(root, parent, pslot + 1);
863 btrfs_tree_lock(right);
864 wret = btrfs_cow_block(trans, root, right,
865 parent, pslot + 1, &right, 0);
872 /* first, try to make some room in the middle buffer */
874 orig_slot += btrfs_header_nritems(left);
875 wret = push_node_left(trans, root, left, mid, 1);
878 if (btrfs_header_nritems(mid) < 2)
883 * then try to empty the right most buffer into the middle
886 wret = push_node_left(trans, root, mid, right, 1);
887 if (wret < 0 && wret != -ENOSPC)
889 if (btrfs_header_nritems(right) == 0) {
890 u64 bytenr = right->start;
891 u64 generation = btrfs_header_generation(parent);
892 u32 blocksize = right->len;
894 clean_tree_block(trans, root, right);
895 btrfs_tree_unlock(right);
896 free_extent_buffer(right);
898 wret = del_ptr(trans, root, path, level + 1, pslot +
902 wret = btrfs_free_extent(trans, root, bytenr,
903 blocksize, parent->start,
904 btrfs_header_owner(parent),
905 generation, 0, 0, 1);
909 struct btrfs_disk_key right_key;
910 btrfs_node_key(right, &right_key, 0);
911 btrfs_set_node_key(parent, &right_key, pslot + 1);
912 btrfs_mark_buffer_dirty(parent);
915 if (btrfs_header_nritems(mid) == 1) {
917 * we're not allowed to leave a node with one item in the
918 * tree during a delete. A deletion from lower in the tree
919 * could try to delete the only pointer in this node.
920 * So, pull some keys from the left.
921 * There has to be a left pointer at this point because
922 * otherwise we would have pulled some pointers from the
926 wret = balance_node_right(trans, root, mid, left);
932 wret = push_node_left(trans, root, left, mid, 1);
938 if (btrfs_header_nritems(mid) == 0) {
939 /* we've managed to empty the middle node, drop it */
940 u64 root_gen = btrfs_header_generation(parent);
941 u64 bytenr = mid->start;
942 u32 blocksize = mid->len;
944 clean_tree_block(trans, root, mid);
945 btrfs_tree_unlock(mid);
946 free_extent_buffer(mid);
948 wret = del_ptr(trans, root, path, level + 1, pslot);
951 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
953 btrfs_header_owner(parent),
958 /* update the parent key to reflect our changes */
959 struct btrfs_disk_key mid_key;
960 btrfs_node_key(mid, &mid_key, 0);
961 btrfs_set_node_key(parent, &mid_key, pslot);
962 btrfs_mark_buffer_dirty(parent);
965 /* update the path */
967 if (btrfs_header_nritems(left) > orig_slot) {
968 extent_buffer_get(left);
969 /* left was locked after cow */
970 path->nodes[level] = left;
971 path->slots[level + 1] -= 1;
972 path->slots[level] = orig_slot;
974 btrfs_tree_unlock(mid);
975 free_extent_buffer(mid);
978 orig_slot -= btrfs_header_nritems(left);
979 path->slots[level] = orig_slot;
982 /* double check we haven't messed things up */
983 check_block(root, path, level);
985 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
989 btrfs_tree_unlock(right);
990 free_extent_buffer(right);
993 if (path->nodes[level] != left)
994 btrfs_tree_unlock(left);
995 free_extent_buffer(left);
1000 /* returns zero if the push worked, non-zero otherwise */
1001 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1002 struct btrfs_root *root,
1003 struct btrfs_path *path, int level)
1005 struct extent_buffer *right = NULL;
1006 struct extent_buffer *mid;
1007 struct extent_buffer *left = NULL;
1008 struct extent_buffer *parent = NULL;
1012 int orig_slot = path->slots[level];
1018 mid = path->nodes[level];
1019 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1020 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1022 if (level < BTRFS_MAX_LEVEL - 1)
1023 parent = path->nodes[level + 1];
1024 pslot = path->slots[level + 1];
1029 left = read_node_slot(root, parent, pslot - 1);
1031 /* first, try to make some room in the middle buffer */
1035 btrfs_tree_lock(left);
1036 left_nr = btrfs_header_nritems(left);
1037 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1040 ret = btrfs_cow_block(trans, root, left, parent,
1041 pslot - 1, &left, 0);
1045 wret = push_node_left(trans, root,
1052 struct btrfs_disk_key disk_key;
1053 orig_slot += left_nr;
1054 btrfs_node_key(mid, &disk_key, 0);
1055 btrfs_set_node_key(parent, &disk_key, pslot);
1056 btrfs_mark_buffer_dirty(parent);
1057 if (btrfs_header_nritems(left) > orig_slot) {
1058 path->nodes[level] = left;
1059 path->slots[level + 1] -= 1;
1060 path->slots[level] = orig_slot;
1061 btrfs_tree_unlock(mid);
1062 free_extent_buffer(mid);
1065 btrfs_header_nritems(left);
1066 path->slots[level] = orig_slot;
1067 btrfs_tree_unlock(left);
1068 free_extent_buffer(left);
1072 btrfs_tree_unlock(left);
1073 free_extent_buffer(left);
1075 right = read_node_slot(root, parent, pslot + 1);
1078 * then try to empty the right most buffer into the middle
1082 btrfs_tree_lock(right);
1083 right_nr = btrfs_header_nritems(right);
1084 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1087 ret = btrfs_cow_block(trans, root, right,
1093 wret = balance_node_right(trans, root,
1100 struct btrfs_disk_key disk_key;
1102 btrfs_node_key(right, &disk_key, 0);
1103 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1104 btrfs_mark_buffer_dirty(parent);
1106 if (btrfs_header_nritems(mid) <= orig_slot) {
1107 path->nodes[level] = right;
1108 path->slots[level + 1] += 1;
1109 path->slots[level] = orig_slot -
1110 btrfs_header_nritems(mid);
1111 btrfs_tree_unlock(mid);
1112 free_extent_buffer(mid);
1114 btrfs_tree_unlock(right);
1115 free_extent_buffer(right);
1119 btrfs_tree_unlock(right);
1120 free_extent_buffer(right);
1126 * readahead one full node of leaves
1128 static noinline void reada_for_search(struct btrfs_root *root,
1129 struct btrfs_path *path,
1130 int level, int slot, u64 objectid)
1132 struct extent_buffer *node;
1133 struct btrfs_disk_key disk_key;
1139 int direction = path->reada;
1140 struct extent_buffer *eb;
1148 if (!path->nodes[level])
1151 node = path->nodes[level];
1153 search = btrfs_node_blockptr(node, slot);
1154 blocksize = btrfs_level_size(root, level - 1);
1155 eb = btrfs_find_tree_block(root, search, blocksize);
1157 free_extent_buffer(eb);
1161 highest_read = search;
1162 lowest_read = search;
1164 nritems = btrfs_header_nritems(node);
1167 if (direction < 0) {
1171 } else if (direction > 0) {
1176 if (path->reada < 0 && objectid) {
1177 btrfs_node_key(node, &disk_key, nr);
1178 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1181 search = btrfs_node_blockptr(node, nr);
1182 if ((search >= lowest_read && search <= highest_read) ||
1183 (search < lowest_read && lowest_read - search <= 32768) ||
1184 (search > highest_read && search - highest_read <= 32768)) {
1185 readahead_tree_block(root, search, blocksize,
1186 btrfs_node_ptr_generation(node, nr));
1190 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1192 if(nread > (1024 * 1024) || nscan > 128)
1195 if (search < lowest_read)
1196 lowest_read = search;
1197 if (search > highest_read)
1198 highest_read = search;
1202 static noinline void unlock_up(struct btrfs_path *path, int level,
1206 int skip_level = level;
1208 struct extent_buffer *t;
1210 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1211 if (!path->nodes[i])
1213 if (!path->locks[i])
1215 if (!no_skips && path->slots[i] == 0) {
1219 if (!no_skips && path->keep_locks) {
1222 nritems = btrfs_header_nritems(t);
1223 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1228 if (skip_level < i && i >= lowest_unlock)
1232 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1233 btrfs_tree_unlock(t);
1240 * look for key in the tree. path is filled in with nodes along the way
1241 * if key is found, we return zero and you can find the item in the leaf
1242 * level of the path (level 0)
1244 * If the key isn't found, the path points to the slot where it should
1245 * be inserted, and 1 is returned. If there are other errors during the
1246 * search a negative error number is returned.
1248 * if ins_len > 0, nodes and leaves will be split as we walk down the
1249 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1252 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1253 *root, struct btrfs_key *key, struct btrfs_path *p, int
1256 struct extent_buffer *b;
1257 struct extent_buffer *tmp;
1261 int should_reada = p->reada;
1262 int lowest_unlock = 1;
1264 u8 lowest_level = 0;
1267 struct btrfs_key prealloc_block;
1269 lowest_level = p->lowest_level;
1270 WARN_ON(lowest_level && ins_len);
1271 WARN_ON(p->nodes[0] != NULL);
1272 WARN_ON(cow && root == root->fs_info->extent_root &&
1273 !mutex_is_locked(&root->fs_info->alloc_mutex));
1277 prealloc_block.objectid = 0;
1280 if (p->skip_locking)
1281 b = btrfs_root_node(root);
1283 b = btrfs_lock_root_node(root);
1286 level = btrfs_header_level(b);
1289 * setup the path here so we can release it under lock
1290 * contention with the cow code
1292 p->nodes[level] = b;
1293 if (!p->skip_locking)
1294 p->locks[level] = 1;
1299 /* is a cow on this block not required */
1300 spin_lock(&root->fs_info->hash_lock);
1301 if (btrfs_header_generation(b) == trans->transid &&
1302 btrfs_header_owner(b) == root->root_key.objectid &&
1303 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1304 spin_unlock(&root->fs_info->hash_lock);
1307 spin_unlock(&root->fs_info->hash_lock);
1309 /* ok, we have to cow, is our old prealloc the right
1312 if (prealloc_block.objectid &&
1313 prealloc_block.offset != b->len) {
1314 btrfs_free_reserved_extent(root,
1315 prealloc_block.objectid,
1316 prealloc_block.offset);
1317 prealloc_block.objectid = 0;
1321 * for higher level blocks, try not to allocate blocks
1322 * with the block and the parent locks held.
1324 if (level > 1 && !prealloc_block.objectid &&
1325 btrfs_path_lock_waiting(p, level)) {
1327 u64 hint = b->start;
1329 btrfs_release_path(root, p);
1330 ret = btrfs_reserve_extent(trans, root,
1333 &prealloc_block, 0);
1338 wret = btrfs_cow_block(trans, root, b,
1339 p->nodes[level + 1],
1340 p->slots[level + 1],
1341 &b, prealloc_block.objectid);
1342 prealloc_block.objectid = 0;
1344 free_extent_buffer(b);
1350 BUG_ON(!cow && ins_len);
1351 if (level != btrfs_header_level(b))
1353 level = btrfs_header_level(b);
1355 p->nodes[level] = b;
1356 if (!p->skip_locking)
1357 p->locks[level] = 1;
1359 ret = check_block(root, p, level);
1365 ret = bin_search(b, key, level, &slot);
1367 if (ret && slot > 0)
1369 p->slots[level] = slot;
1370 if (ins_len > 0 && btrfs_header_nritems(b) >=
1371 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1372 int sret = split_node(trans, root, p, level);
1378 b = p->nodes[level];
1379 slot = p->slots[level];
1380 } else if (ins_len < 0) {
1381 int sret = balance_level(trans, root, p,
1387 b = p->nodes[level];
1389 btrfs_release_path(NULL, p);
1392 slot = p->slots[level];
1393 BUG_ON(btrfs_header_nritems(b) == 1);
1395 unlock_up(p, level, lowest_unlock);
1397 /* this is only true while dropping a snapshot */
1398 if (level == lowest_level) {
1403 blocknr = btrfs_node_blockptr(b, slot);
1404 gen = btrfs_node_ptr_generation(b, slot);
1405 blocksize = btrfs_level_size(root, level - 1);
1407 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1408 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1412 * reduce lock contention at high levels
1413 * of the btree by dropping locks before
1417 btrfs_release_path(NULL, p);
1419 free_extent_buffer(tmp);
1421 reada_for_search(root, p,
1425 tmp = read_tree_block(root, blocknr,
1428 free_extent_buffer(tmp);
1432 free_extent_buffer(tmp);
1434 reada_for_search(root, p,
1437 b = read_node_slot(root, b, slot);
1440 if (!p->skip_locking)
1443 p->slots[level] = slot;
1444 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1445 sizeof(struct btrfs_item) + ins_len) {
1446 int sret = split_leaf(trans, root, key,
1447 p, ins_len, ret == 0);
1454 unlock_up(p, level, lowest_unlock);
1460 if (prealloc_block.objectid) {
1461 btrfs_free_reserved_extent(root,
1462 prealloc_block.objectid,
1463 prealloc_block.offset);
1470 * adjust the pointers going up the tree, starting at level
1471 * making sure the right key of each node is points to 'key'.
1472 * This is used after shifting pointers to the left, so it stops
1473 * fixing up pointers when a given leaf/node is not in slot 0 of the
1476 * If this fails to write a tree block, it returns -1, but continues
1477 * fixing up the blocks in ram so the tree is consistent.
1479 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1480 struct btrfs_root *root, struct btrfs_path *path,
1481 struct btrfs_disk_key *key, int level)
1485 struct extent_buffer *t;
1487 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1488 int tslot = path->slots[i];
1489 if (!path->nodes[i])
1492 btrfs_set_node_key(t, key, tslot);
1493 btrfs_mark_buffer_dirty(path->nodes[i]);
1503 * This function isn't completely safe. It's the caller's responsibility
1504 * that the new key won't break the order
1506 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1507 struct btrfs_root *root, struct btrfs_path *path,
1508 struct btrfs_key *new_key)
1510 struct btrfs_disk_key disk_key;
1511 struct extent_buffer *eb;
1514 eb = path->nodes[0];
1515 slot = path->slots[0];
1517 btrfs_item_key(eb, &disk_key, slot - 1);
1518 if (comp_keys(&disk_key, new_key) >= 0)
1521 if (slot < btrfs_header_nritems(eb) - 1) {
1522 btrfs_item_key(eb, &disk_key, slot + 1);
1523 if (comp_keys(&disk_key, new_key) <= 0)
1527 btrfs_cpu_key_to_disk(&disk_key, new_key);
1528 btrfs_set_item_key(eb, &disk_key, slot);
1529 btrfs_mark_buffer_dirty(eb);
1531 fixup_low_keys(trans, root, path, &disk_key, 1);
1536 * try to push data from one node into the next node left in the
1539 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1540 * error, and > 0 if there was no room in the left hand block.
1542 static int push_node_left(struct btrfs_trans_handle *trans,
1543 struct btrfs_root *root, struct extent_buffer *dst,
1544 struct extent_buffer *src, int empty)
1551 src_nritems = btrfs_header_nritems(src);
1552 dst_nritems = btrfs_header_nritems(dst);
1553 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1554 WARN_ON(btrfs_header_generation(src) != trans->transid);
1555 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1557 if (!empty && src_nritems <= 8)
1560 if (push_items <= 0) {
1565 push_items = min(src_nritems, push_items);
1566 if (push_items < src_nritems) {
1567 /* leave at least 8 pointers in the node if
1568 * we aren't going to empty it
1570 if (src_nritems - push_items < 8) {
1571 if (push_items <= 8)
1577 push_items = min(src_nritems - 8, push_items);
1579 copy_extent_buffer(dst, src,
1580 btrfs_node_key_ptr_offset(dst_nritems),
1581 btrfs_node_key_ptr_offset(0),
1582 push_items * sizeof(struct btrfs_key_ptr));
1584 if (push_items < src_nritems) {
1585 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1586 btrfs_node_key_ptr_offset(push_items),
1587 (src_nritems - push_items) *
1588 sizeof(struct btrfs_key_ptr));
1590 btrfs_set_header_nritems(src, src_nritems - push_items);
1591 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1592 btrfs_mark_buffer_dirty(src);
1593 btrfs_mark_buffer_dirty(dst);
1595 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1602 * try to push data from one node into the next node right in the
1605 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1606 * error, and > 0 if there was no room in the right hand block.
1608 * this will only push up to 1/2 the contents of the left node over
1610 static int balance_node_right(struct btrfs_trans_handle *trans,
1611 struct btrfs_root *root,
1612 struct extent_buffer *dst,
1613 struct extent_buffer *src)
1621 WARN_ON(btrfs_header_generation(src) != trans->transid);
1622 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1624 src_nritems = btrfs_header_nritems(src);
1625 dst_nritems = btrfs_header_nritems(dst);
1626 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1627 if (push_items <= 0) {
1631 if (src_nritems < 4) {
1635 max_push = src_nritems / 2 + 1;
1636 /* don't try to empty the node */
1637 if (max_push >= src_nritems) {
1641 if (max_push < push_items)
1642 push_items = max_push;
1644 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1645 btrfs_node_key_ptr_offset(0),
1647 sizeof(struct btrfs_key_ptr));
1649 copy_extent_buffer(dst, src,
1650 btrfs_node_key_ptr_offset(0),
1651 btrfs_node_key_ptr_offset(src_nritems - push_items),
1652 push_items * sizeof(struct btrfs_key_ptr));
1654 btrfs_set_header_nritems(src, src_nritems - push_items);
1655 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1657 btrfs_mark_buffer_dirty(src);
1658 btrfs_mark_buffer_dirty(dst);
1660 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1667 * helper function to insert a new root level in the tree.
1668 * A new node is allocated, and a single item is inserted to
1669 * point to the existing root
1671 * returns zero on success or < 0 on failure.
1673 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1674 struct btrfs_root *root,
1675 struct btrfs_path *path, int level)
1678 struct extent_buffer *lower;
1679 struct extent_buffer *c;
1680 struct extent_buffer *old;
1681 struct btrfs_disk_key lower_key;
1684 BUG_ON(path->nodes[level]);
1685 BUG_ON(path->nodes[level-1] != root->node);
1687 lower = path->nodes[level-1];
1689 btrfs_item_key(lower, &lower_key, 0);
1691 btrfs_node_key(lower, &lower_key, 0);
1693 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1694 root->root_key.objectid, trans->transid,
1695 level, root->node->start, 0);
1699 memset_extent_buffer(c, 0, 0, root->nodesize);
1700 btrfs_set_header_nritems(c, 1);
1701 btrfs_set_header_level(c, level);
1702 btrfs_set_header_bytenr(c, c->start);
1703 btrfs_set_header_generation(c, trans->transid);
1704 btrfs_set_header_owner(c, root->root_key.objectid);
1706 write_extent_buffer(c, root->fs_info->fsid,
1707 (unsigned long)btrfs_header_fsid(c),
1710 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1711 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1714 btrfs_set_node_key(c, &lower_key, 0);
1715 btrfs_set_node_blockptr(c, 0, lower->start);
1716 lower_gen = btrfs_header_generation(lower);
1717 WARN_ON(lower_gen != trans->transid);
1719 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1721 btrfs_mark_buffer_dirty(c);
1723 spin_lock(&root->node_lock);
1726 spin_unlock(&root->node_lock);
1728 ret = btrfs_update_extent_ref(trans, root, lower->start,
1729 lower->start, c->start,
1730 root->root_key.objectid,
1731 trans->transid, level - 1, 0);
1734 /* the super has an extra ref to root->node */
1735 free_extent_buffer(old);
1737 add_root_to_dirty_list(root);
1738 extent_buffer_get(c);
1739 path->nodes[level] = c;
1740 path->locks[level] = 1;
1741 path->slots[level] = 0;
1746 * worker function to insert a single pointer in a node.
1747 * the node should have enough room for the pointer already
1749 * slot and level indicate where you want the key to go, and
1750 * blocknr is the block the key points to.
1752 * returns zero on success and < 0 on any error
1754 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1755 *root, struct btrfs_path *path, struct btrfs_disk_key
1756 *key, u64 bytenr, int slot, int level)
1758 struct extent_buffer *lower;
1761 BUG_ON(!path->nodes[level]);
1762 lower = path->nodes[level];
1763 nritems = btrfs_header_nritems(lower);
1766 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1768 if (slot != nritems) {
1769 memmove_extent_buffer(lower,
1770 btrfs_node_key_ptr_offset(slot + 1),
1771 btrfs_node_key_ptr_offset(slot),
1772 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1774 btrfs_set_node_key(lower, key, slot);
1775 btrfs_set_node_blockptr(lower, slot, bytenr);
1776 WARN_ON(trans->transid == 0);
1777 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1778 btrfs_set_header_nritems(lower, nritems + 1);
1779 btrfs_mark_buffer_dirty(lower);
1784 * split the node at the specified level in path in two.
1785 * The path is corrected to point to the appropriate node after the split
1787 * Before splitting this tries to make some room in the node by pushing
1788 * left and right, if either one works, it returns right away.
1790 * returns 0 on success and < 0 on failure
1792 static noinline int split_node(struct btrfs_trans_handle *trans,
1793 struct btrfs_root *root,
1794 struct btrfs_path *path, int level)
1796 struct extent_buffer *c;
1797 struct extent_buffer *split;
1798 struct btrfs_disk_key disk_key;
1804 c = path->nodes[level];
1805 WARN_ON(btrfs_header_generation(c) != trans->transid);
1806 if (c == root->node) {
1807 /* trying to split the root, lets make a new one */
1808 ret = insert_new_root(trans, root, path, level + 1);
1812 ret = push_nodes_for_insert(trans, root, path, level);
1813 c = path->nodes[level];
1814 if (!ret && btrfs_header_nritems(c) <
1815 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1821 c_nritems = btrfs_header_nritems(c);
1823 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1824 path->nodes[level + 1]->start,
1825 root->root_key.objectid,
1826 trans->transid, level, c->start, 0);
1828 return PTR_ERR(split);
1830 btrfs_set_header_flags(split, btrfs_header_flags(c));
1831 btrfs_set_header_level(split, btrfs_header_level(c));
1832 btrfs_set_header_bytenr(split, split->start);
1833 btrfs_set_header_generation(split, trans->transid);
1834 btrfs_set_header_owner(split, root->root_key.objectid);
1835 btrfs_set_header_flags(split, 0);
1836 write_extent_buffer(split, root->fs_info->fsid,
1837 (unsigned long)btrfs_header_fsid(split),
1839 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1840 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1843 mid = (c_nritems + 1) / 2;
1845 copy_extent_buffer(split, c,
1846 btrfs_node_key_ptr_offset(0),
1847 btrfs_node_key_ptr_offset(mid),
1848 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1849 btrfs_set_header_nritems(split, c_nritems - mid);
1850 btrfs_set_header_nritems(c, mid);
1853 btrfs_mark_buffer_dirty(c);
1854 btrfs_mark_buffer_dirty(split);
1856 btrfs_node_key(split, &disk_key, 0);
1857 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1858 path->slots[level + 1] + 1,
1863 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
1866 if (path->slots[level] >= mid) {
1867 path->slots[level] -= mid;
1868 btrfs_tree_unlock(c);
1869 free_extent_buffer(c);
1870 path->nodes[level] = split;
1871 path->slots[level + 1] += 1;
1873 btrfs_tree_unlock(split);
1874 free_extent_buffer(split);
1880 * how many bytes are required to store the items in a leaf. start
1881 * and nr indicate which items in the leaf to check. This totals up the
1882 * space used both by the item structs and the item data
1884 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1887 int nritems = btrfs_header_nritems(l);
1888 int end = min(nritems, start + nr) - 1;
1892 data_len = btrfs_item_end_nr(l, start);
1893 data_len = data_len - btrfs_item_offset_nr(l, end);
1894 data_len += sizeof(struct btrfs_item) * nr;
1895 WARN_ON(data_len < 0);
1900 * The space between the end of the leaf items and
1901 * the start of the leaf data. IOW, how much room
1902 * the leaf has left for both items and data
1904 int noinline btrfs_leaf_free_space(struct btrfs_root *root,
1905 struct extent_buffer *leaf)
1907 int nritems = btrfs_header_nritems(leaf);
1909 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1911 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1912 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1913 leaf_space_used(leaf, 0, nritems), nritems);
1919 * push some data in the path leaf to the right, trying to free up at
1920 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1922 * returns 1 if the push failed because the other node didn't have enough
1923 * room, 0 if everything worked out and < 0 if there were major errors.
1925 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1926 *root, struct btrfs_path *path, int data_size,
1929 struct extent_buffer *left = path->nodes[0];
1930 struct extent_buffer *right;
1931 struct extent_buffer *upper;
1932 struct btrfs_disk_key disk_key;
1938 struct btrfs_item *item;
1946 slot = path->slots[1];
1947 if (!path->nodes[1]) {
1950 upper = path->nodes[1];
1951 if (slot >= btrfs_header_nritems(upper) - 1)
1954 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1956 right = read_node_slot(root, upper, slot + 1);
1957 btrfs_tree_lock(right);
1958 free_space = btrfs_leaf_free_space(root, right);
1959 if (free_space < data_size + sizeof(struct btrfs_item))
1962 /* cow and double check */
1963 ret = btrfs_cow_block(trans, root, right, upper,
1964 slot + 1, &right, 0);
1968 free_space = btrfs_leaf_free_space(root, right);
1969 if (free_space < data_size + sizeof(struct btrfs_item))
1972 left_nritems = btrfs_header_nritems(left);
1973 if (left_nritems == 0)
1981 if (path->slots[0] >= left_nritems)
1982 push_space += data_size + sizeof(*item);
1984 i = left_nritems - 1;
1986 item = btrfs_item_nr(left, i);
1988 if (!empty && push_items > 0) {
1989 if (path->slots[0] > i)
1991 if (path->slots[0] == i) {
1992 int space = btrfs_leaf_free_space(root, left);
1993 if (space + push_space * 2 > free_space)
1998 if (path->slots[0] == i)
1999 push_space += data_size + sizeof(*item);
2001 if (!left->map_token) {
2002 map_extent_buffer(left, (unsigned long)item,
2003 sizeof(struct btrfs_item),
2004 &left->map_token, &left->kaddr,
2005 &left->map_start, &left->map_len,
2009 this_item_size = btrfs_item_size(left, item);
2010 if (this_item_size + sizeof(*item) + push_space > free_space)
2014 push_space += this_item_size + sizeof(*item);
2019 if (left->map_token) {
2020 unmap_extent_buffer(left, left->map_token, KM_USER1);
2021 left->map_token = NULL;
2024 if (push_items == 0)
2027 if (!empty && push_items == left_nritems)
2030 /* push left to right */
2031 right_nritems = btrfs_header_nritems(right);
2033 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2034 push_space -= leaf_data_end(root, left);
2036 /* make room in the right data area */
2037 data_end = leaf_data_end(root, right);
2038 memmove_extent_buffer(right,
2039 btrfs_leaf_data(right) + data_end - push_space,
2040 btrfs_leaf_data(right) + data_end,
2041 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2043 /* copy from the left data area */
2044 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2045 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2046 btrfs_leaf_data(left) + leaf_data_end(root, left),
2049 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2050 btrfs_item_nr_offset(0),
2051 right_nritems * sizeof(struct btrfs_item));
2053 /* copy the items from left to right */
2054 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2055 btrfs_item_nr_offset(left_nritems - push_items),
2056 push_items * sizeof(struct btrfs_item));
2058 /* update the item pointers */
2059 right_nritems += push_items;
2060 btrfs_set_header_nritems(right, right_nritems);
2061 push_space = BTRFS_LEAF_DATA_SIZE(root);
2062 for (i = 0; i < right_nritems; i++) {
2063 item = btrfs_item_nr(right, i);
2064 if (!right->map_token) {
2065 map_extent_buffer(right, (unsigned long)item,
2066 sizeof(struct btrfs_item),
2067 &right->map_token, &right->kaddr,
2068 &right->map_start, &right->map_len,
2071 push_space -= btrfs_item_size(right, item);
2072 btrfs_set_item_offset(right, item, push_space);
2075 if (right->map_token) {
2076 unmap_extent_buffer(right, right->map_token, KM_USER1);
2077 right->map_token = NULL;
2079 left_nritems -= push_items;
2080 btrfs_set_header_nritems(left, left_nritems);
2083 btrfs_mark_buffer_dirty(left);
2084 btrfs_mark_buffer_dirty(right);
2086 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2089 btrfs_item_key(right, &disk_key, 0);
2090 btrfs_set_node_key(upper, &disk_key, slot + 1);
2091 btrfs_mark_buffer_dirty(upper);
2093 /* then fixup the leaf pointer in the path */
2094 if (path->slots[0] >= left_nritems) {
2095 path->slots[0] -= left_nritems;
2096 if (btrfs_header_nritems(path->nodes[0]) == 0)
2097 clean_tree_block(trans, root, path->nodes[0]);
2098 btrfs_tree_unlock(path->nodes[0]);
2099 free_extent_buffer(path->nodes[0]);
2100 path->nodes[0] = right;
2101 path->slots[1] += 1;
2103 btrfs_tree_unlock(right);
2104 free_extent_buffer(right);
2109 btrfs_tree_unlock(right);
2110 free_extent_buffer(right);
2115 * push some data in the path leaf to the left, trying to free up at
2116 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2118 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2119 *root, struct btrfs_path *path, int data_size,
2122 struct btrfs_disk_key disk_key;
2123 struct extent_buffer *right = path->nodes[0];
2124 struct extent_buffer *left;
2130 struct btrfs_item *item;
2131 u32 old_left_nritems;
2137 u32 old_left_item_size;
2139 slot = path->slots[1];
2142 if (!path->nodes[1])
2145 right_nritems = btrfs_header_nritems(right);
2146 if (right_nritems == 0) {
2150 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2152 left = read_node_slot(root, path->nodes[1], slot - 1);
2153 btrfs_tree_lock(left);
2154 free_space = btrfs_leaf_free_space(root, left);
2155 if (free_space < data_size + sizeof(struct btrfs_item)) {
2160 /* cow and double check */
2161 ret = btrfs_cow_block(trans, root, left,
2162 path->nodes[1], slot - 1, &left, 0);
2164 /* we hit -ENOSPC, but it isn't fatal here */
2169 free_space = btrfs_leaf_free_space(root, left);
2170 if (free_space < data_size + sizeof(struct btrfs_item)) {
2178 nr = right_nritems - 1;
2180 for (i = 0; i < nr; i++) {
2181 item = btrfs_item_nr(right, i);
2182 if (!right->map_token) {
2183 map_extent_buffer(right, (unsigned long)item,
2184 sizeof(struct btrfs_item),
2185 &right->map_token, &right->kaddr,
2186 &right->map_start, &right->map_len,
2190 if (!empty && push_items > 0) {
2191 if (path->slots[0] < i)
2193 if (path->slots[0] == i) {
2194 int space = btrfs_leaf_free_space(root, right);
2195 if (space + push_space * 2 > free_space)
2200 if (path->slots[0] == i)
2201 push_space += data_size + sizeof(*item);
2203 this_item_size = btrfs_item_size(right, item);
2204 if (this_item_size + sizeof(*item) + push_space > free_space)
2208 push_space += this_item_size + sizeof(*item);
2211 if (right->map_token) {
2212 unmap_extent_buffer(right, right->map_token, KM_USER1);
2213 right->map_token = NULL;
2216 if (push_items == 0) {
2220 if (!empty && push_items == btrfs_header_nritems(right))
2223 /* push data from right to left */
2224 copy_extent_buffer(left, right,
2225 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2226 btrfs_item_nr_offset(0),
2227 push_items * sizeof(struct btrfs_item));
2229 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2230 btrfs_item_offset_nr(right, push_items -1);
2232 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2233 leaf_data_end(root, left) - push_space,
2234 btrfs_leaf_data(right) +
2235 btrfs_item_offset_nr(right, push_items - 1),
2237 old_left_nritems = btrfs_header_nritems(left);
2238 BUG_ON(old_left_nritems < 0);
2240 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2241 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2244 item = btrfs_item_nr(left, i);
2245 if (!left->map_token) {
2246 map_extent_buffer(left, (unsigned long)item,
2247 sizeof(struct btrfs_item),
2248 &left->map_token, &left->kaddr,
2249 &left->map_start, &left->map_len,
2253 ioff = btrfs_item_offset(left, item);
2254 btrfs_set_item_offset(left, item,
2255 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2257 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2258 if (left->map_token) {
2259 unmap_extent_buffer(left, left->map_token, KM_USER1);
2260 left->map_token = NULL;
2263 /* fixup right node */
2264 if (push_items > right_nritems) {
2265 printk("push items %d nr %u\n", push_items, right_nritems);
2269 if (push_items < right_nritems) {
2270 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2271 leaf_data_end(root, right);
2272 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2273 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2274 btrfs_leaf_data(right) +
2275 leaf_data_end(root, right), push_space);
2277 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2278 btrfs_item_nr_offset(push_items),
2279 (btrfs_header_nritems(right) - push_items) *
2280 sizeof(struct btrfs_item));
2282 right_nritems -= push_items;
2283 btrfs_set_header_nritems(right, right_nritems);
2284 push_space = BTRFS_LEAF_DATA_SIZE(root);
2285 for (i = 0; i < right_nritems; i++) {
2286 item = btrfs_item_nr(right, i);
2288 if (!right->map_token) {
2289 map_extent_buffer(right, (unsigned long)item,
2290 sizeof(struct btrfs_item),
2291 &right->map_token, &right->kaddr,
2292 &right->map_start, &right->map_len,
2296 push_space = push_space - btrfs_item_size(right, item);
2297 btrfs_set_item_offset(right, item, push_space);
2299 if (right->map_token) {
2300 unmap_extent_buffer(right, right->map_token, KM_USER1);
2301 right->map_token = NULL;
2304 btrfs_mark_buffer_dirty(left);
2306 btrfs_mark_buffer_dirty(right);
2308 ret = btrfs_update_ref(trans, root, right, left,
2309 old_left_nritems, push_items);
2312 btrfs_item_key(right, &disk_key, 0);
2313 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2317 /* then fixup the leaf pointer in the path */
2318 if (path->slots[0] < push_items) {
2319 path->slots[0] += old_left_nritems;
2320 if (btrfs_header_nritems(path->nodes[0]) == 0)
2321 clean_tree_block(trans, root, path->nodes[0]);
2322 btrfs_tree_unlock(path->nodes[0]);
2323 free_extent_buffer(path->nodes[0]);
2324 path->nodes[0] = left;
2325 path->slots[1] -= 1;
2327 btrfs_tree_unlock(left);
2328 free_extent_buffer(left);
2329 path->slots[0] -= push_items;
2331 BUG_ON(path->slots[0] < 0);
2334 btrfs_tree_unlock(left);
2335 free_extent_buffer(left);
2340 * split the path's leaf in two, making sure there is at least data_size
2341 * available for the resulting leaf level of the path.
2343 * returns 0 if all went well and < 0 on failure.
2345 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2346 struct btrfs_root *root,
2347 struct btrfs_key *ins_key,
2348 struct btrfs_path *path, int data_size,
2351 struct extent_buffer *l;
2355 struct extent_buffer *right;
2356 int space_needed = data_size + sizeof(struct btrfs_item);
2363 int num_doubles = 0;
2364 struct btrfs_disk_key disk_key;
2367 space_needed = data_size;
2369 /* first try to make some room by pushing left and right */
2370 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
2371 wret = push_leaf_right(trans, root, path, data_size, 0);
2376 wret = push_leaf_left(trans, root, path, data_size, 0);
2382 /* did the pushes work? */
2383 if (btrfs_leaf_free_space(root, l) >= space_needed)
2387 if (!path->nodes[1]) {
2388 ret = insert_new_root(trans, root, path, 1);
2395 slot = path->slots[0];
2396 nritems = btrfs_header_nritems(l);
2397 mid = (nritems + 1)/ 2;
2399 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2400 path->nodes[1]->start,
2401 root->root_key.objectid,
2402 trans->transid, 0, l->start, 0);
2403 if (IS_ERR(right)) {
2405 return PTR_ERR(right);
2408 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2409 btrfs_set_header_bytenr(right, right->start);
2410 btrfs_set_header_generation(right, trans->transid);
2411 btrfs_set_header_owner(right, root->root_key.objectid);
2412 btrfs_set_header_level(right, 0);
2413 write_extent_buffer(right, root->fs_info->fsid,
2414 (unsigned long)btrfs_header_fsid(right),
2417 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2418 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2422 leaf_space_used(l, mid, nritems - mid) + space_needed >
2423 BTRFS_LEAF_DATA_SIZE(root)) {
2424 if (slot >= nritems) {
2425 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2426 btrfs_set_header_nritems(right, 0);
2427 wret = insert_ptr(trans, root, path,
2428 &disk_key, right->start,
2429 path->slots[1] + 1, 1);
2433 btrfs_tree_unlock(path->nodes[0]);
2434 free_extent_buffer(path->nodes[0]);
2435 path->nodes[0] = right;
2437 path->slots[1] += 1;
2438 btrfs_mark_buffer_dirty(right);
2442 if (mid != nritems &&
2443 leaf_space_used(l, mid, nritems - mid) +
2444 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2449 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2450 BTRFS_LEAF_DATA_SIZE(root)) {
2451 if (!extend && slot == 0) {
2452 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2453 btrfs_set_header_nritems(right, 0);
2454 wret = insert_ptr(trans, root, path,
2460 btrfs_tree_unlock(path->nodes[0]);
2461 free_extent_buffer(path->nodes[0]);
2462 path->nodes[0] = right;
2464 if (path->slots[1] == 0) {
2465 wret = fixup_low_keys(trans, root,
2466 path, &disk_key, 1);
2470 btrfs_mark_buffer_dirty(right);
2472 } else if (extend && slot == 0) {
2476 if (mid != nritems &&
2477 leaf_space_used(l, mid, nritems - mid) +
2478 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2484 nritems = nritems - mid;
2485 btrfs_set_header_nritems(right, nritems);
2486 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2488 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2489 btrfs_item_nr_offset(mid),
2490 nritems * sizeof(struct btrfs_item));
2492 copy_extent_buffer(right, l,
2493 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2494 data_copy_size, btrfs_leaf_data(l) +
2495 leaf_data_end(root, l), data_copy_size);
2497 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2498 btrfs_item_end_nr(l, mid);
2500 for (i = 0; i < nritems; i++) {
2501 struct btrfs_item *item = btrfs_item_nr(right, i);
2504 if (!right->map_token) {
2505 map_extent_buffer(right, (unsigned long)item,
2506 sizeof(struct btrfs_item),
2507 &right->map_token, &right->kaddr,
2508 &right->map_start, &right->map_len,
2512 ioff = btrfs_item_offset(right, item);
2513 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2516 if (right->map_token) {
2517 unmap_extent_buffer(right, right->map_token, KM_USER1);
2518 right->map_token = NULL;
2521 btrfs_set_header_nritems(l, mid);
2523 btrfs_item_key(right, &disk_key, 0);
2524 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2525 path->slots[1] + 1, 1);
2529 btrfs_mark_buffer_dirty(right);
2530 btrfs_mark_buffer_dirty(l);
2531 BUG_ON(path->slots[0] != slot);
2533 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2537 btrfs_tree_unlock(path->nodes[0]);
2538 free_extent_buffer(path->nodes[0]);
2539 path->nodes[0] = right;
2540 path->slots[0] -= mid;
2541 path->slots[1] += 1;
2543 btrfs_tree_unlock(right);
2544 free_extent_buffer(right);
2547 BUG_ON(path->slots[0] < 0);
2550 BUG_ON(num_doubles != 0);
2557 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2558 struct btrfs_root *root,
2559 struct btrfs_path *path,
2560 u32 new_size, int from_end)
2565 struct extent_buffer *leaf;
2566 struct btrfs_item *item;
2568 unsigned int data_end;
2569 unsigned int old_data_start;
2570 unsigned int old_size;
2571 unsigned int size_diff;
2574 slot_orig = path->slots[0];
2575 leaf = path->nodes[0];
2576 slot = path->slots[0];
2578 old_size = btrfs_item_size_nr(leaf, slot);
2579 if (old_size == new_size)
2582 nritems = btrfs_header_nritems(leaf);
2583 data_end = leaf_data_end(root, leaf);
2585 old_data_start = btrfs_item_offset_nr(leaf, slot);
2587 size_diff = old_size - new_size;
2590 BUG_ON(slot >= nritems);
2593 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2595 /* first correct the data pointers */
2596 for (i = slot; i < nritems; i++) {
2598 item = btrfs_item_nr(leaf, i);
2600 if (!leaf->map_token) {
2601 map_extent_buffer(leaf, (unsigned long)item,
2602 sizeof(struct btrfs_item),
2603 &leaf->map_token, &leaf->kaddr,
2604 &leaf->map_start, &leaf->map_len,
2608 ioff = btrfs_item_offset(leaf, item);
2609 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2612 if (leaf->map_token) {
2613 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2614 leaf->map_token = NULL;
2617 /* shift the data */
2619 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2620 data_end + size_diff, btrfs_leaf_data(leaf) +
2621 data_end, old_data_start + new_size - data_end);
2623 struct btrfs_disk_key disk_key;
2626 btrfs_item_key(leaf, &disk_key, slot);
2628 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2630 struct btrfs_file_extent_item *fi;
2632 fi = btrfs_item_ptr(leaf, slot,
2633 struct btrfs_file_extent_item);
2634 fi = (struct btrfs_file_extent_item *)(
2635 (unsigned long)fi - size_diff);
2637 if (btrfs_file_extent_type(leaf, fi) ==
2638 BTRFS_FILE_EXTENT_INLINE) {
2639 ptr = btrfs_item_ptr_offset(leaf, slot);
2640 memmove_extent_buffer(leaf, ptr,
2642 offsetof(struct btrfs_file_extent_item,
2647 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2648 data_end + size_diff, btrfs_leaf_data(leaf) +
2649 data_end, old_data_start - data_end);
2651 offset = btrfs_disk_key_offset(&disk_key);
2652 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2653 btrfs_set_item_key(leaf, &disk_key, slot);
2655 fixup_low_keys(trans, root, path, &disk_key, 1);
2658 item = btrfs_item_nr(leaf, slot);
2659 btrfs_set_item_size(leaf, item, new_size);
2660 btrfs_mark_buffer_dirty(leaf);
2663 if (btrfs_leaf_free_space(root, leaf) < 0) {
2664 btrfs_print_leaf(root, leaf);
2670 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2671 struct btrfs_root *root, struct btrfs_path *path,
2677 struct extent_buffer *leaf;
2678 struct btrfs_item *item;
2680 unsigned int data_end;
2681 unsigned int old_data;
2682 unsigned int old_size;
2685 slot_orig = path->slots[0];
2686 leaf = path->nodes[0];
2688 nritems = btrfs_header_nritems(leaf);
2689 data_end = leaf_data_end(root, leaf);
2691 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2692 btrfs_print_leaf(root, leaf);
2695 slot = path->slots[0];
2696 old_data = btrfs_item_end_nr(leaf, slot);
2699 if (slot >= nritems) {
2700 btrfs_print_leaf(root, leaf);
2701 printk("slot %d too large, nritems %d\n", slot, nritems);
2706 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2708 /* first correct the data pointers */
2709 for (i = slot; i < nritems; i++) {
2711 item = btrfs_item_nr(leaf, i);
2713 if (!leaf->map_token) {
2714 map_extent_buffer(leaf, (unsigned long)item,
2715 sizeof(struct btrfs_item),
2716 &leaf->map_token, &leaf->kaddr,
2717 &leaf->map_start, &leaf->map_len,
2720 ioff = btrfs_item_offset(leaf, item);
2721 btrfs_set_item_offset(leaf, item, ioff - data_size);
2724 if (leaf->map_token) {
2725 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2726 leaf->map_token = NULL;
2729 /* shift the data */
2730 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2731 data_end - data_size, btrfs_leaf_data(leaf) +
2732 data_end, old_data - data_end);
2734 data_end = old_data;
2735 old_size = btrfs_item_size_nr(leaf, slot);
2736 item = btrfs_item_nr(leaf, slot);
2737 btrfs_set_item_size(leaf, item, old_size + data_size);
2738 btrfs_mark_buffer_dirty(leaf);
2741 if (btrfs_leaf_free_space(root, leaf) < 0) {
2742 btrfs_print_leaf(root, leaf);
2749 * Given a key and some data, insert an item into the tree.
2750 * This does all the path init required, making room in the tree if needed.
2752 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2753 struct btrfs_root *root,
2754 struct btrfs_path *path,
2755 struct btrfs_key *cpu_key, u32 *data_size,
2758 struct extent_buffer *leaf;
2759 struct btrfs_item *item;
2767 unsigned int data_end;
2768 struct btrfs_disk_key disk_key;
2770 for (i = 0; i < nr; i++) {
2771 total_data += data_size[i];
2774 total_size = total_data + (nr * sizeof(struct btrfs_item));
2775 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2781 slot_orig = path->slots[0];
2782 leaf = path->nodes[0];
2784 nritems = btrfs_header_nritems(leaf);
2785 data_end = leaf_data_end(root, leaf);
2787 if (btrfs_leaf_free_space(root, leaf) < total_size) {
2788 btrfs_print_leaf(root, leaf);
2789 printk("not enough freespace need %u have %d\n",
2790 total_size, btrfs_leaf_free_space(root, leaf));
2794 slot = path->slots[0];
2797 if (slot != nritems) {
2798 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2800 if (old_data < data_end) {
2801 btrfs_print_leaf(root, leaf);
2802 printk("slot %d old_data %d data_end %d\n",
2803 slot, old_data, data_end);
2807 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2809 /* first correct the data pointers */
2810 WARN_ON(leaf->map_token);
2811 for (i = slot; i < nritems; i++) {
2814 item = btrfs_item_nr(leaf, i);
2815 if (!leaf->map_token) {
2816 map_extent_buffer(leaf, (unsigned long)item,
2817 sizeof(struct btrfs_item),
2818 &leaf->map_token, &leaf->kaddr,
2819 &leaf->map_start, &leaf->map_len,
2823 ioff = btrfs_item_offset(leaf, item);
2824 btrfs_set_item_offset(leaf, item, ioff - total_data);
2826 if (leaf->map_token) {
2827 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2828 leaf->map_token = NULL;
2831 /* shift the items */
2832 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2833 btrfs_item_nr_offset(slot),
2834 (nritems - slot) * sizeof(struct btrfs_item));
2836 /* shift the data */
2837 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2838 data_end - total_data, btrfs_leaf_data(leaf) +
2839 data_end, old_data - data_end);
2840 data_end = old_data;
2843 /* setup the item for the new data */
2844 for (i = 0; i < nr; i++) {
2845 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2846 btrfs_set_item_key(leaf, &disk_key, slot + i);
2847 item = btrfs_item_nr(leaf, slot + i);
2848 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2849 data_end -= data_size[i];
2850 btrfs_set_item_size(leaf, item, data_size[i]);
2852 btrfs_set_header_nritems(leaf, nritems + nr);
2853 btrfs_mark_buffer_dirty(leaf);
2857 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2858 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2861 if (btrfs_leaf_free_space(root, leaf) < 0) {
2862 btrfs_print_leaf(root, leaf);
2870 * Given a key and some data, insert an item into the tree.
2871 * This does all the path init required, making room in the tree if needed.
2873 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2874 *root, struct btrfs_key *cpu_key, void *data, u32
2878 struct btrfs_path *path;
2879 struct extent_buffer *leaf;
2882 path = btrfs_alloc_path();
2884 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2886 leaf = path->nodes[0];
2887 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2888 write_extent_buffer(leaf, data, ptr, data_size);
2889 btrfs_mark_buffer_dirty(leaf);
2891 btrfs_free_path(path);
2896 * delete the pointer from a given node.
2898 * If the delete empties a node, the node is removed from the tree,
2899 * continuing all the way the root if required. The root is converted into
2900 * a leaf if all the nodes are emptied.
2902 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2903 struct btrfs_path *path, int level, int slot)
2905 struct extent_buffer *parent = path->nodes[level];
2910 nritems = btrfs_header_nritems(parent);
2911 if (slot != nritems -1) {
2912 memmove_extent_buffer(parent,
2913 btrfs_node_key_ptr_offset(slot),
2914 btrfs_node_key_ptr_offset(slot + 1),
2915 sizeof(struct btrfs_key_ptr) *
2916 (nritems - slot - 1));
2919 btrfs_set_header_nritems(parent, nritems);
2920 if (nritems == 0 && parent == root->node) {
2921 BUG_ON(btrfs_header_level(root->node) != 1);
2922 /* just turn the root into a leaf and break */
2923 btrfs_set_header_level(root->node, 0);
2924 } else if (slot == 0) {
2925 struct btrfs_disk_key disk_key;
2927 btrfs_node_key(parent, &disk_key, 0);
2928 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2932 btrfs_mark_buffer_dirty(parent);
2937 * delete the item at the leaf level in path. If that empties
2938 * the leaf, remove it from the tree
2940 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2941 struct btrfs_path *path, int slot, int nr)
2943 struct extent_buffer *leaf;
2944 struct btrfs_item *item;
2952 leaf = path->nodes[0];
2953 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2955 for (i = 0; i < nr; i++)
2956 dsize += btrfs_item_size_nr(leaf, slot + i);
2958 nritems = btrfs_header_nritems(leaf);
2960 if (slot + nr != nritems) {
2961 int data_end = leaf_data_end(root, leaf);
2963 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2965 btrfs_leaf_data(leaf) + data_end,
2966 last_off - data_end);
2968 for (i = slot + nr; i < nritems; i++) {
2971 item = btrfs_item_nr(leaf, i);
2972 if (!leaf->map_token) {
2973 map_extent_buffer(leaf, (unsigned long)item,
2974 sizeof(struct btrfs_item),
2975 &leaf->map_token, &leaf->kaddr,
2976 &leaf->map_start, &leaf->map_len,
2979 ioff = btrfs_item_offset(leaf, item);
2980 btrfs_set_item_offset(leaf, item, ioff + dsize);
2983 if (leaf->map_token) {
2984 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2985 leaf->map_token = NULL;
2988 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2989 btrfs_item_nr_offset(slot + nr),
2990 sizeof(struct btrfs_item) *
2991 (nritems - slot - nr));
2993 btrfs_set_header_nritems(leaf, nritems - nr);
2996 /* delete the leaf if we've emptied it */
2998 if (leaf == root->node) {
2999 btrfs_set_header_level(leaf, 0);
3001 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3002 wret = del_ptr(trans, root, path, 1, path->slots[1]);
3005 wret = btrfs_free_extent(trans, root,
3006 leaf->start, leaf->len,
3007 path->nodes[1]->start,
3008 btrfs_header_owner(path->nodes[1]),
3014 int used = leaf_space_used(leaf, 0, nritems);
3016 struct btrfs_disk_key disk_key;
3018 btrfs_item_key(leaf, &disk_key, 0);
3019 wret = fixup_low_keys(trans, root, path,
3025 /* delete the leaf if it is mostly empty */
3026 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
3027 /* push_leaf_left fixes the path.
3028 * make sure the path still points to our leaf
3029 * for possible call to del_ptr below
3031 slot = path->slots[1];
3032 extent_buffer_get(leaf);
3034 wret = push_leaf_left(trans, root, path, 1, 1);
3035 if (wret < 0 && wret != -ENOSPC)
3038 if (path->nodes[0] == leaf &&
3039 btrfs_header_nritems(leaf)) {
3040 wret = push_leaf_right(trans, root, path, 1, 1);
3041 if (wret < 0 && wret != -ENOSPC)
3045 if (btrfs_header_nritems(leaf) == 0) {
3047 u64 bytenr = leaf->start;
3048 u32 blocksize = leaf->len;
3050 root_gen = btrfs_header_generation(
3053 wret = del_ptr(trans, root, path, 1, slot);
3057 free_extent_buffer(leaf);
3058 wret = btrfs_free_extent(trans, root, bytenr,
3059 blocksize, path->nodes[1]->start,
3060 btrfs_header_owner(path->nodes[1]),
3065 /* if we're still in the path, make sure
3066 * we're dirty. Otherwise, one of the
3067 * push_leaf functions must have already
3068 * dirtied this buffer
3070 if (path->nodes[0] == leaf)
3071 btrfs_mark_buffer_dirty(leaf);
3072 free_extent_buffer(leaf);
3075 btrfs_mark_buffer_dirty(leaf);
3082 * search the tree again to find a leaf with lesser keys
3083 * returns 0 if it found something or 1 if there are no lesser leaves.
3084 * returns < 0 on io errors.
3086 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3088 struct btrfs_key key;
3089 struct btrfs_disk_key found_key;
3092 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3096 else if (key.type > 0)
3098 else if (key.objectid > 0)
3103 btrfs_release_path(root, path);
3104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3107 btrfs_item_key(path->nodes[0], &found_key, 0);
3108 ret = comp_keys(&found_key, &key);
3115 * A helper function to walk down the tree starting at min_key, and looking
3116 * for nodes or leaves that are either in cache or have a minimum
3117 * transaction id. This is used by the btree defrag code, but could
3118 * also be used to search for blocks that have changed since a given
3121 * This does not cow, but it does stuff the starting key it finds back
3122 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3123 * key and get a writable path.
3125 * This does lock as it descends, and path->keep_locks should be set
3126 * to 1 by the caller.
3128 * This honors path->lowest_level to prevent descent past a given level
3131 * returns zero if something useful was found, < 0 on error and 1 if there
3132 * was nothing in the tree that matched the search criteria.
3134 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3135 struct btrfs_key *max_key,
3136 struct btrfs_path *path, int cache_only,
3139 struct extent_buffer *cur;
3140 struct btrfs_key found_key;
3148 cur = btrfs_lock_root_node(root);
3149 level = btrfs_header_level(cur);
3150 WARN_ON(path->nodes[level]);
3151 path->nodes[level] = cur;
3152 path->locks[level] = 1;
3154 if (btrfs_header_generation(cur) < min_trans) {
3159 nritems = btrfs_header_nritems(cur);
3160 level = btrfs_header_level(cur);
3161 sret = bin_search(cur, min_key, level, &slot);
3163 /* at level = 0, we're done, setup the path and exit */
3165 if (slot >= nritems)
3168 path->slots[level] = slot;
3169 btrfs_item_key_to_cpu(cur, &found_key, slot);
3172 if (sret && slot > 0)
3175 * check this node pointer against the cache_only and
3176 * min_trans parameters. If it isn't in cache or is too
3177 * old, skip to the next one.
3179 while(slot < nritems) {
3182 struct extent_buffer *tmp;
3183 struct btrfs_disk_key disk_key;
3185 blockptr = btrfs_node_blockptr(cur, slot);
3186 gen = btrfs_node_ptr_generation(cur, slot);
3187 if (gen < min_trans) {
3195 btrfs_node_key(cur, &disk_key, slot);
3196 if (comp_keys(&disk_key, max_key) >= 0) {
3202 tmp = btrfs_find_tree_block(root, blockptr,
3203 btrfs_level_size(root, level - 1));
3205 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3206 free_extent_buffer(tmp);
3210 free_extent_buffer(tmp);
3215 * we didn't find a candidate key in this node, walk forward
3216 * and find another one
3218 if (slot >= nritems) {
3219 path->slots[level] = slot;
3220 sret = btrfs_find_next_key(root, path, min_key, level,
3221 cache_only, min_trans);
3223 btrfs_release_path(root, path);
3229 /* save our key for returning back */
3230 btrfs_node_key_to_cpu(cur, &found_key, slot);
3231 path->slots[level] = slot;
3232 if (level == path->lowest_level) {
3234 unlock_up(path, level, 1);
3237 cur = read_node_slot(root, cur, slot);
3239 btrfs_tree_lock(cur);
3240 path->locks[level - 1] = 1;
3241 path->nodes[level - 1] = cur;
3242 unlock_up(path, level, 1);
3246 memcpy(min_key, &found_key, sizeof(found_key));
3251 * this is similar to btrfs_next_leaf, but does not try to preserve
3252 * and fixup the path. It looks for and returns the next key in the
3253 * tree based on the current path and the cache_only and min_trans
3256 * 0 is returned if another key is found, < 0 if there are any errors
3257 * and 1 is returned if there are no higher keys in the tree
3259 * path->keep_locks should be set to 1 on the search made before
3260 * calling this function.
3262 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
3263 struct btrfs_key *key, int lowest_level,
3264 int cache_only, u64 min_trans)
3266 int level = lowest_level;
3268 struct extent_buffer *c;
3270 while(level < BTRFS_MAX_LEVEL) {
3271 if (!path->nodes[level])
3274 slot = path->slots[level] + 1;
3275 c = path->nodes[level];
3277 if (slot >= btrfs_header_nritems(c)) {
3279 if (level == BTRFS_MAX_LEVEL) {
3285 btrfs_item_key_to_cpu(c, key, slot);
3287 u64 blockptr = btrfs_node_blockptr(c, slot);
3288 u64 gen = btrfs_node_ptr_generation(c, slot);
3291 struct extent_buffer *cur;
3292 cur = btrfs_find_tree_block(root, blockptr,
3293 btrfs_level_size(root, level - 1));
3294 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3297 free_extent_buffer(cur);
3300 free_extent_buffer(cur);
3302 if (gen < min_trans) {
3306 btrfs_node_key_to_cpu(c, key, slot);
3314 * search the tree again to find a leaf with greater keys
3315 * returns 0 if it found something or 1 if there are no greater leaves.
3316 * returns < 0 on io errors.
3318 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3322 struct extent_buffer *c;
3323 struct extent_buffer *next = NULL;
3324 struct btrfs_key key;
3328 nritems = btrfs_header_nritems(path->nodes[0]);
3333 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3335 btrfs_release_path(root, path);
3336 path->keep_locks = 1;
3337 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3338 path->keep_locks = 0;
3343 nritems = btrfs_header_nritems(path->nodes[0]);
3345 * by releasing the path above we dropped all our locks. A balance
3346 * could have added more items next to the key that used to be
3347 * at the very end of the block. So, check again here and
3348 * advance the path if there are now more items available.
3350 if (nritems > 0 && path->slots[0] < nritems - 1) {
3355 while(level < BTRFS_MAX_LEVEL) {
3356 if (!path->nodes[level])
3359 slot = path->slots[level] + 1;
3360 c = path->nodes[level];
3361 if (slot >= btrfs_header_nritems(c)) {
3363 if (level == BTRFS_MAX_LEVEL) {
3370 btrfs_tree_unlock(next);
3371 free_extent_buffer(next);
3374 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3376 reada_for_search(root, path, level, slot, 0);
3378 next = read_node_slot(root, c, slot);
3379 if (!path->skip_locking) {
3380 WARN_ON(!btrfs_tree_locked(c));
3381 btrfs_tree_lock(next);
3385 path->slots[level] = slot;
3388 c = path->nodes[level];
3389 if (path->locks[level])
3390 btrfs_tree_unlock(c);
3391 free_extent_buffer(c);
3392 path->nodes[level] = next;
3393 path->slots[level] = 0;
3394 if (!path->skip_locking)
3395 path->locks[level] = 1;
3398 if (level == 1 && path->locks[1] && path->reada)
3399 reada_for_search(root, path, level, slot, 0);
3400 next = read_node_slot(root, next, 0);
3401 if (!path->skip_locking) {
3402 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3403 btrfs_tree_lock(next);
3407 unlock_up(path, 0, 1);
3412 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3413 * searching until it gets past min_objectid or finds an item of 'type'
3415 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3417 int btrfs_previous_item(struct btrfs_root *root,
3418 struct btrfs_path *path, u64 min_objectid,
3421 struct btrfs_key found_key;
3422 struct extent_buffer *leaf;
3427 if (path->slots[0] == 0) {
3428 ret = btrfs_prev_leaf(root, path);
3434 leaf = path->nodes[0];
3435 nritems = btrfs_header_nritems(leaf);
3438 if (path->slots[0] == nritems)
3441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3442 if (found_key.type == type)
3444 if (found_key.objectid < min_objectid)
3446 if (found_key.objectid == min_objectid &&
3447 found_key.type < type)