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"
26 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_key *ins_key,
30 struct btrfs_path *path, int data_size, int extend);
31 static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
33 struct extent_buffer *src, int empty);
34 static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
39 inline void btrfs_init_path(struct btrfs_path *p)
41 memset(p, 0, sizeof(*p));
44 struct btrfs_path *btrfs_alloc_path(void)
46 struct btrfs_path *path;
47 path = kzalloc(sizeof(struct btrfs_path), GFP_NOFS);
51 void btrfs_free_path(struct btrfs_path *p)
55 btrfs_release_path(p);
59 void btrfs_release_path(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 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 btrfs_header_fsid(), BTRFS_FSID_SIZE);
127 WARN_ON(btrfs_header_generation(buf) > trans->transid);
128 ret = btrfs_inc_ref(trans, new_root, cow, 0);
134 btrfs_mark_buffer_dirty(cow);
140 * check if the tree block can be shared by multiple trees
142 static int btrfs_block_can_be_shared(struct btrfs_root *root,
143 struct extent_buffer *buf)
146 * Tree blocks not in reference counted trees and tree roots
147 * are never shared. If a block was allocated after the last
148 * snapshot and the block was not allocated by tree relocation,
149 * we know the block is not shared.
151 if (root->ref_cows &&
152 buf != root->node && buf != root->commit_root &&
153 (btrfs_header_generation(buf) <=
154 btrfs_root_last_snapshot(&root->root_item) ||
155 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
157 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
158 if (root->ref_cows &&
159 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
165 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
166 struct btrfs_root *root,
167 struct extent_buffer *buf,
168 struct extent_buffer *cow)
177 * Backrefs update rules:
179 * Always use full backrefs for extent pointers in tree block
180 * allocated by tree relocation.
182 * If a shared tree block is no longer referenced by its owner
183 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
184 * use full backrefs for extent pointers in tree block.
186 * If a tree block is been relocating
187 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
188 * use full backrefs for extent pointers in tree block.
189 * The reason for this is some operations (such as drop tree)
190 * are only allowed for blocks use full backrefs.
193 if (btrfs_block_can_be_shared(root, buf)) {
194 ret = btrfs_lookup_extent_info(trans, root, buf->start,
195 btrfs_header_level(buf), 1,
201 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
202 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
203 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
208 owner = btrfs_header_owner(buf);
209 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
210 owner == BTRFS_TREE_RELOC_OBJECTID);
213 if ((owner == root->root_key.objectid ||
214 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
215 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
216 ret = btrfs_inc_ref(trans, root, buf, 1);
219 if (root->root_key.objectid ==
220 BTRFS_TREE_RELOC_OBJECTID) {
221 ret = btrfs_dec_ref(trans, root, buf, 0);
223 ret = btrfs_inc_ref(trans, root, cow, 1);
226 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
229 if (root->root_key.objectid ==
230 BTRFS_TREE_RELOC_OBJECTID)
231 ret = btrfs_inc_ref(trans, root, cow, 1);
233 ret = btrfs_inc_ref(trans, root, cow, 0);
236 if (new_flags != 0) {
237 ret = btrfs_set_block_flags(trans, root, buf->start,
238 btrfs_header_level(buf),
243 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
244 if (root->root_key.objectid ==
245 BTRFS_TREE_RELOC_OBJECTID)
246 ret = btrfs_inc_ref(trans, root, cow, 1);
248 ret = btrfs_inc_ref(trans, root, cow, 0);
250 ret = btrfs_dec_ref(trans, root, buf, 1);
253 clean_tree_block(trans, root, buf);
258 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
259 struct btrfs_root *root,
260 struct extent_buffer *buf,
261 struct extent_buffer *parent, int parent_slot,
262 struct extent_buffer **cow_ret,
263 u64 search_start, u64 empty_size)
265 struct extent_buffer *cow;
266 struct btrfs_disk_key disk_key;
269 WARN_ON(root->ref_cows && trans->transid !=
270 root->fs_info->running_transaction->transid);
271 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
273 level = btrfs_header_level(buf);
276 btrfs_item_key(buf, &disk_key, 0);
278 btrfs_node_key(buf, &disk_key, 0);
280 cow = btrfs_alloc_free_block(trans, root, buf->len,
281 root->root_key.objectid, &disk_key,
282 level, search_start, empty_size);
286 copy_extent_buffer(cow, buf, 0, 0, cow->len);
287 btrfs_set_header_bytenr(cow, cow->start);
288 btrfs_set_header_generation(cow, trans->transid);
289 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
290 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
291 BTRFS_HEADER_FLAG_RELOC);
292 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
293 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
295 btrfs_set_header_owner(cow, root->root_key.objectid);
297 write_extent_buffer(cow, root->fs_info->fsid,
298 btrfs_header_fsid(), BTRFS_FSID_SIZE);
300 WARN_ON(!(buf->flags & EXTENT_BAD_TRANSID) &&
301 btrfs_header_generation(buf) > trans->transid);
303 update_ref_for_cow(trans, root, buf, cow);
305 if (buf == root->node) {
307 extent_buffer_get(cow);
309 btrfs_free_extent(trans, root, buf->start, buf->len,
310 0, root->root_key.objectid, level, 0);
311 free_extent_buffer(buf);
312 add_root_to_dirty_list(root);
314 btrfs_set_node_blockptr(parent, parent_slot,
316 WARN_ON(trans->transid == 0);
317 btrfs_set_node_ptr_generation(parent, parent_slot,
319 btrfs_mark_buffer_dirty(parent);
320 WARN_ON(btrfs_header_generation(parent) != trans->transid);
322 btrfs_free_extent(trans, root, buf->start, buf->len,
323 0, root->root_key.objectid, level, 1);
325 if (!list_empty(&buf->recow)) {
326 list_del_init(&buf->recow);
327 free_extent_buffer(buf);
329 free_extent_buffer(buf);
330 btrfs_mark_buffer_dirty(cow);
335 static inline int should_cow_block(struct btrfs_trans_handle *trans,
336 struct btrfs_root *root,
337 struct extent_buffer *buf)
339 if (btrfs_header_generation(buf) == trans->transid &&
340 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
341 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
342 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
347 int btrfs_cow_block(struct btrfs_trans_handle *trans,
348 struct btrfs_root *root, struct extent_buffer *buf,
349 struct extent_buffer *parent, int parent_slot,
350 struct extent_buffer **cow_ret)
355 if (trans->transaction != root->fs_info->running_transaction) {
356 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
357 root->fs_info->running_transaction->transid);
361 if (trans->transid != root->fs_info->generation) {
362 printk(KERN_CRIT "trans %llu running %llu\n",
363 (unsigned long long)trans->transid,
364 (unsigned long long)root->fs_info->generation);
367 if (!should_cow_block(trans, root, buf)) {
372 search_start = buf->start & ~((u64)SZ_1G - 1);
373 ret = __btrfs_cow_block(trans, root, buf, parent,
374 parent_slot, cow_ret, search_start, 0);
378 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
380 if (k1->objectid > k2->objectid)
382 if (k1->objectid < k2->objectid)
384 if (k1->type > k2->type)
386 if (k1->type < k2->type)
388 if (k1->offset > k2->offset)
390 if (k1->offset < k2->offset)
396 * compare two keys in a memcmp fashion
398 static int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
402 btrfs_disk_key_to_cpu(&k1, disk);
403 return btrfs_comp_cpu_keys(&k1, k2);
407 * The leaf data grows from end-to-front in the node.
408 * this returns the address of the start of the last item,
409 * which is the stop of the leaf data stack
411 static inline unsigned int leaf_data_end(struct btrfs_root *root,
412 struct extent_buffer *leaf)
414 u32 nr = btrfs_header_nritems(leaf);
416 return BTRFS_LEAF_DATA_SIZE(root);
417 return btrfs_item_offset_nr(leaf, nr - 1);
420 enum btrfs_tree_block_status
421 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
422 struct extent_buffer *buf)
425 struct btrfs_key cpukey;
426 struct btrfs_disk_key key;
427 u32 nritems = btrfs_header_nritems(buf);
428 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
430 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root))
433 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
434 if (parent_key && parent_key->type) {
435 btrfs_node_key(buf, &key, 0);
436 if (memcmp(parent_key, &key, sizeof(key)))
439 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
440 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
441 btrfs_node_key(buf, &key, i);
442 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
443 if (btrfs_comp_keys(&key, &cpukey) >= 0)
446 return BTRFS_TREE_BLOCK_CLEAN;
448 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
450 btrfs_disk_key_to_cpu(&cpukey, parent_key);
452 btrfs_node_key_to_cpu(buf, &cpukey, 0);
453 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
454 buf->start, buf->len,
455 btrfs_header_level(buf));
460 enum btrfs_tree_block_status
461 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
462 struct extent_buffer *buf)
465 struct btrfs_key cpukey;
466 struct btrfs_disk_key key;
467 u32 nritems = btrfs_header_nritems(buf);
468 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
470 if (nritems * sizeof(struct btrfs_item) > buf->len) {
471 fprintf(stderr, "invalid number of items %llu\n",
472 (unsigned long long)buf->start);
476 if (btrfs_header_level(buf) != 0) {
477 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
478 fprintf(stderr, "leaf is not a leaf %llu\n",
479 (unsigned long long)btrfs_header_bytenr(buf));
482 if (btrfs_leaf_free_space(root, buf) < 0) {
483 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
484 fprintf(stderr, "leaf free space incorrect %llu %d\n",
485 (unsigned long long)btrfs_header_bytenr(buf),
486 btrfs_leaf_free_space(root, buf));
491 return BTRFS_TREE_BLOCK_CLEAN;
493 btrfs_item_key(buf, &key, 0);
494 if (parent_key && parent_key->type &&
495 memcmp(parent_key, &key, sizeof(key))) {
496 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
497 fprintf(stderr, "leaf parent key incorrect %llu\n",
498 (unsigned long long)btrfs_header_bytenr(buf));
501 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
502 btrfs_item_key(buf, &key, i);
503 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
504 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
505 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
506 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
509 if (btrfs_item_offset_nr(buf, i) !=
510 btrfs_item_end_nr(buf, i + 1)) {
511 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
512 fprintf(stderr, "incorrect offsets %u %u\n",
513 btrfs_item_offset_nr(buf, i),
514 btrfs_item_end_nr(buf, i + 1));
517 if (i == 0 && btrfs_item_end_nr(buf, i) !=
518 BTRFS_LEAF_DATA_SIZE(root)) {
519 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
520 fprintf(stderr, "bad item end %u wanted %u\n",
521 btrfs_item_end_nr(buf, i),
522 (unsigned)BTRFS_LEAF_DATA_SIZE(root));
527 for (i = 0; i < nritems; i++) {
528 if (btrfs_item_end_nr(buf, i) > BTRFS_LEAF_DATA_SIZE(root)) {
529 btrfs_item_key(buf, &key, 0);
530 btrfs_print_key(&key);
532 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
533 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
534 (unsigned long long)btrfs_item_end_nr(buf, i),
535 (unsigned long long)BTRFS_LEAF_DATA_SIZE(root));
540 return BTRFS_TREE_BLOCK_CLEAN;
542 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
544 btrfs_disk_key_to_cpu(&cpukey, parent_key);
546 btrfs_item_key_to_cpu(buf, &cpukey, 0);
548 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
549 buf->start, buf->len, 0);
554 static int noinline check_block(struct btrfs_root *root,
555 struct btrfs_path *path, int level)
557 struct btrfs_disk_key key;
558 struct btrfs_disk_key *key_ptr = NULL;
559 struct extent_buffer *parent;
560 enum btrfs_tree_block_status ret;
562 if (path->skip_check_block)
564 if (path->nodes[level + 1]) {
565 parent = path->nodes[level + 1];
566 btrfs_node_key(parent, &key, path->slots[level + 1]);
570 ret = btrfs_check_leaf(root, key_ptr, path->nodes[0]);
572 ret = btrfs_check_node(root, key_ptr, path->nodes[level]);
573 if (ret == BTRFS_TREE_BLOCK_CLEAN)
579 * search for key in the extent_buffer. The items start at offset p,
580 * and they are item_size apart. There are 'max' items in p.
582 * the slot in the array is returned via slot, and it points to
583 * the place where you would insert key if it is not found in
586 * slot may point to max if the key is bigger than all of the keys
588 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
589 int item_size, struct btrfs_key *key,
596 unsigned long offset;
597 struct btrfs_disk_key *tmp;
600 mid = (low + high) / 2;
601 offset = p + mid * item_size;
603 tmp = (struct btrfs_disk_key *)(eb->data + offset);
604 ret = btrfs_comp_keys(tmp, key);
620 * simple bin_search frontend that does the right thing for
623 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
624 int level, int *slot)
627 return generic_bin_search(eb,
628 offsetof(struct btrfs_leaf, items),
629 sizeof(struct btrfs_item),
630 key, btrfs_header_nritems(eb),
633 return generic_bin_search(eb,
634 offsetof(struct btrfs_node, ptrs),
635 sizeof(struct btrfs_key_ptr),
636 key, btrfs_header_nritems(eb),
640 struct extent_buffer *read_node_slot(struct btrfs_root *root,
641 struct extent_buffer *parent, int slot)
643 int level = btrfs_header_level(parent);
646 if (slot >= btrfs_header_nritems(parent))
652 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
654 btrfs_node_ptr_generation(parent, slot));
657 static int balance_level(struct btrfs_trans_handle *trans,
658 struct btrfs_root *root,
659 struct btrfs_path *path, int level)
661 struct extent_buffer *right = NULL;
662 struct extent_buffer *mid;
663 struct extent_buffer *left = NULL;
664 struct extent_buffer *parent = NULL;
668 int orig_slot = path->slots[level];
674 mid = path->nodes[level];
675 WARN_ON(btrfs_header_generation(mid) != trans->transid);
677 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
679 if (level < BTRFS_MAX_LEVEL - 1) {
680 parent = path->nodes[level + 1];
681 pslot = path->slots[level + 1];
685 * deal with the case where there is only one pointer in the root
686 * by promoting the node below to a root
689 struct extent_buffer *child;
691 if (btrfs_header_nritems(mid) != 1)
694 /* promote the child to a root */
695 child = read_node_slot(root, mid, 0);
696 BUG_ON(!extent_buffer_uptodate(child));
697 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
701 add_root_to_dirty_list(root);
702 path->nodes[level] = NULL;
703 clean_tree_block(trans, root, mid);
704 /* once for the path */
705 free_extent_buffer(mid);
707 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
708 0, root->root_key.objectid,
710 /* once for the root ptr */
711 free_extent_buffer(mid);
714 if (btrfs_header_nritems(mid) >
715 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
718 left = read_node_slot(root, parent, pslot - 1);
719 if (extent_buffer_uptodate(left)) {
720 wret = btrfs_cow_block(trans, root, left,
721 parent, pslot - 1, &left);
727 right = read_node_slot(root, parent, pslot + 1);
728 if (extent_buffer_uptodate(right)) {
729 wret = btrfs_cow_block(trans, root, right,
730 parent, pslot + 1, &right);
737 /* first, try to make some room in the middle buffer */
739 orig_slot += btrfs_header_nritems(left);
740 wret = push_node_left(trans, root, left, mid, 1);
746 * then try to empty the right most buffer into the middle
749 wret = push_node_left(trans, root, mid, right, 1);
750 if (wret < 0 && wret != -ENOSPC)
752 if (btrfs_header_nritems(right) == 0) {
753 u64 bytenr = right->start;
754 u32 blocksize = right->len;
756 clean_tree_block(trans, root, right);
757 free_extent_buffer(right);
759 wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
762 wret = btrfs_free_extent(trans, root, bytenr,
764 root->root_key.objectid,
769 struct btrfs_disk_key right_key;
770 btrfs_node_key(right, &right_key, 0);
771 btrfs_set_node_key(parent, &right_key, pslot + 1);
772 btrfs_mark_buffer_dirty(parent);
775 if (btrfs_header_nritems(mid) == 1) {
777 * we're not allowed to leave a node with one item in the
778 * tree during a delete. A deletion from lower in the tree
779 * could try to delete the only pointer in this node.
780 * So, pull some keys from the left.
781 * There has to be a left pointer at this point because
782 * otherwise we would have pulled some pointers from the
786 wret = balance_node_right(trans, root, mid, left);
792 wret = push_node_left(trans, root, left, mid, 1);
798 if (btrfs_header_nritems(mid) == 0) {
799 /* we've managed to empty the middle node, drop it */
800 u64 bytenr = mid->start;
801 u32 blocksize = mid->len;
802 clean_tree_block(trans, root, mid);
803 free_extent_buffer(mid);
805 wret = btrfs_del_ptr(root, path, level + 1, pslot);
808 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
809 0, root->root_key.objectid,
814 /* update the parent key to reflect our changes */
815 struct btrfs_disk_key mid_key;
816 btrfs_node_key(mid, &mid_key, 0);
817 btrfs_set_node_key(parent, &mid_key, pslot);
818 btrfs_mark_buffer_dirty(parent);
821 /* update the path */
823 if (btrfs_header_nritems(left) > orig_slot) {
824 extent_buffer_get(left);
825 path->nodes[level] = left;
826 path->slots[level + 1] -= 1;
827 path->slots[level] = orig_slot;
829 free_extent_buffer(mid);
831 orig_slot -= btrfs_header_nritems(left);
832 path->slots[level] = orig_slot;
835 /* double check we haven't messed things up */
836 check_block(root, path, level);
838 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
842 free_extent_buffer(right);
844 free_extent_buffer(left);
848 /* returns zero if the push worked, non-zero otherwise */
849 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
850 struct btrfs_root *root,
851 struct btrfs_path *path, int level)
853 struct extent_buffer *right = NULL;
854 struct extent_buffer *mid;
855 struct extent_buffer *left = NULL;
856 struct extent_buffer *parent = NULL;
860 int orig_slot = path->slots[level];
865 mid = path->nodes[level];
866 WARN_ON(btrfs_header_generation(mid) != trans->transid);
868 if (level < BTRFS_MAX_LEVEL - 1) {
869 parent = path->nodes[level + 1];
870 pslot = path->slots[level + 1];
876 left = read_node_slot(root, parent, pslot - 1);
878 /* first, try to make some room in the middle buffer */
879 if (extent_buffer_uptodate(left)) {
881 left_nr = btrfs_header_nritems(left);
882 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
885 ret = btrfs_cow_block(trans, root, left, parent,
890 wret = push_node_left(trans, root,
897 struct btrfs_disk_key disk_key;
898 orig_slot += left_nr;
899 btrfs_node_key(mid, &disk_key, 0);
900 btrfs_set_node_key(parent, &disk_key, pslot);
901 btrfs_mark_buffer_dirty(parent);
902 if (btrfs_header_nritems(left) > orig_slot) {
903 path->nodes[level] = left;
904 path->slots[level + 1] -= 1;
905 path->slots[level] = orig_slot;
906 free_extent_buffer(mid);
909 btrfs_header_nritems(left);
910 path->slots[level] = orig_slot;
911 free_extent_buffer(left);
915 free_extent_buffer(left);
917 right= read_node_slot(root, parent, pslot + 1);
920 * then try to empty the right most buffer into the middle
922 if (extent_buffer_uptodate(right)) {
924 right_nr = btrfs_header_nritems(right);
925 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
928 ret = btrfs_cow_block(trans, root, right,
934 wret = balance_node_right(trans, root,
941 struct btrfs_disk_key disk_key;
943 btrfs_node_key(right, &disk_key, 0);
944 btrfs_set_node_key(parent, &disk_key, pslot + 1);
945 btrfs_mark_buffer_dirty(parent);
947 if (btrfs_header_nritems(mid) <= orig_slot) {
948 path->nodes[level] = right;
949 path->slots[level + 1] += 1;
950 path->slots[level] = orig_slot -
951 btrfs_header_nritems(mid);
952 free_extent_buffer(mid);
954 free_extent_buffer(right);
958 free_extent_buffer(right);
964 * readahead one full node of leaves
966 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
967 int level, int slot, u64 objectid)
969 struct extent_buffer *node;
970 struct btrfs_disk_key disk_key;
976 int direction = path->reada;
977 struct extent_buffer *eb;
985 if (!path->nodes[level])
988 node = path->nodes[level];
989 search = btrfs_node_blockptr(node, slot);
990 blocksize = root->nodesize;
991 eb = btrfs_find_tree_block(root, search, blocksize);
993 free_extent_buffer(eb);
997 highest_read = search;
998 lowest_read = search;
1000 nritems = btrfs_header_nritems(node);
1003 if (direction < 0) {
1007 } else if (direction > 0) {
1012 if (path->reada < 0 && objectid) {
1013 btrfs_node_key(node, &disk_key, nr);
1014 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1017 search = btrfs_node_blockptr(node, nr);
1018 if ((search >= lowest_read && search <= highest_read) ||
1019 (search < lowest_read && lowest_read - search <= 32768) ||
1020 (search > highest_read && search - highest_read <= 32768)) {
1021 readahead_tree_block(root, search, blocksize,
1022 btrfs_node_ptr_generation(node, nr));
1026 if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
1028 if(nread > SZ_1M || nscan > 128)
1031 if (search < lowest_read)
1032 lowest_read = search;
1033 if (search > highest_read)
1034 highest_read = search;
1038 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1039 u64 iobjectid, u64 ioff, u8 key_type,
1040 struct btrfs_key *found_key)
1043 struct btrfs_key key;
1044 struct extent_buffer *eb;
1045 struct btrfs_path *path;
1047 key.type = key_type;
1048 key.objectid = iobjectid;
1051 if (found_path == NULL) {
1052 path = btrfs_alloc_path();
1058 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1059 if ((ret < 0) || (found_key == NULL))
1062 eb = path->nodes[0];
1063 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1064 ret = btrfs_next_leaf(fs_root, path);
1067 eb = path->nodes[0];
1070 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1071 if (found_key->type != key.type ||
1072 found_key->objectid != key.objectid) {
1078 if (path != found_path)
1079 btrfs_free_path(path);
1084 * look for key in the tree. path is filled in with nodes along the way
1085 * if key is found, we return zero and you can find the item in the leaf
1086 * level of the path (level 0)
1088 * If the key isn't found, the path points to the slot where it should
1089 * be inserted, and 1 is returned. If there are other errors during the
1090 * search a negative error number is returned.
1092 * if ins_len > 0, nodes and leaves will be split as we walk down the
1093 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1096 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1097 *root, struct btrfs_key *key, struct btrfs_path *p, int
1100 struct extent_buffer *b;
1104 int should_reada = p->reada;
1105 u8 lowest_level = 0;
1107 lowest_level = p->lowest_level;
1108 WARN_ON(lowest_level && ins_len > 0);
1109 WARN_ON(p->nodes[0] != NULL);
1111 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1115 extent_buffer_get(b);
1117 level = btrfs_header_level(b);
1120 wret = btrfs_cow_block(trans, root, b,
1121 p->nodes[level + 1],
1122 p->slots[level + 1],
1125 free_extent_buffer(b);
1129 BUG_ON(!cow && ins_len);
1130 if (level != btrfs_header_level(b))
1132 level = btrfs_header_level(b);
1133 p->nodes[level] = b;
1134 ret = check_block(root, p, level);
1137 ret = bin_search(b, key, level, &slot);
1139 if (ret && slot > 0)
1141 p->slots[level] = slot;
1142 if ((p->search_for_split || ins_len > 0) &&
1143 btrfs_header_nritems(b) >=
1144 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1145 int sret = split_node(trans, root, p, level);
1149 b = p->nodes[level];
1150 slot = p->slots[level];
1151 } else if (ins_len < 0) {
1152 int sret = balance_level(trans, root, p,
1156 b = p->nodes[level];
1158 btrfs_release_path(p);
1161 slot = p->slots[level];
1162 BUG_ON(btrfs_header_nritems(b) == 1);
1164 /* this is only true while dropping a snapshot */
1165 if (level == lowest_level)
1169 reada_for_search(root, p, level, slot,
1172 b = read_node_slot(root, b, slot);
1173 if (!extent_buffer_uptodate(b))
1176 p->slots[level] = slot;
1178 ins_len > btrfs_leaf_free_space(root, b)) {
1179 int sret = split_leaf(trans, root, key,
1180 p, ins_len, ret == 0);
1192 * adjust the pointers going up the tree, starting at level
1193 * making sure the right key of each node is points to 'key'.
1194 * This is used after shifting pointers to the left, so it stops
1195 * fixing up pointers when a given leaf/node is not in slot 0 of the
1198 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
1199 struct btrfs_disk_key *key, int level)
1202 struct extent_buffer *t;
1204 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1205 int tslot = path->slots[i];
1206 if (!path->nodes[i])
1209 btrfs_set_node_key(t, key, tslot);
1210 btrfs_mark_buffer_dirty(path->nodes[i]);
1219 * This function isn't completely safe. It's the caller's responsibility
1220 * that the new key won't break the order
1222 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
1223 struct btrfs_key *new_key)
1225 struct btrfs_disk_key disk_key;
1226 struct extent_buffer *eb;
1229 eb = path->nodes[0];
1230 slot = path->slots[0];
1232 btrfs_item_key(eb, &disk_key, slot - 1);
1233 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1236 if (slot < btrfs_header_nritems(eb) - 1) {
1237 btrfs_item_key(eb, &disk_key, slot + 1);
1238 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1242 btrfs_cpu_key_to_disk(&disk_key, new_key);
1243 btrfs_set_item_key(eb, &disk_key, slot);
1244 btrfs_mark_buffer_dirty(eb);
1246 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1251 * update an item key without the safety checks. This is meant to be called by
1254 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
1255 struct btrfs_path *path,
1256 struct btrfs_key *new_key)
1258 struct btrfs_disk_key disk_key;
1259 struct extent_buffer *eb;
1262 eb = path->nodes[0];
1263 slot = path->slots[0];
1265 btrfs_cpu_key_to_disk(&disk_key, new_key);
1266 btrfs_set_item_key(eb, &disk_key, slot);
1267 btrfs_mark_buffer_dirty(eb);
1269 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1273 * try to push data from one node into the next node left in the
1276 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1277 * error, and > 0 if there was no room in the left hand block.
1279 static int push_node_left(struct btrfs_trans_handle *trans,
1280 struct btrfs_root *root, struct extent_buffer *dst,
1281 struct extent_buffer *src, int empty)
1288 src_nritems = btrfs_header_nritems(src);
1289 dst_nritems = btrfs_header_nritems(dst);
1290 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1291 WARN_ON(btrfs_header_generation(src) != trans->transid);
1292 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1294 if (!empty && src_nritems <= 8)
1297 if (push_items <= 0) {
1302 push_items = min(src_nritems, push_items);
1303 if (push_items < src_nritems) {
1304 /* leave at least 8 pointers in the node if
1305 * we aren't going to empty it
1307 if (src_nritems - push_items < 8) {
1308 if (push_items <= 8)
1314 push_items = min(src_nritems - 8, push_items);
1316 copy_extent_buffer(dst, src,
1317 btrfs_node_key_ptr_offset(dst_nritems),
1318 btrfs_node_key_ptr_offset(0),
1319 push_items * sizeof(struct btrfs_key_ptr));
1321 if (push_items < src_nritems) {
1322 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1323 btrfs_node_key_ptr_offset(push_items),
1324 (src_nritems - push_items) *
1325 sizeof(struct btrfs_key_ptr));
1327 btrfs_set_header_nritems(src, src_nritems - push_items);
1328 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1329 btrfs_mark_buffer_dirty(src);
1330 btrfs_mark_buffer_dirty(dst);
1336 * try to push data from one node into the next node right in the
1339 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1340 * error, and > 0 if there was no room in the right hand block.
1342 * this will only push up to 1/2 the contents of the left node over
1344 static int balance_node_right(struct btrfs_trans_handle *trans,
1345 struct btrfs_root *root,
1346 struct extent_buffer *dst,
1347 struct extent_buffer *src)
1355 WARN_ON(btrfs_header_generation(src) != trans->transid);
1356 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1358 src_nritems = btrfs_header_nritems(src);
1359 dst_nritems = btrfs_header_nritems(dst);
1360 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1361 if (push_items <= 0) {
1365 if (src_nritems < 4) {
1369 max_push = src_nritems / 2 + 1;
1370 /* don't try to empty the node */
1371 if (max_push >= src_nritems) {
1375 if (max_push < push_items)
1376 push_items = max_push;
1378 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1379 btrfs_node_key_ptr_offset(0),
1381 sizeof(struct btrfs_key_ptr));
1383 copy_extent_buffer(dst, src,
1384 btrfs_node_key_ptr_offset(0),
1385 btrfs_node_key_ptr_offset(src_nritems - push_items),
1386 push_items * sizeof(struct btrfs_key_ptr));
1388 btrfs_set_header_nritems(src, src_nritems - push_items);
1389 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1391 btrfs_mark_buffer_dirty(src);
1392 btrfs_mark_buffer_dirty(dst);
1398 * helper function to insert a new root level in the tree.
1399 * A new node is allocated, and a single item is inserted to
1400 * point to the existing root
1402 * returns zero on success or < 0 on failure.
1404 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1405 struct btrfs_root *root,
1406 struct btrfs_path *path, int level)
1409 struct extent_buffer *lower;
1410 struct extent_buffer *c;
1411 struct extent_buffer *old;
1412 struct btrfs_disk_key lower_key;
1414 BUG_ON(path->nodes[level]);
1415 BUG_ON(path->nodes[level-1] != root->node);
1417 lower = path->nodes[level-1];
1419 btrfs_item_key(lower, &lower_key, 0);
1421 btrfs_node_key(lower, &lower_key, 0);
1423 c = btrfs_alloc_free_block(trans, root, root->nodesize,
1424 root->root_key.objectid, &lower_key,
1425 level, root->node->start, 0);
1430 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1431 btrfs_set_header_nritems(c, 1);
1432 btrfs_set_header_level(c, level);
1433 btrfs_set_header_bytenr(c, c->start);
1434 btrfs_set_header_generation(c, trans->transid);
1435 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1436 btrfs_set_header_owner(c, root->root_key.objectid);
1438 write_extent_buffer(c, root->fs_info->fsid,
1439 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1441 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1442 btrfs_header_chunk_tree_uuid(c),
1445 btrfs_set_node_key(c, &lower_key, 0);
1446 btrfs_set_node_blockptr(c, 0, lower->start);
1447 lower_gen = btrfs_header_generation(lower);
1448 WARN_ON(lower_gen != trans->transid);
1450 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1452 btrfs_mark_buffer_dirty(c);
1457 /* the super has an extra ref to root->node */
1458 free_extent_buffer(old);
1460 add_root_to_dirty_list(root);
1461 extent_buffer_get(c);
1462 path->nodes[level] = c;
1463 path->slots[level] = 0;
1468 * worker function to insert a single pointer in a node.
1469 * the node should have enough room for the pointer already
1471 * slot and level indicate where you want the key to go, and
1472 * blocknr is the block the key points to.
1474 * returns zero on success and < 0 on any error
1476 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1477 *root, struct btrfs_path *path, struct btrfs_disk_key
1478 *key, u64 bytenr, int slot, int level)
1480 struct extent_buffer *lower;
1483 BUG_ON(!path->nodes[level]);
1484 lower = path->nodes[level];
1485 nritems = btrfs_header_nritems(lower);
1488 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1490 if (slot != nritems) {
1491 memmove_extent_buffer(lower,
1492 btrfs_node_key_ptr_offset(slot + 1),
1493 btrfs_node_key_ptr_offset(slot),
1494 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1496 btrfs_set_node_key(lower, key, slot);
1497 btrfs_set_node_blockptr(lower, slot, bytenr);
1498 WARN_ON(trans->transid == 0);
1499 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1500 btrfs_set_header_nritems(lower, nritems + 1);
1501 btrfs_mark_buffer_dirty(lower);
1506 * split the node at the specified level in path in two.
1507 * The path is corrected to point to the appropriate node after the split
1509 * Before splitting this tries to make some room in the node by pushing
1510 * left and right, if either one works, it returns right away.
1512 * returns 0 on success and < 0 on failure
1514 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1515 *root, struct btrfs_path *path, int level)
1517 struct extent_buffer *c;
1518 struct extent_buffer *split;
1519 struct btrfs_disk_key disk_key;
1525 c = path->nodes[level];
1526 WARN_ON(btrfs_header_generation(c) != trans->transid);
1527 if (c == root->node) {
1528 /* trying to split the root, lets make a new one */
1529 ret = insert_new_root(trans, root, path, level + 1);
1533 ret = push_nodes_for_insert(trans, root, path, level);
1534 c = path->nodes[level];
1535 if (!ret && btrfs_header_nritems(c) <
1536 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1542 c_nritems = btrfs_header_nritems(c);
1543 mid = (c_nritems + 1) / 2;
1544 btrfs_node_key(c, &disk_key, mid);
1546 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1547 root->root_key.objectid,
1548 &disk_key, level, c->start, 0);
1550 return PTR_ERR(split);
1552 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1553 btrfs_set_header_level(split, btrfs_header_level(c));
1554 btrfs_set_header_bytenr(split, split->start);
1555 btrfs_set_header_generation(split, trans->transid);
1556 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1557 btrfs_set_header_owner(split, root->root_key.objectid);
1558 write_extent_buffer(split, root->fs_info->fsid,
1559 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1560 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1561 btrfs_header_chunk_tree_uuid(split),
1565 copy_extent_buffer(split, c,
1566 btrfs_node_key_ptr_offset(0),
1567 btrfs_node_key_ptr_offset(mid),
1568 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1569 btrfs_set_header_nritems(split, c_nritems - mid);
1570 btrfs_set_header_nritems(c, mid);
1573 btrfs_mark_buffer_dirty(c);
1574 btrfs_mark_buffer_dirty(split);
1576 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1577 path->slots[level + 1] + 1,
1582 if (path->slots[level] >= mid) {
1583 path->slots[level] -= mid;
1584 free_extent_buffer(c);
1585 path->nodes[level] = split;
1586 path->slots[level + 1] += 1;
1588 free_extent_buffer(split);
1594 * how many bytes are required to store the items in a leaf. start
1595 * and nr indicate which items in the leaf to check. This totals up the
1596 * space used both by the item structs and the item data
1598 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1601 int nritems = btrfs_header_nritems(l);
1602 int end = min(nritems, start + nr) - 1;
1606 data_len = btrfs_item_end_nr(l, start);
1607 data_len = data_len - btrfs_item_offset_nr(l, end);
1608 data_len += sizeof(struct btrfs_item) * nr;
1609 WARN_ON(data_len < 0);
1614 * The space between the end of the leaf items and
1615 * the start of the leaf data. IOW, how much room
1616 * the leaf has left for both items and data
1618 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1620 u32 nodesize = (root ? BTRFS_LEAF_DATA_SIZE(root) : leaf->len);
1621 int nritems = btrfs_header_nritems(leaf);
1623 ret = nodesize - leaf_space_used(leaf, 0, nritems);
1625 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1626 ret, nodesize, leaf_space_used(leaf, 0, nritems),
1633 * push some data in the path leaf to the right, trying to free up at
1634 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1636 * returns 1 if the push failed because the other node didn't have enough
1637 * room, 0 if everything worked out and < 0 if there were major errors.
1639 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1640 *root, struct btrfs_path *path, int data_size,
1643 struct extent_buffer *left = path->nodes[0];
1644 struct extent_buffer *right;
1645 struct extent_buffer *upper;
1646 struct btrfs_disk_key disk_key;
1652 struct btrfs_item *item;
1660 slot = path->slots[1];
1661 if (!path->nodes[1]) {
1664 upper = path->nodes[1];
1665 if (slot >= btrfs_header_nritems(upper) - 1)
1668 right = read_node_slot(root, upper, slot + 1);
1669 if (!extent_buffer_uptodate(right)) {
1671 return PTR_ERR(right);
1674 free_space = btrfs_leaf_free_space(root, right);
1675 if (free_space < data_size) {
1676 free_extent_buffer(right);
1680 /* cow and double check */
1681 ret = btrfs_cow_block(trans, root, right, upper,
1684 free_extent_buffer(right);
1687 free_space = btrfs_leaf_free_space(root, right);
1688 if (free_space < data_size) {
1689 free_extent_buffer(right);
1693 left_nritems = btrfs_header_nritems(left);
1694 if (left_nritems == 0) {
1695 free_extent_buffer(right);
1704 i = left_nritems - 1;
1706 item = btrfs_item_nr(i);
1708 if (path->slots[0] == i)
1709 push_space += data_size + sizeof(*item);
1711 this_item_size = btrfs_item_size(left, item);
1712 if (this_item_size + sizeof(*item) + push_space > free_space)
1715 push_space += this_item_size + sizeof(*item);
1721 if (push_items == 0) {
1722 free_extent_buffer(right);
1726 if (!empty && push_items == left_nritems)
1729 /* push left to right */
1730 right_nritems = btrfs_header_nritems(right);
1732 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1733 push_space -= leaf_data_end(root, left);
1735 /* make room in the right data area */
1736 data_end = leaf_data_end(root, right);
1737 memmove_extent_buffer(right,
1738 btrfs_leaf_data(right) + data_end - push_space,
1739 btrfs_leaf_data(right) + data_end,
1740 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1742 /* copy from the left data area */
1743 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1744 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1745 btrfs_leaf_data(left) + leaf_data_end(root, left),
1748 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1749 btrfs_item_nr_offset(0),
1750 right_nritems * sizeof(struct btrfs_item));
1752 /* copy the items from left to right */
1753 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1754 btrfs_item_nr_offset(left_nritems - push_items),
1755 push_items * sizeof(struct btrfs_item));
1757 /* update the item pointers */
1758 right_nritems += push_items;
1759 btrfs_set_header_nritems(right, right_nritems);
1760 push_space = BTRFS_LEAF_DATA_SIZE(root);
1761 for (i = 0; i < right_nritems; i++) {
1762 item = btrfs_item_nr(i);
1763 push_space -= btrfs_item_size(right, item);
1764 btrfs_set_item_offset(right, item, push_space);
1767 left_nritems -= push_items;
1768 btrfs_set_header_nritems(left, left_nritems);
1771 btrfs_mark_buffer_dirty(left);
1772 btrfs_mark_buffer_dirty(right);
1774 btrfs_item_key(right, &disk_key, 0);
1775 btrfs_set_node_key(upper, &disk_key, slot + 1);
1776 btrfs_mark_buffer_dirty(upper);
1778 /* then fixup the leaf pointer in the path */
1779 if (path->slots[0] >= left_nritems) {
1780 path->slots[0] -= left_nritems;
1781 free_extent_buffer(path->nodes[0]);
1782 path->nodes[0] = right;
1783 path->slots[1] += 1;
1785 free_extent_buffer(right);
1790 * push some data in the path leaf to the left, trying to free up at
1791 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1793 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1794 *root, struct btrfs_path *path, int data_size,
1797 struct btrfs_disk_key disk_key;
1798 struct extent_buffer *right = path->nodes[0];
1799 struct extent_buffer *left;
1805 struct btrfs_item *item;
1806 u32 old_left_nritems;
1811 u32 old_left_item_size;
1813 slot = path->slots[1];
1816 if (!path->nodes[1])
1819 right_nritems = btrfs_header_nritems(right);
1820 if (right_nritems == 0) {
1824 left = read_node_slot(root, path->nodes[1], slot - 1);
1825 free_space = btrfs_leaf_free_space(root, left);
1826 if (free_space < data_size) {
1827 free_extent_buffer(left);
1831 /* cow and double check */
1832 ret = btrfs_cow_block(trans, root, left,
1833 path->nodes[1], slot - 1, &left);
1835 /* we hit -ENOSPC, but it isn't fatal here */
1836 free_extent_buffer(left);
1840 free_space = btrfs_leaf_free_space(root, left);
1841 if (free_space < data_size) {
1842 free_extent_buffer(left);
1849 nr = right_nritems - 1;
1851 for (i = 0; i < nr; i++) {
1852 item = btrfs_item_nr(i);
1854 if (path->slots[0] == i)
1855 push_space += data_size + sizeof(*item);
1857 this_item_size = btrfs_item_size(right, item);
1858 if (this_item_size + sizeof(*item) + push_space > free_space)
1862 push_space += this_item_size + sizeof(*item);
1865 if (push_items == 0) {
1866 free_extent_buffer(left);
1869 if (!empty && push_items == btrfs_header_nritems(right))
1872 /* push data from right to left */
1873 copy_extent_buffer(left, right,
1874 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1875 btrfs_item_nr_offset(0),
1876 push_items * sizeof(struct btrfs_item));
1878 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1879 btrfs_item_offset_nr(right, push_items -1);
1881 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1882 leaf_data_end(root, left) - push_space,
1883 btrfs_leaf_data(right) +
1884 btrfs_item_offset_nr(right, push_items - 1),
1886 old_left_nritems = btrfs_header_nritems(left);
1887 BUG_ON(old_left_nritems == 0);
1889 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1890 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1893 item = btrfs_item_nr(i);
1894 ioff = btrfs_item_offset(left, item);
1895 btrfs_set_item_offset(left, item,
1896 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1898 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1900 /* fixup right node */
1901 if (push_items > right_nritems) {
1902 printk("push items %d nr %u\n", push_items, right_nritems);
1906 if (push_items < right_nritems) {
1907 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1908 leaf_data_end(root, right);
1909 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1910 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1911 btrfs_leaf_data(right) +
1912 leaf_data_end(root, right), push_space);
1914 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1915 btrfs_item_nr_offset(push_items),
1916 (btrfs_header_nritems(right) - push_items) *
1917 sizeof(struct btrfs_item));
1919 right_nritems -= push_items;
1920 btrfs_set_header_nritems(right, right_nritems);
1921 push_space = BTRFS_LEAF_DATA_SIZE(root);
1922 for (i = 0; i < right_nritems; i++) {
1923 item = btrfs_item_nr(i);
1924 push_space = push_space - btrfs_item_size(right, item);
1925 btrfs_set_item_offset(right, item, push_space);
1928 btrfs_mark_buffer_dirty(left);
1930 btrfs_mark_buffer_dirty(right);
1932 btrfs_item_key(right, &disk_key, 0);
1933 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1935 /* then fixup the leaf pointer in the path */
1936 if (path->slots[0] < push_items) {
1937 path->slots[0] += old_left_nritems;
1938 free_extent_buffer(path->nodes[0]);
1939 path->nodes[0] = left;
1940 path->slots[1] -= 1;
1942 free_extent_buffer(left);
1943 path->slots[0] -= push_items;
1945 BUG_ON(path->slots[0] < 0);
1950 * split the path's leaf in two, making sure there is at least data_size
1951 * available for the resulting leaf level of the path.
1953 * returns 0 if all went well and < 0 on failure.
1955 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
1956 struct btrfs_root *root,
1957 struct btrfs_path *path,
1958 struct extent_buffer *l,
1959 struct extent_buffer *right,
1960 int slot, int mid, int nritems)
1967 struct btrfs_disk_key disk_key;
1969 nritems = nritems - mid;
1970 btrfs_set_header_nritems(right, nritems);
1971 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1973 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1974 btrfs_item_nr_offset(mid),
1975 nritems * sizeof(struct btrfs_item));
1977 copy_extent_buffer(right, l,
1978 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1979 data_copy_size, btrfs_leaf_data(l) +
1980 leaf_data_end(root, l), data_copy_size);
1982 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1983 btrfs_item_end_nr(l, mid);
1985 for (i = 0; i < nritems; i++) {
1986 struct btrfs_item *item = btrfs_item_nr(i);
1987 u32 ioff = btrfs_item_offset(right, item);
1988 btrfs_set_item_offset(right, item, ioff + rt_data_off);
1991 btrfs_set_header_nritems(l, mid);
1993 btrfs_item_key(right, &disk_key, 0);
1994 wret = insert_ptr(trans, root, path, &disk_key, right->start,
1995 path->slots[1] + 1, 1);
1999 btrfs_mark_buffer_dirty(right);
2000 btrfs_mark_buffer_dirty(l);
2001 BUG_ON(path->slots[0] != slot);
2004 free_extent_buffer(path->nodes[0]);
2005 path->nodes[0] = right;
2006 path->slots[0] -= mid;
2007 path->slots[1] += 1;
2009 free_extent_buffer(right);
2012 BUG_ON(path->slots[0] < 0);
2018 * split the path's leaf in two, making sure there is at least data_size
2019 * available for the resulting leaf level of the path.
2021 * returns 0 if all went well and < 0 on failure.
2023 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2024 struct btrfs_root *root,
2025 struct btrfs_key *ins_key,
2026 struct btrfs_path *path, int data_size,
2029 struct btrfs_disk_key disk_key;
2030 struct extent_buffer *l;
2034 struct extent_buffer *right;
2038 int num_doubles = 0;
2041 slot = path->slots[0];
2042 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2043 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2046 /* first try to make some room by pushing left and right */
2047 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2048 wret = push_leaf_right(trans, root, path, data_size, 0);
2052 wret = push_leaf_left(trans, root, path, data_size, 0);
2058 /* did the pushes work? */
2059 if (btrfs_leaf_free_space(root, l) >= data_size)
2063 if (!path->nodes[1]) {
2064 ret = insert_new_root(trans, root, path, 1);
2071 slot = path->slots[0];
2072 nritems = btrfs_header_nritems(l);
2073 mid = (nritems + 1) / 2;
2077 leaf_space_used(l, mid, nritems - mid) + data_size >
2078 BTRFS_LEAF_DATA_SIZE(root)) {
2079 if (slot >= nritems) {
2083 if (mid != nritems &&
2084 leaf_space_used(l, mid, nritems - mid) +
2085 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2091 if (leaf_space_used(l, 0, mid) + data_size >
2092 BTRFS_LEAF_DATA_SIZE(root)) {
2093 if (!extend && data_size && slot == 0) {
2095 } else if ((extend || !data_size) && slot == 0) {
2099 if (mid != nritems &&
2100 leaf_space_used(l, mid, nritems - mid) +
2101 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2109 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2111 btrfs_item_key(l, &disk_key, mid);
2113 right = btrfs_alloc_free_block(trans, root, root->nodesize,
2114 root->root_key.objectid,
2115 &disk_key, 0, l->start, 0);
2116 if (IS_ERR(right)) {
2118 return PTR_ERR(right);
2121 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2122 btrfs_set_header_bytenr(right, right->start);
2123 btrfs_set_header_generation(right, trans->transid);
2124 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2125 btrfs_set_header_owner(right, root->root_key.objectid);
2126 btrfs_set_header_level(right, 0);
2127 write_extent_buffer(right, root->fs_info->fsid,
2128 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2130 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2131 btrfs_header_chunk_tree_uuid(right),
2136 btrfs_set_header_nritems(right, 0);
2137 wret = insert_ptr(trans, root, path,
2138 &disk_key, right->start,
2139 path->slots[1] + 1, 1);
2143 free_extent_buffer(path->nodes[0]);
2144 path->nodes[0] = right;
2146 path->slots[1] += 1;
2148 btrfs_set_header_nritems(right, 0);
2149 wret = insert_ptr(trans, root, path,
2155 free_extent_buffer(path->nodes[0]);
2156 path->nodes[0] = right;
2158 if (path->slots[1] == 0) {
2159 btrfs_fixup_low_keys(root, path,
2163 btrfs_mark_buffer_dirty(right);
2167 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2171 BUG_ON(num_doubles != 0);
2180 * This function splits a single item into two items,
2181 * giving 'new_key' to the new item and splitting the
2182 * old one at split_offset (from the start of the item).
2184 * The path may be released by this operation. After
2185 * the split, the path is pointing to the old item. The
2186 * new item is going to be in the same node as the old one.
2188 * Note, the item being split must be smaller enough to live alone on
2189 * a tree block with room for one extra struct btrfs_item
2191 * This allows us to split the item in place, keeping a lock on the
2192 * leaf the entire time.
2194 int btrfs_split_item(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_path *path,
2197 struct btrfs_key *new_key,
2198 unsigned long split_offset)
2201 struct extent_buffer *leaf;
2202 struct btrfs_key orig_key;
2203 struct btrfs_item *item;
2204 struct btrfs_item *new_item;
2209 struct btrfs_disk_key disk_key;
2212 leaf = path->nodes[0];
2213 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2214 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2217 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2218 btrfs_release_path(path);
2220 path->search_for_split = 1;
2222 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2223 path->search_for_split = 0;
2225 /* if our item isn't there or got smaller, return now */
2226 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2231 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2234 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2235 leaf = path->nodes[0];
2238 item = btrfs_item_nr(path->slots[0]);
2239 orig_offset = btrfs_item_offset(leaf, item);
2240 item_size = btrfs_item_size(leaf, item);
2243 buf = kmalloc(item_size, GFP_NOFS);
2245 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2246 path->slots[0]), item_size);
2247 slot = path->slots[0] + 1;
2248 leaf = path->nodes[0];
2250 nritems = btrfs_header_nritems(leaf);
2252 if (slot != nritems) {
2253 /* shift the items */
2254 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2255 btrfs_item_nr_offset(slot),
2256 (nritems - slot) * sizeof(struct btrfs_item));
2260 btrfs_cpu_key_to_disk(&disk_key, new_key);
2261 btrfs_set_item_key(leaf, &disk_key, slot);
2263 new_item = btrfs_item_nr(slot);
2265 btrfs_set_item_offset(leaf, new_item, orig_offset);
2266 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2268 btrfs_set_item_offset(leaf, item,
2269 orig_offset + item_size - split_offset);
2270 btrfs_set_item_size(leaf, item, split_offset);
2272 btrfs_set_header_nritems(leaf, nritems + 1);
2274 /* write the data for the start of the original item */
2275 write_extent_buffer(leaf, buf,
2276 btrfs_item_ptr_offset(leaf, path->slots[0]),
2279 /* write the data for the new item */
2280 write_extent_buffer(leaf, buf + split_offset,
2281 btrfs_item_ptr_offset(leaf, slot),
2282 item_size - split_offset);
2283 btrfs_mark_buffer_dirty(leaf);
2286 if (btrfs_leaf_free_space(root, leaf) < 0) {
2287 btrfs_print_leaf(root, leaf);
2294 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2295 u32 new_size, int from_end)
2299 struct extent_buffer *leaf;
2300 struct btrfs_item *item;
2302 unsigned int data_end;
2303 unsigned int old_data_start;
2304 unsigned int old_size;
2305 unsigned int size_diff;
2308 leaf = path->nodes[0];
2309 slot = path->slots[0];
2311 old_size = btrfs_item_size_nr(leaf, slot);
2312 if (old_size == new_size)
2315 nritems = btrfs_header_nritems(leaf);
2316 data_end = leaf_data_end(root, leaf);
2318 old_data_start = btrfs_item_offset_nr(leaf, slot);
2320 size_diff = old_size - new_size;
2323 BUG_ON(slot >= nritems);
2326 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2328 /* first correct the data pointers */
2329 for (i = slot; i < nritems; i++) {
2331 item = btrfs_item_nr(i);
2332 ioff = btrfs_item_offset(leaf, item);
2333 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2336 /* shift the data */
2338 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2339 data_end + size_diff, btrfs_leaf_data(leaf) +
2340 data_end, old_data_start + new_size - data_end);
2342 struct btrfs_disk_key disk_key;
2345 btrfs_item_key(leaf, &disk_key, slot);
2347 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2349 struct btrfs_file_extent_item *fi;
2351 fi = btrfs_item_ptr(leaf, slot,
2352 struct btrfs_file_extent_item);
2353 fi = (struct btrfs_file_extent_item *)(
2354 (unsigned long)fi - size_diff);
2356 if (btrfs_file_extent_type(leaf, fi) ==
2357 BTRFS_FILE_EXTENT_INLINE) {
2358 ptr = btrfs_item_ptr_offset(leaf, slot);
2359 memmove_extent_buffer(leaf, ptr,
2361 offsetof(struct btrfs_file_extent_item,
2366 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2367 data_end + size_diff, btrfs_leaf_data(leaf) +
2368 data_end, old_data_start - data_end);
2370 offset = btrfs_disk_key_offset(&disk_key);
2371 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2372 btrfs_set_item_key(leaf, &disk_key, slot);
2374 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2377 item = btrfs_item_nr(slot);
2378 btrfs_set_item_size(leaf, item, new_size);
2379 btrfs_mark_buffer_dirty(leaf);
2382 if (btrfs_leaf_free_space(root, leaf) < 0) {
2383 btrfs_print_leaf(root, leaf);
2389 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2394 struct extent_buffer *leaf;
2395 struct btrfs_item *item;
2397 unsigned int data_end;
2398 unsigned int old_data;
2399 unsigned int old_size;
2402 leaf = path->nodes[0];
2404 nritems = btrfs_header_nritems(leaf);
2405 data_end = leaf_data_end(root, leaf);
2407 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2408 btrfs_print_leaf(root, leaf);
2411 slot = path->slots[0];
2412 old_data = btrfs_item_end_nr(leaf, slot);
2415 if (slot >= nritems) {
2416 btrfs_print_leaf(root, leaf);
2417 printk("slot %d too large, nritems %d\n", slot, nritems);
2422 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2424 /* first correct the data pointers */
2425 for (i = slot; i < nritems; i++) {
2427 item = btrfs_item_nr(i);
2428 ioff = btrfs_item_offset(leaf, item);
2429 btrfs_set_item_offset(leaf, item, ioff - data_size);
2432 /* shift the data */
2433 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2434 data_end - data_size, btrfs_leaf_data(leaf) +
2435 data_end, old_data - data_end);
2437 data_end = old_data;
2438 old_size = btrfs_item_size_nr(leaf, slot);
2439 item = btrfs_item_nr(slot);
2440 btrfs_set_item_size(leaf, item, old_size + data_size);
2441 btrfs_mark_buffer_dirty(leaf);
2444 if (btrfs_leaf_free_space(root, leaf) < 0) {
2445 btrfs_print_leaf(root, leaf);
2452 * Given a key and some data, insert an item into the tree.
2453 * This does all the path init required, making room in the tree if needed.
2455 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2456 struct btrfs_root *root,
2457 struct btrfs_path *path,
2458 struct btrfs_key *cpu_key, u32 *data_size,
2461 struct extent_buffer *leaf;
2462 struct btrfs_item *item;
2469 unsigned int data_end;
2470 struct btrfs_disk_key disk_key;
2472 for (i = 0; i < nr; i++) {
2473 total_data += data_size[i];
2476 /* create a root if there isn't one */
2480 total_size = total_data + nr * sizeof(struct btrfs_item);
2481 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2488 leaf = path->nodes[0];
2490 nritems = btrfs_header_nritems(leaf);
2491 data_end = leaf_data_end(root, leaf);
2493 if (btrfs_leaf_free_space(root, leaf) < total_size) {
2494 btrfs_print_leaf(root, leaf);
2495 printk("not enough freespace need %u have %d\n",
2496 total_size, btrfs_leaf_free_space(root, leaf));
2500 slot = path->slots[0];
2503 if (slot != nritems) {
2504 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2506 if (old_data < data_end) {
2507 btrfs_print_leaf(root, leaf);
2508 printk("slot %d old_data %d data_end %d\n",
2509 slot, old_data, data_end);
2513 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2515 /* first correct the data pointers */
2516 for (i = slot; i < nritems; i++) {
2519 item = btrfs_item_nr(i);
2520 ioff = btrfs_item_offset(leaf, item);
2521 btrfs_set_item_offset(leaf, item, ioff - total_data);
2524 /* shift the items */
2525 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2526 btrfs_item_nr_offset(slot),
2527 (nritems - slot) * sizeof(struct btrfs_item));
2529 /* shift the data */
2530 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2531 data_end - total_data, btrfs_leaf_data(leaf) +
2532 data_end, old_data - data_end);
2533 data_end = old_data;
2536 /* setup the item for the new data */
2537 for (i = 0; i < nr; i++) {
2538 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2539 btrfs_set_item_key(leaf, &disk_key, slot + i);
2540 item = btrfs_item_nr(slot + i);
2541 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2542 data_end -= data_size[i];
2543 btrfs_set_item_size(leaf, item, data_size[i]);
2545 btrfs_set_header_nritems(leaf, nritems + nr);
2546 btrfs_mark_buffer_dirty(leaf);
2550 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2551 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2554 if (btrfs_leaf_free_space(root, leaf) < 0) {
2555 btrfs_print_leaf(root, leaf);
2564 * Given a key and some data, insert an item into the tree.
2565 * This does all the path init required, making room in the tree if needed.
2567 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2568 *root, struct btrfs_key *cpu_key, void *data, u32
2572 struct btrfs_path *path;
2573 struct extent_buffer *leaf;
2576 path = btrfs_alloc_path();
2580 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2582 leaf = path->nodes[0];
2583 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2584 write_extent_buffer(leaf, data, ptr, data_size);
2585 btrfs_mark_buffer_dirty(leaf);
2587 btrfs_free_path(path);
2592 * delete the pointer from a given node.
2594 * If the delete empties a node, the node is removed from the tree,
2595 * continuing all the way the root if required. The root is converted into
2596 * a leaf if all the nodes are emptied.
2598 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2599 int level, int slot)
2601 struct extent_buffer *parent = path->nodes[level];
2605 nritems = btrfs_header_nritems(parent);
2606 if (slot != nritems -1) {
2607 memmove_extent_buffer(parent,
2608 btrfs_node_key_ptr_offset(slot),
2609 btrfs_node_key_ptr_offset(slot + 1),
2610 sizeof(struct btrfs_key_ptr) *
2611 (nritems - slot - 1));
2614 btrfs_set_header_nritems(parent, nritems);
2615 if (nritems == 0 && parent == root->node) {
2616 BUG_ON(btrfs_header_level(root->node) != 1);
2617 /* just turn the root into a leaf and break */
2618 btrfs_set_header_level(root->node, 0);
2619 } else if (slot == 0) {
2620 struct btrfs_disk_key disk_key;
2622 btrfs_node_key(parent, &disk_key, 0);
2623 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2625 btrfs_mark_buffer_dirty(parent);
2630 * a helper function to delete the leaf pointed to by path->slots[1] and
2633 * This deletes the pointer in path->nodes[1] and frees the leaf
2634 * block extent. zero is returned if it all worked out, < 0 otherwise.
2636 * The path must have already been setup for deleting the leaf, including
2637 * all the proper balancing. path->nodes[1] must be locked.
2639 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2640 struct btrfs_root *root,
2641 struct btrfs_path *path,
2642 struct extent_buffer *leaf)
2646 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2647 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2651 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2652 0, root->root_key.objectid, 0, 0);
2657 * delete the item at the leaf level in path. If that empties
2658 * the leaf, remove it from the tree
2660 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2661 struct btrfs_path *path, int slot, int nr)
2663 struct extent_buffer *leaf;
2664 struct btrfs_item *item;
2672 leaf = path->nodes[0];
2673 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2675 for (i = 0; i < nr; i++)
2676 dsize += btrfs_item_size_nr(leaf, slot + i);
2678 nritems = btrfs_header_nritems(leaf);
2680 if (slot + nr != nritems) {
2681 int data_end = leaf_data_end(root, leaf);
2683 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2685 btrfs_leaf_data(leaf) + data_end,
2686 last_off - data_end);
2688 for (i = slot + nr; i < nritems; i++) {
2691 item = btrfs_item_nr(i);
2692 ioff = btrfs_item_offset(leaf, item);
2693 btrfs_set_item_offset(leaf, item, ioff + dsize);
2696 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2697 btrfs_item_nr_offset(slot + nr),
2698 sizeof(struct btrfs_item) *
2699 (nritems - slot - nr));
2701 btrfs_set_header_nritems(leaf, nritems - nr);
2704 /* delete the leaf if we've emptied it */
2706 if (leaf == root->node) {
2707 btrfs_set_header_level(leaf, 0);
2709 clean_tree_block(trans, root, leaf);
2710 wret = btrfs_del_leaf(trans, root, path, leaf);
2716 int used = leaf_space_used(leaf, 0, nritems);
2718 struct btrfs_disk_key disk_key;
2720 btrfs_item_key(leaf, &disk_key, 0);
2721 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2724 /* delete the leaf if it is mostly empty */
2725 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2726 /* push_leaf_left fixes the path.
2727 * make sure the path still points to our leaf
2728 * for possible call to del_ptr below
2730 slot = path->slots[1];
2731 extent_buffer_get(leaf);
2733 wret = push_leaf_left(trans, root, path, 1, 1);
2734 if (wret < 0 && wret != -ENOSPC)
2737 if (path->nodes[0] == leaf &&
2738 btrfs_header_nritems(leaf)) {
2739 wret = push_leaf_right(trans, root, path, 1, 1);
2740 if (wret < 0 && wret != -ENOSPC)
2744 if (btrfs_header_nritems(leaf) == 0) {
2745 clean_tree_block(trans, root, leaf);
2746 path->slots[1] = slot;
2747 ret = btrfs_del_leaf(trans, root, path, leaf);
2749 free_extent_buffer(leaf);
2752 btrfs_mark_buffer_dirty(leaf);
2753 free_extent_buffer(leaf);
2756 btrfs_mark_buffer_dirty(leaf);
2763 * walk up the tree as far as required to find the previous leaf.
2764 * returns 0 if it found something or 1 if there are no lesser leaves.
2765 * returns < 0 on io errors.
2767 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2771 struct extent_buffer *c;
2772 struct extent_buffer *next = NULL;
2774 while(level < BTRFS_MAX_LEVEL) {
2775 if (!path->nodes[level])
2778 slot = path->slots[level];
2779 c = path->nodes[level];
2782 if (level == BTRFS_MAX_LEVEL)
2788 next = read_node_slot(root, c, slot);
2789 if (!extent_buffer_uptodate(next)) {
2791 return PTR_ERR(next);
2796 path->slots[level] = slot;
2799 c = path->nodes[level];
2800 free_extent_buffer(c);
2801 slot = btrfs_header_nritems(next);
2804 path->nodes[level] = next;
2805 path->slots[level] = slot;
2808 next = read_node_slot(root, next, slot);
2809 if (!extent_buffer_uptodate(next)) {
2811 return PTR_ERR(next);
2819 * walk up the tree as far as required to find the next leaf.
2820 * returns 0 if it found something or 1 if there are no greater leaves.
2821 * returns < 0 on io errors.
2823 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2827 struct extent_buffer *c;
2828 struct extent_buffer *next = NULL;
2830 while(level < BTRFS_MAX_LEVEL) {
2831 if (!path->nodes[level])
2834 slot = path->slots[level] + 1;
2835 c = path->nodes[level];
2836 if (slot >= btrfs_header_nritems(c)) {
2838 if (level == BTRFS_MAX_LEVEL)
2844 reada_for_search(root, path, level, slot, 0);
2846 next = read_node_slot(root, c, slot);
2847 if (!extent_buffer_uptodate(next))
2851 path->slots[level] = slot;
2854 c = path->nodes[level];
2855 free_extent_buffer(c);
2856 path->nodes[level] = next;
2857 path->slots[level] = 0;
2861 reada_for_search(root, path, level, 0, 0);
2862 next = read_node_slot(root, next, 0);
2863 if (!extent_buffer_uptodate(next))
2869 int btrfs_previous_item(struct btrfs_root *root,
2870 struct btrfs_path *path, u64 min_objectid,
2873 struct btrfs_key found_key;
2874 struct extent_buffer *leaf;
2879 if (path->slots[0] == 0) {
2880 ret = btrfs_prev_leaf(root, path);
2886 leaf = path->nodes[0];
2887 nritems = btrfs_header_nritems(leaf);
2890 if (path->slots[0] == nritems)
2893 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2894 if (found_key.objectid < min_objectid)
2896 if (found_key.type == type)
2898 if (found_key.objectid == min_objectid &&
2899 found_key.type < type)
2906 * search in extent tree to find a previous Metadata/Data extent item with
2909 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2911 int btrfs_previous_extent_item(struct btrfs_root *root,
2912 struct btrfs_path *path, u64 min_objectid)
2914 struct btrfs_key found_key;
2915 struct extent_buffer *leaf;
2920 if (path->slots[0] == 0) {
2921 ret = btrfs_prev_leaf(root, path);
2927 leaf = path->nodes[0];
2928 nritems = btrfs_header_nritems(leaf);
2931 if (path->slots[0] == nritems)
2934 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2935 if (found_key.objectid < min_objectid)
2937 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2938 found_key.type == BTRFS_METADATA_ITEM_KEY)
2940 if (found_key.objectid == min_objectid &&
2941 found_key.type < BTRFS_EXTENT_ITEM_KEY)
2948 * Search in extent tree to found next meta/data extent
2949 * Caller needs to check for no-hole or skinny metadata features.
2951 int btrfs_next_extent_item(struct btrfs_root *root,
2952 struct btrfs_path *path, u64 max_objectid)
2954 struct btrfs_key found_key;
2958 ret = btrfs_next_item(root, path);
2961 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2963 if (found_key.objectid > max_objectid)
2965 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2966 found_key.type == BTRFS_METADATA_ITEM_KEY)