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
;
87 struct btrfs_root
*new_root
;
88 struct btrfs_disk_key disk_key
;
90 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
94 memcpy(new_root
, root
, sizeof(*new_root
));
95 new_root
->root_key
.objectid
= new_root_objectid
;
97 WARN_ON(root
->ref_cows
&& trans
->transid
!=
98 root
->fs_info
->running_transaction
->transid
);
99 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
101 level
= btrfs_header_level(buf
);
103 btrfs_item_key(buf
, &disk_key
, 0);
105 btrfs_node_key(buf
, &disk_key
, 0);
106 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
107 new_root_objectid
, &disk_key
,
108 level
, buf
->start
, 0);
114 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
115 btrfs_set_header_bytenr(cow
, cow
->start
);
116 btrfs_set_header_generation(cow
, trans
->transid
);
117 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
118 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
119 BTRFS_HEADER_FLAG_RELOC
);
120 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
121 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
123 btrfs_set_header_owner(cow
, new_root_objectid
);
125 write_extent_buffer(cow
, root
->fs_info
->fsid
,
126 (unsigned long)btrfs_header_fsid(cow
),
129 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
130 ret
= btrfs_inc_ref(trans
, new_root
, cow
, 0);
136 btrfs_mark_buffer_dirty(cow
);
142 * check if the tree block can be shared by multiple trees
144 int btrfs_block_can_be_shared(struct btrfs_root
*root
,
145 struct extent_buffer
*buf
)
148 * Tree blocks not in refernece counted trees and tree roots
149 * are never shared. If a block was allocated after the last
150 * snapshot and the block was not allocated by tree relocation,
151 * we know the block is not shared.
153 if (root
->ref_cows
&&
154 buf
!= root
->node
&& buf
!= root
->commit_root
&&
155 (btrfs_header_generation(buf
) <=
156 btrfs_root_last_snapshot(&root
->root_item
) ||
157 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
159 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
160 if (root
->ref_cows
&&
161 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
167 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
168 struct btrfs_root
*root
,
169 struct extent_buffer
*buf
,
170 struct extent_buffer
*cow
)
179 * Backrefs update rules:
181 * Always use full backrefs for extent pointers in tree block
182 * allocated by tree relocation.
184 * If a shared tree block is no longer referenced by its owner
185 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
186 * use full backrefs for extent pointers in tree block.
188 * If a tree block is been relocating
189 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
190 * use full backrefs for extent pointers in tree block.
191 * The reason for this is some operations (such as drop tree)
192 * are only allowed for blocks use full backrefs.
195 if (btrfs_block_can_be_shared(root
, buf
)) {
196 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
197 buf
->len
, &refs
, &flags
);
202 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
203 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
204 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
209 owner
= btrfs_header_owner(buf
);
210 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) &&
211 owner
== BTRFS_TREE_RELOC_OBJECTID
);
214 if ((owner
== root
->root_key
.objectid
||
215 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
216 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
217 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
220 if (root
->root_key
.objectid
==
221 BTRFS_TREE_RELOC_OBJECTID
) {
222 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
224 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
227 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
230 if (root
->root_key
.objectid
==
231 BTRFS_TREE_RELOC_OBJECTID
)
232 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
234 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
237 if (new_flags
!= 0) {
238 ret
= btrfs_set_block_flags(trans
, root
, buf
->start
,
239 buf
->len
, new_flags
);
243 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
244 if (root
->root_key
.objectid
==
245 BTRFS_TREE_RELOC_OBJECTID
)
246 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
248 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
250 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
253 clean_tree_block(trans
, root
, buf
);
258 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
259 struct btrfs_root
*root
,
260 struct extent_buffer
*buf
,
261 struct extent_buffer
*parent
, int parent_slot
,
262 struct extent_buffer
**cow_ret
,
263 u64 search_start
, u64 empty_size
)
266 struct extent_buffer
*cow
;
267 struct btrfs_disk_key disk_key
;
270 WARN_ON(root
->ref_cows
&& trans
->transid
!=
271 root
->fs_info
->running_transaction
->transid
);
272 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
274 level
= btrfs_header_level(buf
);
275 generation
= btrfs_header_generation(buf
);
278 btrfs_item_key(buf
, &disk_key
, 0);
280 btrfs_node_key(buf
, &disk_key
, 0);
282 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
,
283 root
->root_key
.objectid
, &disk_key
,
284 level
, search_start
, empty_size
);
288 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
289 btrfs_set_header_bytenr(cow
, cow
->start
);
290 btrfs_set_header_generation(cow
, trans
->transid
);
291 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
292 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
293 BTRFS_HEADER_FLAG_RELOC
);
294 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
295 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
297 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
299 write_extent_buffer(cow
, root
->fs_info
->fsid
,
300 (unsigned long)btrfs_header_fsid(cow
),
303 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
305 update_ref_for_cow(trans
, root
, buf
, cow
);
307 if (buf
== root
->node
) {
309 extent_buffer_get(cow
);
311 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
312 0, root
->root_key
.objectid
, level
, 0);
313 free_extent_buffer(buf
);
314 add_root_to_dirty_list(root
);
316 btrfs_set_node_blockptr(parent
, parent_slot
,
318 WARN_ON(trans
->transid
== 0);
319 btrfs_set_node_ptr_generation(parent
, parent_slot
,
321 btrfs_mark_buffer_dirty(parent
);
322 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
324 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
325 0, root
->root_key
.objectid
, level
, 1);
327 free_extent_buffer(buf
);
328 btrfs_mark_buffer_dirty(cow
);
333 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
334 struct btrfs_root
*root
,
335 struct extent_buffer
*buf
)
337 if (btrfs_header_generation(buf
) == trans
->transid
&&
338 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
339 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
340 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
345 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
346 struct btrfs_root
*root
, struct extent_buffer
*buf
,
347 struct extent_buffer
*parent
, int parent_slot
,
348 struct extent_buffer
**cow_ret
)
353 if (trans->transaction != root->fs_info->running_transaction) {
354 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
355 root->fs_info->running_transaction->transid);
359 if (trans
->transid
!= root
->fs_info
->generation
) {
360 printk(KERN_CRIT
"trans %llu running %llu\n",
361 (unsigned long long)trans
->transid
,
362 (unsigned long long)root
->fs_info
->generation
);
365 if (!should_cow_block(trans
, root
, buf
)) {
370 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
371 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
372 parent_slot
, cow_ret
, search_start
, 0);
377 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
379 if (blocknr < other && other - (blocknr + blocksize) < 32768)
381 if (blocknr > other && blocknr - (other + blocksize) < 32768)
388 * compare two keys in a memcmp fashion
390 int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
394 btrfs_disk_key_to_cpu(&k1
, disk
);
396 if (k1
.objectid
> k2
->objectid
)
398 if (k1
.objectid
< k2
->objectid
)
400 if (k1
.type
> k2
->type
)
402 if (k1
.type
< k2
->type
)
404 if (k1
.offset
> k2
->offset
)
406 if (k1
.offset
< k2
->offset
)
413 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
414 struct btrfs_root
*root
, struct extent_buffer
*parent
,
415 int start_slot
, int cache_only
, u64
*last_ret
,
416 struct btrfs_key
*progress
)
418 struct extent_buffer
*cur
;
419 struct extent_buffer
*tmp
;
422 u64 search_start
= *last_ret
;
432 int progress_passed
= 0;
433 struct btrfs_disk_key disk_key
;
435 parent_level
= btrfs_header_level(parent
);
436 if (cache_only
&& parent_level
!= 1)
439 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
440 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
441 root
->fs_info
->running_transaction
->transid
);
444 if (trans
->transid
!= root
->fs_info
->generation
) {
445 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
446 root
->fs_info
->generation
);
450 parent_nritems
= btrfs_header_nritems(parent
);
451 blocksize
= btrfs_level_size(root
, parent_level
- 1);
452 end_slot
= parent_nritems
;
454 if (parent_nritems
== 1)
457 for (i
= start_slot
; i
< end_slot
; i
++) {
460 if (!parent
->map_token
) {
461 map_extent_buffer(parent
,
462 btrfs_node_key_ptr_offset(i
),
463 sizeof(struct btrfs_key_ptr
),
464 &parent
->map_token
, &parent
->kaddr
,
465 &parent
->map_start
, &parent
->map_len
,
468 btrfs_node_key(parent
, &disk_key
, i
);
469 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
473 blocknr
= btrfs_node_blockptr(parent
, i
);
474 gen
= btrfs_node_ptr_generation(parent
, i
);
476 last_block
= blocknr
;
479 other
= btrfs_node_blockptr(parent
, i
- 1);
480 close
= close_blocks(blocknr
, other
, blocksize
);
482 if (close
&& i
< end_slot
- 2) {
483 other
= btrfs_node_blockptr(parent
, i
+ 1);
484 close
= close_blocks(blocknr
, other
, blocksize
);
487 last_block
= blocknr
;
490 if (parent
->map_token
) {
491 unmap_extent_buffer(parent
, parent
->map_token
,
493 parent
->map_token
= NULL
;
496 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
498 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
501 if (!cur
|| !uptodate
) {
503 free_extent_buffer(cur
);
507 cur
= read_tree_block(root
, blocknr
,
509 } else if (!uptodate
) {
510 btrfs_read_buffer(cur
, gen
);
513 if (search_start
== 0)
514 search_start
= last_block
;
516 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
519 (end_slot
- i
) * blocksize
));
521 free_extent_buffer(cur
);
524 search_start
= tmp
->start
;
525 last_block
= tmp
->start
;
526 *last_ret
= search_start
;
527 if (parent_level
== 1)
528 btrfs_clear_buffer_defrag(tmp
);
529 free_extent_buffer(tmp
);
531 if (parent
->map_token
) {
532 unmap_extent_buffer(parent
, parent
->map_token
,
534 parent
->map_token
= NULL
;
541 * The leaf data grows from end-to-front in the node.
542 * this returns the address of the start of the last item,
543 * which is the stop of the leaf data stack
545 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
546 struct extent_buffer
*leaf
)
548 u32 nr
= btrfs_header_nritems(leaf
);
550 return BTRFS_LEAF_DATA_SIZE(root
);
551 return btrfs_item_offset_nr(leaf
, nr
- 1);
554 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
557 struct extent_buffer
*parent
= NULL
;
558 struct extent_buffer
*node
= path
->nodes
[level
];
559 struct btrfs_disk_key parent_key
;
560 struct btrfs_disk_key node_key
;
563 struct btrfs_key cpukey
;
564 u32 nritems
= btrfs_header_nritems(node
);
566 if (path
->nodes
[level
+ 1])
567 parent
= path
->nodes
[level
+ 1];
569 slot
= path
->slots
[level
];
570 BUG_ON(nritems
== 0);
572 parent_slot
= path
->slots
[level
+ 1];
573 btrfs_node_key(parent
, &parent_key
, parent_slot
);
574 btrfs_node_key(node
, &node_key
, 0);
575 BUG_ON(memcmp(&parent_key
, &node_key
,
576 sizeof(struct btrfs_disk_key
)));
577 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
578 btrfs_header_bytenr(node
));
580 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
582 btrfs_node_key_to_cpu(node
, &cpukey
, slot
- 1);
583 btrfs_node_key(node
, &node_key
, slot
);
584 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) <= 0);
586 if (slot
< nritems
- 1) {
587 btrfs_node_key_to_cpu(node
, &cpukey
, slot
+ 1);
588 btrfs_node_key(node
, &node_key
, slot
);
589 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) >= 0);
594 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
597 struct extent_buffer
*leaf
= path
->nodes
[level
];
598 struct extent_buffer
*parent
= NULL
;
600 struct btrfs_key cpukey
;
601 struct btrfs_disk_key parent_key
;
602 struct btrfs_disk_key leaf_key
;
603 int slot
= path
->slots
[0];
605 u32 nritems
= btrfs_header_nritems(leaf
);
607 if (path
->nodes
[level
+ 1])
608 parent
= path
->nodes
[level
+ 1];
614 parent_slot
= path
->slots
[level
+ 1];
615 btrfs_node_key(parent
, &parent_key
, parent_slot
);
616 btrfs_item_key(leaf
, &leaf_key
, 0);
618 BUG_ON(memcmp(&parent_key
, &leaf_key
,
619 sizeof(struct btrfs_disk_key
)));
620 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
621 btrfs_header_bytenr(leaf
));
624 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
625 btrfs_item_key_to_cpu(leaf
, &cpukey
, i
+ 1);
626 btrfs_item_key(leaf
, &leaf_key
, i
);
627 if (comp_keys(&leaf_key
, &cpukey
) >= 0) {
628 btrfs_print_leaf(root
, leaf
);
629 printk("slot %d offset bad key\n", i
);
632 if (btrfs_item_offset_nr(leaf
, i
) !=
633 btrfs_item_end_nr(leaf
, i
+ 1)) {
634 btrfs_print_leaf(root
, leaf
);
635 printk("slot %d offset bad\n", i
);
639 if (btrfs_item_offset_nr(leaf
, i
) +
640 btrfs_item_size_nr(leaf
, i
) !=
641 BTRFS_LEAF_DATA_SIZE(root
)) {
642 btrfs_print_leaf(root
, leaf
);
643 printk("slot %d first offset bad\n", i
);
649 if (btrfs_item_size_nr(leaf
, nritems
- 1) > 4096) {
650 btrfs_print_leaf(root
, leaf
);
651 printk("slot %d bad size \n", nritems
- 1);
656 if (slot
!= 0 && slot
< nritems
- 1) {
657 btrfs_item_key(leaf
, &leaf_key
, slot
);
658 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
- 1);
659 if (btrfs_comp_keys(&leaf_key
, &cpukey
) <= 0) {
660 btrfs_print_leaf(root
, leaf
);
661 printk("slot %d offset bad key\n", slot
);
664 if (btrfs_item_offset_nr(leaf
, slot
- 1) !=
665 btrfs_item_end_nr(leaf
, slot
)) {
666 btrfs_print_leaf(root
, leaf
);
667 printk("slot %d offset bad\n", slot
);
671 if (slot
< nritems
- 1) {
672 btrfs_item_key(leaf
, &leaf_key
, slot
);
673 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
+ 1);
674 BUG_ON(btrfs_comp_keys(&leaf_key
, &cpukey
) >= 0);
675 if (btrfs_item_offset_nr(leaf
, slot
) !=
676 btrfs_item_end_nr(leaf
, slot
+ 1)) {
677 btrfs_print_leaf(root
, leaf
);
678 printk("slot %d offset bad\n", slot
);
682 BUG_ON(btrfs_item_offset_nr(leaf
, 0) +
683 btrfs_item_size_nr(leaf
, 0) != BTRFS_LEAF_DATA_SIZE(root
));
687 static int noinline
check_block(struct btrfs_root
*root
,
688 struct btrfs_path
*path
, int level
)
692 struct extent_buffer
*buf
= path
->nodes
[level
];
694 if (memcmp_extent_buffer(buf
, root
->fs_info
->fsid
,
695 (unsigned long)btrfs_header_fsid(buf
),
697 printk("warning bad block %Lu\n", buf
->start
);
702 return check_leaf(root
, path
, level
);
703 return check_node(root
, path
, level
);
707 * search for key in the extent_buffer. The items start at offset p,
708 * and they are item_size apart. There are 'max' items in p.
710 * the slot in the array is returned via slot, and it points to
711 * the place where you would insert key if it is not found in
714 * slot may point to max if the key is bigger than all of the keys
716 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
717 int item_size
, struct btrfs_key
*key
,
724 unsigned long offset
;
725 struct btrfs_disk_key
*tmp
;
728 mid
= (low
+ high
) / 2;
729 offset
= p
+ mid
* item_size
;
731 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
732 ret
= btrfs_comp_keys(tmp
, key
);
748 * simple bin_search frontend that does the right thing for
751 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
752 int level
, int *slot
)
755 return generic_bin_search(eb
,
756 offsetof(struct btrfs_leaf
, items
),
757 sizeof(struct btrfs_item
),
758 key
, btrfs_header_nritems(eb
),
761 return generic_bin_search(eb
,
762 offsetof(struct btrfs_node
, ptrs
),
763 sizeof(struct btrfs_key_ptr
),
764 key
, btrfs_header_nritems(eb
),
770 static struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
771 struct extent_buffer
*parent
, int slot
)
773 int level
= btrfs_header_level(parent
);
776 if (slot
>= btrfs_header_nritems(parent
))
781 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
782 btrfs_level_size(root
, level
- 1),
783 btrfs_node_ptr_generation(parent
, slot
));
786 static int balance_level(struct btrfs_trans_handle
*trans
,
787 struct btrfs_root
*root
,
788 struct btrfs_path
*path
, int level
)
790 struct extent_buffer
*right
= NULL
;
791 struct extent_buffer
*mid
;
792 struct extent_buffer
*left
= NULL
;
793 struct extent_buffer
*parent
= NULL
;
797 int orig_slot
= path
->slots
[level
];
798 int err_on_enospc
= 0;
804 mid
= path
->nodes
[level
];
805 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
807 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
809 if (level
< BTRFS_MAX_LEVEL
- 1)
810 parent
= path
->nodes
[level
+ 1];
811 pslot
= path
->slots
[level
+ 1];
814 * deal with the case where there is only one pointer in the root
815 * by promoting the node below to a root
818 struct extent_buffer
*child
;
820 if (btrfs_header_nritems(mid
) != 1)
823 /* promote the child to a root */
824 child
= read_node_slot(root
, mid
, 0);
826 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
830 add_root_to_dirty_list(root
);
831 path
->nodes
[level
] = NULL
;
832 clean_tree_block(trans
, root
, mid
);
833 wait_on_tree_block_writeback(root
, mid
);
834 /* once for the path */
835 free_extent_buffer(mid
);
837 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
838 0, root
->root_key
.objectid
,
840 /* once for the root ptr */
841 free_extent_buffer(mid
);
844 if (btrfs_header_nritems(mid
) >
845 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
848 if (btrfs_header_nritems(mid
) < 2)
851 left
= read_node_slot(root
, parent
, pslot
- 1);
853 wret
= btrfs_cow_block(trans
, root
, left
,
854 parent
, pslot
- 1, &left
);
860 right
= read_node_slot(root
, parent
, pslot
+ 1);
862 wret
= btrfs_cow_block(trans
, root
, right
,
863 parent
, pslot
+ 1, &right
);
870 /* first, try to make some room in the middle buffer */
872 orig_slot
+= btrfs_header_nritems(left
);
873 wret
= push_node_left(trans
, root
, left
, mid
, 1);
876 if (btrfs_header_nritems(mid
) < 2)
881 * then try to empty the right most buffer into the middle
884 wret
= push_node_left(trans
, root
, mid
, right
, 1);
885 if (wret
< 0 && wret
!= -ENOSPC
)
887 if (btrfs_header_nritems(right
) == 0) {
888 u64 bytenr
= right
->start
;
889 u32 blocksize
= right
->len
;
891 clean_tree_block(trans
, root
, right
);
892 wait_on_tree_block_writeback(root
, right
);
893 free_extent_buffer(right
);
895 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
899 wret
= btrfs_free_extent(trans
, root
, bytenr
,
901 root
->root_key
.objectid
,
906 struct btrfs_disk_key right_key
;
907 btrfs_node_key(right
, &right_key
, 0);
908 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
909 btrfs_mark_buffer_dirty(parent
);
912 if (btrfs_header_nritems(mid
) == 1) {
914 * we're not allowed to leave a node with one item in the
915 * tree during a delete. A deletion from lower in the tree
916 * could try to delete the only pointer in this node.
917 * So, pull some keys from the left.
918 * There has to be a left pointer at this point because
919 * otherwise we would have pulled some pointers from the
923 wret
= balance_node_right(trans
, root
, mid
, left
);
929 wret
= push_node_left(trans
, root
, left
, mid
, 1);
935 if (btrfs_header_nritems(mid
) == 0) {
936 /* we've managed to empty the middle node, drop it */
937 u64 bytenr
= mid
->start
;
938 u32 blocksize
= mid
->len
;
939 clean_tree_block(trans
, root
, mid
);
940 wait_on_tree_block_writeback(root
, mid
);
941 free_extent_buffer(mid
);
943 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
946 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
947 0, root
->root_key
.objectid
,
952 /* update the parent key to reflect our changes */
953 struct btrfs_disk_key mid_key
;
954 btrfs_node_key(mid
, &mid_key
, 0);
955 btrfs_set_node_key(parent
, &mid_key
, pslot
);
956 btrfs_mark_buffer_dirty(parent
);
959 /* update the path */
961 if (btrfs_header_nritems(left
) > orig_slot
) {
962 extent_buffer_get(left
);
963 path
->nodes
[level
] = left
;
964 path
->slots
[level
+ 1] -= 1;
965 path
->slots
[level
] = orig_slot
;
967 free_extent_buffer(mid
);
969 orig_slot
-= btrfs_header_nritems(left
);
970 path
->slots
[level
] = orig_slot
;
973 /* double check we haven't messed things up */
974 check_block(root
, path
, level
);
976 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
980 free_extent_buffer(right
);
982 free_extent_buffer(left
);
986 /* returns zero if the push worked, non-zero otherwise */
987 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
988 struct btrfs_root
*root
,
989 struct btrfs_path
*path
, int level
)
991 struct extent_buffer
*right
= NULL
;
992 struct extent_buffer
*mid
;
993 struct extent_buffer
*left
= NULL
;
994 struct extent_buffer
*parent
= NULL
;
998 int orig_slot
= path
->slots
[level
];
1004 mid
= path
->nodes
[level
];
1005 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1006 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
1008 if (level
< BTRFS_MAX_LEVEL
- 1)
1009 parent
= path
->nodes
[level
+ 1];
1010 pslot
= path
->slots
[level
+ 1];
1015 left
= read_node_slot(root
, parent
, pslot
- 1);
1017 /* first, try to make some room in the middle buffer */
1020 left_nr
= btrfs_header_nritems(left
);
1021 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1024 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
1029 wret
= push_node_left(trans
, root
,
1036 struct btrfs_disk_key disk_key
;
1037 orig_slot
+= left_nr
;
1038 btrfs_node_key(mid
, &disk_key
, 0);
1039 btrfs_set_node_key(parent
, &disk_key
, pslot
);
1040 btrfs_mark_buffer_dirty(parent
);
1041 if (btrfs_header_nritems(left
) > orig_slot
) {
1042 path
->nodes
[level
] = left
;
1043 path
->slots
[level
+ 1] -= 1;
1044 path
->slots
[level
] = orig_slot
;
1045 free_extent_buffer(mid
);
1048 btrfs_header_nritems(left
);
1049 path
->slots
[level
] = orig_slot
;
1050 free_extent_buffer(left
);
1054 free_extent_buffer(left
);
1056 right
= read_node_slot(root
, parent
, pslot
+ 1);
1059 * then try to empty the right most buffer into the middle
1063 right_nr
= btrfs_header_nritems(right
);
1064 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1067 ret
= btrfs_cow_block(trans
, root
, right
,
1073 wret
= balance_node_right(trans
, root
,
1080 struct btrfs_disk_key disk_key
;
1082 btrfs_node_key(right
, &disk_key
, 0);
1083 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
1084 btrfs_mark_buffer_dirty(parent
);
1086 if (btrfs_header_nritems(mid
) <= orig_slot
) {
1087 path
->nodes
[level
] = right
;
1088 path
->slots
[level
+ 1] += 1;
1089 path
->slots
[level
] = orig_slot
-
1090 btrfs_header_nritems(mid
);
1091 free_extent_buffer(mid
);
1093 free_extent_buffer(right
);
1097 free_extent_buffer(right
);
1103 * readahead one full node of leaves
1105 static void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
1106 int level
, int slot
, u64 objectid
)
1108 struct extent_buffer
*node
;
1109 struct btrfs_disk_key disk_key
;
1115 int direction
= path
->reada
;
1116 struct extent_buffer
*eb
;
1124 if (!path
->nodes
[level
])
1127 node
= path
->nodes
[level
];
1128 search
= btrfs_node_blockptr(node
, slot
);
1129 blocksize
= btrfs_level_size(root
, level
- 1);
1130 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1132 free_extent_buffer(eb
);
1136 highest_read
= search
;
1137 lowest_read
= search
;
1139 nritems
= btrfs_header_nritems(node
);
1142 if (direction
< 0) {
1146 } else if (direction
> 0) {
1151 if (path
->reada
< 0 && objectid
) {
1152 btrfs_node_key(node
, &disk_key
, nr
);
1153 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1156 search
= btrfs_node_blockptr(node
, nr
);
1157 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1158 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1159 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1160 readahead_tree_block(root
, search
, blocksize
,
1161 btrfs_node_ptr_generation(node
, nr
));
1165 if (path
->reada
< 2 && (nread
> (256 * 1024) || nscan
> 32))
1167 if(nread
> (1024 * 1024) || nscan
> 128)
1170 if (search
< lowest_read
)
1171 lowest_read
= search
;
1172 if (search
> highest_read
)
1173 highest_read
= search
;
1178 * look for key in the tree. path is filled in with nodes along the way
1179 * if key is found, we return zero and you can find the item in the leaf
1180 * level of the path (level 0)
1182 * If the key isn't found, the path points to the slot where it should
1183 * be inserted, and 1 is returned. If there are other errors during the
1184 * search a negative error number is returned.
1186 * if ins_len > 0, nodes and leaves will be split as we walk down the
1187 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1190 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1191 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1194 struct extent_buffer
*b
;
1198 int should_reada
= p
->reada
;
1199 u8 lowest_level
= 0;
1201 lowest_level
= p
->lowest_level
;
1202 WARN_ON(lowest_level
&& ins_len
);
1203 WARN_ON(p
->nodes
[0] != NULL
);
1205 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1209 extent_buffer_get(b
);
1211 level
= btrfs_header_level(b
);
1214 wret
= btrfs_cow_block(trans
, root
, b
,
1215 p
->nodes
[level
+ 1],
1216 p
->slots
[level
+ 1],
1219 free_extent_buffer(b
);
1223 BUG_ON(!cow
&& ins_len
);
1224 if (level
!= btrfs_header_level(b
))
1226 level
= btrfs_header_level(b
);
1227 p
->nodes
[level
] = b
;
1228 ret
= check_block(root
, p
, level
);
1231 ret
= bin_search(b
, key
, level
, &slot
);
1233 if (ret
&& slot
> 0)
1235 p
->slots
[level
] = slot
;
1236 if ((p
->search_for_split
|| ins_len
> 0) &&
1237 btrfs_header_nritems(b
) >=
1238 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1239 int sret
= split_node(trans
, root
, p
, level
);
1243 b
= p
->nodes
[level
];
1244 slot
= p
->slots
[level
];
1245 } else if (ins_len
< 0) {
1246 int sret
= balance_level(trans
, root
, p
,
1250 b
= p
->nodes
[level
];
1252 btrfs_release_path(NULL
, p
);
1255 slot
= p
->slots
[level
];
1256 BUG_ON(btrfs_header_nritems(b
) == 1);
1258 /* this is only true while dropping a snapshot */
1259 if (level
== lowest_level
)
1263 reada_for_search(root
, p
, level
, slot
,
1266 b
= read_node_slot(root
, b
, slot
);
1268 p
->slots
[level
] = slot
;
1270 ins_len
> btrfs_leaf_free_space(root
, b
)) {
1271 int sret
= split_leaf(trans
, root
, key
,
1272 p
, ins_len
, ret
== 0);
1284 * adjust the pointers going up the tree, starting at level
1285 * making sure the right key of each node is points to 'key'.
1286 * This is used after shifting pointers to the left, so it stops
1287 * fixing up pointers when a given leaf/node is not in slot 0 of the
1290 * If this fails to write a tree block, it returns -1, but continues
1291 * fixing up the blocks in ram so the tree is consistent.
1293 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1294 struct btrfs_root
*root
, struct btrfs_path
*path
,
1295 struct btrfs_disk_key
*key
, int level
)
1299 struct extent_buffer
*t
;
1301 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1302 int tslot
= path
->slots
[i
];
1303 if (!path
->nodes
[i
])
1306 btrfs_set_node_key(t
, key
, tslot
);
1307 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1317 * This function isn't completely safe. It's the caller's responsibility
1318 * that the new key won't break the order
1320 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1321 struct btrfs_root
*root
, struct btrfs_path
*path
,
1322 struct btrfs_key
*new_key
)
1324 struct btrfs_disk_key disk_key
;
1325 struct extent_buffer
*eb
;
1328 eb
= path
->nodes
[0];
1329 slot
= path
->slots
[0];
1331 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1332 if (btrfs_comp_keys(&disk_key
, new_key
) >= 0)
1335 if (slot
< btrfs_header_nritems(eb
) - 1) {
1336 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1337 if (btrfs_comp_keys(&disk_key
, new_key
) <= 0)
1341 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1342 btrfs_set_item_key(eb
, &disk_key
, slot
);
1343 btrfs_mark_buffer_dirty(eb
);
1345 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1350 * try to push data from one node into the next node left in the
1353 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1354 * error, and > 0 if there was no room in the left hand block.
1356 static int push_node_left(struct btrfs_trans_handle
*trans
,
1357 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1358 struct extent_buffer
*src
, int empty
)
1365 src_nritems
= btrfs_header_nritems(src
);
1366 dst_nritems
= btrfs_header_nritems(dst
);
1367 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1368 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1369 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1371 if (!empty
&& src_nritems
<= 8)
1374 if (push_items
<= 0) {
1379 push_items
= min(src_nritems
, push_items
);
1380 if (push_items
< src_nritems
) {
1381 /* leave at least 8 pointers in the node if
1382 * we aren't going to empty it
1384 if (src_nritems
- push_items
< 8) {
1385 if (push_items
<= 8)
1391 push_items
= min(src_nritems
- 8, push_items
);
1393 copy_extent_buffer(dst
, src
,
1394 btrfs_node_key_ptr_offset(dst_nritems
),
1395 btrfs_node_key_ptr_offset(0),
1396 push_items
* sizeof(struct btrfs_key_ptr
));
1398 if (push_items
< src_nritems
) {
1399 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1400 btrfs_node_key_ptr_offset(push_items
),
1401 (src_nritems
- push_items
) *
1402 sizeof(struct btrfs_key_ptr
));
1404 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1405 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1406 btrfs_mark_buffer_dirty(src
);
1407 btrfs_mark_buffer_dirty(dst
);
1413 * try to push data from one node into the next node right in the
1416 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1417 * error, and > 0 if there was no room in the right hand block.
1419 * this will only push up to 1/2 the contents of the left node over
1421 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1422 struct btrfs_root
*root
,
1423 struct extent_buffer
*dst
,
1424 struct extent_buffer
*src
)
1432 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1433 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1435 src_nritems
= btrfs_header_nritems(src
);
1436 dst_nritems
= btrfs_header_nritems(dst
);
1437 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1438 if (push_items
<= 0) {
1442 if (src_nritems
< 4) {
1446 max_push
= src_nritems
/ 2 + 1;
1447 /* don't try to empty the node */
1448 if (max_push
>= src_nritems
) {
1452 if (max_push
< push_items
)
1453 push_items
= max_push
;
1455 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1456 btrfs_node_key_ptr_offset(0),
1458 sizeof(struct btrfs_key_ptr
));
1460 copy_extent_buffer(dst
, src
,
1461 btrfs_node_key_ptr_offset(0),
1462 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1463 push_items
* sizeof(struct btrfs_key_ptr
));
1465 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1466 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1468 btrfs_mark_buffer_dirty(src
);
1469 btrfs_mark_buffer_dirty(dst
);
1475 * helper function to insert a new root level in the tree.
1476 * A new node is allocated, and a single item is inserted to
1477 * point to the existing root
1479 * returns zero on success or < 0 on failure.
1481 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1482 struct btrfs_root
*root
,
1483 struct btrfs_path
*path
, int level
)
1486 struct extent_buffer
*lower
;
1487 struct extent_buffer
*c
;
1488 struct extent_buffer
*old
;
1489 struct btrfs_disk_key lower_key
;
1491 BUG_ON(path
->nodes
[level
]);
1492 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1494 lower
= path
->nodes
[level
-1];
1496 btrfs_item_key(lower
, &lower_key
, 0);
1498 btrfs_node_key(lower
, &lower_key
, 0);
1500 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1501 root
->root_key
.objectid
, &lower_key
,
1502 level
, root
->node
->start
, 0);
1507 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1508 btrfs_set_header_nritems(c
, 1);
1509 btrfs_set_header_level(c
, level
);
1510 btrfs_set_header_bytenr(c
, c
->start
);
1511 btrfs_set_header_generation(c
, trans
->transid
);
1512 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1513 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1515 write_extent_buffer(c
, root
->fs_info
->fsid
,
1516 (unsigned long)btrfs_header_fsid(c
),
1519 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1520 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
1523 btrfs_set_node_key(c
, &lower_key
, 0);
1524 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1525 lower_gen
= btrfs_header_generation(lower
);
1526 WARN_ON(lower_gen
!= trans
->transid
);
1528 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1530 btrfs_mark_buffer_dirty(c
);
1535 /* the super has an extra ref to root->node */
1536 free_extent_buffer(old
);
1538 add_root_to_dirty_list(root
);
1539 extent_buffer_get(c
);
1540 path
->nodes
[level
] = c
;
1541 path
->slots
[level
] = 0;
1546 * worker function to insert a single pointer in a node.
1547 * the node should have enough room for the pointer already
1549 * slot and level indicate where you want the key to go, and
1550 * blocknr is the block the key points to.
1552 * returns zero on success and < 0 on any error
1554 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1555 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1556 *key
, u64 bytenr
, int slot
, int level
)
1558 struct extent_buffer
*lower
;
1561 BUG_ON(!path
->nodes
[level
]);
1562 lower
= path
->nodes
[level
];
1563 nritems
= btrfs_header_nritems(lower
);
1566 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
1568 if (slot
!= nritems
) {
1569 memmove_extent_buffer(lower
,
1570 btrfs_node_key_ptr_offset(slot
+ 1),
1571 btrfs_node_key_ptr_offset(slot
),
1572 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1574 btrfs_set_node_key(lower
, key
, slot
);
1575 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1576 WARN_ON(trans
->transid
== 0);
1577 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1578 btrfs_set_header_nritems(lower
, nritems
+ 1);
1579 btrfs_mark_buffer_dirty(lower
);
1584 * split the node at the specified level in path in two.
1585 * The path is corrected to point to the appropriate node after the split
1587 * Before splitting this tries to make some room in the node by pushing
1588 * left and right, if either one works, it returns right away.
1590 * returns 0 on success and < 0 on failure
1592 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1593 *root
, struct btrfs_path
*path
, int level
)
1595 struct extent_buffer
*c
;
1596 struct extent_buffer
*split
;
1597 struct btrfs_disk_key disk_key
;
1603 c
= path
->nodes
[level
];
1604 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1605 if (c
== root
->node
) {
1606 /* trying to split the root, lets make a new one */
1607 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1611 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1612 c
= path
->nodes
[level
];
1613 if (!ret
&& btrfs_header_nritems(c
) <
1614 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
1620 c_nritems
= btrfs_header_nritems(c
);
1621 mid
= (c_nritems
+ 1) / 2;
1622 btrfs_node_key(c
, &disk_key
, mid
);
1624 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1625 root
->root_key
.objectid
,
1626 &disk_key
, level
, c
->start
, 0);
1628 return PTR_ERR(split
);
1630 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
1631 btrfs_set_header_level(split
, btrfs_header_level(c
));
1632 btrfs_set_header_bytenr(split
, split
->start
);
1633 btrfs_set_header_generation(split
, trans
->transid
);
1634 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
1635 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1636 write_extent_buffer(split
, root
->fs_info
->fsid
,
1637 (unsigned long)btrfs_header_fsid(split
),
1639 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1640 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
1644 copy_extent_buffer(split
, c
,
1645 btrfs_node_key_ptr_offset(0),
1646 btrfs_node_key_ptr_offset(mid
),
1647 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1648 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1649 btrfs_set_header_nritems(c
, mid
);
1652 btrfs_mark_buffer_dirty(c
);
1653 btrfs_mark_buffer_dirty(split
);
1655 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1656 path
->slots
[level
+ 1] + 1,
1661 if (path
->slots
[level
] >= mid
) {
1662 path
->slots
[level
] -= mid
;
1663 free_extent_buffer(c
);
1664 path
->nodes
[level
] = split
;
1665 path
->slots
[level
+ 1] += 1;
1667 free_extent_buffer(split
);
1673 * how many bytes are required to store the items in a leaf. start
1674 * and nr indicate which items in the leaf to check. This totals up the
1675 * space used both by the item structs and the item data
1677 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1680 int nritems
= btrfs_header_nritems(l
);
1681 int end
= min(nritems
, start
+ nr
) - 1;
1685 data_len
= btrfs_item_end_nr(l
, start
);
1686 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1687 data_len
+= sizeof(struct btrfs_item
) * nr
;
1688 WARN_ON(data_len
< 0);
1693 * The space between the end of the leaf items and
1694 * the start of the leaf data. IOW, how much room
1695 * the leaf has left for both items and data
1697 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1699 int nritems
= btrfs_header_nritems(leaf
);
1701 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
1703 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1704 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
1705 leaf_space_used(leaf
, 0, nritems
), nritems
);
1711 * push some data in the path leaf to the right, trying to free up at
1712 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1714 * returns 1 if the push failed because the other node didn't have enough
1715 * room, 0 if everything worked out and < 0 if there were major errors.
1717 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1718 *root
, struct btrfs_path
*path
, int data_size
,
1721 struct extent_buffer
*left
= path
->nodes
[0];
1722 struct extent_buffer
*right
;
1723 struct extent_buffer
*upper
;
1724 struct btrfs_disk_key disk_key
;
1730 struct btrfs_item
*item
;
1738 slot
= path
->slots
[1];
1739 if (!path
->nodes
[1]) {
1742 upper
= path
->nodes
[1];
1743 if (slot
>= btrfs_header_nritems(upper
) - 1)
1746 right
= read_node_slot(root
, upper
, slot
+ 1);
1747 free_space
= btrfs_leaf_free_space(root
, right
);
1748 if (free_space
< data_size
) {
1749 free_extent_buffer(right
);
1753 /* cow and double check */
1754 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1757 free_extent_buffer(right
);
1760 free_space
= btrfs_leaf_free_space(root
, right
);
1761 if (free_space
< data_size
) {
1762 free_extent_buffer(right
);
1766 left_nritems
= btrfs_header_nritems(left
);
1767 if (left_nritems
== 0) {
1768 free_extent_buffer(right
);
1777 i
= left_nritems
- 1;
1779 item
= btrfs_item_nr(left
, i
);
1781 if (path
->slots
[0] == i
)
1782 push_space
+= data_size
+ sizeof(*item
);
1784 this_item_size
= btrfs_item_size(left
, item
);
1785 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1788 push_space
+= this_item_size
+ sizeof(*item
);
1794 if (push_items
== 0) {
1795 free_extent_buffer(right
);
1799 if (!empty
&& push_items
== left_nritems
)
1802 /* push left to right */
1803 right_nritems
= btrfs_header_nritems(right
);
1805 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1806 push_space
-= leaf_data_end(root
, left
);
1808 /* make room in the right data area */
1809 data_end
= leaf_data_end(root
, right
);
1810 memmove_extent_buffer(right
,
1811 btrfs_leaf_data(right
) + data_end
- push_space
,
1812 btrfs_leaf_data(right
) + data_end
,
1813 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
1815 /* copy from the left data area */
1816 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1817 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1818 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1821 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1822 btrfs_item_nr_offset(0),
1823 right_nritems
* sizeof(struct btrfs_item
));
1825 /* copy the items from left to right */
1826 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1827 btrfs_item_nr_offset(left_nritems
- push_items
),
1828 push_items
* sizeof(struct btrfs_item
));
1830 /* update the item pointers */
1831 right_nritems
+= push_items
;
1832 btrfs_set_header_nritems(right
, right_nritems
);
1833 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1834 for (i
= 0; i
< right_nritems
; i
++) {
1835 item
= btrfs_item_nr(right
, i
);
1836 push_space
-= btrfs_item_size(right
, item
);
1837 btrfs_set_item_offset(right
, item
, push_space
);
1840 left_nritems
-= push_items
;
1841 btrfs_set_header_nritems(left
, left_nritems
);
1844 btrfs_mark_buffer_dirty(left
);
1845 btrfs_mark_buffer_dirty(right
);
1847 btrfs_item_key(right
, &disk_key
, 0);
1848 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1849 btrfs_mark_buffer_dirty(upper
);
1851 /* then fixup the leaf pointer in the path */
1852 if (path
->slots
[0] >= left_nritems
) {
1853 path
->slots
[0] -= left_nritems
;
1854 free_extent_buffer(path
->nodes
[0]);
1855 path
->nodes
[0] = right
;
1856 path
->slots
[1] += 1;
1858 free_extent_buffer(right
);
1863 * push some data in the path leaf to the left, trying to free up at
1864 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1866 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1867 *root
, struct btrfs_path
*path
, int data_size
,
1870 struct btrfs_disk_key disk_key
;
1871 struct extent_buffer
*right
= path
->nodes
[0];
1872 struct extent_buffer
*left
;
1878 struct btrfs_item
*item
;
1879 u32 old_left_nritems
;
1885 u32 old_left_item_size
;
1887 slot
= path
->slots
[1];
1890 if (!path
->nodes
[1])
1893 right_nritems
= btrfs_header_nritems(right
);
1894 if (right_nritems
== 0) {
1898 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
1899 free_space
= btrfs_leaf_free_space(root
, left
);
1900 if (free_space
< data_size
) {
1901 free_extent_buffer(left
);
1905 /* cow and double check */
1906 ret
= btrfs_cow_block(trans
, root
, left
,
1907 path
->nodes
[1], slot
- 1, &left
);
1909 /* we hit -ENOSPC, but it isn't fatal here */
1910 free_extent_buffer(left
);
1914 free_space
= btrfs_leaf_free_space(root
, left
);
1915 if (free_space
< data_size
) {
1916 free_extent_buffer(left
);
1923 nr
= right_nritems
- 1;
1925 for (i
= 0; i
< nr
; i
++) {
1926 item
= btrfs_item_nr(right
, i
);
1928 if (path
->slots
[0] == i
)
1929 push_space
+= data_size
+ sizeof(*item
);
1931 this_item_size
= btrfs_item_size(right
, item
);
1932 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1936 push_space
+= this_item_size
+ sizeof(*item
);
1939 if (push_items
== 0) {
1940 free_extent_buffer(left
);
1943 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1946 /* push data from right to left */
1947 copy_extent_buffer(left
, right
,
1948 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1949 btrfs_item_nr_offset(0),
1950 push_items
* sizeof(struct btrfs_item
));
1952 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1953 btrfs_item_offset_nr(right
, push_items
-1);
1955 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1956 leaf_data_end(root
, left
) - push_space
,
1957 btrfs_leaf_data(right
) +
1958 btrfs_item_offset_nr(right
, push_items
- 1),
1960 old_left_nritems
= btrfs_header_nritems(left
);
1961 BUG_ON(old_left_nritems
< 0);
1963 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1964 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1967 item
= btrfs_item_nr(left
, i
);
1968 ioff
= btrfs_item_offset(left
, item
);
1969 btrfs_set_item_offset(left
, item
,
1970 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
1972 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1974 /* fixup right node */
1975 if (push_items
> right_nritems
) {
1976 printk("push items %d nr %u\n", push_items
, right_nritems
);
1980 if (push_items
< right_nritems
) {
1981 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1982 leaf_data_end(root
, right
);
1983 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1984 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1985 btrfs_leaf_data(right
) +
1986 leaf_data_end(root
, right
), push_space
);
1988 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1989 btrfs_item_nr_offset(push_items
),
1990 (btrfs_header_nritems(right
) - push_items
) *
1991 sizeof(struct btrfs_item
));
1993 right_nritems
-= push_items
;
1994 btrfs_set_header_nritems(right
, right_nritems
);
1995 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1996 for (i
= 0; i
< right_nritems
; i
++) {
1997 item
= btrfs_item_nr(right
, i
);
1998 push_space
= push_space
- btrfs_item_size(right
, item
);
1999 btrfs_set_item_offset(right
, item
, push_space
);
2002 btrfs_mark_buffer_dirty(left
);
2004 btrfs_mark_buffer_dirty(right
);
2006 btrfs_item_key(right
, &disk_key
, 0);
2007 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2011 /* then fixup the leaf pointer in the path */
2012 if (path
->slots
[0] < push_items
) {
2013 path
->slots
[0] += old_left_nritems
;
2014 free_extent_buffer(path
->nodes
[0]);
2015 path
->nodes
[0] = left
;
2016 path
->slots
[1] -= 1;
2018 free_extent_buffer(left
);
2019 path
->slots
[0] -= push_items
;
2021 BUG_ON(path
->slots
[0] < 0);
2026 * split the path's leaf in two, making sure there is at least data_size
2027 * available for the resulting leaf level of the path.
2029 * returns 0 if all went well and < 0 on failure.
2031 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
2032 struct btrfs_root
*root
,
2033 struct btrfs_path
*path
,
2034 struct extent_buffer
*l
,
2035 struct extent_buffer
*right
,
2036 int slot
, int mid
, int nritems
)
2043 struct btrfs_disk_key disk_key
;
2045 nritems
= nritems
- mid
;
2046 btrfs_set_header_nritems(right
, nritems
);
2047 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2049 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2050 btrfs_item_nr_offset(mid
),
2051 nritems
* sizeof(struct btrfs_item
));
2053 copy_extent_buffer(right
, l
,
2054 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2055 data_copy_size
, btrfs_leaf_data(l
) +
2056 leaf_data_end(root
, l
), data_copy_size
);
2058 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2059 btrfs_item_end_nr(l
, mid
);
2061 for (i
= 0; i
< nritems
; i
++) {
2062 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2063 u32 ioff
= btrfs_item_offset(right
, item
);
2064 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2067 btrfs_set_header_nritems(l
, mid
);
2069 btrfs_item_key(right
, &disk_key
, 0);
2070 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2071 path
->slots
[1] + 1, 1);
2075 btrfs_mark_buffer_dirty(right
);
2076 btrfs_mark_buffer_dirty(l
);
2077 BUG_ON(path
->slots
[0] != slot
);
2080 free_extent_buffer(path
->nodes
[0]);
2081 path
->nodes
[0] = right
;
2082 path
->slots
[0] -= mid
;
2083 path
->slots
[1] += 1;
2085 free_extent_buffer(right
);
2088 BUG_ON(path
->slots
[0] < 0);
2094 * split the path's leaf in two, making sure there is at least data_size
2095 * available for the resulting leaf level of the path.
2097 * returns 0 if all went well and < 0 on failure.
2099 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2100 struct btrfs_root
*root
,
2101 struct btrfs_key
*ins_key
,
2102 struct btrfs_path
*path
, int data_size
,
2105 struct btrfs_disk_key disk_key
;
2106 struct extent_buffer
*l
;
2110 struct extent_buffer
*right
;
2114 int num_doubles
= 0;
2116 /* first try to make some room by pushing left and right */
2117 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2118 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2122 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2128 /* did the pushes work? */
2129 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2133 if (!path
->nodes
[1]) {
2134 ret
= insert_new_root(trans
, root
, path
, 1);
2141 slot
= path
->slots
[0];
2142 nritems
= btrfs_header_nritems(l
);
2143 mid
= (nritems
+ 1) / 2;
2147 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2148 BTRFS_LEAF_DATA_SIZE(root
)) {
2149 if (slot
>= nritems
) {
2153 if (mid
!= nritems
&&
2154 leaf_space_used(l
, mid
, nritems
- mid
) +
2155 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2161 if (leaf_space_used(l
, 0, mid
) + data_size
>
2162 BTRFS_LEAF_DATA_SIZE(root
)) {
2163 if (!extend
&& data_size
&& slot
== 0) {
2165 } else if ((extend
|| !data_size
) && slot
== 0) {
2169 if (mid
!= nritems
&&
2170 leaf_space_used(l
, mid
, nritems
- mid
) +
2171 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2179 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2181 btrfs_item_key(l
, &disk_key
, mid
);
2183 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2184 root
->root_key
.objectid
,
2185 &disk_key
, 0, l
->start
, 0);
2186 if (IS_ERR(right
)) {
2188 return PTR_ERR(right
);
2191 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2192 btrfs_set_header_bytenr(right
, right
->start
);
2193 btrfs_set_header_generation(right
, trans
->transid
);
2194 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2195 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2196 btrfs_set_header_level(right
, 0);
2197 write_extent_buffer(right
, root
->fs_info
->fsid
,
2198 (unsigned long)btrfs_header_fsid(right
),
2201 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2202 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
2207 btrfs_set_header_nritems(right
, 0);
2208 wret
= insert_ptr(trans
, root
, path
,
2209 &disk_key
, right
->start
,
2210 path
->slots
[1] + 1, 1);
2214 free_extent_buffer(path
->nodes
[0]);
2215 path
->nodes
[0] = right
;
2217 path
->slots
[1] += 1;
2219 btrfs_set_header_nritems(right
, 0);
2220 wret
= insert_ptr(trans
, root
, path
,
2226 free_extent_buffer(path
->nodes
[0]);
2227 path
->nodes
[0] = right
;
2229 if (path
->slots
[1] == 0) {
2230 wret
= fixup_low_keys(trans
, root
,
2231 path
, &disk_key
, 1);
2236 btrfs_mark_buffer_dirty(right
);
2240 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2244 BUG_ON(num_doubles
!= 0);
2253 * This function splits a single item into two items,
2254 * giving 'new_key' to the new item and splitting the
2255 * old one at split_offset (from the start of the item).
2257 * The path may be released by this operation. After
2258 * the split, the path is pointing to the old item. The
2259 * new item is going to be in the same node as the old one.
2261 * Note, the item being split must be smaller enough to live alone on
2262 * a tree block with room for one extra struct btrfs_item
2264 * This allows us to split the item in place, keeping a lock on the
2265 * leaf the entire time.
2267 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2268 struct btrfs_root
*root
,
2269 struct btrfs_path
*path
,
2270 struct btrfs_key
*new_key
,
2271 unsigned long split_offset
)
2274 struct extent_buffer
*leaf
;
2275 struct btrfs_key orig_key
;
2276 struct btrfs_item
*item
;
2277 struct btrfs_item
*new_item
;
2282 struct btrfs_disk_key disk_key
;
2285 leaf
= path
->nodes
[0];
2286 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
2287 if (btrfs_leaf_free_space(root
, leaf
) >= sizeof(struct btrfs_item
))
2290 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2291 btrfs_release_path(root
, path
);
2293 path
->search_for_split
= 1;
2295 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
2296 path
->search_for_split
= 0;
2298 /* if our item isn't there or got smaller, return now */
2299 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
2304 ret
= split_leaf(trans
, root
, &orig_key
, path
, 0, 0);
2307 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
2308 leaf
= path
->nodes
[0];
2311 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
2312 orig_offset
= btrfs_item_offset(leaf
, item
);
2313 item_size
= btrfs_item_size(leaf
, item
);
2316 buf
= kmalloc(item_size
, GFP_NOFS
);
2317 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
2318 path
->slots
[0]), item_size
);
2319 slot
= path
->slots
[0] + 1;
2320 leaf
= path
->nodes
[0];
2322 nritems
= btrfs_header_nritems(leaf
);
2324 if (slot
!= nritems
) {
2325 /* shift the items */
2326 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2327 btrfs_item_nr_offset(slot
),
2328 (nritems
- slot
) * sizeof(struct btrfs_item
));
2332 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
2333 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2335 new_item
= btrfs_item_nr(leaf
, slot
);
2337 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
2338 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
2340 btrfs_set_item_offset(leaf
, item
,
2341 orig_offset
+ item_size
- split_offset
);
2342 btrfs_set_item_size(leaf
, item
, split_offset
);
2344 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2346 /* write the data for the start of the original item */
2347 write_extent_buffer(leaf
, buf
,
2348 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2351 /* write the data for the new item */
2352 write_extent_buffer(leaf
, buf
+ split_offset
,
2353 btrfs_item_ptr_offset(leaf
, slot
),
2354 item_size
- split_offset
);
2355 btrfs_mark_buffer_dirty(leaf
);
2358 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2359 btrfs_print_leaf(root
, leaf
);
2366 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
2367 struct btrfs_root
*root
,
2368 struct btrfs_path
*path
,
2369 u32 new_size
, int from_end
)
2374 struct extent_buffer
*leaf
;
2375 struct btrfs_item
*item
;
2377 unsigned int data_end
;
2378 unsigned int old_data_start
;
2379 unsigned int old_size
;
2380 unsigned int size_diff
;
2383 slot_orig
= path
->slots
[0];
2384 leaf
= path
->nodes
[0];
2385 slot
= path
->slots
[0];
2387 old_size
= btrfs_item_size_nr(leaf
, slot
);
2388 if (old_size
== new_size
)
2391 nritems
= btrfs_header_nritems(leaf
);
2392 data_end
= leaf_data_end(root
, leaf
);
2394 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2396 size_diff
= old_size
- new_size
;
2399 BUG_ON(slot
>= nritems
);
2402 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2404 /* first correct the data pointers */
2405 for (i
= slot
; i
< nritems
; i
++) {
2407 item
= btrfs_item_nr(leaf
, i
);
2408 ioff
= btrfs_item_offset(leaf
, item
);
2409 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2412 /* shift the data */
2414 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2415 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2416 data_end
, old_data_start
+ new_size
- data_end
);
2418 struct btrfs_disk_key disk_key
;
2421 btrfs_item_key(leaf
, &disk_key
, slot
);
2423 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2425 struct btrfs_file_extent_item
*fi
;
2427 fi
= btrfs_item_ptr(leaf
, slot
,
2428 struct btrfs_file_extent_item
);
2429 fi
= (struct btrfs_file_extent_item
*)(
2430 (unsigned long)fi
- size_diff
);
2432 if (btrfs_file_extent_type(leaf
, fi
) ==
2433 BTRFS_FILE_EXTENT_INLINE
) {
2434 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2435 memmove_extent_buffer(leaf
, ptr
,
2437 offsetof(struct btrfs_file_extent_item
,
2442 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2443 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2444 data_end
, old_data_start
- data_end
);
2446 offset
= btrfs_disk_key_offset(&disk_key
);
2447 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2448 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2450 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2453 item
= btrfs_item_nr(leaf
, slot
);
2454 btrfs_set_item_size(leaf
, item
, new_size
);
2455 btrfs_mark_buffer_dirty(leaf
);
2458 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2459 btrfs_print_leaf(root
, leaf
);
2465 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
2466 struct btrfs_root
*root
, struct btrfs_path
*path
,
2472 struct extent_buffer
*leaf
;
2473 struct btrfs_item
*item
;
2475 unsigned int data_end
;
2476 unsigned int old_data
;
2477 unsigned int old_size
;
2480 slot_orig
= path
->slots
[0];
2481 leaf
= path
->nodes
[0];
2483 nritems
= btrfs_header_nritems(leaf
);
2484 data_end
= leaf_data_end(root
, leaf
);
2486 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2487 btrfs_print_leaf(root
, leaf
);
2490 slot
= path
->slots
[0];
2491 old_data
= btrfs_item_end_nr(leaf
, slot
);
2494 if (slot
>= nritems
) {
2495 btrfs_print_leaf(root
, leaf
);
2496 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2501 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2503 /* first correct the data pointers */
2504 for (i
= slot
; i
< nritems
; i
++) {
2506 item
= btrfs_item_nr(leaf
, i
);
2507 ioff
= btrfs_item_offset(leaf
, item
);
2508 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2511 /* shift the data */
2512 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2513 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2514 data_end
, old_data
- data_end
);
2516 data_end
= old_data
;
2517 old_size
= btrfs_item_size_nr(leaf
, slot
);
2518 item
= btrfs_item_nr(leaf
, slot
);
2519 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2520 btrfs_mark_buffer_dirty(leaf
);
2523 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2524 btrfs_print_leaf(root
, leaf
);
2531 * Given a key and some data, insert an item into the tree.
2532 * This does all the path init required, making room in the tree if needed.
2534 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2535 struct btrfs_root
*root
,
2536 struct btrfs_path
*path
,
2537 struct btrfs_key
*cpu_key
, u32
*data_size
,
2540 struct extent_buffer
*leaf
;
2541 struct btrfs_item
*item
;
2549 unsigned int data_end
;
2550 struct btrfs_disk_key disk_key
;
2552 for (i
= 0; i
< nr
; i
++) {
2553 total_data
+= data_size
[i
];
2556 /* create a root if there isn't one */
2560 total_size
= total_data
+ nr
* sizeof(struct btrfs_item
);
2561 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2568 slot_orig
= path
->slots
[0];
2569 leaf
= path
->nodes
[0];
2571 nritems
= btrfs_header_nritems(leaf
);
2572 data_end
= leaf_data_end(root
, leaf
);
2574 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
2575 btrfs_print_leaf(root
, leaf
);
2576 printk("not enough freespace need %u have %d\n",
2577 total_size
, btrfs_leaf_free_space(root
, leaf
));
2581 slot
= path
->slots
[0];
2584 if (slot
!= nritems
) {
2586 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2588 if (old_data
< data_end
) {
2589 btrfs_print_leaf(root
, leaf
);
2590 printk("slot %d old_data %d data_end %d\n",
2591 slot
, old_data
, data_end
);
2595 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2597 /* first correct the data pointers */
2598 for (i
= slot
; i
< nritems
; i
++) {
2601 item
= btrfs_item_nr(leaf
, i
);
2602 ioff
= btrfs_item_offset(leaf
, item
);
2603 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2606 /* shift the items */
2607 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2608 btrfs_item_nr_offset(slot
),
2609 (nritems
- slot
) * sizeof(struct btrfs_item
));
2611 /* shift the data */
2612 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2613 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2614 data_end
, old_data
- data_end
);
2615 data_end
= old_data
;
2618 /* setup the item for the new data */
2619 for (i
= 0; i
< nr
; i
++) {
2620 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2621 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2622 item
= btrfs_item_nr(leaf
, slot
+ i
);
2623 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2624 data_end
-= data_size
[i
];
2625 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2627 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2628 btrfs_mark_buffer_dirty(leaf
);
2632 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2633 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2636 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2637 btrfs_print_leaf(root
, leaf
);
2646 * Given a key and some data, insert an item into the tree.
2647 * This does all the path init required, making room in the tree if needed.
2649 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2650 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2654 struct btrfs_path
*path
;
2655 struct extent_buffer
*leaf
;
2658 path
= btrfs_alloc_path();
2660 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2662 leaf
= path
->nodes
[0];
2663 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2664 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2665 btrfs_mark_buffer_dirty(leaf
);
2667 btrfs_free_path(path
);
2672 * delete the pointer from a given node.
2674 * If the delete empties a node, the node is removed from the tree,
2675 * continuing all the way the root if required. The root is converted into
2676 * a leaf if all the nodes are emptied.
2678 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2679 struct btrfs_path
*path
, int level
, int slot
)
2681 struct extent_buffer
*parent
= path
->nodes
[level
];
2686 nritems
= btrfs_header_nritems(parent
);
2687 if (slot
!= nritems
-1) {
2688 memmove_extent_buffer(parent
,
2689 btrfs_node_key_ptr_offset(slot
),
2690 btrfs_node_key_ptr_offset(slot
+ 1),
2691 sizeof(struct btrfs_key_ptr
) *
2692 (nritems
- slot
- 1));
2695 btrfs_set_header_nritems(parent
, nritems
);
2696 if (nritems
== 0 && parent
== root
->node
) {
2697 BUG_ON(btrfs_header_level(root
->node
) != 1);
2698 /* just turn the root into a leaf and break */
2699 btrfs_set_header_level(root
->node
, 0);
2700 } else if (slot
== 0) {
2701 struct btrfs_disk_key disk_key
;
2703 btrfs_node_key(parent
, &disk_key
, 0);
2704 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
2708 btrfs_mark_buffer_dirty(parent
);
2713 * a helper function to delete the leaf pointed to by path->slots[1] and
2716 * This deletes the pointer in path->nodes[1] and frees the leaf
2717 * block extent. zero is returned if it all worked out, < 0 otherwise.
2719 * The path must have already been setup for deleting the leaf, including
2720 * all the proper balancing. path->nodes[1] must be locked.
2722 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
2723 struct btrfs_root
*root
,
2724 struct btrfs_path
*path
,
2725 struct extent_buffer
*leaf
)
2729 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
2730 ret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
2734 ret
= btrfs_free_extent(trans
, root
, leaf
->start
, leaf
->len
,
2735 0, root
->root_key
.objectid
, 0, 0);
2740 * delete the item at the leaf level in path. If that empties
2741 * the leaf, remove it from the tree
2743 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2744 struct btrfs_path
*path
, int slot
, int nr
)
2746 struct extent_buffer
*leaf
;
2747 struct btrfs_item
*item
;
2755 leaf
= path
->nodes
[0];
2756 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2758 for (i
= 0; i
< nr
; i
++)
2759 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2761 nritems
= btrfs_header_nritems(leaf
);
2763 if (slot
+ nr
!= nritems
) {
2765 int data_end
= leaf_data_end(root
, leaf
);
2767 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2769 btrfs_leaf_data(leaf
) + data_end
,
2770 last_off
- data_end
);
2772 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2775 item
= btrfs_item_nr(leaf
, i
);
2776 ioff
= btrfs_item_offset(leaf
, item
);
2777 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2780 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2781 btrfs_item_nr_offset(slot
+ nr
),
2782 sizeof(struct btrfs_item
) *
2783 (nritems
- slot
- nr
));
2785 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2788 /* delete the leaf if we've emptied it */
2790 if (leaf
== root
->node
) {
2791 btrfs_set_header_level(leaf
, 0);
2793 clean_tree_block(trans
, root
, leaf
);
2794 wait_on_tree_block_writeback(root
, leaf
);
2796 wret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2802 int used
= leaf_space_used(leaf
, 0, nritems
);
2804 struct btrfs_disk_key disk_key
;
2806 btrfs_item_key(leaf
, &disk_key
, 0);
2807 wret
= fixup_low_keys(trans
, root
, path
,
2813 /* delete the leaf if it is mostly empty */
2814 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 4) {
2815 /* push_leaf_left fixes the path.
2816 * make sure the path still points to our leaf
2817 * for possible call to del_ptr below
2819 slot
= path
->slots
[1];
2820 extent_buffer_get(leaf
);
2822 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2823 if (wret
< 0 && wret
!= -ENOSPC
)
2826 if (path
->nodes
[0] == leaf
&&
2827 btrfs_header_nritems(leaf
)) {
2828 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2829 if (wret
< 0 && wret
!= -ENOSPC
)
2833 if (btrfs_header_nritems(leaf
) == 0) {
2834 clean_tree_block(trans
, root
, leaf
);
2835 wait_on_tree_block_writeback(root
, leaf
);
2837 path
->slots
[1] = slot
;
2838 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2840 free_extent_buffer(leaf
);
2843 btrfs_mark_buffer_dirty(leaf
);
2844 free_extent_buffer(leaf
);
2847 btrfs_mark_buffer_dirty(leaf
);
2854 * walk up the tree as far as required to find the previous leaf.
2855 * returns 0 if it found something or 1 if there are no lesser leaves.
2856 * returns < 0 on io errors.
2858 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2862 struct extent_buffer
*c
;
2863 struct extent_buffer
*next
= NULL
;
2865 while(level
< BTRFS_MAX_LEVEL
) {
2866 if (!path
->nodes
[level
])
2869 slot
= path
->slots
[level
];
2870 c
= path
->nodes
[level
];
2873 if (level
== BTRFS_MAX_LEVEL
)
2880 free_extent_buffer(next
);
2882 next
= read_node_slot(root
, c
, slot
);
2885 path
->slots
[level
] = slot
;
2888 c
= path
->nodes
[level
];
2889 free_extent_buffer(c
);
2890 slot
= btrfs_header_nritems(next
);
2893 path
->nodes
[level
] = next
;
2894 path
->slots
[level
] = slot
;
2897 next
= read_node_slot(root
, next
, slot
);
2903 * walk up the tree as far as required to find the next leaf.
2904 * returns 0 if it found something or 1 if there are no greater leaves.
2905 * returns < 0 on io errors.
2907 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2911 struct extent_buffer
*c
;
2912 struct extent_buffer
*next
= NULL
;
2914 while(level
< BTRFS_MAX_LEVEL
) {
2915 if (!path
->nodes
[level
])
2918 slot
= path
->slots
[level
] + 1;
2919 c
= path
->nodes
[level
];
2920 if (slot
>= btrfs_header_nritems(c
)) {
2922 if (level
== BTRFS_MAX_LEVEL
)
2928 free_extent_buffer(next
);
2931 reada_for_search(root
, path
, level
, slot
, 0);
2933 next
= read_node_slot(root
, c
, slot
);
2936 path
->slots
[level
] = slot
;
2939 c
= path
->nodes
[level
];
2940 free_extent_buffer(c
);
2941 path
->nodes
[level
] = next
;
2942 path
->slots
[level
] = 0;
2946 reada_for_search(root
, path
, level
, 0, 0);
2947 next
= read_node_slot(root
, next
, 0);
2952 int btrfs_previous_item(struct btrfs_root
*root
,
2953 struct btrfs_path
*path
, u64 min_objectid
,
2956 struct btrfs_key found_key
;
2957 struct extent_buffer
*leaf
;
2961 if (path
->slots
[0] == 0) {
2962 ret
= btrfs_prev_leaf(root
, path
);
2968 leaf
= path
->nodes
[0];
2969 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2970 if (found_key
.type
== type
)