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 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
80 struct btrfs_root
*root
,
81 struct extent_buffer
*buf
,
82 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
84 struct extent_buffer
*cow
;
87 struct btrfs_root
*new_root
;
88 struct btrfs_disk_key disk_key
;
90 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
94 memcpy(new_root
, root
, sizeof(*new_root
));
95 new_root
->root_key
.objectid
= new_root_objectid
;
97 WARN_ON(root
->ref_cows
&& trans
->transid
!=
98 root
->fs_info
->running_transaction
->transid
);
99 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
101 level
= btrfs_header_level(buf
);
103 btrfs_item_key(buf
, &disk_key
, 0);
105 btrfs_node_key(buf
, &disk_key
, 0);
106 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
107 new_root_objectid
, &disk_key
,
108 level
, buf
->start
, 0);
114 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
115 btrfs_set_header_bytenr(cow
, cow
->start
);
116 btrfs_set_header_generation(cow
, trans
->transid
);
117 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
118 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
119 BTRFS_HEADER_FLAG_RELOC
);
120 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
121 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
123 btrfs_set_header_owner(cow
, new_root_objectid
);
125 write_extent_buffer(cow
, root
->fs_info
->fsid
,
126 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
128 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
129 ret
= btrfs_inc_ref(trans
, new_root
, cow
, 0);
135 btrfs_mark_buffer_dirty(cow
);
141 * check if the tree block can be shared by multiple trees
143 static int btrfs_block_can_be_shared(struct btrfs_root
*root
,
144 struct extent_buffer
*buf
)
147 * Tree blocks not in reference counted trees and tree roots
148 * are never shared. If a block was allocated after the last
149 * snapshot and the block was not allocated by tree relocation,
150 * we know the block is not shared.
152 if (root
->ref_cows
&&
153 buf
!= root
->node
&& buf
!= root
->commit_root
&&
154 (btrfs_header_generation(buf
) <=
155 btrfs_root_last_snapshot(&root
->root_item
) ||
156 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
158 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
159 if (root
->ref_cows
&&
160 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
166 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
167 struct btrfs_root
*root
,
168 struct extent_buffer
*buf
,
169 struct extent_buffer
*cow
)
178 * Backrefs update rules:
180 * Always use full backrefs for extent pointers in tree block
181 * allocated by tree relocation.
183 * If a shared tree block is no longer referenced by its owner
184 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
185 * use full backrefs for extent pointers in tree block.
187 * If a tree block is been relocating
188 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
189 * use full backrefs for extent pointers in tree block.
190 * The reason for this is some operations (such as drop tree)
191 * are only allowed for blocks use full backrefs.
194 if (btrfs_block_can_be_shared(root
, buf
)) {
195 ret
= btrfs_lookup_extent_info(trans
, trans
->fs_info
,
197 btrfs_header_level(buf
), 1,
203 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
204 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
205 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
210 owner
= btrfs_header_owner(buf
);
211 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) &&
212 owner
== BTRFS_TREE_RELOC_OBJECTID
);
215 if ((owner
== root
->root_key
.objectid
||
216 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
217 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
218 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
221 if (root
->root_key
.objectid
==
222 BTRFS_TREE_RELOC_OBJECTID
) {
223 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
225 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
228 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
231 if (root
->root_key
.objectid
==
232 BTRFS_TREE_RELOC_OBJECTID
)
233 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
235 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
238 if (new_flags
!= 0) {
239 ret
= btrfs_set_block_flags(trans
, buf
->start
,
240 btrfs_header_level(buf
),
245 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
246 if (root
->root_key
.objectid
==
247 BTRFS_TREE_RELOC_OBJECTID
)
248 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
250 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
252 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
255 clean_tree_block(buf
);
260 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
261 struct btrfs_root
*root
,
262 struct extent_buffer
*buf
,
263 struct extent_buffer
*parent
, int parent_slot
,
264 struct extent_buffer
**cow_ret
,
265 u64 search_start
, u64 empty_size
)
267 struct extent_buffer
*cow
;
268 struct btrfs_disk_key disk_key
;
271 WARN_ON(root
->ref_cows
&& trans
->transid
!=
272 root
->fs_info
->running_transaction
->transid
);
273 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
275 level
= btrfs_header_level(buf
);
278 btrfs_item_key(buf
, &disk_key
, 0);
280 btrfs_node_key(buf
, &disk_key
, 0);
282 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
,
283 root
->root_key
.objectid
, &disk_key
,
284 level
, search_start
, empty_size
);
288 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
289 btrfs_set_header_bytenr(cow
, cow
->start
);
290 btrfs_set_header_generation(cow
, trans
->transid
);
291 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
292 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
293 BTRFS_HEADER_FLAG_RELOC
);
294 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
295 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
297 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
299 write_extent_buffer(cow
, root
->fs_info
->fsid
,
300 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
302 WARN_ON(!(buf
->flags
& EXTENT_BAD_TRANSID
) &&
303 btrfs_header_generation(buf
) > trans
->transid
);
305 update_ref_for_cow(trans
, root
, buf
, cow
);
307 if (buf
== root
->node
) {
309 extent_buffer_get(cow
);
311 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
312 0, root
->root_key
.objectid
, level
, 0);
313 free_extent_buffer(buf
);
314 add_root_to_dirty_list(root
);
316 btrfs_set_node_blockptr(parent
, parent_slot
,
318 WARN_ON(trans
->transid
== 0);
319 btrfs_set_node_ptr_generation(parent
, parent_slot
,
321 btrfs_mark_buffer_dirty(parent
);
322 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
324 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
325 0, root
->root_key
.objectid
, level
, 1);
327 if (!list_empty(&buf
->recow
)) {
328 list_del_init(&buf
->recow
);
329 free_extent_buffer(buf
);
331 free_extent_buffer(buf
);
332 btrfs_mark_buffer_dirty(cow
);
337 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
338 struct btrfs_root
*root
,
339 struct extent_buffer
*buf
)
341 if (btrfs_header_generation(buf
) == trans
->transid
&&
342 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
343 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
344 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
349 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
350 struct btrfs_root
*root
, struct extent_buffer
*buf
,
351 struct extent_buffer
*parent
, int parent_slot
,
352 struct extent_buffer
**cow_ret
)
357 if (trans->transaction != root->fs_info->running_transaction) {
358 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
359 root->fs_info->running_transaction->transid);
363 if (trans
->transid
!= root
->fs_info
->generation
) {
364 printk(KERN_CRIT
"trans %llu running %llu\n",
365 (unsigned long long)trans
->transid
,
366 (unsigned long long)root
->fs_info
->generation
);
369 if (!should_cow_block(trans
, root
, buf
)) {
374 search_start
= buf
->start
& ~((u64
)SZ_1G
- 1);
375 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
376 parent_slot
, cow_ret
, search_start
, 0);
380 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
382 if (k1
->objectid
> k2
->objectid
)
384 if (k1
->objectid
< k2
->objectid
)
386 if (k1
->type
> k2
->type
)
388 if (k1
->type
< k2
->type
)
390 if (k1
->offset
> k2
->offset
)
392 if (k1
->offset
< k2
->offset
)
398 * compare two keys in a memcmp fashion
400 static int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
404 btrfs_disk_key_to_cpu(&k1
, disk
);
405 return btrfs_comp_cpu_keys(&k1
, k2
);
409 * The leaf data grows from end-to-front in the node.
410 * this returns the address of the start of the last item,
411 * which is the stop of the leaf data stack
413 static inline unsigned int leaf_data_end(const struct btrfs_fs_info
*fs_info
,
414 const struct extent_buffer
*leaf
)
416 u32 nr
= btrfs_header_nritems(leaf
);
418 return BTRFS_LEAF_DATA_SIZE(fs_info
);
419 return btrfs_item_offset_nr(leaf
, nr
- 1);
422 enum btrfs_tree_block_status
423 btrfs_check_node(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
424 struct extent_buffer
*buf
)
427 struct btrfs_key cpukey
;
428 struct btrfs_disk_key key
;
429 u32 nritems
= btrfs_header_nritems(buf
);
430 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
432 if (nritems
== 0 || nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
))
435 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
436 if (parent_key
&& parent_key
->type
) {
437 btrfs_node_key(buf
, &key
, 0);
438 if (memcmp(parent_key
, &key
, sizeof(key
)))
441 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
442 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
443 btrfs_node_key(buf
, &key
, i
);
444 btrfs_node_key_to_cpu(buf
, &cpukey
, i
+ 1);
445 if (btrfs_comp_keys(&key
, &cpukey
) >= 0)
448 return BTRFS_TREE_BLOCK_CLEAN
;
450 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
452 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
454 btrfs_node_key_to_cpu(buf
, &cpukey
, 0);
455 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
456 buf
->start
, buf
->len
,
457 btrfs_header_level(buf
));
462 enum btrfs_tree_block_status
463 btrfs_check_leaf(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
464 struct extent_buffer
*buf
)
467 struct btrfs_key cpukey
;
468 struct btrfs_disk_key key
;
469 u32 nritems
= btrfs_header_nritems(buf
);
470 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
472 if (nritems
* sizeof(struct btrfs_item
) > buf
->len
) {
473 fprintf(stderr
, "invalid number of items %llu\n",
474 (unsigned long long)buf
->start
);
478 if (btrfs_header_level(buf
) != 0) {
479 ret
= BTRFS_TREE_BLOCK_INVALID_LEVEL
;
480 fprintf(stderr
, "leaf is not a leaf %llu\n",
481 (unsigned long long)btrfs_header_bytenr(buf
));
484 if (btrfs_leaf_free_space(buf
) < 0) {
485 ret
= BTRFS_TREE_BLOCK_INVALID_FREE_SPACE
;
486 fprintf(stderr
, "leaf free space incorrect %llu %d\n",
487 (unsigned long long)btrfs_header_bytenr(buf
),
488 btrfs_leaf_free_space(buf
));
493 return BTRFS_TREE_BLOCK_CLEAN
;
495 btrfs_item_key(buf
, &key
, 0);
496 if (parent_key
&& parent_key
->type
&&
497 memcmp(parent_key
, &key
, sizeof(key
))) {
498 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
499 fprintf(stderr
, "leaf parent key incorrect %llu\n",
500 (unsigned long long)btrfs_header_bytenr(buf
));
503 for (i
= 0; nritems
> 1 && i
< nritems
- 1; i
++) {
504 btrfs_item_key(buf
, &key
, i
);
505 btrfs_item_key_to_cpu(buf
, &cpukey
, i
+ 1);
506 if (btrfs_comp_keys(&key
, &cpukey
) >= 0) {
507 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
508 fprintf(stderr
, "bad key ordering %d %d\n", i
, i
+1);
511 if (btrfs_item_offset_nr(buf
, i
) !=
512 btrfs_item_end_nr(buf
, i
+ 1)) {
513 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
514 fprintf(stderr
, "incorrect offsets %u %u\n",
515 btrfs_item_offset_nr(buf
, i
),
516 btrfs_item_end_nr(buf
, i
+ 1));
519 if (i
== 0 && btrfs_item_end_nr(buf
, i
) !=
520 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
521 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
522 fprintf(stderr
, "bad item end %u wanted %u\n",
523 btrfs_item_end_nr(buf
, i
),
524 (unsigned)BTRFS_LEAF_DATA_SIZE(root
->fs_info
));
529 for (i
= 0; i
< nritems
; i
++) {
530 if (btrfs_item_end_nr(buf
, i
) >
531 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
532 btrfs_item_key(buf
, &key
, 0);
533 btrfs_print_key(&key
);
535 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
536 fprintf(stderr
, "slot end outside of leaf %llu > %llu\n",
537 (unsigned long long)btrfs_item_end_nr(buf
, i
),
538 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
544 return BTRFS_TREE_BLOCK_CLEAN
;
546 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
548 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
550 btrfs_item_key_to_cpu(buf
, &cpukey
, 0);
552 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
553 buf
->start
, buf
->len
, 0);
558 static int noinline
check_block(struct btrfs_root
*root
,
559 struct btrfs_path
*path
, int level
)
561 struct btrfs_disk_key key
;
562 struct btrfs_disk_key
*key_ptr
= NULL
;
563 struct extent_buffer
*parent
;
564 enum btrfs_tree_block_status ret
;
566 if (path
->skip_check_block
)
568 if (path
->nodes
[level
+ 1]) {
569 parent
= path
->nodes
[level
+ 1];
570 btrfs_node_key(parent
, &key
, path
->slots
[level
+ 1]);
574 ret
= btrfs_check_leaf(root
, key_ptr
, path
->nodes
[0]);
576 ret
= btrfs_check_node(root
, key_ptr
, path
->nodes
[level
]);
577 if (ret
== BTRFS_TREE_BLOCK_CLEAN
)
583 * search for key in the extent_buffer. The items start at offset p,
584 * and they are item_size apart. There are 'max' items in p.
586 * the slot in the array is returned via slot, and it points to
587 * the place where you would insert key if it is not found in
590 * slot may point to max if the key is bigger than all of the keys
592 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
593 int item_size
, struct btrfs_key
*key
,
600 unsigned long offset
;
601 struct btrfs_disk_key
*tmp
;
604 mid
= (low
+ high
) / 2;
605 offset
= p
+ mid
* item_size
;
607 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
608 ret
= btrfs_comp_keys(tmp
, key
);
624 * simple bin_search frontend that does the right thing for
627 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
628 int level
, int *slot
)
631 return generic_bin_search(eb
,
632 offsetof(struct btrfs_leaf
, items
),
633 sizeof(struct btrfs_item
),
634 key
, btrfs_header_nritems(eb
),
637 return generic_bin_search(eb
,
638 offsetof(struct btrfs_node
, ptrs
),
639 sizeof(struct btrfs_key_ptr
),
640 key
, btrfs_header_nritems(eb
),
644 struct extent_buffer
*read_node_slot(struct btrfs_fs_info
*fs_info
,
645 struct extent_buffer
*parent
, int slot
)
647 struct extent_buffer
*ret
;
648 int level
= btrfs_header_level(parent
);
652 if (slot
>= btrfs_header_nritems(parent
))
658 ret
= read_tree_block(fs_info
, btrfs_node_blockptr(parent
, slot
),
659 btrfs_node_ptr_generation(parent
, slot
));
660 if (!extent_buffer_uptodate(ret
))
661 return ERR_PTR(-EIO
);
663 if (btrfs_header_level(ret
) != level
- 1) {
665 "child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
666 btrfs_header_bytenr(parent
), slot
,
667 btrfs_header_level(parent
), btrfs_header_level(ret
));
668 free_extent_buffer(ret
);
669 return ERR_PTR(-EIO
);
674 static int balance_level(struct btrfs_trans_handle
*trans
,
675 struct btrfs_root
*root
,
676 struct btrfs_path
*path
, int level
)
678 struct extent_buffer
*right
= NULL
;
679 struct extent_buffer
*mid
;
680 struct extent_buffer
*left
= NULL
;
681 struct extent_buffer
*parent
= NULL
;
682 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
686 int orig_slot
= path
->slots
[level
];
692 mid
= path
->nodes
[level
];
693 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
695 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
697 if (level
< BTRFS_MAX_LEVEL
- 1) {
698 parent
= path
->nodes
[level
+ 1];
699 pslot
= path
->slots
[level
+ 1];
703 * deal with the case where there is only one pointer in the root
704 * by promoting the node below to a root
707 struct extent_buffer
*child
;
709 if (btrfs_header_nritems(mid
) != 1)
712 /* promote the child to a root */
713 child
= read_node_slot(fs_info
, mid
, 0);
714 BUG_ON(!extent_buffer_uptodate(child
));
715 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
719 add_root_to_dirty_list(root
);
720 path
->nodes
[level
] = NULL
;
721 clean_tree_block(mid
);
722 /* once for the path */
723 free_extent_buffer(mid
);
725 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
726 0, root
->root_key
.objectid
,
728 /* once for the root ptr */
729 free_extent_buffer(mid
);
732 if (btrfs_header_nritems(mid
) >
733 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) / 4)
736 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
737 if (extent_buffer_uptodate(left
)) {
738 wret
= btrfs_cow_block(trans
, root
, left
,
739 parent
, pslot
- 1, &left
);
745 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
746 if (extent_buffer_uptodate(right
)) {
747 wret
= btrfs_cow_block(trans
, root
, right
,
748 parent
, pslot
+ 1, &right
);
755 /* first, try to make some room in the middle buffer */
757 orig_slot
+= btrfs_header_nritems(left
);
758 wret
= push_node_left(trans
, root
, left
, mid
, 1);
764 * then try to empty the right most buffer into the middle
767 wret
= push_node_left(trans
, root
, mid
, right
, 1);
768 if (wret
< 0 && wret
!= -ENOSPC
)
770 if (btrfs_header_nritems(right
) == 0) {
771 u64 bytenr
= right
->start
;
772 u32 blocksize
= right
->len
;
774 clean_tree_block(right
);
775 free_extent_buffer(right
);
777 wret
= btrfs_del_ptr(root
, path
, level
+ 1, pslot
+ 1);
780 wret
= btrfs_free_extent(trans
, root
, bytenr
,
782 root
->root_key
.objectid
,
787 struct btrfs_disk_key right_key
;
788 btrfs_node_key(right
, &right_key
, 0);
789 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
790 btrfs_mark_buffer_dirty(parent
);
793 if (btrfs_header_nritems(mid
) == 1) {
795 * we're not allowed to leave a node with one item in the
796 * tree during a delete. A deletion from lower in the tree
797 * could try to delete the only pointer in this node.
798 * So, pull some keys from the left.
799 * There has to be a left pointer at this point because
800 * otherwise we would have pulled some pointers from the
804 wret
= balance_node_right(trans
, root
, mid
, left
);
810 wret
= push_node_left(trans
, root
, left
, mid
, 1);
816 if (btrfs_header_nritems(mid
) == 0) {
817 /* we've managed to empty the middle node, drop it */
818 u64 bytenr
= mid
->start
;
819 u32 blocksize
= mid
->len
;
820 clean_tree_block(mid
);
821 free_extent_buffer(mid
);
823 wret
= btrfs_del_ptr(root
, path
, level
+ 1, pslot
);
826 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
827 0, root
->root_key
.objectid
,
832 /* update the parent key to reflect our changes */
833 struct btrfs_disk_key mid_key
;
834 btrfs_node_key(mid
, &mid_key
, 0);
835 btrfs_set_node_key(parent
, &mid_key
, pslot
);
836 btrfs_mark_buffer_dirty(parent
);
839 /* update the path */
841 if (btrfs_header_nritems(left
) > orig_slot
) {
842 extent_buffer_get(left
);
843 path
->nodes
[level
] = left
;
844 path
->slots
[level
+ 1] -= 1;
845 path
->slots
[level
] = orig_slot
;
847 free_extent_buffer(mid
);
849 orig_slot
-= btrfs_header_nritems(left
);
850 path
->slots
[level
] = orig_slot
;
853 /* double check we haven't messed things up */
854 check_block(root
, path
, level
);
856 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
860 free_extent_buffer(right
);
862 free_extent_buffer(left
);
866 /* returns zero if the push worked, non-zero otherwise */
867 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
868 struct btrfs_root
*root
,
869 struct btrfs_path
*path
, int level
)
871 struct extent_buffer
*right
= NULL
;
872 struct extent_buffer
*mid
;
873 struct extent_buffer
*left
= NULL
;
874 struct extent_buffer
*parent
= NULL
;
875 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
879 int orig_slot
= path
->slots
[level
];
884 mid
= path
->nodes
[level
];
885 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
887 if (level
< BTRFS_MAX_LEVEL
- 1) {
888 parent
= path
->nodes
[level
+ 1];
889 pslot
= path
->slots
[level
+ 1];
895 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
897 /* first, try to make some room in the middle buffer */
898 if (extent_buffer_uptodate(left
)) {
900 left_nr
= btrfs_header_nritems(left
);
901 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 1) {
904 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
909 wret
= push_node_left(trans
, root
,
916 struct btrfs_disk_key disk_key
;
917 orig_slot
+= left_nr
;
918 btrfs_node_key(mid
, &disk_key
, 0);
919 btrfs_set_node_key(parent
, &disk_key
, pslot
);
920 btrfs_mark_buffer_dirty(parent
);
921 if (btrfs_header_nritems(left
) > orig_slot
) {
922 path
->nodes
[level
] = left
;
923 path
->slots
[level
+ 1] -= 1;
924 path
->slots
[level
] = orig_slot
;
925 free_extent_buffer(mid
);
928 btrfs_header_nritems(left
);
929 path
->slots
[level
] = orig_slot
;
930 free_extent_buffer(left
);
934 free_extent_buffer(left
);
936 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
939 * then try to empty the right most buffer into the middle
941 if (extent_buffer_uptodate(right
)) {
943 right_nr
= btrfs_header_nritems(right
);
944 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - 1) {
947 ret
= btrfs_cow_block(trans
, root
, right
,
953 wret
= balance_node_right(trans
, root
,
960 struct btrfs_disk_key disk_key
;
962 btrfs_node_key(right
, &disk_key
, 0);
963 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
964 btrfs_mark_buffer_dirty(parent
);
966 if (btrfs_header_nritems(mid
) <= orig_slot
) {
967 path
->nodes
[level
] = right
;
968 path
->slots
[level
+ 1] += 1;
969 path
->slots
[level
] = orig_slot
-
970 btrfs_header_nritems(mid
);
971 free_extent_buffer(mid
);
973 free_extent_buffer(right
);
977 free_extent_buffer(right
);
983 * readahead one full node of leaves
985 void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
986 int level
, int slot
, u64 objectid
)
988 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
989 struct extent_buffer
*node
;
990 struct btrfs_disk_key disk_key
;
996 int direction
= path
->reada
;
997 struct extent_buffer
*eb
;
1004 if (!path
->nodes
[level
])
1007 node
= path
->nodes
[level
];
1008 search
= btrfs_node_blockptr(node
, slot
);
1009 eb
= btrfs_find_tree_block(fs_info
, search
, fs_info
->nodesize
);
1011 free_extent_buffer(eb
);
1015 highest_read
= search
;
1016 lowest_read
= search
;
1018 nritems
= btrfs_header_nritems(node
);
1021 if (direction
< 0) {
1025 } else if (direction
> 0) {
1030 if (path
->reada
< 0 && objectid
) {
1031 btrfs_node_key(node
, &disk_key
, nr
);
1032 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1035 search
= btrfs_node_blockptr(node
, nr
);
1036 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1037 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1038 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1039 readahead_tree_block(fs_info
, search
,
1040 btrfs_node_ptr_generation(node
, nr
));
1041 nread
+= fs_info
->nodesize
;
1044 if (path
->reada
< 2 && (nread
> SZ_256K
|| nscan
> 32))
1046 if(nread
> SZ_1M
|| nscan
> 128)
1049 if (search
< lowest_read
)
1050 lowest_read
= search
;
1051 if (search
> highest_read
)
1052 highest_read
= search
;
1056 int btrfs_find_item(struct btrfs_root
*fs_root
, struct btrfs_path
*found_path
,
1057 u64 iobjectid
, u64 ioff
, u8 key_type
,
1058 struct btrfs_key
*found_key
)
1061 struct btrfs_key key
;
1062 struct extent_buffer
*eb
;
1063 struct btrfs_path
*path
;
1065 key
.type
= key_type
;
1066 key
.objectid
= iobjectid
;
1069 if (found_path
== NULL
) {
1070 path
= btrfs_alloc_path();
1076 ret
= btrfs_search_slot(NULL
, fs_root
, &key
, path
, 0, 0);
1077 if ((ret
< 0) || (found_key
== NULL
))
1080 eb
= path
->nodes
[0];
1081 if (ret
&& path
->slots
[0] >= btrfs_header_nritems(eb
)) {
1082 ret
= btrfs_next_leaf(fs_root
, path
);
1085 eb
= path
->nodes
[0];
1088 btrfs_item_key_to_cpu(eb
, found_key
, path
->slots
[0]);
1089 if (found_key
->type
!= key
.type
||
1090 found_key
->objectid
!= key
.objectid
) {
1096 if (path
!= found_path
)
1097 btrfs_free_path(path
);
1102 * look for key in the tree. path is filled in with nodes along the way
1103 * if key is found, we return zero and you can find the item in the leaf
1104 * level of the path (level 0)
1106 * If the key isn't found, the path points to the slot where it should
1107 * be inserted, and 1 is returned. If there are other errors during the
1108 * search a negative error number is returned.
1110 * if ins_len > 0, nodes and leaves will be split as we walk down the
1111 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1114 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1115 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1118 struct extent_buffer
*b
;
1122 int should_reada
= p
->reada
;
1123 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1124 u8 lowest_level
= 0;
1126 lowest_level
= p
->lowest_level
;
1127 WARN_ON(lowest_level
&& ins_len
> 0);
1128 WARN_ON(p
->nodes
[0] != NULL
);
1130 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1134 extent_buffer_get(b
);
1136 level
= btrfs_header_level(b
);
1139 wret
= btrfs_cow_block(trans
, root
, b
,
1140 p
->nodes
[level
+ 1],
1141 p
->slots
[level
+ 1],
1144 free_extent_buffer(b
);
1148 BUG_ON(!cow
&& ins_len
);
1149 if (level
!= btrfs_header_level(b
))
1151 level
= btrfs_header_level(b
);
1152 p
->nodes
[level
] = b
;
1153 ret
= check_block(root
, p
, level
);
1156 ret
= bin_search(b
, key
, level
, &slot
);
1158 if (ret
&& slot
> 0)
1160 p
->slots
[level
] = slot
;
1161 if ((p
->search_for_split
|| ins_len
> 0) &&
1162 btrfs_header_nritems(b
) >=
1163 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 3) {
1164 int sret
= split_node(trans
, root
, p
, level
);
1168 b
= p
->nodes
[level
];
1169 slot
= p
->slots
[level
];
1170 } else if (ins_len
< 0) {
1171 int sret
= balance_level(trans
, root
, p
,
1175 b
= p
->nodes
[level
];
1177 btrfs_release_path(p
);
1180 slot
= p
->slots
[level
];
1181 BUG_ON(btrfs_header_nritems(b
) == 1);
1183 /* this is only true while dropping a snapshot */
1184 if (level
== lowest_level
)
1188 reada_for_search(root
, p
, level
, slot
,
1191 b
= read_node_slot(fs_info
, b
, slot
);
1192 if (!extent_buffer_uptodate(b
))
1195 p
->slots
[level
] = slot
;
1197 ins_len
> btrfs_leaf_free_space(b
)) {
1198 int sret
= split_leaf(trans
, root
, key
,
1199 p
, ins_len
, ret
== 0);
1211 * adjust the pointers going up the tree, starting at level
1212 * making sure the right key of each node is points to 'key'.
1213 * This is used after shifting pointers to the left, so it stops
1214 * fixing up pointers when a given leaf/node is not in slot 0 of the
1217 void btrfs_fixup_low_keys(struct btrfs_root
*root
, struct btrfs_path
*path
,
1218 struct btrfs_disk_key
*key
, int level
)
1221 struct extent_buffer
*t
;
1223 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1224 int tslot
= path
->slots
[i
];
1225 if (!path
->nodes
[i
])
1228 btrfs_set_node_key(t
, key
, tslot
);
1229 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1238 * This function isn't completely safe. It's the caller's responsibility
1239 * that the new key won't break the order
1241 int btrfs_set_item_key_safe(struct btrfs_root
*root
, struct btrfs_path
*path
,
1242 struct btrfs_key
*new_key
)
1244 struct btrfs_disk_key disk_key
;
1245 struct extent_buffer
*eb
;
1248 eb
= path
->nodes
[0];
1249 slot
= path
->slots
[0];
1251 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1252 if (btrfs_comp_keys(&disk_key
, new_key
) >= 0)
1255 if (slot
< btrfs_header_nritems(eb
) - 1) {
1256 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1257 if (btrfs_comp_keys(&disk_key
, new_key
) <= 0)
1261 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1262 btrfs_set_item_key(eb
, &disk_key
, slot
);
1263 btrfs_mark_buffer_dirty(eb
);
1265 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1270 * update an item key without the safety checks. This is meant to be called by
1273 void btrfs_set_item_key_unsafe(struct btrfs_root
*root
,
1274 struct btrfs_path
*path
,
1275 struct btrfs_key
*new_key
)
1277 struct btrfs_disk_key disk_key
;
1278 struct extent_buffer
*eb
;
1281 eb
= path
->nodes
[0];
1282 slot
= path
->slots
[0];
1284 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1285 btrfs_set_item_key(eb
, &disk_key
, slot
);
1286 btrfs_mark_buffer_dirty(eb
);
1288 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1292 * try to push data from one node into the next node left in the
1295 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1296 * error, and > 0 if there was no room in the left hand block.
1298 static int push_node_left(struct btrfs_trans_handle
*trans
,
1299 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1300 struct extent_buffer
*src
, int empty
)
1307 src_nritems
= btrfs_header_nritems(src
);
1308 dst_nritems
= btrfs_header_nritems(dst
);
1309 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - dst_nritems
;
1310 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1311 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1313 if (!empty
&& src_nritems
<= 8)
1316 if (push_items
<= 0) {
1321 push_items
= min(src_nritems
, push_items
);
1322 if (push_items
< src_nritems
) {
1323 /* leave at least 8 pointers in the node if
1324 * we aren't going to empty it
1326 if (src_nritems
- push_items
< 8) {
1327 if (push_items
<= 8)
1333 push_items
= min(src_nritems
- 8, push_items
);
1335 copy_extent_buffer(dst
, src
,
1336 btrfs_node_key_ptr_offset(dst_nritems
),
1337 btrfs_node_key_ptr_offset(0),
1338 push_items
* sizeof(struct btrfs_key_ptr
));
1340 if (push_items
< src_nritems
) {
1341 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1342 btrfs_node_key_ptr_offset(push_items
),
1343 (src_nritems
- push_items
) *
1344 sizeof(struct btrfs_key_ptr
));
1346 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1347 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1348 btrfs_mark_buffer_dirty(src
);
1349 btrfs_mark_buffer_dirty(dst
);
1355 * try to push data from one node into the next node right in the
1358 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1359 * error, and > 0 if there was no room in the right hand block.
1361 * this will only push up to 1/2 the contents of the left node over
1363 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1364 struct btrfs_root
*root
,
1365 struct extent_buffer
*dst
,
1366 struct extent_buffer
*src
)
1374 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1375 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1377 src_nritems
= btrfs_header_nritems(src
);
1378 dst_nritems
= btrfs_header_nritems(dst
);
1379 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - dst_nritems
;
1380 if (push_items
<= 0) {
1384 if (src_nritems
< 4) {
1388 max_push
= src_nritems
/ 2 + 1;
1389 /* don't try to empty the node */
1390 if (max_push
>= src_nritems
) {
1394 if (max_push
< push_items
)
1395 push_items
= max_push
;
1397 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1398 btrfs_node_key_ptr_offset(0),
1400 sizeof(struct btrfs_key_ptr
));
1402 copy_extent_buffer(dst
, src
,
1403 btrfs_node_key_ptr_offset(0),
1404 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1405 push_items
* sizeof(struct btrfs_key_ptr
));
1407 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1408 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1410 btrfs_mark_buffer_dirty(src
);
1411 btrfs_mark_buffer_dirty(dst
);
1417 * helper function to insert a new root level in the tree.
1418 * A new node is allocated, and a single item is inserted to
1419 * point to the existing root
1421 * returns zero on success or < 0 on failure.
1423 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1424 struct btrfs_root
*root
,
1425 struct btrfs_path
*path
, int level
)
1428 struct extent_buffer
*lower
;
1429 struct extent_buffer
*c
;
1430 struct extent_buffer
*old
;
1431 struct btrfs_disk_key lower_key
;
1433 BUG_ON(path
->nodes
[level
]);
1434 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1436 lower
= path
->nodes
[level
-1];
1438 btrfs_item_key(lower
, &lower_key
, 0);
1440 btrfs_node_key(lower
, &lower_key
, 0);
1442 c
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
1443 root
->root_key
.objectid
, &lower_key
,
1444 level
, root
->node
->start
, 0);
1449 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1450 btrfs_set_header_nritems(c
, 1);
1451 btrfs_set_header_level(c
, level
);
1452 btrfs_set_header_bytenr(c
, c
->start
);
1453 btrfs_set_header_generation(c
, trans
->transid
);
1454 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1455 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1457 write_extent_buffer(c
, root
->fs_info
->fsid
,
1458 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1460 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1461 btrfs_header_chunk_tree_uuid(c
),
1464 btrfs_set_node_key(c
, &lower_key
, 0);
1465 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1466 lower_gen
= btrfs_header_generation(lower
);
1467 WARN_ON(lower_gen
!= trans
->transid
);
1469 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1471 btrfs_mark_buffer_dirty(c
);
1476 /* the super has an extra ref to root->node */
1477 free_extent_buffer(old
);
1479 add_root_to_dirty_list(root
);
1480 extent_buffer_get(c
);
1481 path
->nodes
[level
] = c
;
1482 path
->slots
[level
] = 0;
1487 * worker function to insert a single pointer in a node.
1488 * the node should have enough room for the pointer already
1490 * slot and level indicate where you want the key to go, and
1491 * blocknr is the block the key points to.
1493 * returns zero on success and < 0 on any error
1495 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1496 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1497 *key
, u64 bytenr
, int slot
, int level
)
1499 struct extent_buffer
*lower
;
1502 BUG_ON(!path
->nodes
[level
]);
1503 lower
= path
->nodes
[level
];
1504 nritems
= btrfs_header_nritems(lower
);
1507 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
))
1509 if (slot
< nritems
) {
1510 /* shift the items */
1511 memmove_extent_buffer(lower
,
1512 btrfs_node_key_ptr_offset(slot
+ 1),
1513 btrfs_node_key_ptr_offset(slot
),
1514 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1516 btrfs_set_node_key(lower
, key
, slot
);
1517 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1518 WARN_ON(trans
->transid
== 0);
1519 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1520 btrfs_set_header_nritems(lower
, nritems
+ 1);
1521 btrfs_mark_buffer_dirty(lower
);
1526 * split the node at the specified level in path in two.
1527 * The path is corrected to point to the appropriate node after the split
1529 * Before splitting this tries to make some room in the node by pushing
1530 * left and right, if either one works, it returns right away.
1532 * returns 0 on success and < 0 on failure
1534 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1535 *root
, struct btrfs_path
*path
, int level
)
1537 struct extent_buffer
*c
;
1538 struct extent_buffer
*split
;
1539 struct btrfs_disk_key disk_key
;
1545 c
= path
->nodes
[level
];
1546 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1547 if (c
== root
->node
) {
1548 /* trying to split the root, lets make a new one */
1549 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1553 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1554 c
= path
->nodes
[level
];
1555 if (!ret
&& btrfs_header_nritems(c
) <
1556 BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - 3)
1562 c_nritems
= btrfs_header_nritems(c
);
1563 mid
= (c_nritems
+ 1) / 2;
1564 btrfs_node_key(c
, &disk_key
, mid
);
1566 split
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
1567 root
->root_key
.objectid
,
1568 &disk_key
, level
, c
->start
, 0);
1570 return PTR_ERR(split
);
1572 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
1573 btrfs_set_header_level(split
, btrfs_header_level(c
));
1574 btrfs_set_header_bytenr(split
, split
->start
);
1575 btrfs_set_header_generation(split
, trans
->transid
);
1576 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
1577 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1578 write_extent_buffer(split
, root
->fs_info
->fsid
,
1579 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1580 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1581 btrfs_header_chunk_tree_uuid(split
),
1585 copy_extent_buffer(split
, c
,
1586 btrfs_node_key_ptr_offset(0),
1587 btrfs_node_key_ptr_offset(mid
),
1588 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1589 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1590 btrfs_set_header_nritems(c
, mid
);
1593 btrfs_mark_buffer_dirty(c
);
1594 btrfs_mark_buffer_dirty(split
);
1596 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1597 path
->slots
[level
+ 1] + 1,
1602 if (path
->slots
[level
] >= mid
) {
1603 path
->slots
[level
] -= mid
;
1604 free_extent_buffer(c
);
1605 path
->nodes
[level
] = split
;
1606 path
->slots
[level
+ 1] += 1;
1608 free_extent_buffer(split
);
1614 * how many bytes are required to store the items in a leaf. start
1615 * and nr indicate which items in the leaf to check. This totals up the
1616 * space used both by the item structs and the item data
1618 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1621 int nritems
= btrfs_header_nritems(l
);
1622 int end
= min(nritems
, start
+ nr
) - 1;
1626 data_len
= btrfs_item_end_nr(l
, start
);
1627 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1628 data_len
+= sizeof(struct btrfs_item
) * nr
;
1629 WARN_ON(data_len
< 0);
1634 * The space between the end of the leaf items and
1635 * the start of the leaf data. IOW, how much room
1636 * the leaf has left for both items and data
1638 int btrfs_leaf_free_space(struct extent_buffer
*leaf
)
1640 int nritems
= btrfs_header_nritems(leaf
);
1644 BUG_ON(leaf
->fs_info
&& leaf
->fs_info
->nodesize
!= leaf
->len
);
1645 leaf_data_size
= __BTRFS_LEAF_DATA_SIZE(leaf
->len
);
1646 ret
= leaf_data_size
- leaf_space_used(leaf
, 0 ,nritems
);
1648 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1649 ret
, leaf_data_size
, leaf_space_used(leaf
, 0, nritems
),
1656 * push some data in the path leaf to the right, trying to free up at
1657 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1659 * returns 1 if the push failed because the other node didn't have enough
1660 * room, 0 if everything worked out and < 0 if there were major errors.
1662 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1663 *root
, struct btrfs_path
*path
, int data_size
,
1666 struct extent_buffer
*left
= path
->nodes
[0];
1667 struct extent_buffer
*right
;
1668 struct extent_buffer
*upper
;
1669 struct btrfs_disk_key disk_key
;
1670 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1676 struct btrfs_item
*item
;
1684 slot
= path
->slots
[1];
1685 if (!path
->nodes
[1]) {
1688 upper
= path
->nodes
[1];
1689 if (slot
>= btrfs_header_nritems(upper
) - 1)
1692 right
= read_node_slot(fs_info
, upper
, slot
+ 1);
1693 if (!extent_buffer_uptodate(right
)) {
1695 return PTR_ERR(right
);
1698 free_space
= btrfs_leaf_free_space(right
);
1699 if (free_space
< data_size
) {
1700 free_extent_buffer(right
);
1704 /* cow and double check */
1705 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1708 free_extent_buffer(right
);
1711 free_space
= btrfs_leaf_free_space(right
);
1712 if (free_space
< data_size
) {
1713 free_extent_buffer(right
);
1717 left_nritems
= btrfs_header_nritems(left
);
1718 if (left_nritems
== 0) {
1719 free_extent_buffer(right
);
1728 i
= left_nritems
- 1;
1730 item
= btrfs_item_nr(i
);
1732 if (path
->slots
[0] == i
)
1733 push_space
+= data_size
+ sizeof(*item
);
1735 this_item_size
= btrfs_item_size(left
, item
);
1736 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1739 push_space
+= this_item_size
+ sizeof(*item
);
1745 if (push_items
== 0) {
1746 free_extent_buffer(right
);
1750 if (!empty
&& push_items
== left_nritems
)
1753 /* push left to right */
1754 right_nritems
= btrfs_header_nritems(right
);
1756 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1757 push_space
-= leaf_data_end(fs_info
, left
);
1759 /* make room in the right data area */
1760 data_end
= leaf_data_end(fs_info
, right
);
1761 memmove_extent_buffer(right
,
1762 btrfs_leaf_data(right
) + data_end
- push_space
,
1763 btrfs_leaf_data(right
) + data_end
,
1764 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - data_end
);
1766 /* copy from the left data area */
1767 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1768 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - push_space
,
1769 btrfs_leaf_data(left
) + leaf_data_end(fs_info
, left
),
1772 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1773 btrfs_item_nr_offset(0),
1774 right_nritems
* sizeof(struct btrfs_item
));
1776 /* copy the items from left to right */
1777 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1778 btrfs_item_nr_offset(left_nritems
- push_items
),
1779 push_items
* sizeof(struct btrfs_item
));
1781 /* update the item pointers */
1782 right_nritems
+= push_items
;
1783 btrfs_set_header_nritems(right
, right_nritems
);
1784 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1785 for (i
= 0; i
< right_nritems
; i
++) {
1786 item
= btrfs_item_nr(i
);
1787 push_space
-= btrfs_item_size(right
, item
);
1788 btrfs_set_item_offset(right
, item
, push_space
);
1791 left_nritems
-= push_items
;
1792 btrfs_set_header_nritems(left
, left_nritems
);
1795 btrfs_mark_buffer_dirty(left
);
1796 btrfs_mark_buffer_dirty(right
);
1798 btrfs_item_key(right
, &disk_key
, 0);
1799 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1800 btrfs_mark_buffer_dirty(upper
);
1802 /* then fixup the leaf pointer in the path */
1803 if (path
->slots
[0] >= left_nritems
) {
1804 path
->slots
[0] -= left_nritems
;
1805 free_extent_buffer(path
->nodes
[0]);
1806 path
->nodes
[0] = right
;
1807 path
->slots
[1] += 1;
1809 free_extent_buffer(right
);
1814 * push some data in the path leaf to the left, trying to free up at
1815 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1817 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1818 *root
, struct btrfs_path
*path
, int data_size
,
1821 struct btrfs_disk_key disk_key
;
1822 struct extent_buffer
*right
= path
->nodes
[0];
1823 struct extent_buffer
*left
;
1824 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1830 struct btrfs_item
*item
;
1831 u32 old_left_nritems
;
1836 u32 old_left_item_size
;
1838 slot
= path
->slots
[1];
1841 if (!path
->nodes
[1])
1844 right_nritems
= btrfs_header_nritems(right
);
1845 if (right_nritems
== 0) {
1849 left
= read_node_slot(fs_info
, path
->nodes
[1], slot
- 1);
1850 free_space
= btrfs_leaf_free_space(left
);
1851 if (free_space
< data_size
) {
1852 free_extent_buffer(left
);
1856 /* cow and double check */
1857 ret
= btrfs_cow_block(trans
, root
, left
,
1858 path
->nodes
[1], slot
- 1, &left
);
1860 /* we hit -ENOSPC, but it isn't fatal here */
1861 free_extent_buffer(left
);
1865 free_space
= btrfs_leaf_free_space(left
);
1866 if (free_space
< data_size
) {
1867 free_extent_buffer(left
);
1874 nr
= right_nritems
- 1;
1876 for (i
= 0; i
< nr
; i
++) {
1877 item
= btrfs_item_nr(i
);
1879 if (path
->slots
[0] == i
)
1880 push_space
+= data_size
+ sizeof(*item
);
1882 this_item_size
= btrfs_item_size(right
, item
);
1883 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1887 push_space
+= this_item_size
+ sizeof(*item
);
1890 if (push_items
== 0) {
1891 free_extent_buffer(left
);
1894 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1897 /* push data from right to left */
1898 copy_extent_buffer(left
, right
,
1899 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1900 btrfs_item_nr_offset(0),
1901 push_items
* sizeof(struct btrfs_item
));
1903 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1904 btrfs_item_offset_nr(right
, push_items
-1);
1906 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1907 leaf_data_end(fs_info
, left
) - push_space
,
1908 btrfs_leaf_data(right
) +
1909 btrfs_item_offset_nr(right
, push_items
- 1),
1911 old_left_nritems
= btrfs_header_nritems(left
);
1912 BUG_ON(old_left_nritems
== 0);
1914 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1915 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1918 item
= btrfs_item_nr(i
);
1919 ioff
= btrfs_item_offset(left
, item
);
1920 btrfs_set_item_offset(left
, item
,
1921 ioff
- (BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1922 old_left_item_size
));
1924 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1926 /* fixup right node */
1927 if (push_items
> right_nritems
) {
1928 printk("push items %d nr %u\n", push_items
, right_nritems
);
1932 if (push_items
< right_nritems
) {
1933 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1934 leaf_data_end(fs_info
, right
);
1935 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1936 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1938 btrfs_leaf_data(right
) +
1939 leaf_data_end(fs_info
, right
),
1942 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1943 btrfs_item_nr_offset(push_items
),
1944 (btrfs_header_nritems(right
) - push_items
) *
1945 sizeof(struct btrfs_item
));
1947 right_nritems
-= push_items
;
1948 btrfs_set_header_nritems(right
, right_nritems
);
1949 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1950 for (i
= 0; i
< right_nritems
; i
++) {
1951 item
= btrfs_item_nr(i
);
1952 push_space
= push_space
- btrfs_item_size(right
, item
);
1953 btrfs_set_item_offset(right
, item
, push_space
);
1956 btrfs_mark_buffer_dirty(left
);
1958 btrfs_mark_buffer_dirty(right
);
1960 btrfs_item_key(right
, &disk_key
, 0);
1961 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1963 /* then fixup the leaf pointer in the path */
1964 if (path
->slots
[0] < push_items
) {
1965 path
->slots
[0] += old_left_nritems
;
1966 free_extent_buffer(path
->nodes
[0]);
1967 path
->nodes
[0] = left
;
1968 path
->slots
[1] -= 1;
1970 free_extent_buffer(left
);
1971 path
->slots
[0] -= push_items
;
1973 BUG_ON(path
->slots
[0] < 0);
1978 * split the path's leaf in two, making sure there is at least data_size
1979 * available for the resulting leaf level of the path.
1981 * returns 0 if all went well and < 0 on failure.
1983 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
1984 struct btrfs_root
*root
,
1985 struct btrfs_path
*path
,
1986 struct extent_buffer
*l
,
1987 struct extent_buffer
*right
,
1988 int slot
, int mid
, int nritems
)
1995 struct btrfs_disk_key disk_key
;
1997 nritems
= nritems
- mid
;
1998 btrfs_set_header_nritems(right
, nritems
);
1999 data_copy_size
= btrfs_item_end_nr(l
, mid
) -
2000 leaf_data_end(root
->fs_info
, l
);
2002 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2003 btrfs_item_nr_offset(mid
),
2004 nritems
* sizeof(struct btrfs_item
));
2006 copy_extent_buffer(right
, l
,
2007 btrfs_leaf_data(right
) +
2008 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2009 data_copy_size
, btrfs_leaf_data(l
) +
2010 leaf_data_end(root
->fs_info
, l
), data_copy_size
);
2012 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2013 btrfs_item_end_nr(l
, mid
);
2015 for (i
= 0; i
< nritems
; i
++) {
2016 struct btrfs_item
*item
= btrfs_item_nr(i
);
2017 u32 ioff
= btrfs_item_offset(right
, item
);
2018 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2021 btrfs_set_header_nritems(l
, mid
);
2023 btrfs_item_key(right
, &disk_key
, 0);
2024 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2025 path
->slots
[1] + 1, 1);
2029 btrfs_mark_buffer_dirty(right
);
2030 btrfs_mark_buffer_dirty(l
);
2031 BUG_ON(path
->slots
[0] != slot
);
2034 free_extent_buffer(path
->nodes
[0]);
2035 path
->nodes
[0] = right
;
2036 path
->slots
[0] -= mid
;
2037 path
->slots
[1] += 1;
2039 free_extent_buffer(right
);
2042 BUG_ON(path
->slots
[0] < 0);
2048 * split the path's leaf in two, making sure there is at least data_size
2049 * available for the resulting leaf level of the path.
2051 * returns 0 if all went well and < 0 on failure.
2053 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2054 struct btrfs_root
*root
,
2055 struct btrfs_key
*ins_key
,
2056 struct btrfs_path
*path
, int data_size
,
2059 struct btrfs_disk_key disk_key
;
2060 struct extent_buffer
*l
;
2064 struct extent_buffer
*right
;
2068 int num_doubles
= 0;
2071 slot
= path
->slots
[0];
2072 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2073 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
->fs_info
))
2076 /* first try to make some room by pushing left and right */
2077 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2078 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2082 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2088 /* did the pushes work? */
2089 if (btrfs_leaf_free_space(l
) >= data_size
)
2093 if (!path
->nodes
[1]) {
2094 ret
= insert_new_root(trans
, root
, path
, 1);
2101 slot
= path
->slots
[0];
2102 nritems
= btrfs_header_nritems(l
);
2103 mid
= (nritems
+ 1) / 2;
2107 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2108 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2109 if (slot
>= nritems
) {
2113 if (mid
!= nritems
&&
2114 leaf_space_used(l
, mid
, nritems
- mid
) +
2116 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2122 if (leaf_space_used(l
, 0, mid
) + data_size
>
2123 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2124 if (!extend
&& data_size
&& slot
== 0) {
2126 } else if ((extend
|| !data_size
) && slot
== 0) {
2130 if (mid
!= nritems
&&
2131 leaf_space_used(l
, mid
, nritems
- mid
) +
2133 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2141 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2143 btrfs_item_key(l
, &disk_key
, mid
);
2145 right
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
2146 root
->root_key
.objectid
,
2147 &disk_key
, 0, l
->start
, 0);
2148 if (IS_ERR(right
)) {
2150 return PTR_ERR(right
);
2153 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2154 btrfs_set_header_bytenr(right
, right
->start
);
2155 btrfs_set_header_generation(right
, trans
->transid
);
2156 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2157 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2158 btrfs_set_header_level(right
, 0);
2159 write_extent_buffer(right
, root
->fs_info
->fsid
,
2160 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
2162 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2163 btrfs_header_chunk_tree_uuid(right
),
2168 btrfs_set_header_nritems(right
, 0);
2169 wret
= insert_ptr(trans
, root
, path
,
2170 &disk_key
, right
->start
,
2171 path
->slots
[1] + 1, 1);
2175 free_extent_buffer(path
->nodes
[0]);
2176 path
->nodes
[0] = right
;
2178 path
->slots
[1] += 1;
2180 btrfs_set_header_nritems(right
, 0);
2181 wret
= insert_ptr(trans
, root
, path
,
2187 free_extent_buffer(path
->nodes
[0]);
2188 path
->nodes
[0] = right
;
2190 if (path
->slots
[1] == 0) {
2191 btrfs_fixup_low_keys(root
, path
,
2195 btrfs_mark_buffer_dirty(right
);
2199 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2203 BUG_ON(num_doubles
!= 0);
2212 * This function splits a single item into two items,
2213 * giving 'new_key' to the new item and splitting the
2214 * old one at split_offset (from the start of the item).
2216 * The path may be released by this operation. After
2217 * the split, the path is pointing to the old item. The
2218 * new item is going to be in the same node as the old one.
2220 * Note, the item being split must be smaller enough to live alone on
2221 * a tree block with room for one extra struct btrfs_item
2223 * This allows us to split the item in place, keeping a lock on the
2224 * leaf the entire time.
2226 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2227 struct btrfs_root
*root
,
2228 struct btrfs_path
*path
,
2229 struct btrfs_key
*new_key
,
2230 unsigned long split_offset
)
2233 struct extent_buffer
*leaf
;
2234 struct btrfs_key orig_key
;
2235 struct btrfs_item
*item
;
2236 struct btrfs_item
*new_item
;
2241 struct btrfs_disk_key disk_key
;
2244 leaf
= path
->nodes
[0];
2245 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
2246 if (btrfs_leaf_free_space(leaf
) >=
2247 sizeof(struct btrfs_item
))
2250 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2251 btrfs_release_path(path
);
2253 path
->search_for_split
= 1;
2255 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
2256 path
->search_for_split
= 0;
2258 /* if our item isn't there or got smaller, return now */
2259 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
2264 ret
= split_leaf(trans
, root
, &orig_key
, path
, 0, 0);
2267 BUG_ON(btrfs_leaf_free_space(leaf
) < sizeof(struct btrfs_item
));
2268 leaf
= path
->nodes
[0];
2271 item
= btrfs_item_nr(path
->slots
[0]);
2272 orig_offset
= btrfs_item_offset(leaf
, item
);
2273 item_size
= btrfs_item_size(leaf
, item
);
2276 buf
= kmalloc(item_size
, GFP_NOFS
);
2278 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
2279 path
->slots
[0]), item_size
);
2280 slot
= path
->slots
[0] + 1;
2281 leaf
= path
->nodes
[0];
2283 nritems
= btrfs_header_nritems(leaf
);
2285 if (slot
< nritems
) {
2286 /* shift the items */
2287 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2288 btrfs_item_nr_offset(slot
),
2289 (nritems
- slot
) * sizeof(struct btrfs_item
));
2293 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
2294 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2296 new_item
= btrfs_item_nr(slot
);
2298 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
2299 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
2301 btrfs_set_item_offset(leaf
, item
,
2302 orig_offset
+ item_size
- split_offset
);
2303 btrfs_set_item_size(leaf
, item
, split_offset
);
2305 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2307 /* write the data for the start of the original item */
2308 write_extent_buffer(leaf
, buf
,
2309 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2312 /* write the data for the new item */
2313 write_extent_buffer(leaf
, buf
+ split_offset
,
2314 btrfs_item_ptr_offset(leaf
, slot
),
2315 item_size
- split_offset
);
2316 btrfs_mark_buffer_dirty(leaf
);
2319 if (btrfs_leaf_free_space(leaf
) < 0) {
2320 btrfs_print_leaf(leaf
);
2327 int btrfs_truncate_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2328 u32 new_size
, int from_end
)
2332 struct extent_buffer
*leaf
;
2333 struct btrfs_item
*item
;
2335 unsigned int data_end
;
2336 unsigned int old_data_start
;
2337 unsigned int old_size
;
2338 unsigned int size_diff
;
2341 leaf
= path
->nodes
[0];
2342 slot
= path
->slots
[0];
2344 old_size
= btrfs_item_size_nr(leaf
, slot
);
2345 if (old_size
== new_size
)
2348 nritems
= btrfs_header_nritems(leaf
);
2349 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2351 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2353 size_diff
= old_size
- new_size
;
2356 BUG_ON(slot
>= nritems
);
2359 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2361 /* first correct the data pointers */
2362 for (i
= slot
; i
< nritems
; i
++) {
2364 item
= btrfs_item_nr(i
);
2365 ioff
= btrfs_item_offset(leaf
, item
);
2366 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2369 /* shift the data */
2371 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2372 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2373 data_end
, old_data_start
+ new_size
- data_end
);
2375 struct btrfs_disk_key disk_key
;
2378 btrfs_item_key(leaf
, &disk_key
, slot
);
2380 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2382 struct btrfs_file_extent_item
*fi
;
2384 fi
= btrfs_item_ptr(leaf
, slot
,
2385 struct btrfs_file_extent_item
);
2386 fi
= (struct btrfs_file_extent_item
*)(
2387 (unsigned long)fi
- size_diff
);
2389 if (btrfs_file_extent_type(leaf
, fi
) ==
2390 BTRFS_FILE_EXTENT_INLINE
) {
2391 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2392 memmove_extent_buffer(leaf
, ptr
,
2394 offsetof(struct btrfs_file_extent_item
,
2399 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2400 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2401 data_end
, old_data_start
- data_end
);
2403 offset
= btrfs_disk_key_offset(&disk_key
);
2404 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2405 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2407 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2410 item
= btrfs_item_nr(slot
);
2411 btrfs_set_item_size(leaf
, item
, new_size
);
2412 btrfs_mark_buffer_dirty(leaf
);
2415 if (btrfs_leaf_free_space(leaf
) < 0) {
2416 btrfs_print_leaf(leaf
);
2422 int btrfs_extend_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2427 struct extent_buffer
*leaf
;
2428 struct btrfs_item
*item
;
2430 unsigned int data_end
;
2431 unsigned int old_data
;
2432 unsigned int old_size
;
2435 leaf
= path
->nodes
[0];
2437 nritems
= btrfs_header_nritems(leaf
);
2438 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2440 if (btrfs_leaf_free_space(leaf
) < data_size
) {
2441 btrfs_print_leaf(leaf
);
2444 slot
= path
->slots
[0];
2445 old_data
= btrfs_item_end_nr(leaf
, slot
);
2448 if (slot
>= nritems
) {
2449 btrfs_print_leaf(leaf
);
2450 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2455 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2457 /* first correct the data pointers */
2458 for (i
= slot
; i
< nritems
; i
++) {
2460 item
= btrfs_item_nr(i
);
2461 ioff
= btrfs_item_offset(leaf
, item
);
2462 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2465 /* shift the data */
2466 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2467 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2468 data_end
, old_data
- data_end
);
2470 data_end
= old_data
;
2471 old_size
= btrfs_item_size_nr(leaf
, slot
);
2472 item
= btrfs_item_nr(slot
);
2473 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2474 btrfs_mark_buffer_dirty(leaf
);
2477 if (btrfs_leaf_free_space(leaf
) < 0) {
2478 btrfs_print_leaf(leaf
);
2485 * Given a key and some data, insert an item into the tree.
2486 * This does all the path init required, making room in the tree if needed.
2488 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2489 struct btrfs_root
*root
,
2490 struct btrfs_path
*path
,
2491 struct btrfs_key
*cpu_key
, u32
*data_size
,
2494 struct extent_buffer
*leaf
;
2495 struct btrfs_item
*item
;
2502 unsigned int data_end
;
2503 struct btrfs_disk_key disk_key
;
2505 for (i
= 0; i
< nr
; i
++) {
2506 total_data
+= data_size
[i
];
2509 /* create a root if there isn't one */
2513 total_size
= total_data
+ nr
* sizeof(struct btrfs_item
);
2514 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2521 leaf
= path
->nodes
[0];
2523 nritems
= btrfs_header_nritems(leaf
);
2524 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2526 if (btrfs_leaf_free_space(leaf
) < total_size
) {
2527 btrfs_print_leaf(leaf
);
2528 printk("not enough freespace need %u have %d\n",
2529 total_size
, btrfs_leaf_free_space(leaf
));
2533 slot
= path
->slots
[0];
2536 if (slot
< nritems
) {
2537 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2539 if (old_data
< data_end
) {
2540 btrfs_print_leaf(leaf
);
2541 printk("slot %d old_data %d data_end %d\n",
2542 slot
, old_data
, data_end
);
2546 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2548 /* first correct the data pointers */
2549 for (i
= slot
; i
< nritems
; i
++) {
2552 item
= btrfs_item_nr(i
);
2553 ioff
= btrfs_item_offset(leaf
, item
);
2554 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2557 /* shift the items */
2558 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2559 btrfs_item_nr_offset(slot
),
2560 (nritems
- slot
) * sizeof(struct btrfs_item
));
2562 /* shift the data */
2563 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2564 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2565 data_end
, old_data
- data_end
);
2566 data_end
= old_data
;
2569 /* setup the item for the new data */
2570 for (i
= 0; i
< nr
; i
++) {
2571 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2572 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2573 item
= btrfs_item_nr(slot
+ i
);
2574 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2575 data_end
-= data_size
[i
];
2576 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2578 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2579 btrfs_mark_buffer_dirty(leaf
);
2583 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2584 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2587 if (btrfs_leaf_free_space(leaf
) < 0) {
2588 btrfs_print_leaf(leaf
);
2597 * Given a key and some data, insert an item into the tree.
2598 * This does all the path init required, making room in the tree if needed.
2600 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2601 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2605 struct btrfs_path
*path
;
2606 struct extent_buffer
*leaf
;
2609 path
= btrfs_alloc_path();
2613 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2615 leaf
= path
->nodes
[0];
2616 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2617 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2618 btrfs_mark_buffer_dirty(leaf
);
2620 btrfs_free_path(path
);
2625 * delete the pointer from a given node.
2627 * If the delete empties a node, the node is removed from the tree,
2628 * continuing all the way the root if required. The root is converted into
2629 * a leaf if all the nodes are emptied.
2631 int btrfs_del_ptr(struct btrfs_root
*root
, struct btrfs_path
*path
,
2632 int level
, int slot
)
2634 struct extent_buffer
*parent
= path
->nodes
[level
];
2638 nritems
= btrfs_header_nritems(parent
);
2639 if (slot
< nritems
- 1) {
2640 /* shift the items */
2641 memmove_extent_buffer(parent
,
2642 btrfs_node_key_ptr_offset(slot
),
2643 btrfs_node_key_ptr_offset(slot
+ 1),
2644 sizeof(struct btrfs_key_ptr
) *
2645 (nritems
- slot
- 1));
2648 btrfs_set_header_nritems(parent
, nritems
);
2649 if (nritems
== 0 && parent
== root
->node
) {
2650 BUG_ON(btrfs_header_level(root
->node
) != 1);
2651 /* just turn the root into a leaf and break */
2652 btrfs_set_header_level(root
->node
, 0);
2653 } else if (slot
== 0) {
2654 struct btrfs_disk_key disk_key
;
2656 btrfs_node_key(parent
, &disk_key
, 0);
2657 btrfs_fixup_low_keys(root
, path
, &disk_key
, level
+ 1);
2659 btrfs_mark_buffer_dirty(parent
);
2664 * a helper function to delete the leaf pointed to by path->slots[1] and
2667 * This deletes the pointer in path->nodes[1] and frees the leaf
2668 * block extent. zero is returned if it all worked out, < 0 otherwise.
2670 * The path must have already been setup for deleting the leaf, including
2671 * all the proper balancing. path->nodes[1] must be locked.
2673 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
2674 struct btrfs_root
*root
,
2675 struct btrfs_path
*path
,
2676 struct extent_buffer
*leaf
)
2680 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
2681 ret
= btrfs_del_ptr(root
, path
, 1, path
->slots
[1]);
2685 ret
= btrfs_free_extent(trans
, root
, leaf
->start
, leaf
->len
,
2686 0, root
->root_key
.objectid
, 0, 0);
2691 * delete the item at the leaf level in path. If that empties
2692 * the leaf, remove it from the tree
2694 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2695 struct btrfs_path
*path
, int slot
, int nr
)
2697 struct extent_buffer
*leaf
;
2698 struct btrfs_item
*item
;
2706 leaf
= path
->nodes
[0];
2707 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2709 for (i
= 0; i
< nr
; i
++)
2710 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2712 nritems
= btrfs_header_nritems(leaf
);
2714 if (slot
+ nr
!= nritems
) {
2715 int data_end
= leaf_data_end(root
->fs_info
, leaf
);
2717 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2719 btrfs_leaf_data(leaf
) + data_end
,
2720 last_off
- data_end
);
2722 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2725 item
= btrfs_item_nr(i
);
2726 ioff
= btrfs_item_offset(leaf
, item
);
2727 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2730 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2731 btrfs_item_nr_offset(slot
+ nr
),
2732 sizeof(struct btrfs_item
) *
2733 (nritems
- slot
- nr
));
2735 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2738 /* delete the leaf if we've emptied it */
2740 if (leaf
== root
->node
) {
2741 btrfs_set_header_level(leaf
, 0);
2743 clean_tree_block(leaf
);
2744 wret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2750 int used
= leaf_space_used(leaf
, 0, nritems
);
2752 struct btrfs_disk_key disk_key
;
2754 btrfs_item_key(leaf
, &disk_key
, 0);
2755 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2758 /* delete the leaf if it is mostly empty */
2759 if (used
< BTRFS_LEAF_DATA_SIZE(root
->fs_info
) / 4) {
2760 /* push_leaf_left fixes the path.
2761 * make sure the path still points to our leaf
2762 * for possible call to del_ptr below
2764 slot
= path
->slots
[1];
2765 extent_buffer_get(leaf
);
2767 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2768 if (wret
< 0 && wret
!= -ENOSPC
)
2771 if (path
->nodes
[0] == leaf
&&
2772 btrfs_header_nritems(leaf
)) {
2773 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2774 if (wret
< 0 && wret
!= -ENOSPC
)
2778 if (btrfs_header_nritems(leaf
) == 0) {
2779 clean_tree_block(leaf
);
2780 path
->slots
[1] = slot
;
2781 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2783 free_extent_buffer(leaf
);
2786 btrfs_mark_buffer_dirty(leaf
);
2787 free_extent_buffer(leaf
);
2790 btrfs_mark_buffer_dirty(leaf
);
2797 * walk up the tree as far as required to find the previous leaf.
2798 * returns 0 if it found something or 1 if there are no lesser leaves.
2799 * returns < 0 on io errors.
2801 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2805 struct extent_buffer
*c
;
2806 struct extent_buffer
*next
= NULL
;
2807 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2809 while(level
< BTRFS_MAX_LEVEL
) {
2810 if (!path
->nodes
[level
])
2813 slot
= path
->slots
[level
];
2814 c
= path
->nodes
[level
];
2817 if (level
== BTRFS_MAX_LEVEL
)
2823 next
= read_node_slot(fs_info
, c
, slot
);
2824 if (!extent_buffer_uptodate(next
)) {
2826 return PTR_ERR(next
);
2831 path
->slots
[level
] = slot
;
2834 c
= path
->nodes
[level
];
2835 free_extent_buffer(c
);
2836 slot
= btrfs_header_nritems(next
);
2839 path
->nodes
[level
] = next
;
2840 path
->slots
[level
] = slot
;
2843 next
= read_node_slot(fs_info
, next
, slot
);
2844 if (!extent_buffer_uptodate(next
)) {
2846 return PTR_ERR(next
);
2854 * walk up the tree as far as required to find the next leaf.
2855 * returns 0 if it found something or 1 if there are no greater leaves.
2856 * returns < 0 on io errors.
2858 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2862 struct extent_buffer
*c
;
2863 struct extent_buffer
*next
= NULL
;
2864 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2866 while(level
< BTRFS_MAX_LEVEL
) {
2867 if (!path
->nodes
[level
])
2870 slot
= path
->slots
[level
] + 1;
2871 c
= path
->nodes
[level
];
2872 if (slot
>= btrfs_header_nritems(c
)) {
2874 if (level
== BTRFS_MAX_LEVEL
)
2880 reada_for_search(root
, path
, level
, slot
, 0);
2882 next
= read_node_slot(fs_info
, c
, slot
);
2883 if (!extent_buffer_uptodate(next
))
2887 path
->slots
[level
] = slot
;
2890 c
= path
->nodes
[level
];
2891 free_extent_buffer(c
);
2892 path
->nodes
[level
] = next
;
2893 path
->slots
[level
] = 0;
2897 reada_for_search(root
, path
, level
, 0, 0);
2898 next
= read_node_slot(fs_info
, next
, 0);
2899 if (!extent_buffer_uptodate(next
))
2905 int btrfs_previous_item(struct btrfs_root
*root
,
2906 struct btrfs_path
*path
, u64 min_objectid
,
2909 struct btrfs_key found_key
;
2910 struct extent_buffer
*leaf
;
2915 if (path
->slots
[0] == 0) {
2916 ret
= btrfs_prev_leaf(root
, path
);
2922 leaf
= path
->nodes
[0];
2923 nritems
= btrfs_header_nritems(leaf
);
2926 if (path
->slots
[0] == nritems
)
2929 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2930 if (found_key
.objectid
< min_objectid
)
2932 if (found_key
.type
== type
)
2934 if (found_key
.objectid
== min_objectid
&&
2935 found_key
.type
< type
)
2942 * search in extent tree to find a previous Metadata/Data extent item with
2945 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2947 int btrfs_previous_extent_item(struct btrfs_root
*root
,
2948 struct btrfs_path
*path
, u64 min_objectid
)
2950 struct btrfs_key found_key
;
2951 struct extent_buffer
*leaf
;
2956 if (path
->slots
[0] == 0) {
2957 ret
= btrfs_prev_leaf(root
, path
);
2963 leaf
= path
->nodes
[0];
2964 nritems
= btrfs_header_nritems(leaf
);
2967 if (path
->slots
[0] == nritems
)
2970 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2971 if (found_key
.objectid
< min_objectid
)
2973 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
2974 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)
2976 if (found_key
.objectid
== min_objectid
&&
2977 found_key
.type
< BTRFS_EXTENT_ITEM_KEY
)
2984 * Search in extent tree to found next meta/data extent
2985 * Caller needs to check for no-hole or skinny metadata features.
2987 int btrfs_next_extent_item(struct btrfs_root
*root
,
2988 struct btrfs_path
*path
, u64 max_objectid
)
2990 struct btrfs_key found_key
;
2994 ret
= btrfs_next_item(root
, path
);
2997 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2999 if (found_key
.objectid
> max_objectid
)
3001 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
3002 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)