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"
27 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30 *root, struct btrfs_key *ins_key,
31 struct btrfs_path *path, int data_size, int extend);
32 static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
34 struct extent_buffer *src, int empty);
35 static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
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 = kzalloc(sizeof(struct btrfs_path), GFP_NOFS);
52 void btrfs_free_path(struct btrfs_path *p)
56 btrfs_release_path(p);
60 void btrfs_release_path(struct btrfs_path *p)
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
66 free_extent_buffer(p->nodes[i]);
68 memset(p, 0, sizeof(*p));
71 void add_root_to_dirty_list(struct btrfs_root *root)
73 if (root->track_dirty && list_empty(&root->dirty_list)) {
74 list_add(&root->dirty_list,
75 &root->fs_info->dirty_cowonly_roots);
79 int btrfs_copy_root(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 struct extent_buffer *buf,
82 struct extent_buffer **cow_ret, u64 new_root_objectid)
84 struct extent_buffer *cow;
87 struct btrfs_root *new_root;
88 struct btrfs_disk_key disk_key;
90 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
94 memcpy(new_root, root, sizeof(*new_root));
95 new_root->root_key.objectid = new_root_objectid;
97 WARN_ON(root->ref_cows && trans->transid !=
98 root->fs_info->running_transaction->transid);
99 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
101 level = btrfs_header_level(buf);
103 btrfs_item_key(buf, &disk_key, 0);
105 btrfs_node_key(buf, &disk_key, 0);
106 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
107 new_root_objectid, &disk_key,
108 level, buf->start, 0);
114 copy_extent_buffer(cow, buf, 0, 0, cow->len);
115 btrfs_set_header_bytenr(cow, cow->start);
116 btrfs_set_header_generation(cow, trans->transid);
117 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
118 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
119 BTRFS_HEADER_FLAG_RELOC);
120 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
121 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
123 btrfs_set_header_owner(cow, new_root_objectid);
125 write_extent_buffer(cow, root->fs_info->fsid,
126 btrfs_header_fsid(), BTRFS_FSID_SIZE);
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);
141 * check if the tree block can be shared by multiple trees
143 static int btrfs_block_can_be_shared(struct btrfs_root *root,
144 struct extent_buffer *buf)
147 * Tree blocks not in reference counted trees and tree roots
148 * are never shared. If a block was allocated after the last
149 * snapshot and the block was not allocated by tree relocation,
150 * we know the block is not shared.
152 if (root->ref_cows &&
153 buf != root->node && buf != root->commit_root &&
154 (btrfs_header_generation(buf) <=
155 btrfs_root_last_snapshot(&root->root_item) ||
156 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
158 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
159 if (root->ref_cows &&
160 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
166 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
167 struct btrfs_root *root,
168 struct extent_buffer *buf,
169 struct extent_buffer *cow)
178 * Backrefs update rules:
180 * Always use full backrefs for extent pointers in tree block
181 * allocated by tree relocation.
183 * If a shared tree block is no longer referenced by its owner
184 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
185 * use full backrefs for extent pointers in tree block.
187 * If a tree block is been relocating
188 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
189 * use full backrefs for extent pointers in tree block.
190 * The reason for this is some operations (such as drop tree)
191 * are only allowed for blocks use full backrefs.
194 if (btrfs_block_can_be_shared(root, buf)) {
195 ret = btrfs_lookup_extent_info(trans, root, buf->start,
196 btrfs_header_level(buf), 1,
202 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
203 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
204 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
209 owner = btrfs_header_owner(buf);
210 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
211 owner == BTRFS_TREE_RELOC_OBJECTID);
214 if ((owner == root->root_key.objectid ||
215 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
216 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
217 ret = btrfs_inc_ref(trans, root, buf, 1);
220 if (root->root_key.objectid ==
221 BTRFS_TREE_RELOC_OBJECTID) {
222 ret = btrfs_dec_ref(trans, root, buf, 0);
224 ret = btrfs_inc_ref(trans, root, cow, 1);
227 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
230 if (root->root_key.objectid ==
231 BTRFS_TREE_RELOC_OBJECTID)
232 ret = btrfs_inc_ref(trans, root, cow, 1);
234 ret = btrfs_inc_ref(trans, root, cow, 0);
237 if (new_flags != 0) {
238 ret = btrfs_set_block_flags(trans, root, buf->start,
239 btrfs_header_level(buf),
244 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
245 if (root->root_key.objectid ==
246 BTRFS_TREE_RELOC_OBJECTID)
247 ret = btrfs_inc_ref(trans, root, cow, 1);
249 ret = btrfs_inc_ref(trans, root, cow, 0);
251 ret = btrfs_dec_ref(trans, root, buf, 1);
254 clean_tree_block(trans, root, buf);
259 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root,
261 struct extent_buffer *buf,
262 struct extent_buffer *parent, int parent_slot,
263 struct extent_buffer **cow_ret,
264 u64 search_start, u64 empty_size)
266 struct extent_buffer *cow;
267 struct btrfs_disk_key disk_key;
270 WARN_ON(root->ref_cows && trans->transid !=
271 root->fs_info->running_transaction->transid);
272 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
274 level = btrfs_header_level(buf);
277 btrfs_item_key(buf, &disk_key, 0);
279 btrfs_node_key(buf, &disk_key, 0);
281 cow = btrfs_alloc_free_block(trans, root, buf->len,
282 root->root_key.objectid, &disk_key,
283 level, search_start, empty_size);
287 copy_extent_buffer(cow, buf, 0, 0, cow->len);
288 btrfs_set_header_bytenr(cow, cow->start);
289 btrfs_set_header_generation(cow, trans->transid);
290 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
291 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
292 BTRFS_HEADER_FLAG_RELOC);
293 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
294 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
296 btrfs_set_header_owner(cow, root->root_key.objectid);
298 write_extent_buffer(cow, root->fs_info->fsid,
299 btrfs_header_fsid(), BTRFS_FSID_SIZE);
301 WARN_ON(!(buf->flags & EXTENT_BAD_TRANSID) &&
302 btrfs_header_generation(buf) > trans->transid);
304 update_ref_for_cow(trans, root, buf, cow);
306 if (buf == root->node) {
308 extent_buffer_get(cow);
310 btrfs_free_extent(trans, root, buf->start, buf->len,
311 0, root->root_key.objectid, level, 0);
312 free_extent_buffer(buf);
313 add_root_to_dirty_list(root);
315 btrfs_set_node_blockptr(parent, parent_slot,
317 WARN_ON(trans->transid == 0);
318 btrfs_set_node_ptr_generation(parent, parent_slot,
320 btrfs_mark_buffer_dirty(parent);
321 WARN_ON(btrfs_header_generation(parent) != trans->transid);
323 btrfs_free_extent(trans, root, buf->start, buf->len,
324 0, root->root_key.objectid, level, 1);
326 if (!list_empty(&buf->recow)) {
327 list_del_init(&buf->recow);
328 free_extent_buffer(buf);
330 free_extent_buffer(buf);
331 btrfs_mark_buffer_dirty(cow);
336 static inline int should_cow_block(struct btrfs_trans_handle *trans,
337 struct btrfs_root *root,
338 struct extent_buffer *buf)
340 if (btrfs_header_generation(buf) == trans->transid &&
341 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
342 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
343 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
348 int btrfs_cow_block(struct btrfs_trans_handle *trans,
349 struct btrfs_root *root, struct extent_buffer *buf,
350 struct extent_buffer *parent, int parent_slot,
351 struct extent_buffer **cow_ret)
356 if (trans->transaction != root->fs_info->running_transaction) {
357 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
358 root->fs_info->running_transaction->transid);
362 if (trans->transid != root->fs_info->generation) {
363 printk(KERN_CRIT "trans %llu running %llu\n",
364 (unsigned long long)trans->transid,
365 (unsigned long long)root->fs_info->generation);
368 if (!should_cow_block(trans, root, buf)) {
373 search_start = buf->start & ~((u64)SZ_1G - 1);
374 ret = __btrfs_cow_block(trans, root, buf, parent,
375 parent_slot, cow_ret, search_start, 0);
379 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
381 if (k1->objectid > k2->objectid)
383 if (k1->objectid < k2->objectid)
385 if (k1->type > k2->type)
387 if (k1->type < k2->type)
389 if (k1->offset > k2->offset)
391 if (k1->offset < k2->offset)
397 * compare two keys in a memcmp fashion
399 static int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
403 btrfs_disk_key_to_cpu(&k1, disk);
404 return btrfs_comp_cpu_keys(&k1, k2);
408 * The leaf data grows from end-to-front in the node.
409 * this returns the address of the start of the last item,
410 * which is the stop of the leaf data stack
412 static inline unsigned int leaf_data_end(const struct btrfs_fs_info *fs_info,
413 const struct extent_buffer *leaf)
415 u32 nr = btrfs_header_nritems(leaf);
417 return BTRFS_LEAF_DATA_SIZE(fs_info);
418 return btrfs_item_offset_nr(leaf, nr - 1);
421 enum btrfs_tree_block_status
422 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
423 struct extent_buffer *buf)
426 struct btrfs_key cpukey;
427 struct btrfs_disk_key key;
428 u32 nritems = btrfs_header_nritems(buf);
429 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
431 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
434 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
435 if (parent_key && parent_key->type) {
436 btrfs_node_key(buf, &key, 0);
437 if (memcmp(parent_key, &key, sizeof(key)))
440 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
441 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
442 btrfs_node_key(buf, &key, i);
443 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
444 if (btrfs_comp_keys(&key, &cpukey) >= 0)
447 return BTRFS_TREE_BLOCK_CLEAN;
449 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
451 btrfs_disk_key_to_cpu(&cpukey, parent_key);
453 btrfs_node_key_to_cpu(buf, &cpukey, 0);
454 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
455 buf->start, buf->len,
456 btrfs_header_level(buf));
461 enum btrfs_tree_block_status
462 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
463 struct extent_buffer *buf)
466 struct btrfs_key cpukey;
467 struct btrfs_disk_key key;
468 u32 nritems = btrfs_header_nritems(buf);
469 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
471 if (nritems * sizeof(struct btrfs_item) > buf->len) {
472 fprintf(stderr, "invalid number of items %llu\n",
473 (unsigned long long)buf->start);
477 if (btrfs_header_level(buf) != 0) {
478 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
479 fprintf(stderr, "leaf is not a leaf %llu\n",
480 (unsigned long long)btrfs_header_bytenr(buf));
483 if (btrfs_leaf_free_space(root->fs_info, buf) < 0) {
484 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
485 fprintf(stderr, "leaf free space incorrect %llu %d\n",
486 (unsigned long long)btrfs_header_bytenr(buf),
487 btrfs_leaf_free_space(root->fs_info, buf));
492 return BTRFS_TREE_BLOCK_CLEAN;
494 btrfs_item_key(buf, &key, 0);
495 if (parent_key && parent_key->type &&
496 memcmp(parent_key, &key, sizeof(key))) {
497 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
498 fprintf(stderr, "leaf parent key incorrect %llu\n",
499 (unsigned long long)btrfs_header_bytenr(buf));
502 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
503 btrfs_item_key(buf, &key, i);
504 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
505 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
506 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
507 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
510 if (btrfs_item_offset_nr(buf, i) !=
511 btrfs_item_end_nr(buf, i + 1)) {
512 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
513 fprintf(stderr, "incorrect offsets %u %u\n",
514 btrfs_item_offset_nr(buf, i),
515 btrfs_item_end_nr(buf, i + 1));
518 if (i == 0 && btrfs_item_end_nr(buf, i) !=
519 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
520 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
521 fprintf(stderr, "bad item end %u wanted %u\n",
522 btrfs_item_end_nr(buf, i),
523 (unsigned)BTRFS_LEAF_DATA_SIZE(root->fs_info));
528 for (i = 0; i < nritems; i++) {
529 if (btrfs_item_end_nr(buf, i) >
530 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
531 btrfs_item_key(buf, &key, 0);
532 btrfs_print_key(&key);
534 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
535 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
536 (unsigned long long)btrfs_item_end_nr(buf, i),
537 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
543 return BTRFS_TREE_BLOCK_CLEAN;
545 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
547 btrfs_disk_key_to_cpu(&cpukey, parent_key);
549 btrfs_item_key_to_cpu(buf, &cpukey, 0);
551 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
552 buf->start, buf->len, 0);
557 static int noinline check_block(struct btrfs_root *root,
558 struct btrfs_path *path, int level)
560 struct btrfs_disk_key key;
561 struct btrfs_disk_key *key_ptr = NULL;
562 struct extent_buffer *parent;
563 enum btrfs_tree_block_status ret;
565 if (path->skip_check_block)
567 if (path->nodes[level + 1]) {
568 parent = path->nodes[level + 1];
569 btrfs_node_key(parent, &key, path->slots[level + 1]);
573 ret = btrfs_check_leaf(root, key_ptr, path->nodes[0]);
575 ret = btrfs_check_node(root, key_ptr, path->nodes[level]);
576 if (ret == BTRFS_TREE_BLOCK_CLEAN)
582 * search for key in the extent_buffer. The items start at offset p,
583 * and they are item_size apart. There are 'max' items in p.
585 * the slot in the array is returned via slot, and it points to
586 * the place where you would insert key if it is not found in
589 * slot may point to max if the key is bigger than all of the keys
591 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
592 int item_size, struct btrfs_key *key,
599 unsigned long offset;
600 struct btrfs_disk_key *tmp;
603 mid = (low + high) / 2;
604 offset = p + mid * item_size;
606 tmp = (struct btrfs_disk_key *)(eb->data + offset);
607 ret = btrfs_comp_keys(tmp, key);
623 * simple bin_search frontend that does the right thing for
626 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
627 int level, int *slot)
630 return generic_bin_search(eb,
631 offsetof(struct btrfs_leaf, items),
632 sizeof(struct btrfs_item),
633 key, btrfs_header_nritems(eb),
636 return generic_bin_search(eb,
637 offsetof(struct btrfs_node, ptrs),
638 sizeof(struct btrfs_key_ptr),
639 key, btrfs_header_nritems(eb),
643 struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
644 struct extent_buffer *parent, int slot)
646 struct extent_buffer *ret;
647 int level = btrfs_header_level(parent);
651 if (slot >= btrfs_header_nritems(parent))
657 ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
658 btrfs_node_ptr_generation(parent, slot));
659 if (!extent_buffer_uptodate(ret))
660 return ERR_PTR(-EIO);
662 if (btrfs_header_level(ret) != level - 1) {
664 "child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
665 btrfs_header_bytenr(parent), slot,
666 btrfs_header_level(parent), btrfs_header_level(ret));
667 free_extent_buffer(ret);
668 return ERR_PTR(-EIO);
673 static int balance_level(struct btrfs_trans_handle *trans,
674 struct btrfs_root *root,
675 struct btrfs_path *path, int level)
677 struct extent_buffer *right = NULL;
678 struct extent_buffer *mid;
679 struct extent_buffer *left = NULL;
680 struct extent_buffer *parent = NULL;
681 struct btrfs_fs_info *fs_info = root->fs_info;
685 int orig_slot = path->slots[level];
691 mid = path->nodes[level];
692 WARN_ON(btrfs_header_generation(mid) != trans->transid);
694 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
696 if (level < BTRFS_MAX_LEVEL - 1) {
697 parent = path->nodes[level + 1];
698 pslot = path->slots[level + 1];
702 * deal with the case where there is only one pointer in the root
703 * by promoting the node below to a root
706 struct extent_buffer *child;
708 if (btrfs_header_nritems(mid) != 1)
711 /* promote the child to a root */
712 child = read_node_slot(fs_info, mid, 0);
713 BUG_ON(!extent_buffer_uptodate(child));
714 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
718 add_root_to_dirty_list(root);
719 path->nodes[level] = NULL;
720 clean_tree_block(trans, root, mid);
721 /* once for the path */
722 free_extent_buffer(mid);
724 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
725 0, root->root_key.objectid,
727 /* once for the root ptr */
728 free_extent_buffer(mid);
731 if (btrfs_header_nritems(mid) >
732 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
735 left = read_node_slot(fs_info, parent, pslot - 1);
736 if (extent_buffer_uptodate(left)) {
737 wret = btrfs_cow_block(trans, root, left,
738 parent, pslot - 1, &left);
744 right = read_node_slot(fs_info, parent, pslot + 1);
745 if (extent_buffer_uptodate(right)) {
746 wret = btrfs_cow_block(trans, root, right,
747 parent, pslot + 1, &right);
754 /* first, try to make some room in the middle buffer */
756 orig_slot += btrfs_header_nritems(left);
757 wret = push_node_left(trans, root, left, mid, 1);
763 * then try to empty the right most buffer into the middle
766 wret = push_node_left(trans, root, mid, right, 1);
767 if (wret < 0 && wret != -ENOSPC)
769 if (btrfs_header_nritems(right) == 0) {
770 u64 bytenr = right->start;
771 u32 blocksize = right->len;
773 clean_tree_block(trans, root, right);
774 free_extent_buffer(right);
776 wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
779 wret = btrfs_free_extent(trans, root, bytenr,
781 root->root_key.objectid,
786 struct btrfs_disk_key right_key;
787 btrfs_node_key(right, &right_key, 0);
788 btrfs_set_node_key(parent, &right_key, pslot + 1);
789 btrfs_mark_buffer_dirty(parent);
792 if (btrfs_header_nritems(mid) == 1) {
794 * we're not allowed to leave a node with one item in the
795 * tree during a delete. A deletion from lower in the tree
796 * could try to delete the only pointer in this node.
797 * So, pull some keys from the left.
798 * There has to be a left pointer at this point because
799 * otherwise we would have pulled some pointers from the
803 wret = balance_node_right(trans, root, mid, left);
809 wret = push_node_left(trans, root, left, mid, 1);
815 if (btrfs_header_nritems(mid) == 0) {
816 /* we've managed to empty the middle node, drop it */
817 u64 bytenr = mid->start;
818 u32 blocksize = mid->len;
819 clean_tree_block(trans, root, mid);
820 free_extent_buffer(mid);
822 wret = btrfs_del_ptr(root, path, level + 1, pslot);
825 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
826 0, root->root_key.objectid,
831 /* update the parent key to reflect our changes */
832 struct btrfs_disk_key mid_key;
833 btrfs_node_key(mid, &mid_key, 0);
834 btrfs_set_node_key(parent, &mid_key, pslot);
835 btrfs_mark_buffer_dirty(parent);
838 /* update the path */
840 if (btrfs_header_nritems(left) > orig_slot) {
841 extent_buffer_get(left);
842 path->nodes[level] = left;
843 path->slots[level + 1] -= 1;
844 path->slots[level] = orig_slot;
846 free_extent_buffer(mid);
848 orig_slot -= btrfs_header_nritems(left);
849 path->slots[level] = orig_slot;
852 /* double check we haven't messed things up */
853 check_block(root, path, level);
855 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
859 free_extent_buffer(right);
861 free_extent_buffer(left);
865 /* returns zero if the push worked, non-zero otherwise */
866 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
867 struct btrfs_root *root,
868 struct btrfs_path *path, int level)
870 struct extent_buffer *right = NULL;
871 struct extent_buffer *mid;
872 struct extent_buffer *left = NULL;
873 struct extent_buffer *parent = NULL;
874 struct btrfs_fs_info *fs_info = root->fs_info;
878 int orig_slot = path->slots[level];
883 mid = path->nodes[level];
884 WARN_ON(btrfs_header_generation(mid) != trans->transid);
886 if (level < BTRFS_MAX_LEVEL - 1) {
887 parent = path->nodes[level + 1];
888 pslot = path->slots[level + 1];
894 left = read_node_slot(fs_info, parent, pslot - 1);
896 /* first, try to make some room in the middle buffer */
897 if (extent_buffer_uptodate(left)) {
899 left_nr = btrfs_header_nritems(left);
900 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
903 ret = btrfs_cow_block(trans, root, left, parent,
908 wret = push_node_left(trans, root,
915 struct btrfs_disk_key disk_key;
916 orig_slot += left_nr;
917 btrfs_node_key(mid, &disk_key, 0);
918 btrfs_set_node_key(parent, &disk_key, pslot);
919 btrfs_mark_buffer_dirty(parent);
920 if (btrfs_header_nritems(left) > orig_slot) {
921 path->nodes[level] = left;
922 path->slots[level + 1] -= 1;
923 path->slots[level] = orig_slot;
924 free_extent_buffer(mid);
927 btrfs_header_nritems(left);
928 path->slots[level] = orig_slot;
929 free_extent_buffer(left);
933 free_extent_buffer(left);
935 right= read_node_slot(fs_info, parent, pslot + 1);
938 * then try to empty the right most buffer into the middle
940 if (extent_buffer_uptodate(right)) {
942 right_nr = btrfs_header_nritems(right);
943 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 1) {
946 ret = btrfs_cow_block(trans, root, right,
952 wret = balance_node_right(trans, root,
959 struct btrfs_disk_key disk_key;
961 btrfs_node_key(right, &disk_key, 0);
962 btrfs_set_node_key(parent, &disk_key, pslot + 1);
963 btrfs_mark_buffer_dirty(parent);
965 if (btrfs_header_nritems(mid) <= orig_slot) {
966 path->nodes[level] = right;
967 path->slots[level + 1] += 1;
968 path->slots[level] = orig_slot -
969 btrfs_header_nritems(mid);
970 free_extent_buffer(mid);
972 free_extent_buffer(right);
976 free_extent_buffer(right);
982 * readahead one full node of leaves
984 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
985 int level, int slot, u64 objectid)
987 struct btrfs_fs_info *fs_info = root->fs_info;
988 struct extent_buffer *node;
989 struct btrfs_disk_key disk_key;
995 int direction = path->reada;
996 struct extent_buffer *eb;
1003 if (!path->nodes[level])
1006 node = path->nodes[level];
1007 search = btrfs_node_blockptr(node, slot);
1008 eb = btrfs_find_tree_block(fs_info, search, fs_info->nodesize);
1010 free_extent_buffer(eb);
1014 highest_read = search;
1015 lowest_read = search;
1017 nritems = btrfs_header_nritems(node);
1020 if (direction < 0) {
1024 } else if (direction > 0) {
1029 if (path->reada < 0 && objectid) {
1030 btrfs_node_key(node, &disk_key, nr);
1031 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1034 search = btrfs_node_blockptr(node, nr);
1035 if ((search >= lowest_read && search <= highest_read) ||
1036 (search < lowest_read && lowest_read - search <= 32768) ||
1037 (search > highest_read && search - highest_read <= 32768)) {
1038 readahead_tree_block(fs_info, search,
1039 btrfs_node_ptr_generation(node, nr));
1040 nread += fs_info->nodesize;
1043 if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
1045 if(nread > SZ_1M || nscan > 128)
1048 if (search < lowest_read)
1049 lowest_read = search;
1050 if (search > highest_read)
1051 highest_read = search;
1055 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1056 u64 iobjectid, u64 ioff, u8 key_type,
1057 struct btrfs_key *found_key)
1060 struct btrfs_key key;
1061 struct extent_buffer *eb;
1062 struct btrfs_path *path;
1064 key.type = key_type;
1065 key.objectid = iobjectid;
1068 if (found_path == NULL) {
1069 path = btrfs_alloc_path();
1075 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1076 if ((ret < 0) || (found_key == NULL))
1079 eb = path->nodes[0];
1080 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1081 ret = btrfs_next_leaf(fs_root, path);
1084 eb = path->nodes[0];
1087 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1088 if (found_key->type != key.type ||
1089 found_key->objectid != key.objectid) {
1095 if (path != found_path)
1096 btrfs_free_path(path);
1101 * look for key in the tree. path is filled in with nodes along the way
1102 * if key is found, we return zero and you can find the item in the leaf
1103 * level of the path (level 0)
1105 * If the key isn't found, the path points to the slot where it should
1106 * be inserted, and 1 is returned. If there are other errors during the
1107 * search a negative error number is returned.
1109 * if ins_len > 0, nodes and leaves will be split as we walk down the
1110 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1113 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1114 *root, struct btrfs_key *key, struct btrfs_path *p, int
1117 struct extent_buffer *b;
1121 int should_reada = p->reada;
1122 struct btrfs_fs_info *fs_info = root->fs_info;
1123 u8 lowest_level = 0;
1125 lowest_level = p->lowest_level;
1126 WARN_ON(lowest_level && ins_len > 0);
1127 WARN_ON(p->nodes[0] != NULL);
1129 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1133 extent_buffer_get(b);
1135 level = btrfs_header_level(b);
1138 wret = btrfs_cow_block(trans, root, b,
1139 p->nodes[level + 1],
1140 p->slots[level + 1],
1143 free_extent_buffer(b);
1147 BUG_ON(!cow && ins_len);
1148 if (level != btrfs_header_level(b))
1150 level = btrfs_header_level(b);
1151 p->nodes[level] = b;
1152 ret = check_block(root, p, level);
1155 ret = bin_search(b, key, level, &slot);
1157 if (ret && slot > 0)
1159 p->slots[level] = slot;
1160 if ((p->search_for_split || ins_len > 0) &&
1161 btrfs_header_nritems(b) >=
1162 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1163 int sret = split_node(trans, root, p, level);
1167 b = p->nodes[level];
1168 slot = p->slots[level];
1169 } else if (ins_len < 0) {
1170 int sret = balance_level(trans, root, p,
1174 b = p->nodes[level];
1176 btrfs_release_path(p);
1179 slot = p->slots[level];
1180 BUG_ON(btrfs_header_nritems(b) == 1);
1182 /* this is only true while dropping a snapshot */
1183 if (level == lowest_level)
1187 reada_for_search(root, p, level, slot,
1190 b = read_node_slot(fs_info, b, slot);
1191 if (!extent_buffer_uptodate(b))
1194 p->slots[level] = slot;
1196 ins_len > btrfs_leaf_free_space(root->fs_info, b)) {
1197 int sret = split_leaf(trans, root, key,
1198 p, ins_len, ret == 0);
1210 * adjust the pointers going up the tree, starting at level
1211 * making sure the right key of each node is points to 'key'.
1212 * This is used after shifting pointers to the left, so it stops
1213 * fixing up pointers when a given leaf/node is not in slot 0 of the
1216 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
1217 struct btrfs_disk_key *key, int level)
1220 struct extent_buffer *t;
1222 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1223 int tslot = path->slots[i];
1224 if (!path->nodes[i])
1227 btrfs_set_node_key(t, key, tslot);
1228 btrfs_mark_buffer_dirty(path->nodes[i]);
1237 * This function isn't completely safe. It's the caller's responsibility
1238 * that the new key won't break the order
1240 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
1241 struct btrfs_key *new_key)
1243 struct btrfs_disk_key disk_key;
1244 struct extent_buffer *eb;
1247 eb = path->nodes[0];
1248 slot = path->slots[0];
1250 btrfs_item_key(eb, &disk_key, slot - 1);
1251 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1254 if (slot < btrfs_header_nritems(eb) - 1) {
1255 btrfs_item_key(eb, &disk_key, slot + 1);
1256 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1260 btrfs_cpu_key_to_disk(&disk_key, new_key);
1261 btrfs_set_item_key(eb, &disk_key, slot);
1262 btrfs_mark_buffer_dirty(eb);
1264 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1269 * update an item key without the safety checks. This is meant to be called by
1272 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
1273 struct btrfs_path *path,
1274 struct btrfs_key *new_key)
1276 struct btrfs_disk_key disk_key;
1277 struct extent_buffer *eb;
1280 eb = path->nodes[0];
1281 slot = path->slots[0];
1283 btrfs_cpu_key_to_disk(&disk_key, new_key);
1284 btrfs_set_item_key(eb, &disk_key, slot);
1285 btrfs_mark_buffer_dirty(eb);
1287 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1291 * try to push data from one node into the next node left in the
1294 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1295 * error, and > 0 if there was no room in the left hand block.
1297 static int push_node_left(struct btrfs_trans_handle *trans,
1298 struct btrfs_root *root, struct extent_buffer *dst,
1299 struct extent_buffer *src, int empty)
1306 src_nritems = btrfs_header_nritems(src);
1307 dst_nritems = btrfs_header_nritems(dst);
1308 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1309 WARN_ON(btrfs_header_generation(src) != trans->transid);
1310 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1312 if (!empty && src_nritems <= 8)
1315 if (push_items <= 0) {
1320 push_items = min(src_nritems, push_items);
1321 if (push_items < src_nritems) {
1322 /* leave at least 8 pointers in the node if
1323 * we aren't going to empty it
1325 if (src_nritems - push_items < 8) {
1326 if (push_items <= 8)
1332 push_items = min(src_nritems - 8, push_items);
1334 copy_extent_buffer(dst, src,
1335 btrfs_node_key_ptr_offset(dst_nritems),
1336 btrfs_node_key_ptr_offset(0),
1337 push_items * sizeof(struct btrfs_key_ptr));
1339 if (push_items < src_nritems) {
1340 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1341 btrfs_node_key_ptr_offset(push_items),
1342 (src_nritems - push_items) *
1343 sizeof(struct btrfs_key_ptr));
1345 btrfs_set_header_nritems(src, src_nritems - push_items);
1346 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1347 btrfs_mark_buffer_dirty(src);
1348 btrfs_mark_buffer_dirty(dst);
1354 * try to push data from one node into the next node right in the
1357 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1358 * error, and > 0 if there was no room in the right hand block.
1360 * this will only push up to 1/2 the contents of the left node over
1362 static int balance_node_right(struct btrfs_trans_handle *trans,
1363 struct btrfs_root *root,
1364 struct extent_buffer *dst,
1365 struct extent_buffer *src)
1373 WARN_ON(btrfs_header_generation(src) != trans->transid);
1374 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1376 src_nritems = btrfs_header_nritems(src);
1377 dst_nritems = btrfs_header_nritems(dst);
1378 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1379 if (push_items <= 0) {
1383 if (src_nritems < 4) {
1387 max_push = src_nritems / 2 + 1;
1388 /* don't try to empty the node */
1389 if (max_push >= src_nritems) {
1393 if (max_push < push_items)
1394 push_items = max_push;
1396 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1397 btrfs_node_key_ptr_offset(0),
1399 sizeof(struct btrfs_key_ptr));
1401 copy_extent_buffer(dst, src,
1402 btrfs_node_key_ptr_offset(0),
1403 btrfs_node_key_ptr_offset(src_nritems - push_items),
1404 push_items * sizeof(struct btrfs_key_ptr));
1406 btrfs_set_header_nritems(src, src_nritems - push_items);
1407 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1409 btrfs_mark_buffer_dirty(src);
1410 btrfs_mark_buffer_dirty(dst);
1416 * helper function to insert a new root level in the tree.
1417 * A new node is allocated, and a single item is inserted to
1418 * point to the existing root
1420 * returns zero on success or < 0 on failure.
1422 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1423 struct btrfs_root *root,
1424 struct btrfs_path *path, int level)
1427 struct extent_buffer *lower;
1428 struct extent_buffer *c;
1429 struct extent_buffer *old;
1430 struct btrfs_disk_key lower_key;
1432 BUG_ON(path->nodes[level]);
1433 BUG_ON(path->nodes[level-1] != root->node);
1435 lower = path->nodes[level-1];
1437 btrfs_item_key(lower, &lower_key, 0);
1439 btrfs_node_key(lower, &lower_key, 0);
1441 c = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1442 root->root_key.objectid, &lower_key,
1443 level, root->node->start, 0);
1448 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1449 btrfs_set_header_nritems(c, 1);
1450 btrfs_set_header_level(c, level);
1451 btrfs_set_header_bytenr(c, c->start);
1452 btrfs_set_header_generation(c, trans->transid);
1453 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1454 btrfs_set_header_owner(c, root->root_key.objectid);
1456 write_extent_buffer(c, root->fs_info->fsid,
1457 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1459 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1460 btrfs_header_chunk_tree_uuid(c),
1463 btrfs_set_node_key(c, &lower_key, 0);
1464 btrfs_set_node_blockptr(c, 0, lower->start);
1465 lower_gen = btrfs_header_generation(lower);
1466 WARN_ON(lower_gen != trans->transid);
1468 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1470 btrfs_mark_buffer_dirty(c);
1475 /* the super has an extra ref to root->node */
1476 free_extent_buffer(old);
1478 add_root_to_dirty_list(root);
1479 extent_buffer_get(c);
1480 path->nodes[level] = c;
1481 path->slots[level] = 0;
1486 * worker function to insert a single pointer in a node.
1487 * the node should have enough room for the pointer already
1489 * slot and level indicate where you want the key to go, and
1490 * blocknr is the block the key points to.
1492 * returns zero on success and < 0 on any error
1494 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1495 *root, struct btrfs_path *path, struct btrfs_disk_key
1496 *key, u64 bytenr, int slot, int level)
1498 struct extent_buffer *lower;
1501 BUG_ON(!path->nodes[level]);
1502 lower = path->nodes[level];
1503 nritems = btrfs_header_nritems(lower);
1506 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
1508 if (slot < nritems) {
1509 /* shift the items */
1510 memmove_extent_buffer(lower,
1511 btrfs_node_key_ptr_offset(slot + 1),
1512 btrfs_node_key_ptr_offset(slot),
1513 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1515 btrfs_set_node_key(lower, key, slot);
1516 btrfs_set_node_blockptr(lower, slot, bytenr);
1517 WARN_ON(trans->transid == 0);
1518 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1519 btrfs_set_header_nritems(lower, nritems + 1);
1520 btrfs_mark_buffer_dirty(lower);
1525 * split the node at the specified level in path in two.
1526 * The path is corrected to point to the appropriate node after the split
1528 * Before splitting this tries to make some room in the node by pushing
1529 * left and right, if either one works, it returns right away.
1531 * returns 0 on success and < 0 on failure
1533 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1534 *root, struct btrfs_path *path, int level)
1536 struct extent_buffer *c;
1537 struct extent_buffer *split;
1538 struct btrfs_disk_key disk_key;
1544 c = path->nodes[level];
1545 WARN_ON(btrfs_header_generation(c) != trans->transid);
1546 if (c == root->node) {
1547 /* trying to split the root, lets make a new one */
1548 ret = insert_new_root(trans, root, path, level + 1);
1552 ret = push_nodes_for_insert(trans, root, path, level);
1553 c = path->nodes[level];
1554 if (!ret && btrfs_header_nritems(c) <
1555 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 3)
1561 c_nritems = btrfs_header_nritems(c);
1562 mid = (c_nritems + 1) / 2;
1563 btrfs_node_key(c, &disk_key, mid);
1565 split = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1566 root->root_key.objectid,
1567 &disk_key, level, c->start, 0);
1569 return PTR_ERR(split);
1571 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1572 btrfs_set_header_level(split, btrfs_header_level(c));
1573 btrfs_set_header_bytenr(split, split->start);
1574 btrfs_set_header_generation(split, trans->transid);
1575 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1576 btrfs_set_header_owner(split, root->root_key.objectid);
1577 write_extent_buffer(split, root->fs_info->fsid,
1578 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1579 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1580 btrfs_header_chunk_tree_uuid(split),
1584 copy_extent_buffer(split, c,
1585 btrfs_node_key_ptr_offset(0),
1586 btrfs_node_key_ptr_offset(mid),
1587 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1588 btrfs_set_header_nritems(split, c_nritems - mid);
1589 btrfs_set_header_nritems(c, mid);
1592 btrfs_mark_buffer_dirty(c);
1593 btrfs_mark_buffer_dirty(split);
1595 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1596 path->slots[level + 1] + 1,
1601 if (path->slots[level] >= mid) {
1602 path->slots[level] -= mid;
1603 free_extent_buffer(c);
1604 path->nodes[level] = split;
1605 path->slots[level + 1] += 1;
1607 free_extent_buffer(split);
1613 * how many bytes are required to store the items in a leaf. start
1614 * and nr indicate which items in the leaf to check. This totals up the
1615 * space used both by the item structs and the item data
1617 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1620 int nritems = btrfs_header_nritems(l);
1621 int end = min(nritems, start + nr) - 1;
1625 data_len = btrfs_item_end_nr(l, start);
1626 data_len = data_len - btrfs_item_offset_nr(l, end);
1627 data_len += sizeof(struct btrfs_item) * nr;
1628 WARN_ON(data_len < 0);
1633 * The space between the end of the leaf items and
1634 * the start of the leaf data. IOW, how much room
1635 * the leaf has left for both items and data
1637 int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
1638 struct extent_buffer *leaf)
1640 int nritems = btrfs_header_nritems(leaf);
1643 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
1645 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1646 ret, BTRFS_LEAF_DATA_SIZE(fs_info),
1647 leaf_space_used(leaf, 0, nritems), nritems);
1653 * push some data in the path leaf to the right, trying to free up at
1654 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1656 * returns 1 if the push failed because the other node didn't have enough
1657 * room, 0 if everything worked out and < 0 if there were major errors.
1659 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1660 *root, struct btrfs_path *path, int data_size,
1663 struct extent_buffer *left = path->nodes[0];
1664 struct extent_buffer *right;
1665 struct extent_buffer *upper;
1666 struct btrfs_disk_key disk_key;
1667 struct btrfs_fs_info *fs_info = root->fs_info;
1673 struct btrfs_item *item;
1681 slot = path->slots[1];
1682 if (!path->nodes[1]) {
1685 upper = path->nodes[1];
1686 if (slot >= btrfs_header_nritems(upper) - 1)
1689 right = read_node_slot(fs_info, upper, slot + 1);
1690 if (!extent_buffer_uptodate(right)) {
1692 return PTR_ERR(right);
1695 free_space = btrfs_leaf_free_space(fs_info, right);
1696 if (free_space < data_size) {
1697 free_extent_buffer(right);
1701 /* cow and double check */
1702 ret = btrfs_cow_block(trans, root, right, upper,
1705 free_extent_buffer(right);
1708 free_space = btrfs_leaf_free_space(fs_info, right);
1709 if (free_space < data_size) {
1710 free_extent_buffer(right);
1714 left_nritems = btrfs_header_nritems(left);
1715 if (left_nritems == 0) {
1716 free_extent_buffer(right);
1725 i = left_nritems - 1;
1727 item = btrfs_item_nr(i);
1729 if (path->slots[0] == i)
1730 push_space += data_size + sizeof(*item);
1732 this_item_size = btrfs_item_size(left, item);
1733 if (this_item_size + sizeof(*item) + push_space > free_space)
1736 push_space += this_item_size + sizeof(*item);
1742 if (push_items == 0) {
1743 free_extent_buffer(right);
1747 if (!empty && push_items == left_nritems)
1750 /* push left to right */
1751 right_nritems = btrfs_header_nritems(right);
1753 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1754 push_space -= leaf_data_end(fs_info, left);
1756 /* make room in the right data area */
1757 data_end = leaf_data_end(fs_info, right);
1758 memmove_extent_buffer(right,
1759 btrfs_leaf_data(right) + data_end - push_space,
1760 btrfs_leaf_data(right) + data_end,
1761 BTRFS_LEAF_DATA_SIZE(root->fs_info) - data_end);
1763 /* copy from the left data area */
1764 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1765 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space,
1766 btrfs_leaf_data(left) + leaf_data_end(fs_info, left),
1769 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1770 btrfs_item_nr_offset(0),
1771 right_nritems * sizeof(struct btrfs_item));
1773 /* copy the items from left to right */
1774 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1775 btrfs_item_nr_offset(left_nritems - push_items),
1776 push_items * sizeof(struct btrfs_item));
1778 /* update the item pointers */
1779 right_nritems += push_items;
1780 btrfs_set_header_nritems(right, right_nritems);
1781 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1782 for (i = 0; i < right_nritems; i++) {
1783 item = btrfs_item_nr(i);
1784 push_space -= btrfs_item_size(right, item);
1785 btrfs_set_item_offset(right, item, push_space);
1788 left_nritems -= push_items;
1789 btrfs_set_header_nritems(left, left_nritems);
1792 btrfs_mark_buffer_dirty(left);
1793 btrfs_mark_buffer_dirty(right);
1795 btrfs_item_key(right, &disk_key, 0);
1796 btrfs_set_node_key(upper, &disk_key, slot + 1);
1797 btrfs_mark_buffer_dirty(upper);
1799 /* then fixup the leaf pointer in the path */
1800 if (path->slots[0] >= left_nritems) {
1801 path->slots[0] -= left_nritems;
1802 free_extent_buffer(path->nodes[0]);
1803 path->nodes[0] = right;
1804 path->slots[1] += 1;
1806 free_extent_buffer(right);
1811 * push some data in the path leaf to the left, trying to free up at
1812 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1814 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1815 *root, struct btrfs_path *path, int data_size,
1818 struct btrfs_disk_key disk_key;
1819 struct extent_buffer *right = path->nodes[0];
1820 struct extent_buffer *left;
1821 struct btrfs_fs_info *fs_info = root->fs_info;
1827 struct btrfs_item *item;
1828 u32 old_left_nritems;
1833 u32 old_left_item_size;
1835 slot = path->slots[1];
1838 if (!path->nodes[1])
1841 right_nritems = btrfs_header_nritems(right);
1842 if (right_nritems == 0) {
1846 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
1847 free_space = btrfs_leaf_free_space(fs_info, left);
1848 if (free_space < data_size) {
1849 free_extent_buffer(left);
1853 /* cow and double check */
1854 ret = btrfs_cow_block(trans, root, left,
1855 path->nodes[1], slot - 1, &left);
1857 /* we hit -ENOSPC, but it isn't fatal here */
1858 free_extent_buffer(left);
1862 free_space = btrfs_leaf_free_space(fs_info, left);
1863 if (free_space < data_size) {
1864 free_extent_buffer(left);
1871 nr = right_nritems - 1;
1873 for (i = 0; i < nr; i++) {
1874 item = btrfs_item_nr(i);
1876 if (path->slots[0] == i)
1877 push_space += data_size + sizeof(*item);
1879 this_item_size = btrfs_item_size(right, item);
1880 if (this_item_size + sizeof(*item) + push_space > free_space)
1884 push_space += this_item_size + sizeof(*item);
1887 if (push_items == 0) {
1888 free_extent_buffer(left);
1891 if (!empty && push_items == btrfs_header_nritems(right))
1894 /* push data from right to left */
1895 copy_extent_buffer(left, right,
1896 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1897 btrfs_item_nr_offset(0),
1898 push_items * sizeof(struct btrfs_item));
1900 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1901 btrfs_item_offset_nr(right, push_items -1);
1903 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1904 leaf_data_end(fs_info, left) - push_space,
1905 btrfs_leaf_data(right) +
1906 btrfs_item_offset_nr(right, push_items - 1),
1908 old_left_nritems = btrfs_header_nritems(left);
1909 BUG_ON(old_left_nritems == 0);
1911 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1912 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1915 item = btrfs_item_nr(i);
1916 ioff = btrfs_item_offset(left, item);
1917 btrfs_set_item_offset(left, item,
1918 ioff - (BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1919 old_left_item_size));
1921 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1923 /* fixup right node */
1924 if (push_items > right_nritems) {
1925 printk("push items %d nr %u\n", push_items, right_nritems);
1929 if (push_items < right_nritems) {
1930 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1931 leaf_data_end(fs_info, right);
1932 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1933 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1935 btrfs_leaf_data(right) +
1936 leaf_data_end(fs_info, right),
1939 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1940 btrfs_item_nr_offset(push_items),
1941 (btrfs_header_nritems(right) - push_items) *
1942 sizeof(struct btrfs_item));
1944 right_nritems -= push_items;
1945 btrfs_set_header_nritems(right, right_nritems);
1946 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1947 for (i = 0; i < right_nritems; i++) {
1948 item = btrfs_item_nr(i);
1949 push_space = push_space - btrfs_item_size(right, item);
1950 btrfs_set_item_offset(right, item, push_space);
1953 btrfs_mark_buffer_dirty(left);
1955 btrfs_mark_buffer_dirty(right);
1957 btrfs_item_key(right, &disk_key, 0);
1958 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1960 /* then fixup the leaf pointer in the path */
1961 if (path->slots[0] < push_items) {
1962 path->slots[0] += old_left_nritems;
1963 free_extent_buffer(path->nodes[0]);
1964 path->nodes[0] = left;
1965 path->slots[1] -= 1;
1967 free_extent_buffer(left);
1968 path->slots[0] -= push_items;
1970 BUG_ON(path->slots[0] < 0);
1975 * split the path's leaf in two, making sure there is at least data_size
1976 * available for the resulting leaf level of the path.
1978 * returns 0 if all went well and < 0 on failure.
1980 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
1981 struct btrfs_root *root,
1982 struct btrfs_path *path,
1983 struct extent_buffer *l,
1984 struct extent_buffer *right,
1985 int slot, int mid, int nritems)
1992 struct btrfs_disk_key disk_key;
1994 nritems = nritems - mid;
1995 btrfs_set_header_nritems(right, nritems);
1996 data_copy_size = btrfs_item_end_nr(l, mid) -
1997 leaf_data_end(root->fs_info, l);
1999 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2000 btrfs_item_nr_offset(mid),
2001 nritems * sizeof(struct btrfs_item));
2003 copy_extent_buffer(right, l,
2004 btrfs_leaf_data(right) +
2005 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2006 data_copy_size, btrfs_leaf_data(l) +
2007 leaf_data_end(root->fs_info, l), data_copy_size);
2009 rt_data_off = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2010 btrfs_item_end_nr(l, mid);
2012 for (i = 0; i < nritems; i++) {
2013 struct btrfs_item *item = btrfs_item_nr(i);
2014 u32 ioff = btrfs_item_offset(right, item);
2015 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2018 btrfs_set_header_nritems(l, mid);
2020 btrfs_item_key(right, &disk_key, 0);
2021 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2022 path->slots[1] + 1, 1);
2026 btrfs_mark_buffer_dirty(right);
2027 btrfs_mark_buffer_dirty(l);
2028 BUG_ON(path->slots[0] != slot);
2031 free_extent_buffer(path->nodes[0]);
2032 path->nodes[0] = right;
2033 path->slots[0] -= mid;
2034 path->slots[1] += 1;
2036 free_extent_buffer(right);
2039 BUG_ON(path->slots[0] < 0);
2045 * split the path's leaf in two, making sure there is at least data_size
2046 * available for the resulting leaf level of the path.
2048 * returns 0 if all went well and < 0 on failure.
2050 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2051 struct btrfs_root *root,
2052 struct btrfs_key *ins_key,
2053 struct btrfs_path *path, int data_size,
2056 struct btrfs_disk_key disk_key;
2057 struct extent_buffer *l;
2061 struct extent_buffer *right;
2065 int num_doubles = 0;
2068 slot = path->slots[0];
2069 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2070 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info))
2073 /* first try to make some room by pushing left and right */
2074 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2075 wret = push_leaf_right(trans, root, path, data_size, 0);
2079 wret = push_leaf_left(trans, root, path, data_size, 0);
2085 /* did the pushes work? */
2086 if (btrfs_leaf_free_space(root->fs_info, l) >= data_size)
2090 if (!path->nodes[1]) {
2091 ret = insert_new_root(trans, root, path, 1);
2098 slot = path->slots[0];
2099 nritems = btrfs_header_nritems(l);
2100 mid = (nritems + 1) / 2;
2104 leaf_space_used(l, mid, nritems - mid) + data_size >
2105 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2106 if (slot >= nritems) {
2110 if (mid != nritems &&
2111 leaf_space_used(l, mid, nritems - mid) +
2113 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2119 if (leaf_space_used(l, 0, mid) + data_size >
2120 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2121 if (!extend && data_size && slot == 0) {
2123 } else if ((extend || !data_size) && slot == 0) {
2127 if (mid != nritems &&
2128 leaf_space_used(l, mid, nritems - mid) +
2130 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2138 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2140 btrfs_item_key(l, &disk_key, mid);
2142 right = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
2143 root->root_key.objectid,
2144 &disk_key, 0, l->start, 0);
2145 if (IS_ERR(right)) {
2147 return PTR_ERR(right);
2150 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2151 btrfs_set_header_bytenr(right, right->start);
2152 btrfs_set_header_generation(right, trans->transid);
2153 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2154 btrfs_set_header_owner(right, root->root_key.objectid);
2155 btrfs_set_header_level(right, 0);
2156 write_extent_buffer(right, root->fs_info->fsid,
2157 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2159 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2160 btrfs_header_chunk_tree_uuid(right),
2165 btrfs_set_header_nritems(right, 0);
2166 wret = insert_ptr(trans, root, path,
2167 &disk_key, right->start,
2168 path->slots[1] + 1, 1);
2172 free_extent_buffer(path->nodes[0]);
2173 path->nodes[0] = right;
2175 path->slots[1] += 1;
2177 btrfs_set_header_nritems(right, 0);
2178 wret = insert_ptr(trans, root, path,
2184 free_extent_buffer(path->nodes[0]);
2185 path->nodes[0] = right;
2187 if (path->slots[1] == 0) {
2188 btrfs_fixup_low_keys(root, path,
2192 btrfs_mark_buffer_dirty(right);
2196 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2200 BUG_ON(num_doubles != 0);
2209 * This function splits a single item into two items,
2210 * giving 'new_key' to the new item and splitting the
2211 * old one at split_offset (from the start of the item).
2213 * The path may be released by this operation. After
2214 * the split, the path is pointing to the old item. The
2215 * new item is going to be in the same node as the old one.
2217 * Note, the item being split must be smaller enough to live alone on
2218 * a tree block with room for one extra struct btrfs_item
2220 * This allows us to split the item in place, keeping a lock on the
2221 * leaf the entire time.
2223 int btrfs_split_item(struct btrfs_trans_handle *trans,
2224 struct btrfs_root *root,
2225 struct btrfs_path *path,
2226 struct btrfs_key *new_key,
2227 unsigned long split_offset)
2230 struct extent_buffer *leaf;
2231 struct btrfs_key orig_key;
2232 struct btrfs_item *item;
2233 struct btrfs_item *new_item;
2238 struct btrfs_disk_key disk_key;
2241 leaf = path->nodes[0];
2242 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2243 if (btrfs_leaf_free_space(root->fs_info, leaf) >=
2244 sizeof(struct btrfs_item))
2247 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2248 btrfs_release_path(path);
2250 path->search_for_split = 1;
2252 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2253 path->search_for_split = 0;
2255 /* if our item isn't there or got smaller, return now */
2256 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2261 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2264 BUG_ON(btrfs_leaf_free_space(root->fs_info, leaf) <
2265 sizeof(struct btrfs_item));
2266 leaf = path->nodes[0];
2269 item = btrfs_item_nr(path->slots[0]);
2270 orig_offset = btrfs_item_offset(leaf, item);
2271 item_size = btrfs_item_size(leaf, item);
2274 buf = kmalloc(item_size, GFP_NOFS);
2276 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2277 path->slots[0]), item_size);
2278 slot = path->slots[0] + 1;
2279 leaf = path->nodes[0];
2281 nritems = btrfs_header_nritems(leaf);
2283 if (slot < nritems) {
2284 /* shift the items */
2285 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2286 btrfs_item_nr_offset(slot),
2287 (nritems - slot) * sizeof(struct btrfs_item));
2291 btrfs_cpu_key_to_disk(&disk_key, new_key);
2292 btrfs_set_item_key(leaf, &disk_key, slot);
2294 new_item = btrfs_item_nr(slot);
2296 btrfs_set_item_offset(leaf, new_item, orig_offset);
2297 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2299 btrfs_set_item_offset(leaf, item,
2300 orig_offset + item_size - split_offset);
2301 btrfs_set_item_size(leaf, item, split_offset);
2303 btrfs_set_header_nritems(leaf, nritems + 1);
2305 /* write the data for the start of the original item */
2306 write_extent_buffer(leaf, buf,
2307 btrfs_item_ptr_offset(leaf, path->slots[0]),
2310 /* write the data for the new item */
2311 write_extent_buffer(leaf, buf + split_offset,
2312 btrfs_item_ptr_offset(leaf, slot),
2313 item_size - split_offset);
2314 btrfs_mark_buffer_dirty(leaf);
2317 if (btrfs_leaf_free_space(root->fs_info, leaf) < 0) {
2318 btrfs_print_leaf(root, leaf);
2325 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2326 u32 new_size, int from_end)
2330 struct extent_buffer *leaf;
2331 struct btrfs_item *item;
2333 unsigned int data_end;
2334 unsigned int old_data_start;
2335 unsigned int old_size;
2336 unsigned int size_diff;
2339 leaf = path->nodes[0];
2340 slot = path->slots[0];
2342 old_size = btrfs_item_size_nr(leaf, slot);
2343 if (old_size == new_size)
2346 nritems = btrfs_header_nritems(leaf);
2347 data_end = leaf_data_end(root->fs_info, leaf);
2349 old_data_start = btrfs_item_offset_nr(leaf, slot);
2351 size_diff = old_size - new_size;
2354 BUG_ON(slot >= nritems);
2357 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2359 /* first correct the data pointers */
2360 for (i = slot; i < nritems; i++) {
2362 item = btrfs_item_nr(i);
2363 ioff = btrfs_item_offset(leaf, item);
2364 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2367 /* shift the data */
2369 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2370 data_end + size_diff, btrfs_leaf_data(leaf) +
2371 data_end, old_data_start + new_size - data_end);
2373 struct btrfs_disk_key disk_key;
2376 btrfs_item_key(leaf, &disk_key, slot);
2378 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2380 struct btrfs_file_extent_item *fi;
2382 fi = btrfs_item_ptr(leaf, slot,
2383 struct btrfs_file_extent_item);
2384 fi = (struct btrfs_file_extent_item *)(
2385 (unsigned long)fi - size_diff);
2387 if (btrfs_file_extent_type(leaf, fi) ==
2388 BTRFS_FILE_EXTENT_INLINE) {
2389 ptr = btrfs_item_ptr_offset(leaf, slot);
2390 memmove_extent_buffer(leaf, ptr,
2392 offsetof(struct btrfs_file_extent_item,
2397 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2398 data_end + size_diff, btrfs_leaf_data(leaf) +
2399 data_end, old_data_start - data_end);
2401 offset = btrfs_disk_key_offset(&disk_key);
2402 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2403 btrfs_set_item_key(leaf, &disk_key, slot);
2405 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2408 item = btrfs_item_nr(slot);
2409 btrfs_set_item_size(leaf, item, new_size);
2410 btrfs_mark_buffer_dirty(leaf);
2413 if (btrfs_leaf_free_space(root->fs_info, leaf) < 0) {
2414 btrfs_print_leaf(root, leaf);
2420 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2425 struct extent_buffer *leaf;
2426 struct btrfs_item *item;
2428 unsigned int data_end;
2429 unsigned int old_data;
2430 unsigned int old_size;
2433 leaf = path->nodes[0];
2435 nritems = btrfs_header_nritems(leaf);
2436 data_end = leaf_data_end(root->fs_info, leaf);
2438 if (btrfs_leaf_free_space(root->fs_info, leaf) < data_size) {
2439 btrfs_print_leaf(root, leaf);
2442 slot = path->slots[0];
2443 old_data = btrfs_item_end_nr(leaf, slot);
2446 if (slot >= nritems) {
2447 btrfs_print_leaf(root, leaf);
2448 printk("slot %d too large, nritems %d\n", slot, nritems);
2453 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2455 /* first correct the data pointers */
2456 for (i = slot; i < nritems; i++) {
2458 item = btrfs_item_nr(i);
2459 ioff = btrfs_item_offset(leaf, item);
2460 btrfs_set_item_offset(leaf, item, ioff - data_size);
2463 /* shift the data */
2464 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2465 data_end - data_size, btrfs_leaf_data(leaf) +
2466 data_end, old_data - data_end);
2468 data_end = old_data;
2469 old_size = btrfs_item_size_nr(leaf, slot);
2470 item = btrfs_item_nr(slot);
2471 btrfs_set_item_size(leaf, item, old_size + data_size);
2472 btrfs_mark_buffer_dirty(leaf);
2475 if (btrfs_leaf_free_space(root->fs_info, leaf) < 0) {
2476 btrfs_print_leaf(root, leaf);
2483 * Given a key and some data, insert an item into the tree.
2484 * This does all the path init required, making room in the tree if needed.
2486 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2487 struct btrfs_root *root,
2488 struct btrfs_path *path,
2489 struct btrfs_key *cpu_key, u32 *data_size,
2492 struct extent_buffer *leaf;
2493 struct btrfs_item *item;
2500 unsigned int data_end;
2501 struct btrfs_disk_key disk_key;
2503 for (i = 0; i < nr; i++) {
2504 total_data += data_size[i];
2507 /* create a root if there isn't one */
2511 total_size = total_data + nr * sizeof(struct btrfs_item);
2512 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2519 leaf = path->nodes[0];
2521 nritems = btrfs_header_nritems(leaf);
2522 data_end = leaf_data_end(root->fs_info, leaf);
2524 if (btrfs_leaf_free_space(root->fs_info, leaf) < total_size) {
2525 btrfs_print_leaf(root, leaf);
2526 printk("not enough freespace need %u have %d\n",
2527 total_size, btrfs_leaf_free_space(root->fs_info, leaf));
2531 slot = path->slots[0];
2534 if (slot < nritems) {
2535 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2537 if (old_data < data_end) {
2538 btrfs_print_leaf(root, leaf);
2539 printk("slot %d old_data %d data_end %d\n",
2540 slot, old_data, data_end);
2544 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2546 /* first correct the data pointers */
2547 for (i = slot; i < nritems; i++) {
2550 item = btrfs_item_nr(i);
2551 ioff = btrfs_item_offset(leaf, item);
2552 btrfs_set_item_offset(leaf, item, ioff - total_data);
2555 /* shift the items */
2556 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2557 btrfs_item_nr_offset(slot),
2558 (nritems - slot) * sizeof(struct btrfs_item));
2560 /* shift the data */
2561 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2562 data_end - total_data, btrfs_leaf_data(leaf) +
2563 data_end, old_data - data_end);
2564 data_end = old_data;
2567 /* setup the item for the new data */
2568 for (i = 0; i < nr; i++) {
2569 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2570 btrfs_set_item_key(leaf, &disk_key, slot + i);
2571 item = btrfs_item_nr(slot + i);
2572 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2573 data_end -= data_size[i];
2574 btrfs_set_item_size(leaf, item, data_size[i]);
2576 btrfs_set_header_nritems(leaf, nritems + nr);
2577 btrfs_mark_buffer_dirty(leaf);
2581 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2582 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2585 if (btrfs_leaf_free_space(root->fs_info, leaf) < 0) {
2586 btrfs_print_leaf(root, leaf);
2595 * Given a key and some data, insert an item into the tree.
2596 * This does all the path init required, making room in the tree if needed.
2598 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2599 *root, struct btrfs_key *cpu_key, void *data, u32
2603 struct btrfs_path *path;
2604 struct extent_buffer *leaf;
2607 path = btrfs_alloc_path();
2611 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2613 leaf = path->nodes[0];
2614 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2615 write_extent_buffer(leaf, data, ptr, data_size);
2616 btrfs_mark_buffer_dirty(leaf);
2618 btrfs_free_path(path);
2623 * delete the pointer from a given node.
2625 * If the delete empties a node, the node is removed from the tree,
2626 * continuing all the way the root if required. The root is converted into
2627 * a leaf if all the nodes are emptied.
2629 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2630 int level, int slot)
2632 struct extent_buffer *parent = path->nodes[level];
2636 nritems = btrfs_header_nritems(parent);
2637 if (slot < nritems - 1) {
2638 /* shift the items */
2639 memmove_extent_buffer(parent,
2640 btrfs_node_key_ptr_offset(slot),
2641 btrfs_node_key_ptr_offset(slot + 1),
2642 sizeof(struct btrfs_key_ptr) *
2643 (nritems - slot - 1));
2646 btrfs_set_header_nritems(parent, nritems);
2647 if (nritems == 0 && parent == root->node) {
2648 BUG_ON(btrfs_header_level(root->node) != 1);
2649 /* just turn the root into a leaf and break */
2650 btrfs_set_header_level(root->node, 0);
2651 } else if (slot == 0) {
2652 struct btrfs_disk_key disk_key;
2654 btrfs_node_key(parent, &disk_key, 0);
2655 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2657 btrfs_mark_buffer_dirty(parent);
2662 * a helper function to delete the leaf pointed to by path->slots[1] and
2665 * This deletes the pointer in path->nodes[1] and frees the leaf
2666 * block extent. zero is returned if it all worked out, < 0 otherwise.
2668 * The path must have already been setup for deleting the leaf, including
2669 * all the proper balancing. path->nodes[1] must be locked.
2671 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2672 struct btrfs_root *root,
2673 struct btrfs_path *path,
2674 struct extent_buffer *leaf)
2678 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2679 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2683 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2684 0, root->root_key.objectid, 0, 0);
2689 * delete the item at the leaf level in path. If that empties
2690 * the leaf, remove it from the tree
2692 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2693 struct btrfs_path *path, int slot, int nr)
2695 struct extent_buffer *leaf;
2696 struct btrfs_item *item;
2704 leaf = path->nodes[0];
2705 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2707 for (i = 0; i < nr; i++)
2708 dsize += btrfs_item_size_nr(leaf, slot + i);
2710 nritems = btrfs_header_nritems(leaf);
2712 if (slot + nr != nritems) {
2713 int data_end = leaf_data_end(root->fs_info, leaf);
2715 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2717 btrfs_leaf_data(leaf) + data_end,
2718 last_off - data_end);
2720 for (i = slot + nr; i < nritems; i++) {
2723 item = btrfs_item_nr(i);
2724 ioff = btrfs_item_offset(leaf, item);
2725 btrfs_set_item_offset(leaf, item, ioff + dsize);
2728 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2729 btrfs_item_nr_offset(slot + nr),
2730 sizeof(struct btrfs_item) *
2731 (nritems - slot - nr));
2733 btrfs_set_header_nritems(leaf, nritems - nr);
2736 /* delete the leaf if we've emptied it */
2738 if (leaf == root->node) {
2739 btrfs_set_header_level(leaf, 0);
2741 clean_tree_block(trans, root, leaf);
2742 wret = btrfs_del_leaf(trans, root, path, leaf);
2748 int used = leaf_space_used(leaf, 0, nritems);
2750 struct btrfs_disk_key disk_key;
2752 btrfs_item_key(leaf, &disk_key, 0);
2753 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2756 /* delete the leaf if it is mostly empty */
2757 if (used < BTRFS_LEAF_DATA_SIZE(root->fs_info) / 4) {
2758 /* push_leaf_left fixes the path.
2759 * make sure the path still points to our leaf
2760 * for possible call to del_ptr below
2762 slot = path->slots[1];
2763 extent_buffer_get(leaf);
2765 wret = push_leaf_left(trans, root, path, 1, 1);
2766 if (wret < 0 && wret != -ENOSPC)
2769 if (path->nodes[0] == leaf &&
2770 btrfs_header_nritems(leaf)) {
2771 wret = push_leaf_right(trans, root, path, 1, 1);
2772 if (wret < 0 && wret != -ENOSPC)
2776 if (btrfs_header_nritems(leaf) == 0) {
2777 clean_tree_block(trans, root, leaf);
2778 path->slots[1] = slot;
2779 ret = btrfs_del_leaf(trans, root, path, leaf);
2781 free_extent_buffer(leaf);
2784 btrfs_mark_buffer_dirty(leaf);
2785 free_extent_buffer(leaf);
2788 btrfs_mark_buffer_dirty(leaf);
2795 * walk up the tree as far as required to find the previous leaf.
2796 * returns 0 if it found something or 1 if there are no lesser leaves.
2797 * returns < 0 on io errors.
2799 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2803 struct extent_buffer *c;
2804 struct extent_buffer *next = NULL;
2805 struct btrfs_fs_info *fs_info = root->fs_info;
2807 while(level < BTRFS_MAX_LEVEL) {
2808 if (!path->nodes[level])
2811 slot = path->slots[level];
2812 c = path->nodes[level];
2815 if (level == BTRFS_MAX_LEVEL)
2821 next = read_node_slot(fs_info, c, slot);
2822 if (!extent_buffer_uptodate(next)) {
2824 return PTR_ERR(next);
2829 path->slots[level] = slot;
2832 c = path->nodes[level];
2833 free_extent_buffer(c);
2834 slot = btrfs_header_nritems(next);
2837 path->nodes[level] = next;
2838 path->slots[level] = slot;
2841 next = read_node_slot(fs_info, next, slot);
2842 if (!extent_buffer_uptodate(next)) {
2844 return PTR_ERR(next);
2852 * walk up the tree as far as required to find the next leaf.
2853 * returns 0 if it found something or 1 if there are no greater leaves.
2854 * returns < 0 on io errors.
2856 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2860 struct extent_buffer *c;
2861 struct extent_buffer *next = NULL;
2862 struct btrfs_fs_info *fs_info = root->fs_info;
2864 while(level < BTRFS_MAX_LEVEL) {
2865 if (!path->nodes[level])
2868 slot = path->slots[level] + 1;
2869 c = path->nodes[level];
2870 if (slot >= btrfs_header_nritems(c)) {
2872 if (level == BTRFS_MAX_LEVEL)
2878 reada_for_search(root, path, level, slot, 0);
2880 next = read_node_slot(fs_info, c, slot);
2881 if (!extent_buffer_uptodate(next))
2885 path->slots[level] = slot;
2888 c = path->nodes[level];
2889 free_extent_buffer(c);
2890 path->nodes[level] = next;
2891 path->slots[level] = 0;
2895 reada_for_search(root, path, level, 0, 0);
2896 next = read_node_slot(fs_info, next, 0);
2897 if (!extent_buffer_uptodate(next))
2903 int btrfs_previous_item(struct btrfs_root *root,
2904 struct btrfs_path *path, u64 min_objectid,
2907 struct btrfs_key found_key;
2908 struct extent_buffer *leaf;
2913 if (path->slots[0] == 0) {
2914 ret = btrfs_prev_leaf(root, path);
2920 leaf = path->nodes[0];
2921 nritems = btrfs_header_nritems(leaf);
2924 if (path->slots[0] == nritems)
2927 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2928 if (found_key.objectid < min_objectid)
2930 if (found_key.type == type)
2932 if (found_key.objectid == min_objectid &&
2933 found_key.type < type)
2940 * search in extent tree to find a previous Metadata/Data extent item with
2943 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2945 int btrfs_previous_extent_item(struct btrfs_root *root,
2946 struct btrfs_path *path, u64 min_objectid)
2948 struct btrfs_key found_key;
2949 struct extent_buffer *leaf;
2954 if (path->slots[0] == 0) {
2955 ret = btrfs_prev_leaf(root, path);
2961 leaf = path->nodes[0];
2962 nritems = btrfs_header_nritems(leaf);
2965 if (path->slots[0] == nritems)
2968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2969 if (found_key.objectid < min_objectid)
2971 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2972 found_key.type == BTRFS_METADATA_ITEM_KEY)
2974 if (found_key.objectid == min_objectid &&
2975 found_key.type < BTRFS_EXTENT_ITEM_KEY)
2982 * Search in extent tree to found next meta/data extent
2983 * Caller needs to check for no-hole or skinny metadata features.
2985 int btrfs_next_extent_item(struct btrfs_root *root,
2986 struct btrfs_path *path, u64 max_objectid)
2988 struct btrfs_key found_key;
2992 ret = btrfs_next_item(root, path);
2995 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2997 if (found_key.objectid > max_objectid)
2999 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3000 found_key.type == BTRFS_METADATA_ITEM_KEY)