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"
24 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
25 *root
, struct btrfs_path
*path
, int level
);
26 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
27 *root
, struct btrfs_key
*ins_key
,
28 struct btrfs_path
*path
, int data_size
, int extend
);
29 static int push_node_left(struct btrfs_trans_handle
*trans
,
30 struct btrfs_root
*root
, struct extent_buffer
*dst
,
31 struct extent_buffer
*src
, int empty
);
32 static int balance_node_right(struct btrfs_trans_handle
*trans
,
33 struct btrfs_root
*root
,
34 struct extent_buffer
*dst_buf
,
35 struct extent_buffer
*src_buf
);
37 inline void btrfs_init_path(struct btrfs_path
*p
)
39 memset(p
, 0, sizeof(*p
));
42 struct btrfs_path
*btrfs_alloc_path(void)
44 struct btrfs_path
*path
;
45 path
= kmalloc(sizeof(struct btrfs_path
), GFP_NOFS
);
47 btrfs_init_path(path
);
53 void btrfs_free_path(struct btrfs_path
*p
)
55 btrfs_release_path(NULL
, p
);
59 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
62 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
65 free_extent_buffer(p
->nodes
[i
]);
67 memset(p
, 0, sizeof(*p
));
70 static void add_root_to_dirty_list(struct btrfs_root
*root
)
72 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
73 list_add(&root
->dirty_list
,
74 &root
->fs_info
->dirty_cowonly_roots
);
78 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
79 struct btrfs_root
*root
,
80 struct extent_buffer
*buf
,
81 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
83 struct extent_buffer
*cow
;
86 struct btrfs_root
*new_root
;
87 struct btrfs_disk_key disk_key
;
89 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
93 memcpy(new_root
, root
, sizeof(*new_root
));
94 new_root
->root_key
.objectid
= new_root_objectid
;
96 WARN_ON(root
->ref_cows
&& trans
->transid
!=
97 root
->fs_info
->running_transaction
->transid
);
98 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
100 level
= btrfs_header_level(buf
);
102 btrfs_item_key(buf
, &disk_key
, 0);
104 btrfs_node_key(buf
, &disk_key
, 0);
105 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
106 new_root_objectid
, &disk_key
,
107 level
, buf
->start
, 0);
113 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
114 btrfs_set_header_bytenr(cow
, cow
->start
);
115 btrfs_set_header_generation(cow
, trans
->transid
);
116 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
117 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
118 BTRFS_HEADER_FLAG_RELOC
);
119 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
120 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
122 btrfs_set_header_owner(cow
, new_root_objectid
);
124 write_extent_buffer(cow
, root
->fs_info
->fsid
,
125 (unsigned long)btrfs_header_fsid(cow
),
128 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
129 ret
= btrfs_inc_ref(trans
, new_root
, cow
, 0);
135 btrfs_mark_buffer_dirty(cow
);
140 int btrfs_fsck_reinit_root(struct btrfs_trans_handle
*trans
,
141 struct btrfs_root
*root
)
143 struct extent_buffer
*c
;
144 struct extent_buffer
*old
= root
->node
;
146 struct btrfs_disk_key disk_key
= {0,0,0};
150 c
= btrfs_alloc_free_block(trans
, root
,
151 btrfs_level_size(root
, 0),
152 root
->root_key
.objectid
,
153 &disk_key
, level
, 0, 0);
156 extent_buffer_get(c
);
159 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
160 btrfs_set_header_level(c
, level
);
161 btrfs_set_header_bytenr(c
, c
->start
);
162 btrfs_set_header_generation(c
, trans
->transid
);
163 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
164 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
166 write_extent_buffer(c
, root
->fs_info
->fsid
,
167 (unsigned long)btrfs_header_fsid(c
),
170 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
171 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
174 btrfs_mark_buffer_dirty(c
);
176 free_extent_buffer(old
);
178 add_root_to_dirty_list(root
);
183 * check if the tree block can be shared by multiple trees
185 int btrfs_block_can_be_shared(struct btrfs_root
*root
,
186 struct extent_buffer
*buf
)
189 * Tree blocks not in refernece counted trees and tree roots
190 * are never shared. If a block was allocated after the last
191 * snapshot and the block was not allocated by tree relocation,
192 * we know the block is not shared.
194 if (root
->ref_cows
&&
195 buf
!= root
->node
&& buf
!= root
->commit_root
&&
196 (btrfs_header_generation(buf
) <=
197 btrfs_root_last_snapshot(&root
->root_item
) ||
198 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
200 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
201 if (root
->ref_cows
&&
202 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
208 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
209 struct btrfs_root
*root
,
210 struct extent_buffer
*buf
,
211 struct extent_buffer
*cow
)
220 * Backrefs update rules:
222 * Always use full backrefs for extent pointers in tree block
223 * allocated by tree relocation.
225 * If a shared tree block is no longer referenced by its owner
226 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
227 * use full backrefs for extent pointers in tree block.
229 * If a tree block is been relocating
230 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
231 * use full backrefs for extent pointers in tree block.
232 * The reason for this is some operations (such as drop tree)
233 * are only allowed for blocks use full backrefs.
236 if (btrfs_block_can_be_shared(root
, buf
)) {
237 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
238 buf
->len
, &refs
, &flags
);
243 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
244 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
245 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
250 owner
= btrfs_header_owner(buf
);
251 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) &&
252 owner
== BTRFS_TREE_RELOC_OBJECTID
);
255 if ((owner
== root
->root_key
.objectid
||
256 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
257 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
258 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
261 if (root
->root_key
.objectid
==
262 BTRFS_TREE_RELOC_OBJECTID
) {
263 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
265 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
268 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
271 if (root
->root_key
.objectid
==
272 BTRFS_TREE_RELOC_OBJECTID
)
273 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
275 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
278 if (new_flags
!= 0) {
279 ret
= btrfs_set_block_flags(trans
, root
, buf
->start
,
280 buf
->len
, new_flags
);
284 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
285 if (root
->root_key
.objectid
==
286 BTRFS_TREE_RELOC_OBJECTID
)
287 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
289 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
291 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
294 clean_tree_block(trans
, root
, buf
);
299 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
300 struct btrfs_root
*root
,
301 struct extent_buffer
*buf
,
302 struct extent_buffer
*parent
, int parent_slot
,
303 struct extent_buffer
**cow_ret
,
304 u64 search_start
, u64 empty_size
)
306 struct extent_buffer
*cow
;
307 struct btrfs_disk_key disk_key
;
310 WARN_ON(root
->ref_cows
&& trans
->transid
!=
311 root
->fs_info
->running_transaction
->transid
);
312 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
314 level
= btrfs_header_level(buf
);
317 btrfs_item_key(buf
, &disk_key
, 0);
319 btrfs_node_key(buf
, &disk_key
, 0);
321 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
,
322 root
->root_key
.objectid
, &disk_key
,
323 level
, search_start
, empty_size
);
327 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
328 btrfs_set_header_bytenr(cow
, cow
->start
);
329 btrfs_set_header_generation(cow
, trans
->transid
);
330 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
331 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
332 BTRFS_HEADER_FLAG_RELOC
);
333 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
334 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
336 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
338 write_extent_buffer(cow
, root
->fs_info
->fsid
,
339 (unsigned long)btrfs_header_fsid(cow
),
342 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
344 update_ref_for_cow(trans
, root
, buf
, cow
);
346 if (buf
== root
->node
) {
348 extent_buffer_get(cow
);
350 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
351 0, root
->root_key
.objectid
, level
, 0);
352 free_extent_buffer(buf
);
353 add_root_to_dirty_list(root
);
355 btrfs_set_node_blockptr(parent
, parent_slot
,
357 WARN_ON(trans
->transid
== 0);
358 btrfs_set_node_ptr_generation(parent
, parent_slot
,
360 btrfs_mark_buffer_dirty(parent
);
361 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
363 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
364 0, root
->root_key
.objectid
, level
, 1);
366 free_extent_buffer(buf
);
367 btrfs_mark_buffer_dirty(cow
);
372 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
373 struct btrfs_root
*root
,
374 struct extent_buffer
*buf
)
376 if (btrfs_header_generation(buf
) == trans
->transid
&&
377 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
378 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
379 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
384 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
385 struct btrfs_root
*root
, struct extent_buffer
*buf
,
386 struct extent_buffer
*parent
, int parent_slot
,
387 struct extent_buffer
**cow_ret
)
392 if (trans->transaction != root->fs_info->running_transaction) {
393 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
394 root->fs_info->running_transaction->transid);
398 if (trans
->transid
!= root
->fs_info
->generation
) {
399 printk(KERN_CRIT
"trans %llu running %llu\n",
400 (unsigned long long)trans
->transid
,
401 (unsigned long long)root
->fs_info
->generation
);
404 if (!should_cow_block(trans
, root
, buf
)) {
409 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
410 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
411 parent_slot
, cow_ret
, search_start
, 0);
416 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
418 if (blocknr < other && other - (blocknr + blocksize) < 32768)
420 if (blocknr > other && blocknr - (other + blocksize) < 32768)
427 * compare two keys in a memcmp fashion
429 int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
433 btrfs_disk_key_to_cpu(&k1
, disk
);
435 if (k1
.objectid
> k2
->objectid
)
437 if (k1
.objectid
< k2
->objectid
)
439 if (k1
.type
> k2
->type
)
441 if (k1
.type
< k2
->type
)
443 if (k1
.offset
> k2
->offset
)
445 if (k1
.offset
< k2
->offset
)
452 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
453 struct btrfs_root
*root
, struct extent_buffer
*parent
,
454 int start_slot
, int cache_only
, u64
*last_ret
,
455 struct btrfs_key
*progress
)
457 struct extent_buffer
*cur
;
458 struct extent_buffer
*tmp
;
461 u64 search_start
= *last_ret
;
471 int progress_passed
= 0;
472 struct btrfs_disk_key disk_key
;
474 parent_level
= btrfs_header_level(parent
);
475 if (cache_only
&& parent_level
!= 1)
478 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
479 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
480 root
->fs_info
->running_transaction
->transid
);
483 if (trans
->transid
!= root
->fs_info
->generation
) {
484 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
485 root
->fs_info
->generation
);
489 parent_nritems
= btrfs_header_nritems(parent
);
490 blocksize
= btrfs_level_size(root
, parent_level
- 1);
491 end_slot
= parent_nritems
;
493 if (parent_nritems
== 1)
496 for (i
= start_slot
; i
< end_slot
; i
++) {
499 if (!parent
->map_token
) {
500 map_extent_buffer(parent
,
501 btrfs_node_key_ptr_offset(i
),
502 sizeof(struct btrfs_key_ptr
),
503 &parent
->map_token
, &parent
->kaddr
,
504 &parent
->map_start
, &parent
->map_len
,
507 btrfs_node_key(parent
, &disk_key
, i
);
508 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
512 blocknr
= btrfs_node_blockptr(parent
, i
);
513 gen
= btrfs_node_ptr_generation(parent
, i
);
515 last_block
= blocknr
;
518 other
= btrfs_node_blockptr(parent
, i
- 1);
519 close
= close_blocks(blocknr
, other
, blocksize
);
521 if (close
&& i
< end_slot
- 2) {
522 other
= btrfs_node_blockptr(parent
, i
+ 1);
523 close
= close_blocks(blocknr
, other
, blocksize
);
526 last_block
= blocknr
;
529 if (parent
->map_token
) {
530 unmap_extent_buffer(parent
, parent
->map_token
,
532 parent
->map_token
= NULL
;
535 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
537 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
540 if (!cur
|| !uptodate
) {
542 free_extent_buffer(cur
);
546 cur
= read_tree_block(root
, blocknr
,
548 } else if (!uptodate
) {
549 btrfs_read_buffer(cur
, gen
);
552 if (search_start
== 0)
553 search_start
= last_block
;
555 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
558 (end_slot
- i
) * blocksize
));
560 free_extent_buffer(cur
);
563 search_start
= tmp
->start
;
564 last_block
= tmp
->start
;
565 *last_ret
= search_start
;
566 if (parent_level
== 1)
567 btrfs_clear_buffer_defrag(tmp
);
568 free_extent_buffer(tmp
);
570 if (parent
->map_token
) {
571 unmap_extent_buffer(parent
, parent
->map_token
,
573 parent
->map_token
= NULL
;
580 * The leaf data grows from end-to-front in the node.
581 * this returns the address of the start of the last item,
582 * which is the stop of the leaf data stack
584 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
585 struct extent_buffer
*leaf
)
587 u32 nr
= btrfs_header_nritems(leaf
);
589 return BTRFS_LEAF_DATA_SIZE(root
);
590 return btrfs_item_offset_nr(leaf
, nr
- 1);
593 int btrfs_check_node(struct btrfs_root
*root
,
594 struct btrfs_disk_key
*parent_key
,
595 struct extent_buffer
*buf
)
598 struct btrfs_key cpukey
;
599 struct btrfs_disk_key key
;
600 u32 nritems
= btrfs_header_nritems(buf
);
602 if (nritems
== 0 || nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
))
605 if (parent_key
&& parent_key
->type
) {
606 btrfs_node_key(buf
, &key
, 0);
607 if (memcmp(parent_key
, &key
, sizeof(key
)))
610 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
611 btrfs_node_key(buf
, &key
, i
);
612 btrfs_node_key_to_cpu(buf
, &cpukey
, i
+ 1);
613 if (btrfs_comp_keys(&key
, &cpukey
) >= 0)
618 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
620 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
622 btrfs_node_key_to_cpu(buf
, &cpukey
, 0);
623 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
624 buf
->start
, buf
->len
,
625 btrfs_header_level(buf
));
630 int btrfs_check_leaf(struct btrfs_root
*root
,
631 struct btrfs_disk_key
*parent_key
,
632 struct extent_buffer
*buf
)
635 struct btrfs_key cpukey
;
636 struct btrfs_disk_key key
;
637 u32 nritems
= btrfs_header_nritems(buf
);
639 if (btrfs_header_level(buf
) != 0) {
640 fprintf(stderr
, "leaf is not a leaf %llu\n",
641 (unsigned long long)btrfs_header_bytenr(buf
));
644 if (btrfs_leaf_free_space(root
, buf
) < 0) {
645 fprintf(stderr
, "leaf free space incorrect %llu %d\n",
646 (unsigned long long)btrfs_header_bytenr(buf
),
647 btrfs_leaf_free_space(root
, buf
));
654 btrfs_item_key(buf
, &key
, 0);
655 if (parent_key
&& parent_key
->type
&&
656 memcmp(parent_key
, &key
, sizeof(key
))) {
657 fprintf(stderr
, "leaf parent key incorrect %llu\n",
658 (unsigned long long)btrfs_header_bytenr(buf
));
661 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
662 btrfs_item_key(buf
, &key
, i
);
663 btrfs_item_key_to_cpu(buf
, &cpukey
, i
+ 1);
664 if (btrfs_comp_keys(&key
, &cpukey
) >= 0) {
665 fprintf(stderr
, "bad key ordering %d %d\n", i
, i
+1);
668 if (btrfs_item_offset_nr(buf
, i
) !=
669 btrfs_item_end_nr(buf
, i
+ 1)) {
670 fprintf(stderr
, "incorrect offsets %u %u\n",
671 btrfs_item_offset_nr(buf
, i
),
672 btrfs_item_end_nr(buf
, i
+ 1));
675 if (i
== 0 && btrfs_item_end_nr(buf
, i
) !=
676 BTRFS_LEAF_DATA_SIZE(root
)) {
677 fprintf(stderr
, "bad item end %u wanted %u\n",
678 btrfs_item_end_nr(buf
, i
),
679 (unsigned)BTRFS_LEAF_DATA_SIZE(root
));
685 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
687 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
689 btrfs_item_key_to_cpu(buf
, &cpukey
, 0);
691 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
692 buf
->start
, buf
->len
, 0);
697 static int noinline
check_block(struct btrfs_root
*root
,
698 struct btrfs_path
*path
, int level
)
700 struct btrfs_disk_key key
;
701 struct btrfs_disk_key
*key_ptr
= NULL
;
702 struct extent_buffer
*parent
;
704 if (path
->nodes
[level
+ 1]) {
705 parent
= path
->nodes
[level
+ 1];
706 btrfs_node_key(parent
, &key
, path
->slots
[level
+ 1]);
710 return btrfs_check_leaf(root
, key_ptr
, path
->nodes
[0]);
711 return btrfs_check_node(root
, key_ptr
, path
->nodes
[level
]);
715 * search for key in the extent_buffer. The items start at offset p,
716 * and they are item_size apart. There are 'max' items in p.
718 * the slot in the array is returned via slot, and it points to
719 * the place where you would insert key if it is not found in
722 * slot may point to max if the key is bigger than all of the keys
724 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
725 int item_size
, struct btrfs_key
*key
,
732 unsigned long offset
;
733 struct btrfs_disk_key
*tmp
;
736 mid
= (low
+ high
) / 2;
737 offset
= p
+ mid
* item_size
;
739 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
740 ret
= btrfs_comp_keys(tmp
, key
);
756 * simple bin_search frontend that does the right thing for
759 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
760 int level
, int *slot
)
763 return generic_bin_search(eb
,
764 offsetof(struct btrfs_leaf
, items
),
765 sizeof(struct btrfs_item
),
766 key
, btrfs_header_nritems(eb
),
769 return generic_bin_search(eb
,
770 offsetof(struct btrfs_node
, ptrs
),
771 sizeof(struct btrfs_key_ptr
),
772 key
, btrfs_header_nritems(eb
),
778 struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
779 struct extent_buffer
*parent
, int slot
)
781 int level
= btrfs_header_level(parent
);
784 if (slot
>= btrfs_header_nritems(parent
))
789 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
790 btrfs_level_size(root
, level
- 1),
791 btrfs_node_ptr_generation(parent
, slot
));
794 static int balance_level(struct btrfs_trans_handle
*trans
,
795 struct btrfs_root
*root
,
796 struct btrfs_path
*path
, int level
)
798 struct extent_buffer
*right
= NULL
;
799 struct extent_buffer
*mid
;
800 struct extent_buffer
*left
= NULL
;
801 struct extent_buffer
*parent
= NULL
;
805 int orig_slot
= path
->slots
[level
];
811 mid
= path
->nodes
[level
];
812 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
814 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
816 if (level
< BTRFS_MAX_LEVEL
- 1)
817 parent
= path
->nodes
[level
+ 1];
818 pslot
= path
->slots
[level
+ 1];
821 * deal with the case where there is only one pointer in the root
822 * by promoting the node below to a root
825 struct extent_buffer
*child
;
827 if (btrfs_header_nritems(mid
) != 1)
830 /* promote the child to a root */
831 child
= read_node_slot(root
, mid
, 0);
833 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
837 add_root_to_dirty_list(root
);
838 path
->nodes
[level
] = NULL
;
839 clean_tree_block(trans
, root
, mid
);
840 wait_on_tree_block_writeback(root
, mid
);
841 /* once for the path */
842 free_extent_buffer(mid
);
844 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
845 0, root
->root_key
.objectid
,
847 /* once for the root ptr */
848 free_extent_buffer(mid
);
851 if (btrfs_header_nritems(mid
) >
852 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
855 left
= read_node_slot(root
, parent
, pslot
- 1);
857 wret
= btrfs_cow_block(trans
, root
, left
,
858 parent
, pslot
- 1, &left
);
864 right
= read_node_slot(root
, parent
, pslot
+ 1);
866 wret
= btrfs_cow_block(trans
, root
, right
,
867 parent
, pslot
+ 1, &right
);
874 /* first, try to make some room in the middle buffer */
876 orig_slot
+= btrfs_header_nritems(left
);
877 wret
= push_node_left(trans
, root
, left
, mid
, 1);
883 * then try to empty the right most buffer into the middle
886 wret
= push_node_left(trans
, root
, mid
, right
, 1);
887 if (wret
< 0 && wret
!= -ENOSPC
)
889 if (btrfs_header_nritems(right
) == 0) {
890 u64 bytenr
= right
->start
;
891 u32 blocksize
= right
->len
;
893 clean_tree_block(trans
, root
, right
);
894 wait_on_tree_block_writeback(root
, right
);
895 free_extent_buffer(right
);
897 wret
= btrfs_del_ptr(trans
, root
, path
,
898 level
+ 1, pslot
+ 1);
901 wret
= btrfs_free_extent(trans
, root
, bytenr
,
903 root
->root_key
.objectid
,
908 struct btrfs_disk_key right_key
;
909 btrfs_node_key(right
, &right_key
, 0);
910 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
911 btrfs_mark_buffer_dirty(parent
);
914 if (btrfs_header_nritems(mid
) == 1) {
916 * we're not allowed to leave a node with one item in the
917 * tree during a delete. A deletion from lower in the tree
918 * could try to delete the only pointer in this node.
919 * So, pull some keys from the left.
920 * There has to be a left pointer at this point because
921 * otherwise we would have pulled some pointers from the
925 wret
= balance_node_right(trans
, root
, mid
, left
);
931 wret
= push_node_left(trans
, root
, left
, mid
, 1);
937 if (btrfs_header_nritems(mid
) == 0) {
938 /* we've managed to empty the middle node, drop it */
939 u64 bytenr
= mid
->start
;
940 u32 blocksize
= mid
->len
;
941 clean_tree_block(trans
, root
, mid
);
942 wait_on_tree_block_writeback(root
, mid
);
943 free_extent_buffer(mid
);
945 wret
= btrfs_del_ptr(trans
, root
, path
, level
+ 1, pslot
);
948 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
949 0, root
->root_key
.objectid
,
954 /* update the parent key to reflect our changes */
955 struct btrfs_disk_key mid_key
;
956 btrfs_node_key(mid
, &mid_key
, 0);
957 btrfs_set_node_key(parent
, &mid_key
, pslot
);
958 btrfs_mark_buffer_dirty(parent
);
961 /* update the path */
963 if (btrfs_header_nritems(left
) > orig_slot
) {
964 extent_buffer_get(left
);
965 path
->nodes
[level
] = left
;
966 path
->slots
[level
+ 1] -= 1;
967 path
->slots
[level
] = orig_slot
;
969 free_extent_buffer(mid
);
971 orig_slot
-= btrfs_header_nritems(left
);
972 path
->slots
[level
] = orig_slot
;
975 /* double check we haven't messed things up */
976 check_block(root
, path
, level
);
978 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
982 free_extent_buffer(right
);
984 free_extent_buffer(left
);
988 /* returns zero if the push worked, non-zero otherwise */
989 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
990 struct btrfs_root
*root
,
991 struct btrfs_path
*path
, int level
)
993 struct extent_buffer
*right
= NULL
;
994 struct extent_buffer
*mid
;
995 struct extent_buffer
*left
= NULL
;
996 struct extent_buffer
*parent
= NULL
;
1000 int orig_slot
= path
->slots
[level
];
1005 mid
= path
->nodes
[level
];
1006 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
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 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
> 0);
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
);
1267 if (!extent_buffer_uptodate(b
))
1270 p
->slots
[level
] = slot
;
1272 ins_len
> btrfs_leaf_free_space(root
, b
)) {
1273 int sret
= split_leaf(trans
, root
, key
,
1274 p
, ins_len
, ret
== 0);
1286 * adjust the pointers going up the tree, starting at level
1287 * making sure the right key of each node is points to 'key'.
1288 * This is used after shifting pointers to the left, so it stops
1289 * fixing up pointers when a given leaf/node is not in slot 0 of the
1292 * If this fails to write a tree block, it returns -1, but continues
1293 * fixing up the blocks in ram so the tree is consistent.
1295 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1296 struct btrfs_root
*root
, struct btrfs_path
*path
,
1297 struct btrfs_disk_key
*key
, int level
)
1301 struct extent_buffer
*t
;
1303 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1304 int tslot
= path
->slots
[i
];
1305 if (!path
->nodes
[i
])
1308 btrfs_set_node_key(t
, key
, tslot
);
1309 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1319 * This function isn't completely safe. It's the caller's responsibility
1320 * that the new key won't break the order
1322 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1323 struct btrfs_root
*root
, struct btrfs_path
*path
,
1324 struct btrfs_key
*new_key
)
1326 struct btrfs_disk_key disk_key
;
1327 struct extent_buffer
*eb
;
1330 eb
= path
->nodes
[0];
1331 slot
= path
->slots
[0];
1333 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1334 if (btrfs_comp_keys(&disk_key
, new_key
) >= 0)
1337 if (slot
< btrfs_header_nritems(eb
) - 1) {
1338 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1339 if (btrfs_comp_keys(&disk_key
, new_key
) <= 0)
1343 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1344 btrfs_set_item_key(eb
, &disk_key
, slot
);
1345 btrfs_mark_buffer_dirty(eb
);
1347 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1352 * try to push data from one node into the next node left in the
1355 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1356 * error, and > 0 if there was no room in the left hand block.
1358 static int push_node_left(struct btrfs_trans_handle
*trans
,
1359 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1360 struct extent_buffer
*src
, int empty
)
1367 src_nritems
= btrfs_header_nritems(src
);
1368 dst_nritems
= btrfs_header_nritems(dst
);
1369 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1370 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1371 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1373 if (!empty
&& src_nritems
<= 8)
1376 if (push_items
<= 0) {
1381 push_items
= min(src_nritems
, push_items
);
1382 if (push_items
< src_nritems
) {
1383 /* leave at least 8 pointers in the node if
1384 * we aren't going to empty it
1386 if (src_nritems
- push_items
< 8) {
1387 if (push_items
<= 8)
1393 push_items
= min(src_nritems
- 8, push_items
);
1395 copy_extent_buffer(dst
, src
,
1396 btrfs_node_key_ptr_offset(dst_nritems
),
1397 btrfs_node_key_ptr_offset(0),
1398 push_items
* sizeof(struct btrfs_key_ptr
));
1400 if (push_items
< src_nritems
) {
1401 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1402 btrfs_node_key_ptr_offset(push_items
),
1403 (src_nritems
- push_items
) *
1404 sizeof(struct btrfs_key_ptr
));
1406 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1407 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1408 btrfs_mark_buffer_dirty(src
);
1409 btrfs_mark_buffer_dirty(dst
);
1415 * try to push data from one node into the next node right in the
1418 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1419 * error, and > 0 if there was no room in the right hand block.
1421 * this will only push up to 1/2 the contents of the left node over
1423 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1424 struct btrfs_root
*root
,
1425 struct extent_buffer
*dst
,
1426 struct extent_buffer
*src
)
1434 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1435 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1437 src_nritems
= btrfs_header_nritems(src
);
1438 dst_nritems
= btrfs_header_nritems(dst
);
1439 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1440 if (push_items
<= 0) {
1444 if (src_nritems
< 4) {
1448 max_push
= src_nritems
/ 2 + 1;
1449 /* don't try to empty the node */
1450 if (max_push
>= src_nritems
) {
1454 if (max_push
< push_items
)
1455 push_items
= max_push
;
1457 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1458 btrfs_node_key_ptr_offset(0),
1460 sizeof(struct btrfs_key_ptr
));
1462 copy_extent_buffer(dst
, src
,
1463 btrfs_node_key_ptr_offset(0),
1464 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1465 push_items
* sizeof(struct btrfs_key_ptr
));
1467 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1468 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1470 btrfs_mark_buffer_dirty(src
);
1471 btrfs_mark_buffer_dirty(dst
);
1477 * helper function to insert a new root level in the tree.
1478 * A new node is allocated, and a single item is inserted to
1479 * point to the existing root
1481 * returns zero on success or < 0 on failure.
1483 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1484 struct btrfs_root
*root
,
1485 struct btrfs_path
*path
, int level
)
1488 struct extent_buffer
*lower
;
1489 struct extent_buffer
*c
;
1490 struct extent_buffer
*old
;
1491 struct btrfs_disk_key lower_key
;
1493 BUG_ON(path
->nodes
[level
]);
1494 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1496 lower
= path
->nodes
[level
-1];
1498 btrfs_item_key(lower
, &lower_key
, 0);
1500 btrfs_node_key(lower
, &lower_key
, 0);
1502 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1503 root
->root_key
.objectid
, &lower_key
,
1504 level
, root
->node
->start
, 0);
1509 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1510 btrfs_set_header_nritems(c
, 1);
1511 btrfs_set_header_level(c
, level
);
1512 btrfs_set_header_bytenr(c
, c
->start
);
1513 btrfs_set_header_generation(c
, trans
->transid
);
1514 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1515 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1517 write_extent_buffer(c
, root
->fs_info
->fsid
,
1518 (unsigned long)btrfs_header_fsid(c
),
1521 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1522 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
1525 btrfs_set_node_key(c
, &lower_key
, 0);
1526 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1527 lower_gen
= btrfs_header_generation(lower
);
1528 WARN_ON(lower_gen
!= trans
->transid
);
1530 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1532 btrfs_mark_buffer_dirty(c
);
1537 /* the super has an extra ref to root->node */
1538 free_extent_buffer(old
);
1540 add_root_to_dirty_list(root
);
1541 extent_buffer_get(c
);
1542 path
->nodes
[level
] = c
;
1543 path
->slots
[level
] = 0;
1548 * worker function to insert a single pointer in a node.
1549 * the node should have enough room for the pointer already
1551 * slot and level indicate where you want the key to go, and
1552 * blocknr is the block the key points to.
1554 * returns zero on success and < 0 on any error
1556 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1557 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1558 *key
, u64 bytenr
, int slot
, int level
)
1560 struct extent_buffer
*lower
;
1563 BUG_ON(!path
->nodes
[level
]);
1564 lower
= path
->nodes
[level
];
1565 nritems
= btrfs_header_nritems(lower
);
1568 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
1570 if (slot
!= nritems
) {
1571 memmove_extent_buffer(lower
,
1572 btrfs_node_key_ptr_offset(slot
+ 1),
1573 btrfs_node_key_ptr_offset(slot
),
1574 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1576 btrfs_set_node_key(lower
, key
, slot
);
1577 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1578 WARN_ON(trans
->transid
== 0);
1579 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1580 btrfs_set_header_nritems(lower
, nritems
+ 1);
1581 btrfs_mark_buffer_dirty(lower
);
1586 * split the node at the specified level in path in two.
1587 * The path is corrected to point to the appropriate node after the split
1589 * Before splitting this tries to make some room in the node by pushing
1590 * left and right, if either one works, it returns right away.
1592 * returns 0 on success and < 0 on failure
1594 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1595 *root
, struct btrfs_path
*path
, int level
)
1597 struct extent_buffer
*c
;
1598 struct extent_buffer
*split
;
1599 struct btrfs_disk_key disk_key
;
1605 c
= path
->nodes
[level
];
1606 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1607 if (c
== root
->node
) {
1608 /* trying to split the root, lets make a new one */
1609 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1613 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1614 c
= path
->nodes
[level
];
1615 if (!ret
&& btrfs_header_nritems(c
) <
1616 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
1622 c_nritems
= btrfs_header_nritems(c
);
1623 mid
= (c_nritems
+ 1) / 2;
1624 btrfs_node_key(c
, &disk_key
, mid
);
1626 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1627 root
->root_key
.objectid
,
1628 &disk_key
, level
, c
->start
, 0);
1630 return PTR_ERR(split
);
1632 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
1633 btrfs_set_header_level(split
, btrfs_header_level(c
));
1634 btrfs_set_header_bytenr(split
, split
->start
);
1635 btrfs_set_header_generation(split
, trans
->transid
);
1636 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
1637 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1638 write_extent_buffer(split
, root
->fs_info
->fsid
,
1639 (unsigned long)btrfs_header_fsid(split
),
1641 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1642 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
1646 copy_extent_buffer(split
, c
,
1647 btrfs_node_key_ptr_offset(0),
1648 btrfs_node_key_ptr_offset(mid
),
1649 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1650 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1651 btrfs_set_header_nritems(c
, mid
);
1654 btrfs_mark_buffer_dirty(c
);
1655 btrfs_mark_buffer_dirty(split
);
1657 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1658 path
->slots
[level
+ 1] + 1,
1663 if (path
->slots
[level
] >= mid
) {
1664 path
->slots
[level
] -= mid
;
1665 free_extent_buffer(c
);
1666 path
->nodes
[level
] = split
;
1667 path
->slots
[level
+ 1] += 1;
1669 free_extent_buffer(split
);
1675 * how many bytes are required to store the items in a leaf. start
1676 * and nr indicate which items in the leaf to check. This totals up the
1677 * space used both by the item structs and the item data
1679 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1682 int nritems
= btrfs_header_nritems(l
);
1683 int end
= min(nritems
, start
+ nr
) - 1;
1687 data_len
= btrfs_item_end_nr(l
, start
);
1688 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1689 data_len
+= sizeof(struct btrfs_item
) * nr
;
1690 WARN_ON(data_len
< 0);
1695 * The space between the end of the leaf items and
1696 * the start of the leaf data. IOW, how much room
1697 * the leaf has left for both items and data
1699 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1701 int nritems
= btrfs_header_nritems(leaf
);
1703 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
1705 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1706 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
1707 leaf_space_used(leaf
, 0, nritems
), nritems
);
1713 * push some data in the path leaf to the right, trying to free up at
1714 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1716 * returns 1 if the push failed because the other node didn't have enough
1717 * room, 0 if everything worked out and < 0 if there were major errors.
1719 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1720 *root
, struct btrfs_path
*path
, int data_size
,
1723 struct extent_buffer
*left
= path
->nodes
[0];
1724 struct extent_buffer
*right
;
1725 struct extent_buffer
*upper
;
1726 struct btrfs_disk_key disk_key
;
1732 struct btrfs_item
*item
;
1740 slot
= path
->slots
[1];
1741 if (!path
->nodes
[1]) {
1744 upper
= path
->nodes
[1];
1745 if (slot
>= btrfs_header_nritems(upper
) - 1)
1748 right
= read_node_slot(root
, upper
, slot
+ 1);
1749 free_space
= btrfs_leaf_free_space(root
, right
);
1750 if (free_space
< data_size
) {
1751 free_extent_buffer(right
);
1755 /* cow and double check */
1756 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1759 free_extent_buffer(right
);
1762 free_space
= btrfs_leaf_free_space(root
, right
);
1763 if (free_space
< data_size
) {
1764 free_extent_buffer(right
);
1768 left_nritems
= btrfs_header_nritems(left
);
1769 if (left_nritems
== 0) {
1770 free_extent_buffer(right
);
1779 i
= left_nritems
- 1;
1781 item
= btrfs_item_nr(left
, i
);
1783 if (path
->slots
[0] == i
)
1784 push_space
+= data_size
+ sizeof(*item
);
1786 this_item_size
= btrfs_item_size(left
, item
);
1787 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1790 push_space
+= this_item_size
+ sizeof(*item
);
1796 if (push_items
== 0) {
1797 free_extent_buffer(right
);
1801 if (!empty
&& push_items
== left_nritems
)
1804 /* push left to right */
1805 right_nritems
= btrfs_header_nritems(right
);
1807 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1808 push_space
-= leaf_data_end(root
, left
);
1810 /* make room in the right data area */
1811 data_end
= leaf_data_end(root
, right
);
1812 memmove_extent_buffer(right
,
1813 btrfs_leaf_data(right
) + data_end
- push_space
,
1814 btrfs_leaf_data(right
) + data_end
,
1815 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
1817 /* copy from the left data area */
1818 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1819 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1820 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1823 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1824 btrfs_item_nr_offset(0),
1825 right_nritems
* sizeof(struct btrfs_item
));
1827 /* copy the items from left to right */
1828 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1829 btrfs_item_nr_offset(left_nritems
- push_items
),
1830 push_items
* sizeof(struct btrfs_item
));
1832 /* update the item pointers */
1833 right_nritems
+= push_items
;
1834 btrfs_set_header_nritems(right
, right_nritems
);
1835 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1836 for (i
= 0; i
< right_nritems
; i
++) {
1837 item
= btrfs_item_nr(right
, i
);
1838 push_space
-= btrfs_item_size(right
, item
);
1839 btrfs_set_item_offset(right
, item
, push_space
);
1842 left_nritems
-= push_items
;
1843 btrfs_set_header_nritems(left
, left_nritems
);
1846 btrfs_mark_buffer_dirty(left
);
1847 btrfs_mark_buffer_dirty(right
);
1849 btrfs_item_key(right
, &disk_key
, 0);
1850 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1851 btrfs_mark_buffer_dirty(upper
);
1853 /* then fixup the leaf pointer in the path */
1854 if (path
->slots
[0] >= left_nritems
) {
1855 path
->slots
[0] -= left_nritems
;
1856 free_extent_buffer(path
->nodes
[0]);
1857 path
->nodes
[0] = right
;
1858 path
->slots
[1] += 1;
1860 free_extent_buffer(right
);
1865 * push some data in the path leaf to the left, trying to free up at
1866 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1868 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1869 *root
, struct btrfs_path
*path
, int data_size
,
1872 struct btrfs_disk_key disk_key
;
1873 struct extent_buffer
*right
= path
->nodes
[0];
1874 struct extent_buffer
*left
;
1880 struct btrfs_item
*item
;
1881 u32 old_left_nritems
;
1887 u32 old_left_item_size
;
1889 slot
= path
->slots
[1];
1892 if (!path
->nodes
[1])
1895 right_nritems
= btrfs_header_nritems(right
);
1896 if (right_nritems
== 0) {
1900 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
1901 free_space
= btrfs_leaf_free_space(root
, left
);
1902 if (free_space
< data_size
) {
1903 free_extent_buffer(left
);
1907 /* cow and double check */
1908 ret
= btrfs_cow_block(trans
, root
, left
,
1909 path
->nodes
[1], slot
- 1, &left
);
1911 /* we hit -ENOSPC, but it isn't fatal here */
1912 free_extent_buffer(left
);
1916 free_space
= btrfs_leaf_free_space(root
, left
);
1917 if (free_space
< data_size
) {
1918 free_extent_buffer(left
);
1925 nr
= right_nritems
- 1;
1927 for (i
= 0; i
< nr
; i
++) {
1928 item
= btrfs_item_nr(right
, i
);
1930 if (path
->slots
[0] == i
)
1931 push_space
+= data_size
+ sizeof(*item
);
1933 this_item_size
= btrfs_item_size(right
, item
);
1934 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1938 push_space
+= this_item_size
+ sizeof(*item
);
1941 if (push_items
== 0) {
1942 free_extent_buffer(left
);
1945 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1948 /* push data from right to left */
1949 copy_extent_buffer(left
, right
,
1950 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1951 btrfs_item_nr_offset(0),
1952 push_items
* sizeof(struct btrfs_item
));
1954 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1955 btrfs_item_offset_nr(right
, push_items
-1);
1957 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1958 leaf_data_end(root
, left
) - push_space
,
1959 btrfs_leaf_data(right
) +
1960 btrfs_item_offset_nr(right
, push_items
- 1),
1962 old_left_nritems
= btrfs_header_nritems(left
);
1963 BUG_ON(old_left_nritems
< 0);
1965 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1966 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1969 item
= btrfs_item_nr(left
, i
);
1970 ioff
= btrfs_item_offset(left
, item
);
1971 btrfs_set_item_offset(left
, item
,
1972 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
1974 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1976 /* fixup right node */
1977 if (push_items
> right_nritems
) {
1978 printk("push items %d nr %u\n", push_items
, right_nritems
);
1982 if (push_items
< right_nritems
) {
1983 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1984 leaf_data_end(root
, right
);
1985 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1986 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1987 btrfs_leaf_data(right
) +
1988 leaf_data_end(root
, right
), push_space
);
1990 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1991 btrfs_item_nr_offset(push_items
),
1992 (btrfs_header_nritems(right
) - push_items
) *
1993 sizeof(struct btrfs_item
));
1995 right_nritems
-= push_items
;
1996 btrfs_set_header_nritems(right
, right_nritems
);
1997 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1998 for (i
= 0; i
< right_nritems
; i
++) {
1999 item
= btrfs_item_nr(right
, i
);
2000 push_space
= push_space
- btrfs_item_size(right
, item
);
2001 btrfs_set_item_offset(right
, item
, push_space
);
2004 btrfs_mark_buffer_dirty(left
);
2006 btrfs_mark_buffer_dirty(right
);
2008 btrfs_item_key(right
, &disk_key
, 0);
2009 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2013 /* then fixup the leaf pointer in the path */
2014 if (path
->slots
[0] < push_items
) {
2015 path
->slots
[0] += old_left_nritems
;
2016 free_extent_buffer(path
->nodes
[0]);
2017 path
->nodes
[0] = left
;
2018 path
->slots
[1] -= 1;
2020 free_extent_buffer(left
);
2021 path
->slots
[0] -= push_items
;
2023 BUG_ON(path
->slots
[0] < 0);
2028 * split the path's leaf in two, making sure there is at least data_size
2029 * available for the resulting leaf level of the path.
2031 * returns 0 if all went well and < 0 on failure.
2033 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
2034 struct btrfs_root
*root
,
2035 struct btrfs_path
*path
,
2036 struct extent_buffer
*l
,
2037 struct extent_buffer
*right
,
2038 int slot
, int mid
, int nritems
)
2045 struct btrfs_disk_key disk_key
;
2047 nritems
= nritems
- mid
;
2048 btrfs_set_header_nritems(right
, nritems
);
2049 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2051 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2052 btrfs_item_nr_offset(mid
),
2053 nritems
* sizeof(struct btrfs_item
));
2055 copy_extent_buffer(right
, l
,
2056 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2057 data_copy_size
, btrfs_leaf_data(l
) +
2058 leaf_data_end(root
, l
), data_copy_size
);
2060 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2061 btrfs_item_end_nr(l
, mid
);
2063 for (i
= 0; i
< nritems
; i
++) {
2064 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2065 u32 ioff
= btrfs_item_offset(right
, item
);
2066 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2069 btrfs_set_header_nritems(l
, mid
);
2071 btrfs_item_key(right
, &disk_key
, 0);
2072 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2073 path
->slots
[1] + 1, 1);
2077 btrfs_mark_buffer_dirty(right
);
2078 btrfs_mark_buffer_dirty(l
);
2079 BUG_ON(path
->slots
[0] != slot
);
2082 free_extent_buffer(path
->nodes
[0]);
2083 path
->nodes
[0] = right
;
2084 path
->slots
[0] -= mid
;
2085 path
->slots
[1] += 1;
2087 free_extent_buffer(right
);
2090 BUG_ON(path
->slots
[0] < 0);
2096 * split the path's leaf in two, making sure there is at least data_size
2097 * available for the resulting leaf level of the path.
2099 * returns 0 if all went well and < 0 on failure.
2101 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2102 struct btrfs_root
*root
,
2103 struct btrfs_key
*ins_key
,
2104 struct btrfs_path
*path
, int data_size
,
2107 struct btrfs_disk_key disk_key
;
2108 struct extent_buffer
*l
;
2112 struct extent_buffer
*right
;
2116 int num_doubles
= 0;
2118 /* first try to make some room by pushing left and right */
2119 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2120 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2124 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2130 /* did the pushes work? */
2131 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2135 if (!path
->nodes
[1]) {
2136 ret
= insert_new_root(trans
, root
, path
, 1);
2143 slot
= path
->slots
[0];
2144 nritems
= btrfs_header_nritems(l
);
2145 mid
= (nritems
+ 1) / 2;
2149 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2150 BTRFS_LEAF_DATA_SIZE(root
)) {
2151 if (slot
>= nritems
) {
2155 if (mid
!= nritems
&&
2156 leaf_space_used(l
, mid
, nritems
- mid
) +
2157 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2163 if (leaf_space_used(l
, 0, mid
) + data_size
>
2164 BTRFS_LEAF_DATA_SIZE(root
)) {
2165 if (!extend
&& data_size
&& slot
== 0) {
2167 } else if ((extend
|| !data_size
) && slot
== 0) {
2171 if (mid
!= nritems
&&
2172 leaf_space_used(l
, mid
, nritems
- mid
) +
2173 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2181 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2183 btrfs_item_key(l
, &disk_key
, mid
);
2185 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2186 root
->root_key
.objectid
,
2187 &disk_key
, 0, l
->start
, 0);
2188 if (IS_ERR(right
)) {
2190 return PTR_ERR(right
);
2193 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2194 btrfs_set_header_bytenr(right
, right
->start
);
2195 btrfs_set_header_generation(right
, trans
->transid
);
2196 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2197 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2198 btrfs_set_header_level(right
, 0);
2199 write_extent_buffer(right
, root
->fs_info
->fsid
,
2200 (unsigned long)btrfs_header_fsid(right
),
2203 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2204 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
2209 btrfs_set_header_nritems(right
, 0);
2210 wret
= insert_ptr(trans
, root
, path
,
2211 &disk_key
, right
->start
,
2212 path
->slots
[1] + 1, 1);
2216 free_extent_buffer(path
->nodes
[0]);
2217 path
->nodes
[0] = right
;
2219 path
->slots
[1] += 1;
2221 btrfs_set_header_nritems(right
, 0);
2222 wret
= insert_ptr(trans
, root
, path
,
2228 free_extent_buffer(path
->nodes
[0]);
2229 path
->nodes
[0] = right
;
2231 if (path
->slots
[1] == 0) {
2232 wret
= fixup_low_keys(trans
, root
,
2233 path
, &disk_key
, 1);
2238 btrfs_mark_buffer_dirty(right
);
2242 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2246 BUG_ON(num_doubles
!= 0);
2255 * This function splits a single item into two items,
2256 * giving 'new_key' to the new item and splitting the
2257 * old one at split_offset (from the start of the item).
2259 * The path may be released by this operation. After
2260 * the split, the path is pointing to the old item. The
2261 * new item is going to be in the same node as the old one.
2263 * Note, the item being split must be smaller enough to live alone on
2264 * a tree block with room for one extra struct btrfs_item
2266 * This allows us to split the item in place, keeping a lock on the
2267 * leaf the entire time.
2269 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2270 struct btrfs_root
*root
,
2271 struct btrfs_path
*path
,
2272 struct btrfs_key
*new_key
,
2273 unsigned long split_offset
)
2276 struct extent_buffer
*leaf
;
2277 struct btrfs_key orig_key
;
2278 struct btrfs_item
*item
;
2279 struct btrfs_item
*new_item
;
2284 struct btrfs_disk_key disk_key
;
2287 leaf
= path
->nodes
[0];
2288 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
2289 if (btrfs_leaf_free_space(root
, leaf
) >= sizeof(struct btrfs_item
))
2292 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2293 btrfs_release_path(root
, path
);
2295 path
->search_for_split
= 1;
2297 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
2298 path
->search_for_split
= 0;
2300 /* if our item isn't there or got smaller, return now */
2301 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
2306 ret
= split_leaf(trans
, root
, &orig_key
, path
, 0, 0);
2309 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
2310 leaf
= path
->nodes
[0];
2313 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
2314 orig_offset
= btrfs_item_offset(leaf
, item
);
2315 item_size
= btrfs_item_size(leaf
, item
);
2318 buf
= kmalloc(item_size
, GFP_NOFS
);
2319 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
2320 path
->slots
[0]), item_size
);
2321 slot
= path
->slots
[0] + 1;
2322 leaf
= path
->nodes
[0];
2324 nritems
= btrfs_header_nritems(leaf
);
2326 if (slot
!= nritems
) {
2327 /* shift the items */
2328 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2329 btrfs_item_nr_offset(slot
),
2330 (nritems
- slot
) * sizeof(struct btrfs_item
));
2334 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
2335 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2337 new_item
= btrfs_item_nr(leaf
, slot
);
2339 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
2340 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
2342 btrfs_set_item_offset(leaf
, item
,
2343 orig_offset
+ item_size
- split_offset
);
2344 btrfs_set_item_size(leaf
, item
, split_offset
);
2346 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2348 /* write the data for the start of the original item */
2349 write_extent_buffer(leaf
, buf
,
2350 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2353 /* write the data for the new item */
2354 write_extent_buffer(leaf
, buf
+ split_offset
,
2355 btrfs_item_ptr_offset(leaf
, slot
),
2356 item_size
- split_offset
);
2357 btrfs_mark_buffer_dirty(leaf
);
2360 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2361 btrfs_print_leaf(root
, leaf
);
2368 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
2369 struct btrfs_root
*root
,
2370 struct btrfs_path
*path
,
2371 u32 new_size
, int from_end
)
2375 struct extent_buffer
*leaf
;
2376 struct btrfs_item
*item
;
2378 unsigned int data_end
;
2379 unsigned int old_data_start
;
2380 unsigned int old_size
;
2381 unsigned int size_diff
;
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
,
2471 struct extent_buffer
*leaf
;
2472 struct btrfs_item
*item
;
2474 unsigned int data_end
;
2475 unsigned int old_data
;
2476 unsigned int old_size
;
2479 leaf
= path
->nodes
[0];
2481 nritems
= btrfs_header_nritems(leaf
);
2482 data_end
= leaf_data_end(root
, leaf
);
2484 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2485 btrfs_print_leaf(root
, leaf
);
2488 slot
= path
->slots
[0];
2489 old_data
= btrfs_item_end_nr(leaf
, slot
);
2492 if (slot
>= nritems
) {
2493 btrfs_print_leaf(root
, leaf
);
2494 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2499 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2501 /* first correct the data pointers */
2502 for (i
= slot
; i
< nritems
; i
++) {
2504 item
= btrfs_item_nr(leaf
, i
);
2505 ioff
= btrfs_item_offset(leaf
, item
);
2506 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2509 /* shift the data */
2510 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2511 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2512 data_end
, old_data
- data_end
);
2514 data_end
= old_data
;
2515 old_size
= btrfs_item_size_nr(leaf
, slot
);
2516 item
= btrfs_item_nr(leaf
, slot
);
2517 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2518 btrfs_mark_buffer_dirty(leaf
);
2521 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2522 btrfs_print_leaf(root
, leaf
);
2529 * Given a key and some data, insert an item into the tree.
2530 * This does all the path init required, making room in the tree if needed.
2532 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2533 struct btrfs_root
*root
,
2534 struct btrfs_path
*path
,
2535 struct btrfs_key
*cpu_key
, u32
*data_size
,
2538 struct extent_buffer
*leaf
;
2539 struct btrfs_item
*item
;
2546 unsigned int data_end
;
2547 struct btrfs_disk_key disk_key
;
2549 for (i
= 0; i
< nr
; i
++) {
2550 total_data
+= data_size
[i
];
2553 /* create a root if there isn't one */
2557 total_size
= total_data
+ nr
* sizeof(struct btrfs_item
);
2558 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2565 leaf
= path
->nodes
[0];
2567 nritems
= btrfs_header_nritems(leaf
);
2568 data_end
= leaf_data_end(root
, leaf
);
2570 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
2571 btrfs_print_leaf(root
, leaf
);
2572 printk("not enough freespace need %u have %d\n",
2573 total_size
, btrfs_leaf_free_space(root
, leaf
));
2577 slot
= path
->slots
[0];
2580 if (slot
!= nritems
) {
2582 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2584 if (old_data
< data_end
) {
2585 btrfs_print_leaf(root
, leaf
);
2586 printk("slot %d old_data %d data_end %d\n",
2587 slot
, old_data
, data_end
);
2591 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2593 /* first correct the data pointers */
2594 for (i
= slot
; i
< nritems
; i
++) {
2597 item
= btrfs_item_nr(leaf
, i
);
2598 ioff
= btrfs_item_offset(leaf
, item
);
2599 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2602 /* shift the items */
2603 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2604 btrfs_item_nr_offset(slot
),
2605 (nritems
- slot
) * sizeof(struct btrfs_item
));
2607 /* shift the data */
2608 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2609 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2610 data_end
, old_data
- data_end
);
2611 data_end
= old_data
;
2614 /* setup the item for the new data */
2615 for (i
= 0; i
< nr
; i
++) {
2616 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2617 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2618 item
= btrfs_item_nr(leaf
, slot
+ i
);
2619 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2620 data_end
-= data_size
[i
];
2621 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2623 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2624 btrfs_mark_buffer_dirty(leaf
);
2628 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2629 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2632 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2633 btrfs_print_leaf(root
, leaf
);
2642 * Given a key and some data, insert an item into the tree.
2643 * This does all the path init required, making room in the tree if needed.
2645 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2646 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2650 struct btrfs_path
*path
;
2651 struct extent_buffer
*leaf
;
2654 path
= btrfs_alloc_path();
2656 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2658 leaf
= path
->nodes
[0];
2659 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2660 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2661 btrfs_mark_buffer_dirty(leaf
);
2663 btrfs_free_path(path
);
2668 * delete the pointer from a given node.
2670 * If the delete empties a node, the node is removed from the tree,
2671 * continuing all the way the root if required. The root is converted into
2672 * a leaf if all the nodes are emptied.
2674 int btrfs_del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2675 struct btrfs_path
*path
, int level
, int slot
)
2677 struct extent_buffer
*parent
= path
->nodes
[level
];
2682 nritems
= btrfs_header_nritems(parent
);
2683 if (slot
!= nritems
-1) {
2684 memmove_extent_buffer(parent
,
2685 btrfs_node_key_ptr_offset(slot
),
2686 btrfs_node_key_ptr_offset(slot
+ 1),
2687 sizeof(struct btrfs_key_ptr
) *
2688 (nritems
- slot
- 1));
2691 btrfs_set_header_nritems(parent
, nritems
);
2692 if (nritems
== 0 && parent
== root
->node
) {
2693 BUG_ON(btrfs_header_level(root
->node
) != 1);
2694 /* just turn the root into a leaf and break */
2695 btrfs_set_header_level(root
->node
, 0);
2696 } else if (slot
== 0) {
2697 struct btrfs_disk_key disk_key
;
2699 btrfs_node_key(parent
, &disk_key
, 0);
2700 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
2704 btrfs_mark_buffer_dirty(parent
);
2709 * a helper function to delete the leaf pointed to by path->slots[1] and
2712 * This deletes the pointer in path->nodes[1] and frees the leaf
2713 * block extent. zero is returned if it all worked out, < 0 otherwise.
2715 * The path must have already been setup for deleting the leaf, including
2716 * all the proper balancing. path->nodes[1] must be locked.
2718 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
2719 struct btrfs_root
*root
,
2720 struct btrfs_path
*path
,
2721 struct extent_buffer
*leaf
)
2725 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
2726 ret
= btrfs_del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
2730 ret
= btrfs_free_extent(trans
, root
, leaf
->start
, leaf
->len
,
2731 0, root
->root_key
.objectid
, 0, 0);
2736 * delete the item at the leaf level in path. If that empties
2737 * the leaf, remove it from the tree
2739 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2740 struct btrfs_path
*path
, int slot
, int nr
)
2742 struct extent_buffer
*leaf
;
2743 struct btrfs_item
*item
;
2751 leaf
= path
->nodes
[0];
2752 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2754 for (i
= 0; i
< nr
; i
++)
2755 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2757 nritems
= btrfs_header_nritems(leaf
);
2759 if (slot
+ nr
!= nritems
) {
2761 int data_end
= leaf_data_end(root
, leaf
);
2763 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2765 btrfs_leaf_data(leaf
) + data_end
,
2766 last_off
- data_end
);
2768 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2771 item
= btrfs_item_nr(leaf
, i
);
2772 ioff
= btrfs_item_offset(leaf
, item
);
2773 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2776 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2777 btrfs_item_nr_offset(slot
+ nr
),
2778 sizeof(struct btrfs_item
) *
2779 (nritems
- slot
- nr
));
2781 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2784 /* delete the leaf if we've emptied it */
2786 if (leaf
== root
->node
) {
2787 btrfs_set_header_level(leaf
, 0);
2789 clean_tree_block(trans
, root
, leaf
);
2790 wait_on_tree_block_writeback(root
, leaf
);
2792 wret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2798 int used
= leaf_space_used(leaf
, 0, nritems
);
2800 struct btrfs_disk_key disk_key
;
2802 btrfs_item_key(leaf
, &disk_key
, 0);
2803 wret
= fixup_low_keys(trans
, root
, path
,
2809 /* delete the leaf if it is mostly empty */
2810 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 4) {
2811 /* push_leaf_left fixes the path.
2812 * make sure the path still points to our leaf
2813 * for possible call to del_ptr below
2815 slot
= path
->slots
[1];
2816 extent_buffer_get(leaf
);
2818 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2819 if (wret
< 0 && wret
!= -ENOSPC
)
2822 if (path
->nodes
[0] == leaf
&&
2823 btrfs_header_nritems(leaf
)) {
2824 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2825 if (wret
< 0 && wret
!= -ENOSPC
)
2829 if (btrfs_header_nritems(leaf
) == 0) {
2830 clean_tree_block(trans
, root
, leaf
);
2831 wait_on_tree_block_writeback(root
, leaf
);
2833 path
->slots
[1] = slot
;
2834 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2836 free_extent_buffer(leaf
);
2839 btrfs_mark_buffer_dirty(leaf
);
2840 free_extent_buffer(leaf
);
2843 btrfs_mark_buffer_dirty(leaf
);
2850 * walk up the tree as far as required to find the previous leaf.
2851 * returns 0 if it found something or 1 if there are no lesser leaves.
2852 * returns < 0 on io errors.
2854 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2858 struct extent_buffer
*c
;
2859 struct extent_buffer
*next
= NULL
;
2861 while(level
< BTRFS_MAX_LEVEL
) {
2862 if (!path
->nodes
[level
])
2865 slot
= path
->slots
[level
];
2866 c
= path
->nodes
[level
];
2869 if (level
== BTRFS_MAX_LEVEL
)
2876 free_extent_buffer(next
);
2878 next
= read_node_slot(root
, c
, slot
);
2881 path
->slots
[level
] = slot
;
2884 c
= path
->nodes
[level
];
2885 free_extent_buffer(c
);
2886 slot
= btrfs_header_nritems(next
);
2889 path
->nodes
[level
] = next
;
2890 path
->slots
[level
] = slot
;
2893 next
= read_node_slot(root
, next
, slot
);
2899 * walk up the tree as far as required to find the next leaf.
2900 * returns 0 if it found something or 1 if there are no greater leaves.
2901 * returns < 0 on io errors.
2903 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2907 struct extent_buffer
*c
;
2908 struct extent_buffer
*next
= NULL
;
2910 while(level
< BTRFS_MAX_LEVEL
) {
2911 if (!path
->nodes
[level
])
2914 slot
= path
->slots
[level
] + 1;
2915 c
= path
->nodes
[level
];
2916 if (slot
>= btrfs_header_nritems(c
)) {
2918 if (level
== BTRFS_MAX_LEVEL
)
2924 free_extent_buffer(next
);
2927 reada_for_search(root
, path
, level
, slot
, 0);
2929 next
= read_node_slot(root
, c
, slot
);
2934 path
->slots
[level
] = slot
;
2937 c
= path
->nodes
[level
];
2938 free_extent_buffer(c
);
2939 path
->nodes
[level
] = next
;
2940 path
->slots
[level
] = 0;
2944 reada_for_search(root
, path
, level
, 0, 0);
2945 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
)