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
= kmem_cache_alloc(btrfs_path_cachep
, GFP_NOFS
);
50 btrfs_init_path(path
);
56 void btrfs_free_path(struct btrfs_path
*p
)
58 btrfs_release_path(NULL
, p
);
59 kmem_cache_free(btrfs_path_cachep
, 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
)
227 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
228 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
229 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
);
238 header_trans
= btrfs_header_generation(buf
);
239 if (header_trans
== trans
->transid
) {
244 search_start
= buf
->start
& ~((u64
)BTRFS_BLOCK_GROUP_SIZE
- 1);
245 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
246 parent_slot
, cow_ret
, search_start
, 0);
250 static int close_blocks(u64 blocknr
, u64 other
, u32 blocksize
)
252 if (blocknr
< other
&& other
- (blocknr
+ blocksize
) < 32768)
254 if (blocknr
> other
&& blocknr
- (other
+ blocksize
) < 32768)
260 * compare two keys in a memcmp fashion
262 static int 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
)
284 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
285 struct btrfs_root
*root
, struct extent_buffer
*parent
,
286 int start_slot
, int cache_only
, u64
*last_ret
,
287 struct btrfs_key
*progress
)
289 struct extent_buffer
*cur
;
290 struct extent_buffer
*tmp
;
292 u64 search_start
= *last_ret
;
302 int progress_passed
= 0;
303 struct btrfs_disk_key disk_key
;
305 parent_level
= btrfs_header_level(parent
);
306 if (cache_only
&& parent_level
!= 1)
309 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
310 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
311 root
->fs_info
->running_transaction
->transid
);
314 if (trans
->transid
!= root
->fs_info
->generation
) {
315 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
316 root
->fs_info
->generation
);
320 parent_nritems
= btrfs_header_nritems(parent
);
321 blocksize
= btrfs_level_size(root
, parent_level
- 1);
322 end_slot
= parent_nritems
;
324 if (parent_nritems
== 1)
327 for (i
= start_slot
; i
< end_slot
; i
++) {
330 if (!parent
->map_token
) {
331 map_extent_buffer(parent
,
332 btrfs_node_key_ptr_offset(i
),
333 sizeof(struct btrfs_key_ptr
),
334 &parent
->map_token
, &parent
->kaddr
,
335 &parent
->map_start
, &parent
->map_len
,
338 btrfs_node_key(parent
, &disk_key
, i
);
339 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
343 blocknr
= btrfs_node_blockptr(parent
, i
);
345 last_block
= blocknr
;
348 other
= btrfs_node_blockptr(parent
, i
- 1);
349 close
= close_blocks(blocknr
, other
, blocksize
);
351 if (close
&& i
< end_slot
- 2) {
352 other
= btrfs_node_blockptr(parent
, i
+ 1);
353 close
= close_blocks(blocknr
, other
, blocksize
);
356 last_block
= blocknr
;
359 if (parent
->map_token
) {
360 unmap_extent_buffer(parent
, parent
->map_token
,
362 parent
->map_token
= NULL
;
365 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
367 uptodate
= btrfs_buffer_uptodate(cur
);
370 if (!cur
|| !uptodate
) {
372 free_extent_buffer(cur
);
376 cur
= read_tree_block(root
, blocknr
,
378 } else if (!uptodate
) {
379 btrfs_read_buffer(cur
);
382 if (search_start
== 0)
383 search_start
= last_block
;
385 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
388 (end_slot
- i
) * blocksize
));
390 free_extent_buffer(cur
);
393 search_start
= tmp
->start
;
394 last_block
= tmp
->start
;
395 *last_ret
= search_start
;
396 if (parent_level
== 1)
397 btrfs_clear_buffer_defrag(tmp
);
398 free_extent_buffer(tmp
);
400 if (parent
->map_token
) {
401 unmap_extent_buffer(parent
, parent
->map_token
,
403 parent
->map_token
= NULL
;
409 * The leaf data grows from end-to-front in the node.
410 * this returns the address of the start of the last item,
411 * which is the stop of the leaf data stack
413 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
414 struct extent_buffer
*leaf
)
416 u32 nr
= btrfs_header_nritems(leaf
);
418 return BTRFS_LEAF_DATA_SIZE(root
);
419 return btrfs_item_offset_nr(leaf
, nr
- 1);
422 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
425 struct extent_buffer
*parent
= NULL
;
426 struct extent_buffer
*node
= path
->nodes
[level
];
427 struct btrfs_disk_key parent_key
;
428 struct btrfs_disk_key node_key
;
431 struct btrfs_key cpukey
;
432 u32 nritems
= btrfs_header_nritems(node
);
434 if (path
->nodes
[level
+ 1])
435 parent
= path
->nodes
[level
+ 1];
437 slot
= path
->slots
[level
];
438 BUG_ON(nritems
== 0);
440 parent_slot
= path
->slots
[level
+ 1];
441 btrfs_node_key(parent
, &parent_key
, parent_slot
);
442 btrfs_node_key(node
, &node_key
, 0);
443 BUG_ON(memcmp(&parent_key
, &node_key
,
444 sizeof(struct btrfs_disk_key
)));
445 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
446 btrfs_header_bytenr(node
));
448 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
450 btrfs_node_key_to_cpu(node
, &cpukey
, slot
- 1);
451 btrfs_node_key(node
, &node_key
, slot
);
452 BUG_ON(comp_keys(&node_key
, &cpukey
) <= 0);
454 if (slot
< nritems
- 1) {
455 btrfs_node_key_to_cpu(node
, &cpukey
, slot
+ 1);
456 btrfs_node_key(node
, &node_key
, slot
);
457 BUG_ON(comp_keys(&node_key
, &cpukey
) >= 0);
462 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
465 struct extent_buffer
*leaf
= path
->nodes
[level
];
466 struct extent_buffer
*parent
= NULL
;
468 struct btrfs_key cpukey
;
469 struct btrfs_disk_key parent_key
;
470 struct btrfs_disk_key leaf_key
;
471 int slot
= path
->slots
[0];
473 u32 nritems
= btrfs_header_nritems(leaf
);
475 if (path
->nodes
[level
+ 1])
476 parent
= path
->nodes
[level
+ 1];
482 parent_slot
= path
->slots
[level
+ 1];
483 btrfs_node_key(parent
, &parent_key
, parent_slot
);
484 btrfs_item_key(leaf
, &leaf_key
, 0);
486 BUG_ON(memcmp(&parent_key
, &leaf_key
,
487 sizeof(struct btrfs_disk_key
)));
488 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
489 btrfs_header_bytenr(leaf
));
492 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
493 btrfs_item_key_to_cpu(leaf
, &cpukey
, i
+ 1);
494 btrfs_item_key(leaf
, &leaf_key
, i
);
495 if (comp_keys(&leaf_key
, &cpukey
) >= 0) {
496 btrfs_print_leaf(root
, leaf
);
497 printk("slot %d offset bad key\n", i
);
500 if (btrfs_item_offset_nr(leaf
, i
) !=
501 btrfs_item_end_nr(leaf
, i
+ 1)) {
502 btrfs_print_leaf(root
, leaf
);
503 printk("slot %d offset bad\n", i
);
507 if (btrfs_item_offset_nr(leaf
, i
) +
508 btrfs_item_size_nr(leaf
, i
) !=
509 BTRFS_LEAF_DATA_SIZE(root
)) {
510 btrfs_print_leaf(root
, leaf
);
511 printk("slot %d first offset bad\n", i
);
517 if (btrfs_item_size_nr(leaf
, nritems
- 1) > 4096) {
518 btrfs_print_leaf(root
, leaf
);
519 printk("slot %d bad size \n", nritems
- 1);
524 if (slot
!= 0 && slot
< nritems
- 1) {
525 btrfs_item_key(leaf
, &leaf_key
, slot
);
526 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
- 1);
527 if (comp_keys(&leaf_key
, &cpukey
) <= 0) {
528 btrfs_print_leaf(root
, leaf
);
529 printk("slot %d offset bad key\n", slot
);
532 if (btrfs_item_offset_nr(leaf
, slot
- 1) !=
533 btrfs_item_end_nr(leaf
, slot
)) {
534 btrfs_print_leaf(root
, leaf
);
535 printk("slot %d offset bad\n", slot
);
539 if (slot
< nritems
- 1) {
540 btrfs_item_key(leaf
, &leaf_key
, slot
);
541 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
+ 1);
542 BUG_ON(comp_keys(&leaf_key
, &cpukey
) >= 0);
543 if (btrfs_item_offset_nr(leaf
, slot
) !=
544 btrfs_item_end_nr(leaf
, slot
+ 1)) {
545 btrfs_print_leaf(root
, leaf
);
546 printk("slot %d offset bad\n", slot
);
550 BUG_ON(btrfs_item_offset_nr(leaf
, 0) +
551 btrfs_item_size_nr(leaf
, 0) != BTRFS_LEAF_DATA_SIZE(root
));
555 static int noinline
check_block(struct btrfs_root
*root
,
556 struct btrfs_path
*path
, int level
)
560 struct extent_buffer
*buf
= path
->nodes
[level
];
562 if (memcmp_extent_buffer(buf
, root
->fs_info
->fsid
,
563 (unsigned long)btrfs_header_fsid(buf
),
565 printk("warning bad block %Lu\n", buf
->start
);
570 return check_leaf(root
, path
, level
);
571 return check_node(root
, path
, level
);
575 * search for key in the extent_buffer. The items start at offset p,
576 * and they are item_size apart. There are 'max' items in p.
578 * the slot in the array is returned via slot, and it points to
579 * the place where you would insert key if it is not found in
582 * slot may point to max if the key is bigger than all of the keys
584 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
585 int item_size
, struct btrfs_key
*key
,
592 struct btrfs_disk_key
*tmp
= NULL
;
593 struct btrfs_disk_key unaligned
;
594 unsigned long offset
;
595 char *map_token
= NULL
;
597 unsigned long map_start
= 0;
598 unsigned long map_len
= 0;
602 mid
= (low
+ high
) / 2;
603 offset
= p
+ mid
* item_size
;
605 if (!map_token
|| offset
< map_start
||
606 (offset
+ sizeof(struct btrfs_disk_key
)) >
607 map_start
+ map_len
) {
609 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
612 err
= map_extent_buffer(eb
, offset
,
613 sizeof(struct btrfs_disk_key
),
615 &map_start
, &map_len
, KM_USER0
);
618 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
621 read_extent_buffer(eb
, &unaligned
,
622 offset
, sizeof(unaligned
));
627 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
630 ret
= comp_keys(tmp
, key
);
639 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
645 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
650 * simple bin_search frontend that does the right thing for
653 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
654 int level
, int *slot
)
657 return generic_bin_search(eb
,
658 offsetof(struct btrfs_leaf
, items
),
659 sizeof(struct btrfs_item
),
660 key
, btrfs_header_nritems(eb
),
663 return generic_bin_search(eb
,
664 offsetof(struct btrfs_node
, ptrs
),
665 sizeof(struct btrfs_key_ptr
),
666 key
, btrfs_header_nritems(eb
),
672 static struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
673 struct extent_buffer
*parent
, int slot
)
677 if (slot
>= btrfs_header_nritems(parent
))
679 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
680 btrfs_level_size(root
, btrfs_header_level(parent
) - 1));
683 static int balance_level(struct btrfs_trans_handle
*trans
,
684 struct btrfs_root
*root
,
685 struct btrfs_path
*path
, int level
)
687 struct extent_buffer
*right
= NULL
;
688 struct extent_buffer
*mid
;
689 struct extent_buffer
*left
= NULL
;
690 struct extent_buffer
*parent
= NULL
;
694 int orig_slot
= path
->slots
[level
];
695 int err_on_enospc
= 0;
701 mid
= path
->nodes
[level
];
702 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
704 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
706 if (level
< BTRFS_MAX_LEVEL
- 1)
707 parent
= path
->nodes
[level
+ 1];
708 pslot
= path
->slots
[level
+ 1];
711 * deal with the case where there is only one pointer in the root
712 * by promoting the node below to a root
715 struct extent_buffer
*child
;
717 if (btrfs_header_nritems(mid
) != 1)
720 /* promote the child to a root */
721 child
= read_node_slot(root
, mid
, 0);
724 path
->nodes
[level
] = NULL
;
725 clean_tree_block(trans
, root
, mid
);
726 wait_on_tree_block_writeback(root
, mid
);
727 /* once for the path */
728 free_extent_buffer(mid
);
729 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
730 root
->root_key
.objectid
,
731 btrfs_header_generation(mid
), 0, 0, 1);
732 /* once for the root ptr */
733 free_extent_buffer(mid
);
736 if (btrfs_header_nritems(mid
) >
737 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
740 if (btrfs_header_nritems(mid
) < 2)
743 left
= read_node_slot(root
, parent
, pslot
- 1);
745 wret
= btrfs_cow_block(trans
, root
, left
,
746 parent
, pslot
- 1, &left
);
752 right
= read_node_slot(root
, parent
, pslot
+ 1);
754 wret
= btrfs_cow_block(trans
, root
, right
,
755 parent
, pslot
+ 1, &right
);
762 /* first, try to make some room in the middle buffer */
764 orig_slot
+= btrfs_header_nritems(left
);
765 wret
= push_node_left(trans
, root
, left
, mid
);
768 if (btrfs_header_nritems(mid
) < 2)
773 * then try to empty the right most buffer into the middle
776 wret
= push_node_left(trans
, root
, mid
, right
);
777 if (wret
< 0 && wret
!= -ENOSPC
)
779 if (btrfs_header_nritems(right
) == 0) {
780 u64 bytenr
= right
->start
;
781 u64 generation
= btrfs_header_generation(parent
);
782 u32 blocksize
= right
->len
;
784 clean_tree_block(trans
, root
, right
);
785 wait_on_tree_block_writeback(root
, right
);
786 free_extent_buffer(right
);
788 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
792 wret
= btrfs_free_extent(trans
, root
, bytenr
,
794 btrfs_header_owner(parent
),
795 generation
, 0, 0, 1);
799 struct btrfs_disk_key right_key
;
800 btrfs_node_key(right
, &right_key
, 0);
801 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
802 btrfs_mark_buffer_dirty(parent
);
805 if (btrfs_header_nritems(mid
) == 1) {
807 * we're not allowed to leave a node with one item in the
808 * tree during a delete. A deletion from lower in the tree
809 * could try to delete the only pointer in this node.
810 * So, pull some keys from the left.
811 * There has to be a left pointer at this point because
812 * otherwise we would have pulled some pointers from the
816 wret
= balance_node_right(trans
, root
, mid
, left
);
823 if (btrfs_header_nritems(mid
) == 0) {
824 /* we've managed to empty the middle node, drop it */
825 u64 root_gen
= btrfs_header_generation(parent
);
826 u64 bytenr
= mid
->start
;
827 u32 blocksize
= mid
->len
;
828 clean_tree_block(trans
, root
, mid
);
829 wait_on_tree_block_writeback(root
, mid
);
830 free_extent_buffer(mid
);
832 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
835 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
836 btrfs_header_owner(parent
),
841 /* update the parent key to reflect our changes */
842 struct btrfs_disk_key mid_key
;
843 btrfs_node_key(mid
, &mid_key
, 0);
844 btrfs_set_node_key(parent
, &mid_key
, pslot
);
845 btrfs_mark_buffer_dirty(parent
);
848 /* update the path */
850 if (btrfs_header_nritems(left
) > orig_slot
) {
851 extent_buffer_get(left
);
852 path
->nodes
[level
] = left
;
853 path
->slots
[level
+ 1] -= 1;
854 path
->slots
[level
] = orig_slot
;
856 free_extent_buffer(mid
);
858 orig_slot
-= btrfs_header_nritems(left
);
859 path
->slots
[level
] = orig_slot
;
862 /* double check we haven't messed things up */
863 check_block(root
, path
, level
);
865 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
869 free_extent_buffer(right
);
871 free_extent_buffer(left
);
875 /* returns zero if the push worked, non-zero otherwise */
876 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
877 struct btrfs_root
*root
,
878 struct btrfs_path
*path
, int level
)
880 struct extent_buffer
*right
= NULL
;
881 struct extent_buffer
*mid
;
882 struct extent_buffer
*left
= NULL
;
883 struct extent_buffer
*parent
= NULL
;
887 int orig_slot
= path
->slots
[level
];
893 mid
= path
->nodes
[level
];
894 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
895 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
897 if (level
< BTRFS_MAX_LEVEL
- 1)
898 parent
= path
->nodes
[level
+ 1];
899 pslot
= path
->slots
[level
+ 1];
904 left
= read_node_slot(root
, parent
, pslot
- 1);
906 /* first, try to make some room in the middle buffer */
909 left_nr
= btrfs_header_nritems(left
);
910 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
913 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
918 wret
= push_node_left(trans
, root
,
925 struct btrfs_disk_key disk_key
;
926 orig_slot
+= left_nr
;
927 btrfs_node_key(mid
, &disk_key
, 0);
928 btrfs_set_node_key(parent
, &disk_key
, pslot
);
929 btrfs_mark_buffer_dirty(parent
);
930 if (btrfs_header_nritems(left
) > orig_slot
) {
931 path
->nodes
[level
] = left
;
932 path
->slots
[level
+ 1] -= 1;
933 path
->slots
[level
] = orig_slot
;
934 free_extent_buffer(mid
);
937 btrfs_header_nritems(left
);
938 path
->slots
[level
] = orig_slot
;
939 free_extent_buffer(left
);
943 free_extent_buffer(left
);
945 right
= read_node_slot(root
, parent
, pslot
+ 1);
948 * then try to empty the right most buffer into the middle
952 right_nr
= btrfs_header_nritems(right
);
953 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
956 ret
= btrfs_cow_block(trans
, root
, right
,
962 wret
= balance_node_right(trans
, root
,
969 struct btrfs_disk_key disk_key
;
971 btrfs_node_key(right
, &disk_key
, 0);
972 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
973 btrfs_mark_buffer_dirty(parent
);
975 if (btrfs_header_nritems(mid
) <= orig_slot
) {
976 path
->nodes
[level
] = right
;
977 path
->slots
[level
+ 1] += 1;
978 path
->slots
[level
] = orig_slot
-
979 btrfs_header_nritems(mid
);
980 free_extent_buffer(mid
);
982 free_extent_buffer(right
);
986 free_extent_buffer(right
);
992 * readahead one full node of leaves
994 static void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
995 int level
, int slot
, u64 objectid
)
997 struct extent_buffer
*node
;
998 struct btrfs_disk_key disk_key
;
1004 int direction
= path
->reada
;
1005 struct extent_buffer
*eb
;
1013 if (!path
->nodes
[level
])
1016 node
= path
->nodes
[level
];
1017 search
= btrfs_node_blockptr(node
, slot
);
1018 blocksize
= btrfs_level_size(root
, level
- 1);
1019 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1021 free_extent_buffer(eb
);
1025 highest_read
= search
;
1026 lowest_read
= search
;
1028 nritems
= btrfs_header_nritems(node
);
1031 if (direction
< 0) {
1035 } else if (direction
> 0) {
1040 if (path
->reada
< 0 && objectid
) {
1041 btrfs_node_key(node
, &disk_key
, nr
);
1042 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1045 search
= btrfs_node_blockptr(node
, nr
);
1046 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1047 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1048 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1049 readahead_tree_block(root
, search
, blocksize
);
1053 if (path
->reada
< 2 && (nread
> (256 * 1024) || nscan
> 32))
1055 if(nread
> (1024 * 1024) || nscan
> 128)
1058 if (search
< lowest_read
)
1059 lowest_read
= search
;
1060 if (search
> highest_read
)
1061 highest_read
= search
;
1065 * look for key in the tree. path is filled in with nodes along the way
1066 * if key is found, we return zero and you can find the item in the leaf
1067 * level of the path (level 0)
1069 * If the key isn't found, the path points to the slot where it should
1070 * be inserted, and 1 is returned. If there are other errors during the
1071 * search a negative error number is returned.
1073 * if ins_len > 0, nodes and leaves will be split as we walk down the
1074 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1077 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1078 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1081 struct extent_buffer
*b
;
1087 int should_reada
= p
->reada
;
1088 u8 lowest_level
= 0;
1090 lowest_level
= p
->lowest_level
;
1091 WARN_ON(lowest_level
&& ins_len
);
1092 WARN_ON(p
->nodes
[0] != NULL
);
1093 WARN_ON(!mutex_is_locked(&root
->fs_info
->fs_mutex
));
1096 extent_buffer_get(b
);
1098 level
= btrfs_header_level(b
);
1101 wret
= btrfs_cow_block(trans
, root
, b
,
1102 p
->nodes
[level
+ 1],
1103 p
->slots
[level
+ 1],
1106 free_extent_buffer(b
);
1110 BUG_ON(!cow
&& ins_len
);
1111 if (level
!= btrfs_header_level(b
))
1113 level
= btrfs_header_level(b
);
1114 p
->nodes
[level
] = b
;
1115 ret
= check_block(root
, p
, level
);
1118 ret
= bin_search(b
, key
, level
, &slot
);
1120 if (ret
&& slot
> 0)
1122 p
->slots
[level
] = slot
;
1123 if (ins_len
> 0 && btrfs_header_nritems(b
) >=
1124 BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1125 int sret
= split_node(trans
, root
, p
, level
);
1129 b
= p
->nodes
[level
];
1130 slot
= p
->slots
[level
];
1131 } else if (ins_len
< 0) {
1132 int sret
= balance_level(trans
, root
, p
,
1136 b
= p
->nodes
[level
];
1138 btrfs_release_path(NULL
, p
);
1141 slot
= p
->slots
[level
];
1142 BUG_ON(btrfs_header_nritems(b
) == 1);
1144 /* this is only true while dropping a snapshot */
1145 if (level
== lowest_level
)
1147 bytenr
= btrfs_node_blockptr(b
, slot
);
1148 ptr_gen
= btrfs_node_ptr_generation(b
, slot
);
1150 reada_for_search(root
, p
, level
, slot
,
1152 b
= read_tree_block(root
, bytenr
,
1153 btrfs_level_size(root
, level
- 1));
1154 if (ptr_gen
!= btrfs_header_generation(b
)) {
1155 printk("block %llu bad gen wanted %llu "
1157 (unsigned long long)b
->start
,
1158 (unsigned long long)ptr_gen
,
1159 (unsigned long long)btrfs_header_generation(b
));
1162 p
->slots
[level
] = slot
;
1163 if (ins_len
> 0 && btrfs_leaf_free_space(root
, b
) <
1164 sizeof(struct btrfs_item
) + ins_len
) {
1165 int sret
= split_leaf(trans
, root
, key
,
1166 p
, ins_len
, ret
== 0);
1178 * adjust the pointers going up the tree, starting at level
1179 * making sure the right key of each node is points to 'key'.
1180 * This is used after shifting pointers to the left, so it stops
1181 * fixing up pointers when a given leaf/node is not in slot 0 of the
1184 * If this fails to write a tree block, it returns -1, but continues
1185 * fixing up the blocks in ram so the tree is consistent.
1187 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1188 struct btrfs_root
*root
, struct btrfs_path
*path
,
1189 struct btrfs_disk_key
*key
, int level
)
1193 struct extent_buffer
*t
;
1195 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1196 int tslot
= path
->slots
[i
];
1197 if (!path
->nodes
[i
])
1200 btrfs_set_node_key(t
, key
, tslot
);
1201 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1209 * try to push data from one node into the next node left in the
1212 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1213 * error, and > 0 if there was no room in the left hand block.
1215 static int push_node_left(struct btrfs_trans_handle
*trans
,
1216 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1217 struct extent_buffer
*src
)
1224 src_nritems
= btrfs_header_nritems(src
);
1225 dst_nritems
= btrfs_header_nritems(dst
);
1226 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1227 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1228 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1230 if (push_items
<= 0) {
1234 if (src_nritems
< push_items
)
1235 push_items
= src_nritems
;
1237 copy_extent_buffer(dst
, src
,
1238 btrfs_node_key_ptr_offset(dst_nritems
),
1239 btrfs_node_key_ptr_offset(0),
1240 push_items
* sizeof(struct btrfs_key_ptr
));
1242 if (push_items
< src_nritems
) {
1243 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1244 btrfs_node_key_ptr_offset(push_items
),
1245 (src_nritems
- push_items
) *
1246 sizeof(struct btrfs_key_ptr
));
1248 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1249 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1250 btrfs_mark_buffer_dirty(src
);
1251 btrfs_mark_buffer_dirty(dst
);
1256 * try to push data from one node into the next node right in the
1259 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1260 * error, and > 0 if there was no room in the right hand block.
1262 * this will only push up to 1/2 the contents of the left node over
1264 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1265 struct btrfs_root
*root
,
1266 struct extent_buffer
*dst
,
1267 struct extent_buffer
*src
)
1275 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1276 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1278 src_nritems
= btrfs_header_nritems(src
);
1279 dst_nritems
= btrfs_header_nritems(dst
);
1280 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1281 if (push_items
<= 0)
1284 max_push
= src_nritems
/ 2 + 1;
1285 /* don't try to empty the node */
1286 if (max_push
>= src_nritems
)
1289 if (max_push
< push_items
)
1290 push_items
= max_push
;
1292 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1293 btrfs_node_key_ptr_offset(0),
1295 sizeof(struct btrfs_key_ptr
));
1297 copy_extent_buffer(dst
, src
,
1298 btrfs_node_key_ptr_offset(0),
1299 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1300 push_items
* sizeof(struct btrfs_key_ptr
));
1302 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1303 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1305 btrfs_mark_buffer_dirty(src
);
1306 btrfs_mark_buffer_dirty(dst
);
1311 * helper function to insert a new root level in the tree.
1312 * A new node is allocated, and a single item is inserted to
1313 * point to the existing root
1315 * returns zero on success or < 0 on failure.
1317 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1318 struct btrfs_root
*root
,
1319 struct btrfs_path
*path
, int level
)
1323 struct extent_buffer
*lower
;
1324 struct extent_buffer
*c
;
1325 struct btrfs_disk_key lower_key
;
1327 BUG_ON(path
->nodes
[level
]);
1328 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1331 root_gen
= trans
->transid
;
1335 lower
= path
->nodes
[level
-1];
1337 btrfs_item_key(lower
, &lower_key
, 0);
1339 btrfs_node_key(lower
, &lower_key
, 0);
1341 c
= __btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1342 root
->root_key
.objectid
,
1343 root_gen
, lower_key
.objectid
, level
,
1344 root
->node
->start
, 0);
1347 memset_extent_buffer(c
, 0, 0, root
->nodesize
);
1348 btrfs_set_header_nritems(c
, 1);
1349 btrfs_set_header_level(c
, level
);
1350 btrfs_set_header_bytenr(c
, c
->start
);
1351 btrfs_set_header_generation(c
, trans
->transid
);
1352 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1354 write_extent_buffer(c
, root
->fs_info
->fsid
,
1355 (unsigned long)btrfs_header_fsid(c
),
1357 btrfs_set_node_key(c
, &lower_key
, 0);
1358 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1359 lower_gen
= btrfs_header_generation(lower
);
1360 WARN_ON(lower_gen
== 0);
1362 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1364 btrfs_mark_buffer_dirty(c
);
1366 /* the super has an extra ref to root->node */
1367 free_extent_buffer(root
->node
);
1369 extent_buffer_get(c
);
1370 path
->nodes
[level
] = c
;
1371 path
->slots
[level
] = 0;
1373 if (root
->ref_cows
&& lower_gen
!= trans
->transid
) {
1374 struct btrfs_path
*back_path
= btrfs_alloc_path();
1376 ret
= btrfs_insert_extent_backref(trans
,
1377 root
->fs_info
->extent_root
,
1379 root
->root_key
.objectid
,
1380 trans
->transid
, 0, 0);
1382 btrfs_free_path(back_path
);
1388 * worker function to insert a single pointer in a node.
1389 * the node should have enough room for the pointer already
1391 * slot and level indicate where you want the key to go, and
1392 * blocknr is the block the key points to.
1394 * returns zero on success and < 0 on any error
1396 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1397 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1398 *key
, u64 bytenr
, int slot
, int level
)
1400 struct extent_buffer
*lower
;
1403 BUG_ON(!path
->nodes
[level
]);
1404 lower
= path
->nodes
[level
];
1405 nritems
= btrfs_header_nritems(lower
);
1408 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
1410 if (slot
!= nritems
) {
1411 memmove_extent_buffer(lower
,
1412 btrfs_node_key_ptr_offset(slot
+ 1),
1413 btrfs_node_key_ptr_offset(slot
),
1414 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1416 btrfs_set_node_key(lower
, key
, slot
);
1417 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1418 WARN_ON(trans
->transid
== 0);
1419 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1420 btrfs_set_header_nritems(lower
, nritems
+ 1);
1421 btrfs_mark_buffer_dirty(lower
);
1426 * split the node at the specified level in path in two.
1427 * The path is corrected to point to the appropriate node after the split
1429 * Before splitting this tries to make some room in the node by pushing
1430 * left and right, if either one works, it returns right away.
1432 * returns 0 on success and < 0 on failure
1434 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1435 *root
, struct btrfs_path
*path
, int level
)
1438 struct extent_buffer
*c
;
1439 struct extent_buffer
*split
;
1440 struct btrfs_disk_key disk_key
;
1446 c
= path
->nodes
[level
];
1447 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1448 if (c
== root
->node
) {
1449 /* trying to split the root, lets make a new one */
1450 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1454 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1455 c
= path
->nodes
[level
];
1456 if (!ret
&& btrfs_header_nritems(c
) <
1457 BTRFS_NODEPTRS_PER_BLOCK(root
) - 1)
1463 c_nritems
= btrfs_header_nritems(c
);
1465 root_gen
= trans
->transid
;
1469 btrfs_node_key(c
, &disk_key
, 0);
1470 split
= __btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1471 root
->root_key
.objectid
,
1473 btrfs_disk_key_objectid(&disk_key
),
1474 level
, c
->start
, 0);
1476 return PTR_ERR(split
);
1478 btrfs_set_header_flags(split
, btrfs_header_flags(c
));
1479 btrfs_set_header_level(split
, btrfs_header_level(c
));
1480 btrfs_set_header_bytenr(split
, split
->start
);
1481 btrfs_set_header_generation(split
, trans
->transid
);
1482 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1483 write_extent_buffer(split
, root
->fs_info
->fsid
,
1484 (unsigned long)btrfs_header_fsid(split
),
1487 mid
= (c_nritems
+ 1) / 2;
1489 copy_extent_buffer(split
, c
,
1490 btrfs_node_key_ptr_offset(0),
1491 btrfs_node_key_ptr_offset(mid
),
1492 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1493 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1494 btrfs_set_header_nritems(c
, mid
);
1497 btrfs_mark_buffer_dirty(c
);
1498 btrfs_mark_buffer_dirty(split
);
1500 btrfs_node_key(split
, &disk_key
, 0);
1501 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1502 path
->slots
[level
+ 1] + 1,
1507 if (path
->slots
[level
] >= mid
) {
1508 path
->slots
[level
] -= mid
;
1509 free_extent_buffer(c
);
1510 path
->nodes
[level
] = split
;
1511 path
->slots
[level
+ 1] += 1;
1513 free_extent_buffer(split
);
1519 * how many bytes are required to store the items in a leaf. start
1520 * and nr indicate which items in the leaf to check. This totals up the
1521 * space used both by the item structs and the item data
1523 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1526 int nritems
= btrfs_header_nritems(l
);
1527 int end
= min(nritems
, start
+ nr
) - 1;
1531 data_len
= btrfs_item_end_nr(l
, start
);
1532 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1533 data_len
+= sizeof(struct btrfs_item
) * nr
;
1534 WARN_ON(data_len
< 0);
1539 * The space between the end of the leaf items and
1540 * the start of the leaf data. IOW, how much room
1541 * the leaf has left for both items and data
1543 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1545 int nritems
= btrfs_header_nritems(leaf
);
1547 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
1549 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1550 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
1551 leaf_space_used(leaf
, 0, nritems
), nritems
);
1557 * push some data in the path leaf to the right, trying to free up at
1558 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1560 * returns 1 if the push failed because the other node didn't have enough
1561 * room, 0 if everything worked out and < 0 if there were major errors.
1563 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1564 *root
, struct btrfs_path
*path
, int data_size
,
1567 struct extent_buffer
*left
= path
->nodes
[0];
1568 struct extent_buffer
*right
;
1569 struct extent_buffer
*upper
;
1570 struct btrfs_disk_key disk_key
;
1576 struct btrfs_item
*item
;
1584 slot
= path
->slots
[1];
1585 if (!path
->nodes
[1]) {
1588 upper
= path
->nodes
[1];
1589 if (slot
>= btrfs_header_nritems(upper
) - 1)
1592 right
= read_tree_block(root
, btrfs_node_blockptr(upper
, slot
+ 1),
1594 free_space
= btrfs_leaf_free_space(root
, right
);
1595 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1596 free_extent_buffer(right
);
1600 /* cow and double check */
1601 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1604 free_extent_buffer(right
);
1607 free_space
= btrfs_leaf_free_space(root
, right
);
1608 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1609 free_extent_buffer(right
);
1613 left_nritems
= btrfs_header_nritems(left
);
1614 if (left_nritems
== 0) {
1615 free_extent_buffer(right
);
1624 i
= left_nritems
- 1;
1626 item
= btrfs_item_nr(left
, i
);
1628 if (path
->slots
[0] == i
)
1629 push_space
+= data_size
+ sizeof(*item
);
1631 if (!left
->map_token
) {
1632 map_extent_buffer(left
, (unsigned long)item
,
1633 sizeof(struct btrfs_item
),
1634 &left
->map_token
, &left
->kaddr
,
1635 &left
->map_start
, &left
->map_len
,
1639 this_item_size
= btrfs_item_size(left
, item
);
1640 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1643 push_space
+= this_item_size
+ sizeof(*item
);
1648 if (left
->map_token
) {
1649 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
1650 left
->map_token
= NULL
;
1653 if (push_items
== 0) {
1654 free_extent_buffer(right
);
1658 if (!empty
&& push_items
== left_nritems
)
1661 /* push left to right */
1662 right_nritems
= btrfs_header_nritems(right
);
1664 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1665 push_space
-= leaf_data_end(root
, left
);
1667 /* make room in the right data area */
1668 data_end
= leaf_data_end(root
, right
);
1669 memmove_extent_buffer(right
,
1670 btrfs_leaf_data(right
) + data_end
- push_space
,
1671 btrfs_leaf_data(right
) + data_end
,
1672 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
1674 /* copy from the left data area */
1675 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1676 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1677 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1680 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1681 btrfs_item_nr_offset(0),
1682 right_nritems
* sizeof(struct btrfs_item
));
1684 /* copy the items from left to right */
1685 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1686 btrfs_item_nr_offset(left_nritems
- push_items
),
1687 push_items
* sizeof(struct btrfs_item
));
1689 /* update the item pointers */
1690 right_nritems
+= push_items
;
1691 btrfs_set_header_nritems(right
, right_nritems
);
1692 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1693 for (i
= 0; i
< right_nritems
; i
++) {
1694 item
= btrfs_item_nr(right
, i
);
1695 if (!right
->map_token
) {
1696 map_extent_buffer(right
, (unsigned long)item
,
1697 sizeof(struct btrfs_item
),
1698 &right
->map_token
, &right
->kaddr
,
1699 &right
->map_start
, &right
->map_len
,
1702 push_space
-= btrfs_item_size(right
, item
);
1703 btrfs_set_item_offset(right
, item
, push_space
);
1706 if (right
->map_token
) {
1707 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
1708 right
->map_token
= NULL
;
1710 left_nritems
-= push_items
;
1711 btrfs_set_header_nritems(left
, left_nritems
);
1714 btrfs_mark_buffer_dirty(left
);
1715 btrfs_mark_buffer_dirty(right
);
1717 btrfs_item_key(right
, &disk_key
, 0);
1718 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1719 btrfs_mark_buffer_dirty(upper
);
1721 /* then fixup the leaf pointer in the path */
1722 if (path
->slots
[0] >= left_nritems
) {
1723 path
->slots
[0] -= left_nritems
;
1724 free_extent_buffer(path
->nodes
[0]);
1725 path
->nodes
[0] = right
;
1726 path
->slots
[1] += 1;
1728 free_extent_buffer(right
);
1733 * push some data in the path leaf to the left, trying to free up at
1734 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1736 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1737 *root
, struct btrfs_path
*path
, int data_size
,
1740 struct btrfs_disk_key disk_key
;
1741 struct extent_buffer
*right
= path
->nodes
[0];
1742 struct extent_buffer
*left
;
1748 struct btrfs_item
*item
;
1749 u32 old_left_nritems
;
1755 u32 old_left_item_size
;
1757 slot
= path
->slots
[1];
1760 if (!path
->nodes
[1])
1763 right_nritems
= btrfs_header_nritems(right
);
1764 if (right_nritems
== 0) {
1768 left
= read_tree_block(root
, btrfs_node_blockptr(path
->nodes
[1],
1769 slot
- 1), root
->leafsize
);
1770 free_space
= btrfs_leaf_free_space(root
, left
);
1771 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1772 free_extent_buffer(left
);
1776 /* cow and double check */
1777 ret
= btrfs_cow_block(trans
, root
, left
,
1778 path
->nodes
[1], slot
- 1, &left
);
1780 /* we hit -ENOSPC, but it isn't fatal here */
1781 free_extent_buffer(left
);
1785 free_space
= btrfs_leaf_free_space(root
, left
);
1786 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1787 free_extent_buffer(left
);
1794 nr
= right_nritems
- 1;
1796 for (i
= 0; i
< nr
; i
++) {
1797 item
= btrfs_item_nr(right
, i
);
1798 if (!right
->map_token
) {
1799 map_extent_buffer(right
, (unsigned long)item
,
1800 sizeof(struct btrfs_item
),
1801 &right
->map_token
, &right
->kaddr
,
1802 &right
->map_start
, &right
->map_len
,
1806 if (path
->slots
[0] == i
)
1807 push_space
+= data_size
+ sizeof(*item
);
1809 this_item_size
= btrfs_item_size(right
, item
);
1810 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1814 push_space
+= this_item_size
+ sizeof(*item
);
1817 if (right
->map_token
) {
1818 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
1819 right
->map_token
= NULL
;
1822 if (push_items
== 0) {
1823 free_extent_buffer(left
);
1826 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1829 /* push data from right to left */
1830 copy_extent_buffer(left
, right
,
1831 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1832 btrfs_item_nr_offset(0),
1833 push_items
* sizeof(struct btrfs_item
));
1835 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1836 btrfs_item_offset_nr(right
, push_items
-1);
1838 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1839 leaf_data_end(root
, left
) - push_space
,
1840 btrfs_leaf_data(right
) +
1841 btrfs_item_offset_nr(right
, push_items
- 1),
1843 old_left_nritems
= btrfs_header_nritems(left
);
1844 BUG_ON(old_left_nritems
< 0);
1846 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1847 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1850 item
= btrfs_item_nr(left
, i
);
1851 if (!left
->map_token
) {
1852 map_extent_buffer(left
, (unsigned long)item
,
1853 sizeof(struct btrfs_item
),
1854 &left
->map_token
, &left
->kaddr
,
1855 &left
->map_start
, &left
->map_len
,
1859 ioff
= btrfs_item_offset(left
, item
);
1860 btrfs_set_item_offset(left
, item
,
1861 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
1863 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1864 if (left
->map_token
) {
1865 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
1866 left
->map_token
= NULL
;
1869 /* fixup right node */
1870 if (push_items
> right_nritems
) {
1871 printk("push items %d nr %u\n", push_items
, right_nritems
);
1875 if (push_items
< right_nritems
) {
1876 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1877 leaf_data_end(root
, right
);
1878 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1879 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1880 btrfs_leaf_data(right
) +
1881 leaf_data_end(root
, right
), push_space
);
1883 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1884 btrfs_item_nr_offset(push_items
),
1885 (btrfs_header_nritems(right
) - push_items
) *
1886 sizeof(struct btrfs_item
));
1888 right_nritems
-= push_items
;
1889 btrfs_set_header_nritems(right
, right_nritems
);
1890 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1891 for (i
= 0; i
< right_nritems
; i
++) {
1892 item
= btrfs_item_nr(right
, i
);
1894 if (!right
->map_token
) {
1895 map_extent_buffer(right
, (unsigned long)item
,
1896 sizeof(struct btrfs_item
),
1897 &right
->map_token
, &right
->kaddr
,
1898 &right
->map_start
, &right
->map_len
,
1902 push_space
= push_space
- btrfs_item_size(right
, item
);
1903 btrfs_set_item_offset(right
, item
, push_space
);
1905 if (right
->map_token
) {
1906 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
1907 right
->map_token
= NULL
;
1910 btrfs_mark_buffer_dirty(left
);
1912 btrfs_mark_buffer_dirty(right
);
1914 btrfs_item_key(right
, &disk_key
, 0);
1915 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1919 /* then fixup the leaf pointer in the path */
1920 if (path
->slots
[0] < push_items
) {
1921 path
->slots
[0] += old_left_nritems
;
1922 free_extent_buffer(path
->nodes
[0]);
1923 path
->nodes
[0] = left
;
1924 path
->slots
[1] -= 1;
1926 free_extent_buffer(left
);
1927 path
->slots
[0] -= push_items
;
1929 BUG_ON(path
->slots
[0] < 0);
1934 * split the path's leaf in two, making sure there is at least data_size
1935 * available for the resulting leaf level of the path.
1937 * returns 0 if all went well and < 0 on failure.
1939 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
1940 *root
, struct btrfs_key
*ins_key
,
1941 struct btrfs_path
*path
, int data_size
, int extend
)
1944 struct extent_buffer
*l
;
1948 struct extent_buffer
*right
;
1949 int space_needed
= data_size
+ sizeof(struct btrfs_item
);
1956 int num_doubles
= 0;
1957 struct btrfs_disk_key disk_key
;
1960 space_needed
= data_size
;
1963 root_gen
= trans
->transid
;
1967 /* first try to make some room by pushing left and right */
1968 if (ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
1969 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
1974 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
1980 /* did the pushes work? */
1981 if (btrfs_leaf_free_space(root
, l
) >= space_needed
)
1985 if (!path
->nodes
[1]) {
1986 ret
= insert_new_root(trans
, root
, path
, 1);
1993 slot
= path
->slots
[0];
1994 nritems
= btrfs_header_nritems(l
);
1995 mid
= (nritems
+ 1)/ 2;
1997 btrfs_item_key(l
, &disk_key
, 0);
1999 right
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2000 root
->root_key
.objectid
,
2001 root_gen
, disk_key
.objectid
, 0,
2004 return PTR_ERR(right
);
2006 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2007 btrfs_set_header_bytenr(right
, right
->start
);
2008 btrfs_set_header_generation(right
, trans
->transid
);
2009 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2010 btrfs_set_header_level(right
, 0);
2011 write_extent_buffer(right
, root
->fs_info
->fsid
,
2012 (unsigned long)btrfs_header_fsid(right
),
2016 leaf_space_used(l
, mid
, nritems
- mid
) + space_needed
>
2017 BTRFS_LEAF_DATA_SIZE(root
)) {
2018 if (slot
>= nritems
) {
2019 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2020 btrfs_set_header_nritems(right
, 0);
2021 wret
= insert_ptr(trans
, root
, path
,
2022 &disk_key
, right
->start
,
2023 path
->slots
[1] + 1, 1);
2026 free_extent_buffer(path
->nodes
[0]);
2027 path
->nodes
[0] = right
;
2029 path
->slots
[1] += 1;
2033 if (mid
!= nritems
&&
2034 leaf_space_used(l
, mid
, nritems
- mid
) +
2035 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
2040 if (leaf_space_used(l
, 0, mid
+ 1) + space_needed
>
2041 BTRFS_LEAF_DATA_SIZE(root
)) {
2042 if (!extend
&& slot
== 0) {
2043 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2044 btrfs_set_header_nritems(right
, 0);
2045 wret
= insert_ptr(trans
, root
, path
,
2051 free_extent_buffer(path
->nodes
[0]);
2052 path
->nodes
[0] = right
;
2054 if (path
->slots
[1] == 0) {
2055 wret
= fixup_low_keys(trans
, root
,
2056 path
, &disk_key
, 1);
2061 } else if (extend
&& slot
== 0) {
2065 if (mid
!= nritems
&&
2066 leaf_space_used(l
, mid
, nritems
- mid
) +
2067 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
2073 nritems
= nritems
- mid
;
2074 btrfs_set_header_nritems(right
, nritems
);
2075 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2077 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2078 btrfs_item_nr_offset(mid
),
2079 nritems
* sizeof(struct btrfs_item
));
2081 copy_extent_buffer(right
, l
,
2082 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2083 data_copy_size
, btrfs_leaf_data(l
) +
2084 leaf_data_end(root
, l
), data_copy_size
);
2086 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2087 btrfs_item_end_nr(l
, mid
);
2089 for (i
= 0; i
< nritems
; i
++) {
2090 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2093 if (!right
->map_token
) {
2094 map_extent_buffer(right
, (unsigned long)item
,
2095 sizeof(struct btrfs_item
),
2096 &right
->map_token
, &right
->kaddr
,
2097 &right
->map_start
, &right
->map_len
,
2101 ioff
= btrfs_item_offset(right
, item
);
2102 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2105 if (right
->map_token
) {
2106 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2107 right
->map_token
= NULL
;
2110 btrfs_set_header_nritems(l
, mid
);
2112 btrfs_item_key(right
, &disk_key
, 0);
2113 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2114 path
->slots
[1] + 1, 1);
2118 btrfs_mark_buffer_dirty(right
);
2119 btrfs_mark_buffer_dirty(l
);
2120 BUG_ON(path
->slots
[0] != slot
);
2123 free_extent_buffer(path
->nodes
[0]);
2124 path
->nodes
[0] = right
;
2125 path
->slots
[0] -= mid
;
2126 path
->slots
[1] += 1;
2128 free_extent_buffer(right
);
2130 BUG_ON(path
->slots
[0] < 0);
2133 BUG_ON(num_doubles
!= 0);
2140 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
2141 struct btrfs_root
*root
,
2142 struct btrfs_path
*path
,
2143 u32 new_size
, int from_end
)
2148 struct extent_buffer
*leaf
;
2149 struct btrfs_item
*item
;
2151 unsigned int data_end
;
2152 unsigned int old_data_start
;
2153 unsigned int old_size
;
2154 unsigned int size_diff
;
2157 slot_orig
= path
->slots
[0];
2158 leaf
= path
->nodes
[0];
2159 slot
= path
->slots
[0];
2161 old_size
= btrfs_item_size_nr(leaf
, slot
);
2162 if (old_size
== new_size
)
2165 nritems
= btrfs_header_nritems(leaf
);
2166 data_end
= leaf_data_end(root
, leaf
);
2168 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2170 size_diff
= old_size
- new_size
;
2173 BUG_ON(slot
>= nritems
);
2176 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2178 /* first correct the data pointers */
2179 for (i
= slot
; i
< nritems
; i
++) {
2181 item
= btrfs_item_nr(leaf
, i
);
2183 if (!leaf
->map_token
) {
2184 map_extent_buffer(leaf
, (unsigned long)item
,
2185 sizeof(struct btrfs_item
),
2186 &leaf
->map_token
, &leaf
->kaddr
,
2187 &leaf
->map_start
, &leaf
->map_len
,
2191 ioff
= btrfs_item_offset(leaf
, item
);
2192 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2195 if (leaf
->map_token
) {
2196 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
2197 leaf
->map_token
= NULL
;
2200 /* shift the data */
2202 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2203 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2204 data_end
, old_data_start
+ new_size
- data_end
);
2206 struct btrfs_disk_key disk_key
;
2209 btrfs_item_key(leaf
, &disk_key
, slot
);
2211 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2213 struct btrfs_file_extent_item
*fi
;
2215 fi
= btrfs_item_ptr(leaf
, slot
,
2216 struct btrfs_file_extent_item
);
2217 fi
= (struct btrfs_file_extent_item
*)(
2218 (unsigned long)fi
- size_diff
);
2220 if (btrfs_file_extent_type(leaf
, fi
) ==
2221 BTRFS_FILE_EXTENT_INLINE
) {
2222 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2223 memmove_extent_buffer(leaf
, ptr
,
2225 offsetof(struct btrfs_file_extent_item
,
2230 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2231 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2232 data_end
, old_data_start
- data_end
);
2234 offset
= btrfs_disk_key_offset(&disk_key
);
2235 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2236 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2238 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2241 item
= btrfs_item_nr(leaf
, slot
);
2242 btrfs_set_item_size(leaf
, item
, new_size
);
2243 btrfs_mark_buffer_dirty(leaf
);
2246 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2247 btrfs_print_leaf(root
, leaf
);
2253 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
2254 struct btrfs_root
*root
, struct btrfs_path
*path
,
2260 struct extent_buffer
*leaf
;
2261 struct btrfs_item
*item
;
2263 unsigned int data_end
;
2264 unsigned int old_data
;
2265 unsigned int old_size
;
2268 slot_orig
= path
->slots
[0];
2269 leaf
= path
->nodes
[0];
2271 nritems
= btrfs_header_nritems(leaf
);
2272 data_end
= leaf_data_end(root
, leaf
);
2274 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2275 btrfs_print_leaf(root
, leaf
);
2278 slot
= path
->slots
[0];
2279 old_data
= btrfs_item_end_nr(leaf
, slot
);
2282 if (slot
>= nritems
) {
2283 btrfs_print_leaf(root
, leaf
);
2284 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2289 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2291 /* first correct the data pointers */
2292 for (i
= slot
; i
< nritems
; i
++) {
2294 item
= btrfs_item_nr(leaf
, i
);
2296 if (!leaf
->map_token
) {
2297 map_extent_buffer(leaf
, (unsigned long)item
,
2298 sizeof(struct btrfs_item
),
2299 &leaf
->map_token
, &leaf
->kaddr
,
2300 &leaf
->map_start
, &leaf
->map_len
,
2303 ioff
= btrfs_item_offset(leaf
, item
);
2304 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2307 if (leaf
->map_token
) {
2308 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
2309 leaf
->map_token
= NULL
;
2312 /* shift the data */
2313 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2314 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2315 data_end
, old_data
- data_end
);
2317 data_end
= old_data
;
2318 old_size
= btrfs_item_size_nr(leaf
, slot
);
2319 item
= btrfs_item_nr(leaf
, slot
);
2320 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2321 btrfs_mark_buffer_dirty(leaf
);
2324 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2325 btrfs_print_leaf(root
, leaf
);
2332 * Given a key and some data, insert an item into the tree.
2333 * This does all the path init required, making room in the tree if needed.
2335 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2336 struct btrfs_root
*root
,
2337 struct btrfs_path
*path
,
2338 struct btrfs_key
*cpu_key
, u32
*data_size
,
2341 struct extent_buffer
*leaf
;
2342 struct btrfs_item
*item
;
2350 unsigned int data_end
;
2351 struct btrfs_disk_key disk_key
;
2353 for (i
= 0; i
< nr
; i
++) {
2354 total_data
+= data_size
[i
];
2357 /* create a root if there isn't one */
2361 total_size
= total_data
+ (nr
- 1) * sizeof(struct btrfs_item
);
2362 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2369 slot_orig
= path
->slots
[0];
2370 leaf
= path
->nodes
[0];
2372 nritems
= btrfs_header_nritems(leaf
);
2373 data_end
= leaf_data_end(root
, leaf
);
2375 if (btrfs_leaf_free_space(root
, leaf
) <
2376 sizeof(struct btrfs_item
) + total_size
) {
2377 btrfs_print_leaf(root
, leaf
);
2378 printk("not enough freespace need %u have %d\n",
2379 total_size
, btrfs_leaf_free_space(root
, leaf
));
2383 slot
= path
->slots
[0];
2386 if (slot
!= nritems
) {
2388 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2390 if (old_data
< data_end
) {
2391 btrfs_print_leaf(root
, leaf
);
2392 printk("slot %d old_data %d data_end %d\n",
2393 slot
, old_data
, data_end
);
2397 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2399 /* first correct the data pointers */
2400 WARN_ON(leaf
->map_token
);
2401 for (i
= slot
; i
< nritems
; i
++) {
2404 item
= btrfs_item_nr(leaf
, i
);
2405 if (!leaf
->map_token
) {
2406 map_extent_buffer(leaf
, (unsigned long)item
,
2407 sizeof(struct btrfs_item
),
2408 &leaf
->map_token
, &leaf
->kaddr
,
2409 &leaf
->map_start
, &leaf
->map_len
,
2413 ioff
= btrfs_item_offset(leaf
, item
);
2414 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2416 if (leaf
->map_token
) {
2417 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
2418 leaf
->map_token
= NULL
;
2421 /* shift the items */
2422 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2423 btrfs_item_nr_offset(slot
),
2424 (nritems
- slot
) * sizeof(struct btrfs_item
));
2426 /* shift the data */
2427 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2428 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2429 data_end
, old_data
- data_end
);
2430 data_end
= old_data
;
2433 /* setup the item for the new data */
2434 for (i
= 0; i
< nr
; i
++) {
2435 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2436 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2437 item
= btrfs_item_nr(leaf
, slot
+ i
);
2438 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2439 data_end
-= data_size
[i
];
2440 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2442 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2443 btrfs_mark_buffer_dirty(leaf
);
2447 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2448 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2451 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2452 btrfs_print_leaf(root
, leaf
);
2461 * Given a key and some data, insert an item into the tree.
2462 * This does all the path init required, making room in the tree if needed.
2464 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2465 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2469 struct btrfs_path
*path
;
2470 struct extent_buffer
*leaf
;
2473 path
= btrfs_alloc_path();
2475 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2477 leaf
= path
->nodes
[0];
2478 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2479 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2480 btrfs_mark_buffer_dirty(leaf
);
2482 btrfs_free_path(path
);
2487 * delete the pointer from a given node.
2489 * If the delete empties a node, the node is removed from the tree,
2490 * continuing all the way the root if required. The root is converted into
2491 * a leaf if all the nodes are emptied.
2493 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2494 struct btrfs_path
*path
, int level
, int slot
)
2496 struct extent_buffer
*parent
= path
->nodes
[level
];
2501 nritems
= btrfs_header_nritems(parent
);
2502 if (slot
!= nritems
-1) {
2503 memmove_extent_buffer(parent
,
2504 btrfs_node_key_ptr_offset(slot
),
2505 btrfs_node_key_ptr_offset(slot
+ 1),
2506 sizeof(struct btrfs_key_ptr
) *
2507 (nritems
- slot
- 1));
2510 btrfs_set_header_nritems(parent
, nritems
);
2511 if (nritems
== 0 && parent
== root
->node
) {
2512 BUG_ON(btrfs_header_level(root
->node
) != 1);
2513 /* just turn the root into a leaf and break */
2514 btrfs_set_header_level(root
->node
, 0);
2515 } else if (slot
== 0) {
2516 struct btrfs_disk_key disk_key
;
2518 btrfs_node_key(parent
, &disk_key
, 0);
2519 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
2523 btrfs_mark_buffer_dirty(parent
);
2528 * delete the item at the leaf level in path. If that empties
2529 * the leaf, remove it from the tree
2531 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2532 struct btrfs_path
*path
, int slot
, int nr
)
2534 struct extent_buffer
*leaf
;
2535 struct btrfs_item
*item
;
2543 leaf
= path
->nodes
[0];
2544 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2546 for (i
= 0; i
< nr
; i
++)
2547 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2549 nritems
= btrfs_header_nritems(leaf
);
2551 if (slot
+ nr
!= nritems
) {
2553 int data_end
= leaf_data_end(root
, leaf
);
2555 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2557 btrfs_leaf_data(leaf
) + data_end
,
2558 last_off
- data_end
);
2560 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2563 item
= btrfs_item_nr(leaf
, i
);
2564 if (!leaf
->map_token
) {
2565 map_extent_buffer(leaf
, (unsigned long)item
,
2566 sizeof(struct btrfs_item
),
2567 &leaf
->map_token
, &leaf
->kaddr
,
2568 &leaf
->map_start
, &leaf
->map_len
,
2571 ioff
= btrfs_item_offset(leaf
, item
);
2572 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2575 if (leaf
->map_token
) {
2576 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
2577 leaf
->map_token
= NULL
;
2580 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2581 btrfs_item_nr_offset(slot
+ nr
),
2582 sizeof(struct btrfs_item
) *
2583 (nritems
- slot
- nr
));
2585 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2588 /* delete the leaf if we've emptied it */
2590 if (leaf
== root
->node
) {
2591 btrfs_set_header_level(leaf
, 0);
2593 u64 root_gen
= btrfs_header_generation(path
->nodes
[1]);
2594 clean_tree_block(trans
, root
, leaf
);
2595 wait_on_tree_block_writeback(root
, leaf
);
2596 wret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
2599 wret
= btrfs_free_extent(trans
, root
,
2600 leaf
->start
, leaf
->len
,
2601 btrfs_header_owner(path
->nodes
[1]),
2607 int used
= leaf_space_used(leaf
, 0, nritems
);
2609 struct btrfs_disk_key disk_key
;
2611 btrfs_item_key(leaf
, &disk_key
, 0);
2612 wret
= fixup_low_keys(trans
, root
, path
,
2618 /* delete the leaf if it is mostly empty */
2619 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 4) {
2620 /* push_leaf_left fixes the path.
2621 * make sure the path still points to our leaf
2622 * for possible call to del_ptr below
2624 slot
= path
->slots
[1];
2625 extent_buffer_get(leaf
);
2627 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2628 if (wret
< 0 && wret
!= -ENOSPC
)
2631 if (path
->nodes
[0] == leaf
&&
2632 btrfs_header_nritems(leaf
)) {
2633 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2634 if (wret
< 0 && wret
!= -ENOSPC
)
2638 if (btrfs_header_nritems(leaf
) == 0) {
2640 u64 bytenr
= leaf
->start
;
2641 u32 blocksize
= leaf
->len
;
2643 root_gen
= btrfs_header_generation(
2646 clean_tree_block(trans
, root
, leaf
);
2647 wait_on_tree_block_writeback(root
, leaf
);
2649 wret
= del_ptr(trans
, root
, path
, 1, slot
);
2653 free_extent_buffer(leaf
);
2654 wret
= btrfs_free_extent(trans
, root
, bytenr
,
2656 btrfs_header_owner(path
->nodes
[1]),
2661 btrfs_mark_buffer_dirty(leaf
);
2662 free_extent_buffer(leaf
);
2665 btrfs_mark_buffer_dirty(leaf
);
2672 * walk up the tree as far as required to find the previous leaf.
2673 * returns 0 if it found something or 1 if there are no lesser leaves.
2674 * returns < 0 on io errors.
2676 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2681 struct extent_buffer
*c
;
2682 struct extent_buffer
*next
= NULL
;
2684 while(level
< BTRFS_MAX_LEVEL
) {
2685 if (!path
->nodes
[level
])
2688 slot
= path
->slots
[level
];
2689 c
= path
->nodes
[level
];
2692 if (level
== BTRFS_MAX_LEVEL
)
2698 bytenr
= btrfs_node_blockptr(c
, slot
);
2700 free_extent_buffer(next
);
2702 next
= read_tree_block(root
, bytenr
,
2703 btrfs_level_size(root
, level
- 1));
2706 path
->slots
[level
] = slot
;
2709 c
= path
->nodes
[level
];
2710 free_extent_buffer(c
);
2711 slot
= btrfs_header_nritems(next
);
2714 path
->nodes
[level
] = next
;
2715 path
->slots
[level
] = slot
;
2718 next
= read_tree_block(root
, btrfs_node_blockptr(next
, slot
),
2719 btrfs_level_size(root
, level
- 1));
2725 * walk up the tree as far as required to find the next leaf.
2726 * returns 0 if it found something or 1 if there are no greater leaves.
2727 * returns < 0 on io errors.
2729 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2734 struct extent_buffer
*c
;
2735 struct extent_buffer
*next
= NULL
;
2737 while(level
< BTRFS_MAX_LEVEL
) {
2738 if (!path
->nodes
[level
])
2741 slot
= path
->slots
[level
] + 1;
2742 c
= path
->nodes
[level
];
2743 if (slot
>= btrfs_header_nritems(c
)) {
2745 if (level
== BTRFS_MAX_LEVEL
)
2750 bytenr
= btrfs_node_blockptr(c
, slot
);
2752 free_extent_buffer(next
);
2755 reada_for_search(root
, path
, level
, slot
, 0);
2757 next
= read_tree_block(root
, bytenr
,
2758 btrfs_level_size(root
, level
-1));
2761 path
->slots
[level
] = slot
;
2764 c
= path
->nodes
[level
];
2765 free_extent_buffer(c
);
2766 path
->nodes
[level
] = next
;
2767 path
->slots
[level
] = 0;
2771 reada_for_search(root
, path
, level
, 0, 0);
2772 next
= read_tree_block(root
, btrfs_node_blockptr(next
, 0),
2773 btrfs_level_size(root
, level
- 1));