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"
27 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
28 *root
, struct btrfs_path
*path
, int level
);
29 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
30 *root
, struct btrfs_key
*ins_key
,
31 struct btrfs_path
*path
, int data_size
, int extend
);
32 static int push_node_left(struct btrfs_trans_handle
*trans
,
33 struct btrfs_root
*root
, struct extent_buffer
*dst
,
34 struct extent_buffer
*src
, int empty
);
35 static int balance_node_right(struct btrfs_trans_handle
*trans
,
36 struct btrfs_root
*root
,
37 struct extent_buffer
*dst_buf
,
38 struct extent_buffer
*src_buf
);
40 inline void btrfs_init_path(struct btrfs_path
*p
)
42 memset(p
, 0, sizeof(*p
));
45 struct btrfs_path
*btrfs_alloc_path(void)
47 struct btrfs_path
*path
;
48 path
= kzalloc(sizeof(struct btrfs_path
), GFP_NOFS
);
52 void btrfs_free_path(struct btrfs_path
*p
)
56 btrfs_release_path(p
);
60 void btrfs_release_path(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 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 static void root_add_used(struct btrfs_root
*root
, u32 size
)
81 btrfs_set_root_used(&root
->root_item
,
82 btrfs_root_used(&root
->root_item
) + size
);
85 static void root_sub_used(struct btrfs_root
*root
, u32 size
)
87 btrfs_set_root_used(&root
->root_item
,
88 btrfs_root_used(&root
->root_item
) - size
);
91 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
92 struct btrfs_root
*root
,
93 struct extent_buffer
*buf
,
94 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
96 struct extent_buffer
*cow
;
99 struct btrfs_root
*new_root
;
100 struct btrfs_disk_key disk_key
;
102 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
106 memcpy(new_root
, root
, sizeof(*new_root
));
107 new_root
->root_key
.objectid
= new_root_objectid
;
109 WARN_ON(root
->ref_cows
&& trans
->transid
!=
110 root
->fs_info
->running_transaction
->transid
);
111 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
113 level
= btrfs_header_level(buf
);
115 btrfs_item_key(buf
, &disk_key
, 0);
117 btrfs_node_key(buf
, &disk_key
, 0);
118 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
119 new_root_objectid
, &disk_key
,
120 level
, buf
->start
, 0);
126 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
127 btrfs_set_header_bytenr(cow
, cow
->start
);
128 btrfs_set_header_generation(cow
, trans
->transid
);
129 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
130 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
131 BTRFS_HEADER_FLAG_RELOC
);
132 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
133 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
135 btrfs_set_header_owner(cow
, new_root_objectid
);
137 write_extent_buffer(cow
, root
->fs_info
->fsid
,
138 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
140 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
141 ret
= btrfs_inc_ref(trans
, new_root
, cow
, 0);
147 btrfs_mark_buffer_dirty(cow
);
153 * check if the tree block can be shared by multiple trees
155 static int btrfs_block_can_be_shared(struct btrfs_root
*root
,
156 struct extent_buffer
*buf
)
159 * Tree blocks not in reference counted trees and tree roots
160 * are never shared. If a block was allocated after the last
161 * snapshot and the block was not allocated by tree relocation,
162 * we know the block is not shared.
164 if (root
->ref_cows
&&
165 buf
!= root
->node
&& buf
!= root
->commit_root
&&
166 (btrfs_header_generation(buf
) <=
167 btrfs_root_last_snapshot(&root
->root_item
) ||
168 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
170 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
171 if (root
->ref_cows
&&
172 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
178 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
179 struct btrfs_root
*root
,
180 struct extent_buffer
*buf
,
181 struct extent_buffer
*cow
)
190 * Backrefs update rules:
192 * Always use full backrefs for extent pointers in tree block
193 * allocated by tree relocation.
195 * If a shared tree block is no longer referenced by its owner
196 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
197 * use full backrefs for extent pointers in tree block.
199 * If a tree block is been relocating
200 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
201 * use full backrefs for extent pointers in tree block.
202 * The reason for this is some operations (such as drop tree)
203 * are only allowed for blocks use full backrefs.
206 if (btrfs_block_can_be_shared(root
, buf
)) {
207 ret
= btrfs_lookup_extent_info(trans
, trans
->fs_info
,
209 btrfs_header_level(buf
), 1,
215 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
216 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
217 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
222 owner
= btrfs_header_owner(buf
);
223 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) &&
224 owner
== BTRFS_TREE_RELOC_OBJECTID
);
227 if ((owner
== root
->root_key
.objectid
||
228 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
229 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
230 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
233 if (root
->root_key
.objectid
==
234 BTRFS_TREE_RELOC_OBJECTID
) {
235 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
237 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
240 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
243 if (root
->root_key
.objectid
==
244 BTRFS_TREE_RELOC_OBJECTID
)
245 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
247 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
250 if (new_flags
!= 0) {
251 ret
= btrfs_set_block_flags(trans
, buf
->start
,
252 btrfs_header_level(buf
),
257 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
258 if (root
->root_key
.objectid
==
259 BTRFS_TREE_RELOC_OBJECTID
)
260 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
262 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
264 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
267 clean_tree_block(buf
);
272 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
273 struct btrfs_root
*root
,
274 struct extent_buffer
*buf
,
275 struct extent_buffer
*parent
, int parent_slot
,
276 struct extent_buffer
**cow_ret
,
277 u64 search_start
, u64 empty_size
)
279 struct extent_buffer
*cow
;
280 struct btrfs_disk_key disk_key
;
283 WARN_ON(root
->ref_cows
&& trans
->transid
!=
284 root
->fs_info
->running_transaction
->transid
);
285 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
287 level
= btrfs_header_level(buf
);
290 btrfs_item_key(buf
, &disk_key
, 0);
292 btrfs_node_key(buf
, &disk_key
, 0);
294 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
,
295 root
->root_key
.objectid
, &disk_key
,
296 level
, search_start
, empty_size
);
300 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
301 btrfs_set_header_bytenr(cow
, cow
->start
);
302 btrfs_set_header_generation(cow
, trans
->transid
);
303 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
304 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
305 BTRFS_HEADER_FLAG_RELOC
);
306 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
307 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
309 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
311 write_extent_buffer(cow
, root
->fs_info
->fsid
,
312 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
314 WARN_ON(!(buf
->flags
& EXTENT_BAD_TRANSID
) &&
315 btrfs_header_generation(buf
) > trans
->transid
);
317 update_ref_for_cow(trans
, root
, buf
, cow
);
319 if (buf
== root
->node
) {
321 extent_buffer_get(cow
);
323 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
324 0, root
->root_key
.objectid
, level
, 0);
325 free_extent_buffer(buf
);
326 add_root_to_dirty_list(root
);
328 btrfs_set_node_blockptr(parent
, parent_slot
,
330 WARN_ON(trans
->transid
== 0);
331 btrfs_set_node_ptr_generation(parent
, parent_slot
,
333 btrfs_mark_buffer_dirty(parent
);
334 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
336 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
337 0, root
->root_key
.objectid
, level
, 0);
339 if (!list_empty(&buf
->recow
)) {
340 list_del_init(&buf
->recow
);
341 free_extent_buffer(buf
);
343 free_extent_buffer(buf
);
344 btrfs_mark_buffer_dirty(cow
);
349 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
350 struct btrfs_root
*root
,
351 struct extent_buffer
*buf
)
353 if (btrfs_header_generation(buf
) == trans
->transid
&&
354 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
355 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
356 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
361 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
362 struct btrfs_root
*root
, struct extent_buffer
*buf
,
363 struct extent_buffer
*parent
, int parent_slot
,
364 struct extent_buffer
**cow_ret
)
369 if (trans->transaction != root->fs_info->running_transaction) {
370 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
371 root->fs_info->running_transaction->transid);
375 if (trans
->transid
!= root
->fs_info
->generation
) {
376 printk(KERN_CRIT
"trans %llu running %llu\n",
377 (unsigned long long)trans
->transid
,
378 (unsigned long long)root
->fs_info
->generation
);
381 if (!should_cow_block(trans
, root
, buf
)) {
386 search_start
= buf
->start
& ~((u64
)SZ_1G
- 1);
387 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
388 parent_slot
, cow_ret
, search_start
, 0);
392 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
394 if (k1
->objectid
> k2
->objectid
)
396 if (k1
->objectid
< k2
->objectid
)
398 if (k1
->type
> k2
->type
)
400 if (k1
->type
< k2
->type
)
402 if (k1
->offset
> k2
->offset
)
404 if (k1
->offset
< k2
->offset
)
410 * compare two keys in a memcmp fashion
412 static int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
416 btrfs_disk_key_to_cpu(&k1
, disk
);
417 return btrfs_comp_cpu_keys(&k1
, k2
);
421 * The leaf data grows from end-to-front in the node.
422 * this returns the address of the start of the last item,
423 * which is the stop of the leaf data stack
425 static inline unsigned int leaf_data_end(const struct btrfs_fs_info
*fs_info
,
426 const struct extent_buffer
*leaf
)
428 u32 nr
= btrfs_header_nritems(leaf
);
430 return BTRFS_LEAF_DATA_SIZE(fs_info
);
431 return btrfs_item_offset_nr(leaf
, nr
- 1);
434 enum btrfs_tree_block_status
435 btrfs_check_node(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
436 struct extent_buffer
*buf
)
439 struct btrfs_key cpukey
;
440 struct btrfs_disk_key key
;
441 u32 nritems
= btrfs_header_nritems(buf
);
442 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
444 if (nritems
== 0 || nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
))
447 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
448 if (parent_key
&& parent_key
->type
) {
449 btrfs_node_key(buf
, &key
, 0);
450 if (memcmp(parent_key
, &key
, sizeof(key
)))
453 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
454 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
455 btrfs_node_key(buf
, &key
, i
);
456 btrfs_node_key_to_cpu(buf
, &cpukey
, i
+ 1);
457 if (btrfs_comp_keys(&key
, &cpukey
) >= 0)
460 return BTRFS_TREE_BLOCK_CLEAN
;
462 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
464 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
466 btrfs_node_key_to_cpu(buf
, &cpukey
, 0);
467 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
468 buf
->start
, buf
->len
,
469 btrfs_header_level(buf
));
474 enum btrfs_tree_block_status
475 btrfs_check_leaf(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
476 struct extent_buffer
*buf
)
479 struct btrfs_key cpukey
;
480 struct btrfs_disk_key key
;
481 u32 nritems
= btrfs_header_nritems(buf
);
482 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
484 if (nritems
* sizeof(struct btrfs_item
) > buf
->len
) {
485 fprintf(stderr
, "invalid number of items %llu\n",
486 (unsigned long long)buf
->start
);
490 if (btrfs_header_level(buf
) != 0) {
491 ret
= BTRFS_TREE_BLOCK_INVALID_LEVEL
;
492 fprintf(stderr
, "leaf is not a leaf %llu\n",
493 (unsigned long long)btrfs_header_bytenr(buf
));
496 if (btrfs_leaf_free_space(buf
) < 0) {
497 ret
= BTRFS_TREE_BLOCK_INVALID_FREE_SPACE
;
498 fprintf(stderr
, "leaf free space incorrect %llu %d\n",
499 (unsigned long long)btrfs_header_bytenr(buf
),
500 btrfs_leaf_free_space(buf
));
505 return BTRFS_TREE_BLOCK_CLEAN
;
507 btrfs_item_key(buf
, &key
, 0);
508 if (parent_key
&& parent_key
->type
&&
509 memcmp(parent_key
, &key
, sizeof(key
))) {
510 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
511 fprintf(stderr
, "leaf parent key incorrect %llu\n",
512 (unsigned long long)btrfs_header_bytenr(buf
));
515 for (i
= 0; nritems
> 1 && i
< nritems
- 1; i
++) {
516 btrfs_item_key(buf
, &key
, i
);
517 btrfs_item_key_to_cpu(buf
, &cpukey
, i
+ 1);
518 if (btrfs_comp_keys(&key
, &cpukey
) >= 0) {
519 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
520 fprintf(stderr
, "bad key ordering %d %d\n", i
, i
+1);
523 if (btrfs_item_offset_nr(buf
, i
) !=
524 btrfs_item_end_nr(buf
, i
+ 1)) {
525 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
526 fprintf(stderr
, "incorrect offsets %u %u\n",
527 btrfs_item_offset_nr(buf
, i
),
528 btrfs_item_end_nr(buf
, i
+ 1));
531 if (i
== 0 && btrfs_item_end_nr(buf
, i
) !=
532 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
533 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
534 fprintf(stderr
, "bad item end %u wanted %u\n",
535 btrfs_item_end_nr(buf
, i
),
536 (unsigned)BTRFS_LEAF_DATA_SIZE(root
->fs_info
));
541 for (i
= 0; i
< nritems
; i
++) {
542 if (btrfs_item_end_nr(buf
, i
) >
543 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
544 btrfs_item_key(buf
, &key
, 0);
545 btrfs_print_key(&key
);
547 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
548 fprintf(stderr
, "slot end outside of leaf %llu > %llu\n",
549 (unsigned long long)btrfs_item_end_nr(buf
, i
),
550 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
556 return BTRFS_TREE_BLOCK_CLEAN
;
558 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
560 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
562 btrfs_item_key_to_cpu(buf
, &cpukey
, 0);
564 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
565 buf
->start
, buf
->len
, 0);
570 static int noinline
check_block(struct btrfs_root
*root
,
571 struct btrfs_path
*path
, int level
)
573 struct btrfs_disk_key key
;
574 struct btrfs_disk_key
*key_ptr
= NULL
;
575 struct extent_buffer
*parent
;
576 enum btrfs_tree_block_status ret
;
578 if (path
->skip_check_block
)
580 if (path
->nodes
[level
+ 1]) {
581 parent
= path
->nodes
[level
+ 1];
582 btrfs_node_key(parent
, &key
, path
->slots
[level
+ 1]);
586 ret
= btrfs_check_leaf(root
, key_ptr
, path
->nodes
[0]);
588 ret
= btrfs_check_node(root
, key_ptr
, path
->nodes
[level
]);
589 if (ret
== BTRFS_TREE_BLOCK_CLEAN
)
595 * search for key in the extent_buffer. The items start at offset p,
596 * and they are item_size apart. There are 'max' items in p.
598 * the slot in the array is returned via slot, and it points to
599 * the place where you would insert key if it is not found in
602 * slot may point to max if the key is bigger than all of the keys
604 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
605 int item_size
, struct btrfs_key
*key
,
612 unsigned long offset
;
613 struct btrfs_disk_key
*tmp
;
616 mid
= (low
+ high
) / 2;
617 offset
= p
+ mid
* item_size
;
619 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
620 ret
= btrfs_comp_keys(tmp
, key
);
636 * simple bin_search frontend that does the right thing for
639 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
640 int level
, int *slot
)
643 return generic_bin_search(eb
,
644 offsetof(struct btrfs_leaf
, items
),
645 sizeof(struct btrfs_item
),
646 key
, btrfs_header_nritems(eb
),
649 return generic_bin_search(eb
,
650 offsetof(struct btrfs_node
, ptrs
),
651 sizeof(struct btrfs_key_ptr
),
652 key
, btrfs_header_nritems(eb
),
656 struct extent_buffer
*read_node_slot(struct btrfs_fs_info
*fs_info
,
657 struct extent_buffer
*parent
, int slot
)
659 struct extent_buffer
*ret
;
660 int level
= btrfs_header_level(parent
);
664 if (slot
>= btrfs_header_nritems(parent
))
670 ret
= read_tree_block(fs_info
, btrfs_node_blockptr(parent
, slot
),
671 btrfs_node_ptr_generation(parent
, slot
));
672 if (!extent_buffer_uptodate(ret
))
673 return ERR_PTR(-EIO
);
675 if (btrfs_header_level(ret
) != level
- 1) {
677 "child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
678 btrfs_header_bytenr(parent
), slot
,
679 btrfs_header_level(parent
), btrfs_header_level(ret
));
680 free_extent_buffer(ret
);
681 return ERR_PTR(-EIO
);
686 static int balance_level(struct btrfs_trans_handle
*trans
,
687 struct btrfs_root
*root
,
688 struct btrfs_path
*path
, int level
)
690 struct extent_buffer
*right
= NULL
;
691 struct extent_buffer
*mid
;
692 struct extent_buffer
*left
= NULL
;
693 struct extent_buffer
*parent
= NULL
;
694 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
698 int orig_slot
= path
->slots
[level
];
704 mid
= path
->nodes
[level
];
705 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
707 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
709 if (level
< BTRFS_MAX_LEVEL
- 1) {
710 parent
= path
->nodes
[level
+ 1];
711 pslot
= path
->slots
[level
+ 1];
715 * deal with the case where there is only one pointer in the root
716 * by promoting the node below to a root
719 struct extent_buffer
*child
;
721 if (btrfs_header_nritems(mid
) != 1)
724 /* promote the child to a root */
725 child
= read_node_slot(fs_info
, mid
, 0);
726 BUG_ON(!extent_buffer_uptodate(child
));
727 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
731 add_root_to_dirty_list(root
);
732 path
->nodes
[level
] = NULL
;
733 clean_tree_block(mid
);
734 /* once for the path */
735 free_extent_buffer(mid
);
737 root_sub_used(root
, mid
->len
);
739 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
740 0, root
->root_key
.objectid
,
742 /* once for the root ptr */
743 free_extent_buffer(mid
);
746 if (btrfs_header_nritems(mid
) >
747 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) / 4)
750 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
751 if (extent_buffer_uptodate(left
)) {
752 wret
= btrfs_cow_block(trans
, root
, left
,
753 parent
, pslot
- 1, &left
);
759 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
760 if (extent_buffer_uptodate(right
)) {
761 wret
= btrfs_cow_block(trans
, root
, right
,
762 parent
, pslot
+ 1, &right
);
769 /* first, try to make some room in the middle buffer */
771 orig_slot
+= btrfs_header_nritems(left
);
772 wret
= push_node_left(trans
, root
, left
, mid
, 1);
778 * then try to empty the right most buffer into the middle
781 wret
= push_node_left(trans
, root
, mid
, right
, 1);
782 if (wret
< 0 && wret
!= -ENOSPC
)
784 if (btrfs_header_nritems(right
) == 0) {
785 u64 bytenr
= right
->start
;
786 u32 blocksize
= right
->len
;
788 clean_tree_block(right
);
789 free_extent_buffer(right
);
791 wret
= btrfs_del_ptr(root
, path
, level
+ 1, pslot
+ 1);
795 root_sub_used(root
, right
->len
);
796 wret
= btrfs_free_extent(trans
, root
, bytenr
,
798 root
->root_key
.objectid
,
803 struct btrfs_disk_key right_key
;
804 btrfs_node_key(right
, &right_key
, 0);
805 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
806 btrfs_mark_buffer_dirty(parent
);
809 if (btrfs_header_nritems(mid
) == 1) {
811 * we're not allowed to leave a node with one item in the
812 * tree during a delete. A deletion from lower in the tree
813 * could try to delete the only pointer in this node.
814 * So, pull some keys from the left.
815 * There has to be a left pointer at this point because
816 * otherwise we would have pulled some pointers from the
820 wret
= balance_node_right(trans
, root
, mid
, left
);
826 wret
= push_node_left(trans
, root
, left
, mid
, 1);
832 if (btrfs_header_nritems(mid
) == 0) {
833 /* we've managed to empty the middle node, drop it */
834 u64 bytenr
= mid
->start
;
835 u32 blocksize
= mid
->len
;
836 clean_tree_block(mid
);
837 free_extent_buffer(mid
);
839 wret
= btrfs_del_ptr(root
, path
, level
+ 1, pslot
);
843 root_sub_used(root
, blocksize
);
844 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
845 0, root
->root_key
.objectid
,
850 /* update the parent key to reflect our changes */
851 struct btrfs_disk_key mid_key
;
852 btrfs_node_key(mid
, &mid_key
, 0);
853 btrfs_set_node_key(parent
, &mid_key
, pslot
);
854 btrfs_mark_buffer_dirty(parent
);
857 /* update the path */
859 if (btrfs_header_nritems(left
) > orig_slot
) {
860 extent_buffer_get(left
);
861 path
->nodes
[level
] = left
;
862 path
->slots
[level
+ 1] -= 1;
863 path
->slots
[level
] = orig_slot
;
865 free_extent_buffer(mid
);
867 orig_slot
-= btrfs_header_nritems(left
);
868 path
->slots
[level
] = orig_slot
;
871 /* double check we haven't messed things up */
872 check_block(root
, path
, level
);
874 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
878 free_extent_buffer(right
);
880 free_extent_buffer(left
);
884 /* returns zero if the push worked, non-zero otherwise */
885 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
886 struct btrfs_root
*root
,
887 struct btrfs_path
*path
, int level
)
889 struct extent_buffer
*right
= NULL
;
890 struct extent_buffer
*mid
;
891 struct extent_buffer
*left
= NULL
;
892 struct extent_buffer
*parent
= NULL
;
893 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
897 int orig_slot
= path
->slots
[level
];
902 mid
= path
->nodes
[level
];
903 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
905 if (level
< BTRFS_MAX_LEVEL
- 1) {
906 parent
= path
->nodes
[level
+ 1];
907 pslot
= path
->slots
[level
+ 1];
913 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
915 /* first, try to make some room in the middle buffer */
916 if (extent_buffer_uptodate(left
)) {
918 left_nr
= btrfs_header_nritems(left
);
919 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 1) {
922 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
927 wret
= push_node_left(trans
, root
,
934 struct btrfs_disk_key disk_key
;
935 orig_slot
+= left_nr
;
936 btrfs_node_key(mid
, &disk_key
, 0);
937 btrfs_set_node_key(parent
, &disk_key
, pslot
);
938 btrfs_mark_buffer_dirty(parent
);
939 if (btrfs_header_nritems(left
) > orig_slot
) {
940 path
->nodes
[level
] = left
;
941 path
->slots
[level
+ 1] -= 1;
942 path
->slots
[level
] = orig_slot
;
943 free_extent_buffer(mid
);
946 btrfs_header_nritems(left
);
947 path
->slots
[level
] = orig_slot
;
948 free_extent_buffer(left
);
952 free_extent_buffer(left
);
954 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
957 * then try to empty the right most buffer into the middle
959 if (extent_buffer_uptodate(right
)) {
961 right_nr
= btrfs_header_nritems(right
);
962 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - 1) {
965 ret
= btrfs_cow_block(trans
, root
, right
,
971 wret
= balance_node_right(trans
, root
,
978 struct btrfs_disk_key disk_key
;
980 btrfs_node_key(right
, &disk_key
, 0);
981 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
982 btrfs_mark_buffer_dirty(parent
);
984 if (btrfs_header_nritems(mid
) <= orig_slot
) {
985 path
->nodes
[level
] = right
;
986 path
->slots
[level
+ 1] += 1;
987 path
->slots
[level
] = orig_slot
-
988 btrfs_header_nritems(mid
);
989 free_extent_buffer(mid
);
991 free_extent_buffer(right
);
995 free_extent_buffer(right
);
1001 * readahead one full node of leaves
1003 void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
1004 int level
, int slot
, u64 objectid
)
1006 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1007 struct extent_buffer
*node
;
1008 struct btrfs_disk_key disk_key
;
1014 int direction
= path
->reada
;
1015 struct extent_buffer
*eb
;
1022 if (!path
->nodes
[level
])
1025 node
= path
->nodes
[level
];
1026 search
= btrfs_node_blockptr(node
, slot
);
1027 eb
= btrfs_find_tree_block(fs_info
, search
, fs_info
->nodesize
);
1029 free_extent_buffer(eb
);
1033 highest_read
= search
;
1034 lowest_read
= search
;
1036 nritems
= btrfs_header_nritems(node
);
1039 if (direction
< 0) {
1043 } else if (direction
> 0) {
1048 if (path
->reada
< 0 && objectid
) {
1049 btrfs_node_key(node
, &disk_key
, nr
);
1050 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1053 search
= btrfs_node_blockptr(node
, nr
);
1054 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1055 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1056 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1057 readahead_tree_block(fs_info
, search
,
1058 btrfs_node_ptr_generation(node
, nr
));
1059 nread
+= fs_info
->nodesize
;
1062 if (path
->reada
< 2 && (nread
> SZ_256K
|| nscan
> 32))
1064 if(nread
> SZ_1M
|| nscan
> 128)
1067 if (search
< lowest_read
)
1068 lowest_read
= search
;
1069 if (search
> highest_read
)
1070 highest_read
= search
;
1074 int btrfs_find_item(struct btrfs_root
*fs_root
, struct btrfs_path
*found_path
,
1075 u64 iobjectid
, u64 ioff
, u8 key_type
,
1076 struct btrfs_key
*found_key
)
1079 struct btrfs_key key
;
1080 struct extent_buffer
*eb
;
1081 struct btrfs_path
*path
;
1083 key
.type
= key_type
;
1084 key
.objectid
= iobjectid
;
1087 if (found_path
== NULL
) {
1088 path
= btrfs_alloc_path();
1094 ret
= btrfs_search_slot(NULL
, fs_root
, &key
, path
, 0, 0);
1095 if ((ret
< 0) || (found_key
== NULL
))
1098 eb
= path
->nodes
[0];
1099 if (ret
&& path
->slots
[0] >= btrfs_header_nritems(eb
)) {
1100 ret
= btrfs_next_leaf(fs_root
, path
);
1103 eb
= path
->nodes
[0];
1106 btrfs_item_key_to_cpu(eb
, found_key
, path
->slots
[0]);
1107 if (found_key
->type
!= key
.type
||
1108 found_key
->objectid
!= key
.objectid
) {
1114 if (path
!= found_path
)
1115 btrfs_free_path(path
);
1120 * look for key in the tree. path is filled in with nodes along the way
1121 * if key is found, we return zero and you can find the item in the leaf
1122 * level of the path (level 0)
1124 * If the key isn't found, the path points to the slot where it should
1125 * be inserted, and 1 is returned. If there are other errors during the
1126 * search a negative error number is returned.
1128 * if ins_len > 0, nodes and leaves will be split as we walk down the
1129 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1132 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1133 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1136 struct extent_buffer
*b
;
1140 int should_reada
= p
->reada
;
1141 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1142 u8 lowest_level
= 0;
1144 lowest_level
= p
->lowest_level
;
1145 WARN_ON(lowest_level
&& ins_len
> 0);
1146 WARN_ON(p
->nodes
[0] != NULL
);
1148 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1152 extent_buffer_get(b
);
1154 level
= btrfs_header_level(b
);
1157 wret
= btrfs_cow_block(trans
, root
, b
,
1158 p
->nodes
[level
+ 1],
1159 p
->slots
[level
+ 1],
1162 free_extent_buffer(b
);
1166 BUG_ON(!cow
&& ins_len
);
1167 if (level
!= btrfs_header_level(b
))
1169 level
= btrfs_header_level(b
);
1170 p
->nodes
[level
] = b
;
1171 ret
= check_block(root
, p
, level
);
1174 ret
= bin_search(b
, key
, level
, &slot
);
1176 if (ret
&& slot
> 0)
1178 p
->slots
[level
] = slot
;
1179 if ((p
->search_for_split
|| ins_len
> 0) &&
1180 btrfs_header_nritems(b
) >=
1181 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 3) {
1182 int sret
= split_node(trans
, root
, p
, level
);
1186 b
= p
->nodes
[level
];
1187 slot
= p
->slots
[level
];
1188 } else if (ins_len
< 0) {
1189 int sret
= balance_level(trans
, root
, p
,
1193 b
= p
->nodes
[level
];
1195 btrfs_release_path(p
);
1198 slot
= p
->slots
[level
];
1199 BUG_ON(btrfs_header_nritems(b
) == 1);
1201 /* this is only true while dropping a snapshot */
1202 if (level
== lowest_level
)
1206 reada_for_search(root
, p
, level
, slot
,
1209 b
= read_node_slot(fs_info
, b
, slot
);
1210 if (!extent_buffer_uptodate(b
))
1213 p
->slots
[level
] = slot
;
1215 ins_len
> btrfs_leaf_free_space(b
)) {
1216 int sret
= split_leaf(trans
, root
, key
,
1217 p
, ins_len
, ret
== 0);
1229 * adjust the pointers going up the tree, starting at level
1230 * making sure the right key of each node is points to 'key'.
1231 * This is used after shifting pointers to the left, so it stops
1232 * fixing up pointers when a given leaf/node is not in slot 0 of the
1235 void btrfs_fixup_low_keys(struct btrfs_root
*root
, struct btrfs_path
*path
,
1236 struct btrfs_disk_key
*key
, int level
)
1239 struct extent_buffer
*t
;
1241 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1242 int tslot
= path
->slots
[i
];
1243 if (!path
->nodes
[i
])
1246 btrfs_set_node_key(t
, key
, tslot
);
1247 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1256 * This function isn't completely safe. It's the caller's responsibility
1257 * that the new key won't break the order
1259 int btrfs_set_item_key_safe(struct btrfs_root
*root
, struct btrfs_path
*path
,
1260 struct btrfs_key
*new_key
)
1262 struct btrfs_disk_key disk_key
;
1263 struct extent_buffer
*eb
;
1266 eb
= path
->nodes
[0];
1267 slot
= path
->slots
[0];
1269 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1270 if (btrfs_comp_keys(&disk_key
, new_key
) >= 0)
1273 if (slot
< btrfs_header_nritems(eb
) - 1) {
1274 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1275 if (btrfs_comp_keys(&disk_key
, new_key
) <= 0)
1279 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1280 btrfs_set_item_key(eb
, &disk_key
, slot
);
1281 btrfs_mark_buffer_dirty(eb
);
1283 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1288 * update an item key without the safety checks. This is meant to be called by
1291 void btrfs_set_item_key_unsafe(struct btrfs_root
*root
,
1292 struct btrfs_path
*path
,
1293 struct btrfs_key
*new_key
)
1295 struct btrfs_disk_key disk_key
;
1296 struct extent_buffer
*eb
;
1299 eb
= path
->nodes
[0];
1300 slot
= path
->slots
[0];
1302 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1303 btrfs_set_item_key(eb
, &disk_key
, slot
);
1304 btrfs_mark_buffer_dirty(eb
);
1306 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1310 * try to push data from one node into the next node left in the
1313 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1314 * error, and > 0 if there was no room in the left hand block.
1316 static int push_node_left(struct btrfs_trans_handle
*trans
,
1317 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1318 struct extent_buffer
*src
, int empty
)
1325 src_nritems
= btrfs_header_nritems(src
);
1326 dst_nritems
= btrfs_header_nritems(dst
);
1327 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - dst_nritems
;
1328 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1329 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1331 if (!empty
&& src_nritems
<= 8)
1334 if (push_items
<= 0) {
1339 push_items
= min(src_nritems
, push_items
);
1340 if (push_items
< src_nritems
) {
1341 /* leave at least 8 pointers in the node if
1342 * we aren't going to empty it
1344 if (src_nritems
- push_items
< 8) {
1345 if (push_items
<= 8)
1351 push_items
= min(src_nritems
- 8, push_items
);
1353 copy_extent_buffer(dst
, src
,
1354 btrfs_node_key_ptr_offset(dst_nritems
),
1355 btrfs_node_key_ptr_offset(0),
1356 push_items
* sizeof(struct btrfs_key_ptr
));
1358 if (push_items
< src_nritems
) {
1359 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1360 btrfs_node_key_ptr_offset(push_items
),
1361 (src_nritems
- push_items
) *
1362 sizeof(struct btrfs_key_ptr
));
1364 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1365 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1366 btrfs_mark_buffer_dirty(src
);
1367 btrfs_mark_buffer_dirty(dst
);
1373 * try to push data from one node into the next node right in the
1376 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1377 * error, and > 0 if there was no room in the right hand block.
1379 * this will only push up to 1/2 the contents of the left node over
1381 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1382 struct btrfs_root
*root
,
1383 struct extent_buffer
*dst
,
1384 struct extent_buffer
*src
)
1392 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1393 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1395 src_nritems
= btrfs_header_nritems(src
);
1396 dst_nritems
= btrfs_header_nritems(dst
);
1397 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - dst_nritems
;
1398 if (push_items
<= 0) {
1402 if (src_nritems
< 4) {
1406 max_push
= src_nritems
/ 2 + 1;
1407 /* don't try to empty the node */
1408 if (max_push
>= src_nritems
) {
1412 if (max_push
< push_items
)
1413 push_items
= max_push
;
1415 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1416 btrfs_node_key_ptr_offset(0),
1418 sizeof(struct btrfs_key_ptr
));
1420 copy_extent_buffer(dst
, src
,
1421 btrfs_node_key_ptr_offset(0),
1422 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1423 push_items
* sizeof(struct btrfs_key_ptr
));
1425 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1426 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1428 btrfs_mark_buffer_dirty(src
);
1429 btrfs_mark_buffer_dirty(dst
);
1435 * helper function to insert a new root level in the tree.
1436 * A new node is allocated, and a single item is inserted to
1437 * point to the existing root
1439 * returns zero on success or < 0 on failure.
1441 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1442 struct btrfs_root
*root
,
1443 struct btrfs_path
*path
, int level
)
1446 struct extent_buffer
*lower
;
1447 struct extent_buffer
*c
;
1448 struct extent_buffer
*old
;
1449 struct btrfs_disk_key lower_key
;
1451 BUG_ON(path
->nodes
[level
]);
1452 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1454 lower
= path
->nodes
[level
-1];
1456 btrfs_item_key(lower
, &lower_key
, 0);
1458 btrfs_node_key(lower
, &lower_key
, 0);
1460 c
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
1461 root
->root_key
.objectid
, &lower_key
,
1462 level
, root
->node
->start
, 0);
1467 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1468 btrfs_set_header_nritems(c
, 1);
1469 btrfs_set_header_level(c
, level
);
1470 btrfs_set_header_bytenr(c
, c
->start
);
1471 btrfs_set_header_generation(c
, trans
->transid
);
1472 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1473 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1475 root_add_used(root
, root
->fs_info
->nodesize
);
1477 write_extent_buffer(c
, root
->fs_info
->fsid
,
1478 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1480 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1481 btrfs_header_chunk_tree_uuid(c
),
1484 btrfs_set_node_key(c
, &lower_key
, 0);
1485 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1486 lower_gen
= btrfs_header_generation(lower
);
1487 WARN_ON(lower_gen
!= trans
->transid
);
1489 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1491 btrfs_mark_buffer_dirty(c
);
1496 /* the super has an extra ref to root->node */
1497 free_extent_buffer(old
);
1499 add_root_to_dirty_list(root
);
1500 extent_buffer_get(c
);
1501 path
->nodes
[level
] = c
;
1502 path
->slots
[level
] = 0;
1507 * worker function to insert a single pointer in a node.
1508 * the node should have enough room for the pointer already
1510 * slot and level indicate where you want the key to go, and
1511 * blocknr is the block the key points to.
1513 * returns zero on success and < 0 on any error
1515 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1516 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1517 *key
, u64 bytenr
, int slot
, int level
)
1519 struct extent_buffer
*lower
;
1522 BUG_ON(!path
->nodes
[level
]);
1523 lower
= path
->nodes
[level
];
1524 nritems
= btrfs_header_nritems(lower
);
1527 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
))
1529 if (slot
< nritems
) {
1530 /* shift the items */
1531 memmove_extent_buffer(lower
,
1532 btrfs_node_key_ptr_offset(slot
+ 1),
1533 btrfs_node_key_ptr_offset(slot
),
1534 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1536 btrfs_set_node_key(lower
, key
, slot
);
1537 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1538 WARN_ON(trans
->transid
== 0);
1539 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1540 btrfs_set_header_nritems(lower
, nritems
+ 1);
1541 btrfs_mark_buffer_dirty(lower
);
1546 * split the node at the specified level in path in two.
1547 * The path is corrected to point to the appropriate node after the split
1549 * Before splitting this tries to make some room in the node by pushing
1550 * left and right, if either one works, it returns right away.
1552 * returns 0 on success and < 0 on failure
1554 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1555 *root
, struct btrfs_path
*path
, int level
)
1557 struct extent_buffer
*c
;
1558 struct extent_buffer
*split
;
1559 struct btrfs_disk_key disk_key
;
1565 c
= path
->nodes
[level
];
1566 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1567 if (c
== root
->node
) {
1568 /* trying to split the root, lets make a new one */
1569 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1573 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1574 c
= path
->nodes
[level
];
1575 if (!ret
&& btrfs_header_nritems(c
) <
1576 BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - 3)
1582 c_nritems
= btrfs_header_nritems(c
);
1583 mid
= (c_nritems
+ 1) / 2;
1584 btrfs_node_key(c
, &disk_key
, mid
);
1586 split
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
1587 root
->root_key
.objectid
,
1588 &disk_key
, level
, c
->start
, 0);
1590 return PTR_ERR(split
);
1592 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
1593 btrfs_set_header_level(split
, btrfs_header_level(c
));
1594 btrfs_set_header_bytenr(split
, split
->start
);
1595 btrfs_set_header_generation(split
, trans
->transid
);
1596 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
1597 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1598 write_extent_buffer(split
, root
->fs_info
->fsid
,
1599 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1600 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1601 btrfs_header_chunk_tree_uuid(split
),
1604 root_add_used(root
, root
->fs_info
->nodesize
);
1606 copy_extent_buffer(split
, c
,
1607 btrfs_node_key_ptr_offset(0),
1608 btrfs_node_key_ptr_offset(mid
),
1609 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1610 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1611 btrfs_set_header_nritems(c
, mid
);
1614 btrfs_mark_buffer_dirty(c
);
1615 btrfs_mark_buffer_dirty(split
);
1617 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1618 path
->slots
[level
+ 1] + 1,
1623 if (path
->slots
[level
] >= mid
) {
1624 path
->slots
[level
] -= mid
;
1625 free_extent_buffer(c
);
1626 path
->nodes
[level
] = split
;
1627 path
->slots
[level
+ 1] += 1;
1629 free_extent_buffer(split
);
1635 * how many bytes are required to store the items in a leaf. start
1636 * and nr indicate which items in the leaf to check. This totals up the
1637 * space used both by the item structs and the item data
1639 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1642 int nritems
= btrfs_header_nritems(l
);
1643 int end
= min(nritems
, start
+ nr
) - 1;
1647 data_len
= btrfs_item_end_nr(l
, start
);
1648 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1649 data_len
+= sizeof(struct btrfs_item
) * nr
;
1650 WARN_ON(data_len
< 0);
1655 * The space between the end of the leaf items and
1656 * the start of the leaf data. IOW, how much room
1657 * the leaf has left for both items and data
1659 int btrfs_leaf_free_space(struct extent_buffer
*leaf
)
1661 int nritems
= btrfs_header_nritems(leaf
);
1665 BUG_ON(leaf
->fs_info
&& leaf
->fs_info
->nodesize
!= leaf
->len
);
1666 leaf_data_size
= __BTRFS_LEAF_DATA_SIZE(leaf
->len
);
1667 ret
= leaf_data_size
- leaf_space_used(leaf
, 0 ,nritems
);
1669 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1670 ret
, leaf_data_size
, leaf_space_used(leaf
, 0, nritems
),
1677 * push some data in the path leaf to the right, trying to free up at
1678 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1680 * returns 1 if the push failed because the other node didn't have enough
1681 * room, 0 if everything worked out and < 0 if there were major errors.
1683 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1684 *root
, struct btrfs_path
*path
, int data_size
,
1687 struct extent_buffer
*left
= path
->nodes
[0];
1688 struct extent_buffer
*right
;
1689 struct extent_buffer
*upper
;
1690 struct btrfs_disk_key disk_key
;
1691 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1697 struct btrfs_item
*item
;
1705 slot
= path
->slots
[1];
1706 if (!path
->nodes
[1]) {
1709 upper
= path
->nodes
[1];
1710 if (slot
>= btrfs_header_nritems(upper
) - 1)
1713 right
= read_node_slot(fs_info
, upper
, slot
+ 1);
1714 if (!extent_buffer_uptodate(right
)) {
1716 return PTR_ERR(right
);
1719 free_space
= btrfs_leaf_free_space(right
);
1720 if (free_space
< data_size
) {
1721 free_extent_buffer(right
);
1725 /* cow and double check */
1726 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1729 free_extent_buffer(right
);
1732 free_space
= btrfs_leaf_free_space(right
);
1733 if (free_space
< data_size
) {
1734 free_extent_buffer(right
);
1738 left_nritems
= btrfs_header_nritems(left
);
1739 if (left_nritems
== 0) {
1740 free_extent_buffer(right
);
1749 i
= left_nritems
- 1;
1751 item
= btrfs_item_nr(i
);
1753 if (path
->slots
[0] == i
)
1754 push_space
+= data_size
+ sizeof(*item
);
1756 this_item_size
= btrfs_item_size(left
, item
);
1757 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1760 push_space
+= this_item_size
+ sizeof(*item
);
1766 if (push_items
== 0) {
1767 free_extent_buffer(right
);
1771 if (!empty
&& push_items
== left_nritems
)
1774 /* push left to right */
1775 right_nritems
= btrfs_header_nritems(right
);
1777 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1778 push_space
-= leaf_data_end(fs_info
, left
);
1780 /* make room in the right data area */
1781 data_end
= leaf_data_end(fs_info
, right
);
1782 memmove_extent_buffer(right
,
1783 btrfs_leaf_data(right
) + data_end
- push_space
,
1784 btrfs_leaf_data(right
) + data_end
,
1785 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - data_end
);
1787 /* copy from the left data area */
1788 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1789 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - push_space
,
1790 btrfs_leaf_data(left
) + leaf_data_end(fs_info
, left
),
1793 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1794 btrfs_item_nr_offset(0),
1795 right_nritems
* sizeof(struct btrfs_item
));
1797 /* copy the items from left to right */
1798 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1799 btrfs_item_nr_offset(left_nritems
- push_items
),
1800 push_items
* sizeof(struct btrfs_item
));
1802 /* update the item pointers */
1803 right_nritems
+= push_items
;
1804 btrfs_set_header_nritems(right
, right_nritems
);
1805 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1806 for (i
= 0; i
< right_nritems
; i
++) {
1807 item
= btrfs_item_nr(i
);
1808 push_space
-= btrfs_item_size(right
, item
);
1809 btrfs_set_item_offset(right
, item
, push_space
);
1812 left_nritems
-= push_items
;
1813 btrfs_set_header_nritems(left
, left_nritems
);
1816 btrfs_mark_buffer_dirty(left
);
1817 btrfs_mark_buffer_dirty(right
);
1819 btrfs_item_key(right
, &disk_key
, 0);
1820 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1821 btrfs_mark_buffer_dirty(upper
);
1823 /* then fixup the leaf pointer in the path */
1824 if (path
->slots
[0] >= left_nritems
) {
1825 path
->slots
[0] -= left_nritems
;
1826 free_extent_buffer(path
->nodes
[0]);
1827 path
->nodes
[0] = right
;
1828 path
->slots
[1] += 1;
1830 free_extent_buffer(right
);
1835 * push some data in the path leaf to the left, trying to free up at
1836 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1838 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1839 *root
, struct btrfs_path
*path
, int data_size
,
1842 struct btrfs_disk_key disk_key
;
1843 struct extent_buffer
*right
= path
->nodes
[0];
1844 struct extent_buffer
*left
;
1845 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1851 struct btrfs_item
*item
;
1852 u32 old_left_nritems
;
1857 u32 old_left_item_size
;
1859 slot
= path
->slots
[1];
1862 if (!path
->nodes
[1])
1865 right_nritems
= btrfs_header_nritems(right
);
1866 if (right_nritems
== 0) {
1870 left
= read_node_slot(fs_info
, path
->nodes
[1], slot
- 1);
1871 free_space
= btrfs_leaf_free_space(left
);
1872 if (free_space
< data_size
) {
1873 free_extent_buffer(left
);
1877 /* cow and double check */
1878 ret
= btrfs_cow_block(trans
, root
, left
,
1879 path
->nodes
[1], slot
- 1, &left
);
1881 /* we hit -ENOSPC, but it isn't fatal here */
1882 free_extent_buffer(left
);
1886 free_space
= btrfs_leaf_free_space(left
);
1887 if (free_space
< data_size
) {
1888 free_extent_buffer(left
);
1895 nr
= right_nritems
- 1;
1897 for (i
= 0; i
< nr
; i
++) {
1898 item
= btrfs_item_nr(i
);
1900 if (path
->slots
[0] == i
)
1901 push_space
+= data_size
+ sizeof(*item
);
1903 this_item_size
= btrfs_item_size(right
, item
);
1904 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1908 push_space
+= this_item_size
+ sizeof(*item
);
1911 if (push_items
== 0) {
1912 free_extent_buffer(left
);
1915 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1918 /* push data from right to left */
1919 copy_extent_buffer(left
, right
,
1920 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1921 btrfs_item_nr_offset(0),
1922 push_items
* sizeof(struct btrfs_item
));
1924 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1925 btrfs_item_offset_nr(right
, push_items
-1);
1927 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1928 leaf_data_end(fs_info
, left
) - push_space
,
1929 btrfs_leaf_data(right
) +
1930 btrfs_item_offset_nr(right
, push_items
- 1),
1932 old_left_nritems
= btrfs_header_nritems(left
);
1933 BUG_ON(old_left_nritems
== 0);
1935 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1936 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1939 item
= btrfs_item_nr(i
);
1940 ioff
= btrfs_item_offset(left
, item
);
1941 btrfs_set_item_offset(left
, item
,
1942 ioff
- (BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1943 old_left_item_size
));
1945 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1947 /* fixup right node */
1948 if (push_items
> right_nritems
) {
1949 printk("push items %d nr %u\n", push_items
, right_nritems
);
1953 if (push_items
< right_nritems
) {
1954 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1955 leaf_data_end(fs_info
, right
);
1956 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1957 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1959 btrfs_leaf_data(right
) +
1960 leaf_data_end(fs_info
, right
),
1963 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1964 btrfs_item_nr_offset(push_items
),
1965 (btrfs_header_nritems(right
) - push_items
) *
1966 sizeof(struct btrfs_item
));
1968 right_nritems
-= push_items
;
1969 btrfs_set_header_nritems(right
, right_nritems
);
1970 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1971 for (i
= 0; i
< right_nritems
; i
++) {
1972 item
= btrfs_item_nr(i
);
1973 push_space
= push_space
- btrfs_item_size(right
, item
);
1974 btrfs_set_item_offset(right
, item
, push_space
);
1977 btrfs_mark_buffer_dirty(left
);
1979 btrfs_mark_buffer_dirty(right
);
1981 btrfs_item_key(right
, &disk_key
, 0);
1982 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1984 /* then fixup the leaf pointer in the path */
1985 if (path
->slots
[0] < push_items
) {
1986 path
->slots
[0] += old_left_nritems
;
1987 free_extent_buffer(path
->nodes
[0]);
1988 path
->nodes
[0] = left
;
1989 path
->slots
[1] -= 1;
1991 free_extent_buffer(left
);
1992 path
->slots
[0] -= push_items
;
1994 BUG_ON(path
->slots
[0] < 0);
1999 * split the path's leaf in two, making sure there is at least data_size
2000 * available for the resulting leaf level of the path.
2002 * returns 0 if all went well and < 0 on failure.
2004 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
2005 struct btrfs_root
*root
,
2006 struct btrfs_path
*path
,
2007 struct extent_buffer
*l
,
2008 struct extent_buffer
*right
,
2009 int slot
, int mid
, int nritems
)
2016 struct btrfs_disk_key disk_key
;
2018 nritems
= nritems
- mid
;
2019 btrfs_set_header_nritems(right
, nritems
);
2020 data_copy_size
= btrfs_item_end_nr(l
, mid
) -
2021 leaf_data_end(root
->fs_info
, l
);
2023 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2024 btrfs_item_nr_offset(mid
),
2025 nritems
* sizeof(struct btrfs_item
));
2027 copy_extent_buffer(right
, l
,
2028 btrfs_leaf_data(right
) +
2029 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2030 data_copy_size
, btrfs_leaf_data(l
) +
2031 leaf_data_end(root
->fs_info
, l
), data_copy_size
);
2033 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2034 btrfs_item_end_nr(l
, mid
);
2036 for (i
= 0; i
< nritems
; i
++) {
2037 struct btrfs_item
*item
= btrfs_item_nr(i
);
2038 u32 ioff
= btrfs_item_offset(right
, item
);
2039 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2042 btrfs_set_header_nritems(l
, mid
);
2044 btrfs_item_key(right
, &disk_key
, 0);
2045 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2046 path
->slots
[1] + 1, 1);
2050 btrfs_mark_buffer_dirty(right
);
2051 btrfs_mark_buffer_dirty(l
);
2052 BUG_ON(path
->slots
[0] != slot
);
2055 free_extent_buffer(path
->nodes
[0]);
2056 path
->nodes
[0] = right
;
2057 path
->slots
[0] -= mid
;
2058 path
->slots
[1] += 1;
2060 free_extent_buffer(right
);
2063 BUG_ON(path
->slots
[0] < 0);
2069 * split the path's leaf in two, making sure there is at least data_size
2070 * available for the resulting leaf level of the path.
2072 * returns 0 if all went well and < 0 on failure.
2074 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2075 struct btrfs_root
*root
,
2076 struct btrfs_key
*ins_key
,
2077 struct btrfs_path
*path
, int data_size
,
2080 struct btrfs_disk_key disk_key
;
2081 struct extent_buffer
*l
;
2085 struct extent_buffer
*right
;
2089 int num_doubles
= 0;
2092 slot
= path
->slots
[0];
2093 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2094 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
->fs_info
))
2097 /* first try to make some room by pushing left and right */
2098 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2099 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2103 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2109 /* did the pushes work? */
2110 if (btrfs_leaf_free_space(l
) >= data_size
)
2114 if (!path
->nodes
[1]) {
2115 ret
= insert_new_root(trans
, root
, path
, 1);
2122 slot
= path
->slots
[0];
2123 nritems
= btrfs_header_nritems(l
);
2124 mid
= (nritems
+ 1) / 2;
2128 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2129 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2130 if (slot
>= nritems
) {
2134 if (mid
!= nritems
&&
2135 leaf_space_used(l
, mid
, nritems
- mid
) +
2137 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2143 if (leaf_space_used(l
, 0, mid
) + data_size
>
2144 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2145 if (!extend
&& data_size
&& slot
== 0) {
2147 } else if ((extend
|| !data_size
) && slot
== 0) {
2151 if (mid
!= nritems
&&
2152 leaf_space_used(l
, mid
, nritems
- mid
) +
2154 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2162 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2164 btrfs_item_key(l
, &disk_key
, mid
);
2166 right
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
2167 root
->root_key
.objectid
,
2168 &disk_key
, 0, l
->start
, 0);
2169 if (IS_ERR(right
)) {
2171 return PTR_ERR(right
);
2174 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2175 btrfs_set_header_bytenr(right
, right
->start
);
2176 btrfs_set_header_generation(right
, trans
->transid
);
2177 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2178 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2179 btrfs_set_header_level(right
, 0);
2180 write_extent_buffer(right
, root
->fs_info
->fsid
,
2181 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
2183 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2184 btrfs_header_chunk_tree_uuid(right
),
2187 root_add_used(root
, root
->fs_info
->nodesize
);
2191 btrfs_set_header_nritems(right
, 0);
2192 wret
= insert_ptr(trans
, root
, path
,
2193 &disk_key
, right
->start
,
2194 path
->slots
[1] + 1, 1);
2198 free_extent_buffer(path
->nodes
[0]);
2199 path
->nodes
[0] = right
;
2201 path
->slots
[1] += 1;
2203 btrfs_set_header_nritems(right
, 0);
2204 wret
= insert_ptr(trans
, root
, path
,
2210 free_extent_buffer(path
->nodes
[0]);
2211 path
->nodes
[0] = right
;
2213 if (path
->slots
[1] == 0) {
2214 btrfs_fixup_low_keys(root
, path
,
2218 btrfs_mark_buffer_dirty(right
);
2222 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2226 BUG_ON(num_doubles
!= 0);
2235 * This function splits a single item into two items,
2236 * giving 'new_key' to the new item and splitting the
2237 * old one at split_offset (from the start of the item).
2239 * The path may be released by this operation. After
2240 * the split, the path is pointing to the old item. The
2241 * new item is going to be in the same node as the old one.
2243 * Note, the item being split must be smaller enough to live alone on
2244 * a tree block with room for one extra struct btrfs_item
2246 * This allows us to split the item in place, keeping a lock on the
2247 * leaf the entire time.
2249 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2250 struct btrfs_root
*root
,
2251 struct btrfs_path
*path
,
2252 struct btrfs_key
*new_key
,
2253 unsigned long split_offset
)
2256 struct extent_buffer
*leaf
;
2257 struct btrfs_key orig_key
;
2258 struct btrfs_item
*item
;
2259 struct btrfs_item
*new_item
;
2264 struct btrfs_disk_key disk_key
;
2267 leaf
= path
->nodes
[0];
2268 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
2269 if (btrfs_leaf_free_space(leaf
) >=
2270 sizeof(struct btrfs_item
))
2273 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2274 btrfs_release_path(path
);
2276 path
->search_for_split
= 1;
2278 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
2279 path
->search_for_split
= 0;
2281 /* if our item isn't there or got smaller, return now */
2282 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
2287 ret
= split_leaf(trans
, root
, &orig_key
, path
, 0, 0);
2290 BUG_ON(btrfs_leaf_free_space(leaf
) < sizeof(struct btrfs_item
));
2291 leaf
= path
->nodes
[0];
2294 item
= btrfs_item_nr(path
->slots
[0]);
2295 orig_offset
= btrfs_item_offset(leaf
, item
);
2296 item_size
= btrfs_item_size(leaf
, item
);
2299 buf
= kmalloc(item_size
, GFP_NOFS
);
2301 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
2302 path
->slots
[0]), item_size
);
2303 slot
= path
->slots
[0] + 1;
2304 leaf
= path
->nodes
[0];
2306 nritems
= btrfs_header_nritems(leaf
);
2308 if (slot
< nritems
) {
2309 /* shift the items */
2310 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2311 btrfs_item_nr_offset(slot
),
2312 (nritems
- slot
) * sizeof(struct btrfs_item
));
2316 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
2317 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2319 new_item
= btrfs_item_nr(slot
);
2321 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
2322 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
2324 btrfs_set_item_offset(leaf
, item
,
2325 orig_offset
+ item_size
- split_offset
);
2326 btrfs_set_item_size(leaf
, item
, split_offset
);
2328 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2330 /* write the data for the start of the original item */
2331 write_extent_buffer(leaf
, buf
,
2332 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2335 /* write the data for the new item */
2336 write_extent_buffer(leaf
, buf
+ split_offset
,
2337 btrfs_item_ptr_offset(leaf
, slot
),
2338 item_size
- split_offset
);
2339 btrfs_mark_buffer_dirty(leaf
);
2342 if (btrfs_leaf_free_space(leaf
) < 0) {
2343 btrfs_print_leaf(leaf
);
2350 int btrfs_truncate_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2351 u32 new_size
, int from_end
)
2355 struct extent_buffer
*leaf
;
2356 struct btrfs_item
*item
;
2358 unsigned int data_end
;
2359 unsigned int old_data_start
;
2360 unsigned int old_size
;
2361 unsigned int size_diff
;
2364 leaf
= path
->nodes
[0];
2365 slot
= path
->slots
[0];
2367 old_size
= btrfs_item_size_nr(leaf
, slot
);
2368 if (old_size
== new_size
)
2371 nritems
= btrfs_header_nritems(leaf
);
2372 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2374 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2376 size_diff
= old_size
- new_size
;
2379 BUG_ON(slot
>= nritems
);
2382 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2384 /* first correct the data pointers */
2385 for (i
= slot
; i
< nritems
; i
++) {
2387 item
= btrfs_item_nr(i
);
2388 ioff
= btrfs_item_offset(leaf
, item
);
2389 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2392 /* shift the data */
2394 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2395 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2396 data_end
, old_data_start
+ new_size
- data_end
);
2398 struct btrfs_disk_key disk_key
;
2401 btrfs_item_key(leaf
, &disk_key
, slot
);
2403 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2405 struct btrfs_file_extent_item
*fi
;
2407 fi
= btrfs_item_ptr(leaf
, slot
,
2408 struct btrfs_file_extent_item
);
2409 fi
= (struct btrfs_file_extent_item
*)(
2410 (unsigned long)fi
- size_diff
);
2412 if (btrfs_file_extent_type(leaf
, fi
) ==
2413 BTRFS_FILE_EXTENT_INLINE
) {
2414 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2415 memmove_extent_buffer(leaf
, ptr
,
2417 offsetof(struct btrfs_file_extent_item
,
2422 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2423 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2424 data_end
, old_data_start
- data_end
);
2426 offset
= btrfs_disk_key_offset(&disk_key
);
2427 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2428 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2430 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2433 item
= btrfs_item_nr(slot
);
2434 btrfs_set_item_size(leaf
, item
, new_size
);
2435 btrfs_mark_buffer_dirty(leaf
);
2438 if (btrfs_leaf_free_space(leaf
) < 0) {
2439 btrfs_print_leaf(leaf
);
2445 int btrfs_extend_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2450 struct extent_buffer
*leaf
;
2451 struct btrfs_item
*item
;
2453 unsigned int data_end
;
2454 unsigned int old_data
;
2455 unsigned int old_size
;
2458 leaf
= path
->nodes
[0];
2460 nritems
= btrfs_header_nritems(leaf
);
2461 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2463 if (btrfs_leaf_free_space(leaf
) < data_size
) {
2464 btrfs_print_leaf(leaf
);
2467 slot
= path
->slots
[0];
2468 old_data
= btrfs_item_end_nr(leaf
, slot
);
2471 if (slot
>= nritems
) {
2472 btrfs_print_leaf(leaf
);
2473 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2478 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2480 /* first correct the data pointers */
2481 for (i
= slot
; i
< nritems
; i
++) {
2483 item
= btrfs_item_nr(i
);
2484 ioff
= btrfs_item_offset(leaf
, item
);
2485 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2488 /* shift the data */
2489 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2490 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2491 data_end
, old_data
- data_end
);
2493 data_end
= old_data
;
2494 old_size
= btrfs_item_size_nr(leaf
, slot
);
2495 item
= btrfs_item_nr(slot
);
2496 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2497 btrfs_mark_buffer_dirty(leaf
);
2500 if (btrfs_leaf_free_space(leaf
) < 0) {
2501 btrfs_print_leaf(leaf
);
2508 * Given a key and some data, insert an item into the tree.
2509 * This does all the path init required, making room in the tree if needed.
2511 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2512 struct btrfs_root
*root
,
2513 struct btrfs_path
*path
,
2514 struct btrfs_key
*cpu_key
, u32
*data_size
,
2517 struct extent_buffer
*leaf
;
2518 struct btrfs_item
*item
;
2525 unsigned int data_end
;
2526 struct btrfs_disk_key disk_key
;
2528 for (i
= 0; i
< nr
; i
++) {
2529 total_data
+= data_size
[i
];
2532 /* create a root if there isn't one */
2536 total_size
= total_data
+ nr
* sizeof(struct btrfs_item
);
2537 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2544 leaf
= path
->nodes
[0];
2546 nritems
= btrfs_header_nritems(leaf
);
2547 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2549 if (btrfs_leaf_free_space(leaf
) < total_size
) {
2550 btrfs_print_leaf(leaf
);
2551 printk("not enough freespace need %u have %d\n",
2552 total_size
, btrfs_leaf_free_space(leaf
));
2556 slot
= path
->slots
[0];
2559 if (slot
< nritems
) {
2560 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2562 if (old_data
< data_end
) {
2563 btrfs_print_leaf(leaf
);
2564 printk("slot %d old_data %d data_end %d\n",
2565 slot
, old_data
, data_end
);
2569 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2571 /* first correct the data pointers */
2572 for (i
= slot
; i
< nritems
; i
++) {
2575 item
= btrfs_item_nr(i
);
2576 ioff
= btrfs_item_offset(leaf
, item
);
2577 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2580 /* shift the items */
2581 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2582 btrfs_item_nr_offset(slot
),
2583 (nritems
- slot
) * sizeof(struct btrfs_item
));
2585 /* shift the data */
2586 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2587 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2588 data_end
, old_data
- data_end
);
2589 data_end
= old_data
;
2592 /* setup the item for the new data */
2593 for (i
= 0; i
< nr
; i
++) {
2594 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2595 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2596 item
= btrfs_item_nr(slot
+ i
);
2597 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2598 data_end
-= data_size
[i
];
2599 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2601 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2602 btrfs_mark_buffer_dirty(leaf
);
2606 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2607 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2610 if (btrfs_leaf_free_space(leaf
) < 0) {
2611 btrfs_print_leaf(leaf
);
2620 * Given a key and some data, insert an item into the tree.
2621 * This does all the path init required, making room in the tree if needed.
2623 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2624 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2628 struct btrfs_path
*path
;
2629 struct extent_buffer
*leaf
;
2632 path
= btrfs_alloc_path();
2636 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2638 leaf
= path
->nodes
[0];
2639 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2640 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2641 btrfs_mark_buffer_dirty(leaf
);
2643 btrfs_free_path(path
);
2648 * delete the pointer from a given node.
2650 * If the delete empties a node, the node is removed from the tree,
2651 * continuing all the way the root if required. The root is converted into
2652 * a leaf if all the nodes are emptied.
2654 int btrfs_del_ptr(struct btrfs_root
*root
, struct btrfs_path
*path
,
2655 int level
, int slot
)
2657 struct extent_buffer
*parent
= path
->nodes
[level
];
2661 nritems
= btrfs_header_nritems(parent
);
2662 if (slot
< nritems
- 1) {
2663 /* shift the items */
2664 memmove_extent_buffer(parent
,
2665 btrfs_node_key_ptr_offset(slot
),
2666 btrfs_node_key_ptr_offset(slot
+ 1),
2667 sizeof(struct btrfs_key_ptr
) *
2668 (nritems
- slot
- 1));
2671 btrfs_set_header_nritems(parent
, nritems
);
2672 if (nritems
== 0 && parent
== root
->node
) {
2673 BUG_ON(btrfs_header_level(root
->node
) != 1);
2674 /* just turn the root into a leaf and break */
2675 btrfs_set_header_level(root
->node
, 0);
2676 } else if (slot
== 0) {
2677 struct btrfs_disk_key disk_key
;
2679 btrfs_node_key(parent
, &disk_key
, 0);
2680 btrfs_fixup_low_keys(root
, path
, &disk_key
, level
+ 1);
2682 btrfs_mark_buffer_dirty(parent
);
2687 * a helper function to delete the leaf pointed to by path->slots[1] and
2690 * This deletes the pointer in path->nodes[1] and frees the leaf
2691 * block extent. zero is returned if it all worked out, < 0 otherwise.
2693 * The path must have already been setup for deleting the leaf, including
2694 * all the proper balancing. path->nodes[1] must be locked.
2696 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
2697 struct btrfs_root
*root
,
2698 struct btrfs_path
*path
,
2699 struct extent_buffer
*leaf
)
2703 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
2704 ret
= btrfs_del_ptr(root
, path
, 1, path
->slots
[1]);
2708 root_sub_used(root
, leaf
->len
);
2710 ret
= btrfs_free_extent(trans
, root
, leaf
->start
, leaf
->len
,
2711 0, root
->root_key
.objectid
, 0, 0);
2716 * delete the item at the leaf level in path. If that empties
2717 * the leaf, remove it from the tree
2719 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2720 struct btrfs_path
*path
, int slot
, int nr
)
2722 struct extent_buffer
*leaf
;
2723 struct btrfs_item
*item
;
2731 leaf
= path
->nodes
[0];
2732 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2734 for (i
= 0; i
< nr
; i
++)
2735 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2737 nritems
= btrfs_header_nritems(leaf
);
2739 if (slot
+ nr
!= nritems
) {
2740 int data_end
= leaf_data_end(root
->fs_info
, leaf
);
2742 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2744 btrfs_leaf_data(leaf
) + data_end
,
2745 last_off
- data_end
);
2747 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2750 item
= btrfs_item_nr(i
);
2751 ioff
= btrfs_item_offset(leaf
, item
);
2752 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2755 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2756 btrfs_item_nr_offset(slot
+ nr
),
2757 sizeof(struct btrfs_item
) *
2758 (nritems
- slot
- nr
));
2760 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2763 /* delete the leaf if we've emptied it */
2765 if (leaf
== root
->node
) {
2766 btrfs_set_header_level(leaf
, 0);
2768 clean_tree_block(leaf
);
2769 wret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2775 int used
= leaf_space_used(leaf
, 0, nritems
);
2777 struct btrfs_disk_key disk_key
;
2779 btrfs_item_key(leaf
, &disk_key
, 0);
2780 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2783 /* delete the leaf if it is mostly empty */
2784 if (used
< BTRFS_LEAF_DATA_SIZE(root
->fs_info
) / 4) {
2785 /* push_leaf_left fixes the path.
2786 * make sure the path still points to our leaf
2787 * for possible call to del_ptr below
2789 slot
= path
->slots
[1];
2790 extent_buffer_get(leaf
);
2792 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2793 if (wret
< 0 && wret
!= -ENOSPC
)
2796 if (path
->nodes
[0] == leaf
&&
2797 btrfs_header_nritems(leaf
)) {
2798 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2799 if (wret
< 0 && wret
!= -ENOSPC
)
2803 if (btrfs_header_nritems(leaf
) == 0) {
2804 clean_tree_block(leaf
);
2805 path
->slots
[1] = slot
;
2806 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2808 free_extent_buffer(leaf
);
2811 btrfs_mark_buffer_dirty(leaf
);
2812 free_extent_buffer(leaf
);
2815 btrfs_mark_buffer_dirty(leaf
);
2822 * walk up the tree as far as required to find the previous leaf.
2823 * returns 0 if it found something or 1 if there are no lesser leaves.
2824 * returns < 0 on io errors.
2826 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2830 struct extent_buffer
*c
;
2831 struct extent_buffer
*next
= NULL
;
2832 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2834 while(level
< BTRFS_MAX_LEVEL
) {
2835 if (!path
->nodes
[level
])
2838 slot
= path
->slots
[level
];
2839 c
= path
->nodes
[level
];
2842 if (level
== BTRFS_MAX_LEVEL
)
2848 next
= read_node_slot(fs_info
, c
, slot
);
2849 if (!extent_buffer_uptodate(next
)) {
2851 return PTR_ERR(next
);
2856 path
->slots
[level
] = slot
;
2859 c
= path
->nodes
[level
];
2860 free_extent_buffer(c
);
2861 slot
= btrfs_header_nritems(next
);
2864 path
->nodes
[level
] = next
;
2865 path
->slots
[level
] = slot
;
2868 next
= read_node_slot(fs_info
, next
, slot
);
2869 if (!extent_buffer_uptodate(next
)) {
2871 return PTR_ERR(next
);
2879 * walk up the tree as far as required to find the next leaf.
2880 * returns 0 if it found something or 1 if there are no greater leaves.
2881 * returns < 0 on io errors.
2883 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2887 struct extent_buffer
*c
;
2888 struct extent_buffer
*next
= NULL
;
2889 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2891 while(level
< BTRFS_MAX_LEVEL
) {
2892 if (!path
->nodes
[level
])
2895 slot
= path
->slots
[level
] + 1;
2896 c
= path
->nodes
[level
];
2897 if (slot
>= btrfs_header_nritems(c
)) {
2899 if (level
== BTRFS_MAX_LEVEL
)
2905 reada_for_search(root
, path
, level
, slot
, 0);
2907 next
= read_node_slot(fs_info
, c
, slot
);
2908 if (!extent_buffer_uptodate(next
))
2912 path
->slots
[level
] = slot
;
2915 c
= path
->nodes
[level
];
2916 free_extent_buffer(c
);
2917 path
->nodes
[level
] = next
;
2918 path
->slots
[level
] = 0;
2922 reada_for_search(root
, path
, level
, 0, 0);
2923 next
= read_node_slot(fs_info
, next
, 0);
2924 if (!extent_buffer_uptodate(next
))
2930 int btrfs_previous_item(struct btrfs_root
*root
,
2931 struct btrfs_path
*path
, u64 min_objectid
,
2934 struct btrfs_key found_key
;
2935 struct extent_buffer
*leaf
;
2940 if (path
->slots
[0] == 0) {
2941 ret
= btrfs_prev_leaf(root
, path
);
2947 leaf
= path
->nodes
[0];
2948 nritems
= btrfs_header_nritems(leaf
);
2951 if (path
->slots
[0] == nritems
)
2954 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2955 if (found_key
.objectid
< min_objectid
)
2957 if (found_key
.type
== type
)
2959 if (found_key
.objectid
== min_objectid
&&
2960 found_key
.type
< type
)
2967 * search in extent tree to find a previous Metadata/Data extent item with
2970 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2972 int btrfs_previous_extent_item(struct btrfs_root
*root
,
2973 struct btrfs_path
*path
, u64 min_objectid
)
2975 struct btrfs_key found_key
;
2976 struct extent_buffer
*leaf
;
2981 if (path
->slots
[0] == 0) {
2982 ret
= btrfs_prev_leaf(root
, path
);
2988 leaf
= path
->nodes
[0];
2989 nritems
= btrfs_header_nritems(leaf
);
2992 if (path
->slots
[0] == nritems
)
2995 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2996 if (found_key
.objectid
< min_objectid
)
2998 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
2999 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)
3001 if (found_key
.objectid
== min_objectid
&&
3002 found_key
.type
< BTRFS_EXTENT_ITEM_KEY
)
3009 * Search in extent tree to found next meta/data extent
3010 * Caller needs to check for no-hole or skinny metadata features.
3012 int btrfs_next_extent_item(struct btrfs_root
*root
,
3013 struct btrfs_path
*path
, u64 max_objectid
)
3015 struct btrfs_key found_key
;
3019 ret
= btrfs_next_item(root
, path
);
3022 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
3024 if (found_key
.objectid
> max_objectid
)
3026 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
3027 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)