2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include "transaction.h"
21 #include "print-tree.h"
23 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
24 *root
, struct btrfs_path
*path
, int level
);
25 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
26 *root
, struct btrfs_key
*ins_key
,
27 struct btrfs_path
*path
, int data_size
, int extend
);
28 static int push_node_left(struct btrfs_trans_handle
*trans
,
29 struct btrfs_root
*root
, struct extent_buffer
*dst
,
30 struct extent_buffer
*src
, int empty
);
31 static int balance_node_right(struct btrfs_trans_handle
*trans
,
32 struct btrfs_root
*root
,
33 struct extent_buffer
*dst_buf
,
34 struct extent_buffer
*src_buf
);
35 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
36 struct btrfs_path
*path
, int level
, int slot
);
38 inline void btrfs_init_path(struct btrfs_path
*p
)
40 memset(p
, 0, sizeof(*p
));
43 struct btrfs_path
*btrfs_alloc_path(void)
45 struct btrfs_path
*path
;
46 path
= kmalloc(sizeof(struct btrfs_path
), GFP_NOFS
);
48 btrfs_init_path(path
);
54 void btrfs_free_path(struct btrfs_path
*p
)
56 btrfs_release_path(NULL
, p
);
60 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
63 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
66 free_extent_buffer(p
->nodes
[i
]);
68 memset(p
, 0, sizeof(*p
));
71 static void add_root_to_dirty_list(struct btrfs_root
*root
)
73 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
74 list_add(&root
->dirty_list
,
75 &root
->fs_info
->dirty_cowonly_roots
);
79 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
80 struct btrfs_root
*root
,
81 struct extent_buffer
*buf
,
82 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
84 struct extent_buffer
*cow
;
87 struct btrfs_root
*new_root
;
89 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
93 memcpy(new_root
, root
, sizeof(*new_root
));
94 new_root
->root_key
.objectid
= new_root_objectid
;
96 WARN_ON(root
->ref_cows
&& trans
->transid
!=
97 root
->fs_info
->running_transaction
->transid
);
98 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
100 level
= btrfs_header_level(buf
);
101 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
, 0,
102 new_root_objectid
, trans
->transid
,
103 level
, buf
->start
, 0);
109 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
110 btrfs_set_header_bytenr(cow
, cow
->start
);
111 btrfs_set_header_generation(cow
, trans
->transid
);
112 btrfs_set_header_owner(cow
, new_root_objectid
);
113 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
);
115 write_extent_buffer(cow
, root
->fs_info
->fsid
,
116 (unsigned long)btrfs_header_fsid(cow
),
119 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
120 ret
= btrfs_inc_ref(trans
, new_root
, buf
, cow
, NULL
);
126 btrfs_mark_buffer_dirty(cow
);
131 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
132 struct btrfs_root
*root
,
133 struct extent_buffer
*buf
,
134 struct extent_buffer
*parent
, int parent_slot
,
135 struct extent_buffer
**cow_ret
,
136 u64 search_start
, u64 empty_size
)
139 struct extent_buffer
*cow
;
142 int different_trans
= 0;
145 WARN_ON(root
->ref_cows
&& trans
->transid
!=
146 root
->fs_info
->running_transaction
->transid
);
147 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
150 parent_start
= parent
->start
;
154 level
= btrfs_header_level(buf
);
155 nritems
= btrfs_header_nritems(buf
);
156 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, parent_start
,
157 root
->root_key
.objectid
, trans
->transid
,
158 level
, search_start
, empty_size
);
162 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
163 btrfs_set_header_bytenr(cow
, cow
->start
);
164 btrfs_set_header_generation(cow
, trans
->transid
);
165 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
166 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
);
168 write_extent_buffer(cow
, root
->fs_info
->fsid
,
169 (unsigned long)btrfs_header_fsid(cow
),
172 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
173 if (btrfs_header_generation(buf
) != trans
->transid
) {
175 ret
= btrfs_inc_ref(trans
, root
, buf
, cow
, NULL
);
179 ret
= btrfs_update_ref(trans
, root
, buf
, cow
, 0, nritems
);
182 clean_tree_block(trans
, root
, buf
);
185 if (buf
== root
->node
) {
187 extent_buffer_get(cow
);
188 if (buf
!= root
->commit_root
) {
189 btrfs_free_extent(trans
, root
, buf
->start
,
190 buf
->len
, buf
->start
,
191 root
->root_key
.objectid
,
192 btrfs_header_generation(buf
),
195 free_extent_buffer(buf
);
196 add_root_to_dirty_list(root
);
198 btrfs_set_node_blockptr(parent
, parent_slot
,
200 WARN_ON(trans
->transid
== 0);
201 btrfs_set_node_ptr_generation(parent
, parent_slot
,
203 btrfs_mark_buffer_dirty(parent
);
204 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
205 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
206 parent_start
, btrfs_header_owner(parent
),
207 btrfs_header_generation(parent
), level
, 1);
209 free_extent_buffer(buf
);
210 btrfs_mark_buffer_dirty(cow
);
215 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
216 struct btrfs_root
*root
, struct extent_buffer
*buf
,
217 struct extent_buffer
*parent
, int parent_slot
,
218 struct extent_buffer
**cow_ret
)
223 if (trans->transaction != root->fs_info->running_transaction) {
224 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
225 root->fs_info->running_transaction->transid);
229 if (trans
->transid
!= root
->fs_info
->generation
) {
230 printk(KERN_CRIT
"trans %llu running %llu\n",
231 (unsigned long long)trans
->transid
,
232 (unsigned long long)root
->fs_info
->generation
);
235 if (btrfs_header_generation(buf
) == trans
->transid
&&
236 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
241 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
242 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
243 parent_slot
, cow_ret
, search_start
, 0);
248 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
250 if (blocknr < other && other - (blocknr + blocksize) < 32768)
252 if (blocknr > other && blocknr - (other + blocksize) < 32768)
259 * compare two keys in a memcmp fashion
261 int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
265 btrfs_disk_key_to_cpu(&k1
, disk
);
267 if (k1
.objectid
> k2
->objectid
)
269 if (k1
.objectid
< k2
->objectid
)
271 if (k1
.type
> k2
->type
)
273 if (k1
.type
< k2
->type
)
275 if (k1
.offset
> k2
->offset
)
277 if (k1
.offset
< k2
->offset
)
284 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
285 struct btrfs_root
*root
, struct extent_buffer
*parent
,
286 int start_slot
, int cache_only
, u64
*last_ret
,
287 struct btrfs_key
*progress
)
289 struct extent_buffer
*cur
;
290 struct extent_buffer
*tmp
;
293 u64 search_start
= *last_ret
;
303 int progress_passed
= 0;
304 struct btrfs_disk_key disk_key
;
306 parent_level
= btrfs_header_level(parent
);
307 if (cache_only
&& parent_level
!= 1)
310 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
311 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
312 root
->fs_info
->running_transaction
->transid
);
315 if (trans
->transid
!= root
->fs_info
->generation
) {
316 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
317 root
->fs_info
->generation
);
321 parent_nritems
= btrfs_header_nritems(parent
);
322 blocksize
= btrfs_level_size(root
, parent_level
- 1);
323 end_slot
= parent_nritems
;
325 if (parent_nritems
== 1)
328 for (i
= start_slot
; i
< end_slot
; i
++) {
331 if (!parent
->map_token
) {
332 map_extent_buffer(parent
,
333 btrfs_node_key_ptr_offset(i
),
334 sizeof(struct btrfs_key_ptr
),
335 &parent
->map_token
, &parent
->kaddr
,
336 &parent
->map_start
, &parent
->map_len
,
339 btrfs_node_key(parent
, &disk_key
, i
);
340 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
344 blocknr
= btrfs_node_blockptr(parent
, i
);
345 gen
= btrfs_node_ptr_generation(parent
, i
);
347 last_block
= blocknr
;
350 other
= btrfs_node_blockptr(parent
, i
- 1);
351 close
= close_blocks(blocknr
, other
, blocksize
);
353 if (close
&& i
< end_slot
- 2) {
354 other
= btrfs_node_blockptr(parent
, i
+ 1);
355 close
= close_blocks(blocknr
, other
, blocksize
);
358 last_block
= blocknr
;
361 if (parent
->map_token
) {
362 unmap_extent_buffer(parent
, parent
->map_token
,
364 parent
->map_token
= NULL
;
367 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
369 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
372 if (!cur
|| !uptodate
) {
374 free_extent_buffer(cur
);
378 cur
= read_tree_block(root
, blocknr
,
380 } else if (!uptodate
) {
381 btrfs_read_buffer(cur
, gen
);
384 if (search_start
== 0)
385 search_start
= last_block
;
387 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
390 (end_slot
- i
) * blocksize
));
392 free_extent_buffer(cur
);
395 search_start
= tmp
->start
;
396 last_block
= tmp
->start
;
397 *last_ret
= search_start
;
398 if (parent_level
== 1)
399 btrfs_clear_buffer_defrag(tmp
);
400 free_extent_buffer(tmp
);
402 if (parent
->map_token
) {
403 unmap_extent_buffer(parent
, parent
->map_token
,
405 parent
->map_token
= NULL
;
412 * The leaf data grows from end-to-front in the node.
413 * this returns the address of the start of the last item,
414 * which is the stop of the leaf data stack
416 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
417 struct extent_buffer
*leaf
)
419 u32 nr
= btrfs_header_nritems(leaf
);
421 return BTRFS_LEAF_DATA_SIZE(root
);
422 return btrfs_item_offset_nr(leaf
, nr
- 1);
425 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
428 struct extent_buffer
*parent
= NULL
;
429 struct extent_buffer
*node
= path
->nodes
[level
];
430 struct btrfs_disk_key parent_key
;
431 struct btrfs_disk_key node_key
;
434 struct btrfs_key cpukey
;
435 u32 nritems
= btrfs_header_nritems(node
);
437 if (path
->nodes
[level
+ 1])
438 parent
= path
->nodes
[level
+ 1];
440 slot
= path
->slots
[level
];
441 BUG_ON(nritems
== 0);
443 parent_slot
= path
->slots
[level
+ 1];
444 btrfs_node_key(parent
, &parent_key
, parent_slot
);
445 btrfs_node_key(node
, &node_key
, 0);
446 BUG_ON(memcmp(&parent_key
, &node_key
,
447 sizeof(struct btrfs_disk_key
)));
448 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
449 btrfs_header_bytenr(node
));
451 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
453 btrfs_node_key_to_cpu(node
, &cpukey
, slot
- 1);
454 btrfs_node_key(node
, &node_key
, slot
);
455 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) <= 0);
457 if (slot
< nritems
- 1) {
458 btrfs_node_key_to_cpu(node
, &cpukey
, slot
+ 1);
459 btrfs_node_key(node
, &node_key
, slot
);
460 BUG_ON(btrfs_comp_keys(&node_key
, &cpukey
) >= 0);
465 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
468 struct extent_buffer
*leaf
= path
->nodes
[level
];
469 struct extent_buffer
*parent
= NULL
;
471 struct btrfs_key cpukey
;
472 struct btrfs_disk_key parent_key
;
473 struct btrfs_disk_key leaf_key
;
474 int slot
= path
->slots
[0];
476 u32 nritems
= btrfs_header_nritems(leaf
);
478 if (path
->nodes
[level
+ 1])
479 parent
= path
->nodes
[level
+ 1];
485 parent_slot
= path
->slots
[level
+ 1];
486 btrfs_node_key(parent
, &parent_key
, parent_slot
);
487 btrfs_item_key(leaf
, &leaf_key
, 0);
489 BUG_ON(memcmp(&parent_key
, &leaf_key
,
490 sizeof(struct btrfs_disk_key
)));
491 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
492 btrfs_header_bytenr(leaf
));
495 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
496 btrfs_item_key_to_cpu(leaf
, &cpukey
, i
+ 1);
497 btrfs_item_key(leaf
, &leaf_key
, i
);
498 if (comp_keys(&leaf_key
, &cpukey
) >= 0) {
499 btrfs_print_leaf(root
, leaf
);
500 printk("slot %d offset bad key\n", i
);
503 if (btrfs_item_offset_nr(leaf
, i
) !=
504 btrfs_item_end_nr(leaf
, i
+ 1)) {
505 btrfs_print_leaf(root
, leaf
);
506 printk("slot %d offset bad\n", i
);
510 if (btrfs_item_offset_nr(leaf
, i
) +
511 btrfs_item_size_nr(leaf
, i
) !=
512 BTRFS_LEAF_DATA_SIZE(root
)) {
513 btrfs_print_leaf(root
, leaf
);
514 printk("slot %d first offset bad\n", i
);
520 if (btrfs_item_size_nr(leaf
, nritems
- 1) > 4096) {
521 btrfs_print_leaf(root
, leaf
);
522 printk("slot %d bad size \n", nritems
- 1);
527 if (slot
!= 0 && slot
< nritems
- 1) {
528 btrfs_item_key(leaf
, &leaf_key
, slot
);
529 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
- 1);
530 if (btrfs_comp_keys(&leaf_key
, &cpukey
) <= 0) {
531 btrfs_print_leaf(root
, leaf
);
532 printk("slot %d offset bad key\n", slot
);
535 if (btrfs_item_offset_nr(leaf
, slot
- 1) !=
536 btrfs_item_end_nr(leaf
, slot
)) {
537 btrfs_print_leaf(root
, leaf
);
538 printk("slot %d offset bad\n", slot
);
542 if (slot
< nritems
- 1) {
543 btrfs_item_key(leaf
, &leaf_key
, slot
);
544 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
+ 1);
545 BUG_ON(btrfs_comp_keys(&leaf_key
, &cpukey
) >= 0);
546 if (btrfs_item_offset_nr(leaf
, slot
) !=
547 btrfs_item_end_nr(leaf
, slot
+ 1)) {
548 btrfs_print_leaf(root
, leaf
);
549 printk("slot %d offset bad\n", slot
);
553 BUG_ON(btrfs_item_offset_nr(leaf
, 0) +
554 btrfs_item_size_nr(leaf
, 0) != BTRFS_LEAF_DATA_SIZE(root
));
558 static int noinline
check_block(struct btrfs_root
*root
,
559 struct btrfs_path
*path
, int level
)
563 struct extent_buffer
*buf
= path
->nodes
[level
];
565 if (memcmp_extent_buffer(buf
, root
->fs_info
->fsid
,
566 (unsigned long)btrfs_header_fsid(buf
),
568 printk("warning bad block %Lu\n", buf
->start
);
573 return check_leaf(root
, path
, level
);
574 return check_node(root
, path
, level
);
578 * search for key in the extent_buffer. The items start at offset p,
579 * and they are item_size apart. There are 'max' items in p.
581 * the slot in the array is returned via slot, and it points to
582 * the place where you would insert key if it is not found in
585 * slot may point to max if the key is bigger than all of the keys
587 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
588 int item_size
, struct btrfs_key
*key
,
595 unsigned long offset
;
596 struct btrfs_disk_key
*tmp
;
599 mid
= (low
+ high
) / 2;
600 offset
= p
+ mid
* item_size
;
602 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
603 ret
= btrfs_comp_keys(tmp
, key
);
619 * simple bin_search frontend that does the right thing for
622 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
623 int level
, int *slot
)
626 return generic_bin_search(eb
,
627 offsetof(struct btrfs_leaf
, items
),
628 sizeof(struct btrfs_item
),
629 key
, btrfs_header_nritems(eb
),
632 return generic_bin_search(eb
,
633 offsetof(struct btrfs_node
, ptrs
),
634 sizeof(struct btrfs_key_ptr
),
635 key
, btrfs_header_nritems(eb
),
641 static struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
642 struct extent_buffer
*parent
, int slot
)
644 int level
= btrfs_header_level(parent
);
647 if (slot
>= btrfs_header_nritems(parent
))
652 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
653 btrfs_level_size(root
, level
- 1),
654 btrfs_node_ptr_generation(parent
, slot
));
657 static int balance_level(struct btrfs_trans_handle
*trans
,
658 struct btrfs_root
*root
,
659 struct btrfs_path
*path
, int level
)
661 struct extent_buffer
*right
= NULL
;
662 struct extent_buffer
*mid
;
663 struct extent_buffer
*left
= NULL
;
664 struct extent_buffer
*parent
= NULL
;
668 int orig_slot
= path
->slots
[level
];
669 int err_on_enospc
= 0;
675 mid
= path
->nodes
[level
];
676 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
678 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
680 if (level
< BTRFS_MAX_LEVEL
- 1)
681 parent
= path
->nodes
[level
+ 1];
682 pslot
= path
->slots
[level
+ 1];
685 * deal with the case where there is only one pointer in the root
686 * by promoting the node below to a root
689 struct extent_buffer
*child
;
691 if (btrfs_header_nritems(mid
) != 1)
694 /* promote the child to a root */
695 child
= read_node_slot(root
, mid
, 0);
697 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
702 ret
= btrfs_update_extent_ref(trans
, root
, child
->start
,
703 mid
->start
, child
->start
,
704 root
->root_key
.objectid
,
705 trans
->transid
, level
- 1);
708 add_root_to_dirty_list(root
);
709 path
->nodes
[level
] = NULL
;
710 clean_tree_block(trans
, root
, mid
);
711 wait_on_tree_block_writeback(root
, mid
);
712 /* once for the path */
713 free_extent_buffer(mid
);
714 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
715 mid
->start
, root
->root_key
.objectid
,
716 btrfs_header_generation(mid
),
718 /* once for the root ptr */
719 free_extent_buffer(mid
);
722 if (btrfs_header_nritems(mid
) >
723 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
726 if (btrfs_header_nritems(mid
) < 2)
729 left
= read_node_slot(root
, parent
, pslot
- 1);
731 wret
= btrfs_cow_block(trans
, root
, left
,
732 parent
, pslot
- 1, &left
);
738 right
= read_node_slot(root
, parent
, pslot
+ 1);
740 wret
= btrfs_cow_block(trans
, root
, right
,
741 parent
, pslot
+ 1, &right
);
748 /* first, try to make some room in the middle buffer */
750 orig_slot
+= btrfs_header_nritems(left
);
751 wret
= push_node_left(trans
, root
, left
, mid
, 1);
754 if (btrfs_header_nritems(mid
) < 2)
759 * then try to empty the right most buffer into the middle
762 wret
= push_node_left(trans
, root
, mid
, right
, 1);
763 if (wret
< 0 && wret
!= -ENOSPC
)
765 if (btrfs_header_nritems(right
) == 0) {
766 u64 bytenr
= right
->start
;
767 u64 generation
= btrfs_header_generation(parent
);
768 u32 blocksize
= right
->len
;
770 clean_tree_block(trans
, root
, right
);
771 wait_on_tree_block_writeback(root
, right
);
772 free_extent_buffer(right
);
774 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
778 wret
= btrfs_free_extent(trans
, root
, bytenr
,
779 blocksize
, parent
->start
,
780 btrfs_header_owner(parent
),
781 generation
, level
, 1);
785 struct btrfs_disk_key right_key
;
786 btrfs_node_key(right
, &right_key
, 0);
787 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
788 btrfs_mark_buffer_dirty(parent
);
791 if (btrfs_header_nritems(mid
) == 1) {
793 * we're not allowed to leave a node with one item in the
794 * tree during a delete. A deletion from lower in the tree
795 * could try to delete the only pointer in this node.
796 * So, pull some keys from the left.
797 * There has to be a left pointer at this point because
798 * otherwise we would have pulled some pointers from the
802 wret
= balance_node_right(trans
, root
, mid
, left
);
808 wret
= push_node_left(trans
, root
, left
, mid
, 1);
814 if (btrfs_header_nritems(mid
) == 0) {
815 /* we've managed to empty the middle node, drop it */
816 u64 root_gen
= btrfs_header_generation(parent
);
817 u64 bytenr
= mid
->start
;
818 u32 blocksize
= mid
->len
;
819 clean_tree_block(trans
, root
, mid
);
820 wait_on_tree_block_writeback(root
, mid
);
821 free_extent_buffer(mid
);
823 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
826 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
828 btrfs_header_owner(parent
),
833 /* update the parent key to reflect our changes */
834 struct btrfs_disk_key mid_key
;
835 btrfs_node_key(mid
, &mid_key
, 0);
836 btrfs_set_node_key(parent
, &mid_key
, pslot
);
837 btrfs_mark_buffer_dirty(parent
);
840 /* update the path */
842 if (btrfs_header_nritems(left
) > orig_slot
) {
843 extent_buffer_get(left
);
844 path
->nodes
[level
] = left
;
845 path
->slots
[level
+ 1] -= 1;
846 path
->slots
[level
] = orig_slot
;
848 free_extent_buffer(mid
);
850 orig_slot
-= btrfs_header_nritems(left
);
851 path
->slots
[level
] = orig_slot
;
854 /* double check we haven't messed things up */
855 check_block(root
, path
, level
);
857 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
861 free_extent_buffer(right
);
863 free_extent_buffer(left
);
867 /* returns zero if the push worked, non-zero otherwise */
868 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
869 struct btrfs_root
*root
,
870 struct btrfs_path
*path
, int level
)
872 struct extent_buffer
*right
= NULL
;
873 struct extent_buffer
*mid
;
874 struct extent_buffer
*left
= NULL
;
875 struct extent_buffer
*parent
= NULL
;
879 int orig_slot
= path
->slots
[level
];
885 mid
= path
->nodes
[level
];
886 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
887 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
889 if (level
< BTRFS_MAX_LEVEL
- 1)
890 parent
= path
->nodes
[level
+ 1];
891 pslot
= path
->slots
[level
+ 1];
896 left
= read_node_slot(root
, parent
, pslot
- 1);
898 /* first, try to make some room in the middle buffer */
901 left_nr
= btrfs_header_nritems(left
);
902 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
905 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
910 wret
= push_node_left(trans
, root
,
917 struct btrfs_disk_key disk_key
;
918 orig_slot
+= left_nr
;
919 btrfs_node_key(mid
, &disk_key
, 0);
920 btrfs_set_node_key(parent
, &disk_key
, pslot
);
921 btrfs_mark_buffer_dirty(parent
);
922 if (btrfs_header_nritems(left
) > orig_slot
) {
923 path
->nodes
[level
] = left
;
924 path
->slots
[level
+ 1] -= 1;
925 path
->slots
[level
] = orig_slot
;
926 free_extent_buffer(mid
);
929 btrfs_header_nritems(left
);
930 path
->slots
[level
] = orig_slot
;
931 free_extent_buffer(left
);
935 free_extent_buffer(left
);
937 right
= read_node_slot(root
, parent
, pslot
+ 1);
940 * then try to empty the right most buffer into the middle
944 right_nr
= btrfs_header_nritems(right
);
945 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
948 ret
= btrfs_cow_block(trans
, root
, right
,
954 wret
= balance_node_right(trans
, root
,
961 struct btrfs_disk_key disk_key
;
963 btrfs_node_key(right
, &disk_key
, 0);
964 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
965 btrfs_mark_buffer_dirty(parent
);
967 if (btrfs_header_nritems(mid
) <= orig_slot
) {
968 path
->nodes
[level
] = right
;
969 path
->slots
[level
+ 1] += 1;
970 path
->slots
[level
] = orig_slot
-
971 btrfs_header_nritems(mid
);
972 free_extent_buffer(mid
);
974 free_extent_buffer(right
);
978 free_extent_buffer(right
);
984 * readahead one full node of leaves
986 static void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
987 int level
, int slot
, u64 objectid
)
989 struct extent_buffer
*node
;
990 struct btrfs_disk_key disk_key
;
996 int direction
= path
->reada
;
997 struct extent_buffer
*eb
;
1005 if (!path
->nodes
[level
])
1008 node
= path
->nodes
[level
];
1009 search
= btrfs_node_blockptr(node
, slot
);
1010 blocksize
= btrfs_level_size(root
, level
- 1);
1011 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1013 free_extent_buffer(eb
);
1017 highest_read
= search
;
1018 lowest_read
= search
;
1020 nritems
= btrfs_header_nritems(node
);
1023 if (direction
< 0) {
1027 } else if (direction
> 0) {
1032 if (path
->reada
< 0 && objectid
) {
1033 btrfs_node_key(node
, &disk_key
, nr
);
1034 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1037 search
= btrfs_node_blockptr(node
, nr
);
1038 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1039 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1040 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1041 readahead_tree_block(root
, search
, blocksize
,
1042 btrfs_node_ptr_generation(node
, nr
));
1046 if (path
->reada
< 2 && (nread
> (256 * 1024) || nscan
> 32))
1048 if(nread
> (1024 * 1024) || nscan
> 128)
1051 if (search
< lowest_read
)
1052 lowest_read
= search
;
1053 if (search
> highest_read
)
1054 highest_read
= search
;
1059 * look for key in the tree. path is filled in with nodes along the way
1060 * if key is found, we return zero and you can find the item in the leaf
1061 * level of the path (level 0)
1063 * If the key isn't found, the path points to the slot where it should
1064 * be inserted, and 1 is returned. If there are other errors during the
1065 * search a negative error number is returned.
1067 * if ins_len > 0, nodes and leaves will be split as we walk down the
1068 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1071 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1072 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1075 struct extent_buffer
*b
;
1079 int should_reada
= p
->reada
;
1080 u8 lowest_level
= 0;
1082 lowest_level
= p
->lowest_level
;
1083 WARN_ON(lowest_level
&& ins_len
);
1084 WARN_ON(p
->nodes
[0] != NULL
);
1086 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1090 extent_buffer_get(b
);
1092 level
= btrfs_header_level(b
);
1095 wret
= btrfs_cow_block(trans
, root
, b
,
1096 p
->nodes
[level
+ 1],
1097 p
->slots
[level
+ 1],
1100 free_extent_buffer(b
);
1104 BUG_ON(!cow
&& ins_len
);
1105 if (level
!= btrfs_header_level(b
))
1107 level
= btrfs_header_level(b
);
1108 p
->nodes
[level
] = b
;
1109 ret
= check_block(root
, p
, level
);
1112 ret
= bin_search(b
, key
, level
, &slot
);
1114 if (ret
&& slot
> 0)
1116 p
->slots
[level
] = slot
;
1117 if (ins_len
> 0 && btrfs_header_nritems(b
) >=
1118 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1119 int sret
= split_node(trans
, root
, p
, level
);
1123 b
= p
->nodes
[level
];
1124 slot
= p
->slots
[level
];
1125 } else if (ins_len
< 0) {
1126 int sret
= balance_level(trans
, root
, p
,
1130 b
= p
->nodes
[level
];
1132 btrfs_release_path(NULL
, p
);
1135 slot
= p
->slots
[level
];
1136 BUG_ON(btrfs_header_nritems(b
) == 1);
1138 /* this is only true while dropping a snapshot */
1139 if (level
== lowest_level
)
1143 reada_for_search(root
, p
, level
, slot
,
1146 b
= read_node_slot(root
, b
, slot
);
1148 p
->slots
[level
] = slot
;
1149 if (ins_len
> 0 && btrfs_leaf_free_space(root
, b
) <
1150 sizeof(struct btrfs_item
) + ins_len
) {
1151 int sret
= split_leaf(trans
, root
, key
,
1152 p
, ins_len
, ret
== 0);
1164 * adjust the pointers going up the tree, starting at level
1165 * making sure the right key of each node is points to 'key'.
1166 * This is used after shifting pointers to the left, so it stops
1167 * fixing up pointers when a given leaf/node is not in slot 0 of the
1170 * If this fails to write a tree block, it returns -1, but continues
1171 * fixing up the blocks in ram so the tree is consistent.
1173 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1174 struct btrfs_root
*root
, struct btrfs_path
*path
,
1175 struct btrfs_disk_key
*key
, int level
)
1179 struct extent_buffer
*t
;
1181 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1182 int tslot
= path
->slots
[i
];
1183 if (!path
->nodes
[i
])
1186 btrfs_set_node_key(t
, key
, tslot
);
1187 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1197 * This function isn't completely safe. It's the caller's responsibility
1198 * that the new key won't break the order
1200 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1201 struct btrfs_root
*root
, struct btrfs_path
*path
,
1202 struct btrfs_key
*new_key
)
1204 struct btrfs_disk_key disk_key
;
1205 struct extent_buffer
*eb
;
1208 eb
= path
->nodes
[0];
1209 slot
= path
->slots
[0];
1211 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1212 if (btrfs_comp_keys(&disk_key
, new_key
) >= 0)
1215 if (slot
< btrfs_header_nritems(eb
) - 1) {
1216 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1217 if (btrfs_comp_keys(&disk_key
, new_key
) <= 0)
1221 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1222 btrfs_set_item_key(eb
, &disk_key
, slot
);
1223 btrfs_mark_buffer_dirty(eb
);
1225 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1230 * try to push data from one node into the next node left in the
1233 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1234 * error, and > 0 if there was no room in the left hand block.
1236 static int push_node_left(struct btrfs_trans_handle
*trans
,
1237 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1238 struct extent_buffer
*src
, int empty
)
1245 src_nritems
= btrfs_header_nritems(src
);
1246 dst_nritems
= btrfs_header_nritems(dst
);
1247 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1248 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1249 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1251 if (!empty
&& src_nritems
<= 8)
1254 if (push_items
<= 0) {
1259 push_items
= min(src_nritems
, push_items
);
1260 if (push_items
< src_nritems
) {
1261 /* leave at least 8 pointers in the node if
1262 * we aren't going to empty it
1264 if (src_nritems
- push_items
< 8) {
1265 if (push_items
<= 8)
1271 push_items
= min(src_nritems
- 8, push_items
);
1273 copy_extent_buffer(dst
, src
,
1274 btrfs_node_key_ptr_offset(dst_nritems
),
1275 btrfs_node_key_ptr_offset(0),
1276 push_items
* sizeof(struct btrfs_key_ptr
));
1278 if (push_items
< src_nritems
) {
1279 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1280 btrfs_node_key_ptr_offset(push_items
),
1281 (src_nritems
- push_items
) *
1282 sizeof(struct btrfs_key_ptr
));
1284 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1285 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1286 btrfs_mark_buffer_dirty(src
);
1287 btrfs_mark_buffer_dirty(dst
);
1289 ret
= btrfs_update_ref(trans
, root
, src
, dst
, dst_nritems
, push_items
);
1295 * try to push data from one node into the next node right in the
1298 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1299 * error, and > 0 if there was no room in the right hand block.
1301 * this will only push up to 1/2 the contents of the left node over
1303 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1304 struct btrfs_root
*root
,
1305 struct extent_buffer
*dst
,
1306 struct extent_buffer
*src
)
1314 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1315 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1317 src_nritems
= btrfs_header_nritems(src
);
1318 dst_nritems
= btrfs_header_nritems(dst
);
1319 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1320 if (push_items
<= 0) {
1324 if (src_nritems
< 4) {
1328 max_push
= src_nritems
/ 2 + 1;
1329 /* don't try to empty the node */
1330 if (max_push
>= src_nritems
) {
1334 if (max_push
< push_items
)
1335 push_items
= max_push
;
1337 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1338 btrfs_node_key_ptr_offset(0),
1340 sizeof(struct btrfs_key_ptr
));
1342 copy_extent_buffer(dst
, src
,
1343 btrfs_node_key_ptr_offset(0),
1344 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1345 push_items
* sizeof(struct btrfs_key_ptr
));
1347 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1348 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1350 btrfs_mark_buffer_dirty(src
);
1351 btrfs_mark_buffer_dirty(dst
);
1353 ret
= btrfs_update_ref(trans
, root
, src
, dst
, 0, push_items
);
1359 * helper function to insert a new root level in the tree.
1360 * A new node is allocated, and a single item is inserted to
1361 * point to the existing root
1363 * returns zero on success or < 0 on failure.
1365 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1366 struct btrfs_root
*root
,
1367 struct btrfs_path
*path
, int level
)
1370 struct extent_buffer
*lower
;
1371 struct extent_buffer
*c
;
1372 struct extent_buffer
*old
;
1373 struct btrfs_disk_key lower_key
;
1376 BUG_ON(path
->nodes
[level
]);
1377 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1379 lower
= path
->nodes
[level
-1];
1381 btrfs_item_key(lower
, &lower_key
, 0);
1383 btrfs_node_key(lower
, &lower_key
, 0);
1385 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
1386 root
->root_key
.objectid
,
1387 trans
->transid
, level
,
1388 root
->node
->start
, 0);
1392 memset_extent_buffer(c
, 0, 0, root
->nodesize
);
1393 btrfs_set_header_nritems(c
, 1);
1394 btrfs_set_header_level(c
, level
);
1395 btrfs_set_header_bytenr(c
, c
->start
);
1396 btrfs_set_header_generation(c
, trans
->transid
);
1397 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1399 write_extent_buffer(c
, root
->fs_info
->fsid
,
1400 (unsigned long)btrfs_header_fsid(c
),
1403 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1404 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
1407 btrfs_set_node_key(c
, &lower_key
, 0);
1408 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1409 lower_gen
= btrfs_header_generation(lower
);
1410 WARN_ON(lower_gen
!= trans
->transid
);
1412 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1414 btrfs_mark_buffer_dirty(c
);
1419 ret
= btrfs_update_extent_ref(trans
, root
, lower
->start
,
1420 lower
->start
, c
->start
,
1421 root
->root_key
.objectid
,
1422 trans
->transid
, level
- 1);
1425 /* the super has an extra ref to root->node */
1426 free_extent_buffer(old
);
1428 add_root_to_dirty_list(root
);
1429 extent_buffer_get(c
);
1430 path
->nodes
[level
] = c
;
1431 path
->slots
[level
] = 0;
1436 * worker function to insert a single pointer in a node.
1437 * the node should have enough room for the pointer already
1439 * slot and level indicate where you want the key to go, and
1440 * blocknr is the block the key points to.
1442 * returns zero on success and < 0 on any error
1444 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1445 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1446 *key
, u64 bytenr
, int slot
, int level
)
1448 struct extent_buffer
*lower
;
1451 BUG_ON(!path
->nodes
[level
]);
1452 lower
= path
->nodes
[level
];
1453 nritems
= btrfs_header_nritems(lower
);
1456 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
1458 if (slot
!= nritems
) {
1459 memmove_extent_buffer(lower
,
1460 btrfs_node_key_ptr_offset(slot
+ 1),
1461 btrfs_node_key_ptr_offset(slot
),
1462 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1464 btrfs_set_node_key(lower
, key
, slot
);
1465 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1466 WARN_ON(trans
->transid
== 0);
1467 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1468 btrfs_set_header_nritems(lower
, nritems
+ 1);
1469 btrfs_mark_buffer_dirty(lower
);
1474 * split the node at the specified level in path in two.
1475 * The path is corrected to point to the appropriate node after the split
1477 * Before splitting this tries to make some room in the node by pushing
1478 * left and right, if either one works, it returns right away.
1480 * returns 0 on success and < 0 on failure
1482 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1483 *root
, struct btrfs_path
*path
, int level
)
1485 struct extent_buffer
*c
;
1486 struct extent_buffer
*split
;
1487 struct btrfs_disk_key disk_key
;
1493 c
= path
->nodes
[level
];
1494 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1495 if (c
== root
->node
) {
1496 /* trying to split the root, lets make a new one */
1497 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1501 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1502 c
= path
->nodes
[level
];
1503 if (!ret
&& btrfs_header_nritems(c
) <
1504 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
1510 c_nritems
= btrfs_header_nritems(c
);
1512 btrfs_node_key(c
, &disk_key
, 0);
1513 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
1514 path
->nodes
[level
+ 1]->start
,
1515 root
->root_key
.objectid
,
1516 trans
->transid
, level
, c
->start
, 0);
1518 return PTR_ERR(split
);
1520 btrfs_set_header_flags(split
, btrfs_header_flags(c
));
1521 btrfs_set_header_level(split
, btrfs_header_level(c
));
1522 btrfs_set_header_bytenr(split
, split
->start
);
1523 btrfs_set_header_generation(split
, trans
->transid
);
1524 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1525 btrfs_set_header_flags(split
, 0);
1526 write_extent_buffer(split
, root
->fs_info
->fsid
,
1527 (unsigned long)btrfs_header_fsid(split
),
1529 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1530 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
1533 mid
= (c_nritems
+ 1) / 2;
1535 copy_extent_buffer(split
, c
,
1536 btrfs_node_key_ptr_offset(0),
1537 btrfs_node_key_ptr_offset(mid
),
1538 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1539 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1540 btrfs_set_header_nritems(c
, mid
);
1543 btrfs_mark_buffer_dirty(c
);
1544 btrfs_mark_buffer_dirty(split
);
1546 btrfs_node_key(split
, &disk_key
, 0);
1547 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1548 path
->slots
[level
+ 1] + 1,
1553 ret
= btrfs_update_ref(trans
, root
, c
, split
, 0, c_nritems
- mid
);
1556 if (path
->slots
[level
] >= mid
) {
1557 path
->slots
[level
] -= mid
;
1558 free_extent_buffer(c
);
1559 path
->nodes
[level
] = split
;
1560 path
->slots
[level
+ 1] += 1;
1562 free_extent_buffer(split
);
1568 * how many bytes are required to store the items in a leaf. start
1569 * and nr indicate which items in the leaf to check. This totals up the
1570 * space used both by the item structs and the item data
1572 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1575 int nritems
= btrfs_header_nritems(l
);
1576 int end
= min(nritems
, start
+ nr
) - 1;
1580 data_len
= btrfs_item_end_nr(l
, start
);
1581 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1582 data_len
+= sizeof(struct btrfs_item
) * nr
;
1583 WARN_ON(data_len
< 0);
1588 * The space between the end of the leaf items and
1589 * the start of the leaf data. IOW, how much room
1590 * the leaf has left for both items and data
1592 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1594 int nritems
= btrfs_header_nritems(leaf
);
1596 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
1598 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1599 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
1600 leaf_space_used(leaf
, 0, nritems
), nritems
);
1606 * push some data in the path leaf to the right, trying to free up at
1607 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1609 * returns 1 if the push failed because the other node didn't have enough
1610 * room, 0 if everything worked out and < 0 if there were major errors.
1612 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1613 *root
, struct btrfs_path
*path
, int data_size
,
1616 struct extent_buffer
*left
= path
->nodes
[0];
1617 struct extent_buffer
*right
;
1618 struct extent_buffer
*upper
;
1619 struct btrfs_disk_key disk_key
;
1625 struct btrfs_item
*item
;
1633 slot
= path
->slots
[1];
1634 if (!path
->nodes
[1]) {
1637 upper
= path
->nodes
[1];
1638 if (slot
>= btrfs_header_nritems(upper
) - 1)
1641 right
= read_node_slot(root
, upper
, slot
+ 1);
1642 free_space
= btrfs_leaf_free_space(root
, right
);
1643 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1644 free_extent_buffer(right
);
1648 /* cow and double check */
1649 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1652 free_extent_buffer(right
);
1655 free_space
= btrfs_leaf_free_space(root
, right
);
1656 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1657 free_extent_buffer(right
);
1661 left_nritems
= btrfs_header_nritems(left
);
1662 if (left_nritems
== 0) {
1663 free_extent_buffer(right
);
1672 i
= left_nritems
- 1;
1674 item
= btrfs_item_nr(left
, i
);
1676 if (path
->slots
[0] == i
)
1677 push_space
+= data_size
+ sizeof(*item
);
1679 this_item_size
= btrfs_item_size(left
, item
);
1680 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1683 push_space
+= this_item_size
+ sizeof(*item
);
1689 if (push_items
== 0) {
1690 free_extent_buffer(right
);
1694 if (!empty
&& push_items
== left_nritems
)
1697 /* push left to right */
1698 right_nritems
= btrfs_header_nritems(right
);
1700 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1701 push_space
-= leaf_data_end(root
, left
);
1703 /* make room in the right data area */
1704 data_end
= leaf_data_end(root
, right
);
1705 memmove_extent_buffer(right
,
1706 btrfs_leaf_data(right
) + data_end
- push_space
,
1707 btrfs_leaf_data(right
) + data_end
,
1708 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
1710 /* copy from the left data area */
1711 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1712 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1713 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1716 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1717 btrfs_item_nr_offset(0),
1718 right_nritems
* sizeof(struct btrfs_item
));
1720 /* copy the items from left to right */
1721 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1722 btrfs_item_nr_offset(left_nritems
- push_items
),
1723 push_items
* sizeof(struct btrfs_item
));
1725 /* update the item pointers */
1726 right_nritems
+= push_items
;
1727 btrfs_set_header_nritems(right
, right_nritems
);
1728 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1729 for (i
= 0; i
< right_nritems
; i
++) {
1730 item
= btrfs_item_nr(right
, i
);
1731 push_space
-= btrfs_item_size(right
, item
);
1732 btrfs_set_item_offset(right
, item
, push_space
);
1735 left_nritems
-= push_items
;
1736 btrfs_set_header_nritems(left
, left_nritems
);
1739 btrfs_mark_buffer_dirty(left
);
1740 btrfs_mark_buffer_dirty(right
);
1742 btrfs_item_key(right
, &disk_key
, 0);
1743 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1744 btrfs_mark_buffer_dirty(upper
);
1746 ret
= btrfs_update_ref(trans
, root
, left
, right
, 0, push_items
);
1749 /* then fixup the leaf pointer in the path */
1750 if (path
->slots
[0] >= left_nritems
) {
1751 path
->slots
[0] -= left_nritems
;
1752 free_extent_buffer(path
->nodes
[0]);
1753 path
->nodes
[0] = right
;
1754 path
->slots
[1] += 1;
1756 free_extent_buffer(right
);
1761 * push some data in the path leaf to the left, trying to free up at
1762 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1764 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1765 *root
, struct btrfs_path
*path
, int data_size
,
1768 struct btrfs_disk_key disk_key
;
1769 struct extent_buffer
*right
= path
->nodes
[0];
1770 struct extent_buffer
*left
;
1776 struct btrfs_item
*item
;
1777 u32 old_left_nritems
;
1783 u32 old_left_item_size
;
1785 slot
= path
->slots
[1];
1788 if (!path
->nodes
[1])
1791 right_nritems
= btrfs_header_nritems(right
);
1792 if (right_nritems
== 0) {
1796 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
1797 free_space
= btrfs_leaf_free_space(root
, left
);
1798 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1799 free_extent_buffer(left
);
1803 /* cow and double check */
1804 ret
= btrfs_cow_block(trans
, root
, left
,
1805 path
->nodes
[1], slot
- 1, &left
);
1807 /* we hit -ENOSPC, but it isn't fatal here */
1808 free_extent_buffer(left
);
1812 free_space
= btrfs_leaf_free_space(root
, left
);
1813 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1814 free_extent_buffer(left
);
1821 nr
= right_nritems
- 1;
1823 for (i
= 0; i
< nr
; i
++) {
1824 item
= btrfs_item_nr(right
, i
);
1826 if (path
->slots
[0] == i
)
1827 push_space
+= data_size
+ sizeof(*item
);
1829 this_item_size
= btrfs_item_size(right
, item
);
1830 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1834 push_space
+= this_item_size
+ sizeof(*item
);
1837 if (push_items
== 0) {
1838 free_extent_buffer(left
);
1841 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1844 /* push data from right to left */
1845 copy_extent_buffer(left
, right
,
1846 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1847 btrfs_item_nr_offset(0),
1848 push_items
* sizeof(struct btrfs_item
));
1850 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1851 btrfs_item_offset_nr(right
, push_items
-1);
1853 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1854 leaf_data_end(root
, left
) - push_space
,
1855 btrfs_leaf_data(right
) +
1856 btrfs_item_offset_nr(right
, push_items
- 1),
1858 old_left_nritems
= btrfs_header_nritems(left
);
1859 BUG_ON(old_left_nritems
< 0);
1861 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1862 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1865 item
= btrfs_item_nr(left
, i
);
1866 ioff
= btrfs_item_offset(left
, item
);
1867 btrfs_set_item_offset(left
, item
,
1868 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
1870 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1872 /* fixup right node */
1873 if (push_items
> right_nritems
) {
1874 printk("push items %d nr %u\n", push_items
, right_nritems
);
1878 if (push_items
< right_nritems
) {
1879 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1880 leaf_data_end(root
, right
);
1881 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1882 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1883 btrfs_leaf_data(right
) +
1884 leaf_data_end(root
, right
), push_space
);
1886 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1887 btrfs_item_nr_offset(push_items
),
1888 (btrfs_header_nritems(right
) - push_items
) *
1889 sizeof(struct btrfs_item
));
1891 right_nritems
-= push_items
;
1892 btrfs_set_header_nritems(right
, right_nritems
);
1893 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1894 for (i
= 0; i
< right_nritems
; i
++) {
1895 item
= btrfs_item_nr(right
, i
);
1896 push_space
= push_space
- btrfs_item_size(right
, item
);
1897 btrfs_set_item_offset(right
, item
, push_space
);
1900 btrfs_mark_buffer_dirty(left
);
1902 btrfs_mark_buffer_dirty(right
);
1904 btrfs_item_key(right
, &disk_key
, 0);
1905 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1909 ret
= btrfs_update_ref(trans
, root
, right
, left
,
1910 old_left_nritems
, push_items
);
1913 /* then fixup the leaf pointer in the path */
1914 if (path
->slots
[0] < push_items
) {
1915 path
->slots
[0] += old_left_nritems
;
1916 free_extent_buffer(path
->nodes
[0]);
1917 path
->nodes
[0] = left
;
1918 path
->slots
[1] -= 1;
1920 free_extent_buffer(left
);
1921 path
->slots
[0] -= push_items
;
1923 BUG_ON(path
->slots
[0] < 0);
1928 * split the path's leaf in two, making sure there is at least data_size
1929 * available for the resulting leaf level of the path.
1931 * returns 0 if all went well and < 0 on failure.
1933 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
1934 *root
, struct btrfs_key
*ins_key
,
1935 struct btrfs_path
*path
, int data_size
, int extend
)
1937 struct extent_buffer
*l
;
1941 struct extent_buffer
*right
;
1942 int space_needed
= data_size
+ sizeof(struct btrfs_item
);
1949 int num_doubles
= 0;
1950 struct btrfs_disk_key disk_key
;
1953 space_needed
= data_size
;
1955 /* first try to make some room by pushing left and right */
1956 if (ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
1957 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
1962 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
1968 /* did the pushes work? */
1969 if (btrfs_leaf_free_space(root
, l
) >= space_needed
)
1973 if (!path
->nodes
[1]) {
1974 ret
= insert_new_root(trans
, root
, path
, 1);
1981 slot
= path
->slots
[0];
1982 nritems
= btrfs_header_nritems(l
);
1983 mid
= (nritems
+ 1)/ 2;
1985 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
1986 path
->nodes
[1]->start
,
1987 root
->root_key
.objectid
,
1988 trans
->transid
, 0, l
->start
, 0);
1989 if (IS_ERR(right
)) {
1991 return PTR_ERR(right
);
1994 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
1995 btrfs_set_header_bytenr(right
, right
->start
);
1996 btrfs_set_header_generation(right
, trans
->transid
);
1997 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
1998 btrfs_set_header_level(right
, 0);
1999 write_extent_buffer(right
, root
->fs_info
->fsid
,
2000 (unsigned long)btrfs_header_fsid(right
),
2003 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2004 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
2008 leaf_space_used(l
, mid
, nritems
- mid
) + space_needed
>
2009 BTRFS_LEAF_DATA_SIZE(root
)) {
2010 if (slot
>= nritems
) {
2011 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2012 btrfs_set_header_nritems(right
, 0);
2013 wret
= insert_ptr(trans
, root
, path
,
2014 &disk_key
, right
->start
,
2015 path
->slots
[1] + 1, 1);
2018 free_extent_buffer(path
->nodes
[0]);
2019 path
->nodes
[0] = right
;
2021 path
->slots
[1] += 1;
2025 if (mid
!= nritems
&&
2026 leaf_space_used(l
, mid
, nritems
- mid
) +
2027 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
2032 if (leaf_space_used(l
, 0, mid
+ 1) + space_needed
>
2033 BTRFS_LEAF_DATA_SIZE(root
)) {
2034 if (!extend
&& slot
== 0) {
2035 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2036 btrfs_set_header_nritems(right
, 0);
2037 wret
= insert_ptr(trans
, root
, path
,
2043 free_extent_buffer(path
->nodes
[0]);
2044 path
->nodes
[0] = right
;
2046 if (path
->slots
[1] == 0) {
2047 wret
= fixup_low_keys(trans
, root
,
2048 path
, &disk_key
, 1);
2053 } else if (extend
&& slot
== 0) {
2057 if (mid
!= nritems
&&
2058 leaf_space_used(l
, mid
, nritems
- mid
) +
2059 space_needed
> BTRFS_LEAF_DATA_SIZE(root
)) {
2065 nritems
= nritems
- mid
;
2066 btrfs_set_header_nritems(right
, nritems
);
2067 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2069 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2070 btrfs_item_nr_offset(mid
),
2071 nritems
* sizeof(struct btrfs_item
));
2073 copy_extent_buffer(right
, l
,
2074 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2075 data_copy_size
, btrfs_leaf_data(l
) +
2076 leaf_data_end(root
, l
), data_copy_size
);
2078 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2079 btrfs_item_end_nr(l
, mid
);
2081 for (i
= 0; i
< nritems
; i
++) {
2082 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2083 u32 ioff
= btrfs_item_offset(right
, item
);
2084 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2087 btrfs_set_header_nritems(l
, mid
);
2089 btrfs_item_key(right
, &disk_key
, 0);
2090 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2091 path
->slots
[1] + 1, 1);
2095 btrfs_mark_buffer_dirty(right
);
2096 btrfs_mark_buffer_dirty(l
);
2097 BUG_ON(path
->slots
[0] != slot
);
2099 ret
= btrfs_update_ref(trans
, root
, l
, right
, 0, nritems
);
2103 free_extent_buffer(path
->nodes
[0]);
2104 path
->nodes
[0] = right
;
2105 path
->slots
[0] -= mid
;
2106 path
->slots
[1] += 1;
2108 free_extent_buffer(right
);
2110 BUG_ON(path
->slots
[0] < 0);
2113 BUG_ON(num_doubles
!= 0);
2120 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
2121 struct btrfs_root
*root
,
2122 struct btrfs_path
*path
,
2123 u32 new_size
, int from_end
)
2128 struct extent_buffer
*leaf
;
2129 struct btrfs_item
*item
;
2131 unsigned int data_end
;
2132 unsigned int old_data_start
;
2133 unsigned int old_size
;
2134 unsigned int size_diff
;
2137 slot_orig
= path
->slots
[0];
2138 leaf
= path
->nodes
[0];
2139 slot
= path
->slots
[0];
2141 old_size
= btrfs_item_size_nr(leaf
, slot
);
2142 if (old_size
== new_size
)
2145 nritems
= btrfs_header_nritems(leaf
);
2146 data_end
= leaf_data_end(root
, leaf
);
2148 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2150 size_diff
= old_size
- new_size
;
2153 BUG_ON(slot
>= nritems
);
2156 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2158 /* first correct the data pointers */
2159 for (i
= slot
; i
< nritems
; i
++) {
2161 item
= btrfs_item_nr(leaf
, i
);
2162 ioff
= btrfs_item_offset(leaf
, item
);
2163 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2166 /* shift the data */
2168 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2169 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2170 data_end
, old_data_start
+ new_size
- data_end
);
2172 struct btrfs_disk_key disk_key
;
2175 btrfs_item_key(leaf
, &disk_key
, slot
);
2177 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2179 struct btrfs_file_extent_item
*fi
;
2181 fi
= btrfs_item_ptr(leaf
, slot
,
2182 struct btrfs_file_extent_item
);
2183 fi
= (struct btrfs_file_extent_item
*)(
2184 (unsigned long)fi
- size_diff
);
2186 if (btrfs_file_extent_type(leaf
, fi
) ==
2187 BTRFS_FILE_EXTENT_INLINE
) {
2188 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2189 memmove_extent_buffer(leaf
, ptr
,
2191 offsetof(struct btrfs_file_extent_item
,
2196 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2197 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2198 data_end
, old_data_start
- data_end
);
2200 offset
= btrfs_disk_key_offset(&disk_key
);
2201 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2202 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2204 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2207 item
= btrfs_item_nr(leaf
, slot
);
2208 btrfs_set_item_size(leaf
, item
, new_size
);
2209 btrfs_mark_buffer_dirty(leaf
);
2212 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2213 btrfs_print_leaf(root
, leaf
);
2219 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
2220 struct btrfs_root
*root
, struct btrfs_path
*path
,
2226 struct extent_buffer
*leaf
;
2227 struct btrfs_item
*item
;
2229 unsigned int data_end
;
2230 unsigned int old_data
;
2231 unsigned int old_size
;
2234 slot_orig
= path
->slots
[0];
2235 leaf
= path
->nodes
[0];
2237 nritems
= btrfs_header_nritems(leaf
);
2238 data_end
= leaf_data_end(root
, leaf
);
2240 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2241 btrfs_print_leaf(root
, leaf
);
2244 slot
= path
->slots
[0];
2245 old_data
= btrfs_item_end_nr(leaf
, slot
);
2248 if (slot
>= nritems
) {
2249 btrfs_print_leaf(root
, leaf
);
2250 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2255 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2257 /* first correct the data pointers */
2258 for (i
= slot
; i
< nritems
; i
++) {
2260 item
= btrfs_item_nr(leaf
, i
);
2261 ioff
= btrfs_item_offset(leaf
, item
);
2262 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2265 /* shift the data */
2266 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2267 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2268 data_end
, old_data
- data_end
);
2270 data_end
= old_data
;
2271 old_size
= btrfs_item_size_nr(leaf
, slot
);
2272 item
= btrfs_item_nr(leaf
, slot
);
2273 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2274 btrfs_mark_buffer_dirty(leaf
);
2277 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2278 btrfs_print_leaf(root
, leaf
);
2285 * Given a key and some data, insert an item into the tree.
2286 * This does all the path init required, making room in the tree if needed.
2288 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2289 struct btrfs_root
*root
,
2290 struct btrfs_path
*path
,
2291 struct btrfs_key
*cpu_key
, u32
*data_size
,
2294 struct extent_buffer
*leaf
;
2295 struct btrfs_item
*item
;
2303 unsigned int data_end
;
2304 struct btrfs_disk_key disk_key
;
2306 for (i
= 0; i
< nr
; i
++) {
2307 total_data
+= data_size
[i
];
2310 /* create a root if there isn't one */
2314 total_size
= total_data
+ (nr
- 1) * sizeof(struct btrfs_item
);
2315 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2322 slot_orig
= path
->slots
[0];
2323 leaf
= path
->nodes
[0];
2325 nritems
= btrfs_header_nritems(leaf
);
2326 data_end
= leaf_data_end(root
, leaf
);
2328 if (btrfs_leaf_free_space(root
, leaf
) <
2329 sizeof(struct btrfs_item
) + total_size
) {
2330 btrfs_print_leaf(root
, leaf
);
2331 printk("not enough freespace need %u have %d\n",
2332 total_size
, btrfs_leaf_free_space(root
, leaf
));
2336 slot
= path
->slots
[0];
2339 if (slot
!= nritems
) {
2341 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2343 if (old_data
< data_end
) {
2344 btrfs_print_leaf(root
, leaf
);
2345 printk("slot %d old_data %d data_end %d\n",
2346 slot
, old_data
, data_end
);
2350 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2352 /* first correct the data pointers */
2353 for (i
= slot
; i
< nritems
; i
++) {
2356 item
= btrfs_item_nr(leaf
, i
);
2357 ioff
= btrfs_item_offset(leaf
, item
);
2358 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2361 /* shift the items */
2362 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2363 btrfs_item_nr_offset(slot
),
2364 (nritems
- slot
) * sizeof(struct btrfs_item
));
2366 /* shift the data */
2367 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2368 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2369 data_end
, old_data
- data_end
);
2370 data_end
= old_data
;
2373 /* setup the item for the new data */
2374 for (i
= 0; i
< nr
; i
++) {
2375 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2376 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2377 item
= btrfs_item_nr(leaf
, slot
+ i
);
2378 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2379 data_end
-= data_size
[i
];
2380 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2382 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2383 btrfs_mark_buffer_dirty(leaf
);
2387 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2388 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2391 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2392 btrfs_print_leaf(root
, leaf
);
2401 * Given a key and some data, insert an item into the tree.
2402 * This does all the path init required, making room in the tree if needed.
2404 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2405 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2409 struct btrfs_path
*path
;
2410 struct extent_buffer
*leaf
;
2413 path
= btrfs_alloc_path();
2415 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2417 leaf
= path
->nodes
[0];
2418 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2419 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2420 btrfs_mark_buffer_dirty(leaf
);
2422 btrfs_free_path(path
);
2427 * delete the pointer from a given node.
2429 * If the delete empties a node, the node is removed from the tree,
2430 * continuing all the way the root if required. The root is converted into
2431 * a leaf if all the nodes are emptied.
2433 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2434 struct btrfs_path
*path
, int level
, int slot
)
2436 struct extent_buffer
*parent
= path
->nodes
[level
];
2441 nritems
= btrfs_header_nritems(parent
);
2442 if (slot
!= nritems
-1) {
2443 memmove_extent_buffer(parent
,
2444 btrfs_node_key_ptr_offset(slot
),
2445 btrfs_node_key_ptr_offset(slot
+ 1),
2446 sizeof(struct btrfs_key_ptr
) *
2447 (nritems
- slot
- 1));
2450 btrfs_set_header_nritems(parent
, nritems
);
2451 if (nritems
== 0 && parent
== root
->node
) {
2452 BUG_ON(btrfs_header_level(root
->node
) != 1);
2453 /* just turn the root into a leaf and break */
2454 btrfs_set_header_level(root
->node
, 0);
2455 } else if (slot
== 0) {
2456 struct btrfs_disk_key disk_key
;
2458 btrfs_node_key(parent
, &disk_key
, 0);
2459 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
2463 btrfs_mark_buffer_dirty(parent
);
2468 * delete the item at the leaf level in path. If that empties
2469 * the leaf, remove it from the tree
2471 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2472 struct btrfs_path
*path
, int slot
, int nr
)
2474 struct extent_buffer
*leaf
;
2475 struct btrfs_item
*item
;
2483 leaf
= path
->nodes
[0];
2484 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2486 for (i
= 0; i
< nr
; i
++)
2487 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2489 nritems
= btrfs_header_nritems(leaf
);
2491 if (slot
+ nr
!= nritems
) {
2493 int data_end
= leaf_data_end(root
, leaf
);
2495 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2497 btrfs_leaf_data(leaf
) + data_end
,
2498 last_off
- data_end
);
2500 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2503 item
= btrfs_item_nr(leaf
, i
);
2504 ioff
= btrfs_item_offset(leaf
, item
);
2505 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2508 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2509 btrfs_item_nr_offset(slot
+ nr
),
2510 sizeof(struct btrfs_item
) *
2511 (nritems
- slot
- nr
));
2513 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2516 /* delete the leaf if we've emptied it */
2518 if (leaf
== root
->node
) {
2519 btrfs_set_header_level(leaf
, 0);
2521 u64 root_gen
= btrfs_header_generation(path
->nodes
[1]);
2522 clean_tree_block(trans
, root
, leaf
);
2523 wait_on_tree_block_writeback(root
, leaf
);
2524 wret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
2527 wret
= btrfs_free_extent(trans
, root
,
2528 leaf
->start
, leaf
->len
,
2529 path
->nodes
[1]->start
,
2530 btrfs_header_owner(path
->nodes
[1]),
2536 int used
= leaf_space_used(leaf
, 0, nritems
);
2538 struct btrfs_disk_key disk_key
;
2540 btrfs_item_key(leaf
, &disk_key
, 0);
2541 wret
= fixup_low_keys(trans
, root
, path
,
2547 /* delete the leaf if it is mostly empty */
2548 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 4) {
2549 /* push_leaf_left fixes the path.
2550 * make sure the path still points to our leaf
2551 * for possible call to del_ptr below
2553 slot
= path
->slots
[1];
2554 extent_buffer_get(leaf
);
2556 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2557 if (wret
< 0 && wret
!= -ENOSPC
)
2560 if (path
->nodes
[0] == leaf
&&
2561 btrfs_header_nritems(leaf
)) {
2562 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2563 if (wret
< 0 && wret
!= -ENOSPC
)
2567 if (btrfs_header_nritems(leaf
) == 0) {
2569 u64 bytenr
= leaf
->start
;
2570 u32 blocksize
= leaf
->len
;
2572 root_gen
= btrfs_header_generation(
2575 clean_tree_block(trans
, root
, leaf
);
2576 wait_on_tree_block_writeback(root
, leaf
);
2578 wret
= del_ptr(trans
, root
, path
, 1, slot
);
2582 free_extent_buffer(leaf
);
2583 wret
= btrfs_free_extent(trans
, root
, bytenr
,
2584 blocksize
, path
->nodes
[1]->start
,
2585 btrfs_header_owner(path
->nodes
[1]),
2590 btrfs_mark_buffer_dirty(leaf
);
2591 free_extent_buffer(leaf
);
2594 btrfs_mark_buffer_dirty(leaf
);
2601 * walk up the tree as far as required to find the previous leaf.
2602 * returns 0 if it found something or 1 if there are no lesser leaves.
2603 * returns < 0 on io errors.
2605 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2609 struct extent_buffer
*c
;
2610 struct extent_buffer
*next
= NULL
;
2612 while(level
< BTRFS_MAX_LEVEL
) {
2613 if (!path
->nodes
[level
])
2616 slot
= path
->slots
[level
];
2617 c
= path
->nodes
[level
];
2620 if (level
== BTRFS_MAX_LEVEL
)
2627 free_extent_buffer(next
);
2629 next
= read_node_slot(root
, c
, slot
);
2632 path
->slots
[level
] = slot
;
2635 c
= path
->nodes
[level
];
2636 free_extent_buffer(c
);
2637 slot
= btrfs_header_nritems(next
);
2640 path
->nodes
[level
] = next
;
2641 path
->slots
[level
] = slot
;
2644 next
= read_node_slot(root
, next
, slot
);
2650 * walk up the tree as far as required to find the next leaf.
2651 * returns 0 if it found something or 1 if there are no greater leaves.
2652 * returns < 0 on io errors.
2654 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2658 struct extent_buffer
*c
;
2659 struct extent_buffer
*next
= NULL
;
2661 while(level
< BTRFS_MAX_LEVEL
) {
2662 if (!path
->nodes
[level
])
2665 slot
= path
->slots
[level
] + 1;
2666 c
= path
->nodes
[level
];
2667 if (slot
>= btrfs_header_nritems(c
)) {
2669 if (level
== BTRFS_MAX_LEVEL
)
2675 free_extent_buffer(next
);
2678 reada_for_search(root
, path
, level
, slot
, 0);
2680 next
= read_node_slot(root
, c
, slot
);
2683 path
->slots
[level
] = slot
;
2686 c
= path
->nodes
[level
];
2687 free_extent_buffer(c
);
2688 path
->nodes
[level
] = next
;
2689 path
->slots
[level
] = 0;
2693 reada_for_search(root
, path
, level
, 0, 0);
2694 next
= read_node_slot(root
, next
, 0);
2699 int btrfs_previous_item(struct btrfs_root
*root
,
2700 struct btrfs_path
*path
, u64 min_objectid
,
2703 struct btrfs_key found_key
;
2704 struct extent_buffer
*leaf
;
2708 if (path
->slots
[0] == 0) {
2709 ret
= btrfs_prev_leaf(root
, path
);
2715 leaf
= path
->nodes
[0];
2716 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2717 if (found_key
.type
== type
)