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.
20 #include "transaction.h"
21 #include "print-tree.h"
24 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
25 *root, struct btrfs_path *path, int level);
26 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_key *ins_key,
28 struct btrfs_path *path, int data_size, int extend);
29 static int push_node_left(struct btrfs_trans_handle *trans,
30 struct btrfs_root *root, struct extent_buffer *dst,
31 struct extent_buffer *src, int empty);
32 static int balance_node_right(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root,
34 struct extent_buffer *dst_buf,
35 struct extent_buffer *src_buf);
37 inline void btrfs_init_path(struct btrfs_path *p)
39 memset(p, 0, sizeof(*p));
42 struct btrfs_path *btrfs_alloc_path(void)
44 struct btrfs_path *path;
45 path = kmalloc(sizeof(struct btrfs_path), GFP_NOFS);
47 btrfs_init_path(path);
53 void btrfs_free_path(struct btrfs_path *p)
55 btrfs_release_path(NULL, p);
59 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
62 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
65 free_extent_buffer(p->nodes[i]);
67 memset(p, 0, sizeof(*p));
70 static void add_root_to_dirty_list(struct btrfs_root *root)
72 if (root->track_dirty && list_empty(&root->dirty_list)) {
73 list_add(&root->dirty_list,
74 &root->fs_info->dirty_cowonly_roots);
78 int btrfs_copy_root(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root,
80 struct extent_buffer *buf,
81 struct extent_buffer **cow_ret, u64 new_root_objectid)
83 struct extent_buffer *cow;
86 struct btrfs_root *new_root;
87 struct btrfs_disk_key disk_key;
89 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
93 memcpy(new_root, root, sizeof(*new_root));
94 new_root->root_key.objectid = new_root_objectid;
96 WARN_ON(root->ref_cows && trans->transid !=
97 root->fs_info->running_transaction->transid);
98 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
100 level = btrfs_header_level(buf);
102 btrfs_item_key(buf, &disk_key, 0);
104 btrfs_node_key(buf, &disk_key, 0);
105 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
106 new_root_objectid, &disk_key,
107 level, buf->start, 0);
113 copy_extent_buffer(cow, buf, 0, 0, cow->len);
114 btrfs_set_header_bytenr(cow, cow->start);
115 btrfs_set_header_generation(cow, trans->transid);
116 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
117 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
118 BTRFS_HEADER_FLAG_RELOC);
119 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
120 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
122 btrfs_set_header_owner(cow, new_root_objectid);
124 write_extent_buffer(cow, root->fs_info->fsid,
125 (unsigned long)btrfs_header_fsid(cow),
128 WARN_ON(btrfs_header_generation(buf) > trans->transid);
129 ret = btrfs_inc_ref(trans, new_root, cow, 0);
135 btrfs_mark_buffer_dirty(cow);
140 int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
141 struct btrfs_root *root)
143 struct extent_buffer *c;
144 struct extent_buffer *old = root->node;
146 struct btrfs_disk_key disk_key = {0,0,0};
150 c = btrfs_alloc_free_block(trans, root,
151 btrfs_level_size(root, 0),
152 root->root_key.objectid,
153 &disk_key, level, 0, 0);
157 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
158 btrfs_set_header_level(c, level);
159 btrfs_set_header_bytenr(c, c->start);
160 btrfs_set_header_generation(c, trans->transid);
161 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
162 btrfs_set_header_owner(c, root->root_key.objectid);
164 write_extent_buffer(c, root->fs_info->fsid,
165 (unsigned long)btrfs_header_fsid(c),
168 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
169 (unsigned long)btrfs_header_chunk_tree_uuid(c),
172 btrfs_mark_buffer_dirty(c);
174 free_extent_buffer(old);
176 add_root_to_dirty_list(root);
181 * check if the tree block can be shared by multiple trees
183 int btrfs_block_can_be_shared(struct btrfs_root *root,
184 struct extent_buffer *buf)
187 * Tree blocks not in refernece counted trees and tree roots
188 * are never shared. If a block was allocated after the last
189 * snapshot and the block was not allocated by tree relocation,
190 * we know the block is not shared.
192 if (root->ref_cows &&
193 buf != root->node && buf != root->commit_root &&
194 (btrfs_header_generation(buf) <=
195 btrfs_root_last_snapshot(&root->root_item) ||
196 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
198 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
199 if (root->ref_cows &&
200 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
206 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
207 struct btrfs_root *root,
208 struct extent_buffer *buf,
209 struct extent_buffer *cow)
218 * Backrefs update rules:
220 * Always use full backrefs for extent pointers in tree block
221 * allocated by tree relocation.
223 * If a shared tree block is no longer referenced by its owner
224 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
225 * use full backrefs for extent pointers in tree block.
227 * If a tree block is been relocating
228 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
229 * use full backrefs for extent pointers in tree block.
230 * The reason for this is some operations (such as drop tree)
231 * are only allowed for blocks use full backrefs.
234 if (btrfs_block_can_be_shared(root, buf)) {
235 ret = btrfs_lookup_extent_info(trans, root, buf->start,
236 buf->len, &refs, &flags);
241 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
242 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
243 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
248 owner = btrfs_header_owner(buf);
249 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
250 owner == BTRFS_TREE_RELOC_OBJECTID);
253 if ((owner == root->root_key.objectid ||
254 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
255 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
256 ret = btrfs_inc_ref(trans, root, buf, 1);
259 if (root->root_key.objectid ==
260 BTRFS_TREE_RELOC_OBJECTID) {
261 ret = btrfs_dec_ref(trans, root, buf, 0);
263 ret = btrfs_inc_ref(trans, root, cow, 1);
266 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
269 if (root->root_key.objectid ==
270 BTRFS_TREE_RELOC_OBJECTID)
271 ret = btrfs_inc_ref(trans, root, cow, 1);
273 ret = btrfs_inc_ref(trans, root, cow, 0);
276 if (new_flags != 0) {
277 ret = btrfs_set_block_flags(trans, root, buf->start,
278 buf->len, new_flags);
282 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
283 if (root->root_key.objectid ==
284 BTRFS_TREE_RELOC_OBJECTID)
285 ret = btrfs_inc_ref(trans, root, cow, 1);
287 ret = btrfs_inc_ref(trans, root, cow, 0);
289 ret = btrfs_dec_ref(trans, root, buf, 1);
292 clean_tree_block(trans, root, buf);
297 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
298 struct btrfs_root *root,
299 struct extent_buffer *buf,
300 struct extent_buffer *parent, int parent_slot,
301 struct extent_buffer **cow_ret,
302 u64 search_start, u64 empty_size)
304 struct extent_buffer *cow;
305 struct btrfs_disk_key disk_key;
308 WARN_ON(root->ref_cows && trans->transid !=
309 root->fs_info->running_transaction->transid);
310 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
312 level = btrfs_header_level(buf);
315 btrfs_item_key(buf, &disk_key, 0);
317 btrfs_node_key(buf, &disk_key, 0);
319 cow = btrfs_alloc_free_block(trans, root, buf->len,
320 root->root_key.objectid, &disk_key,
321 level, search_start, empty_size);
325 copy_extent_buffer(cow, buf, 0, 0, cow->len);
326 btrfs_set_header_bytenr(cow, cow->start);
327 btrfs_set_header_generation(cow, trans->transid);
328 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
329 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
330 BTRFS_HEADER_FLAG_RELOC);
331 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
332 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
334 btrfs_set_header_owner(cow, root->root_key.objectid);
336 write_extent_buffer(cow, root->fs_info->fsid,
337 (unsigned long)btrfs_header_fsid(cow),
340 WARN_ON(btrfs_header_generation(buf) > trans->transid);
342 update_ref_for_cow(trans, root, buf, cow);
344 if (buf == root->node) {
346 extent_buffer_get(cow);
348 btrfs_free_extent(trans, root, buf->start, buf->len,
349 0, root->root_key.objectid, level, 0);
350 free_extent_buffer(buf);
351 add_root_to_dirty_list(root);
353 btrfs_set_node_blockptr(parent, parent_slot,
355 WARN_ON(trans->transid == 0);
356 btrfs_set_node_ptr_generation(parent, parent_slot,
358 btrfs_mark_buffer_dirty(parent);
359 WARN_ON(btrfs_header_generation(parent) != trans->transid);
361 btrfs_free_extent(trans, root, buf->start, buf->len,
362 0, root->root_key.objectid, level, 1);
364 free_extent_buffer(buf);
365 btrfs_mark_buffer_dirty(cow);
370 static inline int should_cow_block(struct btrfs_trans_handle *trans,
371 struct btrfs_root *root,
372 struct extent_buffer *buf)
374 if (btrfs_header_generation(buf) == trans->transid &&
375 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
376 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
377 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
382 int btrfs_cow_block(struct btrfs_trans_handle *trans,
383 struct btrfs_root *root, struct extent_buffer *buf,
384 struct extent_buffer *parent, int parent_slot,
385 struct extent_buffer **cow_ret)
390 if (trans->transaction != root->fs_info->running_transaction) {
391 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
392 root->fs_info->running_transaction->transid);
396 if (trans->transid != root->fs_info->generation) {
397 printk(KERN_CRIT "trans %llu running %llu\n",
398 (unsigned long long)trans->transid,
399 (unsigned long long)root->fs_info->generation);
402 if (!should_cow_block(trans, root, buf)) {
407 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
408 ret = __btrfs_cow_block(trans, root, buf, parent,
409 parent_slot, cow_ret, search_start, 0);
414 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
416 if (blocknr < other && other - (blocknr + blocksize) < 32768)
418 if (blocknr > other && blocknr - (other + blocksize) < 32768)
425 * compare two keys in a memcmp fashion
427 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
431 btrfs_disk_key_to_cpu(&k1, disk);
433 if (k1.objectid > k2->objectid)
435 if (k1.objectid < k2->objectid)
437 if (k1.type > k2->type)
439 if (k1.type < k2->type)
441 if (k1.offset > k2->offset)
443 if (k1.offset < k2->offset)
450 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
451 struct btrfs_root *root, struct extent_buffer *parent,
452 int start_slot, int cache_only, u64 *last_ret,
453 struct btrfs_key *progress)
455 struct extent_buffer *cur;
456 struct extent_buffer *tmp;
459 u64 search_start = *last_ret;
469 int progress_passed = 0;
470 struct btrfs_disk_key disk_key;
472 parent_level = btrfs_header_level(parent);
473 if (cache_only && parent_level != 1)
476 if (trans->transaction != root->fs_info->running_transaction) {
477 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
478 root->fs_info->running_transaction->transid);
481 if (trans->transid != root->fs_info->generation) {
482 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
483 root->fs_info->generation);
487 parent_nritems = btrfs_header_nritems(parent);
488 blocksize = btrfs_level_size(root, parent_level - 1);
489 end_slot = parent_nritems;
491 if (parent_nritems == 1)
494 for (i = start_slot; i < end_slot; i++) {
497 if (!parent->map_token) {
498 map_extent_buffer(parent,
499 btrfs_node_key_ptr_offset(i),
500 sizeof(struct btrfs_key_ptr),
501 &parent->map_token, &parent->kaddr,
502 &parent->map_start, &parent->map_len,
505 btrfs_node_key(parent, &disk_key, i);
506 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
510 blocknr = btrfs_node_blockptr(parent, i);
511 gen = btrfs_node_ptr_generation(parent, i);
513 last_block = blocknr;
516 other = btrfs_node_blockptr(parent, i - 1);
517 close = close_blocks(blocknr, other, blocksize);
519 if (close && i < end_slot - 2) {
520 other = btrfs_node_blockptr(parent, i + 1);
521 close = close_blocks(blocknr, other, blocksize);
524 last_block = blocknr;
527 if (parent->map_token) {
528 unmap_extent_buffer(parent, parent->map_token,
530 parent->map_token = NULL;
533 cur = btrfs_find_tree_block(root, blocknr, blocksize);
535 uptodate = btrfs_buffer_uptodate(cur, gen);
538 if (!cur || !uptodate) {
540 free_extent_buffer(cur);
544 cur = read_tree_block(root, blocknr,
546 } else if (!uptodate) {
547 btrfs_read_buffer(cur, gen);
550 if (search_start == 0)
551 search_start = last_block;
553 err = __btrfs_cow_block(trans, root, cur, parent, i,
556 (end_slot - i) * blocksize));
558 free_extent_buffer(cur);
561 search_start = tmp->start;
562 last_block = tmp->start;
563 *last_ret = search_start;
564 if (parent_level == 1)
565 btrfs_clear_buffer_defrag(tmp);
566 free_extent_buffer(tmp);
568 if (parent->map_token) {
569 unmap_extent_buffer(parent, parent->map_token,
571 parent->map_token = NULL;
578 * The leaf data grows from end-to-front in the node.
579 * this returns the address of the start of the last item,
580 * which is the stop of the leaf data stack
582 static inline unsigned int leaf_data_end(struct btrfs_root *root,
583 struct extent_buffer *leaf)
585 u32 nr = btrfs_header_nritems(leaf);
587 return BTRFS_LEAF_DATA_SIZE(root);
588 return btrfs_item_offset_nr(leaf, nr - 1);
591 int btrfs_check_node(struct btrfs_root *root,
592 struct btrfs_disk_key *parent_key,
593 struct extent_buffer *buf)
596 struct btrfs_key cpukey;
597 struct btrfs_disk_key key;
598 u32 nritems = btrfs_header_nritems(buf);
600 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root))
603 if (parent_key && parent_key->type) {
604 btrfs_node_key(buf, &key, 0);
605 if (memcmp(parent_key, &key, sizeof(key)))
608 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
609 btrfs_node_key(buf, &key, i);
610 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
611 if (btrfs_comp_keys(&key, &cpukey) >= 0)
616 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
618 btrfs_disk_key_to_cpu(&cpukey, parent_key);
620 btrfs_node_key_to_cpu(buf, &cpukey, 0);
621 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
622 buf->start, buf->len,
623 btrfs_header_level(buf));
628 int btrfs_check_leaf(struct btrfs_root *root,
629 struct btrfs_disk_key *parent_key,
630 struct extent_buffer *buf)
633 struct btrfs_key cpukey;
634 struct btrfs_disk_key key;
635 u32 nritems = btrfs_header_nritems(buf);
637 if (btrfs_header_level(buf) != 0) {
638 fprintf(stderr, "leaf is not a leaf %llu\n",
639 (unsigned long long)btrfs_header_bytenr(buf));
642 if (btrfs_leaf_free_space(root, buf) < 0) {
643 fprintf(stderr, "leaf free space incorrect %llu %d\n",
644 (unsigned long long)btrfs_header_bytenr(buf),
645 btrfs_leaf_free_space(root, buf));
652 btrfs_item_key(buf, &key, 0);
653 if (parent_key && parent_key->type &&
654 memcmp(parent_key, &key, sizeof(key))) {
655 fprintf(stderr, "leaf parent key incorrect %llu\n",
656 (unsigned long long)btrfs_header_bytenr(buf));
659 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
660 btrfs_item_key(buf, &key, i);
661 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
662 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
663 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
666 if (btrfs_item_offset_nr(buf, i) !=
667 btrfs_item_end_nr(buf, i + 1)) {
668 fprintf(stderr, "incorrect offsets %u %u\n",
669 btrfs_item_offset_nr(buf, i),
670 btrfs_item_end_nr(buf, i + 1));
673 if (i == 0 && btrfs_item_end_nr(buf, i) !=
674 BTRFS_LEAF_DATA_SIZE(root)) {
675 fprintf(stderr, "bad item end %u wanted %u\n",
676 btrfs_item_end_nr(buf, i),
677 (unsigned)BTRFS_LEAF_DATA_SIZE(root));
683 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
685 btrfs_disk_key_to_cpu(&cpukey, parent_key);
687 btrfs_item_key_to_cpu(buf, &cpukey, 0);
689 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
690 buf->start, buf->len, 0);
695 static int noinline check_block(struct btrfs_root *root,
696 struct btrfs_path *path, int level)
698 struct btrfs_disk_key key;
699 struct btrfs_disk_key *key_ptr = NULL;
700 struct extent_buffer *parent;
702 if (path->nodes[level + 1]) {
703 parent = path->nodes[level + 1];
704 btrfs_node_key(parent, &key, path->slots[level + 1]);
708 return btrfs_check_leaf(root, key_ptr, path->nodes[0]);
709 return btrfs_check_node(root, key_ptr, path->nodes[level]);
713 * search for key in the extent_buffer. The items start at offset p,
714 * and they are item_size apart. There are 'max' items in p.
716 * the slot in the array is returned via slot, and it points to
717 * the place where you would insert key if it is not found in
720 * slot may point to max if the key is bigger than all of the keys
722 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
723 int item_size, struct btrfs_key *key,
730 unsigned long offset;
731 struct btrfs_disk_key *tmp;
734 mid = (low + high) / 2;
735 offset = p + mid * item_size;
737 tmp = (struct btrfs_disk_key *)(eb->data + offset);
738 ret = btrfs_comp_keys(tmp, key);
754 * simple bin_search frontend that does the right thing for
757 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
758 int level, int *slot)
761 return generic_bin_search(eb,
762 offsetof(struct btrfs_leaf, items),
763 sizeof(struct btrfs_item),
764 key, btrfs_header_nritems(eb),
767 return generic_bin_search(eb,
768 offsetof(struct btrfs_node, ptrs),
769 sizeof(struct btrfs_key_ptr),
770 key, btrfs_header_nritems(eb),
776 struct extent_buffer *read_node_slot(struct btrfs_root *root,
777 struct extent_buffer *parent, int slot)
779 int level = btrfs_header_level(parent);
782 if (slot >= btrfs_header_nritems(parent))
787 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
788 btrfs_level_size(root, level - 1),
789 btrfs_node_ptr_generation(parent, slot));
792 static int balance_level(struct btrfs_trans_handle *trans,
793 struct btrfs_root *root,
794 struct btrfs_path *path, int level)
796 struct extent_buffer *right = NULL;
797 struct extent_buffer *mid;
798 struct extent_buffer *left = NULL;
799 struct extent_buffer *parent = NULL;
803 int orig_slot = path->slots[level];
809 mid = path->nodes[level];
810 WARN_ON(btrfs_header_generation(mid) != trans->transid);
812 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
814 if (level < BTRFS_MAX_LEVEL - 1)
815 parent = path->nodes[level + 1];
816 pslot = path->slots[level + 1];
819 * deal with the case where there is only one pointer in the root
820 * by promoting the node below to a root
823 struct extent_buffer *child;
825 if (btrfs_header_nritems(mid) != 1)
828 /* promote the child to a root */
829 child = read_node_slot(root, mid, 0);
831 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
835 add_root_to_dirty_list(root);
836 path->nodes[level] = NULL;
837 clean_tree_block(trans, root, mid);
838 wait_on_tree_block_writeback(root, mid);
839 /* once for the path */
840 free_extent_buffer(mid);
842 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
843 0, root->root_key.objectid,
845 /* once for the root ptr */
846 free_extent_buffer(mid);
849 if (btrfs_header_nritems(mid) >
850 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
853 left = read_node_slot(root, parent, pslot - 1);
855 wret = btrfs_cow_block(trans, root, left,
856 parent, pslot - 1, &left);
862 right = read_node_slot(root, parent, pslot + 1);
864 wret = btrfs_cow_block(trans, root, right,
865 parent, pslot + 1, &right);
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);
881 * then try to empty the right most buffer into the middle
884 wret = push_node_left(trans, root, mid, right, 1);
885 if (wret < 0 && wret != -ENOSPC)
887 if (btrfs_header_nritems(right) == 0) {
888 u64 bytenr = right->start;
889 u32 blocksize = right->len;
891 clean_tree_block(trans, root, right);
892 wait_on_tree_block_writeback(root, right);
893 free_extent_buffer(right);
895 wret = btrfs_del_ptr(trans, root, path,
896 level + 1, pslot + 1);
899 wret = btrfs_free_extent(trans, root, bytenr,
901 root->root_key.objectid,
906 struct btrfs_disk_key right_key;
907 btrfs_node_key(right, &right_key, 0);
908 btrfs_set_node_key(parent, &right_key, pslot + 1);
909 btrfs_mark_buffer_dirty(parent);
912 if (btrfs_header_nritems(mid) == 1) {
914 * we're not allowed to leave a node with one item in the
915 * tree during a delete. A deletion from lower in the tree
916 * could try to delete the only pointer in this node.
917 * So, pull some keys from the left.
918 * There has to be a left pointer at this point because
919 * otherwise we would have pulled some pointers from the
923 wret = balance_node_right(trans, root, mid, left);
929 wret = push_node_left(trans, root, left, mid, 1);
935 if (btrfs_header_nritems(mid) == 0) {
936 /* we've managed to empty the middle node, drop it */
937 u64 bytenr = mid->start;
938 u32 blocksize = mid->len;
939 clean_tree_block(trans, root, mid);
940 wait_on_tree_block_writeback(root, mid);
941 free_extent_buffer(mid);
943 wret = btrfs_del_ptr(trans, root, path, level + 1, pslot);
946 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
947 0, root->root_key.objectid,
952 /* update the parent key to reflect our changes */
953 struct btrfs_disk_key mid_key;
954 btrfs_node_key(mid, &mid_key, 0);
955 btrfs_set_node_key(parent, &mid_key, pslot);
956 btrfs_mark_buffer_dirty(parent);
959 /* update the path */
961 if (btrfs_header_nritems(left) > orig_slot) {
962 extent_buffer_get(left);
963 path->nodes[level] = left;
964 path->slots[level + 1] -= 1;
965 path->slots[level] = orig_slot;
967 free_extent_buffer(mid);
969 orig_slot -= btrfs_header_nritems(left);
970 path->slots[level] = orig_slot;
973 /* double check we haven't messed things up */
974 check_block(root, path, level);
976 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
980 free_extent_buffer(right);
982 free_extent_buffer(left);
986 /* returns zero if the push worked, non-zero otherwise */
987 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
989 struct btrfs_path *path, int level)
991 struct extent_buffer *right = NULL;
992 struct extent_buffer *mid;
993 struct extent_buffer *left = NULL;
994 struct extent_buffer *parent = NULL;
998 int orig_slot = path->slots[level];
1003 mid = path->nodes[level];
1004 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1006 if (level < BTRFS_MAX_LEVEL - 1)
1007 parent = path->nodes[level + 1];
1008 pslot = path->slots[level + 1];
1013 left = read_node_slot(root, parent, pslot - 1);
1015 /* first, try to make some room in the middle buffer */
1018 left_nr = btrfs_header_nritems(left);
1019 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1022 ret = btrfs_cow_block(trans, root, left, parent,
1027 wret = push_node_left(trans, root,
1034 struct btrfs_disk_key disk_key;
1035 orig_slot += left_nr;
1036 btrfs_node_key(mid, &disk_key, 0);
1037 btrfs_set_node_key(parent, &disk_key, pslot);
1038 btrfs_mark_buffer_dirty(parent);
1039 if (btrfs_header_nritems(left) > orig_slot) {
1040 path->nodes[level] = left;
1041 path->slots[level + 1] -= 1;
1042 path->slots[level] = orig_slot;
1043 free_extent_buffer(mid);
1046 btrfs_header_nritems(left);
1047 path->slots[level] = orig_slot;
1048 free_extent_buffer(left);
1052 free_extent_buffer(left);
1054 right= read_node_slot(root, parent, pslot + 1);
1057 * then try to empty the right most buffer into the middle
1061 right_nr = btrfs_header_nritems(right);
1062 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1065 ret = btrfs_cow_block(trans, root, right,
1071 wret = balance_node_right(trans, root,
1078 struct btrfs_disk_key disk_key;
1080 btrfs_node_key(right, &disk_key, 0);
1081 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1082 btrfs_mark_buffer_dirty(parent);
1084 if (btrfs_header_nritems(mid) <= orig_slot) {
1085 path->nodes[level] = right;
1086 path->slots[level + 1] += 1;
1087 path->slots[level] = orig_slot -
1088 btrfs_header_nritems(mid);
1089 free_extent_buffer(mid);
1091 free_extent_buffer(right);
1095 free_extent_buffer(right);
1101 * readahead one full node of leaves
1103 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
1104 int level, int slot, u64 objectid)
1106 struct extent_buffer *node;
1107 struct btrfs_disk_key disk_key;
1113 int direction = path->reada;
1114 struct extent_buffer *eb;
1122 if (!path->nodes[level])
1125 node = path->nodes[level];
1126 search = btrfs_node_blockptr(node, slot);
1127 blocksize = btrfs_level_size(root, level - 1);
1128 eb = btrfs_find_tree_block(root, search, blocksize);
1130 free_extent_buffer(eb);
1134 highest_read = search;
1135 lowest_read = search;
1137 nritems = btrfs_header_nritems(node);
1140 if (direction < 0) {
1144 } else if (direction > 0) {
1149 if (path->reada < 0 && objectid) {
1150 btrfs_node_key(node, &disk_key, nr);
1151 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1154 search = btrfs_node_blockptr(node, nr);
1155 if ((search >= lowest_read && search <= highest_read) ||
1156 (search < lowest_read && lowest_read - search <= 32768) ||
1157 (search > highest_read && search - highest_read <= 32768)) {
1158 readahead_tree_block(root, search, blocksize,
1159 btrfs_node_ptr_generation(node, nr));
1163 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1165 if(nread > (1024 * 1024) || nscan > 128)
1168 if (search < lowest_read)
1169 lowest_read = search;
1170 if (search > highest_read)
1171 highest_read = search;
1176 * look for key in the tree. path is filled in with nodes along the way
1177 * if key is found, we return zero and you can find the item in the leaf
1178 * level of the path (level 0)
1180 * If the key isn't found, the path points to the slot where it should
1181 * be inserted, and 1 is returned. If there are other errors during the
1182 * search a negative error number is returned.
1184 * if ins_len > 0, nodes and leaves will be split as we walk down the
1185 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1188 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1189 *root, struct btrfs_key *key, struct btrfs_path *p, int
1192 struct extent_buffer *b;
1196 int should_reada = p->reada;
1197 u8 lowest_level = 0;
1199 lowest_level = p->lowest_level;
1200 WARN_ON(lowest_level && ins_len > 0);
1201 WARN_ON(p->nodes[0] != NULL);
1203 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1207 extent_buffer_get(b);
1209 level = btrfs_header_level(b);
1212 wret = btrfs_cow_block(trans, root, b,
1213 p->nodes[level + 1],
1214 p->slots[level + 1],
1217 free_extent_buffer(b);
1221 BUG_ON(!cow && ins_len);
1222 if (level != btrfs_header_level(b))
1224 level = btrfs_header_level(b);
1225 p->nodes[level] = b;
1226 ret = check_block(root, p, level);
1229 ret = bin_search(b, key, level, &slot);
1231 if (ret && slot > 0)
1233 p->slots[level] = slot;
1234 if ((p->search_for_split || ins_len > 0) &&
1235 btrfs_header_nritems(b) >=
1236 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1237 int sret = split_node(trans, root, p, level);
1241 b = p->nodes[level];
1242 slot = p->slots[level];
1243 } else if (ins_len < 0) {
1244 int sret = balance_level(trans, root, p,
1248 b = p->nodes[level];
1250 btrfs_release_path(NULL, p);
1253 slot = p->slots[level];
1254 BUG_ON(btrfs_header_nritems(b) == 1);
1256 /* this is only true while dropping a snapshot */
1257 if (level == lowest_level)
1261 reada_for_search(root, p, level, slot,
1264 b = read_node_slot(root, b, slot);
1266 p->slots[level] = slot;
1268 ins_len > btrfs_leaf_free_space(root, b)) {
1269 int sret = split_leaf(trans, root, key,
1270 p, ins_len, ret == 0);
1282 * adjust the pointers going up the tree, starting at level
1283 * making sure the right key of each node is points to 'key'.
1284 * This is used after shifting pointers to the left, so it stops
1285 * fixing up pointers when a given leaf/node is not in slot 0 of the
1288 * If this fails to write a tree block, it returns -1, but continues
1289 * fixing up the blocks in ram so the tree is consistent.
1291 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1292 struct btrfs_root *root, struct btrfs_path *path,
1293 struct btrfs_disk_key *key, int level)
1297 struct extent_buffer *t;
1299 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1300 int tslot = path->slots[i];
1301 if (!path->nodes[i])
1304 btrfs_set_node_key(t, key, tslot);
1305 btrfs_mark_buffer_dirty(path->nodes[i]);
1315 * This function isn't completely safe. It's the caller's responsibility
1316 * that the new key won't break the order
1318 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1319 struct btrfs_root *root, struct btrfs_path *path,
1320 struct btrfs_key *new_key)
1322 struct btrfs_disk_key disk_key;
1323 struct extent_buffer *eb;
1326 eb = path->nodes[0];
1327 slot = path->slots[0];
1329 btrfs_item_key(eb, &disk_key, slot - 1);
1330 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1333 if (slot < btrfs_header_nritems(eb) - 1) {
1334 btrfs_item_key(eb, &disk_key, slot + 1);
1335 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1339 btrfs_cpu_key_to_disk(&disk_key, new_key);
1340 btrfs_set_item_key(eb, &disk_key, slot);
1341 btrfs_mark_buffer_dirty(eb);
1343 fixup_low_keys(trans, root, path, &disk_key, 1);
1348 * try to push data from one node into the next node left in the
1351 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1352 * error, and > 0 if there was no room in the left hand block.
1354 static int push_node_left(struct btrfs_trans_handle *trans,
1355 struct btrfs_root *root, struct extent_buffer *dst,
1356 struct extent_buffer *src, int empty)
1363 src_nritems = btrfs_header_nritems(src);
1364 dst_nritems = btrfs_header_nritems(dst);
1365 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1366 WARN_ON(btrfs_header_generation(src) != trans->transid);
1367 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1369 if (!empty && src_nritems <= 8)
1372 if (push_items <= 0) {
1377 push_items = min(src_nritems, push_items);
1378 if (push_items < src_nritems) {
1379 /* leave at least 8 pointers in the node if
1380 * we aren't going to empty it
1382 if (src_nritems - push_items < 8) {
1383 if (push_items <= 8)
1389 push_items = min(src_nritems - 8, push_items);
1391 copy_extent_buffer(dst, src,
1392 btrfs_node_key_ptr_offset(dst_nritems),
1393 btrfs_node_key_ptr_offset(0),
1394 push_items * sizeof(struct btrfs_key_ptr));
1396 if (push_items < src_nritems) {
1397 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1398 btrfs_node_key_ptr_offset(push_items),
1399 (src_nritems - push_items) *
1400 sizeof(struct btrfs_key_ptr));
1402 btrfs_set_header_nritems(src, src_nritems - push_items);
1403 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1404 btrfs_mark_buffer_dirty(src);
1405 btrfs_mark_buffer_dirty(dst);
1411 * try to push data from one node into the next node right in the
1414 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1415 * error, and > 0 if there was no room in the right hand block.
1417 * this will only push up to 1/2 the contents of the left node over
1419 static int balance_node_right(struct btrfs_trans_handle *trans,
1420 struct btrfs_root *root,
1421 struct extent_buffer *dst,
1422 struct extent_buffer *src)
1430 WARN_ON(btrfs_header_generation(src) != trans->transid);
1431 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1433 src_nritems = btrfs_header_nritems(src);
1434 dst_nritems = btrfs_header_nritems(dst);
1435 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1436 if (push_items <= 0) {
1440 if (src_nritems < 4) {
1444 max_push = src_nritems / 2 + 1;
1445 /* don't try to empty the node */
1446 if (max_push >= src_nritems) {
1450 if (max_push < push_items)
1451 push_items = max_push;
1453 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1454 btrfs_node_key_ptr_offset(0),
1456 sizeof(struct btrfs_key_ptr));
1458 copy_extent_buffer(dst, src,
1459 btrfs_node_key_ptr_offset(0),
1460 btrfs_node_key_ptr_offset(src_nritems - push_items),
1461 push_items * sizeof(struct btrfs_key_ptr));
1463 btrfs_set_header_nritems(src, src_nritems - push_items);
1464 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1466 btrfs_mark_buffer_dirty(src);
1467 btrfs_mark_buffer_dirty(dst);
1473 * helper function to insert a new root level in the tree.
1474 * A new node is allocated, and a single item is inserted to
1475 * point to the existing root
1477 * returns zero on success or < 0 on failure.
1479 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1480 struct btrfs_root *root,
1481 struct btrfs_path *path, int level)
1484 struct extent_buffer *lower;
1485 struct extent_buffer *c;
1486 struct extent_buffer *old;
1487 struct btrfs_disk_key lower_key;
1489 BUG_ON(path->nodes[level]);
1490 BUG_ON(path->nodes[level-1] != root->node);
1492 lower = path->nodes[level-1];
1494 btrfs_item_key(lower, &lower_key, 0);
1496 btrfs_node_key(lower, &lower_key, 0);
1498 c = btrfs_alloc_free_block(trans, root, root->nodesize,
1499 root->root_key.objectid, &lower_key,
1500 level, root->node->start, 0);
1505 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1506 btrfs_set_header_nritems(c, 1);
1507 btrfs_set_header_level(c, level);
1508 btrfs_set_header_bytenr(c, c->start);
1509 btrfs_set_header_generation(c, trans->transid);
1510 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1511 btrfs_set_header_owner(c, root->root_key.objectid);
1513 write_extent_buffer(c, root->fs_info->fsid,
1514 (unsigned long)btrfs_header_fsid(c),
1517 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1518 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1521 btrfs_set_node_key(c, &lower_key, 0);
1522 btrfs_set_node_blockptr(c, 0, lower->start);
1523 lower_gen = btrfs_header_generation(lower);
1524 WARN_ON(lower_gen != trans->transid);
1526 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1528 btrfs_mark_buffer_dirty(c);
1533 /* the super has an extra ref to root->node */
1534 free_extent_buffer(old);
1536 add_root_to_dirty_list(root);
1537 extent_buffer_get(c);
1538 path->nodes[level] = c;
1539 path->slots[level] = 0;
1544 * worker function to insert a single pointer in a node.
1545 * the node should have enough room for the pointer already
1547 * slot and level indicate where you want the key to go, and
1548 * blocknr is the block the key points to.
1550 * returns zero on success and < 0 on any error
1552 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1553 *root, struct btrfs_path *path, struct btrfs_disk_key
1554 *key, u64 bytenr, int slot, int level)
1556 struct extent_buffer *lower;
1559 BUG_ON(!path->nodes[level]);
1560 lower = path->nodes[level];
1561 nritems = btrfs_header_nritems(lower);
1564 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1566 if (slot != nritems) {
1567 memmove_extent_buffer(lower,
1568 btrfs_node_key_ptr_offset(slot + 1),
1569 btrfs_node_key_ptr_offset(slot),
1570 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1572 btrfs_set_node_key(lower, key, slot);
1573 btrfs_set_node_blockptr(lower, slot, bytenr);
1574 WARN_ON(trans->transid == 0);
1575 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1576 btrfs_set_header_nritems(lower, nritems + 1);
1577 btrfs_mark_buffer_dirty(lower);
1582 * split the node at the specified level in path in two.
1583 * The path is corrected to point to the appropriate node after the split
1585 * Before splitting this tries to make some room in the node by pushing
1586 * left and right, if either one works, it returns right away.
1588 * returns 0 on success and < 0 on failure
1590 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1591 *root, struct btrfs_path *path, int level)
1593 struct extent_buffer *c;
1594 struct extent_buffer *split;
1595 struct btrfs_disk_key disk_key;
1601 c = path->nodes[level];
1602 WARN_ON(btrfs_header_generation(c) != trans->transid);
1603 if (c == root->node) {
1604 /* trying to split the root, lets make a new one */
1605 ret = insert_new_root(trans, root, path, level + 1);
1609 ret = push_nodes_for_insert(trans, root, path, level);
1610 c = path->nodes[level];
1611 if (!ret && btrfs_header_nritems(c) <
1612 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1618 c_nritems = btrfs_header_nritems(c);
1619 mid = (c_nritems + 1) / 2;
1620 btrfs_node_key(c, &disk_key, mid);
1622 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1623 root->root_key.objectid,
1624 &disk_key, level, c->start, 0);
1626 return PTR_ERR(split);
1628 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1629 btrfs_set_header_level(split, btrfs_header_level(c));
1630 btrfs_set_header_bytenr(split, split->start);
1631 btrfs_set_header_generation(split, trans->transid);
1632 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1633 btrfs_set_header_owner(split, root->root_key.objectid);
1634 write_extent_buffer(split, root->fs_info->fsid,
1635 (unsigned long)btrfs_header_fsid(split),
1637 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1638 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1642 copy_extent_buffer(split, c,
1643 btrfs_node_key_ptr_offset(0),
1644 btrfs_node_key_ptr_offset(mid),
1645 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1646 btrfs_set_header_nritems(split, c_nritems - mid);
1647 btrfs_set_header_nritems(c, mid);
1650 btrfs_mark_buffer_dirty(c);
1651 btrfs_mark_buffer_dirty(split);
1653 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1654 path->slots[level + 1] + 1,
1659 if (path->slots[level] >= mid) {
1660 path->slots[level] -= mid;
1661 free_extent_buffer(c);
1662 path->nodes[level] = split;
1663 path->slots[level + 1] += 1;
1665 free_extent_buffer(split);
1671 * how many bytes are required to store the items in a leaf. start
1672 * and nr indicate which items in the leaf to check. This totals up the
1673 * space used both by the item structs and the item data
1675 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1678 int nritems = btrfs_header_nritems(l);
1679 int end = min(nritems, start + nr) - 1;
1683 data_len = btrfs_item_end_nr(l, start);
1684 data_len = data_len - btrfs_item_offset_nr(l, end);
1685 data_len += sizeof(struct btrfs_item) * nr;
1686 WARN_ON(data_len < 0);
1691 * The space between the end of the leaf items and
1692 * the start of the leaf data. IOW, how much room
1693 * the leaf has left for both items and data
1695 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1697 int nritems = btrfs_header_nritems(leaf);
1699 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1701 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1702 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1703 leaf_space_used(leaf, 0, nritems), nritems);
1709 * push some data in the path leaf to the right, trying to free up at
1710 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1712 * returns 1 if the push failed because the other node didn't have enough
1713 * room, 0 if everything worked out and < 0 if there were major errors.
1715 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1716 *root, struct btrfs_path *path, int data_size,
1719 struct extent_buffer *left = path->nodes[0];
1720 struct extent_buffer *right;
1721 struct extent_buffer *upper;
1722 struct btrfs_disk_key disk_key;
1728 struct btrfs_item *item;
1736 slot = path->slots[1];
1737 if (!path->nodes[1]) {
1740 upper = path->nodes[1];
1741 if (slot >= btrfs_header_nritems(upper) - 1)
1744 right = read_node_slot(root, upper, slot + 1);
1745 free_space = btrfs_leaf_free_space(root, right);
1746 if (free_space < data_size) {
1747 free_extent_buffer(right);
1751 /* cow and double check */
1752 ret = btrfs_cow_block(trans, root, right, upper,
1755 free_extent_buffer(right);
1758 free_space = btrfs_leaf_free_space(root, right);
1759 if (free_space < data_size) {
1760 free_extent_buffer(right);
1764 left_nritems = btrfs_header_nritems(left);
1765 if (left_nritems == 0) {
1766 free_extent_buffer(right);
1775 i = left_nritems - 1;
1777 item = btrfs_item_nr(left, i);
1779 if (path->slots[0] == i)
1780 push_space += data_size + sizeof(*item);
1782 this_item_size = btrfs_item_size(left, item);
1783 if (this_item_size + sizeof(*item) + push_space > free_space)
1786 push_space += this_item_size + sizeof(*item);
1792 if (push_items == 0) {
1793 free_extent_buffer(right);
1797 if (!empty && push_items == left_nritems)
1800 /* push left to right */
1801 right_nritems = btrfs_header_nritems(right);
1803 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1804 push_space -= leaf_data_end(root, left);
1806 /* make room in the right data area */
1807 data_end = leaf_data_end(root, right);
1808 memmove_extent_buffer(right,
1809 btrfs_leaf_data(right) + data_end - push_space,
1810 btrfs_leaf_data(right) + data_end,
1811 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1813 /* copy from the left data area */
1814 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1815 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1816 btrfs_leaf_data(left) + leaf_data_end(root, left),
1819 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1820 btrfs_item_nr_offset(0),
1821 right_nritems * sizeof(struct btrfs_item));
1823 /* copy the items from left to right */
1824 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1825 btrfs_item_nr_offset(left_nritems - push_items),
1826 push_items * sizeof(struct btrfs_item));
1828 /* update the item pointers */
1829 right_nritems += push_items;
1830 btrfs_set_header_nritems(right, right_nritems);
1831 push_space = BTRFS_LEAF_DATA_SIZE(root);
1832 for (i = 0; i < right_nritems; i++) {
1833 item = btrfs_item_nr(right, i);
1834 push_space -= btrfs_item_size(right, item);
1835 btrfs_set_item_offset(right, item, push_space);
1838 left_nritems -= push_items;
1839 btrfs_set_header_nritems(left, left_nritems);
1842 btrfs_mark_buffer_dirty(left);
1843 btrfs_mark_buffer_dirty(right);
1845 btrfs_item_key(right, &disk_key, 0);
1846 btrfs_set_node_key(upper, &disk_key, slot + 1);
1847 btrfs_mark_buffer_dirty(upper);
1849 /* then fixup the leaf pointer in the path */
1850 if (path->slots[0] >= left_nritems) {
1851 path->slots[0] -= left_nritems;
1852 free_extent_buffer(path->nodes[0]);
1853 path->nodes[0] = right;
1854 path->slots[1] += 1;
1856 free_extent_buffer(right);
1861 * push some data in the path leaf to the left, trying to free up at
1862 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1864 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1865 *root, struct btrfs_path *path, int data_size,
1868 struct btrfs_disk_key disk_key;
1869 struct extent_buffer *right = path->nodes[0];
1870 struct extent_buffer *left;
1876 struct btrfs_item *item;
1877 u32 old_left_nritems;
1883 u32 old_left_item_size;
1885 slot = path->slots[1];
1888 if (!path->nodes[1])
1891 right_nritems = btrfs_header_nritems(right);
1892 if (right_nritems == 0) {
1896 left = read_node_slot(root, path->nodes[1], slot - 1);
1897 free_space = btrfs_leaf_free_space(root, left);
1898 if (free_space < data_size) {
1899 free_extent_buffer(left);
1903 /* cow and double check */
1904 ret = btrfs_cow_block(trans, root, left,
1905 path->nodes[1], slot - 1, &left);
1907 /* we hit -ENOSPC, but it isn't fatal here */
1908 free_extent_buffer(left);
1912 free_space = btrfs_leaf_free_space(root, left);
1913 if (free_space < data_size) {
1914 free_extent_buffer(left);
1921 nr = right_nritems - 1;
1923 for (i = 0; i < nr; i++) {
1924 item = btrfs_item_nr(right, i);
1926 if (path->slots[0] == i)
1927 push_space += data_size + sizeof(*item);
1929 this_item_size = btrfs_item_size(right, item);
1930 if (this_item_size + sizeof(*item) + push_space > free_space)
1934 push_space += this_item_size + sizeof(*item);
1937 if (push_items == 0) {
1938 free_extent_buffer(left);
1941 if (!empty && push_items == btrfs_header_nritems(right))
1944 /* push data from right to left */
1945 copy_extent_buffer(left, right,
1946 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1947 btrfs_item_nr_offset(0),
1948 push_items * sizeof(struct btrfs_item));
1950 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1951 btrfs_item_offset_nr(right, push_items -1);
1953 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1954 leaf_data_end(root, left) - push_space,
1955 btrfs_leaf_data(right) +
1956 btrfs_item_offset_nr(right, push_items - 1),
1958 old_left_nritems = btrfs_header_nritems(left);
1959 BUG_ON(old_left_nritems < 0);
1961 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1962 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1965 item = btrfs_item_nr(left, i);
1966 ioff = btrfs_item_offset(left, item);
1967 btrfs_set_item_offset(left, item,
1968 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1970 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1972 /* fixup right node */
1973 if (push_items > right_nritems) {
1974 printk("push items %d nr %u\n", push_items, right_nritems);
1978 if (push_items < right_nritems) {
1979 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1980 leaf_data_end(root, right);
1981 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1982 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1983 btrfs_leaf_data(right) +
1984 leaf_data_end(root, right), push_space);
1986 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1987 btrfs_item_nr_offset(push_items),
1988 (btrfs_header_nritems(right) - push_items) *
1989 sizeof(struct btrfs_item));
1991 right_nritems -= push_items;
1992 btrfs_set_header_nritems(right, right_nritems);
1993 push_space = BTRFS_LEAF_DATA_SIZE(root);
1994 for (i = 0; i < right_nritems; i++) {
1995 item = btrfs_item_nr(right, i);
1996 push_space = push_space - btrfs_item_size(right, item);
1997 btrfs_set_item_offset(right, item, push_space);
2000 btrfs_mark_buffer_dirty(left);
2002 btrfs_mark_buffer_dirty(right);
2004 btrfs_item_key(right, &disk_key, 0);
2005 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2009 /* then fixup the leaf pointer in the path */
2010 if (path->slots[0] < push_items) {
2011 path->slots[0] += old_left_nritems;
2012 free_extent_buffer(path->nodes[0]);
2013 path->nodes[0] = left;
2014 path->slots[1] -= 1;
2016 free_extent_buffer(left);
2017 path->slots[0] -= push_items;
2019 BUG_ON(path->slots[0] < 0);
2024 * split the path's leaf in two, making sure there is at least data_size
2025 * available for the resulting leaf level of the path.
2027 * returns 0 if all went well and < 0 on failure.
2029 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2030 struct btrfs_root *root,
2031 struct btrfs_path *path,
2032 struct extent_buffer *l,
2033 struct extent_buffer *right,
2034 int slot, int mid, int nritems)
2041 struct btrfs_disk_key disk_key;
2043 nritems = nritems - mid;
2044 btrfs_set_header_nritems(right, nritems);
2045 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2047 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2048 btrfs_item_nr_offset(mid),
2049 nritems * sizeof(struct btrfs_item));
2051 copy_extent_buffer(right, l,
2052 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2053 data_copy_size, btrfs_leaf_data(l) +
2054 leaf_data_end(root, l), data_copy_size);
2056 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2057 btrfs_item_end_nr(l, mid);
2059 for (i = 0; i < nritems; i++) {
2060 struct btrfs_item *item = btrfs_item_nr(right, i);
2061 u32 ioff = btrfs_item_offset(right, item);
2062 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2065 btrfs_set_header_nritems(l, mid);
2067 btrfs_item_key(right, &disk_key, 0);
2068 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2069 path->slots[1] + 1, 1);
2073 btrfs_mark_buffer_dirty(right);
2074 btrfs_mark_buffer_dirty(l);
2075 BUG_ON(path->slots[0] != slot);
2078 free_extent_buffer(path->nodes[0]);
2079 path->nodes[0] = right;
2080 path->slots[0] -= mid;
2081 path->slots[1] += 1;
2083 free_extent_buffer(right);
2086 BUG_ON(path->slots[0] < 0);
2092 * split the path's leaf in two, making sure there is at least data_size
2093 * available for the resulting leaf level of the path.
2095 * returns 0 if all went well and < 0 on failure.
2097 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2098 struct btrfs_root *root,
2099 struct btrfs_key *ins_key,
2100 struct btrfs_path *path, int data_size,
2103 struct btrfs_disk_key disk_key;
2104 struct extent_buffer *l;
2108 struct extent_buffer *right;
2112 int num_doubles = 0;
2114 /* first try to make some room by pushing left and right */
2115 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2116 wret = push_leaf_right(trans, root, path, data_size, 0);
2120 wret = push_leaf_left(trans, root, path, data_size, 0);
2126 /* did the pushes work? */
2127 if (btrfs_leaf_free_space(root, l) >= data_size)
2131 if (!path->nodes[1]) {
2132 ret = insert_new_root(trans, root, path, 1);
2139 slot = path->slots[0];
2140 nritems = btrfs_header_nritems(l);
2141 mid = (nritems + 1) / 2;
2145 leaf_space_used(l, mid, nritems - mid) + data_size >
2146 BTRFS_LEAF_DATA_SIZE(root)) {
2147 if (slot >= nritems) {
2151 if (mid != nritems &&
2152 leaf_space_used(l, mid, nritems - mid) +
2153 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2159 if (leaf_space_used(l, 0, mid) + data_size >
2160 BTRFS_LEAF_DATA_SIZE(root)) {
2161 if (!extend && data_size && slot == 0) {
2163 } else if ((extend || !data_size) && slot == 0) {
2167 if (mid != nritems &&
2168 leaf_space_used(l, mid, nritems - mid) +
2169 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2177 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2179 btrfs_item_key(l, &disk_key, mid);
2181 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2182 root->root_key.objectid,
2183 &disk_key, 0, l->start, 0);
2184 if (IS_ERR(right)) {
2186 return PTR_ERR(right);
2189 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2190 btrfs_set_header_bytenr(right, right->start);
2191 btrfs_set_header_generation(right, trans->transid);
2192 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2193 btrfs_set_header_owner(right, root->root_key.objectid);
2194 btrfs_set_header_level(right, 0);
2195 write_extent_buffer(right, root->fs_info->fsid,
2196 (unsigned long)btrfs_header_fsid(right),
2199 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2200 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2205 btrfs_set_header_nritems(right, 0);
2206 wret = insert_ptr(trans, root, path,
2207 &disk_key, right->start,
2208 path->slots[1] + 1, 1);
2212 free_extent_buffer(path->nodes[0]);
2213 path->nodes[0] = right;
2215 path->slots[1] += 1;
2217 btrfs_set_header_nritems(right, 0);
2218 wret = insert_ptr(trans, root, path,
2224 free_extent_buffer(path->nodes[0]);
2225 path->nodes[0] = right;
2227 if (path->slots[1] == 0) {
2228 wret = fixup_low_keys(trans, root,
2229 path, &disk_key, 1);
2234 btrfs_mark_buffer_dirty(right);
2238 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2242 BUG_ON(num_doubles != 0);
2251 * This function splits a single item into two items,
2252 * giving 'new_key' to the new item and splitting the
2253 * old one at split_offset (from the start of the item).
2255 * The path may be released by this operation. After
2256 * the split, the path is pointing to the old item. The
2257 * new item is going to be in the same node as the old one.
2259 * Note, the item being split must be smaller enough to live alone on
2260 * a tree block with room for one extra struct btrfs_item
2262 * This allows us to split the item in place, keeping a lock on the
2263 * leaf the entire time.
2265 int btrfs_split_item(struct btrfs_trans_handle *trans,
2266 struct btrfs_root *root,
2267 struct btrfs_path *path,
2268 struct btrfs_key *new_key,
2269 unsigned long split_offset)
2272 struct extent_buffer *leaf;
2273 struct btrfs_key orig_key;
2274 struct btrfs_item *item;
2275 struct btrfs_item *new_item;
2280 struct btrfs_disk_key disk_key;
2283 leaf = path->nodes[0];
2284 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2285 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2288 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2289 btrfs_release_path(root, path);
2291 path->search_for_split = 1;
2293 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2294 path->search_for_split = 0;
2296 /* if our item isn't there or got smaller, return now */
2297 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2302 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2305 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2306 leaf = path->nodes[0];
2309 item = btrfs_item_nr(leaf, path->slots[0]);
2310 orig_offset = btrfs_item_offset(leaf, item);
2311 item_size = btrfs_item_size(leaf, item);
2314 buf = kmalloc(item_size, GFP_NOFS);
2315 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2316 path->slots[0]), item_size);
2317 slot = path->slots[0] + 1;
2318 leaf = path->nodes[0];
2320 nritems = btrfs_header_nritems(leaf);
2322 if (slot != nritems) {
2323 /* shift the items */
2324 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2325 btrfs_item_nr_offset(slot),
2326 (nritems - slot) * sizeof(struct btrfs_item));
2330 btrfs_cpu_key_to_disk(&disk_key, new_key);
2331 btrfs_set_item_key(leaf, &disk_key, slot);
2333 new_item = btrfs_item_nr(leaf, slot);
2335 btrfs_set_item_offset(leaf, new_item, orig_offset);
2336 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2338 btrfs_set_item_offset(leaf, item,
2339 orig_offset + item_size - split_offset);
2340 btrfs_set_item_size(leaf, item, split_offset);
2342 btrfs_set_header_nritems(leaf, nritems + 1);
2344 /* write the data for the start of the original item */
2345 write_extent_buffer(leaf, buf,
2346 btrfs_item_ptr_offset(leaf, path->slots[0]),
2349 /* write the data for the new item */
2350 write_extent_buffer(leaf, buf + split_offset,
2351 btrfs_item_ptr_offset(leaf, slot),
2352 item_size - split_offset);
2353 btrfs_mark_buffer_dirty(leaf);
2356 if (btrfs_leaf_free_space(root, leaf) < 0) {
2357 btrfs_print_leaf(root, leaf);
2364 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2365 struct btrfs_root *root,
2366 struct btrfs_path *path,
2367 u32 new_size, int from_end)
2371 struct extent_buffer *leaf;
2372 struct btrfs_item *item;
2374 unsigned int data_end;
2375 unsigned int old_data_start;
2376 unsigned int old_size;
2377 unsigned int size_diff;
2380 leaf = path->nodes[0];
2381 slot = path->slots[0];
2383 old_size = btrfs_item_size_nr(leaf, slot);
2384 if (old_size == new_size)
2387 nritems = btrfs_header_nritems(leaf);
2388 data_end = leaf_data_end(root, leaf);
2390 old_data_start = btrfs_item_offset_nr(leaf, slot);
2392 size_diff = old_size - new_size;
2395 BUG_ON(slot >= nritems);
2398 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2400 /* first correct the data pointers */
2401 for (i = slot; i < nritems; i++) {
2403 item = btrfs_item_nr(leaf, i);
2404 ioff = btrfs_item_offset(leaf, item);
2405 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2408 /* shift the data */
2410 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2411 data_end + size_diff, btrfs_leaf_data(leaf) +
2412 data_end, old_data_start + new_size - data_end);
2414 struct btrfs_disk_key disk_key;
2417 btrfs_item_key(leaf, &disk_key, slot);
2419 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2421 struct btrfs_file_extent_item *fi;
2423 fi = btrfs_item_ptr(leaf, slot,
2424 struct btrfs_file_extent_item);
2425 fi = (struct btrfs_file_extent_item *)(
2426 (unsigned long)fi - size_diff);
2428 if (btrfs_file_extent_type(leaf, fi) ==
2429 BTRFS_FILE_EXTENT_INLINE) {
2430 ptr = btrfs_item_ptr_offset(leaf, slot);
2431 memmove_extent_buffer(leaf, ptr,
2433 offsetof(struct btrfs_file_extent_item,
2438 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2439 data_end + size_diff, btrfs_leaf_data(leaf) +
2440 data_end, old_data_start - data_end);
2442 offset = btrfs_disk_key_offset(&disk_key);
2443 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2444 btrfs_set_item_key(leaf, &disk_key, slot);
2446 fixup_low_keys(trans, root, path, &disk_key, 1);
2449 item = btrfs_item_nr(leaf, slot);
2450 btrfs_set_item_size(leaf, item, new_size);
2451 btrfs_mark_buffer_dirty(leaf);
2454 if (btrfs_leaf_free_space(root, leaf) < 0) {
2455 btrfs_print_leaf(root, leaf);
2461 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2462 struct btrfs_root *root, struct btrfs_path *path,
2467 struct extent_buffer *leaf;
2468 struct btrfs_item *item;
2470 unsigned int data_end;
2471 unsigned int old_data;
2472 unsigned int old_size;
2475 leaf = path->nodes[0];
2477 nritems = btrfs_header_nritems(leaf);
2478 data_end = leaf_data_end(root, leaf);
2480 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2481 btrfs_print_leaf(root, leaf);
2484 slot = path->slots[0];
2485 old_data = btrfs_item_end_nr(leaf, slot);
2488 if (slot >= nritems) {
2489 btrfs_print_leaf(root, leaf);
2490 printk("slot %d too large, nritems %d\n", slot, nritems);
2495 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2497 /* first correct the data pointers */
2498 for (i = slot; i < nritems; i++) {
2500 item = btrfs_item_nr(leaf, i);
2501 ioff = btrfs_item_offset(leaf, item);
2502 btrfs_set_item_offset(leaf, item, ioff - data_size);
2505 /* shift the data */
2506 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2507 data_end - data_size, btrfs_leaf_data(leaf) +
2508 data_end, old_data - data_end);
2510 data_end = old_data;
2511 old_size = btrfs_item_size_nr(leaf, slot);
2512 item = btrfs_item_nr(leaf, slot);
2513 btrfs_set_item_size(leaf, item, old_size + data_size);
2514 btrfs_mark_buffer_dirty(leaf);
2517 if (btrfs_leaf_free_space(root, leaf) < 0) {
2518 btrfs_print_leaf(root, leaf);
2525 * Given a key and some data, insert an item into the tree.
2526 * This does all the path init required, making room in the tree if needed.
2528 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2529 struct btrfs_root *root,
2530 struct btrfs_path *path,
2531 struct btrfs_key *cpu_key, u32 *data_size,
2534 struct extent_buffer *leaf;
2535 struct btrfs_item *item;
2542 unsigned int data_end;
2543 struct btrfs_disk_key disk_key;
2545 for (i = 0; i < nr; i++) {
2546 total_data += data_size[i];
2549 /* create a root if there isn't one */
2553 total_size = total_data + nr * sizeof(struct btrfs_item);
2554 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2561 leaf = path->nodes[0];
2563 nritems = btrfs_header_nritems(leaf);
2564 data_end = leaf_data_end(root, leaf);
2566 if (btrfs_leaf_free_space(root, leaf) < total_size) {
2567 btrfs_print_leaf(root, leaf);
2568 printk("not enough freespace need %u have %d\n",
2569 total_size, btrfs_leaf_free_space(root, leaf));
2573 slot = path->slots[0];
2576 if (slot != nritems) {
2578 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2580 if (old_data < data_end) {
2581 btrfs_print_leaf(root, leaf);
2582 printk("slot %d old_data %d data_end %d\n",
2583 slot, old_data, data_end);
2587 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2589 /* first correct the data pointers */
2590 for (i = slot; i < nritems; i++) {
2593 item = btrfs_item_nr(leaf, i);
2594 ioff = btrfs_item_offset(leaf, item);
2595 btrfs_set_item_offset(leaf, item, ioff - total_data);
2598 /* shift the items */
2599 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2600 btrfs_item_nr_offset(slot),
2601 (nritems - slot) * sizeof(struct btrfs_item));
2603 /* shift the data */
2604 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2605 data_end - total_data, btrfs_leaf_data(leaf) +
2606 data_end, old_data - data_end);
2607 data_end = old_data;
2610 /* setup the item for the new data */
2611 for (i = 0; i < nr; i++) {
2612 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2613 btrfs_set_item_key(leaf, &disk_key, slot + i);
2614 item = btrfs_item_nr(leaf, slot + i);
2615 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2616 data_end -= data_size[i];
2617 btrfs_set_item_size(leaf, item, data_size[i]);
2619 btrfs_set_header_nritems(leaf, nritems + nr);
2620 btrfs_mark_buffer_dirty(leaf);
2624 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2625 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2628 if (btrfs_leaf_free_space(root, leaf) < 0) {
2629 btrfs_print_leaf(root, leaf);
2638 * Given a key and some data, insert an item into the tree.
2639 * This does all the path init required, making room in the tree if needed.
2641 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2642 *root, struct btrfs_key *cpu_key, void *data, u32
2646 struct btrfs_path *path;
2647 struct extent_buffer *leaf;
2650 path = btrfs_alloc_path();
2652 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2654 leaf = path->nodes[0];
2655 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2656 write_extent_buffer(leaf, data, ptr, data_size);
2657 btrfs_mark_buffer_dirty(leaf);
2659 btrfs_free_path(path);
2664 * delete the pointer from a given node.
2666 * If the delete empties a node, the node is removed from the tree,
2667 * continuing all the way the root if required. The root is converted into
2668 * a leaf if all the nodes are emptied.
2670 int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2671 struct btrfs_path *path, int level, int slot)
2673 struct extent_buffer *parent = path->nodes[level];
2678 nritems = btrfs_header_nritems(parent);
2679 if (slot != nritems -1) {
2680 memmove_extent_buffer(parent,
2681 btrfs_node_key_ptr_offset(slot),
2682 btrfs_node_key_ptr_offset(slot + 1),
2683 sizeof(struct btrfs_key_ptr) *
2684 (nritems - slot - 1));
2687 btrfs_set_header_nritems(parent, nritems);
2688 if (nritems == 0 && parent == root->node) {
2689 BUG_ON(btrfs_header_level(root->node) != 1);
2690 /* just turn the root into a leaf and break */
2691 btrfs_set_header_level(root->node, 0);
2692 } else if (slot == 0) {
2693 struct btrfs_disk_key disk_key;
2695 btrfs_node_key(parent, &disk_key, 0);
2696 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2700 btrfs_mark_buffer_dirty(parent);
2705 * a helper function to delete the leaf pointed to by path->slots[1] and
2708 * This deletes the pointer in path->nodes[1] and frees the leaf
2709 * block extent. zero is returned if it all worked out, < 0 otherwise.
2711 * The path must have already been setup for deleting the leaf, including
2712 * all the proper balancing. path->nodes[1] must be locked.
2714 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2715 struct btrfs_root *root,
2716 struct btrfs_path *path,
2717 struct extent_buffer *leaf)
2721 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2722 ret = btrfs_del_ptr(trans, root, path, 1, path->slots[1]);
2726 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2727 0, root->root_key.objectid, 0, 0);
2732 * delete the item at the leaf level in path. If that empties
2733 * the leaf, remove it from the tree
2735 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2736 struct btrfs_path *path, int slot, int nr)
2738 struct extent_buffer *leaf;
2739 struct btrfs_item *item;
2747 leaf = path->nodes[0];
2748 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2750 for (i = 0; i < nr; i++)
2751 dsize += btrfs_item_size_nr(leaf, slot + i);
2753 nritems = btrfs_header_nritems(leaf);
2755 if (slot + nr != nritems) {
2757 int data_end = leaf_data_end(root, leaf);
2759 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2761 btrfs_leaf_data(leaf) + data_end,
2762 last_off - data_end);
2764 for (i = slot + nr; i < nritems; i++) {
2767 item = btrfs_item_nr(leaf, i);
2768 ioff = btrfs_item_offset(leaf, item);
2769 btrfs_set_item_offset(leaf, item, ioff + dsize);
2772 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2773 btrfs_item_nr_offset(slot + nr),
2774 sizeof(struct btrfs_item) *
2775 (nritems - slot - nr));
2777 btrfs_set_header_nritems(leaf, nritems - nr);
2780 /* delete the leaf if we've emptied it */
2782 if (leaf == root->node) {
2783 btrfs_set_header_level(leaf, 0);
2785 clean_tree_block(trans, root, leaf);
2786 wait_on_tree_block_writeback(root, leaf);
2788 wret = btrfs_del_leaf(trans, root, path, leaf);
2794 int used = leaf_space_used(leaf, 0, nritems);
2796 struct btrfs_disk_key disk_key;
2798 btrfs_item_key(leaf, &disk_key, 0);
2799 wret = fixup_low_keys(trans, root, path,
2805 /* delete the leaf if it is mostly empty */
2806 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2807 /* push_leaf_left fixes the path.
2808 * make sure the path still points to our leaf
2809 * for possible call to del_ptr below
2811 slot = path->slots[1];
2812 extent_buffer_get(leaf);
2814 wret = push_leaf_left(trans, root, path, 1, 1);
2815 if (wret < 0 && wret != -ENOSPC)
2818 if (path->nodes[0] == leaf &&
2819 btrfs_header_nritems(leaf)) {
2820 wret = push_leaf_right(trans, root, path, 1, 1);
2821 if (wret < 0 && wret != -ENOSPC)
2825 if (btrfs_header_nritems(leaf) == 0) {
2826 clean_tree_block(trans, root, leaf);
2827 wait_on_tree_block_writeback(root, leaf);
2829 path->slots[1] = slot;
2830 ret = btrfs_del_leaf(trans, root, path, leaf);
2832 free_extent_buffer(leaf);
2835 btrfs_mark_buffer_dirty(leaf);
2836 free_extent_buffer(leaf);
2839 btrfs_mark_buffer_dirty(leaf);
2846 * walk up the tree as far as required to find the previous leaf.
2847 * returns 0 if it found something or 1 if there are no lesser leaves.
2848 * returns < 0 on io errors.
2850 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2854 struct extent_buffer *c;
2855 struct extent_buffer *next = NULL;
2857 while(level < BTRFS_MAX_LEVEL) {
2858 if (!path->nodes[level])
2861 slot = path->slots[level];
2862 c = path->nodes[level];
2865 if (level == BTRFS_MAX_LEVEL)
2872 free_extent_buffer(next);
2874 next = read_node_slot(root, c, slot);
2877 path->slots[level] = slot;
2880 c = path->nodes[level];
2881 free_extent_buffer(c);
2882 slot = btrfs_header_nritems(next);
2885 path->nodes[level] = next;
2886 path->slots[level] = slot;
2889 next = read_node_slot(root, next, slot);
2895 * walk up the tree as far as required to find the next leaf.
2896 * returns 0 if it found something or 1 if there are no greater leaves.
2897 * returns < 0 on io errors.
2899 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2903 struct extent_buffer *c;
2904 struct extent_buffer *next = NULL;
2906 while(level < BTRFS_MAX_LEVEL) {
2907 if (!path->nodes[level])
2910 slot = path->slots[level] + 1;
2911 c = path->nodes[level];
2912 if (slot >= btrfs_header_nritems(c)) {
2914 if (level == BTRFS_MAX_LEVEL)
2920 free_extent_buffer(next);
2923 reada_for_search(root, path, level, slot, 0);
2925 next = read_node_slot(root, c, slot);
2930 path->slots[level] = slot;
2933 c = path->nodes[level];
2934 free_extent_buffer(c);
2935 path->nodes[level] = next;
2936 path->slots[level] = 0;
2940 reada_for_search(root, path, level, 0, 0);
2941 next = read_node_slot(root, next, 0);
2948 int btrfs_previous_item(struct btrfs_root *root,
2949 struct btrfs_path *path, u64 min_objectid,
2952 struct btrfs_key found_key;
2953 struct extent_buffer *leaf;
2957 if (path->slots[0] == 0) {
2958 ret = btrfs_prev_leaf(root, path);
2964 leaf = path->nodes[0];
2965 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2966 if (found_key.type == type)