1 #include <linux/module.h>
4 #include "transaction.h"
6 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
7 *root
, struct btrfs_path
*path
, int level
);
8 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
9 *root
, struct btrfs_path
*path
, int data_size
);
10 static int push_node_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
11 *root
, struct buffer_head
*dst
, struct buffer_head
13 static int balance_node_right(struct btrfs_trans_handle
*trans
, struct
14 btrfs_root
*root
, struct buffer_head
*dst_buf
,
15 struct buffer_head
*src_buf
);
16 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
17 struct btrfs_path
*path
, int level
, int slot
);
19 inline void btrfs_init_path(struct btrfs_path
*p
)
21 memset(p
, 0, sizeof(*p
));
24 struct btrfs_path
*btrfs_alloc_path(void)
26 struct btrfs_path
*path
;
27 path
= kmem_cache_alloc(btrfs_path_cachep
, GFP_NOFS
);
29 btrfs_init_path(path
);
33 void btrfs_free_path(struct btrfs_path
*p
)
35 btrfs_release_path(NULL
, p
);
36 kmem_cache_free(btrfs_path_cachep
, p
);
39 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
42 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
45 btrfs_block_release(root
, p
->nodes
[i
]);
47 memset(p
, 0, sizeof(*p
));
50 static int btrfs_cow_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
51 *root
, struct buffer_head
*buf
, struct buffer_head
52 *parent
, int parent_slot
, struct buffer_head
55 struct buffer_head
*cow
;
56 struct btrfs_node
*cow_node
;
58 if (btrfs_header_generation(btrfs_buffer_header(buf
)) ==
63 cow
= btrfs_alloc_free_block(trans
, root
);
64 cow_node
= btrfs_buffer_node(cow
);
65 if (buf
->b_size
!= root
->blocksize
|| cow
->b_size
!= root
->blocksize
)
67 memcpy(cow_node
, btrfs_buffer_node(buf
), root
->blocksize
);
68 btrfs_set_header_blocknr(&cow_node
->header
, cow
->b_blocknr
);
69 btrfs_set_header_generation(&cow_node
->header
, trans
->transid
);
70 btrfs_inc_ref(trans
, root
, buf
);
71 if (buf
== root
->node
) {
74 if (buf
!= root
->commit_root
) {
75 btrfs_free_extent(trans
, root
, buf
->b_blocknr
, 1, 1);
77 btrfs_block_release(root
, buf
);
79 btrfs_set_node_blockptr(btrfs_buffer_node(parent
), parent_slot
,
81 btrfs_mark_buffer_dirty(parent
);
82 btrfs_free_extent(trans
, root
, buf
->b_blocknr
, 1, 1);
84 btrfs_block_release(root
, buf
);
85 mark_buffer_dirty(cow
);
91 * The leaf data grows from end-to-front in the node.
92 * this returns the address of the start of the last item,
93 * which is the stop of the leaf data stack
95 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
96 struct btrfs_leaf
*leaf
)
98 u32 nr
= btrfs_header_nritems(&leaf
->header
);
100 return BTRFS_LEAF_DATA_SIZE(root
);
101 return btrfs_item_offset(leaf
->items
+ nr
- 1);
105 * The space between the end of the leaf items and
106 * the start of the leaf data. IOW, how much room
107 * the leaf has left for both items and data
109 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct btrfs_leaf
*leaf
)
111 int data_end
= leaf_data_end(root
, leaf
);
112 int nritems
= btrfs_header_nritems(&leaf
->header
);
113 char *items_end
= (char *)(leaf
->items
+ nritems
+ 1);
114 return (char *)(btrfs_leaf_data(leaf
) + data_end
) - (char *)items_end
;
118 * compare two keys in a memcmp fashion
120 static int comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
124 btrfs_disk_key_to_cpu(&k1
, disk
);
126 if (k1
.objectid
> k2
->objectid
)
128 if (k1
.objectid
< k2
->objectid
)
130 if (k1
.offset
> k2
->offset
)
132 if (k1
.offset
< k2
->offset
)
134 if (k1
.flags
> k2
->flags
)
136 if (k1
.flags
< k2
->flags
)
141 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
145 struct btrfs_node
*parent
= NULL
;
146 struct btrfs_node
*node
= btrfs_buffer_node(path
->nodes
[level
]);
148 u32 nritems
= btrfs_header_nritems(&node
->header
);
150 if (path
->nodes
[level
+ 1])
151 parent
= btrfs_buffer_node(path
->nodes
[level
+ 1]);
152 parent_slot
= path
->slots
[level
+ 1];
153 BUG_ON(nritems
== 0);
155 struct btrfs_disk_key
*parent_key
;
156 parent_key
= &parent
->ptrs
[parent_slot
].key
;
157 BUG_ON(memcmp(parent_key
, &node
->ptrs
[0].key
,
158 sizeof(struct btrfs_disk_key
)));
159 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
160 btrfs_header_blocknr(&node
->header
));
162 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
163 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
164 struct btrfs_key cpukey
;
165 btrfs_disk_key_to_cpu(&cpukey
, &node
->ptrs
[i
+ 1].key
);
166 BUG_ON(comp_keys(&node
->ptrs
[i
].key
, &cpukey
) >= 0);
171 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
175 struct btrfs_leaf
*leaf
= btrfs_buffer_leaf(path
->nodes
[level
]);
176 struct btrfs_node
*parent
= NULL
;
178 u32 nritems
= btrfs_header_nritems(&leaf
->header
);
180 if (path
->nodes
[level
+ 1])
181 parent
= btrfs_buffer_node(path
->nodes
[level
+ 1]);
182 parent_slot
= path
->slots
[level
+ 1];
183 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < 0);
189 struct btrfs_disk_key
*parent_key
;
190 parent_key
= &parent
->ptrs
[parent_slot
].key
;
191 BUG_ON(memcmp(parent_key
, &leaf
->items
[0].key
,
192 sizeof(struct btrfs_disk_key
)));
193 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
194 btrfs_header_blocknr(&leaf
->header
));
196 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
197 struct btrfs_key cpukey
;
198 btrfs_disk_key_to_cpu(&cpukey
, &leaf
->items
[i
+ 1].key
);
199 BUG_ON(comp_keys(&leaf
->items
[i
].key
,
201 BUG_ON(btrfs_item_offset(leaf
->items
+ i
) !=
202 btrfs_item_end(leaf
->items
+ i
+ 1));
204 BUG_ON(btrfs_item_offset(leaf
->items
+ i
) +
205 btrfs_item_size(leaf
->items
+ i
) !=
206 BTRFS_LEAF_DATA_SIZE(root
));
212 static int check_block(struct btrfs_root
*root
, struct btrfs_path
*path
,
216 return check_leaf(root
, path
, level
);
217 return check_node(root
, path
, level
);
221 * search for key in the array p. items p are item_size apart
222 * and there are 'max' items in p
223 * the slot in the array is returned via slot, and it points to
224 * the place where you would insert key if it is not found in
227 * slot may point to max if the key is bigger than all of the keys
229 static int generic_bin_search(char *p
, int item_size
, struct btrfs_key
*key
,
236 struct btrfs_disk_key
*tmp
;
239 mid
= (low
+ high
) / 2;
240 tmp
= (struct btrfs_disk_key
*)(p
+ mid
* item_size
);
241 ret
= comp_keys(tmp
, key
);
257 * simple bin_search frontend that does the right thing for
260 static int bin_search(struct btrfs_node
*c
, struct btrfs_key
*key
, int *slot
)
262 if (btrfs_is_leaf(c
)) {
263 struct btrfs_leaf
*l
= (struct btrfs_leaf
*)c
;
264 return generic_bin_search((void *)l
->items
,
265 sizeof(struct btrfs_item
),
266 key
, btrfs_header_nritems(&c
->header
),
269 return generic_bin_search((void *)c
->ptrs
,
270 sizeof(struct btrfs_key_ptr
),
271 key
, btrfs_header_nritems(&c
->header
),
277 static struct buffer_head
*read_node_slot(struct btrfs_root
*root
,
278 struct buffer_head
*parent_buf
,
281 struct btrfs_node
*node
= btrfs_buffer_node(parent_buf
);
284 if (slot
>= btrfs_header_nritems(&node
->header
))
286 return read_tree_block(root
, btrfs_node_blockptr(node
, slot
));
289 static int balance_level(struct btrfs_trans_handle
*trans
, struct btrfs_root
290 *root
, struct btrfs_path
*path
, int level
)
292 struct buffer_head
*right_buf
;
293 struct buffer_head
*mid_buf
;
294 struct buffer_head
*left_buf
;
295 struct buffer_head
*parent_buf
= NULL
;
296 struct btrfs_node
*right
= NULL
;
297 struct btrfs_node
*mid
;
298 struct btrfs_node
*left
= NULL
;
299 struct btrfs_node
*parent
= NULL
;
303 int orig_slot
= path
->slots
[level
];
309 mid_buf
= path
->nodes
[level
];
310 mid
= btrfs_buffer_node(mid_buf
);
311 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
313 if (level
< BTRFS_MAX_LEVEL
- 1)
314 parent_buf
= path
->nodes
[level
+ 1];
315 pslot
= path
->slots
[level
+ 1];
318 * deal with the case where there is only one pointer in the root
319 * by promoting the node below to a root
322 struct buffer_head
*child
;
323 u64 blocknr
= mid_buf
->b_blocknr
;
325 if (btrfs_header_nritems(&mid
->header
) != 1)
328 /* promote the child to a root */
329 child
= read_node_slot(root
, mid_buf
, 0);
332 path
->nodes
[level
] = NULL
;
333 clean_tree_block(trans
, root
, mid_buf
);
334 wait_on_buffer(mid_buf
);
335 /* once for the path */
336 btrfs_block_release(root
, mid_buf
);
337 /* once for the root ptr */
338 btrfs_block_release(root
, mid_buf
);
339 return btrfs_free_extent(trans
, root
, blocknr
, 1, 1);
341 parent
= btrfs_buffer_node(parent_buf
);
343 if (btrfs_header_nritems(&mid
->header
) >
344 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
347 left_buf
= read_node_slot(root
, parent_buf
, pslot
- 1);
348 right_buf
= read_node_slot(root
, parent_buf
, pslot
+ 1);
350 /* first, try to make some room in the middle buffer */
352 btrfs_cow_block(trans
, root
, left_buf
, parent_buf
, pslot
- 1,
354 left
= btrfs_buffer_node(left_buf
);
355 orig_slot
+= btrfs_header_nritems(&left
->header
);
356 wret
= push_node_left(trans
, root
, left_buf
, mid_buf
);
362 * then try to empty the right most buffer into the middle
365 btrfs_cow_block(trans
, root
, right_buf
, parent_buf
, pslot
+ 1,
367 right
= btrfs_buffer_node(right_buf
);
368 wret
= push_node_left(trans
, root
, mid_buf
, right_buf
);
371 if (btrfs_header_nritems(&right
->header
) == 0) {
372 u64 blocknr
= right_buf
->b_blocknr
;
373 clean_tree_block(trans
, root
, right_buf
);
374 wait_on_buffer(right_buf
);
375 btrfs_block_release(root
, right_buf
);
378 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
382 wret
= btrfs_free_extent(trans
, root
, blocknr
, 1, 1);
386 btrfs_memcpy(root
, parent
,
387 &parent
->ptrs
[pslot
+ 1].key
,
389 sizeof(struct btrfs_disk_key
));
390 btrfs_mark_buffer_dirty(parent_buf
);
393 if (btrfs_header_nritems(&mid
->header
) == 1) {
395 * we're not allowed to leave a node with one item in the
396 * tree during a delete. A deletion from lower in the tree
397 * could try to delete the only pointer in this node.
398 * So, pull some keys from the left.
399 * There has to be a left pointer at this point because
400 * otherwise we would have pulled some pointers from the
404 wret
= balance_node_right(trans
, root
, mid_buf
, left_buf
);
409 if (btrfs_header_nritems(&mid
->header
) == 0) {
410 /* we've managed to empty the middle node, drop it */
411 u64 blocknr
= mid_buf
->b_blocknr
;
412 clean_tree_block(trans
, root
, mid_buf
);
413 wait_on_buffer(mid_buf
);
414 btrfs_block_release(root
, mid_buf
);
417 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
420 wret
= btrfs_free_extent(trans
, root
, blocknr
, 1, 1);
424 /* update the parent key to reflect our changes */
425 btrfs_memcpy(root
, parent
,
426 &parent
->ptrs
[pslot
].key
, &mid
->ptrs
[0].key
,
427 sizeof(struct btrfs_disk_key
));
428 btrfs_mark_buffer_dirty(parent_buf
);
431 /* update the path */
433 if (btrfs_header_nritems(&left
->header
) > orig_slot
) {
435 path
->nodes
[level
] = left_buf
;
436 path
->slots
[level
+ 1] -= 1;
437 path
->slots
[level
] = orig_slot
;
439 btrfs_block_release(root
, mid_buf
);
441 orig_slot
-= btrfs_header_nritems(&left
->header
);
442 path
->slots
[level
] = orig_slot
;
445 /* double check we haven't messed things up */
446 check_block(root
, path
, level
);
448 btrfs_node_blockptr(btrfs_buffer_node(path
->nodes
[level
]),
453 btrfs_block_release(root
, right_buf
);
455 btrfs_block_release(root
, left_buf
);
460 * look for key in the tree. path is filled in with nodes along the way
461 * if key is found, we return zero and you can find the item in the leaf
462 * level of the path (level 0)
464 * If the key isn't found, the path points to the slot where it should
465 * be inserted, and 1 is returned. If there are other errors during the
466 * search a negative error number is returned.
468 * if ins_len > 0, nodes and leaves will be split as we walk down the
469 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
472 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
473 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
476 struct buffer_head
*b
;
477 struct buffer_head
*cow_buf
;
478 struct btrfs_node
*c
;
483 WARN_ON(p
->nodes
[0] != NULL
);
484 WARN_ON(!mutex_is_locked(&root
->fs_info
->fs_mutex
));
489 c
= btrfs_buffer_node(b
);
490 level
= btrfs_header_level(&c
->header
);
493 wret
= btrfs_cow_block(trans
, root
, b
,
498 c
= btrfs_buffer_node(b
);
500 BUG_ON(!cow
&& ins_len
);
501 if (level
!= btrfs_header_level(&c
->header
))
503 level
= btrfs_header_level(&c
->header
);
505 ret
= check_block(root
, p
, level
);
508 ret
= bin_search(c
, key
, &slot
);
509 if (!btrfs_is_leaf(c
)) {
512 p
->slots
[level
] = slot
;
513 if (ins_len
> 0 && btrfs_header_nritems(&c
->header
) ==
514 BTRFS_NODEPTRS_PER_BLOCK(root
)) {
515 int sret
= split_node(trans
, root
, p
, level
);
520 c
= btrfs_buffer_node(b
);
521 slot
= p
->slots
[level
];
522 } else if (ins_len
< 0) {
523 int sret
= balance_level(trans
, root
, p
,
530 c
= btrfs_buffer_node(b
);
531 slot
= p
->slots
[level
];
532 BUG_ON(btrfs_header_nritems(&c
->header
) == 1);
534 b
= read_tree_block(root
, btrfs_node_blockptr(c
, slot
));
536 struct btrfs_leaf
*l
= (struct btrfs_leaf
*)c
;
537 p
->slots
[level
] = slot
;
538 if (ins_len
> 0 && btrfs_leaf_free_space(root
, l
) <
539 sizeof(struct btrfs_item
) + ins_len
) {
540 int sret
= split_leaf(trans
, root
, p
, ins_len
);
552 * adjust the pointers going up the tree, starting at level
553 * making sure the right key of each node is points to 'key'.
554 * This is used after shifting pointers to the left, so it stops
555 * fixing up pointers when a given leaf/node is not in slot 0 of the
558 * If this fails to write a tree block, it returns -1, but continues
559 * fixing up the blocks in ram so the tree is consistent.
561 static int fixup_low_keys(struct btrfs_trans_handle
*trans
, struct btrfs_root
562 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
567 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
568 struct btrfs_node
*t
;
569 int tslot
= path
->slots
[i
];
572 t
= btrfs_buffer_node(path
->nodes
[i
]);
573 btrfs_memcpy(root
, t
, &t
->ptrs
[tslot
].key
, key
, sizeof(*key
));
574 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
582 * try to push data from one node into the next node left in the
585 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
586 * error, and > 0 if there was no room in the left hand block.
588 static int push_node_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
589 *root
, struct buffer_head
*dst_buf
, struct
590 buffer_head
*src_buf
)
592 struct btrfs_node
*src
= btrfs_buffer_node(src_buf
);
593 struct btrfs_node
*dst
= btrfs_buffer_node(dst_buf
);
599 src_nritems
= btrfs_header_nritems(&src
->header
);
600 dst_nritems
= btrfs_header_nritems(&dst
->header
);
601 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
602 if (push_items
<= 0) {
606 if (src_nritems
< push_items
)
607 push_items
= src_nritems
;
609 btrfs_memcpy(root
, dst
, dst
->ptrs
+ dst_nritems
, src
->ptrs
,
610 push_items
* sizeof(struct btrfs_key_ptr
));
611 if (push_items
< src_nritems
) {
612 btrfs_memmove(root
, src
, src
->ptrs
, src
->ptrs
+ push_items
,
613 (src_nritems
- push_items
) *
614 sizeof(struct btrfs_key_ptr
));
616 btrfs_set_header_nritems(&src
->header
, src_nritems
- push_items
);
617 btrfs_set_header_nritems(&dst
->header
, dst_nritems
+ push_items
);
618 btrfs_mark_buffer_dirty(src_buf
);
619 btrfs_mark_buffer_dirty(dst_buf
);
624 * try to push data from one node into the next node right in the
627 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
628 * error, and > 0 if there was no room in the right hand block.
630 * this will only push up to 1/2 the contents of the left node over
632 static int balance_node_right(struct btrfs_trans_handle
*trans
, struct
633 btrfs_root
*root
, struct buffer_head
*dst_buf
,
634 struct buffer_head
*src_buf
)
636 struct btrfs_node
*src
= btrfs_buffer_node(src_buf
);
637 struct btrfs_node
*dst
= btrfs_buffer_node(dst_buf
);
644 src_nritems
= btrfs_header_nritems(&src
->header
);
645 dst_nritems
= btrfs_header_nritems(&dst
->header
);
646 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
647 if (push_items
<= 0) {
651 max_push
= src_nritems
/ 2 + 1;
652 /* don't try to empty the node */
653 if (max_push
> src_nritems
)
655 if (max_push
< push_items
)
656 push_items
= max_push
;
658 btrfs_memmove(root
, dst
, dst
->ptrs
+ push_items
, dst
->ptrs
,
659 dst_nritems
* sizeof(struct btrfs_key_ptr
));
661 btrfs_memcpy(root
, dst
, dst
->ptrs
,
662 src
->ptrs
+ src_nritems
- push_items
,
663 push_items
* sizeof(struct btrfs_key_ptr
));
665 btrfs_set_header_nritems(&src
->header
, src_nritems
- push_items
);
666 btrfs_set_header_nritems(&dst
->header
, dst_nritems
+ push_items
);
668 btrfs_mark_buffer_dirty(src_buf
);
669 btrfs_mark_buffer_dirty(dst_buf
);
674 * helper function to insert a new root level in the tree.
675 * A new node is allocated, and a single item is inserted to
676 * point to the existing root
678 * returns zero on success or < 0 on failure.
680 static int insert_new_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
681 *root
, struct btrfs_path
*path
, int level
)
683 struct buffer_head
*t
;
684 struct btrfs_node
*lower
;
685 struct btrfs_node
*c
;
686 struct btrfs_disk_key
*lower_key
;
688 BUG_ON(path
->nodes
[level
]);
689 BUG_ON(path
->nodes
[level
-1] != root
->node
);
691 t
= btrfs_alloc_free_block(trans
, root
);
692 c
= btrfs_buffer_node(t
);
693 memset(c
, 0, root
->blocksize
);
694 btrfs_set_header_nritems(&c
->header
, 1);
695 btrfs_set_header_level(&c
->header
, level
);
696 btrfs_set_header_blocknr(&c
->header
, t
->b_blocknr
);
697 btrfs_set_header_generation(&c
->header
, trans
->transid
);
698 btrfs_set_header_parentid(&c
->header
,
699 btrfs_header_parentid(btrfs_buffer_header(root
->node
)));
700 lower
= btrfs_buffer_node(path
->nodes
[level
-1]);
701 if (btrfs_is_leaf(lower
))
702 lower_key
= &((struct btrfs_leaf
*)lower
)->items
[0].key
;
704 lower_key
= &lower
->ptrs
[0].key
;
705 btrfs_memcpy(root
, c
, &c
->ptrs
[0].key
, lower_key
,
706 sizeof(struct btrfs_disk_key
));
707 btrfs_set_node_blockptr(c
, 0, path
->nodes
[level
- 1]->b_blocknr
);
709 btrfs_mark_buffer_dirty(t
);
711 /* the super has an extra ref to root->node */
712 btrfs_block_release(root
, root
->node
);
715 path
->nodes
[level
] = t
;
716 path
->slots
[level
] = 0;
721 * worker function to insert a single pointer in a node.
722 * the node should have enough room for the pointer already
724 * slot and level indicate where you want the key to go, and
725 * blocknr is the block the key points to.
727 * returns zero on success and < 0 on any error
729 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
730 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
731 *key
, u64 blocknr
, int slot
, int level
)
733 struct btrfs_node
*lower
;
736 BUG_ON(!path
->nodes
[level
]);
737 lower
= btrfs_buffer_node(path
->nodes
[level
]);
738 nritems
= btrfs_header_nritems(&lower
->header
);
741 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
743 if (slot
!= nritems
) {
744 btrfs_memmove(root
, lower
, lower
->ptrs
+ slot
+ 1,
746 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
748 btrfs_memcpy(root
, lower
, &lower
->ptrs
[slot
].key
,
749 key
, sizeof(struct btrfs_disk_key
));
750 btrfs_set_node_blockptr(lower
, slot
, blocknr
);
751 btrfs_set_header_nritems(&lower
->header
, nritems
+ 1);
752 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
757 * split the node at the specified level in path in two.
758 * The path is corrected to point to the appropriate node after the split
760 * Before splitting this tries to make some room in the node by pushing
761 * left and right, if either one works, it returns right away.
763 * returns 0 on success and < 0 on failure
765 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
766 *root
, struct btrfs_path
*path
, int level
)
768 struct buffer_head
*t
;
769 struct btrfs_node
*c
;
770 struct buffer_head
*split_buffer
;
771 struct btrfs_node
*split
;
777 t
= path
->nodes
[level
];
778 c
= btrfs_buffer_node(t
);
779 if (t
== root
->node
) {
780 /* trying to split the root, lets make a new one */
781 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
785 c_nritems
= btrfs_header_nritems(&c
->header
);
786 split_buffer
= btrfs_alloc_free_block(trans
, root
);
787 split
= btrfs_buffer_node(split_buffer
);
788 btrfs_set_header_flags(&split
->header
, btrfs_header_flags(&c
->header
));
789 btrfs_set_header_level(&split
->header
, btrfs_header_level(&c
->header
));
790 btrfs_set_header_blocknr(&split
->header
, split_buffer
->b_blocknr
);
791 btrfs_set_header_generation(&split
->header
, trans
->transid
);
792 btrfs_set_header_parentid(&split
->header
,
793 btrfs_header_parentid(btrfs_buffer_header(root
->node
)));
794 mid
= (c_nritems
+ 1) / 2;
795 btrfs_memcpy(root
, split
, split
->ptrs
, c
->ptrs
+ mid
,
796 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
797 btrfs_set_header_nritems(&split
->header
, c_nritems
- mid
);
798 btrfs_set_header_nritems(&c
->header
, mid
);
801 btrfs_mark_buffer_dirty(t
);
802 btrfs_mark_buffer_dirty(split_buffer
);
803 wret
= insert_ptr(trans
, root
, path
, &split
->ptrs
[0].key
,
804 split_buffer
->b_blocknr
, path
->slots
[level
+ 1] + 1,
809 if (path
->slots
[level
] >= mid
) {
810 path
->slots
[level
] -= mid
;
811 btrfs_block_release(root
, t
);
812 path
->nodes
[level
] = split_buffer
;
813 path
->slots
[level
+ 1] += 1;
815 btrfs_block_release(root
, split_buffer
);
821 * how many bytes are required to store the items in a leaf. start
822 * and nr indicate which items in the leaf to check. This totals up the
823 * space used both by the item structs and the item data
825 static int leaf_space_used(struct btrfs_leaf
*l
, int start
, int nr
)
828 int end
= start
+ nr
- 1;
832 data_len
= btrfs_item_end(l
->items
+ start
);
833 data_len
= data_len
- btrfs_item_offset(l
->items
+ end
);
834 data_len
+= sizeof(struct btrfs_item
) * nr
;
839 * push some data in the path leaf to the right, trying to free up at
840 * least data_size bytes. returns zero if the push worked, nonzero otherwise
842 * returns 1 if the push failed because the other node didn't have enough
843 * room, 0 if everything worked out and < 0 if there were major errors.
845 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
846 *root
, struct btrfs_path
*path
, int data_size
)
848 struct buffer_head
*left_buf
= path
->nodes
[0];
849 struct btrfs_leaf
*left
= btrfs_buffer_leaf(left_buf
);
850 struct btrfs_leaf
*right
;
851 struct buffer_head
*right_buf
;
852 struct buffer_head
*upper
;
853 struct btrfs_node
*upper_node
;
859 struct btrfs_item
*item
;
863 slot
= path
->slots
[1];
864 if (!path
->nodes
[1]) {
867 upper
= path
->nodes
[1];
868 upper_node
= btrfs_buffer_node(upper
);
869 if (slot
>= btrfs_header_nritems(&upper_node
->header
) - 1) {
872 right_buf
= read_tree_block(root
,
873 btrfs_node_blockptr(btrfs_buffer_node(upper
), slot
+ 1));
874 right
= btrfs_buffer_leaf(right_buf
);
875 free_space
= btrfs_leaf_free_space(root
, right
);
876 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
877 btrfs_block_release(root
, right_buf
);
880 /* cow and double check */
881 btrfs_cow_block(trans
, root
, right_buf
, upper
, slot
+ 1, &right_buf
);
882 right
= btrfs_buffer_leaf(right_buf
);
883 free_space
= btrfs_leaf_free_space(root
, right
);
884 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
885 btrfs_block_release(root
, right_buf
);
889 left_nritems
= btrfs_header_nritems(&left
->header
);
890 for (i
= left_nritems
- 1; i
>= 0; i
--) {
891 item
= left
->items
+ i
;
892 if (path
->slots
[0] == i
)
893 push_space
+= data_size
+ sizeof(*item
);
894 if (btrfs_item_size(item
) + sizeof(*item
) + push_space
>
898 push_space
+= btrfs_item_size(item
) + sizeof(*item
);
900 if (push_items
== 0) {
901 btrfs_block_release(root
, right_buf
);
904 right_nritems
= btrfs_header_nritems(&right
->header
);
905 /* push left to right */
906 push_space
= btrfs_item_end(left
->items
+ left_nritems
- push_items
);
907 push_space
-= leaf_data_end(root
, left
);
908 /* make room in the right data area */
909 btrfs_memmove(root
, right
, btrfs_leaf_data(right
) +
910 leaf_data_end(root
, right
) - push_space
,
911 btrfs_leaf_data(right
) +
912 leaf_data_end(root
, right
), BTRFS_LEAF_DATA_SIZE(root
) -
913 leaf_data_end(root
, right
));
914 /* copy from the left data area */
915 btrfs_memcpy(root
, right
, btrfs_leaf_data(right
) +
916 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
917 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
919 btrfs_memmove(root
, right
, right
->items
+ push_items
, right
->items
,
920 right_nritems
* sizeof(struct btrfs_item
));
921 /* copy the items from left to right */
922 btrfs_memcpy(root
, right
, right
->items
, left
->items
+
923 left_nritems
- push_items
,
924 push_items
* sizeof(struct btrfs_item
));
926 /* update the item pointers */
927 right_nritems
+= push_items
;
928 btrfs_set_header_nritems(&right
->header
, right_nritems
);
929 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
930 for (i
= 0; i
< right_nritems
; i
++) {
931 btrfs_set_item_offset(right
->items
+ i
, push_space
-
932 btrfs_item_size(right
->items
+ i
));
933 push_space
= btrfs_item_offset(right
->items
+ i
);
935 left_nritems
-= push_items
;
936 btrfs_set_header_nritems(&left
->header
, left_nritems
);
938 btrfs_mark_buffer_dirty(left_buf
);
939 btrfs_mark_buffer_dirty(right_buf
);
940 btrfs_memcpy(root
, upper_node
, &upper_node
->ptrs
[slot
+ 1].key
,
941 &right
->items
[0].key
, sizeof(struct btrfs_disk_key
));
942 btrfs_mark_buffer_dirty(upper
);
944 /* then fixup the leaf pointer in the path */
945 if (path
->slots
[0] >= left_nritems
) {
946 path
->slots
[0] -= left_nritems
;
947 btrfs_block_release(root
, path
->nodes
[0]);
948 path
->nodes
[0] = right_buf
;
951 btrfs_block_release(root
, right_buf
);
956 * push some data in the path leaf to the left, trying to free up at
957 * least data_size bytes. returns zero if the push worked, nonzero otherwise
959 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
960 *root
, struct btrfs_path
*path
, int data_size
)
962 struct buffer_head
*right_buf
= path
->nodes
[0];
963 struct btrfs_leaf
*right
= btrfs_buffer_leaf(right_buf
);
964 struct buffer_head
*t
;
965 struct btrfs_leaf
*left
;
971 struct btrfs_item
*item
;
972 u32 old_left_nritems
;
976 slot
= path
->slots
[1];
980 if (!path
->nodes
[1]) {
983 t
= read_tree_block(root
,
984 btrfs_node_blockptr(btrfs_buffer_node(path
->nodes
[1]), slot
- 1));
985 left
= btrfs_buffer_leaf(t
);
986 free_space
= btrfs_leaf_free_space(root
, left
);
987 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
988 btrfs_block_release(root
, t
);
992 /* cow and double check */
993 btrfs_cow_block(trans
, root
, t
, path
->nodes
[1], slot
- 1, &t
);
994 left
= btrfs_buffer_leaf(t
);
995 free_space
= btrfs_leaf_free_space(root
, left
);
996 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
997 btrfs_block_release(root
, t
);
1001 for (i
= 0; i
< btrfs_header_nritems(&right
->header
); i
++) {
1002 item
= right
->items
+ i
;
1003 if (path
->slots
[0] == i
)
1004 push_space
+= data_size
+ sizeof(*item
);
1005 if (btrfs_item_size(item
) + sizeof(*item
) + push_space
>
1009 push_space
+= btrfs_item_size(item
) + sizeof(*item
);
1011 if (push_items
== 0) {
1012 btrfs_block_release(root
, t
);
1015 /* push data from right to left */
1016 btrfs_memcpy(root
, left
, left
->items
+
1017 btrfs_header_nritems(&left
->header
),
1018 right
->items
, push_items
* sizeof(struct btrfs_item
));
1019 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1020 btrfs_item_offset(right
->items
+ push_items
-1);
1021 btrfs_memcpy(root
, left
, btrfs_leaf_data(left
) +
1022 leaf_data_end(root
, left
) - push_space
,
1023 btrfs_leaf_data(right
) +
1024 btrfs_item_offset(right
->items
+ push_items
- 1),
1026 old_left_nritems
= btrfs_header_nritems(&left
->header
);
1027 BUG_ON(old_left_nritems
< 0);
1029 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1030 u32 ioff
= btrfs_item_offset(left
->items
+ i
);
1031 btrfs_set_item_offset(left
->items
+ i
, ioff
-
1032 (BTRFS_LEAF_DATA_SIZE(root
) -
1033 btrfs_item_offset(left
->items
+
1034 old_left_nritems
- 1)));
1036 btrfs_set_header_nritems(&left
->header
, old_left_nritems
+ push_items
);
1038 /* fixup right node */
1039 push_space
= btrfs_item_offset(right
->items
+ push_items
- 1) -
1040 leaf_data_end(root
, right
);
1041 btrfs_memmove(root
, right
, btrfs_leaf_data(right
) +
1042 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1043 btrfs_leaf_data(right
) +
1044 leaf_data_end(root
, right
), push_space
);
1045 btrfs_memmove(root
, right
, right
->items
, right
->items
+ push_items
,
1046 (btrfs_header_nritems(&right
->header
) - push_items
) *
1047 sizeof(struct btrfs_item
));
1048 btrfs_set_header_nritems(&right
->header
,
1049 btrfs_header_nritems(&right
->header
) -
1051 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1053 for (i
= 0; i
< btrfs_header_nritems(&right
->header
); i
++) {
1054 btrfs_set_item_offset(right
->items
+ i
, push_space
-
1055 btrfs_item_size(right
->items
+ i
));
1056 push_space
= btrfs_item_offset(right
->items
+ i
);
1059 btrfs_mark_buffer_dirty(t
);
1060 btrfs_mark_buffer_dirty(right_buf
);
1062 wret
= fixup_low_keys(trans
, root
, path
, &right
->items
[0].key
, 1);
1066 /* then fixup the leaf pointer in the path */
1067 if (path
->slots
[0] < push_items
) {
1068 path
->slots
[0] += old_left_nritems
;
1069 btrfs_block_release(root
, path
->nodes
[0]);
1071 path
->slots
[1] -= 1;
1073 btrfs_block_release(root
, t
);
1074 path
->slots
[0] -= push_items
;
1076 BUG_ON(path
->slots
[0] < 0);
1081 * split the path's leaf in two, making sure there is at least data_size
1082 * available for the resulting leaf level of the path.
1084 * returns 0 if all went well and < 0 on failure.
1086 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
1087 *root
, struct btrfs_path
*path
, int data_size
)
1089 struct buffer_head
*l_buf
;
1090 struct btrfs_leaf
*l
;
1094 struct btrfs_leaf
*right
;
1095 struct buffer_head
*right_buffer
;
1096 int space_needed
= data_size
+ sizeof(struct btrfs_item
);
1103 /* first try to make some room by pushing left and right */
1104 wret
= push_leaf_left(trans
, root
, path
, data_size
);
1108 wret
= push_leaf_right(trans
, root
, path
, data_size
);
1112 l_buf
= path
->nodes
[0];
1113 l
= btrfs_buffer_leaf(l_buf
);
1115 /* did the pushes work? */
1116 if (btrfs_leaf_free_space(root
, l
) >=
1117 sizeof(struct btrfs_item
) + data_size
)
1120 if (!path
->nodes
[1]) {
1121 ret
= insert_new_root(trans
, root
, path
, 1);
1125 slot
= path
->slots
[0];
1126 nritems
= btrfs_header_nritems(&l
->header
);
1127 mid
= (nritems
+ 1)/ 2;
1128 right_buffer
= btrfs_alloc_free_block(trans
, root
);
1129 BUG_ON(!right_buffer
);
1130 BUG_ON(mid
== nritems
);
1131 right
= btrfs_buffer_leaf(right_buffer
);
1132 memset(&right
->header
, 0, sizeof(right
->header
));
1134 /* FIXME, just alloc a new leaf here */
1135 if (leaf_space_used(l
, mid
, nritems
- mid
) + space_needed
>
1136 BTRFS_LEAF_DATA_SIZE(root
))
1139 /* FIXME, just alloc a new leaf here */
1140 if (leaf_space_used(l
, 0, mid
+ 1) + space_needed
>
1141 BTRFS_LEAF_DATA_SIZE(root
))
1144 btrfs_set_header_nritems(&right
->header
, nritems
- mid
);
1145 btrfs_set_header_blocknr(&right
->header
, right_buffer
->b_blocknr
);
1146 btrfs_set_header_generation(&right
->header
, trans
->transid
);
1147 btrfs_set_header_level(&right
->header
, 0);
1148 btrfs_set_header_parentid(&right
->header
,
1149 btrfs_header_parentid(btrfs_buffer_header(root
->node
)));
1150 data_copy_size
= btrfs_item_end(l
->items
+ mid
) -
1151 leaf_data_end(root
, l
);
1152 btrfs_memcpy(root
, right
, right
->items
, l
->items
+ mid
,
1153 (nritems
- mid
) * sizeof(struct btrfs_item
));
1154 btrfs_memcpy(root
, right
,
1155 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
1156 data_copy_size
, btrfs_leaf_data(l
) +
1157 leaf_data_end(root
, l
), data_copy_size
);
1158 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
1159 btrfs_item_end(l
->items
+ mid
);
1161 for (i
= 0; i
< btrfs_header_nritems(&right
->header
); i
++) {
1162 u32 ioff
= btrfs_item_offset(right
->items
+ i
);
1163 btrfs_set_item_offset(right
->items
+ i
, ioff
+ rt_data_off
);
1166 btrfs_set_header_nritems(&l
->header
, mid
);
1168 wret
= insert_ptr(trans
, root
, path
, &right
->items
[0].key
,
1169 right_buffer
->b_blocknr
, path
->slots
[1] + 1, 1);
1172 btrfs_mark_buffer_dirty(right_buffer
);
1173 btrfs_mark_buffer_dirty(l_buf
);
1174 BUG_ON(path
->slots
[0] != slot
);
1176 btrfs_block_release(root
, path
->nodes
[0]);
1177 path
->nodes
[0] = right_buffer
;
1178 path
->slots
[0] -= mid
;
1179 path
->slots
[1] += 1;
1181 btrfs_block_release(root
, right_buffer
);
1182 BUG_ON(path
->slots
[0] < 0);
1187 * Given a key and some data, insert an item into the tree.
1188 * This does all the path init required, making room in the tree if needed.
1190 int btrfs_insert_empty_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
1191 *root
, struct btrfs_path
*path
, struct btrfs_key
1192 *cpu_key
, u32 data_size
)
1197 struct btrfs_leaf
*leaf
;
1198 struct buffer_head
*leaf_buf
;
1200 unsigned int data_end
;
1201 struct btrfs_disk_key disk_key
;
1203 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
1205 /* create a root if there isn't one */
1208 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, data_size
, 1);
1215 slot_orig
= path
->slots
[0];
1216 leaf_buf
= path
->nodes
[0];
1217 leaf
= btrfs_buffer_leaf(leaf_buf
);
1219 nritems
= btrfs_header_nritems(&leaf
->header
);
1220 data_end
= leaf_data_end(root
, leaf
);
1222 if (btrfs_leaf_free_space(root
, leaf
) <
1223 sizeof(struct btrfs_item
) + data_size
)
1226 slot
= path
->slots
[0];
1228 if (slot
!= nritems
) {
1230 unsigned int old_data
= btrfs_item_end(leaf
->items
+ slot
);
1233 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1235 /* first correct the data pointers */
1236 for (i
= slot
; i
< nritems
; i
++) {
1237 u32 ioff
= btrfs_item_offset(leaf
->items
+ i
);
1238 btrfs_set_item_offset(leaf
->items
+ i
,
1242 /* shift the items */
1243 btrfs_memmove(root
, leaf
, leaf
->items
+ slot
+ 1,
1245 (nritems
- slot
) * sizeof(struct btrfs_item
));
1247 /* shift the data */
1248 btrfs_memmove(root
, leaf
, btrfs_leaf_data(leaf
) +
1249 data_end
- data_size
, btrfs_leaf_data(leaf
) +
1250 data_end
, old_data
- data_end
);
1251 data_end
= old_data
;
1253 /* setup the item for the new data */
1254 btrfs_memcpy(root
, leaf
, &leaf
->items
[slot
].key
, &disk_key
,
1255 sizeof(struct btrfs_disk_key
));
1256 btrfs_set_item_offset(leaf
->items
+ slot
, data_end
- data_size
);
1257 btrfs_set_item_size(leaf
->items
+ slot
, data_size
);
1258 btrfs_set_header_nritems(&leaf
->header
, nritems
+ 1);
1259 btrfs_mark_buffer_dirty(leaf_buf
);
1263 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1265 if (btrfs_leaf_free_space(root
, leaf
) < 0)
1267 check_leaf(root
, path
, 0);
1273 * Given a key and some data, insert an item into the tree.
1274 * This does all the path init required, making room in the tree if needed.
1276 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
1277 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
1281 struct btrfs_path
*path
;
1284 path
= btrfs_alloc_path();
1286 btrfs_init_path(path
);
1287 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
1289 ptr
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1290 path
->slots
[0], u8
);
1291 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
1292 ptr
, data
, data_size
);
1293 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1295 btrfs_release_path(root
, path
);
1296 btrfs_free_path(path
);
1301 * delete the pointer from a given node.
1303 * If the delete empties a node, the node is removed from the tree,
1304 * continuing all the way the root if required. The root is converted into
1305 * a leaf if all the nodes are emptied.
1307 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1308 struct btrfs_path
*path
, int level
, int slot
)
1310 struct btrfs_node
*node
;
1311 struct buffer_head
*parent
= path
->nodes
[level
];
1316 node
= btrfs_buffer_node(parent
);
1317 nritems
= btrfs_header_nritems(&node
->header
);
1318 if (slot
!= nritems
-1) {
1319 btrfs_memmove(root
, node
, node
->ptrs
+ slot
,
1320 node
->ptrs
+ slot
+ 1,
1321 sizeof(struct btrfs_key_ptr
) *
1322 (nritems
- slot
- 1));
1325 btrfs_set_header_nritems(&node
->header
, nritems
);
1326 if (nritems
== 0 && parent
== root
->node
) {
1327 struct btrfs_header
*header
= btrfs_buffer_header(root
->node
);
1328 BUG_ON(btrfs_header_level(header
) != 1);
1329 /* just turn the root into a leaf and break */
1330 btrfs_set_header_level(header
, 0);
1331 } else if (slot
== 0) {
1332 wret
= fixup_low_keys(trans
, root
, path
, &node
->ptrs
[0].key
,
1337 btrfs_mark_buffer_dirty(parent
);
1342 * delete the item at the leaf level in path. If that empties
1343 * the leaf, remove it from the tree
1345 int btrfs_del_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1346 struct btrfs_path
*path
)
1349 struct btrfs_leaf
*leaf
;
1350 struct buffer_head
*leaf_buf
;
1357 leaf_buf
= path
->nodes
[0];
1358 leaf
= btrfs_buffer_leaf(leaf_buf
);
1359 slot
= path
->slots
[0];
1360 doff
= btrfs_item_offset(leaf
->items
+ slot
);
1361 dsize
= btrfs_item_size(leaf
->items
+ slot
);
1362 nritems
= btrfs_header_nritems(&leaf
->header
);
1364 if (slot
!= nritems
- 1) {
1366 int data_end
= leaf_data_end(root
, leaf
);
1367 btrfs_memmove(root
, leaf
, btrfs_leaf_data(leaf
) +
1369 btrfs_leaf_data(leaf
) + data_end
,
1371 for (i
= slot
+ 1; i
< nritems
; i
++) {
1372 u32 ioff
= btrfs_item_offset(leaf
->items
+ i
);
1373 btrfs_set_item_offset(leaf
->items
+ i
, ioff
+ dsize
);
1375 btrfs_memmove(root
, leaf
, leaf
->items
+ slot
,
1376 leaf
->items
+ slot
+ 1,
1377 sizeof(struct btrfs_item
) *
1378 (nritems
- slot
- 1));
1380 btrfs_set_header_nritems(&leaf
->header
, nritems
- 1);
1382 /* delete the leaf if we've emptied it */
1384 if (leaf_buf
== root
->node
) {
1385 btrfs_set_header_level(&leaf
->header
, 0);
1387 clean_tree_block(trans
, root
, leaf_buf
);
1388 wait_on_buffer(leaf_buf
);
1389 wret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
1392 wret
= btrfs_free_extent(trans
, root
,
1393 leaf_buf
->b_blocknr
, 1, 1);
1398 int used
= leaf_space_used(leaf
, 0, nritems
);
1400 wret
= fixup_low_keys(trans
, root
, path
,
1401 &leaf
->items
[0].key
, 1);
1406 /* delete the leaf if it is mostly empty */
1407 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
1408 /* push_leaf_left fixes the path.
1409 * make sure the path still points to our leaf
1410 * for possible call to del_ptr below
1412 slot
= path
->slots
[1];
1414 wret
= push_leaf_left(trans
, root
, path
, 1);
1417 if (path
->nodes
[0] == leaf_buf
&&
1418 btrfs_header_nritems(&leaf
->header
)) {
1419 wret
= push_leaf_right(trans
, root
, path
, 1);
1423 if (btrfs_header_nritems(&leaf
->header
) == 0) {
1424 u64 blocknr
= leaf_buf
->b_blocknr
;
1425 clean_tree_block(trans
, root
, leaf_buf
);
1426 wait_on_buffer(leaf_buf
);
1427 wret
= del_ptr(trans
, root
, path
, 1, slot
);
1430 btrfs_block_release(root
, leaf_buf
);
1431 wret
= btrfs_free_extent(trans
, root
, blocknr
,
1436 btrfs_mark_buffer_dirty(leaf_buf
);
1437 btrfs_block_release(root
, leaf_buf
);
1440 btrfs_mark_buffer_dirty(leaf_buf
);
1447 * walk up the tree as far as required to find the next leaf.
1448 * returns 0 if it found something or 1 if there are no greater leaves.
1449 * returns < 0 on io errors.
1451 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
1456 struct buffer_head
*c
;
1457 struct btrfs_node
*c_node
;
1458 struct buffer_head
*next
= NULL
;
1460 while(level
< BTRFS_MAX_LEVEL
) {
1461 if (!path
->nodes
[level
])
1463 slot
= path
->slots
[level
] + 1;
1464 c
= path
->nodes
[level
];
1465 c_node
= btrfs_buffer_node(c
);
1466 if (slot
>= btrfs_header_nritems(&c_node
->header
)) {
1470 blocknr
= btrfs_node_blockptr(c_node
, slot
);
1472 btrfs_block_release(root
, next
);
1473 next
= read_tree_block(root
, blocknr
);
1476 path
->slots
[level
] = slot
;
1479 c
= path
->nodes
[level
];
1480 btrfs_block_release(root
, c
);
1481 path
->nodes
[level
] = next
;
1482 path
->slots
[level
] = 0;
1485 next
= read_tree_block(root
,
1486 btrfs_node_blockptr(btrfs_buffer_node(next
), 0));
This page took 0.090148 seconds and 6 git commands to generate.