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"
25 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_path *path, int level);
27 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_key *ins_key,
29 struct btrfs_path *path, int data_size, int extend);
30 static int push_node_left(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root, struct extent_buffer *dst,
32 struct extent_buffer *src);
33 static int balance_node_right(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root,
35 struct extent_buffer *dst_buf,
36 struct extent_buffer *src_buf);
37 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
38 struct btrfs_path *path, int level, int slot);
40 inline void btrfs_init_path(struct btrfs_path *p)
42 memset(p, 0, sizeof(*p));
45 struct btrfs_path *btrfs_alloc_path(void)
47 struct btrfs_path *path;
48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
50 btrfs_init_path(path);
56 void btrfs_free_path(struct btrfs_path *p)
58 btrfs_release_path(NULL, p);
59 kmem_cache_free(btrfs_path_cachep, p);
62 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
65 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
68 free_extent_buffer(p->nodes[i]);
70 memset(p, 0, sizeof(*p));
73 static int __btrfs_cow_block(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 struct extent_buffer *buf,
76 struct extent_buffer *parent, int parent_slot,
77 struct extent_buffer **cow_ret,
78 u64 search_start, u64 empty_size)
80 struct extent_buffer *cow;
82 int different_trans = 0;
84 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
86 cow = btrfs_alloc_free_block(trans, root, buf->len,
87 search_start, empty_size);
91 copy_extent_buffer(cow, buf, 0, 0, cow->len);
92 btrfs_set_header_bytenr(cow, cow->start);
93 btrfs_set_header_generation(cow, trans->transid);
94 btrfs_set_header_owner(cow, root->root_key.objectid);
96 WARN_ON(btrfs_header_generation(buf) > trans->transid);
97 if (btrfs_header_generation(buf) != trans->transid) {
99 ret = btrfs_inc_ref(trans, root, buf);
103 clean_tree_block(trans, root, buf);
106 if (buf == root->node) {
108 extent_buffer_get(cow);
109 if (buf != root->commit_root) {
110 btrfs_free_extent(trans, root, buf->start,
113 free_extent_buffer(buf);
115 btrfs_set_node_blockptr(parent, parent_slot,
117 btrfs_mark_buffer_dirty(parent);
118 WARN_ON(btrfs_header_generation(parent) != trans->transid);
119 btrfs_free_extent(trans, root, buf->start, buf->len, 1);
121 free_extent_buffer(buf);
122 btrfs_mark_buffer_dirty(cow);
127 int btrfs_cow_block(struct btrfs_trans_handle *trans,
128 struct btrfs_root *root, struct extent_buffer *buf,
129 struct extent_buffer *parent, int parent_slot,
130 struct extent_buffer **cow_ret)
134 if (trans->transaction != root->fs_info->running_transaction) {
135 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
136 root->fs_info->running_transaction->transid);
139 if (trans->transid != root->fs_info->generation) {
140 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
141 root->fs_info->generation);
144 if (btrfs_header_generation(buf) == trans->transid) {
149 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
150 ret = __btrfs_cow_block(trans, root, buf, parent,
151 parent_slot, cow_ret, search_start, 0);
155 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
157 if (blocknr < other && other - (blocknr + blocksize) < 32768)
159 if (blocknr > other && blocknr - (other + blocksize) < 32768)
165 * compare two keys in a memcmp fashion
167 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
171 btrfs_disk_key_to_cpu(&k1, disk);
173 if (k1.objectid > k2->objectid)
175 if (k1.objectid < k2->objectid)
177 if (k1.type > k2->type)
179 if (k1.type < k2->type)
181 if (k1.offset > k2->offset)
183 if (k1.offset < k2->offset)
189 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root, struct extent_buffer *parent,
191 int start_slot, int cache_only, u64 *last_ret,
192 struct btrfs_key *progress)
194 struct extent_buffer *cur;
195 struct extent_buffer *tmp;
197 u64 search_start = *last_ret;
207 int progress_passed = 0;
208 struct btrfs_disk_key disk_key;
210 parent_level = btrfs_header_level(parent);
211 if (cache_only && parent_level != 1)
214 if (trans->transaction != root->fs_info->running_transaction) {
215 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
216 root->fs_info->running_transaction->transid);
219 if (trans->transid != root->fs_info->generation) {
220 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
221 root->fs_info->generation);
225 parent_nritems = btrfs_header_nritems(parent);
226 blocksize = btrfs_level_size(root, parent_level - 1);
227 end_slot = parent_nritems;
229 if (parent_nritems == 1)
232 for (i = start_slot; i < end_slot; i++) {
235 if (!parent->map_token) {
236 map_extent_buffer(parent,
237 btrfs_node_key_ptr_offset(i),
238 sizeof(struct btrfs_key_ptr),
239 &parent->map_token, &parent->kaddr,
240 &parent->map_start, &parent->map_len,
243 btrfs_node_key(parent, &disk_key, i);
244 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
248 blocknr = btrfs_node_blockptr(parent, i);
250 last_block = blocknr;
253 other = btrfs_node_blockptr(parent, i - 1);
254 close = close_blocks(blocknr, other, blocksize);
256 if (close && i < end_slot - 2) {
257 other = btrfs_node_blockptr(parent, i + 1);
258 close = close_blocks(blocknr, other, blocksize);
261 last_block = blocknr;
264 if (parent->map_token) {
265 unmap_extent_buffer(parent, parent->map_token,
267 parent->map_token = NULL;
270 cur = btrfs_find_tree_block(root, blocknr, blocksize);
272 uptodate = btrfs_buffer_uptodate(cur);
275 if (!cur || !uptodate) {
277 free_extent_buffer(cur);
281 cur = read_tree_block(root, blocknr,
283 } else if (!uptodate) {
284 btrfs_read_buffer(cur);
287 if (search_start == 0)
288 search_start = last_block;
290 err = __btrfs_cow_block(trans, root, cur, parent, i,
293 (end_slot - i) * blocksize));
295 free_extent_buffer(cur);
298 search_start = tmp->start;
299 last_block = tmp->start;
300 *last_ret = search_start;
301 if (parent_level == 1)
302 btrfs_clear_buffer_defrag(tmp);
303 free_extent_buffer(tmp);
305 if (parent->map_token) {
306 unmap_extent_buffer(parent, parent->map_token,
308 parent->map_token = NULL;
314 * The leaf data grows from end-to-front in the node.
315 * this returns the address of the start of the last item,
316 * which is the stop of the leaf data stack
318 static inline unsigned int leaf_data_end(struct btrfs_root *root,
319 struct extent_buffer *leaf)
321 u32 nr = btrfs_header_nritems(leaf);
323 return BTRFS_LEAF_DATA_SIZE(root);
324 return btrfs_item_offset_nr(leaf, nr - 1);
327 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
330 struct extent_buffer *parent = NULL;
331 struct extent_buffer *node = path->nodes[level];
332 struct btrfs_disk_key parent_key;
333 struct btrfs_disk_key node_key;
336 struct btrfs_key cpukey;
337 u32 nritems = btrfs_header_nritems(node);
339 if (path->nodes[level + 1])
340 parent = path->nodes[level + 1];
342 slot = path->slots[level];
343 BUG_ON(nritems == 0);
345 parent_slot = path->slots[level + 1];
346 btrfs_node_key(parent, &parent_key, parent_slot);
347 btrfs_node_key(node, &node_key, 0);
348 BUG_ON(memcmp(&parent_key, &node_key,
349 sizeof(struct btrfs_disk_key)));
350 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
351 btrfs_header_bytenr(node));
353 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
355 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
356 btrfs_node_key(node, &node_key, slot);
357 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
359 if (slot < nritems - 1) {
360 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
361 btrfs_node_key(node, &node_key, slot);
362 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
367 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
370 struct extent_buffer *leaf = path->nodes[level];
371 struct extent_buffer *parent = NULL;
373 struct btrfs_key cpukey;
374 struct btrfs_disk_key parent_key;
375 struct btrfs_disk_key leaf_key;
376 int slot = path->slots[0];
378 u32 nritems = btrfs_header_nritems(leaf);
380 if (path->nodes[level + 1])
381 parent = path->nodes[level + 1];
387 parent_slot = path->slots[level + 1];
388 btrfs_node_key(parent, &parent_key, parent_slot);
389 btrfs_item_key(leaf, &leaf_key, 0);
391 BUG_ON(memcmp(&parent_key, &leaf_key,
392 sizeof(struct btrfs_disk_key)));
393 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
394 btrfs_header_bytenr(leaf));
397 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
398 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
399 btrfs_item_key(leaf, &leaf_key, i);
400 if (comp_keys(&leaf_key, &cpukey) >= 0) {
401 btrfs_print_leaf(root, leaf);
402 printk("slot %d offset bad key\n", i);
405 if (btrfs_item_offset_nr(leaf, i) !=
406 btrfs_item_end_nr(leaf, i + 1)) {
407 btrfs_print_leaf(root, leaf);
408 printk("slot %d offset bad\n", i);
412 if (btrfs_item_offset_nr(leaf, i) +
413 btrfs_item_size_nr(leaf, i) !=
414 BTRFS_LEAF_DATA_SIZE(root)) {
415 btrfs_print_leaf(root, leaf);
416 printk("slot %d first offset bad\n", i);
422 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
423 btrfs_print_leaf(root, leaf);
424 printk("slot %d bad size \n", nritems - 1);
429 if (slot != 0 && slot < nritems - 1) {
430 btrfs_item_key(leaf, &leaf_key, slot);
431 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
432 if (comp_keys(&leaf_key, &cpukey) <= 0) {
433 btrfs_print_leaf(root, leaf);
434 printk("slot %d offset bad key\n", slot);
437 if (btrfs_item_offset_nr(leaf, slot - 1) !=
438 btrfs_item_end_nr(leaf, slot)) {
439 btrfs_print_leaf(root, leaf);
440 printk("slot %d offset bad\n", slot);
444 if (slot < nritems - 1) {
445 btrfs_item_key(leaf, &leaf_key, slot);
446 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
447 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
448 if (btrfs_item_offset_nr(leaf, slot) !=
449 btrfs_item_end_nr(leaf, slot + 1)) {
450 btrfs_print_leaf(root, leaf);
451 printk("slot %d offset bad\n", slot);
455 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
456 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
460 static int check_block(struct btrfs_root *root, struct btrfs_path *path,
465 struct extent_buffer *buf = path->nodes[level];
467 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
468 (unsigned long)btrfs_header_fsid(buf),
470 printk("warning bad block %Lu\n", buf->start);
475 return check_leaf(root, path, level);
476 return check_node(root, path, level);
480 * search for key in the extent_buffer. The items start at offset p,
481 * and they are item_size apart. There are 'max' items in p.
483 * the slot in the array is returned via slot, and it points to
484 * the place where you would insert key if it is not found in
487 * slot may point to max if the key is bigger than all of the keys
489 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
490 int item_size, struct btrfs_key *key,
497 struct btrfs_disk_key *tmp = NULL;
498 struct btrfs_disk_key unaligned;
499 unsigned long offset;
500 char *map_token = NULL;
502 unsigned long map_start = 0;
503 unsigned long map_len = 0;
507 mid = (low + high) / 2;
508 offset = p + mid * item_size;
510 if (!map_token || offset < map_start ||
511 (offset + sizeof(struct btrfs_disk_key)) >
512 map_start + map_len) {
514 unmap_extent_buffer(eb, map_token, KM_USER0);
517 err = map_extent_buffer(eb, offset,
518 sizeof(struct btrfs_disk_key),
520 &map_start, &map_len, KM_USER0);
523 tmp = (struct btrfs_disk_key *)(kaddr + offset -
526 read_extent_buffer(eb, &unaligned,
527 offset, sizeof(unaligned));
532 tmp = (struct btrfs_disk_key *)(kaddr + offset -
535 ret = comp_keys(tmp, key);
544 unmap_extent_buffer(eb, map_token, KM_USER0);
550 unmap_extent_buffer(eb, map_token, KM_USER0);
555 * simple bin_search frontend that does the right thing for
558 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
559 int level, int *slot)
562 return generic_bin_search(eb,
563 offsetof(struct btrfs_leaf, items),
564 sizeof(struct btrfs_item),
565 key, btrfs_header_nritems(eb),
568 return generic_bin_search(eb,
569 offsetof(struct btrfs_node, ptrs),
570 sizeof(struct btrfs_key_ptr),
571 key, btrfs_header_nritems(eb),
577 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
578 struct extent_buffer *parent, int slot)
582 if (slot >= btrfs_header_nritems(parent))
584 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
585 btrfs_level_size(root, btrfs_header_level(parent) - 1));
588 static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
589 *root, struct btrfs_path *path, int level)
591 struct extent_buffer *right = NULL;
592 struct extent_buffer *mid;
593 struct extent_buffer *left = NULL;
594 struct extent_buffer *parent = NULL;
598 int orig_slot = path->slots[level];
599 int err_on_enospc = 0;
605 mid = path->nodes[level];
606 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
608 if (level < BTRFS_MAX_LEVEL - 1)
609 parent = path->nodes[level + 1];
610 pslot = path->slots[level + 1];
613 * deal with the case where there is only one pointer in the root
614 * by promoting the node below to a root
617 struct extent_buffer *child;
619 if (btrfs_header_nritems(mid) != 1)
622 /* promote the child to a root */
623 child = read_node_slot(root, mid, 0);
626 path->nodes[level] = NULL;
627 clean_tree_block(trans, root, mid);
628 wait_on_tree_block_writeback(root, mid);
629 /* once for the path */
630 free_extent_buffer(mid);
631 ret = btrfs_free_extent(trans, root, mid->start, mid->len, 1);
632 /* once for the root ptr */
633 free_extent_buffer(mid);
636 if (btrfs_header_nritems(mid) >
637 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
640 if (btrfs_header_nritems(mid) < 2)
643 left = read_node_slot(root, parent, pslot - 1);
645 wret = btrfs_cow_block(trans, root, left,
646 parent, pslot - 1, &left);
652 right = read_node_slot(root, parent, pslot + 1);
654 wret = btrfs_cow_block(trans, root, right,
655 parent, pslot + 1, &right);
662 /* first, try to make some room in the middle buffer */
664 orig_slot += btrfs_header_nritems(left);
665 wret = push_node_left(trans, root, left, mid);
668 if (btrfs_header_nritems(mid) < 2)
673 * then try to empty the right most buffer into the middle
676 wret = push_node_left(trans, root, mid, right);
677 if (wret < 0 && wret != -ENOSPC)
679 if (btrfs_header_nritems(right) == 0) {
680 u64 bytenr = right->start;
681 u32 blocksize = right->len;
683 clean_tree_block(trans, root, right);
684 wait_on_tree_block_writeback(root, right);
685 free_extent_buffer(right);
687 wret = del_ptr(trans, root, path, level + 1, pslot +
691 wret = btrfs_free_extent(trans, root, bytenr,
696 struct btrfs_disk_key right_key;
697 btrfs_node_key(right, &right_key, 0);
698 btrfs_set_node_key(parent, &right_key, pslot + 1);
699 btrfs_mark_buffer_dirty(parent);
702 if (btrfs_header_nritems(mid) == 1) {
704 * we're not allowed to leave a node with one item in the
705 * tree during a delete. A deletion from lower in the tree
706 * could try to delete the only pointer in this node.
707 * So, pull some keys from the left.
708 * There has to be a left pointer at this point because
709 * otherwise we would have pulled some pointers from the
713 wret = balance_node_right(trans, root, mid, left);
720 if (btrfs_header_nritems(mid) == 0) {
721 /* we've managed to empty the middle node, drop it */
722 u64 bytenr = mid->start;
723 u32 blocksize = mid->len;
724 clean_tree_block(trans, root, mid);
725 wait_on_tree_block_writeback(root, mid);
726 free_extent_buffer(mid);
728 wret = del_ptr(trans, root, path, level + 1, pslot);
731 wret = btrfs_free_extent(trans, root, bytenr, blocksize, 1);
735 /* update the parent key to reflect our changes */
736 struct btrfs_disk_key mid_key;
737 btrfs_node_key(mid, &mid_key, 0);
738 btrfs_set_node_key(parent, &mid_key, pslot);
739 btrfs_mark_buffer_dirty(parent);
742 /* update the path */
744 if (btrfs_header_nritems(left) > orig_slot) {
745 extent_buffer_get(left);
746 path->nodes[level] = left;
747 path->slots[level + 1] -= 1;
748 path->slots[level] = orig_slot;
750 free_extent_buffer(mid);
752 orig_slot -= btrfs_header_nritems(left);
753 path->slots[level] = orig_slot;
756 /* double check we haven't messed things up */
757 check_block(root, path, level);
759 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
763 free_extent_buffer(right);
765 free_extent_buffer(left);
769 /* returns zero if the push worked, non-zero otherwise */
770 static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
771 struct btrfs_root *root,
772 struct btrfs_path *path, int level)
774 struct extent_buffer *right = NULL;
775 struct extent_buffer *mid;
776 struct extent_buffer *left = NULL;
777 struct extent_buffer *parent = NULL;
781 int orig_slot = path->slots[level];
787 mid = path->nodes[level];
788 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
790 if (level < BTRFS_MAX_LEVEL - 1)
791 parent = path->nodes[level + 1];
792 pslot = path->slots[level + 1];
797 left = read_node_slot(root, parent, pslot - 1);
799 /* first, try to make some room in the middle buffer */
802 left_nr = btrfs_header_nritems(left);
803 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
806 ret = btrfs_cow_block(trans, root, left, parent,
811 wret = push_node_left(trans, root,
818 struct btrfs_disk_key disk_key;
819 orig_slot += left_nr;
820 btrfs_node_key(mid, &disk_key, 0);
821 btrfs_set_node_key(parent, &disk_key, pslot);
822 btrfs_mark_buffer_dirty(parent);
823 if (btrfs_header_nritems(left) > orig_slot) {
824 path->nodes[level] = left;
825 path->slots[level + 1] -= 1;
826 path->slots[level] = orig_slot;
827 free_extent_buffer(mid);
830 btrfs_header_nritems(left);
831 path->slots[level] = orig_slot;
832 free_extent_buffer(left);
836 free_extent_buffer(left);
838 right= read_node_slot(root, parent, pslot + 1);
841 * then try to empty the right most buffer into the middle
845 right_nr = btrfs_header_nritems(right);
846 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
849 ret = btrfs_cow_block(trans, root, right,
855 wret = balance_node_right(trans, root,
862 struct btrfs_disk_key disk_key;
864 btrfs_node_key(right, &disk_key, 0);
865 btrfs_set_node_key(parent, &disk_key, pslot + 1);
866 btrfs_mark_buffer_dirty(parent);
868 if (btrfs_header_nritems(mid) <= orig_slot) {
869 path->nodes[level] = right;
870 path->slots[level + 1] += 1;
871 path->slots[level] = orig_slot -
872 btrfs_header_nritems(mid);
873 free_extent_buffer(mid);
875 free_extent_buffer(right);
879 free_extent_buffer(right);
885 * readahead one full node of leaves
887 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
890 struct extent_buffer *node;
896 int direction = path->reada;
897 struct extent_buffer *eb;
905 if (!path->nodes[level])
908 node = path->nodes[level];
909 search = btrfs_node_blockptr(node, slot);
910 blocksize = btrfs_level_size(root, level - 1);
911 eb = btrfs_find_tree_block(root, search, blocksize);
913 free_extent_buffer(eb);
917 highest_read = search;
918 lowest_read = search;
920 nritems = btrfs_header_nritems(node);
927 } else if (direction > 0) {
932 search = btrfs_node_blockptr(node, nr);
933 if ((search >= lowest_read && search <= highest_read) ||
934 (search < lowest_read && lowest_read - search <= 32768) ||
935 (search > highest_read && search - highest_read <= 32768)) {
936 readahead_tree_block(root, search, blocksize);
940 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
942 if(nread > (1024 * 1024) || nscan > 128)
945 if (search < lowest_read)
946 lowest_read = search;
947 if (search > highest_read)
948 highest_read = search;
952 * look for key in the tree. path is filled in with nodes along the way
953 * if key is found, we return zero and you can find the item in the leaf
954 * level of the path (level 0)
956 * If the key isn't found, the path points to the slot where it should
957 * be inserted, and 1 is returned. If there are other errors during the
958 * search a negative error number is returned.
960 * if ins_len > 0, nodes and leaves will be split as we walk down the
961 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
964 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
965 *root, struct btrfs_key *key, struct btrfs_path *p, int
968 struct extent_buffer *b;
973 int should_reada = p->reada;
976 lowest_level = p->lowest_level;
977 WARN_ON(lowest_level && ins_len);
978 WARN_ON(p->nodes[0] != NULL);
979 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
982 extent_buffer_get(b);
984 level = btrfs_header_level(b);
987 wret = btrfs_cow_block(trans, root, b,
992 free_extent_buffer(b);
996 BUG_ON(!cow && ins_len);
997 if (level != btrfs_header_level(b))
999 level = btrfs_header_level(b);
1000 p->nodes[level] = b;
1001 ret = check_block(root, p, level);
1004 ret = bin_search(b, key, level, &slot);
1006 if (ret && slot > 0)
1008 p->slots[level] = slot;
1009 if (ins_len > 0 && btrfs_header_nritems(b) >=
1010 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1011 int sret = split_node(trans, root, p, level);
1015 b = p->nodes[level];
1016 slot = p->slots[level];
1017 } else if (ins_len < 0) {
1018 int sret = balance_level(trans, root, p,
1022 b = p->nodes[level];
1024 btrfs_release_path(NULL, p);
1027 slot = p->slots[level];
1028 BUG_ON(btrfs_header_nritems(b) == 1);
1030 /* this is only true while dropping a snapshot */
1031 if (level == lowest_level)
1033 bytenr = btrfs_node_blockptr(b, slot);
1035 reada_for_search(root, p, level, slot);
1036 b = read_tree_block(root, bytenr,
1037 btrfs_level_size(root, level - 1));
1039 p->slots[level] = slot;
1040 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1041 sizeof(struct btrfs_item) + ins_len) {
1042 int sret = split_leaf(trans, root, key,
1043 p, ins_len, ret == 0);
1055 * adjust the pointers going up the tree, starting at level
1056 * making sure the right key of each node is points to 'key'.
1057 * This is used after shifting pointers to the left, so it stops
1058 * fixing up pointers when a given leaf/node is not in slot 0 of the
1061 * If this fails to write a tree block, it returns -1, but continues
1062 * fixing up the blocks in ram so the tree is consistent.
1064 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1065 struct btrfs_root *root, struct btrfs_path *path,
1066 struct btrfs_disk_key *key, int level)
1070 struct extent_buffer *t;
1072 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1073 int tslot = path->slots[i];
1074 if (!path->nodes[i])
1077 btrfs_set_node_key(t, key, tslot);
1078 btrfs_mark_buffer_dirty(path->nodes[i]);
1086 * try to push data from one node into the next node left in the
1089 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1090 * error, and > 0 if there was no room in the left hand block.
1092 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
1093 *root, struct extent_buffer *dst,
1094 struct extent_buffer *src)
1101 src_nritems = btrfs_header_nritems(src);
1102 dst_nritems = btrfs_header_nritems(dst);
1103 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1105 if (push_items <= 0) {
1109 if (src_nritems < push_items)
1110 push_items = src_nritems;
1112 copy_extent_buffer(dst, src,
1113 btrfs_node_key_ptr_offset(dst_nritems),
1114 btrfs_node_key_ptr_offset(0),
1115 push_items * sizeof(struct btrfs_key_ptr));
1117 if (push_items < src_nritems) {
1118 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1119 btrfs_node_key_ptr_offset(push_items),
1120 (src_nritems - push_items) *
1121 sizeof(struct btrfs_key_ptr));
1123 btrfs_set_header_nritems(src, src_nritems - push_items);
1124 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1125 btrfs_mark_buffer_dirty(src);
1126 btrfs_mark_buffer_dirty(dst);
1131 * try to push data from one node into the next node right in the
1134 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1135 * error, and > 0 if there was no room in the right hand block.
1137 * this will only push up to 1/2 the contents of the left node over
1139 static int balance_node_right(struct btrfs_trans_handle *trans,
1140 struct btrfs_root *root,
1141 struct extent_buffer *dst,
1142 struct extent_buffer *src)
1150 src_nritems = btrfs_header_nritems(src);
1151 dst_nritems = btrfs_header_nritems(dst);
1152 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1153 if (push_items <= 0)
1156 max_push = src_nritems / 2 + 1;
1157 /* don't try to empty the node */
1158 if (max_push >= src_nritems)
1161 if (max_push < push_items)
1162 push_items = max_push;
1164 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1165 btrfs_node_key_ptr_offset(0),
1167 sizeof(struct btrfs_key_ptr));
1169 copy_extent_buffer(dst, src,
1170 btrfs_node_key_ptr_offset(0),
1171 btrfs_node_key_ptr_offset(src_nritems - push_items),
1172 push_items * sizeof(struct btrfs_key_ptr));
1174 btrfs_set_header_nritems(src, src_nritems - push_items);
1175 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1177 btrfs_mark_buffer_dirty(src);
1178 btrfs_mark_buffer_dirty(dst);
1183 * helper function to insert a new root level in the tree.
1184 * A new node is allocated, and a single item is inserted to
1185 * point to the existing root
1187 * returns zero on success or < 0 on failure.
1189 static int insert_new_root(struct btrfs_trans_handle *trans,
1190 struct btrfs_root *root,
1191 struct btrfs_path *path, int level)
1193 struct extent_buffer *lower;
1194 struct extent_buffer *c;
1195 struct btrfs_disk_key lower_key;
1197 BUG_ON(path->nodes[level]);
1198 BUG_ON(path->nodes[level-1] != root->node);
1200 c = btrfs_alloc_free_block(trans, root, root->nodesize,
1201 root->node->start, 0);
1204 memset_extent_buffer(c, 0, 0, root->nodesize);
1205 btrfs_set_header_nritems(c, 1);
1206 btrfs_set_header_level(c, level);
1207 btrfs_set_header_bytenr(c, c->start);
1208 btrfs_set_header_generation(c, trans->transid);
1209 btrfs_set_header_owner(c, root->root_key.objectid);
1210 lower = path->nodes[level-1];
1212 write_extent_buffer(c, root->fs_info->fsid,
1213 (unsigned long)btrfs_header_fsid(c),
1216 btrfs_item_key(lower, &lower_key, 0);
1218 btrfs_node_key(lower, &lower_key, 0);
1219 btrfs_set_node_key(c, &lower_key, 0);
1220 btrfs_set_node_blockptr(c, 0, lower->start);
1222 btrfs_mark_buffer_dirty(c);
1224 /* the super has an extra ref to root->node */
1225 free_extent_buffer(root->node);
1227 extent_buffer_get(c);
1228 path->nodes[level] = c;
1229 path->slots[level] = 0;
1234 * worker function to insert a single pointer in a node.
1235 * the node should have enough room for the pointer already
1237 * slot and level indicate where you want the key to go, and
1238 * blocknr is the block the key points to.
1240 * returns zero on success and < 0 on any error
1242 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1243 *root, struct btrfs_path *path, struct btrfs_disk_key
1244 *key, u64 bytenr, int slot, int level)
1246 struct extent_buffer *lower;
1249 BUG_ON(!path->nodes[level]);
1250 lower = path->nodes[level];
1251 nritems = btrfs_header_nritems(lower);
1254 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1256 if (slot != nritems) {
1257 memmove_extent_buffer(lower,
1258 btrfs_node_key_ptr_offset(slot + 1),
1259 btrfs_node_key_ptr_offset(slot),
1260 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1262 btrfs_set_node_key(lower, key, slot);
1263 btrfs_set_node_blockptr(lower, slot, bytenr);
1264 btrfs_set_header_nritems(lower, nritems + 1);
1265 btrfs_mark_buffer_dirty(lower);
1270 * split the node at the specified level in path in two.
1271 * The path is corrected to point to the appropriate node after the split
1273 * Before splitting this tries to make some room in the node by pushing
1274 * left and right, if either one works, it returns right away.
1276 * returns 0 on success and < 0 on failure
1278 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1279 *root, struct btrfs_path *path, int level)
1281 struct extent_buffer *c;
1282 struct extent_buffer *split;
1283 struct btrfs_disk_key disk_key;
1289 c = path->nodes[level];
1290 if (c == root->node) {
1291 /* trying to split the root, lets make a new one */
1292 ret = insert_new_root(trans, root, path, level + 1);
1296 ret = push_nodes_for_insert(trans, root, path, level);
1297 c = path->nodes[level];
1298 if (!ret && btrfs_header_nritems(c) <
1299 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1305 c_nritems = btrfs_header_nritems(c);
1306 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1309 return PTR_ERR(split);
1311 btrfs_set_header_flags(split, btrfs_header_flags(c));
1312 btrfs_set_header_level(split, btrfs_header_level(c));
1313 btrfs_set_header_bytenr(split, split->start);
1314 btrfs_set_header_generation(split, trans->transid);
1315 btrfs_set_header_owner(split, root->root_key.objectid);
1316 write_extent_buffer(split, root->fs_info->fsid,
1317 (unsigned long)btrfs_header_fsid(split),
1320 mid = (c_nritems + 1) / 2;
1322 copy_extent_buffer(split, c,
1323 btrfs_node_key_ptr_offset(0),
1324 btrfs_node_key_ptr_offset(mid),
1325 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1326 btrfs_set_header_nritems(split, c_nritems - mid);
1327 btrfs_set_header_nritems(c, mid);
1330 btrfs_mark_buffer_dirty(c);
1331 btrfs_mark_buffer_dirty(split);
1333 btrfs_node_key(split, &disk_key, 0);
1334 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1335 path->slots[level + 1] + 1,
1340 if (path->slots[level] >= mid) {
1341 path->slots[level] -= mid;
1342 free_extent_buffer(c);
1343 path->nodes[level] = split;
1344 path->slots[level + 1] += 1;
1346 free_extent_buffer(split);
1352 * how many bytes are required to store the items in a leaf. start
1353 * and nr indicate which items in the leaf to check. This totals up the
1354 * space used both by the item structs and the item data
1356 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1359 int nritems = btrfs_header_nritems(l);
1360 int end = min(nritems, start + nr) - 1;
1364 data_len = btrfs_item_end_nr(l, start);
1365 data_len = data_len - btrfs_item_offset_nr(l, end);
1366 data_len += sizeof(struct btrfs_item) * nr;
1367 WARN_ON(data_len < 0);
1372 * The space between the end of the leaf items and
1373 * the start of the leaf data. IOW, how much room
1374 * the leaf has left for both items and data
1376 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1378 int nritems = btrfs_header_nritems(leaf);
1380 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1382 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1383 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1384 leaf_space_used(leaf, 0, nritems), nritems);
1390 * push some data in the path leaf to the right, trying to free up at
1391 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1393 * returns 1 if the push failed because the other node didn't have enough
1394 * room, 0 if everything worked out and < 0 if there were major errors.
1396 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1397 *root, struct btrfs_path *path, int data_size,
1400 struct extent_buffer *left = path->nodes[0];
1401 struct extent_buffer *right;
1402 struct extent_buffer *upper;
1403 struct btrfs_disk_key disk_key;
1409 struct btrfs_item *item;
1417 slot = path->slots[1];
1418 if (!path->nodes[1]) {
1421 upper = path->nodes[1];
1422 if (slot >= btrfs_header_nritems(upper) - 1)
1425 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1427 free_space = btrfs_leaf_free_space(root, right);
1428 if (free_space < data_size + sizeof(struct btrfs_item)) {
1429 free_extent_buffer(right);
1433 /* cow and double check */
1434 ret = btrfs_cow_block(trans, root, right, upper,
1437 free_extent_buffer(right);
1440 free_space = btrfs_leaf_free_space(root, right);
1441 if (free_space < data_size + sizeof(struct btrfs_item)) {
1442 free_extent_buffer(right);
1446 left_nritems = btrfs_header_nritems(left);
1447 if (left_nritems == 0) {
1448 free_extent_buffer(right);
1457 i = left_nritems - 1;
1459 item = btrfs_item_nr(left, i);
1461 if (path->slots[0] == i)
1462 push_space += data_size + sizeof(*item);
1464 if (!left->map_token) {
1465 map_extent_buffer(left, (unsigned long)item,
1466 sizeof(struct btrfs_item),
1467 &left->map_token, &left->kaddr,
1468 &left->map_start, &left->map_len,
1472 this_item_size = btrfs_item_size(left, item);
1473 if (this_item_size + sizeof(*item) + push_space > free_space)
1476 push_space += this_item_size + sizeof(*item);
1481 if (left->map_token) {
1482 unmap_extent_buffer(left, left->map_token, KM_USER1);
1483 left->map_token = NULL;
1486 if (push_items == 0) {
1487 free_extent_buffer(right);
1491 if (!empty && push_items == left_nritems)
1494 /* push left to right */
1495 right_nritems = btrfs_header_nritems(right);
1497 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1498 push_space -= leaf_data_end(root, left);
1500 /* make room in the right data area */
1501 data_end = leaf_data_end(root, right);
1502 memmove_extent_buffer(right,
1503 btrfs_leaf_data(right) + data_end - push_space,
1504 btrfs_leaf_data(right) + data_end,
1505 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1507 /* copy from the left data area */
1508 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1509 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1510 btrfs_leaf_data(left) + leaf_data_end(root, left),
1513 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1514 btrfs_item_nr_offset(0),
1515 right_nritems * sizeof(struct btrfs_item));
1517 /* copy the items from left to right */
1518 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1519 btrfs_item_nr_offset(left_nritems - push_items),
1520 push_items * sizeof(struct btrfs_item));
1522 /* update the item pointers */
1523 right_nritems += push_items;
1524 btrfs_set_header_nritems(right, right_nritems);
1525 push_space = BTRFS_LEAF_DATA_SIZE(root);
1526 for (i = 0; i < right_nritems; i++) {
1527 item = btrfs_item_nr(right, i);
1528 if (!right->map_token) {
1529 map_extent_buffer(right, (unsigned long)item,
1530 sizeof(struct btrfs_item),
1531 &right->map_token, &right->kaddr,
1532 &right->map_start, &right->map_len,
1535 push_space -= btrfs_item_size(right, item);
1536 btrfs_set_item_offset(right, item, push_space);
1539 if (right->map_token) {
1540 unmap_extent_buffer(right, right->map_token, KM_USER1);
1541 right->map_token = NULL;
1543 left_nritems -= push_items;
1544 btrfs_set_header_nritems(left, left_nritems);
1547 btrfs_mark_buffer_dirty(left);
1548 btrfs_mark_buffer_dirty(right);
1550 btrfs_item_key(right, &disk_key, 0);
1551 btrfs_set_node_key(upper, &disk_key, slot + 1);
1552 btrfs_mark_buffer_dirty(upper);
1554 /* then fixup the leaf pointer in the path */
1555 if (path->slots[0] >= left_nritems) {
1556 path->slots[0] -= left_nritems;
1557 free_extent_buffer(path->nodes[0]);
1558 path->nodes[0] = right;
1559 path->slots[1] += 1;
1561 free_extent_buffer(right);
1566 * push some data in the path leaf to the left, trying to free up at
1567 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1569 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1570 *root, struct btrfs_path *path, int data_size,
1573 struct btrfs_disk_key disk_key;
1574 struct extent_buffer *right = path->nodes[0];
1575 struct extent_buffer *left;
1581 struct btrfs_item *item;
1582 u32 old_left_nritems;
1588 u32 old_left_item_size;
1590 slot = path->slots[1];
1593 if (!path->nodes[1])
1596 right_nritems = btrfs_header_nritems(right);
1597 if (right_nritems == 0) {
1601 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
1602 slot - 1), root->leafsize);
1603 free_space = btrfs_leaf_free_space(root, left);
1604 if (free_space < data_size + sizeof(struct btrfs_item)) {
1605 free_extent_buffer(left);
1609 /* cow and double check */
1610 ret = btrfs_cow_block(trans, root, left,
1611 path->nodes[1], slot - 1, &left);
1613 /* we hit -ENOSPC, but it isn't fatal here */
1614 free_extent_buffer(left);
1618 free_space = btrfs_leaf_free_space(root, left);
1619 if (free_space < data_size + sizeof(struct btrfs_item)) {
1620 free_extent_buffer(left);
1627 nr = right_nritems - 1;
1629 for (i = 0; i < nr; i++) {
1630 item = btrfs_item_nr(right, i);
1631 if (!right->map_token) {
1632 map_extent_buffer(right, (unsigned long)item,
1633 sizeof(struct btrfs_item),
1634 &right->map_token, &right->kaddr,
1635 &right->map_start, &right->map_len,
1639 if (path->slots[0] == i)
1640 push_space += data_size + sizeof(*item);
1642 this_item_size = btrfs_item_size(right, item);
1643 if (this_item_size + sizeof(*item) + push_space > free_space)
1647 push_space += this_item_size + sizeof(*item);
1650 if (right->map_token) {
1651 unmap_extent_buffer(right, right->map_token, KM_USER1);
1652 right->map_token = NULL;
1655 if (push_items == 0) {
1656 free_extent_buffer(left);
1659 if (!empty && push_items == btrfs_header_nritems(right))
1662 /* push data from right to left */
1663 copy_extent_buffer(left, right,
1664 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1665 btrfs_item_nr_offset(0),
1666 push_items * sizeof(struct btrfs_item));
1668 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1669 btrfs_item_offset_nr(right, push_items -1);
1671 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1672 leaf_data_end(root, left) - push_space,
1673 btrfs_leaf_data(right) +
1674 btrfs_item_offset_nr(right, push_items - 1),
1676 old_left_nritems = btrfs_header_nritems(left);
1677 BUG_ON(old_left_nritems < 0);
1679 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1680 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1683 item = btrfs_item_nr(left, i);
1684 if (!left->map_token) {
1685 map_extent_buffer(left, (unsigned long)item,
1686 sizeof(struct btrfs_item),
1687 &left->map_token, &left->kaddr,
1688 &left->map_start, &left->map_len,
1692 ioff = btrfs_item_offset(left, item);
1693 btrfs_set_item_offset(left, item,
1694 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1696 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1697 if (left->map_token) {
1698 unmap_extent_buffer(left, left->map_token, KM_USER1);
1699 left->map_token = NULL;
1702 /* fixup right node */
1703 if (push_items > right_nritems) {
1704 printk("push items %d nr %u\n", push_items, right_nritems);
1708 if (push_items < right_nritems) {
1709 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1710 leaf_data_end(root, right);
1711 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1712 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1713 btrfs_leaf_data(right) +
1714 leaf_data_end(root, right), push_space);
1716 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1717 btrfs_item_nr_offset(push_items),
1718 (btrfs_header_nritems(right) - push_items) *
1719 sizeof(struct btrfs_item));
1721 right_nritems -= push_items;
1722 btrfs_set_header_nritems(right, right_nritems);
1723 push_space = BTRFS_LEAF_DATA_SIZE(root);
1724 for (i = 0; i < right_nritems; i++) {
1725 item = btrfs_item_nr(right, i);
1727 if (!right->map_token) {
1728 map_extent_buffer(right, (unsigned long)item,
1729 sizeof(struct btrfs_item),
1730 &right->map_token, &right->kaddr,
1731 &right->map_start, &right->map_len,
1735 push_space = push_space - btrfs_item_size(right, item);
1736 btrfs_set_item_offset(right, item, push_space);
1738 if (right->map_token) {
1739 unmap_extent_buffer(right, right->map_token, KM_USER1);
1740 right->map_token = NULL;
1743 btrfs_mark_buffer_dirty(left);
1745 btrfs_mark_buffer_dirty(right);
1747 btrfs_item_key(right, &disk_key, 0);
1748 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1752 /* then fixup the leaf pointer in the path */
1753 if (path->slots[0] < push_items) {
1754 path->slots[0] += old_left_nritems;
1755 free_extent_buffer(path->nodes[0]);
1756 path->nodes[0] = left;
1757 path->slots[1] -= 1;
1759 free_extent_buffer(left);
1760 path->slots[0] -= push_items;
1762 BUG_ON(path->slots[0] < 0);
1767 * split the path's leaf in two, making sure there is at least data_size
1768 * available for the resulting leaf level of the path.
1770 * returns 0 if all went well and < 0 on failure.
1772 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1773 *root, struct btrfs_key *ins_key,
1774 struct btrfs_path *path, int data_size, int extend)
1776 struct extent_buffer *l;
1780 struct extent_buffer *right;
1781 int space_needed = data_size + sizeof(struct btrfs_item);
1788 int num_doubles = 0;
1789 struct btrfs_disk_key disk_key;
1792 space_needed = data_size;
1794 /* first try to make some room by pushing left and right */
1795 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
1796 wret = push_leaf_right(trans, root, path, data_size, 0);
1801 wret = push_leaf_left(trans, root, path, data_size, 0);
1807 /* did the pushes work? */
1808 if (btrfs_leaf_free_space(root, l) >= space_needed)
1812 if (!path->nodes[1]) {
1813 ret = insert_new_root(trans, root, path, 1);
1820 slot = path->slots[0];
1821 nritems = btrfs_header_nritems(l);
1822 mid = (nritems + 1)/ 2;
1824 right = btrfs_alloc_free_block(trans, root, root->leafsize,
1827 return PTR_ERR(right);
1829 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
1830 btrfs_set_header_bytenr(right, right->start);
1831 btrfs_set_header_generation(right, trans->transid);
1832 btrfs_set_header_owner(right, root->root_key.objectid);
1833 btrfs_set_header_level(right, 0);
1834 write_extent_buffer(right, root->fs_info->fsid,
1835 (unsigned long)btrfs_header_fsid(right),
1839 leaf_space_used(l, mid, nritems - mid) + space_needed >
1840 BTRFS_LEAF_DATA_SIZE(root)) {
1841 if (slot >= nritems) {
1842 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1843 btrfs_set_header_nritems(right, 0);
1844 wret = insert_ptr(trans, root, path,
1845 &disk_key, right->start,
1846 path->slots[1] + 1, 1);
1849 free_extent_buffer(path->nodes[0]);
1850 path->nodes[0] = right;
1852 path->slots[1] += 1;
1856 if (mid != nritems &&
1857 leaf_space_used(l, mid, nritems - mid) +
1858 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1863 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1864 BTRFS_LEAF_DATA_SIZE(root)) {
1865 if (!extend && slot == 0) {
1866 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1867 btrfs_set_header_nritems(right, 0);
1868 wret = insert_ptr(trans, root, path,
1874 free_extent_buffer(path->nodes[0]);
1875 path->nodes[0] = right;
1877 if (path->slots[1] == 0) {
1878 wret = fixup_low_keys(trans, root,
1879 path, &disk_key, 1);
1884 } else if (extend && slot == 0) {
1888 if (mid != nritems &&
1889 leaf_space_used(l, mid, nritems - mid) +
1890 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1896 nritems = nritems - mid;
1897 btrfs_set_header_nritems(right, nritems);
1898 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1900 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1901 btrfs_item_nr_offset(mid),
1902 nritems * sizeof(struct btrfs_item));
1904 copy_extent_buffer(right, l,
1905 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1906 data_copy_size, btrfs_leaf_data(l) +
1907 leaf_data_end(root, l), data_copy_size);
1909 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1910 btrfs_item_end_nr(l, mid);
1912 for (i = 0; i < nritems; i++) {
1913 struct btrfs_item *item = btrfs_item_nr(right, i);
1916 if (!right->map_token) {
1917 map_extent_buffer(right, (unsigned long)item,
1918 sizeof(struct btrfs_item),
1919 &right->map_token, &right->kaddr,
1920 &right->map_start, &right->map_len,
1924 ioff = btrfs_item_offset(right, item);
1925 btrfs_set_item_offset(right, item, ioff + rt_data_off);
1928 if (right->map_token) {
1929 unmap_extent_buffer(right, right->map_token, KM_USER1);
1930 right->map_token = NULL;
1933 btrfs_set_header_nritems(l, mid);
1935 btrfs_item_key(right, &disk_key, 0);
1936 wret = insert_ptr(trans, root, path, &disk_key, right->start,
1937 path->slots[1] + 1, 1);
1941 btrfs_mark_buffer_dirty(right);
1942 btrfs_mark_buffer_dirty(l);
1943 BUG_ON(path->slots[0] != slot);
1946 free_extent_buffer(path->nodes[0]);
1947 path->nodes[0] = right;
1948 path->slots[0] -= mid;
1949 path->slots[1] += 1;
1951 free_extent_buffer(right);
1953 BUG_ON(path->slots[0] < 0);
1956 BUG_ON(num_doubles != 0);
1963 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1964 struct btrfs_root *root,
1965 struct btrfs_path *path,
1966 u32 new_size, int from_end)
1971 struct extent_buffer *leaf;
1972 struct btrfs_item *item;
1974 unsigned int data_end;
1975 unsigned int old_data_start;
1976 unsigned int old_size;
1977 unsigned int size_diff;
1980 slot_orig = path->slots[0];
1981 leaf = path->nodes[0];
1982 slot = path->slots[0];
1984 old_size = btrfs_item_size_nr(leaf, slot);
1985 if (old_size == new_size)
1988 nritems = btrfs_header_nritems(leaf);
1989 data_end = leaf_data_end(root, leaf);
1991 old_data_start = btrfs_item_offset_nr(leaf, slot);
1993 size_diff = old_size - new_size;
1996 BUG_ON(slot >= nritems);
1999 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2001 /* first correct the data pointers */
2002 for (i = slot; i < nritems; i++) {
2004 item = btrfs_item_nr(leaf, i);
2006 if (!leaf->map_token) {
2007 map_extent_buffer(leaf, (unsigned long)item,
2008 sizeof(struct btrfs_item),
2009 &leaf->map_token, &leaf->kaddr,
2010 &leaf->map_start, &leaf->map_len,
2014 ioff = btrfs_item_offset(leaf, item);
2015 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2018 if (leaf->map_token) {
2019 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2020 leaf->map_token = NULL;
2023 /* shift the data */
2025 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2026 data_end + size_diff, btrfs_leaf_data(leaf) +
2027 data_end, old_data_start + new_size - data_end);
2029 struct btrfs_disk_key disk_key;
2032 btrfs_item_key(leaf, &disk_key, slot);
2034 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2036 struct btrfs_file_extent_item *fi;
2038 fi = btrfs_item_ptr(leaf, slot,
2039 struct btrfs_file_extent_item);
2040 fi = (struct btrfs_file_extent_item *)(
2041 (unsigned long)fi - size_diff);
2043 if (btrfs_file_extent_type(leaf, fi) ==
2044 BTRFS_FILE_EXTENT_INLINE) {
2045 ptr = btrfs_item_ptr_offset(leaf, slot);
2046 memmove_extent_buffer(leaf, ptr,
2048 offsetof(struct btrfs_file_extent_item,
2053 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2054 data_end + size_diff, btrfs_leaf_data(leaf) +
2055 data_end, old_data_start - data_end);
2057 offset = btrfs_disk_key_offset(&disk_key);
2058 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2059 btrfs_set_item_key(leaf, &disk_key, slot);
2061 fixup_low_keys(trans, root, path, &disk_key, 1);
2064 item = btrfs_item_nr(leaf, slot);
2065 btrfs_set_item_size(leaf, item, new_size);
2066 btrfs_mark_buffer_dirty(leaf);
2069 if (btrfs_leaf_free_space(root, leaf) < 0) {
2070 btrfs_print_leaf(root, leaf);
2076 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2077 struct btrfs_root *root, struct btrfs_path *path,
2083 struct extent_buffer *leaf;
2084 struct btrfs_item *item;
2086 unsigned int data_end;
2087 unsigned int old_data;
2088 unsigned int old_size;
2091 slot_orig = path->slots[0];
2092 leaf = path->nodes[0];
2094 nritems = btrfs_header_nritems(leaf);
2095 data_end = leaf_data_end(root, leaf);
2097 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2098 btrfs_print_leaf(root, leaf);
2101 slot = path->slots[0];
2102 old_data = btrfs_item_end_nr(leaf, slot);
2105 if (slot >= nritems) {
2106 btrfs_print_leaf(root, leaf);
2107 printk("slot %d too large, nritems %d\n", slot, nritems);
2112 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2114 /* first correct the data pointers */
2115 for (i = slot; i < nritems; i++) {
2117 item = btrfs_item_nr(leaf, i);
2119 if (!leaf->map_token) {
2120 map_extent_buffer(leaf, (unsigned long)item,
2121 sizeof(struct btrfs_item),
2122 &leaf->map_token, &leaf->kaddr,
2123 &leaf->map_start, &leaf->map_len,
2126 ioff = btrfs_item_offset(leaf, item);
2127 btrfs_set_item_offset(leaf, item, ioff - data_size);
2130 if (leaf->map_token) {
2131 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2132 leaf->map_token = NULL;
2135 /* shift the data */
2136 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2137 data_end - data_size, btrfs_leaf_data(leaf) +
2138 data_end, old_data - data_end);
2140 data_end = old_data;
2141 old_size = btrfs_item_size_nr(leaf, slot);
2142 item = btrfs_item_nr(leaf, slot);
2143 btrfs_set_item_size(leaf, item, old_size + data_size);
2144 btrfs_mark_buffer_dirty(leaf);
2147 if (btrfs_leaf_free_space(root, leaf) < 0) {
2148 btrfs_print_leaf(root, leaf);
2155 * Given a key and some data, insert an item into the tree.
2156 * This does all the path init required, making room in the tree if needed.
2158 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2159 struct btrfs_root *root,
2160 struct btrfs_path *path,
2161 struct btrfs_key *cpu_key, u32 data_size)
2163 struct extent_buffer *leaf;
2164 struct btrfs_item *item;
2169 unsigned int data_end;
2170 struct btrfs_disk_key disk_key;
2172 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2174 /* create a root if there isn't one */
2178 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
2185 slot_orig = path->slots[0];
2186 leaf = path->nodes[0];
2188 nritems = btrfs_header_nritems(leaf);
2189 data_end = leaf_data_end(root, leaf);
2191 if (btrfs_leaf_free_space(root, leaf) <
2192 sizeof(struct btrfs_item) + data_size) {
2193 btrfs_print_leaf(root, leaf);
2194 printk("not enough freespace need %u have %d\n",
2195 data_size, btrfs_leaf_free_space(root, leaf));
2199 slot = path->slots[0];
2202 if (slot != nritems) {
2204 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2206 if (old_data < data_end) {
2207 btrfs_print_leaf(root, leaf);
2208 printk("slot %d old_data %d data_end %d\n",
2209 slot, old_data, data_end);
2213 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2215 /* first correct the data pointers */
2216 WARN_ON(leaf->map_token);
2217 for (i = slot; i < nritems; i++) {
2220 item = btrfs_item_nr(leaf, i);
2221 if (!leaf->map_token) {
2222 map_extent_buffer(leaf, (unsigned long)item,
2223 sizeof(struct btrfs_item),
2224 &leaf->map_token, &leaf->kaddr,
2225 &leaf->map_start, &leaf->map_len,
2229 ioff = btrfs_item_offset(leaf, item);
2230 btrfs_set_item_offset(leaf, item, ioff - data_size);
2232 if (leaf->map_token) {
2233 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2234 leaf->map_token = NULL;
2237 /* shift the items */
2238 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2239 btrfs_item_nr_offset(slot),
2240 (nritems - slot) * sizeof(struct btrfs_item));
2242 /* shift the data */
2243 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2244 data_end - data_size, btrfs_leaf_data(leaf) +
2245 data_end, old_data - data_end);
2246 data_end = old_data;
2249 /* setup the item for the new data */
2250 btrfs_set_item_key(leaf, &disk_key, slot);
2251 item = btrfs_item_nr(leaf, slot);
2252 btrfs_set_item_offset(leaf, item, data_end - data_size);
2253 btrfs_set_item_size(leaf, item, data_size);
2254 btrfs_set_header_nritems(leaf, nritems + 1);
2255 btrfs_mark_buffer_dirty(leaf);
2259 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2261 if (btrfs_leaf_free_space(root, leaf) < 0) {
2262 btrfs_print_leaf(root, leaf);
2270 * Given a key and some data, insert an item into the tree.
2271 * This does all the path init required, making room in the tree if needed.
2273 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2274 *root, struct btrfs_key *cpu_key, void *data, u32
2278 struct btrfs_path *path;
2279 struct extent_buffer *leaf;
2282 path = btrfs_alloc_path();
2284 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2286 leaf = path->nodes[0];
2287 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2288 write_extent_buffer(leaf, data, ptr, data_size);
2289 btrfs_mark_buffer_dirty(leaf);
2291 btrfs_free_path(path);
2296 * delete the pointer from a given node.
2298 * If the delete empties a node, the node is removed from the tree,
2299 * continuing all the way the root if required. The root is converted into
2300 * a leaf if all the nodes are emptied.
2302 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2303 struct btrfs_path *path, int level, int slot)
2305 struct extent_buffer *parent = path->nodes[level];
2310 nritems = btrfs_header_nritems(parent);
2311 if (slot != nritems -1) {
2312 memmove_extent_buffer(parent,
2313 btrfs_node_key_ptr_offset(slot),
2314 btrfs_node_key_ptr_offset(slot + 1),
2315 sizeof(struct btrfs_key_ptr) *
2316 (nritems - slot - 1));
2319 btrfs_set_header_nritems(parent, nritems);
2320 if (nritems == 0 && parent == root->node) {
2321 BUG_ON(btrfs_header_level(root->node) != 1);
2322 /* just turn the root into a leaf and break */
2323 btrfs_set_header_level(root->node, 0);
2324 } else if (slot == 0) {
2325 struct btrfs_disk_key disk_key;
2327 btrfs_node_key(parent, &disk_key, 0);
2328 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2332 btrfs_mark_buffer_dirty(parent);
2337 * delete the item at the leaf level in path. If that empties
2338 * the leaf, remove it from the tree
2340 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2341 struct btrfs_path *path)
2344 struct extent_buffer *leaf;
2345 struct btrfs_item *item;
2352 leaf = path->nodes[0];
2353 slot = path->slots[0];
2354 doff = btrfs_item_offset_nr(leaf, slot);
2355 dsize = btrfs_item_size_nr(leaf, slot);
2356 nritems = btrfs_header_nritems(leaf);
2358 if (slot != nritems - 1) {
2360 int data_end = leaf_data_end(root, leaf);
2362 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2364 btrfs_leaf_data(leaf) + data_end,
2367 for (i = slot + 1; i < nritems; i++) {
2370 item = btrfs_item_nr(leaf, i);
2371 if (!leaf->map_token) {
2372 map_extent_buffer(leaf, (unsigned long)item,
2373 sizeof(struct btrfs_item),
2374 &leaf->map_token, &leaf->kaddr,
2375 &leaf->map_start, &leaf->map_len,
2378 ioff = btrfs_item_offset(leaf, item);
2379 btrfs_set_item_offset(leaf, item, ioff + dsize);
2382 if (leaf->map_token) {
2383 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2384 leaf->map_token = NULL;
2387 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2388 btrfs_item_nr_offset(slot + 1),
2389 sizeof(struct btrfs_item) *
2390 (nritems - slot - 1));
2392 btrfs_set_header_nritems(leaf, nritems - 1);
2395 /* delete the leaf if we've emptied it */
2397 if (leaf == root->node) {
2398 btrfs_set_header_level(leaf, 0);
2400 clean_tree_block(trans, root, leaf);
2401 wait_on_tree_block_writeback(root, leaf);
2402 wret = del_ptr(trans, root, path, 1, path->slots[1]);
2405 wret = btrfs_free_extent(trans, root,
2406 leaf->start, leaf->len, 1);
2411 int used = leaf_space_used(leaf, 0, nritems);
2413 struct btrfs_disk_key disk_key;
2415 btrfs_item_key(leaf, &disk_key, 0);
2416 wret = fixup_low_keys(trans, root, path,
2422 /* delete the leaf if it is mostly empty */
2423 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
2424 /* push_leaf_left fixes the path.
2425 * make sure the path still points to our leaf
2426 * for possible call to del_ptr below
2428 slot = path->slots[1];
2429 extent_buffer_get(leaf);
2431 wret = push_leaf_right(trans, root, path, 1, 1);
2432 if (wret < 0 && wret != -ENOSPC)
2435 if (path->nodes[0] == leaf &&
2436 btrfs_header_nritems(leaf)) {
2437 wret = push_leaf_left(trans, root, path, 1, 1);
2438 if (wret < 0 && wret != -ENOSPC)
2442 if (btrfs_header_nritems(leaf) == 0) {
2443 u64 bytenr = leaf->start;
2444 u32 blocksize = leaf->len;
2446 clean_tree_block(trans, root, leaf);
2447 wait_on_tree_block_writeback(root, leaf);
2449 wret = del_ptr(trans, root, path, 1, slot);
2453 free_extent_buffer(leaf);
2454 wret = btrfs_free_extent(trans, root, bytenr,
2459 btrfs_mark_buffer_dirty(leaf);
2460 free_extent_buffer(leaf);
2463 btrfs_mark_buffer_dirty(leaf);
2470 * walk up the tree as far as required to find the next leaf.
2471 * returns 0 if it found something or 1 if there are no greater leaves.
2472 * returns < 0 on io errors.
2474 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2479 struct extent_buffer *c;
2480 struct extent_buffer *next = NULL;
2482 while(level < BTRFS_MAX_LEVEL) {
2483 if (!path->nodes[level])
2486 slot = path->slots[level] + 1;
2487 c = path->nodes[level];
2488 if (slot >= btrfs_header_nritems(c)) {
2493 bytenr = btrfs_node_blockptr(c, slot);
2495 free_extent_buffer(next);
2498 reada_for_search(root, path, level, slot);
2500 next = read_tree_block(root, bytenr,
2501 btrfs_level_size(root, level -1));
2504 path->slots[level] = slot;
2507 c = path->nodes[level];
2508 free_extent_buffer(c);
2509 path->nodes[level] = next;
2510 path->slots[level] = 0;
2514 reada_for_search(root, path, level, 0);
2515 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2516 btrfs_level_size(root, level - 1));