2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
22 #include "transaction.h"
23 #include "print-tree.h"
25 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
26 *root
, struct btrfs_path
*path
, int level
);
27 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
28 *root
, struct btrfs_key
*ins_key
,
29 struct btrfs_path
*path
, int data_size
, int extend
);
30 static int push_node_left(struct btrfs_trans_handle
*trans
,
31 struct btrfs_root
*root
, struct extent_buffer
*dst
,
32 struct extent_buffer
*src
);
33 static int balance_node_right(struct btrfs_trans_handle
*trans
,
34 struct btrfs_root
*root
,
35 struct extent_buffer
*dst_buf
,
36 struct extent_buffer
*src_buf
);
37 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
38 struct btrfs_path
*path
, int level
, int slot
);
40 inline void btrfs_init_path(struct btrfs_path
*p
)
42 memset(p
, 0, sizeof(*p
));
45 struct btrfs_path
*btrfs_alloc_path(void)
47 struct btrfs_path
*path
;
48 path
= kmalloc(sizeof(struct btrfs_path
), GFP_NOFS
);
50 btrfs_init_path(path
);
56 void btrfs_free_path(struct btrfs_path
*p
)
58 btrfs_release_path(NULL
, p
);
62 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
65 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
68 free_extent_buffer(p
->nodes
[i
]);
70 memset(p
, 0, sizeof(*p
));
73 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
74 struct btrfs_root
*root
,
75 struct extent_buffer
*buf
,
76 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
78 struct extent_buffer
*cow
;
82 struct btrfs_key first_key
;
83 struct btrfs_root
*new_root
;
85 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
89 memcpy(new_root
, root
, sizeof(*new_root
));
90 new_root
->root_key
.objectid
= new_root_objectid
;
92 WARN_ON(root
->ref_cows
&& trans
->transid
!=
93 root
->fs_info
->running_transaction
->transid
);
94 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
96 level
= btrfs_header_level(buf
);
97 nritems
= btrfs_header_nritems(buf
);
100 btrfs_item_key_to_cpu(buf
, &first_key
, 0);
102 btrfs_node_key_to_cpu(buf
, &first_key
, 0);
104 first_key
.objectid
= 0;
106 cow
= __btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
108 trans
->transid
, first_key
.objectid
,
109 level
, buf
->start
, 0);
115 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
116 btrfs_set_header_bytenr(cow
, cow
->start
);
117 btrfs_set_header_generation(cow
, trans
->transid
);
118 btrfs_set_header_owner(cow
, new_root_objectid
);
120 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
121 ret
= btrfs_inc_ref(trans
, new_root
, buf
);
127 btrfs_mark_buffer_dirty(cow
);
132 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
133 struct btrfs_root
*root
,
134 struct extent_buffer
*buf
,
135 struct extent_buffer
*parent
, int parent_slot
,
136 struct extent_buffer
**cow_ret
,
137 u64 search_start
, u64 empty_size
)
140 struct extent_buffer
*cow
;
143 int different_trans
= 0;
145 struct btrfs_key first_key
;
147 if (root
->ref_cows
) {
148 root_gen
= trans
->transid
;
153 WARN_ON(root
->ref_cows
&& trans
->transid
!=
154 root
->fs_info
->running_transaction
->transid
);
155 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
157 level
= btrfs_header_level(buf
);
158 nritems
= btrfs_header_nritems(buf
);
161 btrfs_item_key_to_cpu(buf
, &first_key
, 0);
163 btrfs_node_key_to_cpu(buf
, &first_key
, 0);
165 first_key
.objectid
= 0;
167 cow
= __btrfs_alloc_free_block(trans
, root
, buf
->len
,
168 root
->root_key
.objectid
,
169 root_gen
, first_key
.objectid
, level
,
170 search_start
, empty_size
);
174 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
175 btrfs_set_header_bytenr(cow
, cow
->start
);
176 btrfs_set_header_generation(cow
, trans
->transid
);
177 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
179 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
180 if (btrfs_header_generation(buf
) != trans
->transid
) {
182 ret
= btrfs_inc_ref(trans
, root
, buf
);
186 clean_tree_block(trans
, root
, buf
);
189 if (buf
== root
->node
) {
190 root_gen
= btrfs_header_generation(buf
);
192 extent_buffer_get(cow
);
193 if (buf
!= root
->commit_root
) {
194 btrfs_free_extent(trans
, root
, buf
->start
,
195 buf
->len
, root
->root_key
.objectid
,
198 free_extent_buffer(buf
);
200 root_gen
= btrfs_header_generation(parent
);
201 btrfs_set_node_blockptr(parent
, parent_slot
,
203 WARN_ON(trans
->transid
== 0);
204 btrfs_set_node_ptr_generation(parent
, parent_slot
,
206 btrfs_mark_buffer_dirty(parent
);
207 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
208 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
209 btrfs_header_owner(parent
), root_gen
,
212 free_extent_buffer(buf
);
213 btrfs_mark_buffer_dirty(cow
);
218 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
219 struct btrfs_root
*root
, struct extent_buffer
*buf
,
220 struct extent_buffer
*parent
, int parent_slot
,
221 struct extent_buffer
**cow_ret
)
226 if (trans->transaction != root->fs_info->running_transaction) {
227 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
228 root->fs_info->running_transaction->transid);
232 if (trans
->transid
!= root
->fs_info
->generation
) {
233 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
234 root
->fs_info
->generation
);
237 if (btrfs_header_generation(buf
) == trans
->transid
) {
242 search_start
= buf
->start
& ~((u64
)BTRFS_BLOCK_GROUP_SIZE
- 1);
243 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
244 parent_slot
, cow_ret
, search_start
, 0);
249 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
251 if (blocknr < other && other - (blocknr + blocksize) < 32768)
253 if (blocknr > other && blocknr - (other + blocksize) < 32768)
260 * compare two keys in a memcmp fashion
262 int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
266 btrfs_disk_key_to_cpu(&k1
, disk
);
268 if (k1
.objectid
> k2
->objectid
)
270 if (k1
.objectid
< k2
->objectid
)
272 if (k1
.type
> k2
->type
)
274 if (k1
.type
< k2
->type
)
276 if (k1
.offset
> k2
->offset
)
278 if (k1
.offset
< k2
->offset
)
285 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
286 struct btrfs_root
*root
, struct extent_buffer
*parent
,
287 int start_slot
, int cache_only
, u64
*last_ret
,
288 struct btrfs_key
*progress
)
290 struct extent_buffer
*cur
;
291 struct extent_buffer
*tmp
;
293 u64 search_start
= *last_ret
;
303 int progress_passed
= 0;
304 struct btrfs_disk_key disk_key
;
306 parent_level
= btrfs_header_level(parent
);
307 if (cache_only
&& parent_level
!= 1)
310 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
311 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
312 root
->fs_info
->running_transaction
->transid
);
315 if (trans
->transid
!= root
->fs_info
->generation
) {
316 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
317 root
->fs_info
->generation
);
321 parent_nritems
= btrfs_header_nritems(parent
);
322 blocksize
= btrfs_level_size(root
, parent_level
- 1);
323 end_slot
= parent_nritems
;
325 if (parent_nritems
== 1)
328 for (i
= start_slot
; i
< end_slot
; i
++) {
331 if (!parent
->map_token
) {
332 map_extent_buffer(parent
,
333 btrfs_node_key_ptr_offset(i
),
334 sizeof(struct btrfs_key_ptr
),
335 &parent
->map_token
, &parent
->kaddr
,
336 &parent
->map_start
, &parent
->map_len
,
339 btrfs_node_key(parent
, &disk_key
, i
);
340 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
344 blocknr
= btrfs_node_blockptr(parent
, i
);
346 last_block
= blocknr
;
349 other
= btrfs_node_blockptr(parent
, i
- 1);
350 close
= close_blocks(blocknr
, other
, blocksize
);
352 if (close
&& i
< end_slot
- 2) {
353 other
= btrfs_node_blockptr(parent
, i
+ 1);
354 close
= close_blocks(blocknr
, other
, blocksize
);
357 last_block
= blocknr
;
360 if (parent
->map_token
) {
361 unmap_extent_buffer(parent
, parent
->map_token
,
363 parent
->map_token
= NULL
;
366 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
368 uptodate
= btrfs_buffer_uptodate(cur
);
371 if (!cur
|| !uptodate
) {
373 free_extent_buffer(cur
);
377 cur
= read_tree_block(root
, blocknr
,
379 } else if (!uptodate
) {
380 btrfs_read_buffer(cur
);
383 if (search_start
== 0)
384 search_start
= last_block
;
386 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
389 (end_slot
- i
) * blocksize
));
391 free_extent_buffer(cur
);
394 search_start
= tmp
->start
;
395 last_block
= tmp
->start
;
396 *last_ret
= search_start
;
397 if (parent_level
== 1)
398 btrfs_clear_buffer_defrag(tmp
);
399 free_extent_buffer(tmp
);
401 if (parent
->map_token
) {
402 unmap_extent_buffer(parent
, parent
->map_token
,
404 parent
->map_token
= NULL
;
411 * The leaf data grows from end-to-front in the node.
412 * this returns the address of the start of the last item,
413 * which is the stop of the leaf data stack
415 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
416 struct extent_buffer
*leaf
)
418 u32 nr
= btrfs_header_nritems(leaf
);
420 return BTRFS_LEAF_DATA_SIZE(root
);
421 return btrfs_item_offset_nr(leaf
, nr
- 1);
424 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
427 struct extent_buffer
*parent
= NULL
;
428 struct extent_buffer
*node
= path
->nodes
[level
];
429 struct btrfs_disk_key parent_key
;
430 struct btrfs_disk_key node_key
;
433 struct btrfs_key cpukey
;
434 u32 nritems
= btrfs_header_nritems(node
);
436 if (path
->nodes
[level
+ 1])
437 parent
= path
->nodes
[level
+ 1];
439 slot
= path
->slots
[level
];
440 BUG_ON(nritems
== 0);
442 parent_slot
= path
->slots
[level
+ 1];
443 btrfs_node_key(parent
, &parent_key
, parent_slot
);
444 btrfs_node_key(node
, &node_key
, 0);
445 BUG_ON(memcmp(&parent_key
, &node_key
,
446 sizeof(struct btrfs_disk_key
)));
447 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
448 btrfs_header_bytenr(node
));
450 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
452 btrfs_node_key_to_cpu(node
, &cpukey
, slot
- 1);
453 btrfs_node_key(node
, &node_key
, slot
);
454 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) <= 0);
456 if (slot
< nritems
- 1) {
457 btrfs_node_key_to_cpu(node
, &cpukey
, slot
+ 1);
458 btrfs_node_key(node
, &node_key
, slot
);
459 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) >= 0);
464 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
467 struct extent_buffer
*leaf
= path
->nodes
[level
];
468 struct extent_buffer
*parent
= NULL
;
470 struct btrfs_key cpukey
;
471 struct btrfs_disk_key parent_key
;
472 struct btrfs_disk_key leaf_key
;
473 int slot
= path
->slots
[0];
475 u32 nritems
= btrfs_header_nritems(leaf
);
477 if (path
->nodes
[level
+ 1])
478 parent
= path
->nodes
[level
+ 1];
484 parent_slot
= path
->slots
[level
+ 1];
485 btrfs_node_key(parent
, &parent_key
, parent_slot
);
486 btrfs_item_key(leaf
, &leaf_key
, 0);
488 BUG_ON(memcmp(&parent_key
, &leaf_key
,
489 sizeof(struct btrfs_disk_key
)));
490 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
491 btrfs_header_bytenr(leaf
));
494 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
495 btrfs_item_key_to_cpu(leaf
, &cpukey
, i
+ 1);
496 btrfs_item_key(leaf
, &leaf_key
, i
);
497 if (comp_keys(&leaf_key
, &cpukey
) >= 0) {
498 btrfs_print_leaf(root
, leaf
);
499 printk("slot %d offset bad key\n", i
);
502 if (btrfs_item_offset_nr(leaf
, i
) !=
503 btrfs_item_end_nr(leaf
, i
+ 1)) {
504 btrfs_print_leaf(root
, leaf
);
505 printk("slot %d offset bad\n", i
);
509 if (btrfs_item_offset_nr(leaf
, i
) +
510 btrfs_item_size_nr(leaf
, i
) !=
511 BTRFS_LEAF_DATA_SIZE(root
)) {
512 btrfs_print_leaf(root
, leaf
);
513 printk("slot %d first offset bad\n", i
);
519 if (btrfs_item_size_nr(leaf
, nritems
- 1) > 4096) {
520 btrfs_print_leaf(root
, leaf
);
521 printk("slot %d bad size \n", nritems
- 1);
526 if (slot
!= 0 && slot
< nritems
- 1) {
527 btrfs_item_key(leaf
, &leaf_key
, slot
);
528 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
- 1);
529 if (btrfs_comp_keys(&leaf_key
, &cpukey
) <= 0) {
530 btrfs_print_leaf(root
, leaf
);
531 printk("slot %d offset bad key\n", slot
);
534 if (btrfs_item_offset_nr(leaf
, slot
- 1) !=
535 btrfs_item_end_nr(leaf
, slot
)) {
536 btrfs_print_leaf(root
, leaf
);
537 printk("slot %d offset bad\n", slot
);
541 if (slot
< nritems
- 1) {
542 btrfs_item_key(leaf
, &leaf_key
, slot
);
543 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
+ 1);
544 BUG_ON(btrfs_comp_keys(&leaf_key
, &cpukey
) >= 0);
545 if (btrfs_item_offset_nr(leaf
, slot
) !=
546 btrfs_item_end_nr(leaf
, slot
+ 1)) {
547 btrfs_print_leaf(root
, leaf
);
548 printk("slot %d offset bad\n", slot
);
552 BUG_ON(btrfs_item_offset_nr(leaf
, 0) +
553 btrfs_item_size_nr(leaf
, 0) != BTRFS_LEAF_DATA_SIZE(root
));
557 static int noinline
check_block(struct btrfs_root
*root
,
558 struct btrfs_path
*path
, int level
)
562 struct extent_buffer
*buf
= path
->nodes
[level
];
564 if (memcmp_extent_buffer(buf
, root
->fs_info
->fsid
,
565 (unsigned long)btrfs_header_fsid(buf
),
567 printk("warning bad block %Lu\n", buf
->start
);
572 return check_leaf(root
, path
, level
);
573 return check_node(root
, path
, level
);
577 * search for key in the extent_buffer. The items start at offset p,
578 * and they are item_size apart. There are 'max' items in p.
580 * the slot in the array is returned via slot, and it points to
581 * the place where you would insert key if it is not found in
584 * slot may point to max if the key is bigger than all of the keys
586 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
587 int item_size
, struct btrfs_key
*key
,
594 unsigned long offset
;
595 struct btrfs_disk_key
*tmp
;
598 mid
= (low
+ high
) / 2;
599 offset
= p
+ mid
* item_size
;
601 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
602 ret
= btrfs_comp_keys(tmp
, key
);
618 * simple bin_search frontend that does the right thing for
621 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
622 int level
, int *slot
)
625 return generic_bin_search(eb
,
626 offsetof(struct btrfs_leaf
, items
),
627 sizeof(struct btrfs_item
),
628 key
, btrfs_header_nritems(eb
),
631 return generic_bin_search(eb
,
632 offsetof(struct btrfs_node
, ptrs
),
633 sizeof(struct btrfs_key_ptr
),
634 key
, btrfs_header_nritems(eb
),
640 static struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
641 struct extent_buffer
*parent
, int slot
)
645 if (slot
>= btrfs_header_nritems(parent
))
647 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
648 btrfs_level_size(root
, btrfs_header_level(parent
) - 1));
651 static int balance_level(struct btrfs_trans_handle
*trans
,
652 struct btrfs_root
*root
,
653 struct btrfs_path
*path
, int level
)
655 struct extent_buffer
*right
= NULL
;
656 struct extent_buffer
*mid
;
657 struct extent_buffer
*left
= NULL
;
658 struct extent_buffer
*parent
= NULL
;
662 int orig_slot
= path
->slots
[level
];
663 int err_on_enospc
= 0;
669 mid
= path
->nodes
[level
];
670 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
672 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
674 if (level
< BTRFS_MAX_LEVEL
- 1)
675 parent
= path
->nodes
[level
+ 1];
676 pslot
= path
->slots
[level
+ 1];
679 * deal with the case where there is only one pointer in the root
680 * by promoting the node below to a root
683 struct extent_buffer
*child
;
685 if (btrfs_header_nritems(mid
) != 1)
688 /* promote the child to a root */
689 child
= read_node_slot(root
, mid
, 0);
692 path
->nodes
[level
] = NULL
;
693 clean_tree_block(trans
, root
, mid
);
694 wait_on_tree_block_writeback(root
, mid
);
695 /* once for the path */
696 free_extent_buffer(mid
);
697 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
698 root
->root_key
.objectid
,
699 btrfs_header_generation(mid
), 0, 0, 1);
700 /* once for the root ptr */
701 free_extent_buffer(mid
);
704 if (btrfs_header_nritems(mid
) >
705 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
708 if (btrfs_header_nritems(mid
) < 2)
711 left
= read_node_slot(root
, parent
, pslot
- 1);
713 wret
= btrfs_cow_block(trans
, root
, left
,
714 parent
, pslot
- 1, &left
);
720 right
= read_node_slot(root
, parent
, pslot
+ 1);
722 wret
= btrfs_cow_block(trans
, root
, right
,
723 parent
, pslot
+ 1, &right
);
730 /* first, try to make some room in the middle buffer */
732 orig_slot
+= btrfs_header_nritems(left
);
733 wret
= push_node_left(trans
, root
, left
, mid
);
736 if (btrfs_header_nritems(mid
) < 2)
741 * then try to empty the right most buffer into the middle
744 wret
= push_node_left(trans
, root
, mid
, right
);
745 if (wret
< 0 && wret
!= -ENOSPC
)
747 if (btrfs_header_nritems(right
) == 0) {
748 u64 bytenr
= right
->start
;
749 u64 generation
= btrfs_header_generation(parent
);
750 u32 blocksize
= right
->len
;
752 clean_tree_block(trans
, root
, right
);
753 wait_on_tree_block_writeback(root
, right
);
754 free_extent_buffer(right
);
756 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
760 wret
= btrfs_free_extent(trans
, root
, bytenr
,
762 btrfs_header_owner(parent
),
763 generation
, 0, 0, 1);
767 struct btrfs_disk_key right_key
;
768 btrfs_node_key(right
, &right_key
, 0);
769 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
770 btrfs_mark_buffer_dirty(parent
);
773 if (btrfs_header_nritems(mid
) == 1) {
775 * we're not allowed to leave a node with one item in the
776 * tree during a delete. A deletion from lower in the tree
777 * could try to delete the only pointer in this node.
778 * So, pull some keys from the left.
779 * There has to be a left pointer at this point because
780 * otherwise we would have pulled some pointers from the
784 wret
= balance_node_right(trans
, root
, mid
, left
);
791 if (btrfs_header_nritems(mid
) == 0) {
792 /* we've managed to empty the middle node, drop it */
793 u64 root_gen
= btrfs_header_generation(parent
);
794 u64 bytenr
= mid
->start
;
795 u32 blocksize
= mid
->len
;
796 clean_tree_block(trans
, root
, mid
);
797 wait_on_tree_block_writeback(root
, mid
);
798 free_extent_buffer(mid
);
800 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
803 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
804 btrfs_header_owner(parent
),
809 /* update the parent key to reflect our changes */
810 struct btrfs_disk_key mid_key
;
811 btrfs_node_key(mid
, &mid_key
, 0);
812 btrfs_set_node_key(parent
, &mid_key
, pslot
);
813 btrfs_mark_buffer_dirty(parent
);
816 /* update the path */
818 if (btrfs_header_nritems(left
) > orig_slot
) {
819 extent_buffer_get(left
);
820 path
->nodes
[level
] = left
;
821 path
->slots
[level
+ 1] -= 1;
822 path
->slots
[level
] = orig_slot
;
824 free_extent_buffer(mid
);
826 orig_slot
-= btrfs_header_nritems(left
);
827 path
->slots
[level
] = orig_slot
;
830 /* double check we haven't messed things up */
831 check_block(root
, path
, level
);
833 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
837 free_extent_buffer(right
);
839 free_extent_buffer(left
);
843 /* returns zero if the push worked, non-zero otherwise */
844 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
845 struct btrfs_root
*root
,
846 struct btrfs_path
*path
, int level
)
848 struct extent_buffer
*right
= NULL
;
849 struct extent_buffer
*mid
;
850 struct extent_buffer
*left
= NULL
;
851 struct extent_buffer
*parent
= NULL
;
855 int orig_slot
= path
->slots
[level
];
861 mid
= path
->nodes
[level
];
862 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
863 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
865 if (level
< BTRFS_MAX_LEVEL
- 1)
866 parent
= path
->nodes
[level
+ 1];
867 pslot
= path
->slots
[level
+ 1];
872 left
= read_node_slot(root
, parent
, pslot
- 1);
874 /* first, try to make some room in the middle buffer */
877 left_nr
= btrfs_header_nritems(left
);
878 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
881 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
886 wret
= push_node_left(trans
, root
,
893 struct btrfs_disk_key disk_key
;
894 orig_slot
+= left_nr
;
895 btrfs_node_key(mid
, &disk_key
, 0);
896 btrfs_set_node_key(parent
, &disk_key
, pslot
);
897 btrfs_mark_buffer_dirty(parent
);
898 if (btrfs_header_nritems(left
) > orig_slot
) {
899 path
->nodes
[level
] = left
;
900 path
->slots
[level
+ 1] -= 1;
901 path
->slots
[level
] = orig_slot
;
902 free_extent_buffer(mid
);
905 btrfs_header_nritems(left
);
906 path
->slots
[level
] = orig_slot
;
907 free_extent_buffer(left
);
911 free_extent_buffer(left
);
913 right
= read_node_slot(root
, parent
, pslot
+ 1);
916 * then try to empty the right most buffer into the middle
920 right_nr
= btrfs_header_nritems(right
);
921 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
924 ret
= btrfs_cow_block(trans
, root
, right
,
930 wret
= balance_node_right(trans
, root
,
937 struct btrfs_disk_key disk_key
;
939 btrfs_node_key(right
, &disk_key
, 0);
940 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
941 btrfs_mark_buffer_dirty(parent
);
943 if (btrfs_header_nritems(mid
) <= orig_slot
) {
944 path
->nodes
[level
] = right
;
945 path
->slots
[level
+ 1] += 1;
946 path
->slots
[level
] = orig_slot
-
947 btrfs_header_nritems(mid
);
948 free_extent_buffer(mid
);
950 free_extent_buffer(right
);
954 free_extent_buffer(right
);
960 * readahead one full node of leaves
962 static void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
963 int level
, int slot
, u64 objectid
)
965 struct extent_buffer
*node
;
966 struct btrfs_disk_key disk_key
;
972 int direction
= path
->reada
;
973 struct extent_buffer
*eb
;
981 if (!path
->nodes
[level
])
984 node
= path
->nodes
[level
];
985 search
= btrfs_node_blockptr(node
, slot
);
986 blocksize
= btrfs_level_size(root
, level
- 1);
987 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
989 free_extent_buffer(eb
);
993 highest_read
= search
;
994 lowest_read
= search
;
996 nritems
= btrfs_header_nritems(node
);
1003 } else if (direction
> 0) {
1008 if (path
->reada
< 0 && objectid
) {
1009 btrfs_node_key(node
, &disk_key
, nr
);
1010 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1013 search
= btrfs_node_blockptr(node
, nr
);
1014 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1015 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1016 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1017 readahead_tree_block(root
, search
, blocksize
);
1021 if (path
->reada
< 2 && (nread
> (256 * 1024) || nscan
> 32))
1023 if(nread
> (1024 * 1024) || nscan
> 128)
1026 if (search
< lowest_read
)
1027 lowest_read
= search
;
1028 if (search
> highest_read
)
1029 highest_read
= search
;
1034 * look for key in the tree. path is filled in with nodes along the way
1035 * if key is found, we return zero and you can find the item in the leaf
1036 * level of the path (level 0)
1038 * If the key isn't found, the path points to the slot where it should
1039 * be inserted, and 1 is returned. If there are other errors during the
1040 * search a negative error number is returned.
1042 * if ins_len > 0, nodes and leaves will be split as we walk down the
1043 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1046 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1047 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1050 struct extent_buffer
*b
;
1056 int should_reada
= p
->reada
;
1057 u8 lowest_level
= 0;
1059 lowest_level
= p
->lowest_level
;
1060 WARN_ON(lowest_level
&& ins_len
);
1061 WARN_ON(p
->nodes
[0] != NULL
);
1063 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1067 extent_buffer_get(b
);
1069 level
= btrfs_header_level(b
);
1072 wret
= btrfs_cow_block(trans
, root
, b
,
1073 p
->nodes
[level
+ 1],
1074 p
->slots
[level
+ 1],
1077 free_extent_buffer(b
);
1081 BUG_ON(!cow
&& ins_len
);
1082 if (level
!= btrfs_header_level(b
))
1084 level
= btrfs_header_level(b
);
1085 p
->nodes
[level
] = b
;
1086 ret
= check_block(root
, p
, level
);
1089 ret
= bin_search(b
, key
, level
, &slot
);
1091 if (ret
&& slot
> 0)
1093 p
->slots
[level
] = slot
;
1094 if (ins_len
> 0 && btrfs_header_nritems(b
) >=
1095 BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1096 int sret
= split_node(trans
, root
, p
, level
);
1100 b
= p
->nodes
[level
];
1101 slot
= p
->slots
[level
];
1102 } else if (ins_len
< 0) {
1103 int sret
= balance_level(trans
, root
, p
,
1107 b
= p
->nodes
[level
];
1109 btrfs_release_path(NULL
, p
);
1112 slot
= p
->slots
[level
];
1113 BUG_ON(btrfs_header_nritems(b
) == 1);
1115 /* this is only true while dropping a snapshot */
1116 if (level
== lowest_level
)
1118 bytenr
= btrfs_node_blockptr(b
, slot
);
1119 ptr_gen
= btrfs_node_ptr_generation(b
, slot
);
1121 reada_for_search(root
, p
, level
, slot
,
1123 b
= read_tree_block(root
, bytenr
,
1124 btrfs_level_size(root
, level
- 1));
1125 if (ptr_gen
!= btrfs_header_generation(b
)) {
1126 printk("block %llu bad gen wanted %llu "
1128 (unsigned long long)b
->start
,
1129 (unsigned long long)ptr_gen
,
1130 (unsigned long long)btrfs_header_generation(b
));
1133 p
->slots
[level
] = slot
;
1134 if (ins_len
> 0 && btrfs_leaf_free_space(root
, b
) <
1135 sizeof(struct btrfs_item
) + ins_len
) {
1136 int sret
= split_leaf(trans
, root
, key
,
1137 p
, ins_len
, ret
== 0);
1149 * adjust the pointers going up the tree, starting at level
1150 * making sure the right key of each node is points to 'key'.
1151 * This is used after shifting pointers to the left, so it stops
1152 * fixing up pointers when a given leaf/node is not in slot 0 of the
1155 * If this fails to write a tree block, it returns -1, but continues
1156 * fixing up the blocks in ram so the tree is consistent.
1158 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1159 struct btrfs_root
*root
, struct btrfs_path
*path
,
1160 struct btrfs_disk_key
*key
, int level
)
1164 struct extent_buffer
*t
;
1166 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1167 int tslot
= path
->slots
[i
];
1168 if (!path
->nodes
[i
])
1171 btrfs_set_node_key(t
, key
, tslot
);
1172 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1180 * try to push data from one node into the next node left in the
1183 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1184 * error, and > 0 if there was no room in the left hand block.
1186 static int push_node_left(struct btrfs_trans_handle
*trans
,
1187 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1188 struct extent_buffer
*src
)
1195 src_nritems
= btrfs_header_nritems(src
);
1196 dst_nritems
= btrfs_header_nritems(dst
);
1197 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1198 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1199 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1201 if (push_items
<= 0) {
1205 if (src_nritems
< push_items
)
1206 push_items
= src_nritems
;
1208 copy_extent_buffer(dst
, src
,
1209 btrfs_node_key_ptr_offset(dst_nritems
),
1210 btrfs_node_key_ptr_offset(0),
1211 push_items
* sizeof(struct btrfs_key_ptr
));
1213 if (push_items
< src_nritems
) {
1214 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1215 btrfs_node_key_ptr_offset(push_items
),
1216 (src_nritems
- push_items
) *
1217 sizeof(struct btrfs_key_ptr
));
1219 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1220 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1221 btrfs_mark_buffer_dirty(src
);
1222 btrfs_mark_buffer_dirty(dst
);
1227 * try to push data from one node into the next node right in the
1230 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1231 * error, and > 0 if there was no room in the right hand block.
1233 * this will only push up to 1/2 the contents of the left node over
1235 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1236 struct btrfs_root
*root
,
1237 struct extent_buffer
*dst
,
1238 struct extent_buffer
*src
)
1246 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1247 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1249 src_nritems
= btrfs_header_nritems(src
);
1250 dst_nritems
= btrfs_header_nritems(dst
);
1251 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1252 if (push_items
<= 0)
1255 max_push
= src_nritems
/ 2 + 1;
1256 /* don't try to empty the node */
1257 if (max_push
>= src_nritems
)
1260 if (max_push
< push_items
)
1261 push_items
= max_push
;
1263 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1264 btrfs_node_key_ptr_offset(0),
1266 sizeof(struct btrfs_key_ptr
));
1268 copy_extent_buffer(dst
, src
,
1269 btrfs_node_key_ptr_offset(0),
1270 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1271 push_items
* sizeof(struct btrfs_key_ptr
));
1273 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1274 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1276 btrfs_mark_buffer_dirty(src
);
1277 btrfs_mark_buffer_dirty(dst
);
1282 * helper function to insert a new root level in the tree.
1283 * A new node is allocated, and a single item is inserted to
1284 * point to the existing root
1286 * returns zero on success or < 0 on failure.
1288 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1289 struct btrfs_root
*root
,
1290 struct btrfs_path
*path
, int level
)
1294 struct extent_buffer
*lower
;
1295 struct extent_buffer
*c
;
1296 struct btrfs_disk_key lower_key
;
1298 BUG_ON(path
->nodes
[level
]);
1299 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1302 root_gen
= trans
->transid
;
1306 lower
= path
->nodes
[level
-1];
1308 btrfs_item_key(lower
, &lower_key
, 0);
1310 btrfs_node_key(lower
, &lower_key
, 0);
1312 c
= __btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1313 root
->root_key
.objectid
,
1314 root_gen
, lower_key
.objectid
, level
,
1315 root
->node
->start
, 0);
1318 memset_extent_buffer(c
, 0, 0, root
->nodesize
);
1319 btrfs_set_header_nritems(c
, 1);
1320 btrfs_set_header_level(c
, level
);
1321 btrfs_set_header_bytenr(c
, c
->start
);
1322 btrfs_set_header_generation(c
, trans
->transid
);
1323 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1325 write_extent_buffer(c
, root
->fs_info
->fsid
,
1326 (unsigned long)btrfs_header_fsid(c
),
1328 btrfs_set_node_key(c
, &lower_key
, 0);
1329 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1330 lower_gen
= btrfs_header_generation(lower
);
1331 WARN_ON(lower_gen
== 0);
1333 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1335 btrfs_mark_buffer_dirty(c
);
1337 /* the super has an extra ref to root->node */
1338 free_extent_buffer(root
->node
);
1340 extent_buffer_get(c
);
1341 path
->nodes
[level
] = c
;
1342 path
->slots
[level
] = 0;
1344 if (root
->ref_cows
&& lower_gen
!= trans
->transid
) {
1345 struct btrfs_path
*back_path
= btrfs_alloc_path();
1347 ret
= btrfs_insert_extent_backref(trans
,
1348 root
->fs_info
->extent_root
,
1350 root
->root_key
.objectid
,
1351 trans
->transid
, 0, 0);
1353 btrfs_free_path(back_path
);
1359 * worker function to insert a single pointer in a node.
1360 * the node should have enough room for the pointer already
1362 * slot and level indicate where you want the key to go, and
1363 * blocknr is the block the key points to.
1365 * returns zero on success and < 0 on any error
1367 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1368 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1369 *key
, u64 bytenr
, int slot
, int level
)
1371 struct extent_buffer
*lower
;
1374 BUG_ON(!path
->nodes
[level
]);
1375 lower
= path
->nodes
[level
];
1376 nritems
= btrfs_header_nritems(lower
);
1379 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
1381 if (slot
!= nritems
) {
1382 memmove_extent_buffer(lower
,
1383 btrfs_node_key_ptr_offset(slot
+ 1),
1384 btrfs_node_key_ptr_offset(slot
),
1385 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1387 btrfs_set_node_key(lower
, key
, slot
);
1388 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1389 WARN_ON(trans
->transid
== 0);
1390 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1391 btrfs_set_header_nritems(lower
, nritems
+ 1);
1392 btrfs_mark_buffer_dirty(lower
);
1397 * split the node at the specified level in path in two.
1398 * The path is corrected to point to the appropriate node after the split
1400 * Before splitting this tries to make some room in the node by pushing
1401 * left and right, if either one works, it returns right away.
1403 * returns 0 on success and < 0 on failure
1405 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1406 *root
, struct btrfs_path
*path
, int level
)
1409 struct extent_buffer
*c
;
1410 struct extent_buffer
*split
;
1411 struct btrfs_disk_key disk_key
;
1417 c
= path
->nodes
[level
];
1418 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1419 if (c
== root
->node
) {
1420 /* trying to split the root, lets make a new one */
1421 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1425 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1426 c
= path
->nodes
[level
];
1427 if (!ret
&& btrfs_header_nritems(c
) <
1428 BTRFS_NODEPTRS_PER_BLOCK(root
) - 1)
1434 c_nritems
= btrfs_header_nritems(c
);
1436 root_gen
= trans
->transid
;
1440 btrfs_node_key(c
, &disk_key
, 0);
1441 split
= __btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1442 root
->root_key
.objectid
,
1444 btrfs_disk_key_objectid(&disk_key
),
1445 level
, c
->start
, 0);
1447 return PTR_ERR(split
);
1449 btrfs_set_header_flags(split
, btrfs_header_flags(c
));
1450 btrfs_set_header_level(split
, btrfs_header_level(c
));
1451 btrfs_set_header_bytenr(split
, split
->start
);
1452 btrfs_set_header_generation(split
, trans
->transid
);
1453 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1454 write_extent_buffer(split
, root
->fs_info
->fsid
,
1455 (unsigned long)btrfs_header_fsid(split
),
1458 mid
= (c_nritems
+ 1) / 2;
1460 copy_extent_buffer(split
, c
,
1461 btrfs_node_key_ptr_offset(0),
1462 btrfs_node_key_ptr_offset(mid
),
1463 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1464 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1465 btrfs_set_header_nritems(c
, mid
);
1468 btrfs_mark_buffer_dirty(c
);
1469 btrfs_mark_buffer_dirty(split
);
1471 btrfs_node_key(split
, &disk_key
, 0);
1472 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1473 path
->slots
[level
+ 1] + 1,
1478 if (path
->slots
[level
] >= mid
) {
1479 path
->slots
[level
] -= mid
;
1480 free_extent_buffer(c
);
1481 path
->nodes
[level
] = split
;
1482 path
->slots
[level
+ 1] += 1;
1484 free_extent_buffer(split
);
1490 * how many bytes are required to store the items in a leaf. start
1491 * and nr indicate which items in the leaf to check. This totals up the
1492 * space used both by the item structs and the item data
1494 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1497 int nritems
= btrfs_header_nritems(l
);
1498 int end
= min(nritems
, start
+ nr
) - 1;
1502 data_len
= btrfs_item_end_nr(l
, start
);
1503 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1504 data_len
+= sizeof(struct btrfs_item
) * nr
;
1505 WARN_ON(data_len
< 0);
1510 * The space between the end of the leaf items and
1511 * the start of the leaf data. IOW, how much room
1512 * the leaf has left for both items and data
1514 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1516 int nritems
= btrfs_header_nritems(leaf
);
1518 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
1520 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1521 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
1522 leaf_space_used(leaf
, 0, nritems
), nritems
);
1528 * push some data in the path leaf to the right, trying to free up at
1529 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1531 * returns 1 if the push failed because the other node didn't have enough
1532 * room, 0 if everything worked out and < 0 if there were major errors.
1534 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1535 *root
, struct btrfs_path
*path
, int data_size
,
1538 struct extent_buffer
*left
= path
->nodes
[0];
1539 struct extent_buffer
*right
;
1540 struct extent_buffer
*upper
;
1541 struct btrfs_disk_key disk_key
;
1547 struct btrfs_item
*item
;
1555 slot
= path
->slots
[1];
1556 if (!path
->nodes
[1]) {
1559 upper
= path
->nodes
[1];
1560 if (slot
>= btrfs_header_nritems(upper
) - 1)
1563 right
= read_tree_block(root
, btrfs_node_blockptr(upper
, slot
+ 1),
1565 free_space
= btrfs_leaf_free_space(root
, right
);
1566 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1567 free_extent_buffer(right
);
1571 /* cow and double check */
1572 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1575 free_extent_buffer(right
);
1578 free_space
= btrfs_leaf_free_space(root
, right
);
1579 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1580 free_extent_buffer(right
);
1584 left_nritems
= btrfs_header_nritems(left
);
1585 if (left_nritems
== 0) {
1586 free_extent_buffer(right
);
1595 i
= left_nritems
- 1;
1597 item
= btrfs_item_nr(left
, i
);
1599 if (path
->slots
[0] == i
)
1600 push_space
+= data_size
+ sizeof(*item
);
1602 this_item_size
= btrfs_item_size(left
, item
);
1603 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1606 push_space
+= this_item_size
+ sizeof(*item
);
1612 if (push_items
== 0) {
1613 free_extent_buffer(right
);
1617 if (!empty
&& push_items
== left_nritems
)
1620 /* push left to right */
1621 right_nritems
= btrfs_header_nritems(right
);
1623 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1624 push_space
-= leaf_data_end(root
, left
);
1626 /* make room in the right data area */
1627 data_end
= leaf_data_end(root
, right
);
1628 memmove_extent_buffer(right
,
1629 btrfs_leaf_data(right
) + data_end
- push_space
,
1630 btrfs_leaf_data(right
) + data_end
,
1631 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
1633 /* copy from the left data area */
1634 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1635 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1636 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1639 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1640 btrfs_item_nr_offset(0),
1641 right_nritems
* sizeof(struct btrfs_item
));
1643 /* copy the items from left to right */
1644 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1645 btrfs_item_nr_offset(left_nritems
- push_items
),
1646 push_items
* sizeof(struct btrfs_item
));
1648 /* update the item pointers */
1649 right_nritems
+= push_items
;
1650 btrfs_set_header_nritems(right
, right_nritems
);
1651 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1652 for (i
= 0; i
< right_nritems
; i
++) {
1653 item
= btrfs_item_nr(right
, i
);
1654 push_space
-= btrfs_item_size(right
, item
);
1655 btrfs_set_item_offset(right
, item
, push_space
);
1658 left_nritems
-= push_items
;
1659 btrfs_set_header_nritems(left
, left_nritems
);
1662 btrfs_mark_buffer_dirty(left
);
1663 btrfs_mark_buffer_dirty(right
);
1665 btrfs_item_key(right
, &disk_key
, 0);
1666 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1667 btrfs_mark_buffer_dirty(upper
);
1669 /* then fixup the leaf pointer in the path */
1670 if (path
->slots
[0] >= left_nritems
) {
1671 path
->slots
[0] -= left_nritems
;
1672 free_extent_buffer(path
->nodes
[0]);
1673 path
->nodes
[0] = right
;
1674 path
->slots
[1] += 1;
1676 free_extent_buffer(right
);
1681 * push some data in the path leaf to the left, trying to free up at
1682 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1684 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1685 *root
, struct btrfs_path
*path
, int data_size
,
1688 struct btrfs_disk_key disk_key
;
1689 struct extent_buffer
*right
= path
->nodes
[0];
1690 struct extent_buffer
*left
;
1696 struct btrfs_item
*item
;
1697 u32 old_left_nritems
;
1703 u32 old_left_item_size
;
1705 slot
= path
->slots
[1];
1708 if (!path
->nodes
[1])
1711 right_nritems
= btrfs_header_nritems(right
);
1712 if (right_nritems
== 0) {
1716 left
= read_tree_block(root
, btrfs_node_blockptr(path
->nodes
[1],
1717 slot
- 1), root
->leafsize
);
1718 free_space
= btrfs_leaf_free_space(root
, left
);
1719 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1720 free_extent_buffer(left
);
1724 /* cow and double check */
1725 ret
= btrfs_cow_block(trans
, root
, left
,
1726 path
->nodes
[1], slot
- 1, &left
);
1728 /* we hit -ENOSPC, but it isn't fatal here */
1729 free_extent_buffer(left
);
1733 free_space
= btrfs_leaf_free_space(root
, left
);
1734 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1735 free_extent_buffer(left
);
1742 nr
= right_nritems
- 1;
1744 for (i
= 0; i
< nr
; i
++) {
1745 item
= btrfs_item_nr(right
, i
);
1747 if (path
->slots
[0] == i
)
1748 push_space
+= data_size
+ sizeof(*item
);
1750 this_item_size
= btrfs_item_size(right
, item
);
1751 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1755 push_space
+= this_item_size
+ sizeof(*item
);
1758 if (push_items
== 0) {
1759 free_extent_buffer(left
);
1762 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1765 /* push data from right to left */
1766 copy_extent_buffer(left
, right
,
1767 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1768 btrfs_item_nr_offset(0),
1769 push_items
* sizeof(struct btrfs_item
));
1771 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1772 btrfs_item_offset_nr(right
, push_items
-1);
1774 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1775 leaf_data_end(root
, left
) - push_space
,
1776 btrfs_leaf_data(right
) +
1777 btrfs_item_offset_nr(right
, push_items
- 1),
1779 old_left_nritems
= btrfs_header_nritems(left
);
1780 BUG_ON(old_left_nritems
< 0);
1782 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1783 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1786 item
= btrfs_item_nr(left
, i
);
1787 ioff
= btrfs_item_offset(left
, item
);
1788 btrfs_set_item_offset(left
, item
,
1789 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
1791 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1793 /* fixup right node */
1794 if (push_items
> right_nritems
) {
1795 printk("push items %d nr %u\n", push_items
, right_nritems
);
1799 if (push_items
< right_nritems
) {
1800 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1801 leaf_data_end(root
, right
);
1802 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1803 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1804 btrfs_leaf_data(right
) +
1805 leaf_data_end(root
, right
), push_space
);
1807 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1808 btrfs_item_nr_offset(push_items
),
1809 (btrfs_header_nritems(right
) - push_items
) *
1810 sizeof(struct btrfs_item
));
1812 right_nritems
-= push_items
;
1813 btrfs_set_header_nritems(right
, right_nritems
);
1814 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1815 for (i
= 0; i
< right_nritems
; i
++) {
1816 item
= btrfs_item_nr(right
, i
);
1817 push_space
= push_space
- btrfs_item_size(right
, item
);
1818 btrfs_set_item_offset(right
, item
, push_space
);
1821 btrfs_mark_buffer_dirty(left
);
1823 btrfs_mark_buffer_dirty(right
);
1825 btrfs_item_key(right
, &disk_key
, 0);
1826 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1830 /* then fixup the leaf pointer in the path */
1831 if (path
->slots
[0] < push_items
) {
1832 path
->slots
[0] += old_left_nritems
;
1833 free_extent_buffer(path
->nodes
[0]);
1834 path
->nodes
[0] = left
;
1835 path
->slots
[1] -= 1;
1837 free_extent_buffer(left
);
1838 path
->slots
[0] -= push_items
;
1840 BUG_ON(path
->slots
[0] < 0);
1845 * split the path's leaf in two, making sure there is at least data_size
1846 * available for the resulting leaf level of the path.
1848 * returns 0 if all went well and < 0 on failure.
1850 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
1851 *root
, struct btrfs_key
*ins_key
,
1852 struct btrfs_path
*path
, int data_size
, int extend
)
1855 struct extent_buffer
*l
;
1859 struct extent_buffer
*right
;
1860 int space_needed
= data_size
+ sizeof(struct btrfs_item
);
1867 int num_doubles
= 0;
1868 struct btrfs_disk_key disk_key
;
1871 space_needed
= data_size
;
1874 root_gen
= trans
->transid
;
1878 /* first try to make some room by pushing left and right */
1879 if (ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
1880 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
1885 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
1891 /* did the pushes work? */
1892 if (btrfs_leaf_free_space(root
, l
) >= space_needed
)
1896 if (!path
->nodes
[1]) {
1897 ret
= insert_new_root(trans
, root
, path
, 1);
1904 slot
= path
->slots
[0];
1905 nritems
= btrfs_header_nritems(l
);
1906 mid
= (nritems
+ 1)/ 2;
1908 btrfs_item_key(l
, &disk_key
, 0);
1910 right
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
1911 root
->root_key
.objectid
,
1912 root_gen
, disk_key
.objectid
, 0,
1915 return PTR_ERR(right
);
1917 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
1918 btrfs_set_header_bytenr(right
, right
->start
);
1919 btrfs_set_header_generation(right
, trans
->transid
);
1920 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
1921 btrfs_set_header_level(right
, 0);
1922 write_extent_buffer(right
, root
->fs_info
->fsid
,
1923 (unsigned long)btrfs_header_fsid(right
),
1927 leaf_space_used(l
, mid
, nritems
- mid
) + space_needed
>
1928 BTRFS_LEAF_DATA_SIZE(root
)) {
1929 if (slot
>= nritems
) {
1930 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
1931 btrfs_set_header_nritems(right
, 0);
1932 wret
= insert_ptr(trans
, root
, path
,
1933 &disk_key
, right
->start
,
1934 path
->slots
[1] + 1, 1);
1937 free_extent_buffer(path
->nodes
[0]);
1938 path
->nodes
[0] = right
;
1940 path
->slots
[1] += 1;
1944 if (mid
!= nritems
&&
1945 leaf_space_used(l
, mid
, nritems
- mid
) +
1946 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
1951 if (leaf_space_used(l
, 0, mid
+ 1) + space_needed
>
1952 BTRFS_LEAF_DATA_SIZE(root
)) {
1953 if (!extend
&& slot
== 0) {
1954 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
1955 btrfs_set_header_nritems(right
, 0);
1956 wret
= insert_ptr(trans
, root
, path
,
1962 free_extent_buffer(path
->nodes
[0]);
1963 path
->nodes
[0] = right
;
1965 if (path
->slots
[1] == 0) {
1966 wret
= fixup_low_keys(trans
, root
,
1967 path
, &disk_key
, 1);
1972 } else if (extend
&& slot
== 0) {
1976 if (mid
!= nritems
&&
1977 leaf_space_used(l
, mid
, nritems
- mid
) +
1978 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
1984 nritems
= nritems
- mid
;
1985 btrfs_set_header_nritems(right
, nritems
);
1986 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
1988 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
1989 btrfs_item_nr_offset(mid
),
1990 nritems
* sizeof(struct btrfs_item
));
1992 copy_extent_buffer(right
, l
,
1993 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
1994 data_copy_size
, btrfs_leaf_data(l
) +
1995 leaf_data_end(root
, l
), data_copy_size
);
1997 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
1998 btrfs_item_end_nr(l
, mid
);
2000 for (i
= 0; i
< nritems
; i
++) {
2001 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2002 u32 ioff
= btrfs_item_offset(right
, item
);
2003 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2006 btrfs_set_header_nritems(l
, mid
);
2008 btrfs_item_key(right
, &disk_key
, 0);
2009 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2010 path
->slots
[1] + 1, 1);
2014 btrfs_mark_buffer_dirty(right
);
2015 btrfs_mark_buffer_dirty(l
);
2016 BUG_ON(path
->slots
[0] != slot
);
2019 free_extent_buffer(path
->nodes
[0]);
2020 path
->nodes
[0] = right
;
2021 path
->slots
[0] -= mid
;
2022 path
->slots
[1] += 1;
2024 free_extent_buffer(right
);
2026 BUG_ON(path
->slots
[0] < 0);
2029 BUG_ON(num_doubles
!= 0);
2036 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
2037 struct btrfs_root
*root
,
2038 struct btrfs_path
*path
,
2039 u32 new_size
, int from_end
)
2044 struct extent_buffer
*leaf
;
2045 struct btrfs_item
*item
;
2047 unsigned int data_end
;
2048 unsigned int old_data_start
;
2049 unsigned int old_size
;
2050 unsigned int size_diff
;
2053 slot_orig
= path
->slots
[0];
2054 leaf
= path
->nodes
[0];
2055 slot
= path
->slots
[0];
2057 old_size
= btrfs_item_size_nr(leaf
, slot
);
2058 if (old_size
== new_size
)
2061 nritems
= btrfs_header_nritems(leaf
);
2062 data_end
= leaf_data_end(root
, leaf
);
2064 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2066 size_diff
= old_size
- new_size
;
2069 BUG_ON(slot
>= nritems
);
2072 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2074 /* first correct the data pointers */
2075 for (i
= slot
; i
< nritems
; i
++) {
2077 item
= btrfs_item_nr(leaf
, i
);
2078 ioff
= btrfs_item_offset(leaf
, item
);
2079 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2082 /* shift the data */
2084 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2085 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2086 data_end
, old_data_start
+ new_size
- data_end
);
2088 struct btrfs_disk_key disk_key
;
2091 btrfs_item_key(leaf
, &disk_key
, slot
);
2093 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2095 struct btrfs_file_extent_item
*fi
;
2097 fi
= btrfs_item_ptr(leaf
, slot
,
2098 struct btrfs_file_extent_item
);
2099 fi
= (struct btrfs_file_extent_item
*)(
2100 (unsigned long)fi
- size_diff
);
2102 if (btrfs_file_extent_type(leaf
, fi
) ==
2103 BTRFS_FILE_EXTENT_INLINE
) {
2104 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2105 memmove_extent_buffer(leaf
, ptr
,
2107 offsetof(struct btrfs_file_extent_item
,
2112 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2113 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2114 data_end
, old_data_start
- data_end
);
2116 offset
= btrfs_disk_key_offset(&disk_key
);
2117 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2118 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2120 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2123 item
= btrfs_item_nr(leaf
, slot
);
2124 btrfs_set_item_size(leaf
, item
, new_size
);
2125 btrfs_mark_buffer_dirty(leaf
);
2128 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2129 btrfs_print_leaf(root
, leaf
);
2135 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
2136 struct btrfs_root
*root
, struct btrfs_path
*path
,
2142 struct extent_buffer
*leaf
;
2143 struct btrfs_item
*item
;
2145 unsigned int data_end
;
2146 unsigned int old_data
;
2147 unsigned int old_size
;
2150 slot_orig
= path
->slots
[0];
2151 leaf
= path
->nodes
[0];
2153 nritems
= btrfs_header_nritems(leaf
);
2154 data_end
= leaf_data_end(root
, leaf
);
2156 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2157 btrfs_print_leaf(root
, leaf
);
2160 slot
= path
->slots
[0];
2161 old_data
= btrfs_item_end_nr(leaf
, slot
);
2164 if (slot
>= nritems
) {
2165 btrfs_print_leaf(root
, leaf
);
2166 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2171 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2173 /* first correct the data pointers */
2174 for (i
= slot
; i
< nritems
; i
++) {
2176 item
= btrfs_item_nr(leaf
, i
);
2177 ioff
= btrfs_item_offset(leaf
, item
);
2178 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2181 /* shift the data */
2182 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2183 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2184 data_end
, old_data
- data_end
);
2186 data_end
= old_data
;
2187 old_size
= btrfs_item_size_nr(leaf
, slot
);
2188 item
= btrfs_item_nr(leaf
, slot
);
2189 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2190 btrfs_mark_buffer_dirty(leaf
);
2193 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2194 btrfs_print_leaf(root
, leaf
);
2201 * Given a key and some data, insert an item into the tree.
2202 * This does all the path init required, making room in the tree if needed.
2204 int btrfs_insert_empty_item(struct btrfs_trans_handle
*trans
,
2205 struct btrfs_root
*root
,
2206 struct btrfs_path
*path
,
2207 struct btrfs_key
*cpu_key
, u32 data_size
)
2209 struct extent_buffer
*leaf
;
2210 struct btrfs_item
*item
;
2215 unsigned int data_end
;
2216 struct btrfs_disk_key disk_key
;
2218 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2220 /* create a root if there isn't one */
2224 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, data_size
, 1);
2231 slot_orig
= path
->slots
[0];
2232 leaf
= path
->nodes
[0];
2234 nritems
= btrfs_header_nritems(leaf
);
2235 data_end
= leaf_data_end(root
, leaf
);
2237 if (btrfs_leaf_free_space(root
, leaf
) <
2238 sizeof(struct btrfs_item
) + data_size
) {
2239 btrfs_print_leaf(root
, leaf
);
2240 printk("not enough freespace need %u have %d\n",
2241 data_size
, btrfs_leaf_free_space(root
, leaf
));
2245 slot
= path
->slots
[0];
2248 if (slot
!= nritems
) {
2250 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2252 if (old_data
< data_end
) {
2253 btrfs_print_leaf(root
, leaf
);
2254 printk("slot %d old_data %d data_end %d\n",
2255 slot
, old_data
, data_end
);
2259 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2261 /* first correct the data pointers */
2262 for (i
= slot
; i
< nritems
; i
++) {
2265 item
= btrfs_item_nr(leaf
, i
);
2266 ioff
= btrfs_item_offset(leaf
, item
);
2267 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2270 /* shift the items */
2271 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2272 btrfs_item_nr_offset(slot
),
2273 (nritems
- slot
) * sizeof(struct btrfs_item
));
2275 /* shift the data */
2276 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2277 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2278 data_end
, old_data
- data_end
);
2279 data_end
= old_data
;
2282 /* setup the item for the new data */
2283 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2284 item
= btrfs_item_nr(leaf
, slot
);
2285 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
);
2286 btrfs_set_item_size(leaf
, item
, data_size
);
2287 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2288 btrfs_mark_buffer_dirty(leaf
);
2292 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2294 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2295 btrfs_print_leaf(root
, leaf
);
2303 * Given a key and some data, insert an item into the tree.
2304 * This does all the path init required, making room in the tree if needed.
2306 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2307 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2311 struct btrfs_path
*path
;
2312 struct extent_buffer
*leaf
;
2315 path
= btrfs_alloc_path();
2317 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2319 leaf
= path
->nodes
[0];
2320 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2321 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2322 btrfs_mark_buffer_dirty(leaf
);
2324 btrfs_free_path(path
);
2329 * delete the pointer from a given node.
2331 * If the delete empties a node, the node is removed from the tree,
2332 * continuing all the way the root if required. The root is converted into
2333 * a leaf if all the nodes are emptied.
2335 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2336 struct btrfs_path
*path
, int level
, int slot
)
2338 struct extent_buffer
*parent
= path
->nodes
[level
];
2343 nritems
= btrfs_header_nritems(parent
);
2344 if (slot
!= nritems
-1) {
2345 memmove_extent_buffer(parent
,
2346 btrfs_node_key_ptr_offset(slot
),
2347 btrfs_node_key_ptr_offset(slot
+ 1),
2348 sizeof(struct btrfs_key_ptr
) *
2349 (nritems
- slot
- 1));
2352 btrfs_set_header_nritems(parent
, nritems
);
2353 if (nritems
== 0 && parent
== root
->node
) {
2354 BUG_ON(btrfs_header_level(root
->node
) != 1);
2355 /* just turn the root into a leaf and break */
2356 btrfs_set_header_level(root
->node
, 0);
2357 } else if (slot
== 0) {
2358 struct btrfs_disk_key disk_key
;
2360 btrfs_node_key(parent
, &disk_key
, 0);
2361 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
2365 btrfs_mark_buffer_dirty(parent
);
2370 * delete the item at the leaf level in path. If that empties
2371 * the leaf, remove it from the tree
2373 int btrfs_del_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2374 struct btrfs_path
*path
)
2377 struct extent_buffer
*leaf
;
2378 struct btrfs_item
*item
;
2385 leaf
= path
->nodes
[0];
2386 slot
= path
->slots
[0];
2387 doff
= btrfs_item_offset_nr(leaf
, slot
);
2388 dsize
= btrfs_item_size_nr(leaf
, slot
);
2389 nritems
= btrfs_header_nritems(leaf
);
2391 if (slot
!= nritems
- 1) {
2393 int data_end
= leaf_data_end(root
, leaf
);
2395 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2397 btrfs_leaf_data(leaf
) + data_end
,
2400 for (i
= slot
+ 1; i
< nritems
; i
++) {
2403 item
= btrfs_item_nr(leaf
, i
);
2404 ioff
= btrfs_item_offset(leaf
, item
);
2405 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2408 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2409 btrfs_item_nr_offset(slot
+ 1),
2410 sizeof(struct btrfs_item
) *
2411 (nritems
- slot
- 1));
2413 btrfs_set_header_nritems(leaf
, nritems
- 1);
2416 /* delete the leaf if we've emptied it */
2418 if (leaf
== root
->node
) {
2419 btrfs_set_header_level(leaf
, 0);
2421 u64 root_gen
= btrfs_header_generation(path
->nodes
[1]);
2422 clean_tree_block(trans
, root
, leaf
);
2423 wait_on_tree_block_writeback(root
, leaf
);
2424 wret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
2427 wret
= btrfs_free_extent(trans
, root
,
2428 leaf
->start
, leaf
->len
,
2429 btrfs_header_owner(path
->nodes
[1]),
2435 int used
= leaf_space_used(leaf
, 0, nritems
);
2437 struct btrfs_disk_key disk_key
;
2439 btrfs_item_key(leaf
, &disk_key
, 0);
2440 wret
= fixup_low_keys(trans
, root
, path
,
2446 /* delete the leaf if it is mostly empty */
2447 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
2448 /* push_leaf_left fixes the path.
2449 * make sure the path still points to our leaf
2450 * for possible call to del_ptr below
2452 slot
= path
->slots
[1];
2453 extent_buffer_get(leaf
);
2455 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2456 if (wret
< 0 && wret
!= -ENOSPC
)
2459 if (path
->nodes
[0] == leaf
&&
2460 btrfs_header_nritems(leaf
)) {
2461 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2462 if (wret
< 0 && wret
!= -ENOSPC
)
2466 if (btrfs_header_nritems(leaf
) == 0) {
2468 u64 bytenr
= leaf
->start
;
2469 u32 blocksize
= leaf
->len
;
2471 root_gen
= btrfs_header_generation(
2474 clean_tree_block(trans
, root
, leaf
);
2475 wait_on_tree_block_writeback(root
, leaf
);
2477 wret
= del_ptr(trans
, root
, path
, 1, slot
);
2481 free_extent_buffer(leaf
);
2482 wret
= btrfs_free_extent(trans
, root
, bytenr
,
2484 btrfs_header_owner(path
->nodes
[1]),
2489 btrfs_mark_buffer_dirty(leaf
);
2490 free_extent_buffer(leaf
);
2493 btrfs_mark_buffer_dirty(leaf
);
2500 * walk up the tree as far as required to find the previous leaf.
2501 * returns 0 if it found something or 1 if there are no lesser leaves.
2502 * returns < 0 on io errors.
2504 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2509 struct extent_buffer
*c
;
2510 struct extent_buffer
*next
= NULL
;
2512 while(level
< BTRFS_MAX_LEVEL
) {
2513 if (!path
->nodes
[level
])
2516 slot
= path
->slots
[level
];
2517 c
= path
->nodes
[level
];
2520 if (level
== BTRFS_MAX_LEVEL
)
2526 bytenr
= btrfs_node_blockptr(c
, slot
);
2528 free_extent_buffer(next
);
2530 next
= read_tree_block(root
, bytenr
,
2531 btrfs_level_size(root
, level
- 1));
2534 path
->slots
[level
] = slot
;
2537 c
= path
->nodes
[level
];
2538 free_extent_buffer(c
);
2539 slot
= btrfs_header_nritems(next
);
2542 path
->nodes
[level
] = next
;
2543 path
->slots
[level
] = slot
;
2546 next
= read_tree_block(root
, btrfs_node_blockptr(next
, slot
),
2547 btrfs_level_size(root
, level
- 1));
2553 * walk up the tree as far as required to find the next leaf.
2554 * returns 0 if it found something or 1 if there are no greater leaves.
2555 * returns < 0 on io errors.
2557 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2562 struct extent_buffer
*c
;
2563 struct extent_buffer
*next
= NULL
;
2565 while(level
< BTRFS_MAX_LEVEL
) {
2566 if (!path
->nodes
[level
])
2569 slot
= path
->slots
[level
] + 1;
2570 c
= path
->nodes
[level
];
2571 if (slot
>= btrfs_header_nritems(c
)) {
2573 if (level
== BTRFS_MAX_LEVEL
)
2578 bytenr
= btrfs_node_blockptr(c
, slot
);
2580 free_extent_buffer(next
);
2583 reada_for_search(root
, path
, level
, slot
, 0);
2585 next
= read_tree_block(root
, bytenr
,
2586 btrfs_level_size(root
, level
-1));
2589 path
->slots
[level
] = slot
;
2592 c
= path
->nodes
[level
];
2593 free_extent_buffer(c
);
2594 path
->nodes
[level
] = next
;
2595 path
->slots
[level
] = 0;
2599 reada_for_search(root
, path
, level
, 0, 0);
2600 next
= read_tree_block(root
, btrfs_node_blockptr(next
, 0),
2601 btrfs_level_size(root
, level
- 1));