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
= kzalloc(sizeof(struct btrfs_path
), GFP_NOFS
);
49 void btrfs_free_path(struct btrfs_path
*p
)
53 btrfs_release_path(p
);
57 void btrfs_release_path(struct btrfs_path
*p
)
60 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
63 free_extent_buffer(p
->nodes
[i
]);
65 memset(p
, 0, sizeof(*p
));
68 void add_root_to_dirty_list(struct btrfs_root
*root
)
70 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
71 list_add(&root
->dirty_list
,
72 &root
->fs_info
->dirty_cowonly_roots
);
76 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
77 struct btrfs_root
*root
,
78 struct extent_buffer
*buf
,
79 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
81 struct extent_buffer
*cow
;
84 struct btrfs_root
*new_root
;
85 struct btrfs_disk_key disk_key
;
87 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
91 memcpy(new_root
, root
, sizeof(*new_root
));
92 new_root
->root_key
.objectid
= new_root_objectid
;
94 WARN_ON(root
->ref_cows
&& trans
->transid
!=
95 root
->fs_info
->running_transaction
->transid
);
96 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
98 level
= btrfs_header_level(buf
);
100 btrfs_item_key(buf
, &disk_key
, 0);
102 btrfs_node_key(buf
, &disk_key
, 0);
103 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
104 new_root_objectid
, &disk_key
,
105 level
, buf
->start
, 0);
111 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
112 btrfs_set_header_bytenr(cow
, cow
->start
);
113 btrfs_set_header_generation(cow
, trans
->transid
);
114 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
115 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
116 BTRFS_HEADER_FLAG_RELOC
);
117 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
118 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
120 btrfs_set_header_owner(cow
, new_root_objectid
);
122 write_extent_buffer(cow
, root
->fs_info
->fsid
,
123 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
125 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
126 ret
= btrfs_inc_ref(trans
, new_root
, cow
, 0);
132 btrfs_mark_buffer_dirty(cow
);
138 * check if the tree block can be shared by multiple trees
140 static int btrfs_block_can_be_shared(struct btrfs_root
*root
,
141 struct extent_buffer
*buf
)
144 * Tree blocks not in refernece counted trees and tree roots
145 * are never shared. If a block was allocated after the last
146 * snapshot and the block was not allocated by tree relocation,
147 * we know the block is not shared.
149 if (root
->ref_cows
&&
150 buf
!= root
->node
&& buf
!= root
->commit_root
&&
151 (btrfs_header_generation(buf
) <=
152 btrfs_root_last_snapshot(&root
->root_item
) ||
153 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
155 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
156 if (root
->ref_cows
&&
157 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
163 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
164 struct btrfs_root
*root
,
165 struct extent_buffer
*buf
,
166 struct extent_buffer
*cow
)
175 * Backrefs update rules:
177 * Always use full backrefs for extent pointers in tree block
178 * allocated by tree relocation.
180 * If a shared tree block is no longer referenced by its owner
181 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
182 * use full backrefs for extent pointers in tree block.
184 * If a tree block is been relocating
185 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
186 * use full backrefs for extent pointers in tree block.
187 * The reason for this is some operations (such as drop tree)
188 * are only allowed for blocks use full backrefs.
191 if (btrfs_block_can_be_shared(root
, buf
)) {
192 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
193 btrfs_header_level(buf
), 1,
199 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
200 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
201 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
206 owner
= btrfs_header_owner(buf
);
207 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) &&
208 owner
== BTRFS_TREE_RELOC_OBJECTID
);
211 if ((owner
== root
->root_key
.objectid
||
212 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
213 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
214 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
217 if (root
->root_key
.objectid
==
218 BTRFS_TREE_RELOC_OBJECTID
) {
219 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
221 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
224 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
227 if (root
->root_key
.objectid
==
228 BTRFS_TREE_RELOC_OBJECTID
)
229 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
231 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
234 if (new_flags
!= 0) {
235 ret
= btrfs_set_block_flags(trans
, root
, buf
->start
,
236 btrfs_header_level(buf
),
241 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
242 if (root
->root_key
.objectid
==
243 BTRFS_TREE_RELOC_OBJECTID
)
244 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
246 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
248 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
251 clean_tree_block(trans
, root
, buf
);
256 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
257 struct btrfs_root
*root
,
258 struct extent_buffer
*buf
,
259 struct extent_buffer
*parent
, int parent_slot
,
260 struct extent_buffer
**cow_ret
,
261 u64 search_start
, u64 empty_size
)
263 struct extent_buffer
*cow
;
264 struct btrfs_disk_key disk_key
;
267 WARN_ON(root
->ref_cows
&& trans
->transid
!=
268 root
->fs_info
->running_transaction
->transid
);
269 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
271 level
= btrfs_header_level(buf
);
274 btrfs_item_key(buf
, &disk_key
, 0);
276 btrfs_node_key(buf
, &disk_key
, 0);
278 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
,
279 root
->root_key
.objectid
, &disk_key
,
280 level
, search_start
, empty_size
);
284 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
285 btrfs_set_header_bytenr(cow
, cow
->start
);
286 btrfs_set_header_generation(cow
, trans
->transid
);
287 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
288 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
289 BTRFS_HEADER_FLAG_RELOC
);
290 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
291 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
293 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
295 write_extent_buffer(cow
, root
->fs_info
->fsid
,
296 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
298 WARN_ON(!(buf
->flags
& EXTENT_BAD_TRANSID
) &&
299 btrfs_header_generation(buf
) > trans
->transid
);
301 update_ref_for_cow(trans
, root
, buf
, cow
);
303 if (buf
== root
->node
) {
305 extent_buffer_get(cow
);
307 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
308 0, root
->root_key
.objectid
, level
, 0);
309 free_extent_buffer(buf
);
310 add_root_to_dirty_list(root
);
312 btrfs_set_node_blockptr(parent
, parent_slot
,
314 WARN_ON(trans
->transid
== 0);
315 btrfs_set_node_ptr_generation(parent
, parent_slot
,
317 btrfs_mark_buffer_dirty(parent
);
318 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
320 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
321 0, root
->root_key
.objectid
, level
, 1);
323 if (!list_empty(&buf
->recow
)) {
324 list_del_init(&buf
->recow
);
325 free_extent_buffer(buf
);
327 free_extent_buffer(buf
);
328 btrfs_mark_buffer_dirty(cow
);
333 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
334 struct btrfs_root
*root
,
335 struct extent_buffer
*buf
)
337 if (btrfs_header_generation(buf
) == trans
->transid
&&
338 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
339 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
340 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
345 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
346 struct btrfs_root
*root
, struct extent_buffer
*buf
,
347 struct extent_buffer
*parent
, int parent_slot
,
348 struct extent_buffer
**cow_ret
)
353 if (trans->transaction != root->fs_info->running_transaction) {
354 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
355 root->fs_info->running_transaction->transid);
359 if (trans
->transid
!= root
->fs_info
->generation
) {
360 printk(KERN_CRIT
"trans %llu running %llu\n",
361 (unsigned long long)trans
->transid
,
362 (unsigned long long)root
->fs_info
->generation
);
365 if (!should_cow_block(trans
, root
, buf
)) {
370 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
371 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
372 parent_slot
, cow_ret
, search_start
, 0);
376 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
378 if (k1
->objectid
> k2
->objectid
)
380 if (k1
->objectid
< k2
->objectid
)
382 if (k1
->type
> k2
->type
)
384 if (k1
->type
< k2
->type
)
386 if (k1
->offset
> k2
->offset
)
388 if (k1
->offset
< k2
->offset
)
394 * compare two keys in a memcmp fashion
396 static int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
400 btrfs_disk_key_to_cpu(&k1
, disk
);
401 return btrfs_comp_cpu_keys(&k1
, k2
);
405 * The leaf data grows from end-to-front in the node.
406 * this returns the address of the start of the last item,
407 * which is the stop of the leaf data stack
409 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
410 struct extent_buffer
*leaf
)
412 u32 nr
= btrfs_header_nritems(leaf
);
414 return BTRFS_LEAF_DATA_SIZE(root
);
415 return btrfs_item_offset_nr(leaf
, nr
- 1);
418 enum btrfs_tree_block_status
419 btrfs_check_node(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
420 struct extent_buffer
*buf
)
423 struct btrfs_key cpukey
;
424 struct btrfs_disk_key key
;
425 u32 nritems
= btrfs_header_nritems(buf
);
426 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
428 if (nritems
== 0 || nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
))
431 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
432 if (parent_key
&& parent_key
->type
) {
433 btrfs_node_key(buf
, &key
, 0);
434 if (memcmp(parent_key
, &key
, sizeof(key
)))
437 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
438 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
439 btrfs_node_key(buf
, &key
, i
);
440 btrfs_node_key_to_cpu(buf
, &cpukey
, i
+ 1);
441 if (btrfs_comp_keys(&key
, &cpukey
) >= 0)
444 return BTRFS_TREE_BLOCK_CLEAN
;
446 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
448 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
450 btrfs_node_key_to_cpu(buf
, &cpukey
, 0);
451 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
452 buf
->start
, buf
->len
,
453 btrfs_header_level(buf
));
458 enum btrfs_tree_block_status
459 btrfs_check_leaf(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
460 struct extent_buffer
*buf
)
463 struct btrfs_key cpukey
;
464 struct btrfs_disk_key key
;
465 u32 nritems
= btrfs_header_nritems(buf
);
466 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
468 if (nritems
* sizeof(struct btrfs_item
) > buf
->len
) {
469 fprintf(stderr
, "invalid number of items %llu\n",
470 (unsigned long long)buf
->start
);
474 if (btrfs_header_level(buf
) != 0) {
475 ret
= BTRFS_TREE_BLOCK_INVALID_LEVEL
;
476 fprintf(stderr
, "leaf is not a leaf %llu\n",
477 (unsigned long long)btrfs_header_bytenr(buf
));
480 if (btrfs_leaf_free_space(root
, buf
) < 0) {
481 ret
= BTRFS_TREE_BLOCK_INVALID_FREE_SPACE
;
482 fprintf(stderr
, "leaf free space incorrect %llu %d\n",
483 (unsigned long long)btrfs_header_bytenr(buf
),
484 btrfs_leaf_free_space(root
, buf
));
489 return BTRFS_TREE_BLOCK_CLEAN
;
491 btrfs_item_key(buf
, &key
, 0);
492 if (parent_key
&& parent_key
->type
&&
493 memcmp(parent_key
, &key
, sizeof(key
))) {
494 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
495 fprintf(stderr
, "leaf parent key incorrect %llu\n",
496 (unsigned long long)btrfs_header_bytenr(buf
));
499 for (i
= 0; nritems
> 1 && i
< nritems
- 1; i
++) {
500 btrfs_item_key(buf
, &key
, i
);
501 btrfs_item_key_to_cpu(buf
, &cpukey
, i
+ 1);
502 if (btrfs_comp_keys(&key
, &cpukey
) >= 0) {
503 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
504 fprintf(stderr
, "bad key ordering %d %d\n", i
, i
+1);
507 if (btrfs_item_offset_nr(buf
, i
) !=
508 btrfs_item_end_nr(buf
, i
+ 1)) {
509 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
510 fprintf(stderr
, "incorrect offsets %u %u\n",
511 btrfs_item_offset_nr(buf
, i
),
512 btrfs_item_end_nr(buf
, i
+ 1));
515 if (i
== 0 && btrfs_item_end_nr(buf
, i
) !=
516 BTRFS_LEAF_DATA_SIZE(root
)) {
517 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
518 fprintf(stderr
, "bad item end %u wanted %u\n",
519 btrfs_item_end_nr(buf
, i
),
520 (unsigned)BTRFS_LEAF_DATA_SIZE(root
));
525 for (i
= 0; i
< nritems
; i
++) {
526 if (btrfs_item_end_nr(buf
, i
) > BTRFS_LEAF_DATA_SIZE(root
)) {
527 btrfs_item_key(buf
, &key
, 0);
528 btrfs_print_key(&key
);
530 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
531 fprintf(stderr
, "slot end outside of leaf %llu > %llu\n",
532 (unsigned long long)btrfs_item_end_nr(buf
, i
),
533 (unsigned long long)BTRFS_LEAF_DATA_SIZE(root
));
538 return BTRFS_TREE_BLOCK_CLEAN
;
540 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
542 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
544 btrfs_item_key_to_cpu(buf
, &cpukey
, 0);
546 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
547 buf
->start
, buf
->len
, 0);
552 static int noinline
check_block(struct btrfs_root
*root
,
553 struct btrfs_path
*path
, int level
)
555 struct btrfs_disk_key key
;
556 struct btrfs_disk_key
*key_ptr
= NULL
;
557 struct extent_buffer
*parent
;
558 enum btrfs_tree_block_status ret
;
560 if (path
->skip_check_block
)
562 if (path
->nodes
[level
+ 1]) {
563 parent
= path
->nodes
[level
+ 1];
564 btrfs_node_key(parent
, &key
, path
->slots
[level
+ 1]);
568 ret
= btrfs_check_leaf(root
, key_ptr
, path
->nodes
[0]);
570 ret
= btrfs_check_node(root
, key_ptr
, path
->nodes
[level
]);
571 if (ret
== BTRFS_TREE_BLOCK_CLEAN
)
577 * search for key in the extent_buffer. The items start at offset p,
578 * and they are item_size apart. There are 'max' items in p.
580 * the slot in the array is returned via slot, and it points to
581 * the place where you would insert key if it is not found in
584 * slot may point to max if the key is bigger than all of the keys
586 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
587 int item_size
, struct btrfs_key
*key
,
594 unsigned long offset
;
595 struct btrfs_disk_key
*tmp
;
598 mid
= (low
+ high
) / 2;
599 offset
= p
+ mid
* item_size
;
601 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
602 ret
= btrfs_comp_keys(tmp
, key
);
618 * simple bin_search frontend that does the right thing for
621 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
622 int level
, int *slot
)
625 return generic_bin_search(eb
,
626 offsetof(struct btrfs_leaf
, items
),
627 sizeof(struct btrfs_item
),
628 key
, btrfs_header_nritems(eb
),
631 return generic_bin_search(eb
,
632 offsetof(struct btrfs_node
, ptrs
),
633 sizeof(struct btrfs_key_ptr
),
634 key
, btrfs_header_nritems(eb
),
638 struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
639 struct extent_buffer
*parent
, int slot
)
641 int level
= btrfs_header_level(parent
);
644 if (slot
>= btrfs_header_nritems(parent
))
650 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
651 btrfs_level_size(root
, level
- 1),
652 btrfs_node_ptr_generation(parent
, slot
));
655 static int balance_level(struct btrfs_trans_handle
*trans
,
656 struct btrfs_root
*root
,
657 struct btrfs_path
*path
, int level
)
659 struct extent_buffer
*right
= NULL
;
660 struct extent_buffer
*mid
;
661 struct extent_buffer
*left
= NULL
;
662 struct extent_buffer
*parent
= NULL
;
666 int orig_slot
= path
->slots
[level
];
672 mid
= path
->nodes
[level
];
673 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
675 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
677 if (level
< BTRFS_MAX_LEVEL
- 1) {
678 parent
= path
->nodes
[level
+ 1];
679 pslot
= path
->slots
[level
+ 1];
683 * deal with the case where there is only one pointer in the root
684 * by promoting the node below to a root
687 struct extent_buffer
*child
;
689 if (btrfs_header_nritems(mid
) != 1)
692 /* promote the child to a root */
693 child
= read_node_slot(root
, mid
, 0);
694 BUG_ON(!extent_buffer_uptodate(child
));
695 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
699 add_root_to_dirty_list(root
);
700 path
->nodes
[level
] = NULL
;
701 clean_tree_block(trans
, root
, mid
);
702 wait_on_tree_block_writeback(root
, mid
);
703 /* once for the path */
704 free_extent_buffer(mid
);
706 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
707 0, root
->root_key
.objectid
,
709 /* once for the root ptr */
710 free_extent_buffer(mid
);
713 if (btrfs_header_nritems(mid
) >
714 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
717 left
= read_node_slot(root
, parent
, pslot
- 1);
718 if (extent_buffer_uptodate(left
)) {
719 wret
= btrfs_cow_block(trans
, root
, left
,
720 parent
, pslot
- 1, &left
);
726 right
= read_node_slot(root
, parent
, pslot
+ 1);
727 if (extent_buffer_uptodate(right
)) {
728 wret
= btrfs_cow_block(trans
, root
, right
,
729 parent
, pslot
+ 1, &right
);
736 /* first, try to make some room in the middle buffer */
738 orig_slot
+= btrfs_header_nritems(left
);
739 wret
= push_node_left(trans
, root
, left
, mid
, 1);
745 * then try to empty the right most buffer into the middle
748 wret
= push_node_left(trans
, root
, mid
, right
, 1);
749 if (wret
< 0 && wret
!= -ENOSPC
)
751 if (btrfs_header_nritems(right
) == 0) {
752 u64 bytenr
= right
->start
;
753 u32 blocksize
= right
->len
;
755 clean_tree_block(trans
, root
, right
);
756 wait_on_tree_block_writeback(root
, right
);
757 free_extent_buffer(right
);
759 wret
= btrfs_del_ptr(trans
, root
, path
,
760 level
+ 1, pslot
+ 1);
763 wret
= btrfs_free_extent(trans
, root
, bytenr
,
765 root
->root_key
.objectid
,
770 struct btrfs_disk_key right_key
;
771 btrfs_node_key(right
, &right_key
, 0);
772 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
773 btrfs_mark_buffer_dirty(parent
);
776 if (btrfs_header_nritems(mid
) == 1) {
778 * we're not allowed to leave a node with one item in the
779 * tree during a delete. A deletion from lower in the tree
780 * could try to delete the only pointer in this node.
781 * So, pull some keys from the left.
782 * There has to be a left pointer at this point because
783 * otherwise we would have pulled some pointers from the
787 wret
= balance_node_right(trans
, root
, mid
, left
);
793 wret
= push_node_left(trans
, root
, left
, mid
, 1);
799 if (btrfs_header_nritems(mid
) == 0) {
800 /* we've managed to empty the middle node, drop it */
801 u64 bytenr
= mid
->start
;
802 u32 blocksize
= mid
->len
;
803 clean_tree_block(trans
, root
, mid
);
804 wait_on_tree_block_writeback(root
, mid
);
805 free_extent_buffer(mid
);
807 wret
= btrfs_del_ptr(trans
, root
, path
, level
+ 1, pslot
);
810 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
811 0, root
->root_key
.objectid
,
816 /* update the parent key to reflect our changes */
817 struct btrfs_disk_key mid_key
;
818 btrfs_node_key(mid
, &mid_key
, 0);
819 btrfs_set_node_key(parent
, &mid_key
, pslot
);
820 btrfs_mark_buffer_dirty(parent
);
823 /* update the path */
825 if (btrfs_header_nritems(left
) > orig_slot
) {
826 extent_buffer_get(left
);
827 path
->nodes
[level
] = left
;
828 path
->slots
[level
+ 1] -= 1;
829 path
->slots
[level
] = orig_slot
;
831 free_extent_buffer(mid
);
833 orig_slot
-= btrfs_header_nritems(left
);
834 path
->slots
[level
] = orig_slot
;
837 /* double check we haven't messed things up */
838 check_block(root
, path
, level
);
840 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
844 free_extent_buffer(right
);
846 free_extent_buffer(left
);
850 /* returns zero if the push worked, non-zero otherwise */
851 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
852 struct btrfs_root
*root
,
853 struct btrfs_path
*path
, int level
)
855 struct extent_buffer
*right
= NULL
;
856 struct extent_buffer
*mid
;
857 struct extent_buffer
*left
= NULL
;
858 struct extent_buffer
*parent
= NULL
;
862 int orig_slot
= path
->slots
[level
];
867 mid
= path
->nodes
[level
];
868 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
870 if (level
< BTRFS_MAX_LEVEL
- 1) {
871 parent
= path
->nodes
[level
+ 1];
872 pslot
= path
->slots
[level
+ 1];
878 left
= read_node_slot(root
, parent
, pslot
- 1);
880 /* first, try to make some room in the middle buffer */
881 if (extent_buffer_uptodate(left
)) {
883 left_nr
= btrfs_header_nritems(left
);
884 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
887 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
892 wret
= push_node_left(trans
, root
,
899 struct btrfs_disk_key disk_key
;
900 orig_slot
+= left_nr
;
901 btrfs_node_key(mid
, &disk_key
, 0);
902 btrfs_set_node_key(parent
, &disk_key
, pslot
);
903 btrfs_mark_buffer_dirty(parent
);
904 if (btrfs_header_nritems(left
) > orig_slot
) {
905 path
->nodes
[level
] = left
;
906 path
->slots
[level
+ 1] -= 1;
907 path
->slots
[level
] = orig_slot
;
908 free_extent_buffer(mid
);
911 btrfs_header_nritems(left
);
912 path
->slots
[level
] = orig_slot
;
913 free_extent_buffer(left
);
917 free_extent_buffer(left
);
919 right
= read_node_slot(root
, parent
, pslot
+ 1);
922 * then try to empty the right most buffer into the middle
924 if (extent_buffer_uptodate(right
)) {
926 right_nr
= btrfs_header_nritems(right
);
927 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
930 ret
= btrfs_cow_block(trans
, root
, right
,
936 wret
= balance_node_right(trans
, root
,
943 struct btrfs_disk_key disk_key
;
945 btrfs_node_key(right
, &disk_key
, 0);
946 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
947 btrfs_mark_buffer_dirty(parent
);
949 if (btrfs_header_nritems(mid
) <= orig_slot
) {
950 path
->nodes
[level
] = right
;
951 path
->slots
[level
+ 1] += 1;
952 path
->slots
[level
] = orig_slot
-
953 btrfs_header_nritems(mid
);
954 free_extent_buffer(mid
);
956 free_extent_buffer(right
);
960 free_extent_buffer(right
);
966 * readahead one full node of leaves
968 void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
969 int level
, int slot
, u64 objectid
)
971 struct extent_buffer
*node
;
972 struct btrfs_disk_key disk_key
;
978 int direction
= path
->reada
;
979 struct extent_buffer
*eb
;
987 if (!path
->nodes
[level
])
990 node
= path
->nodes
[level
];
991 search
= btrfs_node_blockptr(node
, slot
);
992 blocksize
= btrfs_level_size(root
, level
- 1);
993 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
995 free_extent_buffer(eb
);
999 highest_read
= search
;
1000 lowest_read
= search
;
1002 nritems
= btrfs_header_nritems(node
);
1005 if (direction
< 0) {
1009 } else if (direction
> 0) {
1014 if (path
->reada
< 0 && objectid
) {
1015 btrfs_node_key(node
, &disk_key
, nr
);
1016 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1019 search
= btrfs_node_blockptr(node
, nr
);
1020 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1021 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1022 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1023 readahead_tree_block(root
, search
, blocksize
,
1024 btrfs_node_ptr_generation(node
, nr
));
1028 if (path
->reada
< 2 && (nread
> (256 * 1024) || nscan
> 32))
1030 if(nread
> (1024 * 1024) || nscan
> 128)
1033 if (search
< lowest_read
)
1034 lowest_read
= search
;
1035 if (search
> highest_read
)
1036 highest_read
= search
;
1040 int btrfs_find_item(struct btrfs_root
*fs_root
, struct btrfs_path
*found_path
,
1041 u64 iobjectid
, u64 ioff
, u8 key_type
,
1042 struct btrfs_key
*found_key
)
1045 struct btrfs_key key
;
1046 struct extent_buffer
*eb
;
1047 struct btrfs_path
*path
;
1049 key
.type
= key_type
;
1050 key
.objectid
= iobjectid
;
1053 if (found_path
== NULL
) {
1054 path
= btrfs_alloc_path();
1060 ret
= btrfs_search_slot(NULL
, fs_root
, &key
, path
, 0, 0);
1061 if ((ret
< 0) || (found_key
== NULL
))
1064 eb
= path
->nodes
[0];
1065 if (ret
&& path
->slots
[0] >= btrfs_header_nritems(eb
)) {
1066 ret
= btrfs_next_leaf(fs_root
, path
);
1069 eb
= path
->nodes
[0];
1072 btrfs_item_key_to_cpu(eb
, found_key
, path
->slots
[0]);
1073 if (found_key
->type
!= key
.type
||
1074 found_key
->objectid
!= key
.objectid
) {
1080 if (path
!= found_path
)
1081 btrfs_free_path(path
);
1086 * look for key in the tree. path is filled in with nodes along the way
1087 * if key is found, we return zero and you can find the item in the leaf
1088 * level of the path (level 0)
1090 * If the key isn't found, the path points to the slot where it should
1091 * be inserted, and 1 is returned. If there are other errors during the
1092 * search a negative error number is returned.
1094 * if ins_len > 0, nodes and leaves will be split as we walk down the
1095 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1098 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1099 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1102 struct extent_buffer
*b
;
1106 int should_reada
= p
->reada
;
1107 u8 lowest_level
= 0;
1109 lowest_level
= p
->lowest_level
;
1110 WARN_ON(lowest_level
&& ins_len
> 0);
1111 WARN_ON(p
->nodes
[0] != NULL
);
1113 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1117 extent_buffer_get(b
);
1119 level
= btrfs_header_level(b
);
1122 wret
= btrfs_cow_block(trans
, root
, b
,
1123 p
->nodes
[level
+ 1],
1124 p
->slots
[level
+ 1],
1127 free_extent_buffer(b
);
1131 BUG_ON(!cow
&& ins_len
);
1132 if (level
!= btrfs_header_level(b
))
1134 level
= btrfs_header_level(b
);
1135 p
->nodes
[level
] = b
;
1136 ret
= check_block(root
, p
, level
);
1139 ret
= bin_search(b
, key
, level
, &slot
);
1141 if (ret
&& slot
> 0)
1143 p
->slots
[level
] = slot
;
1144 if ((p
->search_for_split
|| ins_len
> 0) &&
1145 btrfs_header_nritems(b
) >=
1146 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1147 int sret
= split_node(trans
, root
, p
, level
);
1151 b
= p
->nodes
[level
];
1152 slot
= p
->slots
[level
];
1153 } else if (ins_len
< 0) {
1154 int sret
= balance_level(trans
, root
, p
,
1158 b
= p
->nodes
[level
];
1160 btrfs_release_path(p
);
1163 slot
= p
->slots
[level
];
1164 BUG_ON(btrfs_header_nritems(b
) == 1);
1166 /* this is only true while dropping a snapshot */
1167 if (level
== lowest_level
)
1171 reada_for_search(root
, p
, level
, slot
,
1174 b
= read_node_slot(root
, b
, slot
);
1175 if (!extent_buffer_uptodate(b
))
1178 p
->slots
[level
] = slot
;
1180 ins_len
> btrfs_leaf_free_space(root
, b
)) {
1181 int sret
= split_leaf(trans
, root
, key
,
1182 p
, ins_len
, ret
== 0);
1194 * adjust the pointers going up the tree, starting at level
1195 * making sure the right key of each node is points to 'key'.
1196 * This is used after shifting pointers to the left, so it stops
1197 * fixing up pointers when a given leaf/node is not in slot 0 of the
1200 void btrfs_fixup_low_keys(struct btrfs_root
*root
, struct btrfs_path
*path
,
1201 struct btrfs_disk_key
*key
, int level
)
1204 struct extent_buffer
*t
;
1206 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1207 int tslot
= path
->slots
[i
];
1208 if (!path
->nodes
[i
])
1211 btrfs_set_node_key(t
, key
, tslot
);
1212 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1221 * This function isn't completely safe. It's the caller's responsibility
1222 * that the new key won't break the order
1224 int btrfs_set_item_key_safe(struct btrfs_root
*root
, struct btrfs_path
*path
,
1225 struct btrfs_key
*new_key
)
1227 struct btrfs_disk_key disk_key
;
1228 struct extent_buffer
*eb
;
1231 eb
= path
->nodes
[0];
1232 slot
= path
->slots
[0];
1234 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1235 if (btrfs_comp_keys(&disk_key
, new_key
) >= 0)
1238 if (slot
< btrfs_header_nritems(eb
) - 1) {
1239 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1240 if (btrfs_comp_keys(&disk_key
, new_key
) <= 0)
1244 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1245 btrfs_set_item_key(eb
, &disk_key
, slot
);
1246 btrfs_mark_buffer_dirty(eb
);
1248 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1253 * update an item key without the safety checks. This is meant to be called by
1256 void btrfs_set_item_key_unsafe(struct btrfs_root
*root
,
1257 struct btrfs_path
*path
,
1258 struct btrfs_key
*new_key
)
1260 struct btrfs_disk_key disk_key
;
1261 struct extent_buffer
*eb
;
1264 eb
= path
->nodes
[0];
1265 slot
= path
->slots
[0];
1267 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1268 btrfs_set_item_key(eb
, &disk_key
, slot
);
1269 btrfs_mark_buffer_dirty(eb
);
1271 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1275 * try to push data from one node into the next node left in the
1278 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1279 * error, and > 0 if there was no room in the left hand block.
1281 static int push_node_left(struct btrfs_trans_handle
*trans
,
1282 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1283 struct extent_buffer
*src
, int empty
)
1290 src_nritems
= btrfs_header_nritems(src
);
1291 dst_nritems
= btrfs_header_nritems(dst
);
1292 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1293 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1294 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1296 if (!empty
&& src_nritems
<= 8)
1299 if (push_items
<= 0) {
1304 push_items
= min(src_nritems
, push_items
);
1305 if (push_items
< src_nritems
) {
1306 /* leave at least 8 pointers in the node if
1307 * we aren't going to empty it
1309 if (src_nritems
- push_items
< 8) {
1310 if (push_items
<= 8)
1316 push_items
= min(src_nritems
- 8, push_items
);
1318 copy_extent_buffer(dst
, src
,
1319 btrfs_node_key_ptr_offset(dst_nritems
),
1320 btrfs_node_key_ptr_offset(0),
1321 push_items
* sizeof(struct btrfs_key_ptr
));
1323 if (push_items
< src_nritems
) {
1324 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1325 btrfs_node_key_ptr_offset(push_items
),
1326 (src_nritems
- push_items
) *
1327 sizeof(struct btrfs_key_ptr
));
1329 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1330 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1331 btrfs_mark_buffer_dirty(src
);
1332 btrfs_mark_buffer_dirty(dst
);
1338 * try to push data from one node into the next node right in the
1341 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1342 * error, and > 0 if there was no room in the right hand block.
1344 * this will only push up to 1/2 the contents of the left node over
1346 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1347 struct btrfs_root
*root
,
1348 struct extent_buffer
*dst
,
1349 struct extent_buffer
*src
)
1357 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1358 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1360 src_nritems
= btrfs_header_nritems(src
);
1361 dst_nritems
= btrfs_header_nritems(dst
);
1362 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1363 if (push_items
<= 0) {
1367 if (src_nritems
< 4) {
1371 max_push
= src_nritems
/ 2 + 1;
1372 /* don't try to empty the node */
1373 if (max_push
>= src_nritems
) {
1377 if (max_push
< push_items
)
1378 push_items
= max_push
;
1380 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1381 btrfs_node_key_ptr_offset(0),
1383 sizeof(struct btrfs_key_ptr
));
1385 copy_extent_buffer(dst
, src
,
1386 btrfs_node_key_ptr_offset(0),
1387 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1388 push_items
* sizeof(struct btrfs_key_ptr
));
1390 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1391 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1393 btrfs_mark_buffer_dirty(src
);
1394 btrfs_mark_buffer_dirty(dst
);
1400 * helper function to insert a new root level in the tree.
1401 * A new node is allocated, and a single item is inserted to
1402 * point to the existing root
1404 * returns zero on success or < 0 on failure.
1406 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1407 struct btrfs_root
*root
,
1408 struct btrfs_path
*path
, int level
)
1411 struct extent_buffer
*lower
;
1412 struct extent_buffer
*c
;
1413 struct extent_buffer
*old
;
1414 struct btrfs_disk_key lower_key
;
1416 BUG_ON(path
->nodes
[level
]);
1417 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1419 lower
= path
->nodes
[level
-1];
1421 btrfs_item_key(lower
, &lower_key
, 0);
1423 btrfs_node_key(lower
, &lower_key
, 0);
1425 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1426 root
->root_key
.objectid
, &lower_key
,
1427 level
, root
->node
->start
, 0);
1432 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1433 btrfs_set_header_nritems(c
, 1);
1434 btrfs_set_header_level(c
, level
);
1435 btrfs_set_header_bytenr(c
, c
->start
);
1436 btrfs_set_header_generation(c
, trans
->transid
);
1437 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1438 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1440 write_extent_buffer(c
, root
->fs_info
->fsid
,
1441 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1443 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1444 btrfs_header_chunk_tree_uuid(c
),
1447 btrfs_set_node_key(c
, &lower_key
, 0);
1448 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1449 lower_gen
= btrfs_header_generation(lower
);
1450 WARN_ON(lower_gen
!= trans
->transid
);
1452 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1454 btrfs_mark_buffer_dirty(c
);
1459 /* the super has an extra ref to root->node */
1460 free_extent_buffer(old
);
1462 add_root_to_dirty_list(root
);
1463 extent_buffer_get(c
);
1464 path
->nodes
[level
] = c
;
1465 path
->slots
[level
] = 0;
1470 * worker function to insert a single pointer in a node.
1471 * the node should have enough room for the pointer already
1473 * slot and level indicate where you want the key to go, and
1474 * blocknr is the block the key points to.
1476 * returns zero on success and < 0 on any error
1478 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1479 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1480 *key
, u64 bytenr
, int slot
, int level
)
1482 struct extent_buffer
*lower
;
1485 BUG_ON(!path
->nodes
[level
]);
1486 lower
= path
->nodes
[level
];
1487 nritems
= btrfs_header_nritems(lower
);
1490 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
1492 if (slot
!= nritems
) {
1493 memmove_extent_buffer(lower
,
1494 btrfs_node_key_ptr_offset(slot
+ 1),
1495 btrfs_node_key_ptr_offset(slot
),
1496 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1498 btrfs_set_node_key(lower
, key
, slot
);
1499 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1500 WARN_ON(trans
->transid
== 0);
1501 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1502 btrfs_set_header_nritems(lower
, nritems
+ 1);
1503 btrfs_mark_buffer_dirty(lower
);
1508 * split the node at the specified level in path in two.
1509 * The path is corrected to point to the appropriate node after the split
1511 * Before splitting this tries to make some room in the node by pushing
1512 * left and right, if either one works, it returns right away.
1514 * returns 0 on success and < 0 on failure
1516 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1517 *root
, struct btrfs_path
*path
, int level
)
1519 struct extent_buffer
*c
;
1520 struct extent_buffer
*split
;
1521 struct btrfs_disk_key disk_key
;
1527 c
= path
->nodes
[level
];
1528 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1529 if (c
== root
->node
) {
1530 /* trying to split the root, lets make a new one */
1531 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1535 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1536 c
= path
->nodes
[level
];
1537 if (!ret
&& btrfs_header_nritems(c
) <
1538 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
1544 c_nritems
= btrfs_header_nritems(c
);
1545 mid
= (c_nritems
+ 1) / 2;
1546 btrfs_node_key(c
, &disk_key
, mid
);
1548 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1549 root
->root_key
.objectid
,
1550 &disk_key
, level
, c
->start
, 0);
1552 return PTR_ERR(split
);
1554 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
1555 btrfs_set_header_level(split
, btrfs_header_level(c
));
1556 btrfs_set_header_bytenr(split
, split
->start
);
1557 btrfs_set_header_generation(split
, trans
->transid
);
1558 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
1559 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1560 write_extent_buffer(split
, root
->fs_info
->fsid
,
1561 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1562 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1563 btrfs_header_chunk_tree_uuid(split
),
1567 copy_extent_buffer(split
, c
,
1568 btrfs_node_key_ptr_offset(0),
1569 btrfs_node_key_ptr_offset(mid
),
1570 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1571 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1572 btrfs_set_header_nritems(c
, mid
);
1575 btrfs_mark_buffer_dirty(c
);
1576 btrfs_mark_buffer_dirty(split
);
1578 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1579 path
->slots
[level
+ 1] + 1,
1584 if (path
->slots
[level
] >= mid
) {
1585 path
->slots
[level
] -= mid
;
1586 free_extent_buffer(c
);
1587 path
->nodes
[level
] = split
;
1588 path
->slots
[level
+ 1] += 1;
1590 free_extent_buffer(split
);
1596 * how many bytes are required to store the items in a leaf. start
1597 * and nr indicate which items in the leaf to check. This totals up the
1598 * space used both by the item structs and the item data
1600 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1603 int nritems
= btrfs_header_nritems(l
);
1604 int end
= min(nritems
, start
+ nr
) - 1;
1608 data_len
= btrfs_item_end_nr(l
, start
);
1609 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1610 data_len
+= sizeof(struct btrfs_item
) * nr
;
1611 WARN_ON(data_len
< 0);
1616 * The space between the end of the leaf items and
1617 * the start of the leaf data. IOW, how much room
1618 * the leaf has left for both items and data
1620 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1622 u32 nodesize
= (root
? BTRFS_LEAF_DATA_SIZE(root
) : leaf
->len
);
1623 int nritems
= btrfs_header_nritems(leaf
);
1625 ret
= nodesize
- leaf_space_used(leaf
, 0, nritems
);
1627 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1628 ret
, nodesize
, leaf_space_used(leaf
, 0, nritems
),
1635 * push some data in the path leaf to the right, trying to free up at
1636 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1638 * returns 1 if the push failed because the other node didn't have enough
1639 * room, 0 if everything worked out and < 0 if there were major errors.
1641 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1642 *root
, struct btrfs_path
*path
, int data_size
,
1645 struct extent_buffer
*left
= path
->nodes
[0];
1646 struct extent_buffer
*right
;
1647 struct extent_buffer
*upper
;
1648 struct btrfs_disk_key disk_key
;
1654 struct btrfs_item
*item
;
1662 slot
= path
->slots
[1];
1663 if (!path
->nodes
[1]) {
1666 upper
= path
->nodes
[1];
1667 if (slot
>= btrfs_header_nritems(upper
) - 1)
1670 right
= read_node_slot(root
, upper
, slot
+ 1);
1671 if (!extent_buffer_uptodate(right
)) {
1673 return PTR_ERR(right
);
1676 free_space
= btrfs_leaf_free_space(root
, right
);
1677 if (free_space
< data_size
) {
1678 free_extent_buffer(right
);
1682 /* cow and double check */
1683 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1686 free_extent_buffer(right
);
1689 free_space
= btrfs_leaf_free_space(root
, right
);
1690 if (free_space
< data_size
) {
1691 free_extent_buffer(right
);
1695 left_nritems
= btrfs_header_nritems(left
);
1696 if (left_nritems
== 0) {
1697 free_extent_buffer(right
);
1706 i
= left_nritems
- 1;
1708 item
= btrfs_item_nr(i
);
1710 if (path
->slots
[0] == i
)
1711 push_space
+= data_size
+ sizeof(*item
);
1713 this_item_size
= btrfs_item_size(left
, item
);
1714 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1717 push_space
+= this_item_size
+ sizeof(*item
);
1723 if (push_items
== 0) {
1724 free_extent_buffer(right
);
1728 if (!empty
&& push_items
== left_nritems
)
1731 /* push left to right */
1732 right_nritems
= btrfs_header_nritems(right
);
1734 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1735 push_space
-= leaf_data_end(root
, left
);
1737 /* make room in the right data area */
1738 data_end
= leaf_data_end(root
, right
);
1739 memmove_extent_buffer(right
,
1740 btrfs_leaf_data(right
) + data_end
- push_space
,
1741 btrfs_leaf_data(right
) + data_end
,
1742 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
1744 /* copy from the left data area */
1745 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1746 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1747 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1750 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1751 btrfs_item_nr_offset(0),
1752 right_nritems
* sizeof(struct btrfs_item
));
1754 /* copy the items from left to right */
1755 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1756 btrfs_item_nr_offset(left_nritems
- push_items
),
1757 push_items
* sizeof(struct btrfs_item
));
1759 /* update the item pointers */
1760 right_nritems
+= push_items
;
1761 btrfs_set_header_nritems(right
, right_nritems
);
1762 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1763 for (i
= 0; i
< right_nritems
; i
++) {
1764 item
= btrfs_item_nr(i
);
1765 push_space
-= btrfs_item_size(right
, item
);
1766 btrfs_set_item_offset(right
, item
, push_space
);
1769 left_nritems
-= push_items
;
1770 btrfs_set_header_nritems(left
, left_nritems
);
1773 btrfs_mark_buffer_dirty(left
);
1774 btrfs_mark_buffer_dirty(right
);
1776 btrfs_item_key(right
, &disk_key
, 0);
1777 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1778 btrfs_mark_buffer_dirty(upper
);
1780 /* then fixup the leaf pointer in the path */
1781 if (path
->slots
[0] >= left_nritems
) {
1782 path
->slots
[0] -= left_nritems
;
1783 free_extent_buffer(path
->nodes
[0]);
1784 path
->nodes
[0] = right
;
1785 path
->slots
[1] += 1;
1787 free_extent_buffer(right
);
1792 * push some data in the path leaf to the left, trying to free up at
1793 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1795 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1796 *root
, struct btrfs_path
*path
, int data_size
,
1799 struct btrfs_disk_key disk_key
;
1800 struct extent_buffer
*right
= path
->nodes
[0];
1801 struct extent_buffer
*left
;
1807 struct btrfs_item
*item
;
1808 u32 old_left_nritems
;
1813 u32 old_left_item_size
;
1815 slot
= path
->slots
[1];
1818 if (!path
->nodes
[1])
1821 right_nritems
= btrfs_header_nritems(right
);
1822 if (right_nritems
== 0) {
1826 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
1827 free_space
= btrfs_leaf_free_space(root
, left
);
1828 if (free_space
< data_size
) {
1829 free_extent_buffer(left
);
1833 /* cow and double check */
1834 ret
= btrfs_cow_block(trans
, root
, left
,
1835 path
->nodes
[1], slot
- 1, &left
);
1837 /* we hit -ENOSPC, but it isn't fatal here */
1838 free_extent_buffer(left
);
1842 free_space
= btrfs_leaf_free_space(root
, left
);
1843 if (free_space
< data_size
) {
1844 free_extent_buffer(left
);
1851 nr
= right_nritems
- 1;
1853 for (i
= 0; i
< nr
; i
++) {
1854 item
= btrfs_item_nr(i
);
1856 if (path
->slots
[0] == i
)
1857 push_space
+= data_size
+ sizeof(*item
);
1859 this_item_size
= btrfs_item_size(right
, item
);
1860 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1864 push_space
+= this_item_size
+ sizeof(*item
);
1867 if (push_items
== 0) {
1868 free_extent_buffer(left
);
1871 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1874 /* push data from right to left */
1875 copy_extent_buffer(left
, right
,
1876 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1877 btrfs_item_nr_offset(0),
1878 push_items
* sizeof(struct btrfs_item
));
1880 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1881 btrfs_item_offset_nr(right
, push_items
-1);
1883 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1884 leaf_data_end(root
, left
) - push_space
,
1885 btrfs_leaf_data(right
) +
1886 btrfs_item_offset_nr(right
, push_items
- 1),
1888 old_left_nritems
= btrfs_header_nritems(left
);
1889 BUG_ON(old_left_nritems
== 0);
1891 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1892 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1895 item
= btrfs_item_nr(i
);
1896 ioff
= btrfs_item_offset(left
, item
);
1897 btrfs_set_item_offset(left
, item
,
1898 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
1900 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1902 /* fixup right node */
1903 if (push_items
> right_nritems
) {
1904 printk("push items %d nr %u\n", push_items
, right_nritems
);
1908 if (push_items
< right_nritems
) {
1909 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1910 leaf_data_end(root
, right
);
1911 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1912 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1913 btrfs_leaf_data(right
) +
1914 leaf_data_end(root
, right
), push_space
);
1916 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1917 btrfs_item_nr_offset(push_items
),
1918 (btrfs_header_nritems(right
) - push_items
) *
1919 sizeof(struct btrfs_item
));
1921 right_nritems
-= push_items
;
1922 btrfs_set_header_nritems(right
, right_nritems
);
1923 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1924 for (i
= 0; i
< right_nritems
; i
++) {
1925 item
= btrfs_item_nr(i
);
1926 push_space
= push_space
- btrfs_item_size(right
, item
);
1927 btrfs_set_item_offset(right
, item
, push_space
);
1930 btrfs_mark_buffer_dirty(left
);
1932 btrfs_mark_buffer_dirty(right
);
1934 btrfs_item_key(right
, &disk_key
, 0);
1935 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1937 /* then fixup the leaf pointer in the path */
1938 if (path
->slots
[0] < push_items
) {
1939 path
->slots
[0] += old_left_nritems
;
1940 free_extent_buffer(path
->nodes
[0]);
1941 path
->nodes
[0] = left
;
1942 path
->slots
[1] -= 1;
1944 free_extent_buffer(left
);
1945 path
->slots
[0] -= push_items
;
1947 BUG_ON(path
->slots
[0] < 0);
1952 * split the path's leaf in two, making sure there is at least data_size
1953 * available for the resulting leaf level of the path.
1955 * returns 0 if all went well and < 0 on failure.
1957 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
1958 struct btrfs_root
*root
,
1959 struct btrfs_path
*path
,
1960 struct extent_buffer
*l
,
1961 struct extent_buffer
*right
,
1962 int slot
, int mid
, int nritems
)
1969 struct btrfs_disk_key disk_key
;
1971 nritems
= nritems
- mid
;
1972 btrfs_set_header_nritems(right
, nritems
);
1973 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
1975 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
1976 btrfs_item_nr_offset(mid
),
1977 nritems
* sizeof(struct btrfs_item
));
1979 copy_extent_buffer(right
, l
,
1980 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
1981 data_copy_size
, btrfs_leaf_data(l
) +
1982 leaf_data_end(root
, l
), data_copy_size
);
1984 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
1985 btrfs_item_end_nr(l
, mid
);
1987 for (i
= 0; i
< nritems
; i
++) {
1988 struct btrfs_item
*item
= btrfs_item_nr(i
);
1989 u32 ioff
= btrfs_item_offset(right
, item
);
1990 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
1993 btrfs_set_header_nritems(l
, mid
);
1995 btrfs_item_key(right
, &disk_key
, 0);
1996 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
1997 path
->slots
[1] + 1, 1);
2001 btrfs_mark_buffer_dirty(right
);
2002 btrfs_mark_buffer_dirty(l
);
2003 BUG_ON(path
->slots
[0] != slot
);
2006 free_extent_buffer(path
->nodes
[0]);
2007 path
->nodes
[0] = right
;
2008 path
->slots
[0] -= mid
;
2009 path
->slots
[1] += 1;
2011 free_extent_buffer(right
);
2014 BUG_ON(path
->slots
[0] < 0);
2020 * split the path's leaf in two, making sure there is at least data_size
2021 * available for the resulting leaf level of the path.
2023 * returns 0 if all went well and < 0 on failure.
2025 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2026 struct btrfs_root
*root
,
2027 struct btrfs_key
*ins_key
,
2028 struct btrfs_path
*path
, int data_size
,
2031 struct btrfs_disk_key disk_key
;
2032 struct extent_buffer
*l
;
2036 struct extent_buffer
*right
;
2040 int num_doubles
= 0;
2043 slot
= path
->slots
[0];
2044 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2045 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
))
2048 /* first try to make some room by pushing left and right */
2049 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2050 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2054 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2060 /* did the pushes work? */
2061 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2065 if (!path
->nodes
[1]) {
2066 ret
= insert_new_root(trans
, root
, path
, 1);
2073 slot
= path
->slots
[0];
2074 nritems
= btrfs_header_nritems(l
);
2075 mid
= (nritems
+ 1) / 2;
2079 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2080 BTRFS_LEAF_DATA_SIZE(root
)) {
2081 if (slot
>= nritems
) {
2085 if (mid
!= nritems
&&
2086 leaf_space_used(l
, mid
, nritems
- mid
) +
2087 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2093 if (leaf_space_used(l
, 0, mid
) + data_size
>
2094 BTRFS_LEAF_DATA_SIZE(root
)) {
2095 if (!extend
&& data_size
&& slot
== 0) {
2097 } else if ((extend
|| !data_size
) && slot
== 0) {
2101 if (mid
!= nritems
&&
2102 leaf_space_used(l
, mid
, nritems
- mid
) +
2103 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2111 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2113 btrfs_item_key(l
, &disk_key
, mid
);
2115 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2116 root
->root_key
.objectid
,
2117 &disk_key
, 0, l
->start
, 0);
2118 if (IS_ERR(right
)) {
2120 return PTR_ERR(right
);
2123 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2124 btrfs_set_header_bytenr(right
, right
->start
);
2125 btrfs_set_header_generation(right
, trans
->transid
);
2126 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2127 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2128 btrfs_set_header_level(right
, 0);
2129 write_extent_buffer(right
, root
->fs_info
->fsid
,
2130 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
2132 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2133 btrfs_header_chunk_tree_uuid(right
),
2138 btrfs_set_header_nritems(right
, 0);
2139 wret
= insert_ptr(trans
, root
, path
,
2140 &disk_key
, right
->start
,
2141 path
->slots
[1] + 1, 1);
2145 free_extent_buffer(path
->nodes
[0]);
2146 path
->nodes
[0] = right
;
2148 path
->slots
[1] += 1;
2150 btrfs_set_header_nritems(right
, 0);
2151 wret
= insert_ptr(trans
, root
, path
,
2157 free_extent_buffer(path
->nodes
[0]);
2158 path
->nodes
[0] = right
;
2160 if (path
->slots
[1] == 0) {
2161 btrfs_fixup_low_keys(root
, path
,
2165 btrfs_mark_buffer_dirty(right
);
2169 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2173 BUG_ON(num_doubles
!= 0);
2182 * This function splits a single item into two items,
2183 * giving 'new_key' to the new item and splitting the
2184 * old one at split_offset (from the start of the item).
2186 * The path may be released by this operation. After
2187 * the split, the path is pointing to the old item. The
2188 * new item is going to be in the same node as the old one.
2190 * Note, the item being split must be smaller enough to live alone on
2191 * a tree block with room for one extra struct btrfs_item
2193 * This allows us to split the item in place, keeping a lock on the
2194 * leaf the entire time.
2196 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2197 struct btrfs_root
*root
,
2198 struct btrfs_path
*path
,
2199 struct btrfs_key
*new_key
,
2200 unsigned long split_offset
)
2203 struct extent_buffer
*leaf
;
2204 struct btrfs_key orig_key
;
2205 struct btrfs_item
*item
;
2206 struct btrfs_item
*new_item
;
2211 struct btrfs_disk_key disk_key
;
2214 leaf
= path
->nodes
[0];
2215 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
2216 if (btrfs_leaf_free_space(root
, leaf
) >= sizeof(struct btrfs_item
))
2219 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2220 btrfs_release_path(path
);
2222 path
->search_for_split
= 1;
2224 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
2225 path
->search_for_split
= 0;
2227 /* if our item isn't there or got smaller, return now */
2228 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
2233 ret
= split_leaf(trans
, root
, &orig_key
, path
, 0, 0);
2236 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
2237 leaf
= path
->nodes
[0];
2240 item
= btrfs_item_nr(path
->slots
[0]);
2241 orig_offset
= btrfs_item_offset(leaf
, item
);
2242 item_size
= btrfs_item_size(leaf
, item
);
2245 buf
= kmalloc(item_size
, GFP_NOFS
);
2246 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
2247 path
->slots
[0]), item_size
);
2248 slot
= path
->slots
[0] + 1;
2249 leaf
= path
->nodes
[0];
2251 nritems
= btrfs_header_nritems(leaf
);
2253 if (slot
!= nritems
) {
2254 /* shift the items */
2255 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2256 btrfs_item_nr_offset(slot
),
2257 (nritems
- slot
) * sizeof(struct btrfs_item
));
2261 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
2262 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2264 new_item
= btrfs_item_nr(slot
);
2266 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
2267 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
2269 btrfs_set_item_offset(leaf
, item
,
2270 orig_offset
+ item_size
- split_offset
);
2271 btrfs_set_item_size(leaf
, item
, split_offset
);
2273 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2275 /* write the data for the start of the original item */
2276 write_extent_buffer(leaf
, buf
,
2277 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2280 /* write the data for the new item */
2281 write_extent_buffer(leaf
, buf
+ split_offset
,
2282 btrfs_item_ptr_offset(leaf
, slot
),
2283 item_size
- split_offset
);
2284 btrfs_mark_buffer_dirty(leaf
);
2287 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2288 btrfs_print_leaf(root
, leaf
);
2295 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
2296 struct btrfs_root
*root
,
2297 struct btrfs_path
*path
,
2298 u32 new_size
, int from_end
)
2302 struct extent_buffer
*leaf
;
2303 struct btrfs_item
*item
;
2305 unsigned int data_end
;
2306 unsigned int old_data_start
;
2307 unsigned int old_size
;
2308 unsigned int size_diff
;
2311 leaf
= path
->nodes
[0];
2312 slot
= path
->slots
[0];
2314 old_size
= btrfs_item_size_nr(leaf
, slot
);
2315 if (old_size
== new_size
)
2318 nritems
= btrfs_header_nritems(leaf
);
2319 data_end
= leaf_data_end(root
, leaf
);
2321 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2323 size_diff
= old_size
- new_size
;
2326 BUG_ON(slot
>= nritems
);
2329 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2331 /* first correct the data pointers */
2332 for (i
= slot
; i
< nritems
; i
++) {
2334 item
= btrfs_item_nr(i
);
2335 ioff
= btrfs_item_offset(leaf
, item
);
2336 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2339 /* shift the data */
2341 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2342 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2343 data_end
, old_data_start
+ new_size
- data_end
);
2345 struct btrfs_disk_key disk_key
;
2348 btrfs_item_key(leaf
, &disk_key
, slot
);
2350 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2352 struct btrfs_file_extent_item
*fi
;
2354 fi
= btrfs_item_ptr(leaf
, slot
,
2355 struct btrfs_file_extent_item
);
2356 fi
= (struct btrfs_file_extent_item
*)(
2357 (unsigned long)fi
- size_diff
);
2359 if (btrfs_file_extent_type(leaf
, fi
) ==
2360 BTRFS_FILE_EXTENT_INLINE
) {
2361 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2362 memmove_extent_buffer(leaf
, ptr
,
2364 offsetof(struct btrfs_file_extent_item
,
2369 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2370 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2371 data_end
, old_data_start
- data_end
);
2373 offset
= btrfs_disk_key_offset(&disk_key
);
2374 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2375 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2377 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2380 item
= btrfs_item_nr(slot
);
2381 btrfs_set_item_size(leaf
, item
, new_size
);
2382 btrfs_mark_buffer_dirty(leaf
);
2385 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2386 btrfs_print_leaf(root
, leaf
);
2392 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
2393 struct btrfs_root
*root
, struct btrfs_path
*path
,
2398 struct extent_buffer
*leaf
;
2399 struct btrfs_item
*item
;
2401 unsigned int data_end
;
2402 unsigned int old_data
;
2403 unsigned int old_size
;
2406 leaf
= path
->nodes
[0];
2408 nritems
= btrfs_header_nritems(leaf
);
2409 data_end
= leaf_data_end(root
, leaf
);
2411 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2412 btrfs_print_leaf(root
, leaf
);
2415 slot
= path
->slots
[0];
2416 old_data
= btrfs_item_end_nr(leaf
, slot
);
2419 if (slot
>= nritems
) {
2420 btrfs_print_leaf(root
, leaf
);
2421 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2426 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2428 /* first correct the data pointers */
2429 for (i
= slot
; i
< nritems
; i
++) {
2431 item
= btrfs_item_nr(i
);
2432 ioff
= btrfs_item_offset(leaf
, item
);
2433 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2436 /* shift the data */
2437 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2438 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2439 data_end
, old_data
- data_end
);
2441 data_end
= old_data
;
2442 old_size
= btrfs_item_size_nr(leaf
, slot
);
2443 item
= btrfs_item_nr(slot
);
2444 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2445 btrfs_mark_buffer_dirty(leaf
);
2448 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2449 btrfs_print_leaf(root
, leaf
);
2456 * Given a key and some data, insert an item into the tree.
2457 * This does all the path init required, making room in the tree if needed.
2459 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2460 struct btrfs_root
*root
,
2461 struct btrfs_path
*path
,
2462 struct btrfs_key
*cpu_key
, u32
*data_size
,
2465 struct extent_buffer
*leaf
;
2466 struct btrfs_item
*item
;
2473 unsigned int data_end
;
2474 struct btrfs_disk_key disk_key
;
2476 for (i
= 0; i
< nr
; i
++) {
2477 total_data
+= data_size
[i
];
2480 /* create a root if there isn't one */
2484 total_size
= total_data
+ nr
* sizeof(struct btrfs_item
);
2485 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2492 leaf
= path
->nodes
[0];
2494 nritems
= btrfs_header_nritems(leaf
);
2495 data_end
= leaf_data_end(root
, leaf
);
2497 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
2498 btrfs_print_leaf(root
, leaf
);
2499 printk("not enough freespace need %u have %d\n",
2500 total_size
, btrfs_leaf_free_space(root
, leaf
));
2504 slot
= path
->slots
[0];
2507 if (slot
!= nritems
) {
2508 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2510 if (old_data
< data_end
) {
2511 btrfs_print_leaf(root
, leaf
);
2512 printk("slot %d old_data %d data_end %d\n",
2513 slot
, old_data
, data_end
);
2517 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2519 /* first correct the data pointers */
2520 for (i
= slot
; i
< nritems
; i
++) {
2523 item
= btrfs_item_nr(i
);
2524 ioff
= btrfs_item_offset(leaf
, item
);
2525 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2528 /* shift the items */
2529 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2530 btrfs_item_nr_offset(slot
),
2531 (nritems
- slot
) * sizeof(struct btrfs_item
));
2533 /* shift the data */
2534 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2535 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2536 data_end
, old_data
- data_end
);
2537 data_end
= old_data
;
2540 /* setup the item for the new data */
2541 for (i
= 0; i
< nr
; i
++) {
2542 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2543 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2544 item
= btrfs_item_nr(slot
+ i
);
2545 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2546 data_end
-= data_size
[i
];
2547 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2549 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2550 btrfs_mark_buffer_dirty(leaf
);
2554 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2555 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2558 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2559 btrfs_print_leaf(root
, leaf
);
2568 * Given a key and some data, insert an item into the tree.
2569 * This does all the path init required, making room in the tree if needed.
2571 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2572 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2576 struct btrfs_path
*path
;
2577 struct extent_buffer
*leaf
;
2580 path
= btrfs_alloc_path();
2582 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2584 leaf
= path
->nodes
[0];
2585 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2586 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2587 btrfs_mark_buffer_dirty(leaf
);
2589 btrfs_free_path(path
);
2594 * delete the pointer from a given node.
2596 * If the delete empties a node, the node is removed from the tree,
2597 * continuing all the way the root if required. The root is converted into
2598 * a leaf if all the nodes are emptied.
2600 int btrfs_del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2601 struct btrfs_path
*path
, int level
, int slot
)
2603 struct extent_buffer
*parent
= path
->nodes
[level
];
2607 nritems
= btrfs_header_nritems(parent
);
2608 if (slot
!= nritems
-1) {
2609 memmove_extent_buffer(parent
,
2610 btrfs_node_key_ptr_offset(slot
),
2611 btrfs_node_key_ptr_offset(slot
+ 1),
2612 sizeof(struct btrfs_key_ptr
) *
2613 (nritems
- slot
- 1));
2616 btrfs_set_header_nritems(parent
, nritems
);
2617 if (nritems
== 0 && parent
== root
->node
) {
2618 BUG_ON(btrfs_header_level(root
->node
) != 1);
2619 /* just turn the root into a leaf and break */
2620 btrfs_set_header_level(root
->node
, 0);
2621 } else if (slot
== 0) {
2622 struct btrfs_disk_key disk_key
;
2624 btrfs_node_key(parent
, &disk_key
, 0);
2625 btrfs_fixup_low_keys(root
, path
, &disk_key
, level
+ 1);
2627 btrfs_mark_buffer_dirty(parent
);
2632 * a helper function to delete the leaf pointed to by path->slots[1] and
2635 * This deletes the pointer in path->nodes[1] and frees the leaf
2636 * block extent. zero is returned if it all worked out, < 0 otherwise.
2638 * The path must have already been setup for deleting the leaf, including
2639 * all the proper balancing. path->nodes[1] must be locked.
2641 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
2642 struct btrfs_root
*root
,
2643 struct btrfs_path
*path
,
2644 struct extent_buffer
*leaf
)
2648 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
2649 ret
= btrfs_del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
2653 ret
= btrfs_free_extent(trans
, root
, leaf
->start
, leaf
->len
,
2654 0, root
->root_key
.objectid
, 0, 0);
2659 * delete the item at the leaf level in path. If that empties
2660 * the leaf, remove it from the tree
2662 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2663 struct btrfs_path
*path
, int slot
, int nr
)
2665 struct extent_buffer
*leaf
;
2666 struct btrfs_item
*item
;
2674 leaf
= path
->nodes
[0];
2675 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2677 for (i
= 0; i
< nr
; i
++)
2678 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2680 nritems
= btrfs_header_nritems(leaf
);
2682 if (slot
+ nr
!= nritems
) {
2683 int data_end
= leaf_data_end(root
, leaf
);
2685 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2687 btrfs_leaf_data(leaf
) + data_end
,
2688 last_off
- data_end
);
2690 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2693 item
= btrfs_item_nr(i
);
2694 ioff
= btrfs_item_offset(leaf
, item
);
2695 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2698 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2699 btrfs_item_nr_offset(slot
+ nr
),
2700 sizeof(struct btrfs_item
) *
2701 (nritems
- slot
- nr
));
2703 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2706 /* delete the leaf if we've emptied it */
2708 if (leaf
== root
->node
) {
2709 btrfs_set_header_level(leaf
, 0);
2711 clean_tree_block(trans
, root
, leaf
);
2712 wait_on_tree_block_writeback(root
, leaf
);
2714 wret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2720 int used
= leaf_space_used(leaf
, 0, nritems
);
2722 struct btrfs_disk_key disk_key
;
2724 btrfs_item_key(leaf
, &disk_key
, 0);
2725 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2728 /* delete the leaf if it is mostly empty */
2729 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 4) {
2730 /* push_leaf_left fixes the path.
2731 * make sure the path still points to our leaf
2732 * for possible call to del_ptr below
2734 slot
= path
->slots
[1];
2735 extent_buffer_get(leaf
);
2737 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2738 if (wret
< 0 && wret
!= -ENOSPC
)
2741 if (path
->nodes
[0] == leaf
&&
2742 btrfs_header_nritems(leaf
)) {
2743 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2744 if (wret
< 0 && wret
!= -ENOSPC
)
2748 if (btrfs_header_nritems(leaf
) == 0) {
2749 clean_tree_block(trans
, root
, leaf
);
2750 wait_on_tree_block_writeback(root
, leaf
);
2752 path
->slots
[1] = slot
;
2753 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2755 free_extent_buffer(leaf
);
2758 btrfs_mark_buffer_dirty(leaf
);
2759 free_extent_buffer(leaf
);
2762 btrfs_mark_buffer_dirty(leaf
);
2769 * walk up the tree as far as required to find the previous leaf.
2770 * returns 0 if it found something or 1 if there are no lesser leaves.
2771 * returns < 0 on io errors.
2773 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2777 struct extent_buffer
*c
;
2778 struct extent_buffer
*next
= NULL
;
2780 while(level
< BTRFS_MAX_LEVEL
) {
2781 if (!path
->nodes
[level
])
2784 slot
= path
->slots
[level
];
2785 c
= path
->nodes
[level
];
2788 if (level
== BTRFS_MAX_LEVEL
)
2794 next
= read_node_slot(root
, c
, slot
);
2795 if (!extent_buffer_uptodate(next
)) {
2797 return PTR_ERR(next
);
2802 path
->slots
[level
] = slot
;
2805 c
= path
->nodes
[level
];
2806 free_extent_buffer(c
);
2807 slot
= btrfs_header_nritems(next
);
2810 path
->nodes
[level
] = next
;
2811 path
->slots
[level
] = slot
;
2814 next
= read_node_slot(root
, next
, slot
);
2815 if (!extent_buffer_uptodate(next
)) {
2817 return PTR_ERR(next
);
2825 * walk up the tree as far as required to find the next leaf.
2826 * returns 0 if it found something or 1 if there are no greater leaves.
2827 * returns < 0 on io errors.
2829 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2833 struct extent_buffer
*c
;
2834 struct extent_buffer
*next
= NULL
;
2836 while(level
< BTRFS_MAX_LEVEL
) {
2837 if (!path
->nodes
[level
])
2840 slot
= path
->slots
[level
] + 1;
2841 c
= path
->nodes
[level
];
2842 if (slot
>= btrfs_header_nritems(c
)) {
2844 if (level
== BTRFS_MAX_LEVEL
)
2850 reada_for_search(root
, path
, level
, slot
, 0);
2852 next
= read_node_slot(root
, c
, slot
);
2853 if (!extent_buffer_uptodate(next
))
2857 path
->slots
[level
] = slot
;
2860 c
= path
->nodes
[level
];
2861 free_extent_buffer(c
);
2862 path
->nodes
[level
] = next
;
2863 path
->slots
[level
] = 0;
2867 reada_for_search(root
, path
, level
, 0, 0);
2868 next
= read_node_slot(root
, next
, 0);
2869 if (!extent_buffer_uptodate(next
))
2875 int btrfs_previous_item(struct btrfs_root
*root
,
2876 struct btrfs_path
*path
, u64 min_objectid
,
2879 struct btrfs_key found_key
;
2880 struct extent_buffer
*leaf
;
2884 if (path
->slots
[0] == 0) {
2885 ret
= btrfs_prev_leaf(root
, path
);
2891 leaf
= path
->nodes
[0];
2892 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2893 if (found_key
.type
== type
)
2900 * search in extent tree to find a previous Metadata/Data extent item with
2903 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2905 int btrfs_previous_extent_item(struct btrfs_root
*root
,
2906 struct btrfs_path
*path
, u64 min_objectid
)
2908 struct btrfs_key found_key
;
2909 struct extent_buffer
*leaf
;
2914 if (path
->slots
[0] == 0) {
2915 ret
= btrfs_prev_leaf(root
, path
);
2921 leaf
= path
->nodes
[0];
2922 nritems
= btrfs_header_nritems(leaf
);
2925 if (path
->slots
[0] == nritems
)
2928 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2929 if (found_key
.objectid
< min_objectid
)
2931 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
2932 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)
2934 if (found_key
.objectid
== min_objectid
&&
2935 found_key
.type
< BTRFS_EXTENT_ITEM_KEY
)