2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include "transaction.h"
21 #include "print-tree.h"
27 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
28 *root
, struct btrfs_path
*path
, int level
);
29 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
30 *root
, struct btrfs_key
*ins_key
,
31 struct btrfs_path
*path
, int data_size
, int extend
);
32 static int push_node_left(struct btrfs_trans_handle
*trans
,
33 struct btrfs_root
*root
, struct extent_buffer
*dst
,
34 struct extent_buffer
*src
, int empty
);
35 static int balance_node_right(struct btrfs_trans_handle
*trans
,
36 struct btrfs_root
*root
,
37 struct extent_buffer
*dst_buf
,
38 struct extent_buffer
*src_buf
);
40 inline void btrfs_init_path(struct btrfs_path
*p
)
42 memset(p
, 0, sizeof(*p
));
45 struct btrfs_path
*btrfs_alloc_path(void)
47 struct btrfs_path
*path
;
48 path
= kzalloc(sizeof(struct btrfs_path
), GFP_NOFS
);
52 void btrfs_free_path(struct btrfs_path
*p
)
56 btrfs_release_path(p
);
60 void btrfs_release_path(struct btrfs_path
*p
)
63 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
66 free_extent_buffer(p
->nodes
[i
]);
68 memset(p
, 0, sizeof(*p
));
71 void add_root_to_dirty_list(struct btrfs_root
*root
)
73 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
74 list_add(&root
->dirty_list
,
75 &root
->fs_info
->dirty_cowonly_roots
);
79 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
80 struct btrfs_root
*root
,
81 struct extent_buffer
*buf
,
82 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
84 struct extent_buffer
*cow
;
87 struct btrfs_root
*new_root
;
88 struct btrfs_disk_key disk_key
;
90 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
94 memcpy(new_root
, root
, sizeof(*new_root
));
95 new_root
->root_key
.objectid
= new_root_objectid
;
97 WARN_ON(root
->ref_cows
&& trans
->transid
!=
98 root
->fs_info
->running_transaction
->transid
);
99 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
101 level
= btrfs_header_level(buf
);
103 btrfs_item_key(buf
, &disk_key
, 0);
105 btrfs_node_key(buf
, &disk_key
, 0);
106 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
,
107 new_root_objectid
, &disk_key
,
108 level
, buf
->start
, 0);
114 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
115 btrfs_set_header_bytenr(cow
, cow
->start
);
116 btrfs_set_header_generation(cow
, trans
->transid
);
117 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
118 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
119 BTRFS_HEADER_FLAG_RELOC
);
120 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
121 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
123 btrfs_set_header_owner(cow
, new_root_objectid
);
125 write_extent_buffer(cow
, root
->fs_info
->fsid
,
126 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
128 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
129 ret
= btrfs_inc_ref(trans
, new_root
, cow
, 0);
135 btrfs_mark_buffer_dirty(cow
);
141 * check if the tree block can be shared by multiple trees
143 static int btrfs_block_can_be_shared(struct btrfs_root
*root
,
144 struct extent_buffer
*buf
)
147 * Tree blocks not in reference counted trees and tree roots
148 * are never shared. If a block was allocated after the last
149 * snapshot and the block was not allocated by tree relocation,
150 * we know the block is not shared.
152 if (root
->ref_cows
&&
153 buf
!= root
->node
&& buf
!= root
->commit_root
&&
154 (btrfs_header_generation(buf
) <=
155 btrfs_root_last_snapshot(&root
->root_item
) ||
156 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
158 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
159 if (root
->ref_cows
&&
160 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
166 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
167 struct btrfs_root
*root
,
168 struct extent_buffer
*buf
,
169 struct extent_buffer
*cow
)
178 * Backrefs update rules:
180 * Always use full backrefs for extent pointers in tree block
181 * allocated by tree relocation.
183 * If a shared tree block is no longer referenced by its owner
184 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
185 * use full backrefs for extent pointers in tree block.
187 * If a tree block is been relocating
188 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
189 * use full backrefs for extent pointers in tree block.
190 * The reason for this is some operations (such as drop tree)
191 * are only allowed for blocks use full backrefs.
194 if (btrfs_block_can_be_shared(root
, buf
)) {
195 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
196 btrfs_header_level(buf
), 1,
202 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
203 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
204 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
209 owner
= btrfs_header_owner(buf
);
210 BUG_ON(!(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) &&
211 owner
== BTRFS_TREE_RELOC_OBJECTID
);
214 if ((owner
== root
->root_key
.objectid
||
215 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
216 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
217 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
220 if (root
->root_key
.objectid
==
221 BTRFS_TREE_RELOC_OBJECTID
) {
222 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
224 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
227 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
230 if (root
->root_key
.objectid
==
231 BTRFS_TREE_RELOC_OBJECTID
)
232 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
234 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
237 if (new_flags
!= 0) {
238 ret
= btrfs_set_block_flags(trans
, root
, buf
->start
,
239 btrfs_header_level(buf
),
244 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
245 if (root
->root_key
.objectid
==
246 BTRFS_TREE_RELOC_OBJECTID
)
247 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
249 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
251 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
254 clean_tree_block(trans
, root
, buf
);
259 int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
260 struct btrfs_root
*root
,
261 struct extent_buffer
*buf
,
262 struct extent_buffer
*parent
, int parent_slot
,
263 struct extent_buffer
**cow_ret
,
264 u64 search_start
, u64 empty_size
)
266 struct extent_buffer
*cow
;
267 struct btrfs_disk_key disk_key
;
270 WARN_ON(root
->ref_cows
&& trans
->transid
!=
271 root
->fs_info
->running_transaction
->transid
);
272 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
274 level
= btrfs_header_level(buf
);
277 btrfs_item_key(buf
, &disk_key
, 0);
279 btrfs_node_key(buf
, &disk_key
, 0);
281 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
,
282 root
->root_key
.objectid
, &disk_key
,
283 level
, search_start
, empty_size
);
287 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
288 btrfs_set_header_bytenr(cow
, cow
->start
);
289 btrfs_set_header_generation(cow
, trans
->transid
);
290 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
291 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
292 BTRFS_HEADER_FLAG_RELOC
);
293 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
294 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
296 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
298 write_extent_buffer(cow
, root
->fs_info
->fsid
,
299 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
301 WARN_ON(!(buf
->flags
& EXTENT_BAD_TRANSID
) &&
302 btrfs_header_generation(buf
) > trans
->transid
);
304 update_ref_for_cow(trans
, root
, buf
, cow
);
306 if (buf
== root
->node
) {
308 extent_buffer_get(cow
);
310 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
311 0, root
->root_key
.objectid
, level
, 0);
312 free_extent_buffer(buf
);
313 add_root_to_dirty_list(root
);
315 btrfs_set_node_blockptr(parent
, parent_slot
,
317 WARN_ON(trans
->transid
== 0);
318 btrfs_set_node_ptr_generation(parent
, parent_slot
,
320 btrfs_mark_buffer_dirty(parent
);
321 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
323 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
324 0, root
->root_key
.objectid
, level
, 1);
326 if (!list_empty(&buf
->recow
)) {
327 list_del_init(&buf
->recow
);
328 free_extent_buffer(buf
);
330 free_extent_buffer(buf
);
331 btrfs_mark_buffer_dirty(cow
);
336 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
337 struct btrfs_root
*root
,
338 struct extent_buffer
*buf
)
340 if (btrfs_header_generation(buf
) == trans
->transid
&&
341 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
342 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
343 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
348 int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
349 struct btrfs_root
*root
, struct extent_buffer
*buf
,
350 struct extent_buffer
*parent
, int parent_slot
,
351 struct extent_buffer
**cow_ret
)
356 if (trans->transaction != root->fs_info->running_transaction) {
357 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
358 root->fs_info->running_transaction->transid);
362 if (trans
->transid
!= root
->fs_info
->generation
) {
363 printk(KERN_CRIT
"trans %llu running %llu\n",
364 (unsigned long long)trans
->transid
,
365 (unsigned long long)root
->fs_info
->generation
);
368 if (!should_cow_block(trans
, root
, buf
)) {
373 search_start
= buf
->start
& ~((u64
)SZ_1G
- 1);
374 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
375 parent_slot
, cow_ret
, search_start
, 0);
379 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
381 if (k1
->objectid
> k2
->objectid
)
383 if (k1
->objectid
< k2
->objectid
)
385 if (k1
->type
> k2
->type
)
387 if (k1
->type
< k2
->type
)
389 if (k1
->offset
> k2
->offset
)
391 if (k1
->offset
< k2
->offset
)
397 * compare two keys in a memcmp fashion
399 static int btrfs_comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
403 btrfs_disk_key_to_cpu(&k1
, disk
);
404 return btrfs_comp_cpu_keys(&k1
, k2
);
408 * The leaf data grows from end-to-front in the node.
409 * this returns the address of the start of the last item,
410 * which is the stop of the leaf data stack
412 static inline unsigned int leaf_data_end(const struct btrfs_fs_info
*fs_info
,
413 const struct extent_buffer
*leaf
)
415 u32 nr
= btrfs_header_nritems(leaf
);
417 return BTRFS_LEAF_DATA_SIZE(fs_info
);
418 return btrfs_item_offset_nr(leaf
, nr
- 1);
421 enum btrfs_tree_block_status
422 btrfs_check_node(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
423 struct extent_buffer
*buf
)
426 struct btrfs_key cpukey
;
427 struct btrfs_disk_key key
;
428 u32 nritems
= btrfs_header_nritems(buf
);
429 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
431 if (nritems
== 0 || nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
))
434 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
435 if (parent_key
&& parent_key
->type
) {
436 btrfs_node_key(buf
, &key
, 0);
437 if (memcmp(parent_key
, &key
, sizeof(key
)))
440 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
441 for (i
= 0; nritems
> 1 && i
< nritems
- 2; i
++) {
442 btrfs_node_key(buf
, &key
, i
);
443 btrfs_node_key_to_cpu(buf
, &cpukey
, i
+ 1);
444 if (btrfs_comp_keys(&key
, &cpukey
) >= 0)
447 return BTRFS_TREE_BLOCK_CLEAN
;
449 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
451 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
453 btrfs_node_key_to_cpu(buf
, &cpukey
, 0);
454 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
455 buf
->start
, buf
->len
,
456 btrfs_header_level(buf
));
461 enum btrfs_tree_block_status
462 btrfs_check_leaf(struct btrfs_root
*root
, struct btrfs_disk_key
*parent_key
,
463 struct extent_buffer
*buf
)
466 struct btrfs_key cpukey
;
467 struct btrfs_disk_key key
;
468 u32 nritems
= btrfs_header_nritems(buf
);
469 enum btrfs_tree_block_status ret
= BTRFS_TREE_BLOCK_INVALID_NRITEMS
;
471 if (nritems
* sizeof(struct btrfs_item
) > buf
->len
) {
472 fprintf(stderr
, "invalid number of items %llu\n",
473 (unsigned long long)buf
->start
);
477 if (btrfs_header_level(buf
) != 0) {
478 ret
= BTRFS_TREE_BLOCK_INVALID_LEVEL
;
479 fprintf(stderr
, "leaf is not a leaf %llu\n",
480 (unsigned long long)btrfs_header_bytenr(buf
));
483 if (btrfs_leaf_free_space(root
, buf
) < 0) {
484 ret
= BTRFS_TREE_BLOCK_INVALID_FREE_SPACE
;
485 fprintf(stderr
, "leaf free space incorrect %llu %d\n",
486 (unsigned long long)btrfs_header_bytenr(buf
),
487 btrfs_leaf_free_space(root
, buf
));
492 return BTRFS_TREE_BLOCK_CLEAN
;
494 btrfs_item_key(buf
, &key
, 0);
495 if (parent_key
&& parent_key
->type
&&
496 memcmp(parent_key
, &key
, sizeof(key
))) {
497 ret
= BTRFS_TREE_BLOCK_INVALID_PARENT_KEY
;
498 fprintf(stderr
, "leaf parent key incorrect %llu\n",
499 (unsigned long long)btrfs_header_bytenr(buf
));
502 for (i
= 0; nritems
> 1 && i
< nritems
- 1; i
++) {
503 btrfs_item_key(buf
, &key
, i
);
504 btrfs_item_key_to_cpu(buf
, &cpukey
, i
+ 1);
505 if (btrfs_comp_keys(&key
, &cpukey
) >= 0) {
506 ret
= BTRFS_TREE_BLOCK_BAD_KEY_ORDER
;
507 fprintf(stderr
, "bad key ordering %d %d\n", i
, i
+1);
510 if (btrfs_item_offset_nr(buf
, i
) !=
511 btrfs_item_end_nr(buf
, i
+ 1)) {
512 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
513 fprintf(stderr
, "incorrect offsets %u %u\n",
514 btrfs_item_offset_nr(buf
, i
),
515 btrfs_item_end_nr(buf
, i
+ 1));
518 if (i
== 0 && btrfs_item_end_nr(buf
, i
) !=
519 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
520 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
521 fprintf(stderr
, "bad item end %u wanted %u\n",
522 btrfs_item_end_nr(buf
, i
),
523 (unsigned)BTRFS_LEAF_DATA_SIZE(root
->fs_info
));
528 for (i
= 0; i
< nritems
; i
++) {
529 if (btrfs_item_end_nr(buf
, i
) >
530 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
531 btrfs_item_key(buf
, &key
, 0);
532 btrfs_print_key(&key
);
534 ret
= BTRFS_TREE_BLOCK_INVALID_OFFSETS
;
535 fprintf(stderr
, "slot end outside of leaf %llu > %llu\n",
536 (unsigned long long)btrfs_item_end_nr(buf
, i
),
537 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
543 return BTRFS_TREE_BLOCK_CLEAN
;
545 if (btrfs_header_owner(buf
) == BTRFS_EXTENT_TREE_OBJECTID
) {
547 btrfs_disk_key_to_cpu(&cpukey
, parent_key
);
549 btrfs_item_key_to_cpu(buf
, &cpukey
, 0);
551 btrfs_add_corrupt_extent_record(root
->fs_info
, &cpukey
,
552 buf
->start
, buf
->len
, 0);
557 static int noinline
check_block(struct btrfs_root
*root
,
558 struct btrfs_path
*path
, int level
)
560 struct btrfs_disk_key key
;
561 struct btrfs_disk_key
*key_ptr
= NULL
;
562 struct extent_buffer
*parent
;
563 enum btrfs_tree_block_status ret
;
565 if (path
->skip_check_block
)
567 if (path
->nodes
[level
+ 1]) {
568 parent
= path
->nodes
[level
+ 1];
569 btrfs_node_key(parent
, &key
, path
->slots
[level
+ 1]);
573 ret
= btrfs_check_leaf(root
, key_ptr
, path
->nodes
[0]);
575 ret
= btrfs_check_node(root
, key_ptr
, path
->nodes
[level
]);
576 if (ret
== BTRFS_TREE_BLOCK_CLEAN
)
582 * search for key in the extent_buffer. The items start at offset p,
583 * and they are item_size apart. There are 'max' items in p.
585 * the slot in the array is returned via slot, and it points to
586 * the place where you would insert key if it is not found in
589 * slot may point to max if the key is bigger than all of the keys
591 static int generic_bin_search(struct extent_buffer
*eb
, unsigned long p
,
592 int item_size
, struct btrfs_key
*key
,
599 unsigned long offset
;
600 struct btrfs_disk_key
*tmp
;
603 mid
= (low
+ high
) / 2;
604 offset
= p
+ mid
* item_size
;
606 tmp
= (struct btrfs_disk_key
*)(eb
->data
+ offset
);
607 ret
= btrfs_comp_keys(tmp
, key
);
623 * simple bin_search frontend that does the right thing for
626 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
627 int level
, int *slot
)
630 return generic_bin_search(eb
,
631 offsetof(struct btrfs_leaf
, items
),
632 sizeof(struct btrfs_item
),
633 key
, btrfs_header_nritems(eb
),
636 return generic_bin_search(eb
,
637 offsetof(struct btrfs_node
, ptrs
),
638 sizeof(struct btrfs_key_ptr
),
639 key
, btrfs_header_nritems(eb
),
643 struct extent_buffer
*read_node_slot(struct btrfs_fs_info
*fs_info
,
644 struct extent_buffer
*parent
, int slot
)
646 struct extent_buffer
*ret
;
647 int level
= btrfs_header_level(parent
);
651 if (slot
>= btrfs_header_nritems(parent
))
657 ret
= read_tree_block(fs_info
, btrfs_node_blockptr(parent
, slot
),
658 btrfs_node_ptr_generation(parent
, slot
));
659 if (!extent_buffer_uptodate(ret
))
660 return ERR_PTR(-EIO
);
662 if (btrfs_header_level(ret
) != level
- 1) {
664 "child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
665 btrfs_header_bytenr(parent
), slot
,
666 btrfs_header_level(parent
), btrfs_header_level(ret
));
667 free_extent_buffer(ret
);
668 return ERR_PTR(-EIO
);
673 static int balance_level(struct btrfs_trans_handle
*trans
,
674 struct btrfs_root
*root
,
675 struct btrfs_path
*path
, int level
)
677 struct extent_buffer
*right
= NULL
;
678 struct extent_buffer
*mid
;
679 struct extent_buffer
*left
= NULL
;
680 struct extent_buffer
*parent
= NULL
;
681 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
685 int orig_slot
= path
->slots
[level
];
691 mid
= path
->nodes
[level
];
692 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
694 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
696 if (level
< BTRFS_MAX_LEVEL
- 1) {
697 parent
= path
->nodes
[level
+ 1];
698 pslot
= path
->slots
[level
+ 1];
702 * deal with the case where there is only one pointer in the root
703 * by promoting the node below to a root
706 struct extent_buffer
*child
;
708 if (btrfs_header_nritems(mid
) != 1)
711 /* promote the child to a root */
712 child
= read_node_slot(fs_info
, mid
, 0);
713 BUG_ON(!extent_buffer_uptodate(child
));
714 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
718 add_root_to_dirty_list(root
);
719 path
->nodes
[level
] = NULL
;
720 clean_tree_block(trans
, root
, mid
);
721 /* once for the path */
722 free_extent_buffer(mid
);
724 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
725 0, root
->root_key
.objectid
,
727 /* once for the root ptr */
728 free_extent_buffer(mid
);
731 if (btrfs_header_nritems(mid
) >
732 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) / 4)
735 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
736 if (extent_buffer_uptodate(left
)) {
737 wret
= btrfs_cow_block(trans
, root
, left
,
738 parent
, pslot
- 1, &left
);
744 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
745 if (extent_buffer_uptodate(right
)) {
746 wret
= btrfs_cow_block(trans
, root
, right
,
747 parent
, pslot
+ 1, &right
);
754 /* first, try to make some room in the middle buffer */
756 orig_slot
+= btrfs_header_nritems(left
);
757 wret
= push_node_left(trans
, root
, left
, mid
, 1);
763 * then try to empty the right most buffer into the middle
766 wret
= push_node_left(trans
, root
, mid
, right
, 1);
767 if (wret
< 0 && wret
!= -ENOSPC
)
769 if (btrfs_header_nritems(right
) == 0) {
770 u64 bytenr
= right
->start
;
771 u32 blocksize
= right
->len
;
773 clean_tree_block(trans
, root
, right
);
774 free_extent_buffer(right
);
776 wret
= btrfs_del_ptr(root
, path
, level
+ 1, pslot
+ 1);
779 wret
= btrfs_free_extent(trans
, root
, bytenr
,
781 root
->root_key
.objectid
,
786 struct btrfs_disk_key right_key
;
787 btrfs_node_key(right
, &right_key
, 0);
788 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
789 btrfs_mark_buffer_dirty(parent
);
792 if (btrfs_header_nritems(mid
) == 1) {
794 * we're not allowed to leave a node with one item in the
795 * tree during a delete. A deletion from lower in the tree
796 * could try to delete the only pointer in this node.
797 * So, pull some keys from the left.
798 * There has to be a left pointer at this point because
799 * otherwise we would have pulled some pointers from the
803 wret
= balance_node_right(trans
, root
, mid
, left
);
809 wret
= push_node_left(trans
, root
, left
, mid
, 1);
815 if (btrfs_header_nritems(mid
) == 0) {
816 /* we've managed to empty the middle node, drop it */
817 u64 bytenr
= mid
->start
;
818 u32 blocksize
= mid
->len
;
819 clean_tree_block(trans
, root
, mid
);
820 free_extent_buffer(mid
);
822 wret
= btrfs_del_ptr(root
, path
, level
+ 1, pslot
);
825 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
826 0, root
->root_key
.objectid
,
831 /* update the parent key to reflect our changes */
832 struct btrfs_disk_key mid_key
;
833 btrfs_node_key(mid
, &mid_key
, 0);
834 btrfs_set_node_key(parent
, &mid_key
, pslot
);
835 btrfs_mark_buffer_dirty(parent
);
838 /* update the path */
840 if (btrfs_header_nritems(left
) > orig_slot
) {
841 extent_buffer_get(left
);
842 path
->nodes
[level
] = left
;
843 path
->slots
[level
+ 1] -= 1;
844 path
->slots
[level
] = orig_slot
;
846 free_extent_buffer(mid
);
848 orig_slot
-= btrfs_header_nritems(left
);
849 path
->slots
[level
] = orig_slot
;
852 /* double check we haven't messed things up */
853 check_block(root
, path
, level
);
855 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
859 free_extent_buffer(right
);
861 free_extent_buffer(left
);
865 /* returns zero if the push worked, non-zero otherwise */
866 static int noinline
push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
867 struct btrfs_root
*root
,
868 struct btrfs_path
*path
, int level
)
870 struct extent_buffer
*right
= NULL
;
871 struct extent_buffer
*mid
;
872 struct extent_buffer
*left
= NULL
;
873 struct extent_buffer
*parent
= NULL
;
874 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
878 int orig_slot
= path
->slots
[level
];
883 mid
= path
->nodes
[level
];
884 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
886 if (level
< BTRFS_MAX_LEVEL
- 1) {
887 parent
= path
->nodes
[level
+ 1];
888 pslot
= path
->slots
[level
+ 1];
894 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
896 /* first, try to make some room in the middle buffer */
897 if (extent_buffer_uptodate(left
)) {
899 left_nr
= btrfs_header_nritems(left
);
900 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 1) {
903 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
908 wret
= push_node_left(trans
, root
,
915 struct btrfs_disk_key disk_key
;
916 orig_slot
+= left_nr
;
917 btrfs_node_key(mid
, &disk_key
, 0);
918 btrfs_set_node_key(parent
, &disk_key
, pslot
);
919 btrfs_mark_buffer_dirty(parent
);
920 if (btrfs_header_nritems(left
) > orig_slot
) {
921 path
->nodes
[level
] = left
;
922 path
->slots
[level
+ 1] -= 1;
923 path
->slots
[level
] = orig_slot
;
924 free_extent_buffer(mid
);
927 btrfs_header_nritems(left
);
928 path
->slots
[level
] = orig_slot
;
929 free_extent_buffer(left
);
933 free_extent_buffer(left
);
935 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
938 * then try to empty the right most buffer into the middle
940 if (extent_buffer_uptodate(right
)) {
942 right_nr
= btrfs_header_nritems(right
);
943 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - 1) {
946 ret
= btrfs_cow_block(trans
, root
, right
,
952 wret
= balance_node_right(trans
, root
,
959 struct btrfs_disk_key disk_key
;
961 btrfs_node_key(right
, &disk_key
, 0);
962 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
963 btrfs_mark_buffer_dirty(parent
);
965 if (btrfs_header_nritems(mid
) <= orig_slot
) {
966 path
->nodes
[level
] = right
;
967 path
->slots
[level
+ 1] += 1;
968 path
->slots
[level
] = orig_slot
-
969 btrfs_header_nritems(mid
);
970 free_extent_buffer(mid
);
972 free_extent_buffer(right
);
976 free_extent_buffer(right
);
982 * readahead one full node of leaves
984 void reada_for_search(struct btrfs_root
*root
, struct btrfs_path
*path
,
985 int level
, int slot
, u64 objectid
)
987 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
988 struct extent_buffer
*node
;
989 struct btrfs_disk_key disk_key
;
995 int direction
= path
->reada
;
996 struct extent_buffer
*eb
;
1003 if (!path
->nodes
[level
])
1006 node
= path
->nodes
[level
];
1007 search
= btrfs_node_blockptr(node
, slot
);
1008 eb
= btrfs_find_tree_block(fs_info
, search
, fs_info
->nodesize
);
1010 free_extent_buffer(eb
);
1014 highest_read
= search
;
1015 lowest_read
= search
;
1017 nritems
= btrfs_header_nritems(node
);
1020 if (direction
< 0) {
1024 } else if (direction
> 0) {
1029 if (path
->reada
< 0 && objectid
) {
1030 btrfs_node_key(node
, &disk_key
, nr
);
1031 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1034 search
= btrfs_node_blockptr(node
, nr
);
1035 if ((search
>= lowest_read
&& search
<= highest_read
) ||
1036 (search
< lowest_read
&& lowest_read
- search
<= 32768) ||
1037 (search
> highest_read
&& search
- highest_read
<= 32768)) {
1038 readahead_tree_block(fs_info
, search
,
1039 btrfs_node_ptr_generation(node
, nr
));
1040 nread
+= fs_info
->nodesize
;
1043 if (path
->reada
< 2 && (nread
> SZ_256K
|| nscan
> 32))
1045 if(nread
> SZ_1M
|| nscan
> 128)
1048 if (search
< lowest_read
)
1049 lowest_read
= search
;
1050 if (search
> highest_read
)
1051 highest_read
= search
;
1055 int btrfs_find_item(struct btrfs_root
*fs_root
, struct btrfs_path
*found_path
,
1056 u64 iobjectid
, u64 ioff
, u8 key_type
,
1057 struct btrfs_key
*found_key
)
1060 struct btrfs_key key
;
1061 struct extent_buffer
*eb
;
1062 struct btrfs_path
*path
;
1064 key
.type
= key_type
;
1065 key
.objectid
= iobjectid
;
1068 if (found_path
== NULL
) {
1069 path
= btrfs_alloc_path();
1075 ret
= btrfs_search_slot(NULL
, fs_root
, &key
, path
, 0, 0);
1076 if ((ret
< 0) || (found_key
== NULL
))
1079 eb
= path
->nodes
[0];
1080 if (ret
&& path
->slots
[0] >= btrfs_header_nritems(eb
)) {
1081 ret
= btrfs_next_leaf(fs_root
, path
);
1084 eb
= path
->nodes
[0];
1087 btrfs_item_key_to_cpu(eb
, found_key
, path
->slots
[0]);
1088 if (found_key
->type
!= key
.type
||
1089 found_key
->objectid
!= key
.objectid
) {
1095 if (path
!= found_path
)
1096 btrfs_free_path(path
);
1101 * look for key in the tree. path is filled in with nodes along the way
1102 * if key is found, we return zero and you can find the item in the leaf
1103 * level of the path (level 0)
1105 * If the key isn't found, the path points to the slot where it should
1106 * be inserted, and 1 is returned. If there are other errors during the
1107 * search a negative error number is returned.
1109 * if ins_len > 0, nodes and leaves will be split as we walk down the
1110 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1113 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1114 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1117 struct extent_buffer
*b
;
1121 int should_reada
= p
->reada
;
1122 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1123 u8 lowest_level
= 0;
1125 lowest_level
= p
->lowest_level
;
1126 WARN_ON(lowest_level
&& ins_len
> 0);
1127 WARN_ON(p
->nodes
[0] != NULL
);
1129 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1133 extent_buffer_get(b
);
1135 level
= btrfs_header_level(b
);
1138 wret
= btrfs_cow_block(trans
, root
, b
,
1139 p
->nodes
[level
+ 1],
1140 p
->slots
[level
+ 1],
1143 free_extent_buffer(b
);
1147 BUG_ON(!cow
&& ins_len
);
1148 if (level
!= btrfs_header_level(b
))
1150 level
= btrfs_header_level(b
);
1151 p
->nodes
[level
] = b
;
1152 ret
= check_block(root
, p
, level
);
1155 ret
= bin_search(b
, key
, level
, &slot
);
1157 if (ret
&& slot
> 0)
1159 p
->slots
[level
] = slot
;
1160 if ((p
->search_for_split
|| ins_len
> 0) &&
1161 btrfs_header_nritems(b
) >=
1162 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 3) {
1163 int sret
= split_node(trans
, root
, p
, level
);
1167 b
= p
->nodes
[level
];
1168 slot
= p
->slots
[level
];
1169 } else if (ins_len
< 0) {
1170 int sret
= balance_level(trans
, root
, p
,
1174 b
= p
->nodes
[level
];
1176 btrfs_release_path(p
);
1179 slot
= p
->slots
[level
];
1180 BUG_ON(btrfs_header_nritems(b
) == 1);
1182 /* this is only true while dropping a snapshot */
1183 if (level
== lowest_level
)
1187 reada_for_search(root
, p
, level
, slot
,
1190 b
= read_node_slot(fs_info
, b
, slot
);
1191 if (!extent_buffer_uptodate(b
))
1194 p
->slots
[level
] = slot
;
1196 ins_len
> btrfs_leaf_free_space(root
, b
)) {
1197 int sret
= split_leaf(trans
, root
, key
,
1198 p
, ins_len
, ret
== 0);
1210 * adjust the pointers going up the tree, starting at level
1211 * making sure the right key of each node is points to 'key'.
1212 * This is used after shifting pointers to the left, so it stops
1213 * fixing up pointers when a given leaf/node is not in slot 0 of the
1216 void btrfs_fixup_low_keys(struct btrfs_root
*root
, struct btrfs_path
*path
,
1217 struct btrfs_disk_key
*key
, int level
)
1220 struct extent_buffer
*t
;
1222 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1223 int tslot
= path
->slots
[i
];
1224 if (!path
->nodes
[i
])
1227 btrfs_set_node_key(t
, key
, tslot
);
1228 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1237 * This function isn't completely safe. It's the caller's responsibility
1238 * that the new key won't break the order
1240 int btrfs_set_item_key_safe(struct btrfs_root
*root
, struct btrfs_path
*path
,
1241 struct btrfs_key
*new_key
)
1243 struct btrfs_disk_key disk_key
;
1244 struct extent_buffer
*eb
;
1247 eb
= path
->nodes
[0];
1248 slot
= path
->slots
[0];
1250 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1251 if (btrfs_comp_keys(&disk_key
, new_key
) >= 0)
1254 if (slot
< btrfs_header_nritems(eb
) - 1) {
1255 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1256 if (btrfs_comp_keys(&disk_key
, new_key
) <= 0)
1260 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1261 btrfs_set_item_key(eb
, &disk_key
, slot
);
1262 btrfs_mark_buffer_dirty(eb
);
1264 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1269 * update an item key without the safety checks. This is meant to be called by
1272 void btrfs_set_item_key_unsafe(struct btrfs_root
*root
,
1273 struct btrfs_path
*path
,
1274 struct btrfs_key
*new_key
)
1276 struct btrfs_disk_key disk_key
;
1277 struct extent_buffer
*eb
;
1280 eb
= path
->nodes
[0];
1281 slot
= path
->slots
[0];
1283 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1284 btrfs_set_item_key(eb
, &disk_key
, slot
);
1285 btrfs_mark_buffer_dirty(eb
);
1287 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1291 * try to push data from one node into the next node left in the
1294 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1295 * error, and > 0 if there was no room in the left hand block.
1297 static int push_node_left(struct btrfs_trans_handle
*trans
,
1298 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1299 struct extent_buffer
*src
, int empty
)
1306 src_nritems
= btrfs_header_nritems(src
);
1307 dst_nritems
= btrfs_header_nritems(dst
);
1308 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - dst_nritems
;
1309 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1310 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1312 if (!empty
&& src_nritems
<= 8)
1315 if (push_items
<= 0) {
1320 push_items
= min(src_nritems
, push_items
);
1321 if (push_items
< src_nritems
) {
1322 /* leave at least 8 pointers in the node if
1323 * we aren't going to empty it
1325 if (src_nritems
- push_items
< 8) {
1326 if (push_items
<= 8)
1332 push_items
= min(src_nritems
- 8, push_items
);
1334 copy_extent_buffer(dst
, src
,
1335 btrfs_node_key_ptr_offset(dst_nritems
),
1336 btrfs_node_key_ptr_offset(0),
1337 push_items
* sizeof(struct btrfs_key_ptr
));
1339 if (push_items
< src_nritems
) {
1340 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1341 btrfs_node_key_ptr_offset(push_items
),
1342 (src_nritems
- push_items
) *
1343 sizeof(struct btrfs_key_ptr
));
1345 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1346 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1347 btrfs_mark_buffer_dirty(src
);
1348 btrfs_mark_buffer_dirty(dst
);
1354 * try to push data from one node into the next node right in the
1357 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1358 * error, and > 0 if there was no room in the right hand block.
1360 * this will only push up to 1/2 the contents of the left node over
1362 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1363 struct btrfs_root
*root
,
1364 struct extent_buffer
*dst
,
1365 struct extent_buffer
*src
)
1373 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1374 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1376 src_nritems
= btrfs_header_nritems(src
);
1377 dst_nritems
= btrfs_header_nritems(dst
);
1378 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - dst_nritems
;
1379 if (push_items
<= 0) {
1383 if (src_nritems
< 4) {
1387 max_push
= src_nritems
/ 2 + 1;
1388 /* don't try to empty the node */
1389 if (max_push
>= src_nritems
) {
1393 if (max_push
< push_items
)
1394 push_items
= max_push
;
1396 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1397 btrfs_node_key_ptr_offset(0),
1399 sizeof(struct btrfs_key_ptr
));
1401 copy_extent_buffer(dst
, src
,
1402 btrfs_node_key_ptr_offset(0),
1403 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1404 push_items
* sizeof(struct btrfs_key_ptr
));
1406 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1407 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1409 btrfs_mark_buffer_dirty(src
);
1410 btrfs_mark_buffer_dirty(dst
);
1416 * helper function to insert a new root level in the tree.
1417 * A new node is allocated, and a single item is inserted to
1418 * point to the existing root
1420 * returns zero on success or < 0 on failure.
1422 static int noinline
insert_new_root(struct btrfs_trans_handle
*trans
,
1423 struct btrfs_root
*root
,
1424 struct btrfs_path
*path
, int level
)
1427 struct extent_buffer
*lower
;
1428 struct extent_buffer
*c
;
1429 struct extent_buffer
*old
;
1430 struct btrfs_disk_key lower_key
;
1432 BUG_ON(path
->nodes
[level
]);
1433 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1435 lower
= path
->nodes
[level
-1];
1437 btrfs_item_key(lower
, &lower_key
, 0);
1439 btrfs_node_key(lower
, &lower_key
, 0);
1441 c
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
1442 root
->root_key
.objectid
, &lower_key
,
1443 level
, root
->node
->start
, 0);
1448 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1449 btrfs_set_header_nritems(c
, 1);
1450 btrfs_set_header_level(c
, level
);
1451 btrfs_set_header_bytenr(c
, c
->start
);
1452 btrfs_set_header_generation(c
, trans
->transid
);
1453 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1454 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1456 write_extent_buffer(c
, root
->fs_info
->fsid
,
1457 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1459 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1460 btrfs_header_chunk_tree_uuid(c
),
1463 btrfs_set_node_key(c
, &lower_key
, 0);
1464 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1465 lower_gen
= btrfs_header_generation(lower
);
1466 WARN_ON(lower_gen
!= trans
->transid
);
1468 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
1470 btrfs_mark_buffer_dirty(c
);
1475 /* the super has an extra ref to root->node */
1476 free_extent_buffer(old
);
1478 add_root_to_dirty_list(root
);
1479 extent_buffer_get(c
);
1480 path
->nodes
[level
] = c
;
1481 path
->slots
[level
] = 0;
1486 * worker function to insert a single pointer in a node.
1487 * the node should have enough room for the pointer already
1489 * slot and level indicate where you want the key to go, and
1490 * blocknr is the block the key points to.
1492 * returns zero on success and < 0 on any error
1494 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
1495 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
1496 *key
, u64 bytenr
, int slot
, int level
)
1498 struct extent_buffer
*lower
;
1501 BUG_ON(!path
->nodes
[level
]);
1502 lower
= path
->nodes
[level
];
1503 nritems
= btrfs_header_nritems(lower
);
1506 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
))
1508 if (slot
< nritems
) {
1509 /* shift the items */
1510 memmove_extent_buffer(lower
,
1511 btrfs_node_key_ptr_offset(slot
+ 1),
1512 btrfs_node_key_ptr_offset(slot
),
1513 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
1515 btrfs_set_node_key(lower
, key
, slot
);
1516 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
1517 WARN_ON(trans
->transid
== 0);
1518 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
1519 btrfs_set_header_nritems(lower
, nritems
+ 1);
1520 btrfs_mark_buffer_dirty(lower
);
1525 * split the node at the specified level in path in two.
1526 * The path is corrected to point to the appropriate node after the split
1528 * Before splitting this tries to make some room in the node by pushing
1529 * left and right, if either one works, it returns right away.
1531 * returns 0 on success and < 0 on failure
1533 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
1534 *root
, struct btrfs_path
*path
, int level
)
1536 struct extent_buffer
*c
;
1537 struct extent_buffer
*split
;
1538 struct btrfs_disk_key disk_key
;
1544 c
= path
->nodes
[level
];
1545 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
1546 if (c
== root
->node
) {
1547 /* trying to split the root, lets make a new one */
1548 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
1552 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
1553 c
= path
->nodes
[level
];
1554 if (!ret
&& btrfs_header_nritems(c
) <
1555 BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) - 3)
1561 c_nritems
= btrfs_header_nritems(c
);
1562 mid
= (c_nritems
+ 1) / 2;
1563 btrfs_node_key(c
, &disk_key
, mid
);
1565 split
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
1566 root
->root_key
.objectid
,
1567 &disk_key
, level
, c
->start
, 0);
1569 return PTR_ERR(split
);
1571 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
1572 btrfs_set_header_level(split
, btrfs_header_level(c
));
1573 btrfs_set_header_bytenr(split
, split
->start
);
1574 btrfs_set_header_generation(split
, trans
->transid
);
1575 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
1576 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
1577 write_extent_buffer(split
, root
->fs_info
->fsid
,
1578 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
1579 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
1580 btrfs_header_chunk_tree_uuid(split
),
1584 copy_extent_buffer(split
, c
,
1585 btrfs_node_key_ptr_offset(0),
1586 btrfs_node_key_ptr_offset(mid
),
1587 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
1588 btrfs_set_header_nritems(split
, c_nritems
- mid
);
1589 btrfs_set_header_nritems(c
, mid
);
1592 btrfs_mark_buffer_dirty(c
);
1593 btrfs_mark_buffer_dirty(split
);
1595 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
1596 path
->slots
[level
+ 1] + 1,
1601 if (path
->slots
[level
] >= mid
) {
1602 path
->slots
[level
] -= mid
;
1603 free_extent_buffer(c
);
1604 path
->nodes
[level
] = split
;
1605 path
->slots
[level
+ 1] += 1;
1607 free_extent_buffer(split
);
1613 * how many bytes are required to store the items in a leaf. start
1614 * and nr indicate which items in the leaf to check. This totals up the
1615 * space used both by the item structs and the item data
1617 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
1620 int nritems
= btrfs_header_nritems(l
);
1621 int end
= min(nritems
, start
+ nr
) - 1;
1625 data_len
= btrfs_item_end_nr(l
, start
);
1626 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
1627 data_len
+= sizeof(struct btrfs_item
) * nr
;
1628 WARN_ON(data_len
< 0);
1633 * The space between the end of the leaf items and
1634 * the start of the leaf data. IOW, how much room
1635 * the leaf has left for both items and data
1637 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1639 u32 nodesize
= (root
? BTRFS_LEAF_DATA_SIZE(root
->fs_info
) : leaf
->len
);
1640 int nritems
= btrfs_header_nritems(leaf
);
1642 ret
= nodesize
- leaf_space_used(leaf
, 0, nritems
);
1644 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1645 ret
, nodesize
, leaf_space_used(leaf
, 0, nritems
),
1652 * push some data in the path leaf to the right, trying to free up at
1653 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1655 * returns 1 if the push failed because the other node didn't have enough
1656 * room, 0 if everything worked out and < 0 if there were major errors.
1658 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1659 *root
, struct btrfs_path
*path
, int data_size
,
1662 struct extent_buffer
*left
= path
->nodes
[0];
1663 struct extent_buffer
*right
;
1664 struct extent_buffer
*upper
;
1665 struct btrfs_disk_key disk_key
;
1666 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1672 struct btrfs_item
*item
;
1680 slot
= path
->slots
[1];
1681 if (!path
->nodes
[1]) {
1684 upper
= path
->nodes
[1];
1685 if (slot
>= btrfs_header_nritems(upper
) - 1)
1688 right
= read_node_slot(fs_info
, upper
, slot
+ 1);
1689 if (!extent_buffer_uptodate(right
)) {
1691 return PTR_ERR(right
);
1694 free_space
= btrfs_leaf_free_space(root
, right
);
1695 if (free_space
< data_size
) {
1696 free_extent_buffer(right
);
1700 /* cow and double check */
1701 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1704 free_extent_buffer(right
);
1707 free_space
= btrfs_leaf_free_space(root
, right
);
1708 if (free_space
< data_size
) {
1709 free_extent_buffer(right
);
1713 left_nritems
= btrfs_header_nritems(left
);
1714 if (left_nritems
== 0) {
1715 free_extent_buffer(right
);
1724 i
= left_nritems
- 1;
1726 item
= btrfs_item_nr(i
);
1728 if (path
->slots
[0] == i
)
1729 push_space
+= data_size
+ sizeof(*item
);
1731 this_item_size
= btrfs_item_size(left
, item
);
1732 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1735 push_space
+= this_item_size
+ sizeof(*item
);
1741 if (push_items
== 0) {
1742 free_extent_buffer(right
);
1746 if (!empty
&& push_items
== left_nritems
)
1749 /* push left to right */
1750 right_nritems
= btrfs_header_nritems(right
);
1752 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1753 push_space
-= leaf_data_end(fs_info
, left
);
1755 /* make room in the right data area */
1756 data_end
= leaf_data_end(fs_info
, right
);
1757 memmove_extent_buffer(right
,
1758 btrfs_leaf_data(right
) + data_end
- push_space
,
1759 btrfs_leaf_data(right
) + data_end
,
1760 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - data_end
);
1762 /* copy from the left data area */
1763 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1764 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - push_space
,
1765 btrfs_leaf_data(left
) + leaf_data_end(fs_info
, left
),
1768 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1769 btrfs_item_nr_offset(0),
1770 right_nritems
* sizeof(struct btrfs_item
));
1772 /* copy the items from left to right */
1773 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1774 btrfs_item_nr_offset(left_nritems
- push_items
),
1775 push_items
* sizeof(struct btrfs_item
));
1777 /* update the item pointers */
1778 right_nritems
+= push_items
;
1779 btrfs_set_header_nritems(right
, right_nritems
);
1780 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1781 for (i
= 0; i
< right_nritems
; i
++) {
1782 item
= btrfs_item_nr(i
);
1783 push_space
-= btrfs_item_size(right
, item
);
1784 btrfs_set_item_offset(right
, item
, push_space
);
1787 left_nritems
-= push_items
;
1788 btrfs_set_header_nritems(left
, left_nritems
);
1791 btrfs_mark_buffer_dirty(left
);
1792 btrfs_mark_buffer_dirty(right
);
1794 btrfs_item_key(right
, &disk_key
, 0);
1795 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1796 btrfs_mark_buffer_dirty(upper
);
1798 /* then fixup the leaf pointer in the path */
1799 if (path
->slots
[0] >= left_nritems
) {
1800 path
->slots
[0] -= left_nritems
;
1801 free_extent_buffer(path
->nodes
[0]);
1802 path
->nodes
[0] = right
;
1803 path
->slots
[1] += 1;
1805 free_extent_buffer(right
);
1810 * push some data in the path leaf to the left, trying to free up at
1811 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1813 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1814 *root
, struct btrfs_path
*path
, int data_size
,
1817 struct btrfs_disk_key disk_key
;
1818 struct extent_buffer
*right
= path
->nodes
[0];
1819 struct extent_buffer
*left
;
1820 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1826 struct btrfs_item
*item
;
1827 u32 old_left_nritems
;
1832 u32 old_left_item_size
;
1834 slot
= path
->slots
[1];
1837 if (!path
->nodes
[1])
1840 right_nritems
= btrfs_header_nritems(right
);
1841 if (right_nritems
== 0) {
1845 left
= read_node_slot(fs_info
, path
->nodes
[1], slot
- 1);
1846 free_space
= btrfs_leaf_free_space(root
, left
);
1847 if (free_space
< data_size
) {
1848 free_extent_buffer(left
);
1852 /* cow and double check */
1853 ret
= btrfs_cow_block(trans
, root
, left
,
1854 path
->nodes
[1], slot
- 1, &left
);
1856 /* we hit -ENOSPC, but it isn't fatal here */
1857 free_extent_buffer(left
);
1861 free_space
= btrfs_leaf_free_space(root
, left
);
1862 if (free_space
< data_size
) {
1863 free_extent_buffer(left
);
1870 nr
= right_nritems
- 1;
1872 for (i
= 0; i
< nr
; i
++) {
1873 item
= btrfs_item_nr(i
);
1875 if (path
->slots
[0] == i
)
1876 push_space
+= data_size
+ sizeof(*item
);
1878 this_item_size
= btrfs_item_size(right
, item
);
1879 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1883 push_space
+= this_item_size
+ sizeof(*item
);
1886 if (push_items
== 0) {
1887 free_extent_buffer(left
);
1890 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1893 /* push data from right to left */
1894 copy_extent_buffer(left
, right
,
1895 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1896 btrfs_item_nr_offset(0),
1897 push_items
* sizeof(struct btrfs_item
));
1899 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1900 btrfs_item_offset_nr(right
, push_items
-1);
1902 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1903 leaf_data_end(fs_info
, left
) - push_space
,
1904 btrfs_leaf_data(right
) +
1905 btrfs_item_offset_nr(right
, push_items
- 1),
1907 old_left_nritems
= btrfs_header_nritems(left
);
1908 BUG_ON(old_left_nritems
== 0);
1910 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1911 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1914 item
= btrfs_item_nr(i
);
1915 ioff
= btrfs_item_offset(left
, item
);
1916 btrfs_set_item_offset(left
, item
,
1917 ioff
- (BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1918 old_left_item_size
));
1920 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1922 /* fixup right node */
1923 if (push_items
> right_nritems
) {
1924 printk("push items %d nr %u\n", push_items
, right_nritems
);
1928 if (push_items
< right_nritems
) {
1929 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1930 leaf_data_end(fs_info
, right
);
1931 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1932 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1934 btrfs_leaf_data(right
) +
1935 leaf_data_end(fs_info
, right
),
1938 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1939 btrfs_item_nr_offset(push_items
),
1940 (btrfs_header_nritems(right
) - push_items
) *
1941 sizeof(struct btrfs_item
));
1943 right_nritems
-= push_items
;
1944 btrfs_set_header_nritems(right
, right_nritems
);
1945 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1946 for (i
= 0; i
< right_nritems
; i
++) {
1947 item
= btrfs_item_nr(i
);
1948 push_space
= push_space
- btrfs_item_size(right
, item
);
1949 btrfs_set_item_offset(right
, item
, push_space
);
1952 btrfs_mark_buffer_dirty(left
);
1954 btrfs_mark_buffer_dirty(right
);
1956 btrfs_item_key(right
, &disk_key
, 0);
1957 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1959 /* then fixup the leaf pointer in the path */
1960 if (path
->slots
[0] < push_items
) {
1961 path
->slots
[0] += old_left_nritems
;
1962 free_extent_buffer(path
->nodes
[0]);
1963 path
->nodes
[0] = left
;
1964 path
->slots
[1] -= 1;
1966 free_extent_buffer(left
);
1967 path
->slots
[0] -= push_items
;
1969 BUG_ON(path
->slots
[0] < 0);
1974 * split the path's leaf in two, making sure there is at least data_size
1975 * available for the resulting leaf level of the path.
1977 * returns 0 if all went well and < 0 on failure.
1979 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
1980 struct btrfs_root
*root
,
1981 struct btrfs_path
*path
,
1982 struct extent_buffer
*l
,
1983 struct extent_buffer
*right
,
1984 int slot
, int mid
, int nritems
)
1991 struct btrfs_disk_key disk_key
;
1993 nritems
= nritems
- mid
;
1994 btrfs_set_header_nritems(right
, nritems
);
1995 data_copy_size
= btrfs_item_end_nr(l
, mid
) -
1996 leaf_data_end(root
->fs_info
, l
);
1998 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
1999 btrfs_item_nr_offset(mid
),
2000 nritems
* sizeof(struct btrfs_item
));
2002 copy_extent_buffer(right
, l
,
2003 btrfs_leaf_data(right
) +
2004 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2005 data_copy_size
, btrfs_leaf_data(l
) +
2006 leaf_data_end(root
->fs_info
, l
), data_copy_size
);
2008 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2009 btrfs_item_end_nr(l
, mid
);
2011 for (i
= 0; i
< nritems
; i
++) {
2012 struct btrfs_item
*item
= btrfs_item_nr(i
);
2013 u32 ioff
= btrfs_item_offset(right
, item
);
2014 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2017 btrfs_set_header_nritems(l
, mid
);
2019 btrfs_item_key(right
, &disk_key
, 0);
2020 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2021 path
->slots
[1] + 1, 1);
2025 btrfs_mark_buffer_dirty(right
);
2026 btrfs_mark_buffer_dirty(l
);
2027 BUG_ON(path
->slots
[0] != slot
);
2030 free_extent_buffer(path
->nodes
[0]);
2031 path
->nodes
[0] = right
;
2032 path
->slots
[0] -= mid
;
2033 path
->slots
[1] += 1;
2035 free_extent_buffer(right
);
2038 BUG_ON(path
->slots
[0] < 0);
2044 * split the path's leaf in two, making sure there is at least data_size
2045 * available for the resulting leaf level of the path.
2047 * returns 0 if all went well and < 0 on failure.
2049 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2050 struct btrfs_root
*root
,
2051 struct btrfs_key
*ins_key
,
2052 struct btrfs_path
*path
, int data_size
,
2055 struct btrfs_disk_key disk_key
;
2056 struct extent_buffer
*l
;
2060 struct extent_buffer
*right
;
2064 int num_doubles
= 0;
2067 slot
= path
->slots
[0];
2068 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2069 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
->fs_info
))
2072 /* first try to make some room by pushing left and right */
2073 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2074 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2078 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2084 /* did the pushes work? */
2085 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2089 if (!path
->nodes
[1]) {
2090 ret
= insert_new_root(trans
, root
, path
, 1);
2097 slot
= path
->slots
[0];
2098 nritems
= btrfs_header_nritems(l
);
2099 mid
= (nritems
+ 1) / 2;
2103 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2104 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2105 if (slot
>= nritems
) {
2109 if (mid
!= nritems
&&
2110 leaf_space_used(l
, mid
, nritems
- mid
) +
2112 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2118 if (leaf_space_used(l
, 0, mid
) + data_size
>
2119 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2120 if (!extend
&& data_size
&& slot
== 0) {
2122 } else if ((extend
|| !data_size
) && slot
== 0) {
2126 if (mid
!= nritems
&&
2127 leaf_space_used(l
, mid
, nritems
- mid
) +
2129 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2137 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2139 btrfs_item_key(l
, &disk_key
, mid
);
2141 right
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
2142 root
->root_key
.objectid
,
2143 &disk_key
, 0, l
->start
, 0);
2144 if (IS_ERR(right
)) {
2146 return PTR_ERR(right
);
2149 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2150 btrfs_set_header_bytenr(right
, right
->start
);
2151 btrfs_set_header_generation(right
, trans
->transid
);
2152 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2153 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2154 btrfs_set_header_level(right
, 0);
2155 write_extent_buffer(right
, root
->fs_info
->fsid
,
2156 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
2158 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2159 btrfs_header_chunk_tree_uuid(right
),
2164 btrfs_set_header_nritems(right
, 0);
2165 wret
= insert_ptr(trans
, root
, path
,
2166 &disk_key
, right
->start
,
2167 path
->slots
[1] + 1, 1);
2171 free_extent_buffer(path
->nodes
[0]);
2172 path
->nodes
[0] = right
;
2174 path
->slots
[1] += 1;
2176 btrfs_set_header_nritems(right
, 0);
2177 wret
= insert_ptr(trans
, root
, path
,
2183 free_extent_buffer(path
->nodes
[0]);
2184 path
->nodes
[0] = right
;
2186 if (path
->slots
[1] == 0) {
2187 btrfs_fixup_low_keys(root
, path
,
2191 btrfs_mark_buffer_dirty(right
);
2195 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2199 BUG_ON(num_doubles
!= 0);
2208 * This function splits a single item into two items,
2209 * giving 'new_key' to the new item and splitting the
2210 * old one at split_offset (from the start of the item).
2212 * The path may be released by this operation. After
2213 * the split, the path is pointing to the old item. The
2214 * new item is going to be in the same node as the old one.
2216 * Note, the item being split must be smaller enough to live alone on
2217 * a tree block with room for one extra struct btrfs_item
2219 * This allows us to split the item in place, keeping a lock on the
2220 * leaf the entire time.
2222 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2223 struct btrfs_root
*root
,
2224 struct btrfs_path
*path
,
2225 struct btrfs_key
*new_key
,
2226 unsigned long split_offset
)
2229 struct extent_buffer
*leaf
;
2230 struct btrfs_key orig_key
;
2231 struct btrfs_item
*item
;
2232 struct btrfs_item
*new_item
;
2237 struct btrfs_disk_key disk_key
;
2240 leaf
= path
->nodes
[0];
2241 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
2242 if (btrfs_leaf_free_space(root
, leaf
) >= sizeof(struct btrfs_item
))
2245 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2246 btrfs_release_path(path
);
2248 path
->search_for_split
= 1;
2250 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
2251 path
->search_for_split
= 0;
2253 /* if our item isn't there or got smaller, return now */
2254 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
2259 ret
= split_leaf(trans
, root
, &orig_key
, path
, 0, 0);
2262 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
2263 leaf
= path
->nodes
[0];
2266 item
= btrfs_item_nr(path
->slots
[0]);
2267 orig_offset
= btrfs_item_offset(leaf
, item
);
2268 item_size
= btrfs_item_size(leaf
, item
);
2271 buf
= kmalloc(item_size
, GFP_NOFS
);
2273 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
2274 path
->slots
[0]), item_size
);
2275 slot
= path
->slots
[0] + 1;
2276 leaf
= path
->nodes
[0];
2278 nritems
= btrfs_header_nritems(leaf
);
2280 if (slot
< nritems
) {
2281 /* shift the items */
2282 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2283 btrfs_item_nr_offset(slot
),
2284 (nritems
- slot
) * sizeof(struct btrfs_item
));
2288 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
2289 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2291 new_item
= btrfs_item_nr(slot
);
2293 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
2294 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
2296 btrfs_set_item_offset(leaf
, item
,
2297 orig_offset
+ item_size
- split_offset
);
2298 btrfs_set_item_size(leaf
, item
, split_offset
);
2300 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2302 /* write the data for the start of the original item */
2303 write_extent_buffer(leaf
, buf
,
2304 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2307 /* write the data for the new item */
2308 write_extent_buffer(leaf
, buf
+ split_offset
,
2309 btrfs_item_ptr_offset(leaf
, slot
),
2310 item_size
- split_offset
);
2311 btrfs_mark_buffer_dirty(leaf
);
2314 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2315 btrfs_print_leaf(root
, leaf
);
2322 int btrfs_truncate_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2323 u32 new_size
, int from_end
)
2327 struct extent_buffer
*leaf
;
2328 struct btrfs_item
*item
;
2330 unsigned int data_end
;
2331 unsigned int old_data_start
;
2332 unsigned int old_size
;
2333 unsigned int size_diff
;
2336 leaf
= path
->nodes
[0];
2337 slot
= path
->slots
[0];
2339 old_size
= btrfs_item_size_nr(leaf
, slot
);
2340 if (old_size
== new_size
)
2343 nritems
= btrfs_header_nritems(leaf
);
2344 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2346 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2348 size_diff
= old_size
- new_size
;
2351 BUG_ON(slot
>= nritems
);
2354 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2356 /* first correct the data pointers */
2357 for (i
= slot
; i
< nritems
; i
++) {
2359 item
= btrfs_item_nr(i
);
2360 ioff
= btrfs_item_offset(leaf
, item
);
2361 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2364 /* shift the data */
2366 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2367 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2368 data_end
, old_data_start
+ new_size
- data_end
);
2370 struct btrfs_disk_key disk_key
;
2373 btrfs_item_key(leaf
, &disk_key
, slot
);
2375 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2377 struct btrfs_file_extent_item
*fi
;
2379 fi
= btrfs_item_ptr(leaf
, slot
,
2380 struct btrfs_file_extent_item
);
2381 fi
= (struct btrfs_file_extent_item
*)(
2382 (unsigned long)fi
- size_diff
);
2384 if (btrfs_file_extent_type(leaf
, fi
) ==
2385 BTRFS_FILE_EXTENT_INLINE
) {
2386 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2387 memmove_extent_buffer(leaf
, ptr
,
2389 offsetof(struct btrfs_file_extent_item
,
2394 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2395 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2396 data_end
, old_data_start
- data_end
);
2398 offset
= btrfs_disk_key_offset(&disk_key
);
2399 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2400 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2402 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2405 item
= btrfs_item_nr(slot
);
2406 btrfs_set_item_size(leaf
, item
, new_size
);
2407 btrfs_mark_buffer_dirty(leaf
);
2410 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2411 btrfs_print_leaf(root
, leaf
);
2417 int btrfs_extend_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2422 struct extent_buffer
*leaf
;
2423 struct btrfs_item
*item
;
2425 unsigned int data_end
;
2426 unsigned int old_data
;
2427 unsigned int old_size
;
2430 leaf
= path
->nodes
[0];
2432 nritems
= btrfs_header_nritems(leaf
);
2433 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2435 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
2436 btrfs_print_leaf(root
, leaf
);
2439 slot
= path
->slots
[0];
2440 old_data
= btrfs_item_end_nr(leaf
, slot
);
2443 if (slot
>= nritems
) {
2444 btrfs_print_leaf(root
, leaf
);
2445 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2450 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2452 /* first correct the data pointers */
2453 for (i
= slot
; i
< nritems
; i
++) {
2455 item
= btrfs_item_nr(i
);
2456 ioff
= btrfs_item_offset(leaf
, item
);
2457 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2460 /* shift the data */
2461 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2462 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2463 data_end
, old_data
- data_end
);
2465 data_end
= old_data
;
2466 old_size
= btrfs_item_size_nr(leaf
, slot
);
2467 item
= btrfs_item_nr(slot
);
2468 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2469 btrfs_mark_buffer_dirty(leaf
);
2472 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2473 btrfs_print_leaf(root
, leaf
);
2480 * Given a key and some data, insert an item into the tree.
2481 * This does all the path init required, making room in the tree if needed.
2483 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2484 struct btrfs_root
*root
,
2485 struct btrfs_path
*path
,
2486 struct btrfs_key
*cpu_key
, u32
*data_size
,
2489 struct extent_buffer
*leaf
;
2490 struct btrfs_item
*item
;
2497 unsigned int data_end
;
2498 struct btrfs_disk_key disk_key
;
2500 for (i
= 0; i
< nr
; i
++) {
2501 total_data
+= data_size
[i
];
2504 /* create a root if there isn't one */
2508 total_size
= total_data
+ nr
* sizeof(struct btrfs_item
);
2509 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2516 leaf
= path
->nodes
[0];
2518 nritems
= btrfs_header_nritems(leaf
);
2519 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2521 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
2522 btrfs_print_leaf(root
, leaf
);
2523 printk("not enough freespace need %u have %d\n",
2524 total_size
, btrfs_leaf_free_space(root
, leaf
));
2528 slot
= path
->slots
[0];
2531 if (slot
< nritems
) {
2532 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2534 if (old_data
< data_end
) {
2535 btrfs_print_leaf(root
, leaf
);
2536 printk("slot %d old_data %d data_end %d\n",
2537 slot
, old_data
, data_end
);
2541 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2543 /* first correct the data pointers */
2544 for (i
= slot
; i
< nritems
; i
++) {
2547 item
= btrfs_item_nr(i
);
2548 ioff
= btrfs_item_offset(leaf
, item
);
2549 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2552 /* shift the items */
2553 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2554 btrfs_item_nr_offset(slot
),
2555 (nritems
- slot
) * sizeof(struct btrfs_item
));
2557 /* shift the data */
2558 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2559 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2560 data_end
, old_data
- data_end
);
2561 data_end
= old_data
;
2564 /* setup the item for the new data */
2565 for (i
= 0; i
< nr
; i
++) {
2566 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2567 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2568 item
= btrfs_item_nr(slot
+ i
);
2569 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2570 data_end
-= data_size
[i
];
2571 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2573 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2574 btrfs_mark_buffer_dirty(leaf
);
2578 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2579 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2582 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
2583 btrfs_print_leaf(root
, leaf
);
2592 * Given a key and some data, insert an item into the tree.
2593 * This does all the path init required, making room in the tree if needed.
2595 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2596 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2600 struct btrfs_path
*path
;
2601 struct extent_buffer
*leaf
;
2604 path
= btrfs_alloc_path();
2608 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2610 leaf
= path
->nodes
[0];
2611 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2612 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2613 btrfs_mark_buffer_dirty(leaf
);
2615 btrfs_free_path(path
);
2620 * delete the pointer from a given node.
2622 * If the delete empties a node, the node is removed from the tree,
2623 * continuing all the way the root if required. The root is converted into
2624 * a leaf if all the nodes are emptied.
2626 int btrfs_del_ptr(struct btrfs_root
*root
, struct btrfs_path
*path
,
2627 int level
, int slot
)
2629 struct extent_buffer
*parent
= path
->nodes
[level
];
2633 nritems
= btrfs_header_nritems(parent
);
2634 if (slot
< nritems
- 1) {
2635 /* shift the items */
2636 memmove_extent_buffer(parent
,
2637 btrfs_node_key_ptr_offset(slot
),
2638 btrfs_node_key_ptr_offset(slot
+ 1),
2639 sizeof(struct btrfs_key_ptr
) *
2640 (nritems
- slot
- 1));
2643 btrfs_set_header_nritems(parent
, nritems
);
2644 if (nritems
== 0 && parent
== root
->node
) {
2645 BUG_ON(btrfs_header_level(root
->node
) != 1);
2646 /* just turn the root into a leaf and break */
2647 btrfs_set_header_level(root
->node
, 0);
2648 } else if (slot
== 0) {
2649 struct btrfs_disk_key disk_key
;
2651 btrfs_node_key(parent
, &disk_key
, 0);
2652 btrfs_fixup_low_keys(root
, path
, &disk_key
, level
+ 1);
2654 btrfs_mark_buffer_dirty(parent
);
2659 * a helper function to delete the leaf pointed to by path->slots[1] and
2662 * This deletes the pointer in path->nodes[1] and frees the leaf
2663 * block extent. zero is returned if it all worked out, < 0 otherwise.
2665 * The path must have already been setup for deleting the leaf, including
2666 * all the proper balancing. path->nodes[1] must be locked.
2668 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
2669 struct btrfs_root
*root
,
2670 struct btrfs_path
*path
,
2671 struct extent_buffer
*leaf
)
2675 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
2676 ret
= btrfs_del_ptr(root
, path
, 1, path
->slots
[1]);
2680 ret
= btrfs_free_extent(trans
, root
, leaf
->start
, leaf
->len
,
2681 0, root
->root_key
.objectid
, 0, 0);
2686 * delete the item at the leaf level in path. If that empties
2687 * the leaf, remove it from the tree
2689 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2690 struct btrfs_path
*path
, int slot
, int nr
)
2692 struct extent_buffer
*leaf
;
2693 struct btrfs_item
*item
;
2701 leaf
= path
->nodes
[0];
2702 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2704 for (i
= 0; i
< nr
; i
++)
2705 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2707 nritems
= btrfs_header_nritems(leaf
);
2709 if (slot
+ nr
!= nritems
) {
2710 int data_end
= leaf_data_end(root
->fs_info
, leaf
);
2712 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2714 btrfs_leaf_data(leaf
) + data_end
,
2715 last_off
- data_end
);
2717 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2720 item
= btrfs_item_nr(i
);
2721 ioff
= btrfs_item_offset(leaf
, item
);
2722 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2725 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2726 btrfs_item_nr_offset(slot
+ nr
),
2727 sizeof(struct btrfs_item
) *
2728 (nritems
- slot
- nr
));
2730 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2733 /* delete the leaf if we've emptied it */
2735 if (leaf
== root
->node
) {
2736 btrfs_set_header_level(leaf
, 0);
2738 clean_tree_block(trans
, root
, leaf
);
2739 wret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2745 int used
= leaf_space_used(leaf
, 0, nritems
);
2747 struct btrfs_disk_key disk_key
;
2749 btrfs_item_key(leaf
, &disk_key
, 0);
2750 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2753 /* delete the leaf if it is mostly empty */
2754 if (used
< BTRFS_LEAF_DATA_SIZE(root
->fs_info
) / 4) {
2755 /* push_leaf_left fixes the path.
2756 * make sure the path still points to our leaf
2757 * for possible call to del_ptr below
2759 slot
= path
->slots
[1];
2760 extent_buffer_get(leaf
);
2762 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2763 if (wret
< 0 && wret
!= -ENOSPC
)
2766 if (path
->nodes
[0] == leaf
&&
2767 btrfs_header_nritems(leaf
)) {
2768 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2769 if (wret
< 0 && wret
!= -ENOSPC
)
2773 if (btrfs_header_nritems(leaf
) == 0) {
2774 clean_tree_block(trans
, root
, leaf
);
2775 path
->slots
[1] = slot
;
2776 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2778 free_extent_buffer(leaf
);
2781 btrfs_mark_buffer_dirty(leaf
);
2782 free_extent_buffer(leaf
);
2785 btrfs_mark_buffer_dirty(leaf
);
2792 * walk up the tree as far as required to find the previous leaf.
2793 * returns 0 if it found something or 1 if there are no lesser leaves.
2794 * returns < 0 on io errors.
2796 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2800 struct extent_buffer
*c
;
2801 struct extent_buffer
*next
= NULL
;
2802 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2804 while(level
< BTRFS_MAX_LEVEL
) {
2805 if (!path
->nodes
[level
])
2808 slot
= path
->slots
[level
];
2809 c
= path
->nodes
[level
];
2812 if (level
== BTRFS_MAX_LEVEL
)
2818 next
= read_node_slot(fs_info
, c
, slot
);
2819 if (!extent_buffer_uptodate(next
)) {
2821 return PTR_ERR(next
);
2826 path
->slots
[level
] = slot
;
2829 c
= path
->nodes
[level
];
2830 free_extent_buffer(c
);
2831 slot
= btrfs_header_nritems(next
);
2834 path
->nodes
[level
] = next
;
2835 path
->slots
[level
] = slot
;
2838 next
= read_node_slot(fs_info
, next
, slot
);
2839 if (!extent_buffer_uptodate(next
)) {
2841 return PTR_ERR(next
);
2849 * walk up the tree as far as required to find the next leaf.
2850 * returns 0 if it found something or 1 if there are no greater leaves.
2851 * returns < 0 on io errors.
2853 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2857 struct extent_buffer
*c
;
2858 struct extent_buffer
*next
= NULL
;
2859 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2861 while(level
< BTRFS_MAX_LEVEL
) {
2862 if (!path
->nodes
[level
])
2865 slot
= path
->slots
[level
] + 1;
2866 c
= path
->nodes
[level
];
2867 if (slot
>= btrfs_header_nritems(c
)) {
2869 if (level
== BTRFS_MAX_LEVEL
)
2875 reada_for_search(root
, path
, level
, slot
, 0);
2877 next
= read_node_slot(fs_info
, c
, slot
);
2878 if (!extent_buffer_uptodate(next
))
2882 path
->slots
[level
] = slot
;
2885 c
= path
->nodes
[level
];
2886 free_extent_buffer(c
);
2887 path
->nodes
[level
] = next
;
2888 path
->slots
[level
] = 0;
2892 reada_for_search(root
, path
, level
, 0, 0);
2893 next
= read_node_slot(fs_info
, next
, 0);
2894 if (!extent_buffer_uptodate(next
))
2900 int btrfs_previous_item(struct btrfs_root
*root
,
2901 struct btrfs_path
*path
, u64 min_objectid
,
2904 struct btrfs_key found_key
;
2905 struct extent_buffer
*leaf
;
2910 if (path
->slots
[0] == 0) {
2911 ret
= btrfs_prev_leaf(root
, path
);
2917 leaf
= path
->nodes
[0];
2918 nritems
= btrfs_header_nritems(leaf
);
2921 if (path
->slots
[0] == nritems
)
2924 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2925 if (found_key
.objectid
< min_objectid
)
2927 if (found_key
.type
== type
)
2929 if (found_key
.objectid
== min_objectid
&&
2930 found_key
.type
< type
)
2937 * search in extent tree to find a previous Metadata/Data extent item with
2940 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2942 int btrfs_previous_extent_item(struct btrfs_root
*root
,
2943 struct btrfs_path
*path
, u64 min_objectid
)
2945 struct btrfs_key found_key
;
2946 struct extent_buffer
*leaf
;
2951 if (path
->slots
[0] == 0) {
2952 ret
= btrfs_prev_leaf(root
, path
);
2958 leaf
= path
->nodes
[0];
2959 nritems
= btrfs_header_nritems(leaf
);
2962 if (path
->slots
[0] == nritems
)
2965 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2966 if (found_key
.objectid
< min_objectid
)
2968 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
2969 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)
2971 if (found_key
.objectid
== min_objectid
&&
2972 found_key
.type
< BTRFS_EXTENT_ITEM_KEY
)
2979 * Search in extent tree to found next meta/data extent
2980 * Caller needs to check for no-hole or skinny metadata features.
2982 int btrfs_next_extent_item(struct btrfs_root
*root
,
2983 struct btrfs_path
*path
, u64 max_objectid
)
2985 struct btrfs_key found_key
;
2989 ret
= btrfs_next_item(root
, path
);
2992 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2994 if (found_key
.objectid
> max_objectid
)
2996 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
2997 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)