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.
21 #include "transaction.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
);
28 static int push_node_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
29 *root
, struct buffer_head
*dst
, struct buffer_head
31 static int balance_node_right(struct btrfs_trans_handle
*trans
, struct
32 btrfs_root
*root
, struct buffer_head
*dst_buf
,
33 struct buffer_head
*src_buf
);
34 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
35 struct btrfs_path
*path
, int level
, int slot
);
37 inline void btrfs_init_path(struct btrfs_path
*p
)
39 memset(p
, 0, sizeof(*p
));
42 struct btrfs_path
*btrfs_alloc_path(void)
44 struct btrfs_path
*path
;
45 path
= kmem_cache_alloc(btrfs_path_cachep
, GFP_NOFS
);
47 btrfs_init_path(path
);
51 void btrfs_free_path(struct btrfs_path
*p
)
53 btrfs_release_path(NULL
, p
);
54 kmem_cache_free(btrfs_path_cachep
, p
);
57 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
60 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
63 btrfs_block_release(root
, p
->nodes
[i
]);
65 memset(p
, 0, sizeof(*p
));
68 static int btrfs_cow_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
69 *root
, struct buffer_head
*buf
, struct buffer_head
70 *parent
, int parent_slot
, struct buffer_head
73 struct buffer_head
*cow
;
74 struct btrfs_node
*cow_node
;
77 WARN_ON(!buffer_uptodate(buf
));
78 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
79 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
80 root
->fs_info
->running_transaction
->transid
);
83 if (trans
->transid
!= root
->fs_info
->generation
) {
84 printk(KERN_CRIT
"trans %Lu running %Lu\n", trans
->transid
,
85 root
->fs_info
->generation
);
88 if (btrfs_header_generation(btrfs_buffer_header(buf
)) ==
93 cow
= btrfs_alloc_free_block(trans
, root
, buf
->b_blocknr
);
96 cow_node
= btrfs_buffer_node(cow
);
97 if (buf
->b_size
!= root
->blocksize
|| cow
->b_size
!= root
->blocksize
)
99 memcpy(cow_node
, btrfs_buffer_node(buf
), root
->blocksize
);
100 btrfs_set_header_blocknr(&cow_node
->header
, bh_blocknr(cow
));
101 btrfs_set_header_generation(&cow_node
->header
, trans
->transid
);
102 btrfs_set_header_owner(&cow_node
->header
, root
->root_key
.objectid
);
103 ret
= btrfs_inc_ref(trans
, root
, buf
);
106 if (buf
== root
->node
) {
109 if (buf
!= root
->commit_root
) {
110 btrfs_free_extent(trans
, root
, bh_blocknr(buf
), 1, 1);
112 btrfs_block_release(root
, buf
);
114 btrfs_set_node_blockptr(btrfs_buffer_node(parent
), parent_slot
,
116 btrfs_mark_buffer_dirty(parent
);
117 btrfs_free_extent(trans
, root
, bh_blocknr(buf
), 1, 1);
119 btrfs_block_release(root
, buf
);
120 btrfs_mark_buffer_dirty(cow
);
126 * The leaf data grows from end-to-front in the node.
127 * this returns the address of the start of the last item,
128 * which is the stop of the leaf data stack
130 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
131 struct btrfs_leaf
*leaf
)
133 u32 nr
= btrfs_header_nritems(&leaf
->header
);
135 return BTRFS_LEAF_DATA_SIZE(root
);
136 return btrfs_item_offset(leaf
->items
+ nr
- 1);
140 * compare two keys in a memcmp fashion
142 static int comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
146 btrfs_disk_key_to_cpu(&k1
, disk
);
148 if (k1
.objectid
> k2
->objectid
)
150 if (k1
.objectid
< k2
->objectid
)
152 if (k1
.flags
> k2
->flags
)
154 if (k1
.flags
< k2
->flags
)
156 if (k1
.offset
> k2
->offset
)
158 if (k1
.offset
< k2
->offset
)
163 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
166 struct btrfs_node
*parent
= NULL
;
167 struct btrfs_node
*node
= btrfs_buffer_node(path
->nodes
[level
]);
170 struct btrfs_key cpukey
;
171 u32 nritems
= btrfs_header_nritems(&node
->header
);
173 if (path
->nodes
[level
+ 1])
174 parent
= btrfs_buffer_node(path
->nodes
[level
+ 1]);
175 parent_slot
= path
->slots
[level
+ 1];
176 slot
= path
->slots
[level
];
177 BUG_ON(nritems
== 0);
179 struct btrfs_disk_key
*parent_key
;
180 parent_key
= &parent
->ptrs
[parent_slot
].key
;
181 BUG_ON(memcmp(parent_key
, &node
->ptrs
[0].key
,
182 sizeof(struct btrfs_disk_key
)));
183 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
184 btrfs_header_blocknr(&node
->header
));
186 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
188 btrfs_disk_key_to_cpu(&cpukey
, &node
->ptrs
[slot
- 1].key
);
189 BUG_ON(comp_keys(&node
->ptrs
[slot
].key
, &cpukey
) <= 0);
191 if (slot
< nritems
- 1) {
192 btrfs_disk_key_to_cpu(&cpukey
, &node
->ptrs
[slot
+ 1].key
);
193 BUG_ON(comp_keys(&node
->ptrs
[slot
].key
, &cpukey
) >= 0);
198 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
201 struct btrfs_leaf
*leaf
= btrfs_buffer_leaf(path
->nodes
[level
]);
202 struct btrfs_node
*parent
= NULL
;
204 int slot
= path
->slots
[0];
205 struct btrfs_key cpukey
;
207 u32 nritems
= btrfs_header_nritems(&leaf
->header
);
209 if (path
->nodes
[level
+ 1])
210 parent
= btrfs_buffer_node(path
->nodes
[level
+ 1]);
211 parent_slot
= path
->slots
[level
+ 1];
212 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < 0);
218 struct btrfs_disk_key
*parent_key
;
219 parent_key
= &parent
->ptrs
[parent_slot
].key
;
220 BUG_ON(memcmp(parent_key
, &leaf
->items
[0].key
,
221 sizeof(struct btrfs_disk_key
)));
222 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
223 btrfs_header_blocknr(&leaf
->header
));
226 btrfs_disk_key_to_cpu(&cpukey
, &leaf
->items
[slot
- 1].key
);
227 BUG_ON(comp_keys(&leaf
->items
[slot
].key
, &cpukey
) <= 0);
228 BUG_ON(btrfs_item_offset(leaf
->items
+ slot
- 1) !=
229 btrfs_item_end(leaf
->items
+ slot
));
231 if (slot
< nritems
- 1) {
232 btrfs_disk_key_to_cpu(&cpukey
, &leaf
->items
[slot
+ 1].key
);
233 BUG_ON(comp_keys(&leaf
->items
[slot
].key
, &cpukey
) >= 0);
234 BUG_ON(btrfs_item_offset(leaf
->items
+ slot
) !=
235 btrfs_item_end(leaf
->items
+ slot
+ 1));
237 BUG_ON(btrfs_item_offset(leaf
->items
) +
238 btrfs_item_size(leaf
->items
) != BTRFS_LEAF_DATA_SIZE(root
));
242 static int check_block(struct btrfs_root
*root
, struct btrfs_path
*path
,
245 struct btrfs_node
*node
= btrfs_buffer_node(path
->nodes
[level
]);
246 if (memcmp(node
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
247 sizeof(node
->header
.fsid
)))
250 return check_leaf(root
, path
, level
);
251 return check_node(root
, path
, level
);
255 * search for key in the array p. items p are item_size apart
256 * and there are 'max' items in p
257 * the slot in the array is returned via slot, and it points to
258 * the place where you would insert key if it is not found in
261 * slot may point to max if the key is bigger than all of the keys
263 static int generic_bin_search(char *p
, int item_size
, struct btrfs_key
*key
,
270 struct btrfs_disk_key
*tmp
;
273 mid
= (low
+ high
) / 2;
274 tmp
= (struct btrfs_disk_key
*)(p
+ mid
* item_size
);
275 ret
= comp_keys(tmp
, key
);
291 * simple bin_search frontend that does the right thing for
294 static int bin_search(struct btrfs_node
*c
, struct btrfs_key
*key
, int *slot
)
296 if (btrfs_is_leaf(c
)) {
297 struct btrfs_leaf
*l
= (struct btrfs_leaf
*)c
;
298 return generic_bin_search((void *)l
->items
,
299 sizeof(struct btrfs_item
),
300 key
, btrfs_header_nritems(&c
->header
),
303 return generic_bin_search((void *)c
->ptrs
,
304 sizeof(struct btrfs_key_ptr
),
305 key
, btrfs_header_nritems(&c
->header
),
311 static struct buffer_head
*read_node_slot(struct btrfs_root
*root
,
312 struct buffer_head
*parent_buf
,
315 struct btrfs_node
*node
= btrfs_buffer_node(parent_buf
);
318 if (slot
>= btrfs_header_nritems(&node
->header
))
320 return read_tree_block(root
, btrfs_node_blockptr(node
, slot
));
323 static int balance_level(struct btrfs_trans_handle
*trans
, struct btrfs_root
324 *root
, struct btrfs_path
*path
, int level
)
326 struct buffer_head
*right_buf
;
327 struct buffer_head
*mid_buf
;
328 struct buffer_head
*left_buf
;
329 struct buffer_head
*parent_buf
= NULL
;
330 struct btrfs_node
*right
= NULL
;
331 struct btrfs_node
*mid
;
332 struct btrfs_node
*left
= NULL
;
333 struct btrfs_node
*parent
= NULL
;
337 int orig_slot
= path
->slots
[level
];
338 int err_on_enospc
= 0;
344 mid_buf
= path
->nodes
[level
];
345 mid
= btrfs_buffer_node(mid_buf
);
346 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
348 if (level
< BTRFS_MAX_LEVEL
- 1)
349 parent_buf
= path
->nodes
[level
+ 1];
350 pslot
= path
->slots
[level
+ 1];
353 * deal with the case where there is only one pointer in the root
354 * by promoting the node below to a root
357 struct buffer_head
*child
;
358 u64 blocknr
= bh_blocknr(mid_buf
);
360 if (btrfs_header_nritems(&mid
->header
) != 1)
363 /* promote the child to a root */
364 child
= read_node_slot(root
, mid_buf
, 0);
367 path
->nodes
[level
] = NULL
;
368 clean_tree_block(trans
, root
, mid_buf
);
369 wait_on_buffer(mid_buf
);
370 /* once for the path */
371 btrfs_block_release(root
, mid_buf
);
372 /* once for the root ptr */
373 btrfs_block_release(root
, mid_buf
);
374 return btrfs_free_extent(trans
, root
, blocknr
, 1, 1);
376 parent
= btrfs_buffer_node(parent_buf
);
378 if (btrfs_header_nritems(&mid
->header
) >
379 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
382 if (btrfs_header_nritems(&mid
->header
) < 2)
385 left_buf
= read_node_slot(root
, parent_buf
, pslot
- 1);
386 right_buf
= read_node_slot(root
, parent_buf
, pslot
+ 1);
388 /* first, try to make some room in the middle buffer */
390 wret
= btrfs_cow_block(trans
, root
, left_buf
,
391 parent_buf
, pslot
- 1, &left_buf
);
396 left
= btrfs_buffer_node(left_buf
);
397 orig_slot
+= btrfs_header_nritems(&left
->header
);
398 wret
= push_node_left(trans
, root
, left_buf
, mid_buf
);
401 if (btrfs_header_nritems(&mid
->header
) < 2)
406 * then try to empty the right most buffer into the middle
409 wret
= btrfs_cow_block(trans
, root
, right_buf
,
410 parent_buf
, pslot
+ 1, &right_buf
);
416 right
= btrfs_buffer_node(right_buf
);
417 wret
= push_node_left(trans
, root
, mid_buf
, right_buf
);
418 if (wret
< 0 && wret
!= -ENOSPC
)
420 if (btrfs_header_nritems(&right
->header
) == 0) {
421 u64 blocknr
= bh_blocknr(right_buf
);
422 clean_tree_block(trans
, root
, right_buf
);
423 wait_on_buffer(right_buf
);
424 btrfs_block_release(root
, right_buf
);
427 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
431 wret
= btrfs_free_extent(trans
, root
, blocknr
, 1, 1);
435 btrfs_memcpy(root
, parent
,
436 &parent
->ptrs
[pslot
+ 1].key
,
438 sizeof(struct btrfs_disk_key
));
439 btrfs_mark_buffer_dirty(parent_buf
);
442 if (btrfs_header_nritems(&mid
->header
) == 1) {
444 * we're not allowed to leave a node with one item in the
445 * tree during a delete. A deletion from lower in the tree
446 * could try to delete the only pointer in this node.
447 * So, pull some keys from the left.
448 * There has to be a left pointer at this point because
449 * otherwise we would have pulled some pointers from the
453 wret
= balance_node_right(trans
, root
, mid_buf
, left_buf
);
460 if (btrfs_header_nritems(&mid
->header
) == 0) {
461 /* we've managed to empty the middle node, drop it */
462 u64 blocknr
= bh_blocknr(mid_buf
);
463 clean_tree_block(trans
, root
, mid_buf
);
464 wait_on_buffer(mid_buf
);
465 btrfs_block_release(root
, mid_buf
);
468 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
471 wret
= btrfs_free_extent(trans
, root
, blocknr
, 1, 1);
475 /* update the parent key to reflect our changes */
476 btrfs_memcpy(root
, parent
,
477 &parent
->ptrs
[pslot
].key
, &mid
->ptrs
[0].key
,
478 sizeof(struct btrfs_disk_key
));
479 btrfs_mark_buffer_dirty(parent_buf
);
482 /* update the path */
484 if (btrfs_header_nritems(&left
->header
) > orig_slot
) {
486 path
->nodes
[level
] = left_buf
;
487 path
->slots
[level
+ 1] -= 1;
488 path
->slots
[level
] = orig_slot
;
490 btrfs_block_release(root
, mid_buf
);
492 orig_slot
-= btrfs_header_nritems(&left
->header
);
493 path
->slots
[level
] = orig_slot
;
496 /* double check we haven't messed things up */
497 check_block(root
, path
, level
);
499 btrfs_node_blockptr(btrfs_buffer_node(path
->nodes
[level
]),
504 btrfs_block_release(root
, right_buf
);
506 btrfs_block_release(root
, left_buf
);
510 /* returns zero if the push worked, non-zero otherwise */
511 static int push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
512 struct btrfs_root
*root
,
513 struct btrfs_path
*path
, int level
)
515 struct buffer_head
*right_buf
;
516 struct buffer_head
*mid_buf
;
517 struct buffer_head
*left_buf
;
518 struct buffer_head
*parent_buf
= NULL
;
519 struct btrfs_node
*right
= NULL
;
520 struct btrfs_node
*mid
;
521 struct btrfs_node
*left
= NULL
;
522 struct btrfs_node
*parent
= NULL
;
526 int orig_slot
= path
->slots
[level
];
532 mid_buf
= path
->nodes
[level
];
533 mid
= btrfs_buffer_node(mid_buf
);
534 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
536 if (level
< BTRFS_MAX_LEVEL
- 1)
537 parent_buf
= path
->nodes
[level
+ 1];
538 pslot
= path
->slots
[level
+ 1];
542 parent
= btrfs_buffer_node(parent_buf
);
544 left_buf
= read_node_slot(root
, parent_buf
, pslot
- 1);
546 /* first, try to make some room in the middle buffer */
549 left
= btrfs_buffer_node(left_buf
);
550 left_nr
= btrfs_header_nritems(&left
->header
);
551 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
554 ret
= btrfs_cow_block(trans
, root
, left_buf
, parent_buf
,
555 pslot
- 1, &left_buf
);
559 left
= btrfs_buffer_node(left_buf
);
560 wret
= push_node_left(trans
, root
,
567 orig_slot
+= left_nr
;
568 btrfs_memcpy(root
, parent
,
569 &parent
->ptrs
[pslot
].key
,
571 sizeof(struct btrfs_disk_key
));
572 btrfs_mark_buffer_dirty(parent_buf
);
573 if (btrfs_header_nritems(&left
->header
) > orig_slot
) {
574 path
->nodes
[level
] = left_buf
;
575 path
->slots
[level
+ 1] -= 1;
576 path
->slots
[level
] = orig_slot
;
577 btrfs_block_release(root
, mid_buf
);
580 btrfs_header_nritems(&left
->header
);
581 path
->slots
[level
] = orig_slot
;
582 btrfs_block_release(root
, left_buf
);
584 check_node(root
, path
, level
);
587 btrfs_block_release(root
, left_buf
);
589 right_buf
= read_node_slot(root
, parent_buf
, pslot
+ 1);
592 * then try to empty the right most buffer into the middle
596 right
= btrfs_buffer_node(right_buf
);
597 right_nr
= btrfs_header_nritems(&right
->header
);
598 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
601 ret
= btrfs_cow_block(trans
, root
, right_buf
,
602 parent_buf
, pslot
+ 1,
607 right
= btrfs_buffer_node(right_buf
);
608 wret
= balance_node_right(trans
, root
,
615 btrfs_memcpy(root
, parent
,
616 &parent
->ptrs
[pslot
+ 1].key
,
618 sizeof(struct btrfs_disk_key
));
619 btrfs_mark_buffer_dirty(parent_buf
);
620 if (btrfs_header_nritems(&mid
->header
) <= orig_slot
) {
621 path
->nodes
[level
] = right_buf
;
622 path
->slots
[level
+ 1] += 1;
623 path
->slots
[level
] = orig_slot
-
624 btrfs_header_nritems(&mid
->header
);
625 btrfs_block_release(root
, mid_buf
);
627 btrfs_block_release(root
, right_buf
);
629 check_node(root
, path
, level
);
632 btrfs_block_release(root
, right_buf
);
634 check_node(root
, path
, level
);
639 * look for key in the tree. path is filled in with nodes along the way
640 * if key is found, we return zero and you can find the item in the leaf
641 * level of the path (level 0)
643 * If the key isn't found, the path points to the slot where it should
644 * be inserted, and 1 is returned. If there are other errors during the
645 * search a negative error number is returned.
647 * if ins_len > 0, nodes and leaves will be split as we walk down the
648 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
651 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
652 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
655 struct buffer_head
*b
;
656 struct buffer_head
*cow_buf
;
657 struct btrfs_node
*c
;
662 WARN_ON(p
->nodes
[0] != NULL
);
663 WARN_ON(!mutex_is_locked(&root
->fs_info
->fs_mutex
));
668 c
= btrfs_buffer_node(b
);
669 level
= btrfs_header_level(&c
->header
);
672 wret
= btrfs_cow_block(trans
, root
, b
,
677 btrfs_block_release(root
, cow_buf
);
681 c
= btrfs_buffer_node(b
);
683 BUG_ON(!cow
&& ins_len
);
684 if (level
!= btrfs_header_level(&c
->header
))
686 level
= btrfs_header_level(&c
->header
);
688 ret
= check_block(root
, p
, level
);
691 ret
= bin_search(c
, key
, &slot
);
692 if (!btrfs_is_leaf(c
)) {
695 p
->slots
[level
] = slot
;
696 if (ins_len
> 0 && btrfs_header_nritems(&c
->header
) >=
697 BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
698 int sret
= split_node(trans
, root
, p
, level
);
703 c
= btrfs_buffer_node(b
);
704 slot
= p
->slots
[level
];
705 } else if (ins_len
< 0) {
706 int sret
= balance_level(trans
, root
, p
,
713 c
= btrfs_buffer_node(b
);
714 slot
= p
->slots
[level
];
715 BUG_ON(btrfs_header_nritems(&c
->header
) == 1);
717 b
= read_tree_block(root
, btrfs_node_blockptr(c
, slot
));
719 struct btrfs_leaf
*l
= (struct btrfs_leaf
*)c
;
720 p
->slots
[level
] = slot
;
721 if (ins_len
> 0 && btrfs_leaf_free_space(root
, l
) <
722 sizeof(struct btrfs_item
) + ins_len
) {
723 int sret
= split_leaf(trans
, root
, key
,
736 * adjust the pointers going up the tree, starting at level
737 * making sure the right key of each node is points to 'key'.
738 * This is used after shifting pointers to the left, so it stops
739 * fixing up pointers when a given leaf/node is not in slot 0 of the
742 * If this fails to write a tree block, it returns -1, but continues
743 * fixing up the blocks in ram so the tree is consistent.
745 static int fixup_low_keys(struct btrfs_trans_handle
*trans
, struct btrfs_root
746 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
751 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
752 struct btrfs_node
*t
;
753 int tslot
= path
->slots
[i
];
756 t
= btrfs_buffer_node(path
->nodes
[i
]);
757 btrfs_memcpy(root
, t
, &t
->ptrs
[tslot
].key
, key
, sizeof(*key
));
758 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
766 * try to push data from one node into the next node left in the
769 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
770 * error, and > 0 if there was no room in the left hand block.
772 static int push_node_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
773 *root
, struct buffer_head
*dst_buf
, struct
774 buffer_head
*src_buf
)
776 struct btrfs_node
*src
= btrfs_buffer_node(src_buf
);
777 struct btrfs_node
*dst
= btrfs_buffer_node(dst_buf
);
783 src_nritems
= btrfs_header_nritems(&src
->header
);
784 dst_nritems
= btrfs_header_nritems(&dst
->header
);
785 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
787 if (push_items
<= 0) {
791 if (src_nritems
< push_items
)
792 push_items
= src_nritems
;
794 btrfs_memcpy(root
, dst
, dst
->ptrs
+ dst_nritems
, src
->ptrs
,
795 push_items
* sizeof(struct btrfs_key_ptr
));
796 if (push_items
< src_nritems
) {
797 btrfs_memmove(root
, src
, src
->ptrs
, src
->ptrs
+ push_items
,
798 (src_nritems
- push_items
) *
799 sizeof(struct btrfs_key_ptr
));
801 btrfs_set_header_nritems(&src
->header
, src_nritems
- push_items
);
802 btrfs_set_header_nritems(&dst
->header
, dst_nritems
+ push_items
);
803 btrfs_mark_buffer_dirty(src_buf
);
804 btrfs_mark_buffer_dirty(dst_buf
);
809 * try to push data from one node into the next node right in the
812 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
813 * error, and > 0 if there was no room in the right hand block.
815 * this will only push up to 1/2 the contents of the left node over
817 static int balance_node_right(struct btrfs_trans_handle
*trans
, struct
818 btrfs_root
*root
, struct buffer_head
*dst_buf
,
819 struct buffer_head
*src_buf
)
821 struct btrfs_node
*src
= btrfs_buffer_node(src_buf
);
822 struct btrfs_node
*dst
= btrfs_buffer_node(dst_buf
);
829 src_nritems
= btrfs_header_nritems(&src
->header
);
830 dst_nritems
= btrfs_header_nritems(&dst
->header
);
831 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
832 if (push_items
<= 0) {
836 max_push
= src_nritems
/ 2 + 1;
837 /* don't try to empty the node */
838 if (max_push
> src_nritems
)
840 if (max_push
< push_items
)
841 push_items
= max_push
;
843 btrfs_memmove(root
, dst
, dst
->ptrs
+ push_items
, dst
->ptrs
,
844 dst_nritems
* sizeof(struct btrfs_key_ptr
));
846 btrfs_memcpy(root
, dst
, dst
->ptrs
,
847 src
->ptrs
+ src_nritems
- push_items
,
848 push_items
* sizeof(struct btrfs_key_ptr
));
850 btrfs_set_header_nritems(&src
->header
, src_nritems
- push_items
);
851 btrfs_set_header_nritems(&dst
->header
, dst_nritems
+ push_items
);
853 btrfs_mark_buffer_dirty(src_buf
);
854 btrfs_mark_buffer_dirty(dst_buf
);
859 * helper function to insert a new root level in the tree.
860 * A new node is allocated, and a single item is inserted to
861 * point to the existing root
863 * returns zero on success or < 0 on failure.
865 static int insert_new_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
866 *root
, struct btrfs_path
*path
, int level
)
868 struct buffer_head
*t
;
869 struct btrfs_node
*lower
;
870 struct btrfs_node
*c
;
871 struct btrfs_disk_key
*lower_key
;
873 BUG_ON(path
->nodes
[level
]);
874 BUG_ON(path
->nodes
[level
-1] != root
->node
);
876 t
= btrfs_alloc_free_block(trans
, root
, root
->node
->b_blocknr
);
879 c
= btrfs_buffer_node(t
);
880 memset(c
, 0, root
->blocksize
);
881 btrfs_set_header_nritems(&c
->header
, 1);
882 btrfs_set_header_level(&c
->header
, level
);
883 btrfs_set_header_blocknr(&c
->header
, bh_blocknr(t
));
884 btrfs_set_header_generation(&c
->header
, trans
->transid
);
885 btrfs_set_header_owner(&c
->header
, root
->root_key
.objectid
);
886 lower
= btrfs_buffer_node(path
->nodes
[level
-1]);
887 memcpy(c
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
888 sizeof(c
->header
.fsid
));
889 if (btrfs_is_leaf(lower
))
890 lower_key
= &((struct btrfs_leaf
*)lower
)->items
[0].key
;
892 lower_key
= &lower
->ptrs
[0].key
;
893 btrfs_memcpy(root
, c
, &c
->ptrs
[0].key
, lower_key
,
894 sizeof(struct btrfs_disk_key
));
895 btrfs_set_node_blockptr(c
, 0, bh_blocknr(path
->nodes
[level
- 1]));
897 btrfs_mark_buffer_dirty(t
);
899 /* the super has an extra ref to root->node */
900 btrfs_block_release(root
, root
->node
);
903 path
->nodes
[level
] = t
;
904 path
->slots
[level
] = 0;
909 * worker function to insert a single pointer in a node.
910 * the node should have enough room for the pointer already
912 * slot and level indicate where you want the key to go, and
913 * blocknr is the block the key points to.
915 * returns zero on success and < 0 on any error
917 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
918 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
919 *key
, u64 blocknr
, int slot
, int level
)
921 struct btrfs_node
*lower
;
924 BUG_ON(!path
->nodes
[level
]);
925 lower
= btrfs_buffer_node(path
->nodes
[level
]);
926 nritems
= btrfs_header_nritems(&lower
->header
);
929 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
931 if (slot
!= nritems
) {
932 btrfs_memmove(root
, lower
, lower
->ptrs
+ slot
+ 1,
934 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
936 btrfs_memcpy(root
, lower
, &lower
->ptrs
[slot
].key
,
937 key
, sizeof(struct btrfs_disk_key
));
938 btrfs_set_node_blockptr(lower
, slot
, blocknr
);
939 btrfs_set_header_nritems(&lower
->header
, nritems
+ 1);
940 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
941 check_node(root
, path
, level
);
946 * split the node at the specified level in path in two.
947 * The path is corrected to point to the appropriate node after the split
949 * Before splitting this tries to make some room in the node by pushing
950 * left and right, if either one works, it returns right away.
952 * returns 0 on success and < 0 on failure
954 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
955 *root
, struct btrfs_path
*path
, int level
)
957 struct buffer_head
*t
;
958 struct btrfs_node
*c
;
959 struct buffer_head
*split_buffer
;
960 struct btrfs_node
*split
;
966 t
= path
->nodes
[level
];
967 c
= btrfs_buffer_node(t
);
968 if (t
== root
->node
) {
969 /* trying to split the root, lets make a new one */
970 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
974 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
975 t
= path
->nodes
[level
];
976 c
= btrfs_buffer_node(t
);
978 btrfs_header_nritems(&c
->header
) <
979 BTRFS_NODEPTRS_PER_BLOCK(root
) - 1)
985 c_nritems
= btrfs_header_nritems(&c
->header
);
986 split_buffer
= btrfs_alloc_free_block(trans
, root
, t
->b_blocknr
);
987 if (IS_ERR(split_buffer
))
988 return PTR_ERR(split_buffer
);
990 split
= btrfs_buffer_node(split_buffer
);
991 btrfs_set_header_flags(&split
->header
, btrfs_header_flags(&c
->header
));
992 btrfs_set_header_level(&split
->header
, btrfs_header_level(&c
->header
));
993 btrfs_set_header_blocknr(&split
->header
, bh_blocknr(split_buffer
));
994 btrfs_set_header_generation(&split
->header
, trans
->transid
);
995 btrfs_set_header_owner(&split
->header
, root
->root_key
.objectid
);
996 memcpy(split
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
997 sizeof(split
->header
.fsid
));
998 mid
= (c_nritems
+ 1) / 2;
999 btrfs_memcpy(root
, split
, split
->ptrs
, c
->ptrs
+ mid
,
1000 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1001 btrfs_set_header_nritems(&split
->header
, c_nritems
- mid
);
1002 btrfs_set_header_nritems(&c
->header
, mid
);
1005 btrfs_mark_buffer_dirty(t
);
1006 btrfs_mark_buffer_dirty(split_buffer
);
1007 wret
= insert_ptr(trans
, root
, path
, &split
->ptrs
[0].key
,
1008 bh_blocknr(split_buffer
), path
->slots
[level
+ 1] + 1,
1013 if (path
->slots
[level
] >= mid
) {
1014 path
->slots
[level
] -= mid
;
1015 btrfs_block_release(root
, t
);
1016 path
->nodes
[level
] = split_buffer
;
1017 path
->slots
[level
+ 1] += 1;
1019 btrfs_block_release(root
, split_buffer
);
1025 * how many bytes are required to store the items in a leaf. start
1026 * and nr indicate which items in the leaf to check. This totals up the
1027 * space used both by the item structs and the item data
1029 static int leaf_space_used(struct btrfs_leaf
*l
, int start
, int nr
)
1032 int nritems
= btrfs_header_nritems(&l
->header
);
1033 int end
= min(nritems
, start
+ nr
) - 1;
1037 data_len
= btrfs_item_end(l
->items
+ start
);
1038 data_len
= data_len
- btrfs_item_offset(l
->items
+ end
);
1039 data_len
+= sizeof(struct btrfs_item
) * nr
;
1040 WARN_ON(data_len
< 0);
1045 * The space between the end of the leaf items and
1046 * the start of the leaf data. IOW, how much room
1047 * the leaf has left for both items and data
1049 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct btrfs_leaf
*leaf
)
1051 int nritems
= btrfs_header_nritems(&leaf
->header
);
1052 return BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
1056 * push some data in the path leaf to the right, trying to free up at
1057 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1059 * returns 1 if the push failed because the other node didn't have enough
1060 * room, 0 if everything worked out and < 0 if there were major errors.
1062 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1063 *root
, struct btrfs_path
*path
, int data_size
)
1065 struct buffer_head
*left_buf
= path
->nodes
[0];
1066 struct btrfs_leaf
*left
= btrfs_buffer_leaf(left_buf
);
1067 struct btrfs_leaf
*right
;
1068 struct buffer_head
*right_buf
;
1069 struct buffer_head
*upper
;
1070 struct btrfs_node
*upper_node
;
1076 struct btrfs_item
*item
;
1081 slot
= path
->slots
[1];
1082 if (!path
->nodes
[1]) {
1085 upper
= path
->nodes
[1];
1086 upper_node
= btrfs_buffer_node(upper
);
1087 if (slot
>= btrfs_header_nritems(&upper_node
->header
) - 1) {
1090 right_buf
= read_tree_block(root
,
1091 btrfs_node_blockptr(btrfs_buffer_node(upper
), slot
+ 1));
1092 right
= btrfs_buffer_leaf(right_buf
);
1093 free_space
= btrfs_leaf_free_space(root
, right
);
1094 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1095 btrfs_block_release(root
, right_buf
);
1098 /* cow and double check */
1099 ret
= btrfs_cow_block(trans
, root
, right_buf
, upper
,
1100 slot
+ 1, &right_buf
);
1102 btrfs_block_release(root
, right_buf
);
1105 right
= btrfs_buffer_leaf(right_buf
);
1106 free_space
= btrfs_leaf_free_space(root
, right
);
1107 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1108 btrfs_block_release(root
, right_buf
);
1112 left_nritems
= btrfs_header_nritems(&left
->header
);
1113 if (left_nritems
== 0) {
1114 btrfs_block_release(root
, right_buf
);
1117 for (i
= left_nritems
- 1; i
>= 1; i
--) {
1118 item
= left
->items
+ i
;
1119 if (path
->slots
[0] == i
)
1120 push_space
+= data_size
+ sizeof(*item
);
1121 if (btrfs_item_size(item
) + sizeof(*item
) + push_space
>
1125 push_space
+= btrfs_item_size(item
) + sizeof(*item
);
1127 if (push_items
== 0) {
1128 btrfs_block_release(root
, right_buf
);
1131 if (push_items
== left_nritems
)
1133 right_nritems
= btrfs_header_nritems(&right
->header
);
1134 /* push left to right */
1135 push_space
= btrfs_item_end(left
->items
+ left_nritems
- push_items
);
1136 push_space
-= leaf_data_end(root
, left
);
1137 /* make room in the right data area */
1138 btrfs_memmove(root
, right
, btrfs_leaf_data(right
) +
1139 leaf_data_end(root
, right
) - push_space
,
1140 btrfs_leaf_data(right
) +
1141 leaf_data_end(root
, right
), BTRFS_LEAF_DATA_SIZE(root
) -
1142 leaf_data_end(root
, right
));
1143 /* copy from the left data area */
1144 btrfs_memcpy(root
, right
, btrfs_leaf_data(right
) +
1145 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1146 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
1148 btrfs_memmove(root
, right
, right
->items
+ push_items
, right
->items
,
1149 right_nritems
* sizeof(struct btrfs_item
));
1150 /* copy the items from left to right */
1151 btrfs_memcpy(root
, right
, right
->items
, left
->items
+
1152 left_nritems
- push_items
,
1153 push_items
* sizeof(struct btrfs_item
));
1155 /* update the item pointers */
1156 right_nritems
+= push_items
;
1157 btrfs_set_header_nritems(&right
->header
, right_nritems
);
1158 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1159 for (i
= 0; i
< right_nritems
; i
++) {
1160 btrfs_set_item_offset(right
->items
+ i
, push_space
-
1161 btrfs_item_size(right
->items
+ i
));
1162 push_space
= btrfs_item_offset(right
->items
+ i
);
1164 left_nritems
-= push_items
;
1165 btrfs_set_header_nritems(&left
->header
, left_nritems
);
1167 btrfs_mark_buffer_dirty(left_buf
);
1168 btrfs_mark_buffer_dirty(right_buf
);
1170 btrfs_memcpy(root
, upper_node
, &upper_node
->ptrs
[slot
+ 1].key
,
1171 &right
->items
[0].key
, sizeof(struct btrfs_disk_key
));
1172 btrfs_mark_buffer_dirty(upper
);
1174 /* then fixup the leaf pointer in the path */
1175 if (path
->slots
[0] >= left_nritems
) {
1176 path
->slots
[0] -= left_nritems
;
1177 btrfs_block_release(root
, path
->nodes
[0]);
1178 path
->nodes
[0] = right_buf
;
1179 path
->slots
[1] += 1;
1181 btrfs_block_release(root
, right_buf
);
1184 check_node(root
, path
, 1);
1188 * push some data in the path leaf to the left, trying to free up at
1189 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1191 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1192 *root
, struct btrfs_path
*path
, int data_size
)
1194 struct buffer_head
*right_buf
= path
->nodes
[0];
1195 struct btrfs_leaf
*right
= btrfs_buffer_leaf(right_buf
);
1196 struct buffer_head
*t
;
1197 struct btrfs_leaf
*left
;
1203 struct btrfs_item
*item
;
1204 u32 old_left_nritems
;
1208 slot
= path
->slots
[1];
1212 if (!path
->nodes
[1]) {
1215 t
= read_tree_block(root
,
1216 btrfs_node_blockptr(btrfs_buffer_node(path
->nodes
[1]), slot
- 1));
1217 left
= btrfs_buffer_leaf(t
);
1218 free_space
= btrfs_leaf_free_space(root
, left
);
1219 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1220 btrfs_block_release(root
, t
);
1224 /* cow and double check */
1225 ret
= btrfs_cow_block(trans
, root
, t
, path
->nodes
[1], slot
- 1, &t
);
1227 /* we hit -ENOSPC, but it isn't fatal here */
1230 left
= btrfs_buffer_leaf(t
);
1231 free_space
= btrfs_leaf_free_space(root
, left
);
1232 if (free_space
< data_size
+ sizeof(struct btrfs_item
)) {
1233 btrfs_block_release(root
, t
);
1237 if (btrfs_header_nritems(&right
->header
) == 0) {
1238 btrfs_block_release(root
, t
);
1242 for (i
= 0; i
< btrfs_header_nritems(&right
->header
) - 1; i
++) {
1243 item
= right
->items
+ i
;
1244 if (path
->slots
[0] == i
)
1245 push_space
+= data_size
+ sizeof(*item
);
1246 if (btrfs_item_size(item
) + sizeof(*item
) + push_space
>
1250 push_space
+= btrfs_item_size(item
) + sizeof(*item
);
1252 if (push_items
== 0) {
1253 btrfs_block_release(root
, t
);
1256 if (push_items
== btrfs_header_nritems(&right
->header
))
1258 /* push data from right to left */
1259 btrfs_memcpy(root
, left
, left
->items
+
1260 btrfs_header_nritems(&left
->header
),
1261 right
->items
, push_items
* sizeof(struct btrfs_item
));
1262 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
1263 btrfs_item_offset(right
->items
+ push_items
-1);
1264 btrfs_memcpy(root
, left
, btrfs_leaf_data(left
) +
1265 leaf_data_end(root
, left
) - push_space
,
1266 btrfs_leaf_data(right
) +
1267 btrfs_item_offset(right
->items
+ push_items
- 1),
1269 old_left_nritems
= btrfs_header_nritems(&left
->header
);
1270 BUG_ON(old_left_nritems
< 0);
1272 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1273 u32 ioff
= btrfs_item_offset(left
->items
+ i
);
1274 btrfs_set_item_offset(left
->items
+ i
, ioff
-
1275 (BTRFS_LEAF_DATA_SIZE(root
) -
1276 btrfs_item_offset(left
->items
+
1277 old_left_nritems
- 1)));
1279 btrfs_set_header_nritems(&left
->header
, old_left_nritems
+ push_items
);
1281 /* fixup right node */
1282 push_space
= btrfs_item_offset(right
->items
+ push_items
- 1) -
1283 leaf_data_end(root
, right
);
1284 btrfs_memmove(root
, right
, btrfs_leaf_data(right
) +
1285 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
1286 btrfs_leaf_data(right
) +
1287 leaf_data_end(root
, right
), push_space
);
1288 btrfs_memmove(root
, right
, right
->items
, right
->items
+ push_items
,
1289 (btrfs_header_nritems(&right
->header
) - push_items
) *
1290 sizeof(struct btrfs_item
));
1291 btrfs_set_header_nritems(&right
->header
,
1292 btrfs_header_nritems(&right
->header
) -
1294 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
1296 for (i
= 0; i
< btrfs_header_nritems(&right
->header
); i
++) {
1297 btrfs_set_item_offset(right
->items
+ i
, push_space
-
1298 btrfs_item_size(right
->items
+ i
));
1299 push_space
= btrfs_item_offset(right
->items
+ i
);
1302 btrfs_mark_buffer_dirty(t
);
1303 btrfs_mark_buffer_dirty(right_buf
);
1305 wret
= fixup_low_keys(trans
, root
, path
, &right
->items
[0].key
, 1);
1309 /* then fixup the leaf pointer in the path */
1310 if (path
->slots
[0] < push_items
) {
1311 path
->slots
[0] += old_left_nritems
;
1312 btrfs_block_release(root
, path
->nodes
[0]);
1314 path
->slots
[1] -= 1;
1316 btrfs_block_release(root
, t
);
1317 path
->slots
[0] -= push_items
;
1319 BUG_ON(path
->slots
[0] < 0);
1321 check_node(root
, path
, 1);
1326 * split the path's leaf in two, making sure there is at least data_size
1327 * available for the resulting leaf level of the path.
1329 * returns 0 if all went well and < 0 on failure.
1331 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
1332 *root
, struct btrfs_key
*ins_key
,
1333 struct btrfs_path
*path
, int data_size
)
1335 struct buffer_head
*l_buf
;
1336 struct btrfs_leaf
*l
;
1340 struct btrfs_leaf
*right
;
1341 struct buffer_head
*right_buffer
;
1342 int space_needed
= data_size
+ sizeof(struct btrfs_item
);
1348 int double_split
= 0;
1349 struct btrfs_disk_key disk_key
;
1351 /* first try to make some room by pushing left and right */
1352 wret
= push_leaf_left(trans
, root
, path
, data_size
);
1356 wret
= push_leaf_right(trans
, root
, path
, data_size
);
1360 l_buf
= path
->nodes
[0];
1361 l
= btrfs_buffer_leaf(l_buf
);
1363 /* did the pushes work? */
1364 if (btrfs_leaf_free_space(root
, l
) >=
1365 sizeof(struct btrfs_item
) + data_size
)
1368 if (!path
->nodes
[1]) {
1369 ret
= insert_new_root(trans
, root
, path
, 1);
1373 slot
= path
->slots
[0];
1374 nritems
= btrfs_header_nritems(&l
->header
);
1375 mid
= (nritems
+ 1)/ 2;
1377 right_buffer
= btrfs_alloc_free_block(trans
, root
, l_buf
->b_blocknr
);
1378 if (IS_ERR(right_buffer
))
1379 return PTR_ERR(right_buffer
);
1381 right
= btrfs_buffer_leaf(right_buffer
);
1382 memset(&right
->header
, 0, sizeof(right
->header
));
1383 btrfs_set_header_blocknr(&right
->header
, bh_blocknr(right_buffer
));
1384 btrfs_set_header_generation(&right
->header
, trans
->transid
);
1385 btrfs_set_header_owner(&right
->header
, root
->root_key
.objectid
);
1386 btrfs_set_header_level(&right
->header
, 0);
1387 memcpy(right
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
1388 sizeof(right
->header
.fsid
));
1391 leaf_space_used(l
, mid
, nritems
- mid
) + space_needed
>
1392 BTRFS_LEAF_DATA_SIZE(root
)) {
1393 if (slot
>= nritems
) {
1394 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
1395 btrfs_set_header_nritems(&right
->header
, 0);
1396 wret
= insert_ptr(trans
, root
, path
,
1398 bh_blocknr(right_buffer
),
1399 path
->slots
[1] + 1, 1);
1402 btrfs_block_release(root
, path
->nodes
[0]);
1403 path
->nodes
[0] = right_buffer
;
1405 path
->slots
[1] += 1;
1412 if (leaf_space_used(l
, 0, mid
+ 1) + space_needed
>
1413 BTRFS_LEAF_DATA_SIZE(root
)) {
1415 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
1416 btrfs_set_header_nritems(&right
->header
, 0);
1417 wret
= insert_ptr(trans
, root
, path
,
1419 bh_blocknr(right_buffer
),
1423 btrfs_block_release(root
, path
->nodes
[0]);
1424 path
->nodes
[0] = right_buffer
;
1426 if (path
->slots
[1] == 0) {
1427 wret
= fixup_low_keys(trans
, root
,
1428 path
, &disk_key
, 1);
1438 btrfs_set_header_nritems(&right
->header
, nritems
- mid
);
1439 data_copy_size
= btrfs_item_end(l
->items
+ mid
) -
1440 leaf_data_end(root
, l
);
1441 btrfs_memcpy(root
, right
, right
->items
, l
->items
+ mid
,
1442 (nritems
- mid
) * sizeof(struct btrfs_item
));
1443 btrfs_memcpy(root
, right
,
1444 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
1445 data_copy_size
, btrfs_leaf_data(l
) +
1446 leaf_data_end(root
, l
), data_copy_size
);
1447 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
1448 btrfs_item_end(l
->items
+ mid
);
1450 for (i
= 0; i
< btrfs_header_nritems(&right
->header
); i
++) {
1451 u32 ioff
= btrfs_item_offset(right
->items
+ i
);
1452 btrfs_set_item_offset(right
->items
+ i
, ioff
+ rt_data_off
);
1455 btrfs_set_header_nritems(&l
->header
, mid
);
1457 wret
= insert_ptr(trans
, root
, path
, &right
->items
[0].key
,
1458 bh_blocknr(right_buffer
), path
->slots
[1] + 1, 1);
1461 btrfs_mark_buffer_dirty(right_buffer
);
1462 btrfs_mark_buffer_dirty(l_buf
);
1463 BUG_ON(path
->slots
[0] != slot
);
1465 btrfs_block_release(root
, path
->nodes
[0]);
1466 path
->nodes
[0] = right_buffer
;
1467 path
->slots
[0] -= mid
;
1468 path
->slots
[1] += 1;
1470 btrfs_block_release(root
, right_buffer
);
1471 BUG_ON(path
->slots
[0] < 0);
1472 check_node(root
, path
, 1);
1476 right_buffer
= btrfs_alloc_free_block(trans
, root
, l_buf
->b_blocknr
);
1477 if (IS_ERR(right_buffer
))
1478 return PTR_ERR(right_buffer
);
1480 right
= btrfs_buffer_leaf(right_buffer
);
1481 memset(&right
->header
, 0, sizeof(right
->header
));
1482 btrfs_set_header_blocknr(&right
->header
, bh_blocknr(right_buffer
));
1483 btrfs_set_header_generation(&right
->header
, trans
->transid
);
1484 btrfs_set_header_owner(&right
->header
, root
->root_key
.objectid
);
1485 btrfs_set_header_level(&right
->header
, 0);
1486 memcpy(right
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
1487 sizeof(right
->header
.fsid
));
1488 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
1489 btrfs_set_header_nritems(&right
->header
, 0);
1490 wret
= insert_ptr(trans
, root
, path
,
1492 bh_blocknr(right_buffer
),
1496 if (path
->slots
[1] == 0) {
1497 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1501 btrfs_block_release(root
, path
->nodes
[0]);
1502 path
->nodes
[0] = right_buffer
;
1504 check_node(root
, path
, 1);
1505 check_leaf(root
, path
, 0);
1509 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
1510 struct btrfs_root
*root
,
1511 struct btrfs_path
*path
,
1517 struct btrfs_leaf
*leaf
;
1518 struct buffer_head
*leaf_buf
;
1520 unsigned int data_end
;
1521 unsigned int old_data_start
;
1522 unsigned int old_size
;
1523 unsigned int size_diff
;
1526 slot_orig
= path
->slots
[0];
1527 leaf_buf
= path
->nodes
[0];
1528 leaf
= btrfs_buffer_leaf(leaf_buf
);
1530 nritems
= btrfs_header_nritems(&leaf
->header
);
1531 data_end
= leaf_data_end(root
, leaf
);
1533 slot
= path
->slots
[0];
1534 old_data_start
= btrfs_item_offset(leaf
->items
+ slot
);
1535 old_size
= btrfs_item_size(leaf
->items
+ slot
);
1536 BUG_ON(old_size
<= new_size
);
1537 size_diff
= old_size
- new_size
;
1540 BUG_ON(slot
>= nritems
);
1543 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1545 /* first correct the data pointers */
1546 for (i
= slot
; i
< nritems
; i
++) {
1547 u32 ioff
= btrfs_item_offset(leaf
->items
+ i
);
1548 btrfs_set_item_offset(leaf
->items
+ i
,
1551 /* shift the data */
1552 btrfs_memmove(root
, leaf
, btrfs_leaf_data(leaf
) +
1553 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
1554 data_end
, old_data_start
+ new_size
- data_end
);
1555 btrfs_set_item_size(leaf
->items
+ slot
, new_size
);
1556 btrfs_mark_buffer_dirty(leaf_buf
);
1559 if (btrfs_leaf_free_space(root
, leaf
) < 0)
1561 check_leaf(root
, path
, 0);
1565 int btrfs_extend_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
1566 *root
, struct btrfs_path
*path
, u32 data_size
)
1571 struct btrfs_leaf
*leaf
;
1572 struct buffer_head
*leaf_buf
;
1574 unsigned int data_end
;
1575 unsigned int old_data
;
1576 unsigned int old_size
;
1579 slot_orig
= path
->slots
[0];
1580 leaf_buf
= path
->nodes
[0];
1581 leaf
= btrfs_buffer_leaf(leaf_buf
);
1583 nritems
= btrfs_header_nritems(&leaf
->header
);
1584 data_end
= leaf_data_end(root
, leaf
);
1586 if (btrfs_leaf_free_space(root
, leaf
) < data_size
)
1588 slot
= path
->slots
[0];
1589 old_data
= btrfs_item_end(leaf
->items
+ slot
);
1592 BUG_ON(slot
>= nritems
);
1595 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1597 /* first correct the data pointers */
1598 for (i
= slot
; i
< nritems
; i
++) {
1599 u32 ioff
= btrfs_item_offset(leaf
->items
+ i
);
1600 btrfs_set_item_offset(leaf
->items
+ i
,
1603 /* shift the data */
1604 btrfs_memmove(root
, leaf
, btrfs_leaf_data(leaf
) +
1605 data_end
- data_size
, btrfs_leaf_data(leaf
) +
1606 data_end
, old_data
- data_end
);
1607 data_end
= old_data
;
1608 old_size
= btrfs_item_size(leaf
->items
+ slot
);
1609 btrfs_set_item_size(leaf
->items
+ slot
, old_size
+ data_size
);
1610 btrfs_mark_buffer_dirty(leaf_buf
);
1613 if (btrfs_leaf_free_space(root
, leaf
) < 0)
1615 check_leaf(root
, path
, 0);
1620 * Given a key and some data, insert an item into the tree.
1621 * This does all the path init required, making room in the tree if needed.
1623 int btrfs_insert_empty_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
1624 *root
, struct btrfs_path
*path
, struct btrfs_key
1625 *cpu_key
, u32 data_size
)
1630 struct btrfs_leaf
*leaf
;
1631 struct buffer_head
*leaf_buf
;
1633 unsigned int data_end
;
1634 struct btrfs_disk_key disk_key
;
1636 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
1638 /* create a root if there isn't one */
1641 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, data_size
, 1);
1648 slot_orig
= path
->slots
[0];
1649 leaf_buf
= path
->nodes
[0];
1650 leaf
= btrfs_buffer_leaf(leaf_buf
);
1652 nritems
= btrfs_header_nritems(&leaf
->header
);
1653 data_end
= leaf_data_end(root
, leaf
);
1655 if (btrfs_leaf_free_space(root
, leaf
) <
1656 sizeof(struct btrfs_item
) + data_size
) {
1659 slot
= path
->slots
[0];
1661 if (slot
!= nritems
) {
1663 unsigned int old_data
= btrfs_item_end(leaf
->items
+ slot
);
1666 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1668 /* first correct the data pointers */
1669 for (i
= slot
; i
< nritems
; i
++) {
1670 u32 ioff
= btrfs_item_offset(leaf
->items
+ i
);
1671 btrfs_set_item_offset(leaf
->items
+ i
,
1675 /* shift the items */
1676 btrfs_memmove(root
, leaf
, leaf
->items
+ slot
+ 1,
1678 (nritems
- slot
) * sizeof(struct btrfs_item
));
1680 /* shift the data */
1681 btrfs_memmove(root
, leaf
, btrfs_leaf_data(leaf
) +
1682 data_end
- data_size
, btrfs_leaf_data(leaf
) +
1683 data_end
, old_data
- data_end
);
1684 data_end
= old_data
;
1686 /* setup the item for the new data */
1687 btrfs_memcpy(root
, leaf
, &leaf
->items
[slot
].key
, &disk_key
,
1688 sizeof(struct btrfs_disk_key
));
1689 btrfs_set_item_offset(leaf
->items
+ slot
, data_end
- data_size
);
1690 btrfs_set_item_size(leaf
->items
+ slot
, data_size
);
1691 btrfs_set_header_nritems(&leaf
->header
, nritems
+ 1);
1692 btrfs_mark_buffer_dirty(leaf_buf
);
1696 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1698 if (btrfs_leaf_free_space(root
, leaf
) < 0)
1700 check_leaf(root
, path
, 0);
1706 * Given a key and some data, insert an item into the tree.
1707 * This does all the path init required, making room in the tree if needed.
1709 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
1710 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
1714 struct btrfs_path
*path
;
1717 path
= btrfs_alloc_path();
1719 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
1721 ptr
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1722 path
->slots
[0], u8
);
1723 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
1724 ptr
, data
, data_size
);
1725 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1727 btrfs_free_path(path
);
1732 * delete the pointer from a given node.
1734 * If the delete empties a node, the node is removed from the tree,
1735 * continuing all the way the root if required. The root is converted into
1736 * a leaf if all the nodes are emptied.
1738 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1739 struct btrfs_path
*path
, int level
, int slot
)
1741 struct btrfs_node
*node
;
1742 struct buffer_head
*parent
= path
->nodes
[level
];
1747 node
= btrfs_buffer_node(parent
);
1748 nritems
= btrfs_header_nritems(&node
->header
);
1749 if (slot
!= nritems
-1) {
1750 btrfs_memmove(root
, node
, node
->ptrs
+ slot
,
1751 node
->ptrs
+ slot
+ 1,
1752 sizeof(struct btrfs_key_ptr
) *
1753 (nritems
- slot
- 1));
1756 btrfs_set_header_nritems(&node
->header
, nritems
);
1757 if (nritems
== 0 && parent
== root
->node
) {
1758 struct btrfs_header
*header
= btrfs_buffer_header(root
->node
);
1759 BUG_ON(btrfs_header_level(header
) != 1);
1760 /* just turn the root into a leaf and break */
1761 btrfs_set_header_level(header
, 0);
1762 } else if (slot
== 0) {
1763 wret
= fixup_low_keys(trans
, root
, path
, &node
->ptrs
[0].key
,
1768 btrfs_mark_buffer_dirty(parent
);
1773 * delete the item at the leaf level in path. If that empties
1774 * the leaf, remove it from the tree
1776 int btrfs_del_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1777 struct btrfs_path
*path
)
1780 struct btrfs_leaf
*leaf
;
1781 struct buffer_head
*leaf_buf
;
1788 leaf_buf
= path
->nodes
[0];
1789 leaf
= btrfs_buffer_leaf(leaf_buf
);
1790 slot
= path
->slots
[0];
1791 doff
= btrfs_item_offset(leaf
->items
+ slot
);
1792 dsize
= btrfs_item_size(leaf
->items
+ slot
);
1793 nritems
= btrfs_header_nritems(&leaf
->header
);
1795 if (slot
!= nritems
- 1) {
1797 int data_end
= leaf_data_end(root
, leaf
);
1798 btrfs_memmove(root
, leaf
, btrfs_leaf_data(leaf
) +
1800 btrfs_leaf_data(leaf
) + data_end
,
1802 for (i
= slot
+ 1; i
< nritems
; i
++) {
1803 u32 ioff
= btrfs_item_offset(leaf
->items
+ i
);
1804 btrfs_set_item_offset(leaf
->items
+ i
, ioff
+ dsize
);
1806 btrfs_memmove(root
, leaf
, leaf
->items
+ slot
,
1807 leaf
->items
+ slot
+ 1,
1808 sizeof(struct btrfs_item
) *
1809 (nritems
- slot
- 1));
1811 btrfs_set_header_nritems(&leaf
->header
, nritems
- 1);
1813 /* delete the leaf if we've emptied it */
1815 if (leaf_buf
== root
->node
) {
1816 btrfs_set_header_level(&leaf
->header
, 0);
1818 clean_tree_block(trans
, root
, leaf_buf
);
1819 wait_on_buffer(leaf_buf
);
1820 wret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
1823 wret
= btrfs_free_extent(trans
, root
,
1824 bh_blocknr(leaf_buf
), 1, 1);
1829 int used
= leaf_space_used(leaf
, 0, nritems
);
1831 wret
= fixup_low_keys(trans
, root
, path
,
1832 &leaf
->items
[0].key
, 1);
1837 /* delete the leaf if it is mostly empty */
1838 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
1839 /* push_leaf_left fixes the path.
1840 * make sure the path still points to our leaf
1841 * for possible call to del_ptr below
1843 slot
= path
->slots
[1];
1845 wret
= push_leaf_left(trans
, root
, path
, 1);
1846 if (wret
< 0 && wret
!= -ENOSPC
)
1848 if (path
->nodes
[0] == leaf_buf
&&
1849 btrfs_header_nritems(&leaf
->header
)) {
1850 wret
= push_leaf_right(trans
, root
, path
, 1);
1851 if (wret
< 0 && wret
!= -ENOSPC
)
1854 if (btrfs_header_nritems(&leaf
->header
) == 0) {
1855 u64 blocknr
= bh_blocknr(leaf_buf
);
1856 clean_tree_block(trans
, root
, leaf_buf
);
1857 wait_on_buffer(leaf_buf
);
1858 wret
= del_ptr(trans
, root
, path
, 1, slot
);
1861 btrfs_block_release(root
, leaf_buf
);
1862 wret
= btrfs_free_extent(trans
, root
, blocknr
,
1867 btrfs_mark_buffer_dirty(leaf_buf
);
1868 btrfs_block_release(root
, leaf_buf
);
1871 btrfs_mark_buffer_dirty(leaf_buf
);
1878 * walk up the tree as far as required to find the next leaf.
1879 * returns 0 if it found something or 1 if there are no greater leaves.
1880 * returns < 0 on io errors.
1882 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
1887 struct buffer_head
*c
;
1888 struct btrfs_node
*c_node
;
1889 struct buffer_head
*next
= NULL
;
1891 while(level
< BTRFS_MAX_LEVEL
) {
1892 if (!path
->nodes
[level
])
1894 slot
= path
->slots
[level
] + 1;
1895 c
= path
->nodes
[level
];
1896 c_node
= btrfs_buffer_node(c
);
1897 if (slot
>= btrfs_header_nritems(&c_node
->header
)) {
1901 blocknr
= btrfs_node_blockptr(c_node
, slot
);
1903 btrfs_block_release(root
, next
);
1904 next
= read_tree_block(root
, blocknr
);
1907 path
->slots
[level
] = slot
;
1910 c
= path
->nodes
[level
];
1911 btrfs_block_release(root
, c
);
1912 path
->nodes
[level
] = next
;
1913 path
->slots
[level
] = 0;
1916 next
= read_tree_block(root
,
1917 btrfs_node_blockptr(btrfs_buffer_node(next
), 0));