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"
23 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
24 *root
, struct btrfs_path
*path
, int level
);
25 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
26 *root
, struct btrfs_key
*ins_key
,
27 struct btrfs_path
*path
, int data_size
, int extend
);
28 static int push_node_left(struct btrfs_trans_handle
*trans
,
29 struct btrfs_root
*root
, struct extent_buffer
*dst
,
30 struct extent_buffer
*src
, int empty
);
31 static int balance_node_right(struct btrfs_trans_handle
*trans
,
32 struct btrfs_root
*root
,
33 struct extent_buffer
*dst_buf
,
34 struct extent_buffer
*src_buf
);
35 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
36 struct btrfs_path
*path
, int level
, int slot
);
38 inline void btrfs_init_path(struct btrfs_path
*p
)
40 memset(p
, 0, sizeof(*p
));
43 struct btrfs_path
*btrfs_alloc_path(void)
45 struct btrfs_path
*path
;
46 path
= kmalloc(sizeof(struct btrfs_path
), GFP_NOFS
);
48 btrfs_init_path(path
);
54 void btrfs_free_path(struct btrfs_path
*p
)
56 btrfs_release_path(NULL
, p
);
60 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
63 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
66 free_extent_buffer(p
->nodes
[i
]);
68 memset(p
, 0, sizeof(*p
));
71 static void add_root_to_dirty_list(struct btrfs_root
*root
)
73 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
74 list_add(&root
->dirty_list
,
75 &root
->fs_info
->dirty_cowonly_roots
);
79 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
80 struct btrfs_root
*root
,
81 struct extent_buffer
*buf
,
82 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
84 struct extent_buffer
*cow
;
88 struct btrfs_key first_key
;
89 struct btrfs_root
*new_root
;
91 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
95 memcpy(new_root
, root
, sizeof(*new_root
));
96 new_root
->root_key
.objectid
= new_root_objectid
;
98 WARN_ON(root
->ref_cows
&& trans
->transid
!=
99 root
->fs_info
->running_transaction
->transid
);
100 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
102 level
= btrfs_header_level(buf
);
103 nritems
= btrfs_header_nritems(buf
);
106 btrfs_item_key_to_cpu(buf
, &first_key
, 0);
108 btrfs_node_key_to_cpu(buf
, &first_key
, 0);
110 first_key
.objectid
= 0;
112 cow
= __btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
114 trans
->transid
, first_key
.objectid
,
115 level
, buf
->start
, 0);
121 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
122 btrfs_set_header_bytenr(cow
, cow
->start
);
123 btrfs_set_header_generation(cow
, trans
->transid
);
124 btrfs_set_header_owner(cow
, new_root_objectid
);
125 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
);
127 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
128 ret
= btrfs_inc_ref(trans
, new_root
, buf
);
134 btrfs_mark_buffer_dirty(cow
);
139 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
140 struct btrfs_root
*root
,
141 struct extent_buffer
*buf
,
142 struct extent_buffer
*parent
, int parent_slot
,
143 struct extent_buffer
**cow_ret
,
144 u64 search_start
, u64 empty_size
)
147 struct extent_buffer
*cow
;
150 int different_trans
= 0;
152 struct btrfs_key first_key
;
154 if (root
->ref_cows
) {
155 root_gen
= trans
->transid
;
160 WARN_ON(root
->ref_cows
&& trans
->transid
!=
161 root
->fs_info
->running_transaction
->transid
);
162 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
164 level
= btrfs_header_level(buf
);
165 nritems
= btrfs_header_nritems(buf
);
168 btrfs_item_key_to_cpu(buf
, &first_key
, 0);
170 btrfs_node_key_to_cpu(buf
, &first_key
, 0);
172 first_key
.objectid
= 0;
174 cow
= __btrfs_alloc_free_block(trans
, root
, buf
->len
,
175 root
->root_key
.objectid
,
176 root_gen
, first_key
.objectid
, level
,
177 search_start
, empty_size
);
181 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
182 btrfs_set_header_bytenr(cow
, cow
->start
);
183 btrfs_set_header_generation(cow
, trans
->transid
);
184 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
185 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
);
187 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
188 if (btrfs_header_generation(buf
) != trans
->transid
) {
190 ret
= btrfs_inc_ref(trans
, root
, buf
);
194 clean_tree_block(trans
, root
, buf
);
197 if (buf
== root
->node
) {
198 root_gen
= btrfs_header_generation(buf
);
200 extent_buffer_get(cow
);
201 if (buf
!= root
->commit_root
) {
202 btrfs_free_extent(trans
, root
, buf
->start
,
203 buf
->len
, root
->root_key
.objectid
,
206 free_extent_buffer(buf
);
207 add_root_to_dirty_list(root
);
209 root_gen
= btrfs_header_generation(parent
);
210 btrfs_set_node_blockptr(parent
, parent_slot
,
212 WARN_ON(trans
->transid
== 0);
213 btrfs_set_node_ptr_generation(parent
, parent_slot
,
215 btrfs_mark_buffer_dirty(parent
);
216 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
217 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
218 btrfs_header_owner(parent
), root_gen
,
221 free_extent_buffer(buf
);
222 btrfs_mark_buffer_dirty(cow
);
227 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
228 struct btrfs_root
*root
, struct extent_buffer
*buf
,
229 struct extent_buffer
*parent
, int parent_slot
,
230 struct extent_buffer
**cow_ret
)
235 if (trans->transaction != root->fs_info->running_transaction) {
236 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
237 root->fs_info->running_transaction->transid);
241 if (trans
->transid
!= root
->fs_info
->generation
) {
242 printk(KERN_CRIT
"trans %llu running %llu\n",
243 (unsigned long long)trans
->transid
,
244 (unsigned long long)root
->fs_info
->generation
);
247 if (btrfs_header_generation(buf
) == trans
->transid
&&
248 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
253 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
254 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
255 parent_slot
, cow_ret
, search_start
, 0);
260 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
262 if (blocknr < other && other - (blocknr + blocksize) < 32768)
264 if (blocknr > other && blocknr - (other + blocksize) < 32768)
271 * compare two keys in a memcmp fashion
273 int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
277 btrfs_disk_key_to_cpu(&k1
, disk
);
279 if (k1
.objectid
> k2
->objectid
)
281 if (k1
.objectid
< k2
->objectid
)
283 if (k1
.type
> k2
->type
)
285 if (k1
.type
< k2
->type
)
287 if (k1
.offset
> k2
->offset
)
289 if (k1
.offset
< k2
->offset
)
296 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
297 struct btrfs_root
*root
, struct extent_buffer
*parent
,
298 int start_slot
, int cache_only
, u64
*last_ret
,
299 struct btrfs_key
*progress
)
301 struct extent_buffer
*cur
;
302 struct extent_buffer
*tmp
;
304 u64 search_start
= *last_ret
;
314 int progress_passed
= 0;
315 struct btrfs_disk_key disk_key
;
317 parent_level
= btrfs_header_level(parent
);
318 if (cache_only
&& parent_level
!= 1)
321 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
322 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
323 root
->fs_info
->running_transaction
->transid
);
326 if (trans
->transid
!= root
->fs_info
->generation
) {
327 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
328 root
->fs_info
->generation
);
332 parent_nritems
= btrfs_header_nritems(parent
);
333 blocksize
= btrfs_level_size(root
, parent_level
- 1);
334 end_slot
= parent_nritems
;
336 if (parent_nritems
== 1)
339 for (i
= start_slot
; i
< end_slot
; i
++) {
342 if (!parent
->map_token
) {
343 map_extent_buffer(parent
,
344 btrfs_node_key_ptr_offset(i
),
345 sizeof(struct btrfs_key_ptr
),
346 &parent
->map_token
, &parent
->kaddr
,
347 &parent
->map_start
, &parent
->map_len
,
350 btrfs_node_key(parent
, &disk_key
, i
);
351 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
355 blocknr
= btrfs_node_blockptr(parent
, i
);
357 last_block
= blocknr
;
360 other
= btrfs_node_blockptr(parent
, i
- 1);
361 close
= close_blocks(blocknr
, other
, blocksize
);
363 if (close
&& i
< end_slot
- 2) {
364 other
= btrfs_node_blockptr(parent
, i
+ 1);
365 close
= close_blocks(blocknr
, other
, blocksize
);
368 last_block
= blocknr
;
371 if (parent
->map_token
) {
372 unmap_extent_buffer(parent
, parent
->map_token
,
374 parent
->map_token
= NULL
;
377 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
379 uptodate
= btrfs_buffer_uptodate(cur
);
382 if (!cur
|| !uptodate
) {
384 free_extent_buffer(cur
);
388 cur
= read_tree_block(root
, blocknr
,
390 } else if (!uptodate
) {
391 btrfs_read_buffer(cur
);
394 if (search_start
== 0)
395 search_start
= last_block
;
397 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
400 (end_slot
- i
) * blocksize
));
402 free_extent_buffer(cur
);
405 search_start
= tmp
->start
;
406 last_block
= tmp
->start
;
407 *last_ret
= search_start
;
408 if (parent_level
== 1)
409 btrfs_clear_buffer_defrag(tmp
);
410 free_extent_buffer(tmp
);
412 if (parent
->map_token
) {
413 unmap_extent_buffer(parent
, parent
->map_token
,
415 parent
->map_token
= NULL
;
422 * The leaf data grows from end-to-front in the node.
423 * this returns the address of the start of the last item,
424 * which is the stop of the leaf data stack
426 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
427 struct extent_buffer
*leaf
)
429 u32 nr
= btrfs_header_nritems(leaf
);
431 return BTRFS_LEAF_DATA_SIZE(root
);
432 return btrfs_item_offset_nr(leaf
, nr
- 1);
435 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
438 struct extent_buffer
*parent
= NULL
;
439 struct extent_buffer
*node
= path
->nodes
[level
];
440 struct btrfs_disk_key parent_key
;
441 struct btrfs_disk_key node_key
;
444 struct btrfs_key cpukey
;
445 u32 nritems
= btrfs_header_nritems(node
);
447 if (path
->nodes
[level
+ 1])
448 parent
= path
->nodes
[level
+ 1];
450 slot
= path
->slots
[level
];
451 BUG_ON(nritems
== 0);
453 parent_slot
= path
->slots
[level
+ 1];
454 btrfs_node_key(parent
, &parent_key
, parent_slot
);
455 btrfs_node_key(node
, &node_key
, 0);
456 BUG_ON(memcmp(&parent_key
, &node_key
,
457 sizeof(struct btrfs_disk_key
)));
458 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
459 btrfs_header_bytenr(node
));
461 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
463 btrfs_node_key_to_cpu(node
, &cpukey
, slot
- 1);
464 btrfs_node_key(node
, &node_key
, slot
);
465 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) <= 0);
467 if (slot
< nritems
- 1) {
468 btrfs_node_key_to_cpu(node
, &cpukey
, slot
+ 1);
469 btrfs_node_key(node
, &node_key
, slot
);
470 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) >= 0);
475 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
478 struct extent_buffer
*leaf
= path
->nodes
[level
];
479 struct extent_buffer
*parent
= NULL
;
481 struct btrfs_key cpukey
;
482 struct btrfs_disk_key parent_key
;
483 struct btrfs_disk_key leaf_key
;
484 int slot
= path
->slots
[0];
486 u32 nritems
= btrfs_header_nritems(leaf
);
488 if (path
->nodes
[level
+ 1])
489 parent
= path
->nodes
[level
+ 1];
495 parent_slot
= path
->slots
[level
+ 1];
496 btrfs_node_key(parent
, &parent_key
, parent_slot
);
497 btrfs_item_key(leaf
, &leaf_key
, 0);
499 BUG_ON(memcmp(&parent_key
, &leaf_key
,
500 sizeof(struct btrfs_disk_key
)));
501 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
502 btrfs_header_bytenr(leaf
));
505 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
506 btrfs_item_key_to_cpu(leaf
, &cpukey
, i
+ 1);
507 btrfs_item_key(leaf
, &leaf_key
, i
);
508 if (comp_keys(&leaf_key
, &cpukey
) >= 0) {
509 btrfs_print_leaf(root
, leaf
);
510 printk("slot %d offset bad key\n", i
);
513 if (btrfs_item_offset_nr(leaf
, i
) !=
514 btrfs_item_end_nr(leaf
, i
+ 1)) {
515 btrfs_print_leaf(root
, leaf
);
516 printk("slot %d offset bad\n", i
);
520 if (btrfs_item_offset_nr(leaf
, i
) +
521 btrfs_item_size_nr(leaf
, i
) !=
522 BTRFS_LEAF_DATA_SIZE(root
)) {
523 btrfs_print_leaf(root
, leaf
);
524 printk("slot %d first offset bad\n", i
);
530 if (btrfs_item_size_nr(leaf
, nritems
- 1) > 4096) {
531 btrfs_print_leaf(root
, leaf
);
532 printk("slot %d bad size \n", nritems
- 1);
537 if (slot
!= 0 && slot
< nritems
- 1) {
538 btrfs_item_key(leaf
, &leaf_key
, slot
);
539 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
- 1);
540 if (btrfs_comp_keys(&leaf_key
, &cpukey
) <= 0) {
541 btrfs_print_leaf(root
, leaf
);
542 printk("slot %d offset bad key\n", slot
);
545 if (btrfs_item_offset_nr(leaf
, slot
- 1) !=
546 btrfs_item_end_nr(leaf
, slot
)) {
547 btrfs_print_leaf(root
, leaf
);
548 printk("slot %d offset bad\n", slot
);
552 if (slot
< nritems
- 1) {
553 btrfs_item_key(leaf
, &leaf_key
, slot
);
554 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
+ 1);
555 BUG_ON(btrfs_comp_keys(&leaf_key
, &cpukey
) >= 0);
556 if (btrfs_item_offset_nr(leaf
, slot
) !=
557 btrfs_item_end_nr(leaf
, slot
+ 1)) {
558 btrfs_print_leaf(root
, leaf
);
559 printk("slot %d offset bad\n", slot
);
563 BUG_ON(btrfs_item_offset_nr(leaf
, 0) +
564 btrfs_item_size_nr(leaf
, 0) != BTRFS_LEAF_DATA_SIZE(root
));
568 static int noinline
check_block(struct btrfs_root
*root
,
569 struct btrfs_path
*path
, int level
)
573 struct extent_buffer
*buf
= path
->nodes
[level
];
575 if (memcmp_extent_buffer(buf
, root
->fs_info
->fsid
,
576 (unsigned long)btrfs_header_fsid(buf
),
578 printk("warning bad block %Lu\n", buf
->start
);
583 return check_leaf(root
, path
, level
);
584 return check_node(root
, path
, level
);
588 * search for key in the extent_buffer. The items start at offset p,
589 * and they are item_size apart. There are 'max' items in p.
591 * the slot in the array is returned via slot, and it points to
592 * the place where you would insert key if it is not found in
595 * slot may point to max if the key is bigger than all of the keys
597 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
598 int item_size
, struct btrfs_key
*key
,
605 unsigned long offset
;
606 struct btrfs_disk_key
*tmp
;
609 mid
= (low
+ high
) / 2;
610 offset
= p
+ mid
* item_size
;
612 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
613 ret
= btrfs_comp_keys(tmp
, key
);
629 * simple bin_search frontend that does the right thing for
632 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
633 int level
, int *slot
)
636 return generic_bin_search(eb
,
637 offsetof(struct btrfs_leaf
, items
),
638 sizeof(struct btrfs_item
),
639 key
, btrfs_header_nritems(eb
),
642 return generic_bin_search(eb
,
643 offsetof(struct btrfs_node
, ptrs
),
644 sizeof(struct btrfs_key_ptr
),
645 key
, btrfs_header_nritems(eb
),
651 static struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
652 struct extent_buffer
*parent
, int slot
)
656 if (slot
>= btrfs_header_nritems(parent
))
658 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
659 btrfs_level_size(root
, btrfs_header_level(parent
) - 1));
662 static int balance_level(struct btrfs_trans_handle
*trans
,
663 struct btrfs_root
*root
,
664 struct btrfs_path
*path
, int level
)
666 struct extent_buffer
*right
= NULL
;
667 struct extent_buffer
*mid
;
668 struct extent_buffer
*left
= NULL
;
669 struct extent_buffer
*parent
= NULL
;
673 int orig_slot
= path
->slots
[level
];
674 int err_on_enospc
= 0;
680 mid
= path
->nodes
[level
];
681 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
683 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
685 if (level
< BTRFS_MAX_LEVEL
- 1)
686 parent
= path
->nodes
[level
+ 1];
687 pslot
= path
->slots
[level
+ 1];
690 * deal with the case where there is only one pointer in the root
691 * by promoting the node below to a root
694 struct extent_buffer
*child
;
696 if (btrfs_header_nritems(mid
) != 1)
699 /* promote the child to a root */
700 child
= read_node_slot(root
, mid
, 0);
702 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
706 add_root_to_dirty_list(root
);
707 path
->nodes
[level
] = NULL
;
708 clean_tree_block(trans
, root
, mid
);
709 wait_on_tree_block_writeback(root
, mid
);
710 /* once for the path */
711 free_extent_buffer(mid
);
712 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
713 root
->root_key
.objectid
,
714 btrfs_header_generation(mid
), 0, 0, 1);
715 /* once for the root ptr */
716 free_extent_buffer(mid
);
719 if (btrfs_header_nritems(mid
) >
720 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
723 if (btrfs_header_nritems(mid
) < 2)
726 left
= read_node_slot(root
, parent
, pslot
- 1);
728 wret
= btrfs_cow_block(trans
, root
, left
,
729 parent
, pslot
- 1, &left
);
735 right
= read_node_slot(root
, parent
, pslot
+ 1);
737 wret
= btrfs_cow_block(trans
, root
, right
,
738 parent
, pslot
+ 1, &right
);
745 /* first, try to make some room in the middle buffer */
747 orig_slot
+= btrfs_header_nritems(left
);
748 wret
= push_node_left(trans
, root
, left
, mid
, 1);
751 if (btrfs_header_nritems(mid
) < 2)
756 * then try to empty the right most buffer into the middle
759 wret
= push_node_left(trans
, root
, mid
, right
, 1);
760 if (wret
< 0 && wret
!= -ENOSPC
)
762 if (btrfs_header_nritems(right
) == 0) {
763 u64 bytenr
= right
->start
;
764 u64 generation
= btrfs_header_generation(parent
);
765 u32 blocksize
= right
->len
;
767 clean_tree_block(trans
, root
, right
);
768 wait_on_tree_block_writeback(root
, right
);
769 free_extent_buffer(right
);
771 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
775 wret
= btrfs_free_extent(trans
, root
, bytenr
,
777 btrfs_header_owner(parent
),
778 generation
, 0, 0, 1);
782 struct btrfs_disk_key right_key
;
783 btrfs_node_key(right
, &right_key
, 0);
784 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
785 btrfs_mark_buffer_dirty(parent
);
788 if (btrfs_header_nritems(mid
) == 1) {
790 * we're not allowed to leave a node with one item in the
791 * tree during a delete. A deletion from lower in the tree
792 * could try to delete the only pointer in this node.
793 * So, pull some keys from the left.
794 * There has to be a left pointer at this point because
795 * otherwise we would have pulled some pointers from the
799 wret
= balance_node_right(trans
, root
, mid
, left
);
805 wret
= push_node_left(trans
, root
, left
, mid
, 1);
811 if (btrfs_header_nritems(mid
) == 0) {
812 /* we've managed to empty the middle node, drop it */
813 u64 root_gen
= btrfs_header_generation(parent
);
814 u64 bytenr
= mid
->start
;
815 u32 blocksize
= mid
->len
;
816 clean_tree_block(trans
, root
, mid
);
817 wait_on_tree_block_writeback(root
, mid
);
818 free_extent_buffer(mid
);
820 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
823 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
824 btrfs_header_owner(parent
),
829 /* update the parent key to reflect our changes */
830 struct btrfs_disk_key mid_key
;
831 btrfs_node_key(mid
, &mid_key
, 0);
832 btrfs_set_node_key(parent
, &mid_key
, pslot
);
833 btrfs_mark_buffer_dirty(parent
);
836 /* update the path */
838 if (btrfs_header_nritems(left
) > orig_slot
) {
839 extent_buffer_get(left
);
840 path
->nodes
[level
] = left
;
841 path
->slots
[level
+ 1] -= 1;
842 path
->slots
[level
] = orig_slot
;
844 free_extent_buffer(mid
);
846 orig_slot
-= btrfs_header_nritems(left
);
847 path
->slots
[level
] = orig_slot
;
850 /* double check we haven't messed things up */
851 check_block(root
, path
, level
);
853 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
857 free_extent_buffer(right
);
859 free_extent_buffer(left
);
863 /* returns zero if the push worked, non-zero otherwise */
864 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
865 struct btrfs_root
*root
,
866 struct btrfs_path
*path
, int level
)
868 struct extent_buffer
*right
= NULL
;
869 struct extent_buffer
*mid
;
870 struct extent_buffer
*left
= NULL
;
871 struct extent_buffer
*parent
= NULL
;
875 int orig_slot
= path
->slots
[level
];
881 mid
= path
->nodes
[level
];
882 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
883 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
885 if (level
< BTRFS_MAX_LEVEL
- 1)
886 parent
= path
->nodes
[level
+ 1];
887 pslot
= path
->slots
[level
+ 1];
892 left
= read_node_slot(root
, parent
, pslot
- 1);
894 /* first, try to make some room in the middle buffer */
897 left_nr
= btrfs_header_nritems(left
);
898 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
901 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
906 wret
= push_node_left(trans
, root
,
913 struct btrfs_disk_key disk_key
;
914 orig_slot
+= left_nr
;
915 btrfs_node_key(mid
, &disk_key
, 0);
916 btrfs_set_node_key(parent
, &disk_key
, pslot
);
917 btrfs_mark_buffer_dirty(parent
);
918 if (btrfs_header_nritems(left
) > orig_slot
) {
919 path
->nodes
[level
] = left
;
920 path
->slots
[level
+ 1] -= 1;
921 path
->slots
[level
] = orig_slot
;
922 free_extent_buffer(mid
);
925 btrfs_header_nritems(left
);
926 path
->slots
[level
] = orig_slot
;
927 free_extent_buffer(left
);
931 free_extent_buffer(left
);
933 right
= read_node_slot(root
, parent
, pslot
+ 1);
936 * then try to empty the right most buffer into the middle
940 right_nr
= btrfs_header_nritems(right
);
941 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
944 ret
= btrfs_cow_block(trans
, root
, right
,
950 wret
= balance_node_right(trans
, root
,
957 struct btrfs_disk_key disk_key
;
959 btrfs_node_key(right
, &disk_key
, 0);
960 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
961 btrfs_mark_buffer_dirty(parent
);
963 if (btrfs_header_nritems(mid
) <= orig_slot
) {
964 path
->nodes
[level
] = right
;
965 path
->slots
[level
+ 1] += 1;
966 path
->slots
[level
] = orig_slot
-
967 btrfs_header_nritems(mid
);
968 free_extent_buffer(mid
);
970 free_extent_buffer(right
);
974 free_extent_buffer(right
);
980 * readahead one full node of leaves
982 static void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
983 int level
, int slot
, u64 objectid
)
985 struct extent_buffer
*node
;
986 struct btrfs_disk_key disk_key
;
992 int direction
= path
->reada
;
993 struct extent_buffer
*eb
;
1001 if (!path
->nodes
[level
])
1004 node
= path
->nodes
[level
];
1005 search
= btrfs_node_blockptr(node
, slot
);
1006 blocksize
= btrfs_level_size(root
, level
- 1);
1007 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1009 free_extent_buffer(eb
);
1013 highest_read
= search
;
1014 lowest_read
= search
;
1016 nritems
= btrfs_header_nritems(node
);
1019 if (direction
< 0) {
1023 } else if (direction
> 0) {
1028 if (path
->reada
< 0 && objectid
) {
1029 btrfs_node_key(node
, &disk_key
, nr
);
1030 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1033 search
= btrfs_node_blockptr(node
, nr
);
1034 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1035 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1036 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1037 readahead_tree_block(root
, search
, blocksize
);
1041 if (path
->reada
< 2 && (nread
> (256 * 1024) || nscan
> 32))
1043 if(nread
> (1024 * 1024) || nscan
> 128)
1046 if (search
< lowest_read
)
1047 lowest_read
= search
;
1048 if (search
> highest_read
)
1049 highest_read
= search
;
1054 * look for key in the tree. path is filled in with nodes along the way
1055 * if key is found, we return zero and you can find the item in the leaf
1056 * level of the path (level 0)
1058 * If the key isn't found, the path points to the slot where it should
1059 * be inserted, and 1 is returned. If there are other errors during the
1060 * search a negative error number is returned.
1062 * if ins_len > 0, nodes and leaves will be split as we walk down the
1063 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1066 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1067 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1070 struct extent_buffer
*b
;
1076 int should_reada
= p
->reada
;
1077 u8 lowest_level
= 0;
1079 lowest_level
= p
->lowest_level
;
1080 WARN_ON(lowest_level
&& ins_len
);
1081 WARN_ON(p
->nodes
[0] != NULL
);
1083 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1087 extent_buffer_get(b
);
1089 level
= btrfs_header_level(b
);
1092 wret
= btrfs_cow_block(trans
, root
, b
,
1093 p
->nodes
[level
+ 1],
1094 p
->slots
[level
+ 1],
1097 free_extent_buffer(b
);
1101 BUG_ON(!cow
&& ins_len
);
1102 if (level
!= btrfs_header_level(b
))
1104 level
= btrfs_header_level(b
);
1105 p
->nodes
[level
] = b
;
1106 ret
= check_block(root
, p
, level
);
1109 ret
= bin_search(b
, key
, level
, &slot
);
1111 if (ret
&& slot
> 0)
1113 p
->slots
[level
] = slot
;
1114 if (ins_len
> 0 && btrfs_header_nritems(b
) >=
1115 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1116 int sret
= split_node(trans
, root
, p
, level
);
1120 b
= p
->nodes
[level
];
1121 slot
= p
->slots
[level
];
1122 } else if (ins_len
< 0) {
1123 int sret
= balance_level(trans
, root
, p
,
1127 b
= p
->nodes
[level
];
1129 btrfs_release_path(NULL
, p
);
1132 slot
= p
->slots
[level
];
1133 BUG_ON(btrfs_header_nritems(b
) == 1);
1135 /* this is only true while dropping a snapshot */
1136 if (level
== lowest_level
)
1138 bytenr
= btrfs_node_blockptr(b
, slot
);
1139 ptr_gen
= btrfs_node_ptr_generation(b
, slot
);
1141 reada_for_search(root
, p
, level
, slot
,
1143 b
= read_tree_block(root
, bytenr
,
1144 btrfs_level_size(root
, level
- 1));
1145 if (ptr_gen
!= btrfs_header_generation(b
)) {
1146 printk("block %llu bad gen wanted %llu "
1148 (unsigned long long)b
->start
,
1149 (unsigned long long)ptr_gen
,
1150 (unsigned long long)btrfs_header_generation(b
));
1153 p
->slots
[level
] = slot
;
1154 if (ins_len
> 0 && btrfs_leaf_free_space(root
, b
) <
1155 sizeof(struct btrfs_item
) + ins_len
) {
1156 int sret
= split_leaf(trans
, root
, key
,
1157 p
, ins_len
, ret
== 0);
1169 * adjust the pointers going up the tree, starting at level
1170 * making sure the right key of each node is points to 'key'.
1171 * This is used after shifting pointers to the left, so it stops
1172 * fixing up pointers when a given leaf/node is not in slot 0 of the
1175 * If this fails to write a tree block, it returns -1, but continues
1176 * fixing up the blocks in ram so the tree is consistent.
1178 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1179 struct btrfs_root
*root
, struct btrfs_path
*path
,
1180 struct btrfs_disk_key
*key
, int level
)
1184 struct extent_buffer
*t
;
1186 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1187 int tslot
= path
->slots
[i
];
1188 if (!path
->nodes
[i
])
1191 btrfs_set_node_key(t
, key
, tslot
);
1192 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1200 * try to push data from one node into the next node left in the
1203 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1204 * error, and > 0 if there was no room in the left hand block.
1206 static int push_node_left(struct btrfs_trans_handle
*trans
,
1207 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1208 struct extent_buffer
*src
, int empty
)
1215 src_nritems
= btrfs_header_nritems(src
);
1216 dst_nritems
= btrfs_header_nritems(dst
);
1217 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1218 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1219 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1221 if (!empty
&& src_nritems
<= 8)
1224 if (push_items
<= 0) {
1229 push_items
= min(src_nritems
, push_items
);
1230 if (push_items
< src_nritems
) {
1231 /* leave at least 8 pointers in the node if
1232 * we aren't going to empty it
1234 if (src_nritems
- push_items
< 8) {
1235 if (push_items
<= 8)
1241 push_items
= min(src_nritems
- 8, push_items
);
1243 copy_extent_buffer(dst
, src
,
1244 btrfs_node_key_ptr_offset(dst_nritems
),
1245 btrfs_node_key_ptr_offset(0),
1246 push_items
* sizeof(struct btrfs_key_ptr
));
1248 if (push_items
< src_nritems
) {
1249 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1250 btrfs_node_key_ptr_offset(push_items
),
1251 (src_nritems
- push_items
) *
1252 sizeof(struct btrfs_key_ptr
));
1254 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1255 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1256 btrfs_mark_buffer_dirty(src
);
1257 btrfs_mark_buffer_dirty(dst
);
1262 * try to push data from one node into the next node right in the
1265 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1266 * error, and > 0 if there was no room in the right hand block.
1268 * this will only push up to 1/2 the contents of the left node over
1270 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1271 struct btrfs_root
*root
,
1272 struct extent_buffer
*dst
,
1273 struct extent_buffer
*src
)
1281 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1282 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1284 src_nritems
= btrfs_header_nritems(src
);
1285 dst_nritems
= btrfs_header_nritems(dst
);
1286 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1287 if (push_items
<= 0) {
1291 if (src_nritems
< 4) {
1295 max_push
= src_nritems
/ 2 + 1;
1296 /* don't try to empty the node */
1297 if (max_push
>= src_nritems
) {
1301 if (max_push
< push_items
)
1302 push_items
= max_push
;
1304 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1305 btrfs_node_key_ptr_offset(0),
1307 sizeof(struct btrfs_key_ptr
));
1309 copy_extent_buffer(dst
, src
,
1310 btrfs_node_key_ptr_offset(0),
1311 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1312 push_items
* sizeof(struct btrfs_key_ptr
));
1314 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1315 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1317 btrfs_mark_buffer_dirty(src
);
1318 btrfs_mark_buffer_dirty(dst
);
1323 * helper function to insert a new root level in the tree.
1324 * A new node is allocated, and a single item is inserted to
1325 * point to the existing root
1327 * returns zero on success or < 0 on failure.
1329 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1330 struct btrfs_root
*root
,
1331 struct btrfs_path
*path
, int level
)
1335 struct extent_buffer
*lower
;
1336 struct extent_buffer
*c
;
1337 struct btrfs_disk_key lower_key
;
1339 BUG_ON(path
->nodes
[level
]);
1340 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1343 root_gen
= trans
->transid
;
1347 lower
= path
->nodes
[level
-1];
1349 btrfs_item_key(lower
, &lower_key
, 0);
1351 btrfs_node_key(lower
, &lower_key
, 0);
1353 c
= __btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1354 root
->root_key
.objectid
,
1355 root_gen
, lower_key
.objectid
, level
,
1356 root
->node
->start
, 0);
1359 memset_extent_buffer(c
, 0, 0, root
->nodesize
);
1360 btrfs_set_header_nritems(c
, 1);
1361 btrfs_set_header_level(c
, level
);
1362 btrfs_set_header_bytenr(c
, c
->start
);
1363 btrfs_set_header_generation(c
, trans
->transid
);
1364 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1366 write_extent_buffer(c
, root
->fs_info
->fsid
,
1367 (unsigned long)btrfs_header_fsid(c
),
1370 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1371 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
1374 btrfs_set_node_key(c
, &lower_key
, 0);
1375 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1376 lower_gen
= btrfs_header_generation(lower
);
1377 WARN_ON(lower_gen
== 0);
1379 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1381 btrfs_mark_buffer_dirty(c
);
1383 /* the super has an extra ref to root->node */
1384 free_extent_buffer(root
->node
);
1386 add_root_to_dirty_list(root
);
1387 extent_buffer_get(c
);
1388 path
->nodes
[level
] = c
;
1389 path
->slots
[level
] = 0;
1391 if (root
->ref_cows
&& lower_gen
!= trans
->transid
) {
1392 struct btrfs_path
*back_path
= btrfs_alloc_path();
1394 ret
= btrfs_insert_extent_backref(trans
,
1395 root
->fs_info
->extent_root
,
1397 root
->root_key
.objectid
,
1398 trans
->transid
, 0, 0);
1400 btrfs_free_path(back_path
);
1406 * worker function to insert a single pointer in a node.
1407 * the node should have enough room for the pointer already
1409 * slot and level indicate where you want the key to go, and
1410 * blocknr is the block the key points to.
1412 * returns zero on success and < 0 on any error
1414 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1415 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1416 *key
, u64 bytenr
, int slot
, int level
)
1418 struct extent_buffer
*lower
;
1421 BUG_ON(!path
->nodes
[level
]);
1422 lower
= path
->nodes
[level
];
1423 nritems
= btrfs_header_nritems(lower
);
1426 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
1428 if (slot
!= nritems
) {
1429 memmove_extent_buffer(lower
,
1430 btrfs_node_key_ptr_offset(slot
+ 1),
1431 btrfs_node_key_ptr_offset(slot
),
1432 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1434 btrfs_set_node_key(lower
, key
, slot
);
1435 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1436 WARN_ON(trans
->transid
== 0);
1437 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1438 btrfs_set_header_nritems(lower
, nritems
+ 1);
1439 btrfs_mark_buffer_dirty(lower
);
1444 * split the node at the specified level in path in two.
1445 * The path is corrected to point to the appropriate node after the split
1447 * Before splitting this tries to make some room in the node by pushing
1448 * left and right, if either one works, it returns right away.
1450 * returns 0 on success and < 0 on failure
1452 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1453 *root
, struct btrfs_path
*path
, int level
)
1456 struct extent_buffer
*c
;
1457 struct extent_buffer
*split
;
1458 struct btrfs_disk_key disk_key
;
1464 c
= path
->nodes
[level
];
1465 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1466 if (c
== root
->node
) {
1467 /* trying to split the root, lets make a new one */
1468 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1472 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1473 c
= path
->nodes
[level
];
1474 if (!ret
&& btrfs_header_nritems(c
) <
1475 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
1481 c_nritems
= btrfs_header_nritems(c
);
1483 root_gen
= trans
->transid
;
1487 btrfs_node_key(c
, &disk_key
, 0);
1488 split
= __btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1489 root
->root_key
.objectid
,
1491 btrfs_disk_key_objectid(&disk_key
),
1492 level
, c
->start
, 0);
1494 return PTR_ERR(split
);
1496 btrfs_set_header_flags(split
, btrfs_header_flags(c
));
1497 btrfs_set_header_level(split
, btrfs_header_level(c
));
1498 btrfs_set_header_bytenr(split
, split
->start
);
1499 btrfs_set_header_generation(split
, trans
->transid
);
1500 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1501 btrfs_set_header_flags(split
, 0);
1502 write_extent_buffer(split
, root
->fs_info
->fsid
,
1503 (unsigned long)btrfs_header_fsid(split
),
1505 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1506 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
1509 mid
= (c_nritems
+ 1) / 2;
1511 copy_extent_buffer(split
, c
,
1512 btrfs_node_key_ptr_offset(0),
1513 btrfs_node_key_ptr_offset(mid
),
1514 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1515 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1516 btrfs_set_header_nritems(c
, mid
);
1519 btrfs_mark_buffer_dirty(c
);
1520 btrfs_mark_buffer_dirty(split
);
1522 btrfs_node_key(split
, &disk_key
, 0);
1523 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1524 path
->slots
[level
+ 1] + 1,
1529 if (path
->slots
[level
] >= mid
) {
1530 path
->slots
[level
] -= mid
;
1531 free_extent_buffer(c
);
1532 path
->nodes
[level
] = split
;
1533 path
->slots
[level
+ 1] += 1;
1535 free_extent_buffer(split
);
1541 * how many bytes are required to store the items in a leaf. start
1542 * and nr indicate which items in the leaf to check. This totals up the
1543 * space used both by the item structs and the item data
1545 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1548 int nritems
= btrfs_header_nritems(l
);
1549 int end
= min(nritems
, start
+ nr
) - 1;
1553 data_len
= btrfs_item_end_nr(l
, start
);
1554 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1555 data_len
+= sizeof(struct btrfs_item
) * nr
;
1556 WARN_ON(data_len
< 0);
1561 * The space between the end of the leaf items and
1562 * the start of the leaf data. IOW, how much room
1563 * the leaf has left for both items and data
1565 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1567 int nritems
= btrfs_header_nritems(leaf
);
1569 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
1571 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1572 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
1573 leaf_space_used(leaf
, 0, nritems
), nritems
);
1579 * push some data in the path leaf to the right, trying to free up at
1580 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1582 * returns 1 if the push failed because the other node didn't have enough
1583 * room, 0 if everything worked out and < 0 if there were major errors.
1585 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1586 *root
, struct btrfs_path
*path
, int data_size
,
1589 struct extent_buffer
*left
= path
->nodes
[0];
1590 struct extent_buffer
*right
;
1591 struct extent_buffer
*upper
;
1592 struct btrfs_disk_key disk_key
;
1598 struct btrfs_item
*item
;
1606 slot
= path
->slots
[1];
1607 if (!path
->nodes
[1]) {
1610 upper
= path
->nodes
[1];
1611 if (slot
>= btrfs_header_nritems(upper
) - 1)
1614 right
= read_tree_block(root
, btrfs_node_blockptr(upper
, slot
+ 1),
1616 free_space
= btrfs_leaf_free_space(root
, right
);
1617 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1618 free_extent_buffer(right
);
1622 /* cow and double check */
1623 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1626 free_extent_buffer(right
);
1629 free_space
= btrfs_leaf_free_space(root
, right
);
1630 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1631 free_extent_buffer(right
);
1635 left_nritems
= btrfs_header_nritems(left
);
1636 if (left_nritems
== 0) {
1637 free_extent_buffer(right
);
1646 i
= left_nritems
- 1;
1648 item
= btrfs_item_nr(left
, i
);
1650 if (path
->slots
[0] == i
)
1651 push_space
+= data_size
+ sizeof(*item
);
1653 this_item_size
= btrfs_item_size(left
, item
);
1654 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1657 push_space
+= this_item_size
+ sizeof(*item
);
1663 if (push_items
== 0) {
1664 free_extent_buffer(right
);
1668 if (!empty
&& push_items
== left_nritems
)
1671 /* push left to right */
1672 right_nritems
= btrfs_header_nritems(right
);
1674 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1675 push_space
-= leaf_data_end(root
, left
);
1677 /* make room in the right data area */
1678 data_end
= leaf_data_end(root
, right
);
1679 memmove_extent_buffer(right
,
1680 btrfs_leaf_data(right
) + data_end
- push_space
,
1681 btrfs_leaf_data(right
) + data_end
,
1682 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
1684 /* copy from the left data area */
1685 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1686 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1687 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1690 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1691 btrfs_item_nr_offset(0),
1692 right_nritems
* sizeof(struct btrfs_item
));
1694 /* copy the items from left to right */
1695 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1696 btrfs_item_nr_offset(left_nritems
- push_items
),
1697 push_items
* sizeof(struct btrfs_item
));
1699 /* update the item pointers */
1700 right_nritems
+= push_items
;
1701 btrfs_set_header_nritems(right
, right_nritems
);
1702 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1703 for (i
= 0; i
< right_nritems
; i
++) {
1704 item
= btrfs_item_nr(right
, i
);
1705 push_space
-= btrfs_item_size(right
, item
);
1706 btrfs_set_item_offset(right
, item
, push_space
);
1709 left_nritems
-= push_items
;
1710 btrfs_set_header_nritems(left
, left_nritems
);
1713 btrfs_mark_buffer_dirty(left
);
1714 btrfs_mark_buffer_dirty(right
);
1716 btrfs_item_key(right
, &disk_key
, 0);
1717 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1718 btrfs_mark_buffer_dirty(upper
);
1720 /* then fixup the leaf pointer in the path */
1721 if (path
->slots
[0] >= left_nritems
) {
1722 path
->slots
[0] -= left_nritems
;
1723 free_extent_buffer(path
->nodes
[0]);
1724 path
->nodes
[0] = right
;
1725 path
->slots
[1] += 1;
1727 free_extent_buffer(right
);
1732 * push some data in the path leaf to the left, trying to free up at
1733 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1735 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1736 *root
, struct btrfs_path
*path
, int data_size
,
1739 struct btrfs_disk_key disk_key
;
1740 struct extent_buffer
*right
= path
->nodes
[0];
1741 struct extent_buffer
*left
;
1747 struct btrfs_item
*item
;
1748 u32 old_left_nritems
;
1754 u32 old_left_item_size
;
1756 slot
= path
->slots
[1];
1759 if (!path
->nodes
[1])
1762 right_nritems
= btrfs_header_nritems(right
);
1763 if (right_nritems
== 0) {
1767 left
= read_tree_block(root
, btrfs_node_blockptr(path
->nodes
[1],
1768 slot
- 1), root
->leafsize
);
1769 free_space
= btrfs_leaf_free_space(root
, left
);
1770 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1771 free_extent_buffer(left
);
1775 /* cow and double check */
1776 ret
= btrfs_cow_block(trans
, root
, left
,
1777 path
->nodes
[1], slot
- 1, &left
);
1779 /* we hit -ENOSPC, but it isn't fatal here */
1780 free_extent_buffer(left
);
1784 free_space
= btrfs_leaf_free_space(root
, left
);
1785 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1786 free_extent_buffer(left
);
1793 nr
= right_nritems
- 1;
1795 for (i
= 0; i
< nr
; i
++) {
1796 item
= btrfs_item_nr(right
, i
);
1798 if (path
->slots
[0] == i
)
1799 push_space
+= data_size
+ sizeof(*item
);
1801 this_item_size
= btrfs_item_size(right
, item
);
1802 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1806 push_space
+= this_item_size
+ sizeof(*item
);
1809 if (push_items
== 0) {
1810 free_extent_buffer(left
);
1813 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1816 /* push data from right to left */
1817 copy_extent_buffer(left
, right
,
1818 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1819 btrfs_item_nr_offset(0),
1820 push_items
* sizeof(struct btrfs_item
));
1822 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1823 btrfs_item_offset_nr(right
, push_items
-1);
1825 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1826 leaf_data_end(root
, left
) - push_space
,
1827 btrfs_leaf_data(right
) +
1828 btrfs_item_offset_nr(right
, push_items
- 1),
1830 old_left_nritems
= btrfs_header_nritems(left
);
1831 BUG_ON(old_left_nritems
< 0);
1833 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1834 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1837 item
= btrfs_item_nr(left
, i
);
1838 ioff
= btrfs_item_offset(left
, item
);
1839 btrfs_set_item_offset(left
, item
,
1840 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
1842 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1844 /* fixup right node */
1845 if (push_items
> right_nritems
) {
1846 printk("push items %d nr %u\n", push_items
, right_nritems
);
1850 if (push_items
< right_nritems
) {
1851 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1852 leaf_data_end(root
, right
);
1853 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1854 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1855 btrfs_leaf_data(right
) +
1856 leaf_data_end(root
, right
), push_space
);
1858 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1859 btrfs_item_nr_offset(push_items
),
1860 (btrfs_header_nritems(right
) - push_items
) *
1861 sizeof(struct btrfs_item
));
1863 right_nritems
-= push_items
;
1864 btrfs_set_header_nritems(right
, right_nritems
);
1865 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1866 for (i
= 0; i
< right_nritems
; i
++) {
1867 item
= btrfs_item_nr(right
, i
);
1868 push_space
= push_space
- btrfs_item_size(right
, item
);
1869 btrfs_set_item_offset(right
, item
, push_space
);
1872 btrfs_mark_buffer_dirty(left
);
1874 btrfs_mark_buffer_dirty(right
);
1876 btrfs_item_key(right
, &disk_key
, 0);
1877 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1881 /* then fixup the leaf pointer in the path */
1882 if (path
->slots
[0] < push_items
) {
1883 path
->slots
[0] += old_left_nritems
;
1884 free_extent_buffer(path
->nodes
[0]);
1885 path
->nodes
[0] = left
;
1886 path
->slots
[1] -= 1;
1888 free_extent_buffer(left
);
1889 path
->slots
[0] -= push_items
;
1891 BUG_ON(path
->slots
[0] < 0);
1896 * split the path's leaf in two, making sure there is at least data_size
1897 * available for the resulting leaf level of the path.
1899 * returns 0 if all went well and < 0 on failure.
1901 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
1902 *root
, struct btrfs_key
*ins_key
,
1903 struct btrfs_path
*path
, int data_size
, int extend
)
1906 struct extent_buffer
*l
;
1910 struct extent_buffer
*right
;
1911 int space_needed
= data_size
+ sizeof(struct btrfs_item
);
1918 int num_doubles
= 0;
1919 struct btrfs_disk_key disk_key
;
1922 space_needed
= data_size
;
1925 root_gen
= trans
->transid
;
1929 /* first try to make some room by pushing left and right */
1930 if (ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
1931 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
1936 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
1942 /* did the pushes work? */
1943 if (btrfs_leaf_free_space(root
, l
) >= space_needed
)
1947 if (!path
->nodes
[1]) {
1948 ret
= insert_new_root(trans
, root
, path
, 1);
1955 slot
= path
->slots
[0];
1956 nritems
= btrfs_header_nritems(l
);
1957 mid
= (nritems
+ 1)/ 2;
1959 btrfs_item_key(l
, &disk_key
, 0);
1961 right
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
1962 root
->root_key
.objectid
,
1963 root_gen
, disk_key
.objectid
, 0,
1965 if (IS_ERR(right
)) {
1967 return PTR_ERR(right
);
1970 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
1971 btrfs_set_header_bytenr(right
, right
->start
);
1972 btrfs_set_header_generation(right
, trans
->transid
);
1973 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
1974 btrfs_set_header_level(right
, 0);
1975 write_extent_buffer(right
, root
->fs_info
->fsid
,
1976 (unsigned long)btrfs_header_fsid(right
),
1979 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
1980 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
1984 leaf_space_used(l
, mid
, nritems
- mid
) + space_needed
>
1985 BTRFS_LEAF_DATA_SIZE(root
)) {
1986 if (slot
>= nritems
) {
1987 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
1988 btrfs_set_header_nritems(right
, 0);
1989 wret
= insert_ptr(trans
, root
, path
,
1990 &disk_key
, right
->start
,
1991 path
->slots
[1] + 1, 1);
1994 free_extent_buffer(path
->nodes
[0]);
1995 path
->nodes
[0] = right
;
1997 path
->slots
[1] += 1;
2001 if (mid
!= nritems
&&
2002 leaf_space_used(l
, mid
, nritems
- mid
) +
2003 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
2008 if (leaf_space_used(l
, 0, mid
+ 1) + space_needed
>
2009 BTRFS_LEAF_DATA_SIZE(root
)) {
2010 if (!extend
&& slot
== 0) {
2011 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2012 btrfs_set_header_nritems(right
, 0);
2013 wret
= insert_ptr(trans
, root
, path
,
2019 free_extent_buffer(path
->nodes
[0]);
2020 path
->nodes
[0] = right
;
2022 if (path
->slots
[1] == 0) {
2023 wret
= fixup_low_keys(trans
, root
,
2024 path
, &disk_key
, 1);
2029 } else if (extend
&& slot
== 0) {
2033 if (mid
!= nritems
&&
2034 leaf_space_used(l
, mid
, nritems
- mid
) +
2035 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
2041 nritems
= nritems
- mid
;
2042 btrfs_set_header_nritems(right
, nritems
);
2043 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2045 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2046 btrfs_item_nr_offset(mid
),
2047 nritems
* sizeof(struct btrfs_item
));
2049 copy_extent_buffer(right
, l
,
2050 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2051 data_copy_size
, btrfs_leaf_data(l
) +
2052 leaf_data_end(root
, l
), data_copy_size
);
2054 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2055 btrfs_item_end_nr(l
, mid
);
2057 for (i
= 0; i
< nritems
; i
++) {
2058 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2059 u32 ioff
= btrfs_item_offset(right
, item
);
2060 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2063 btrfs_set_header_nritems(l
, mid
);
2065 btrfs_item_key(right
, &disk_key
, 0);
2066 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2067 path
->slots
[1] + 1, 1);
2071 btrfs_mark_buffer_dirty(right
);
2072 btrfs_mark_buffer_dirty(l
);
2073 BUG_ON(path
->slots
[0] != slot
);
2076 free_extent_buffer(path
->nodes
[0]);
2077 path
->nodes
[0] = right
;
2078 path
->slots
[0] -= mid
;
2079 path
->slots
[1] += 1;
2081 free_extent_buffer(right
);
2083 BUG_ON(path
->slots
[0] < 0);
2086 BUG_ON(num_doubles
!= 0);
2093 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
2094 struct btrfs_root
*root
,
2095 struct btrfs_path
*path
,
2096 u32 new_size
, int from_end
)
2101 struct extent_buffer
*leaf
;
2102 struct btrfs_item
*item
;
2104 unsigned int data_end
;
2105 unsigned int old_data_start
;
2106 unsigned int old_size
;
2107 unsigned int size_diff
;
2110 slot_orig
= path
->slots
[0];
2111 leaf
= path
->nodes
[0];
2112 slot
= path
->slots
[0];
2114 old_size
= btrfs_item_size_nr(leaf
, slot
);
2115 if (old_size
== new_size
)
2118 nritems
= btrfs_header_nritems(leaf
);
2119 data_end
= leaf_data_end(root
, leaf
);
2121 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2123 size_diff
= old_size
- new_size
;
2126 BUG_ON(slot
>= nritems
);
2129 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2131 /* first correct the data pointers */
2132 for (i
= slot
; i
< nritems
; i
++) {
2134 item
= btrfs_item_nr(leaf
, i
);
2135 ioff
= btrfs_item_offset(leaf
, item
);
2136 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2139 /* shift the data */
2141 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2142 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2143 data_end
, old_data_start
+ new_size
- data_end
);
2145 struct btrfs_disk_key disk_key
;
2148 btrfs_item_key(leaf
, &disk_key
, slot
);
2150 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2152 struct btrfs_file_extent_item
*fi
;
2154 fi
= btrfs_item_ptr(leaf
, slot
,
2155 struct btrfs_file_extent_item
);
2156 fi
= (struct btrfs_file_extent_item
*)(
2157 (unsigned long)fi
- size_diff
);
2159 if (btrfs_file_extent_type(leaf
, fi
) ==
2160 BTRFS_FILE_EXTENT_INLINE
) {
2161 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2162 memmove_extent_buffer(leaf
, ptr
,
2164 offsetof(struct btrfs_file_extent_item
,
2169 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2170 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2171 data_end
, old_data_start
- data_end
);
2173 offset
= btrfs_disk_key_offset(&disk_key
);
2174 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2175 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2177 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2180 item
= btrfs_item_nr(leaf
, slot
);
2181 btrfs_set_item_size(leaf
, item
, new_size
);
2182 btrfs_mark_buffer_dirty(leaf
);
2185 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2186 btrfs_print_leaf(root
, leaf
);
2192 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
2193 struct btrfs_root
*root
, struct btrfs_path
*path
,
2199 struct extent_buffer
*leaf
;
2200 struct btrfs_item
*item
;
2202 unsigned int data_end
;
2203 unsigned int old_data
;
2204 unsigned int old_size
;
2207 slot_orig
= path
->slots
[0];
2208 leaf
= path
->nodes
[0];
2210 nritems
= btrfs_header_nritems(leaf
);
2211 data_end
= leaf_data_end(root
, leaf
);
2213 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2214 btrfs_print_leaf(root
, leaf
);
2217 slot
= path
->slots
[0];
2218 old_data
= btrfs_item_end_nr(leaf
, slot
);
2221 if (slot
>= nritems
) {
2222 btrfs_print_leaf(root
, leaf
);
2223 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2228 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2230 /* first correct the data pointers */
2231 for (i
= slot
; i
< nritems
; i
++) {
2233 item
= btrfs_item_nr(leaf
, i
);
2234 ioff
= btrfs_item_offset(leaf
, item
);
2235 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2238 /* shift the data */
2239 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2240 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2241 data_end
, old_data
- data_end
);
2243 data_end
= old_data
;
2244 old_size
= btrfs_item_size_nr(leaf
, slot
);
2245 item
= btrfs_item_nr(leaf
, slot
);
2246 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2247 btrfs_mark_buffer_dirty(leaf
);
2250 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2251 btrfs_print_leaf(root
, leaf
);
2258 * Given a key and some data, insert an item into the tree.
2259 * This does all the path init required, making room in the tree if needed.
2261 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2262 struct btrfs_root
*root
,
2263 struct btrfs_path
*path
,
2264 struct btrfs_key
*cpu_key
, u32
*data_size
,
2267 struct extent_buffer
*leaf
;
2268 struct btrfs_item
*item
;
2276 unsigned int data_end
;
2277 struct btrfs_disk_key disk_key
;
2279 for (i
= 0; i
< nr
; i
++) {
2280 total_data
+= data_size
[i
];
2283 /* create a root if there isn't one */
2287 total_size
= total_data
+ (nr
- 1) * sizeof(struct btrfs_item
);
2288 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2295 slot_orig
= path
->slots
[0];
2296 leaf
= path
->nodes
[0];
2298 nritems
= btrfs_header_nritems(leaf
);
2299 data_end
= leaf_data_end(root
, leaf
);
2301 if (btrfs_leaf_free_space(root
, leaf
) <
2302 sizeof(struct btrfs_item
) + total_size
) {
2303 btrfs_print_leaf(root
, leaf
);
2304 printk("not enough freespace need %u have %d\n",
2305 total_size
, btrfs_leaf_free_space(root
, leaf
));
2309 slot
= path
->slots
[0];
2312 if (slot
!= nritems
) {
2314 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2316 if (old_data
< data_end
) {
2317 btrfs_print_leaf(root
, leaf
);
2318 printk("slot %d old_data %d data_end %d\n",
2319 slot
, old_data
, data_end
);
2323 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2325 /* first correct the data pointers */
2326 for (i
= slot
; i
< nritems
; i
++) {
2329 item
= btrfs_item_nr(leaf
, i
);
2330 ioff
= btrfs_item_offset(leaf
, item
);
2331 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2334 /* shift the items */
2335 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2336 btrfs_item_nr_offset(slot
),
2337 (nritems
- slot
) * sizeof(struct btrfs_item
));
2339 /* shift the data */
2340 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2341 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2342 data_end
, old_data
- data_end
);
2343 data_end
= old_data
;
2346 /* setup the item for the new data */
2347 for (i
= 0; i
< nr
; i
++) {
2348 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2349 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2350 item
= btrfs_item_nr(leaf
, slot
+ i
);
2351 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2352 data_end
-= data_size
[i
];
2353 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2355 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2356 btrfs_mark_buffer_dirty(leaf
);
2360 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2361 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2364 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2365 btrfs_print_leaf(root
, leaf
);
2374 * Given a key and some data, insert an item into the tree.
2375 * This does all the path init required, making room in the tree if needed.
2377 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2378 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2382 struct btrfs_path
*path
;
2383 struct extent_buffer
*leaf
;
2386 path
= btrfs_alloc_path();
2388 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2390 leaf
= path
->nodes
[0];
2391 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2392 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2393 btrfs_mark_buffer_dirty(leaf
);
2395 btrfs_free_path(path
);
2400 * delete the pointer from a given node.
2402 * If the delete empties a node, the node is removed from the tree,
2403 * continuing all the way the root if required. The root is converted into
2404 * a leaf if all the nodes are emptied.
2406 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2407 struct btrfs_path
*path
, int level
, int slot
)
2409 struct extent_buffer
*parent
= path
->nodes
[level
];
2414 nritems
= btrfs_header_nritems(parent
);
2415 if (slot
!= nritems
-1) {
2416 memmove_extent_buffer(parent
,
2417 btrfs_node_key_ptr_offset(slot
),
2418 btrfs_node_key_ptr_offset(slot
+ 1),
2419 sizeof(struct btrfs_key_ptr
) *
2420 (nritems
- slot
- 1));
2423 btrfs_set_header_nritems(parent
, nritems
);
2424 if (nritems
== 0 && parent
== root
->node
) {
2425 BUG_ON(btrfs_header_level(root
->node
) != 1);
2426 /* just turn the root into a leaf and break */
2427 btrfs_set_header_level(root
->node
, 0);
2428 } else if (slot
== 0) {
2429 struct btrfs_disk_key disk_key
;
2431 btrfs_node_key(parent
, &disk_key
, 0);
2432 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
2436 btrfs_mark_buffer_dirty(parent
);
2441 * delete the item at the leaf level in path. If that empties
2442 * the leaf, remove it from the tree
2444 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2445 struct btrfs_path
*path
, int slot
, int nr
)
2447 struct extent_buffer
*leaf
;
2448 struct btrfs_item
*item
;
2456 leaf
= path
->nodes
[0];
2457 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2459 for (i
= 0; i
< nr
; i
++)
2460 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2462 nritems
= btrfs_header_nritems(leaf
);
2464 if (slot
+ nr
!= nritems
) {
2466 int data_end
= leaf_data_end(root
, leaf
);
2468 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2470 btrfs_leaf_data(leaf
) + data_end
,
2471 last_off
- data_end
);
2473 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2476 item
= btrfs_item_nr(leaf
, i
);
2477 ioff
= btrfs_item_offset(leaf
, item
);
2478 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2481 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2482 btrfs_item_nr_offset(slot
+ nr
),
2483 sizeof(struct btrfs_item
) *
2484 (nritems
- slot
- nr
));
2486 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2489 /* delete the leaf if we've emptied it */
2491 if (leaf
== root
->node
) {
2492 btrfs_set_header_level(leaf
, 0);
2494 u64 root_gen
= btrfs_header_generation(path
->nodes
[1]);
2495 clean_tree_block(trans
, root
, leaf
);
2496 wait_on_tree_block_writeback(root
, leaf
);
2497 wret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
2500 wret
= btrfs_free_extent(trans
, root
,
2501 leaf
->start
, leaf
->len
,
2502 btrfs_header_owner(path
->nodes
[1]),
2508 int used
= leaf_space_used(leaf
, 0, nritems
);
2510 struct btrfs_disk_key disk_key
;
2512 btrfs_item_key(leaf
, &disk_key
, 0);
2513 wret
= fixup_low_keys(trans
, root
, path
,
2519 /* delete the leaf if it is mostly empty */
2520 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 4) {
2521 /* push_leaf_left fixes the path.
2522 * make sure the path still points to our leaf
2523 * for possible call to del_ptr below
2525 slot
= path
->slots
[1];
2526 extent_buffer_get(leaf
);
2528 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2529 if (wret
< 0 && wret
!= -ENOSPC
)
2532 if (path
->nodes
[0] == leaf
&&
2533 btrfs_header_nritems(leaf
)) {
2534 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2535 if (wret
< 0 && wret
!= -ENOSPC
)
2539 if (btrfs_header_nritems(leaf
) == 0) {
2541 u64 bytenr
= leaf
->start
;
2542 u32 blocksize
= leaf
->len
;
2544 root_gen
= btrfs_header_generation(
2547 clean_tree_block(trans
, root
, leaf
);
2548 wait_on_tree_block_writeback(root
, leaf
);
2550 wret
= del_ptr(trans
, root
, path
, 1, slot
);
2554 free_extent_buffer(leaf
);
2555 wret
= btrfs_free_extent(trans
, root
, bytenr
,
2557 btrfs_header_owner(path
->nodes
[1]),
2562 btrfs_mark_buffer_dirty(leaf
);
2563 free_extent_buffer(leaf
);
2566 btrfs_mark_buffer_dirty(leaf
);
2573 * walk up the tree as far as required to find the previous leaf.
2574 * returns 0 if it found something or 1 if there are no lesser leaves.
2575 * returns < 0 on io errors.
2577 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2582 struct extent_buffer
*c
;
2583 struct extent_buffer
*next
= NULL
;
2585 while(level
< BTRFS_MAX_LEVEL
) {
2586 if (!path
->nodes
[level
])
2589 slot
= path
->slots
[level
];
2590 c
= path
->nodes
[level
];
2593 if (level
== BTRFS_MAX_LEVEL
)
2599 bytenr
= btrfs_node_blockptr(c
, slot
);
2601 free_extent_buffer(next
);
2603 next
= read_tree_block(root
, bytenr
,
2604 btrfs_level_size(root
, level
- 1));
2607 path
->slots
[level
] = slot
;
2610 c
= path
->nodes
[level
];
2611 free_extent_buffer(c
);
2612 slot
= btrfs_header_nritems(next
);
2615 path
->nodes
[level
] = next
;
2616 path
->slots
[level
] = slot
;
2619 next
= read_tree_block(root
, btrfs_node_blockptr(next
, slot
),
2620 btrfs_level_size(root
, level
- 1));
2626 * walk up the tree as far as required to find the next leaf.
2627 * returns 0 if it found something or 1 if there are no greater leaves.
2628 * returns < 0 on io errors.
2630 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2635 struct extent_buffer
*c
;
2636 struct extent_buffer
*next
= NULL
;
2638 while(level
< BTRFS_MAX_LEVEL
) {
2639 if (!path
->nodes
[level
])
2642 slot
= path
->slots
[level
] + 1;
2643 c
= path
->nodes
[level
];
2644 if (slot
>= btrfs_header_nritems(c
)) {
2646 if (level
== BTRFS_MAX_LEVEL
)
2651 bytenr
= btrfs_node_blockptr(c
, slot
);
2653 free_extent_buffer(next
);
2656 reada_for_search(root
, path
, level
, slot
, 0);
2658 next
= read_tree_block(root
, bytenr
,
2659 btrfs_level_size(root
, level
-1));
2662 path
->slots
[level
] = slot
;
2665 c
= path
->nodes
[level
];
2666 free_extent_buffer(c
);
2667 path
->nodes
[level
] = next
;
2668 path
->slots
[level
] = 0;
2672 reada_for_search(root
, path
, level
, 0, 0);
2673 next
= read_tree_block(root
, btrfs_node_blockptr(next
, 0),
2674 btrfs_level_size(root
, level
- 1));
2679 int btrfs_previous_item(struct btrfs_root
*root
,
2680 struct btrfs_path
*path
, u64 min_objectid
,
2683 struct btrfs_key found_key
;
2684 struct extent_buffer
*leaf
;
2688 if (path
->slots
[0] == 0) {
2689 ret
= btrfs_prev_leaf(root
, path
);
2695 leaf
= path
->nodes
[0];
2696 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2697 if (found_key
.type
== type
)