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(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(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(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 extent_buffer
*leaf
)
1639 int nritems
= btrfs_header_nritems(leaf
);
1643 BUG_ON(leaf
->fs_info
&& leaf
->fs_info
->nodesize
!= leaf
->len
);
1644 leaf_data_size
= __BTRFS_LEAF_DATA_SIZE(leaf
->len
);
1645 ret
= leaf_data_size
- leaf_space_used(leaf
, 0 ,nritems
);
1647 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1648 ret
, leaf_data_size
, leaf_space_used(leaf
, 0, nritems
),
1655 * push some data in the path leaf to the right, trying to free up at
1656 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1658 * returns 1 if the push failed because the other node didn't have enough
1659 * room, 0 if everything worked out and < 0 if there were major errors.
1661 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
1662 *root
, struct btrfs_path
*path
, int data_size
,
1665 struct extent_buffer
*left
= path
->nodes
[0];
1666 struct extent_buffer
*right
;
1667 struct extent_buffer
*upper
;
1668 struct btrfs_disk_key disk_key
;
1669 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1675 struct btrfs_item
*item
;
1683 slot
= path
->slots
[1];
1684 if (!path
->nodes
[1]) {
1687 upper
= path
->nodes
[1];
1688 if (slot
>= btrfs_header_nritems(upper
) - 1)
1691 right
= read_node_slot(fs_info
, upper
, slot
+ 1);
1692 if (!extent_buffer_uptodate(right
)) {
1694 return PTR_ERR(right
);
1697 free_space
= btrfs_leaf_free_space(right
);
1698 if (free_space
< data_size
) {
1699 free_extent_buffer(right
);
1703 /* cow and double check */
1704 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
1707 free_extent_buffer(right
);
1710 free_space
= btrfs_leaf_free_space(right
);
1711 if (free_space
< data_size
) {
1712 free_extent_buffer(right
);
1716 left_nritems
= btrfs_header_nritems(left
);
1717 if (left_nritems
== 0) {
1718 free_extent_buffer(right
);
1727 i
= left_nritems
- 1;
1729 item
= btrfs_item_nr(i
);
1731 if (path
->slots
[0] == i
)
1732 push_space
+= data_size
+ sizeof(*item
);
1734 this_item_size
= btrfs_item_size(left
, item
);
1735 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1738 push_space
+= this_item_size
+ sizeof(*item
);
1744 if (push_items
== 0) {
1745 free_extent_buffer(right
);
1749 if (!empty
&& push_items
== left_nritems
)
1752 /* push left to right */
1753 right_nritems
= btrfs_header_nritems(right
);
1755 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
1756 push_space
-= leaf_data_end(fs_info
, left
);
1758 /* make room in the right data area */
1759 data_end
= leaf_data_end(fs_info
, right
);
1760 memmove_extent_buffer(right
,
1761 btrfs_leaf_data(right
) + data_end
- push_space
,
1762 btrfs_leaf_data(right
) + data_end
,
1763 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - data_end
);
1765 /* copy from the left data area */
1766 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
1767 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) - push_space
,
1768 btrfs_leaf_data(left
) + leaf_data_end(fs_info
, left
),
1771 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
1772 btrfs_item_nr_offset(0),
1773 right_nritems
* sizeof(struct btrfs_item
));
1775 /* copy the items from left to right */
1776 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
1777 btrfs_item_nr_offset(left_nritems
- push_items
),
1778 push_items
* sizeof(struct btrfs_item
));
1780 /* update the item pointers */
1781 right_nritems
+= push_items
;
1782 btrfs_set_header_nritems(right
, right_nritems
);
1783 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1784 for (i
= 0; i
< right_nritems
; i
++) {
1785 item
= btrfs_item_nr(i
);
1786 push_space
-= btrfs_item_size(right
, item
);
1787 btrfs_set_item_offset(right
, item
, push_space
);
1790 left_nritems
-= push_items
;
1791 btrfs_set_header_nritems(left
, left_nritems
);
1794 btrfs_mark_buffer_dirty(left
);
1795 btrfs_mark_buffer_dirty(right
);
1797 btrfs_item_key(right
, &disk_key
, 0);
1798 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
1799 btrfs_mark_buffer_dirty(upper
);
1801 /* then fixup the leaf pointer in the path */
1802 if (path
->slots
[0] >= left_nritems
) {
1803 path
->slots
[0] -= left_nritems
;
1804 free_extent_buffer(path
->nodes
[0]);
1805 path
->nodes
[0] = right
;
1806 path
->slots
[1] += 1;
1808 free_extent_buffer(right
);
1813 * push some data in the path leaf to the left, trying to free up at
1814 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1816 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
1817 *root
, struct btrfs_path
*path
, int data_size
,
1820 struct btrfs_disk_key disk_key
;
1821 struct extent_buffer
*right
= path
->nodes
[0];
1822 struct extent_buffer
*left
;
1823 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1829 struct btrfs_item
*item
;
1830 u32 old_left_nritems
;
1835 u32 old_left_item_size
;
1837 slot
= path
->slots
[1];
1840 if (!path
->nodes
[1])
1843 right_nritems
= btrfs_header_nritems(right
);
1844 if (right_nritems
== 0) {
1848 left
= read_node_slot(fs_info
, path
->nodes
[1], slot
- 1);
1849 free_space
= btrfs_leaf_free_space(left
);
1850 if (free_space
< data_size
) {
1851 free_extent_buffer(left
);
1855 /* cow and double check */
1856 ret
= btrfs_cow_block(trans
, root
, left
,
1857 path
->nodes
[1], slot
- 1, &left
);
1859 /* we hit -ENOSPC, but it isn't fatal here */
1860 free_extent_buffer(left
);
1864 free_space
= btrfs_leaf_free_space(left
);
1865 if (free_space
< data_size
) {
1866 free_extent_buffer(left
);
1873 nr
= right_nritems
- 1;
1875 for (i
= 0; i
< nr
; i
++) {
1876 item
= btrfs_item_nr(i
);
1878 if (path
->slots
[0] == i
)
1879 push_space
+= data_size
+ sizeof(*item
);
1881 this_item_size
= btrfs_item_size(right
, item
);
1882 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
1886 push_space
+= this_item_size
+ sizeof(*item
);
1889 if (push_items
== 0) {
1890 free_extent_buffer(left
);
1893 if (!empty
&& push_items
== btrfs_header_nritems(right
))
1896 /* push data from right to left */
1897 copy_extent_buffer(left
, right
,
1898 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
1899 btrfs_item_nr_offset(0),
1900 push_items
* sizeof(struct btrfs_item
));
1902 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1903 btrfs_item_offset_nr(right
, push_items
-1);
1905 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
1906 leaf_data_end(fs_info
, left
) - push_space
,
1907 btrfs_leaf_data(right
) +
1908 btrfs_item_offset_nr(right
, push_items
- 1),
1910 old_left_nritems
= btrfs_header_nritems(left
);
1911 BUG_ON(old_left_nritems
== 0);
1913 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
1914 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
1917 item
= btrfs_item_nr(i
);
1918 ioff
= btrfs_item_offset(left
, item
);
1919 btrfs_set_item_offset(left
, item
,
1920 ioff
- (BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1921 old_left_item_size
));
1923 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
1925 /* fixup right node */
1926 if (push_items
> right_nritems
) {
1927 printk("push items %d nr %u\n", push_items
, right_nritems
);
1931 if (push_items
< right_nritems
) {
1932 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
1933 leaf_data_end(fs_info
, right
);
1934 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
1935 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
1937 btrfs_leaf_data(right
) +
1938 leaf_data_end(fs_info
, right
),
1941 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
1942 btrfs_item_nr_offset(push_items
),
1943 (btrfs_header_nritems(right
) - push_items
) *
1944 sizeof(struct btrfs_item
));
1946 right_nritems
-= push_items
;
1947 btrfs_set_header_nritems(right
, right_nritems
);
1948 push_space
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
);
1949 for (i
= 0; i
< right_nritems
; i
++) {
1950 item
= btrfs_item_nr(i
);
1951 push_space
= push_space
- btrfs_item_size(right
, item
);
1952 btrfs_set_item_offset(right
, item
, push_space
);
1955 btrfs_mark_buffer_dirty(left
);
1957 btrfs_mark_buffer_dirty(right
);
1959 btrfs_item_key(right
, &disk_key
, 0);
1960 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
1962 /* then fixup the leaf pointer in the path */
1963 if (path
->slots
[0] < push_items
) {
1964 path
->slots
[0] += old_left_nritems
;
1965 free_extent_buffer(path
->nodes
[0]);
1966 path
->nodes
[0] = left
;
1967 path
->slots
[1] -= 1;
1969 free_extent_buffer(left
);
1970 path
->slots
[0] -= push_items
;
1972 BUG_ON(path
->slots
[0] < 0);
1977 * split the path's leaf in two, making sure there is at least data_size
1978 * available for the resulting leaf level of the path.
1980 * returns 0 if all went well and < 0 on failure.
1982 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
1983 struct btrfs_root
*root
,
1984 struct btrfs_path
*path
,
1985 struct extent_buffer
*l
,
1986 struct extent_buffer
*right
,
1987 int slot
, int mid
, int nritems
)
1994 struct btrfs_disk_key disk_key
;
1996 nritems
= nritems
- mid
;
1997 btrfs_set_header_nritems(right
, nritems
);
1998 data_copy_size
= btrfs_item_end_nr(l
, mid
) -
1999 leaf_data_end(root
->fs_info
, l
);
2001 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2002 btrfs_item_nr_offset(mid
),
2003 nritems
* sizeof(struct btrfs_item
));
2005 copy_extent_buffer(right
, l
,
2006 btrfs_leaf_data(right
) +
2007 BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2008 data_copy_size
, btrfs_leaf_data(l
) +
2009 leaf_data_end(root
->fs_info
, l
), data_copy_size
);
2011 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
->fs_info
) -
2012 btrfs_item_end_nr(l
, mid
);
2014 for (i
= 0; i
< nritems
; i
++) {
2015 struct btrfs_item
*item
= btrfs_item_nr(i
);
2016 u32 ioff
= btrfs_item_offset(right
, item
);
2017 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2020 btrfs_set_header_nritems(l
, mid
);
2022 btrfs_item_key(right
, &disk_key
, 0);
2023 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2024 path
->slots
[1] + 1, 1);
2028 btrfs_mark_buffer_dirty(right
);
2029 btrfs_mark_buffer_dirty(l
);
2030 BUG_ON(path
->slots
[0] != slot
);
2033 free_extent_buffer(path
->nodes
[0]);
2034 path
->nodes
[0] = right
;
2035 path
->slots
[0] -= mid
;
2036 path
->slots
[1] += 1;
2038 free_extent_buffer(right
);
2041 BUG_ON(path
->slots
[0] < 0);
2047 * split the path's leaf in two, making sure there is at least data_size
2048 * available for the resulting leaf level of the path.
2050 * returns 0 if all went well and < 0 on failure.
2052 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2053 struct btrfs_root
*root
,
2054 struct btrfs_key
*ins_key
,
2055 struct btrfs_path
*path
, int data_size
,
2058 struct btrfs_disk_key disk_key
;
2059 struct extent_buffer
*l
;
2063 struct extent_buffer
*right
;
2067 int num_doubles
= 0;
2070 slot
= path
->slots
[0];
2071 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2072 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
->fs_info
))
2075 /* first try to make some room by pushing left and right */
2076 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2077 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2081 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2087 /* did the pushes work? */
2088 if (btrfs_leaf_free_space(l
) >= data_size
)
2092 if (!path
->nodes
[1]) {
2093 ret
= insert_new_root(trans
, root
, path
, 1);
2100 slot
= path
->slots
[0];
2101 nritems
= btrfs_header_nritems(l
);
2102 mid
= (nritems
+ 1) / 2;
2106 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2107 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2108 if (slot
>= nritems
) {
2112 if (mid
!= nritems
&&
2113 leaf_space_used(l
, mid
, nritems
- mid
) +
2115 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2121 if (leaf_space_used(l
, 0, mid
) + data_size
>
2122 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2123 if (!extend
&& data_size
&& slot
== 0) {
2125 } else if ((extend
|| !data_size
) && slot
== 0) {
2129 if (mid
!= nritems
&&
2130 leaf_space_used(l
, mid
, nritems
- mid
) +
2132 BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
2140 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2142 btrfs_item_key(l
, &disk_key
, mid
);
2144 right
= btrfs_alloc_free_block(trans
, root
, root
->fs_info
->nodesize
,
2145 root
->root_key
.objectid
,
2146 &disk_key
, 0, l
->start
, 0);
2147 if (IS_ERR(right
)) {
2149 return PTR_ERR(right
);
2152 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2153 btrfs_set_header_bytenr(right
, right
->start
);
2154 btrfs_set_header_generation(right
, trans
->transid
);
2155 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2156 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2157 btrfs_set_header_level(right
, 0);
2158 write_extent_buffer(right
, root
->fs_info
->fsid
,
2159 btrfs_header_fsid(), BTRFS_FSID_SIZE
);
2161 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2162 btrfs_header_chunk_tree_uuid(right
),
2167 btrfs_set_header_nritems(right
, 0);
2168 wret
= insert_ptr(trans
, root
, path
,
2169 &disk_key
, right
->start
,
2170 path
->slots
[1] + 1, 1);
2174 free_extent_buffer(path
->nodes
[0]);
2175 path
->nodes
[0] = right
;
2177 path
->slots
[1] += 1;
2179 btrfs_set_header_nritems(right
, 0);
2180 wret
= insert_ptr(trans
, root
, path
,
2186 free_extent_buffer(path
->nodes
[0]);
2187 path
->nodes
[0] = right
;
2189 if (path
->slots
[1] == 0) {
2190 btrfs_fixup_low_keys(root
, path
,
2194 btrfs_mark_buffer_dirty(right
);
2198 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2202 BUG_ON(num_doubles
!= 0);
2211 * This function splits a single item into two items,
2212 * giving 'new_key' to the new item and splitting the
2213 * old one at split_offset (from the start of the item).
2215 * The path may be released by this operation. After
2216 * the split, the path is pointing to the old item. The
2217 * new item is going to be in the same node as the old one.
2219 * Note, the item being split must be smaller enough to live alone on
2220 * a tree block with room for one extra struct btrfs_item
2222 * This allows us to split the item in place, keeping a lock on the
2223 * leaf the entire time.
2225 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2226 struct btrfs_root
*root
,
2227 struct btrfs_path
*path
,
2228 struct btrfs_key
*new_key
,
2229 unsigned long split_offset
)
2232 struct extent_buffer
*leaf
;
2233 struct btrfs_key orig_key
;
2234 struct btrfs_item
*item
;
2235 struct btrfs_item
*new_item
;
2240 struct btrfs_disk_key disk_key
;
2243 leaf
= path
->nodes
[0];
2244 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
2245 if (btrfs_leaf_free_space(leaf
) >=
2246 sizeof(struct btrfs_item
))
2249 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2250 btrfs_release_path(path
);
2252 path
->search_for_split
= 1;
2254 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
2255 path
->search_for_split
= 0;
2257 /* if our item isn't there or got smaller, return now */
2258 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
2263 ret
= split_leaf(trans
, root
, &orig_key
, path
, 0, 0);
2266 BUG_ON(btrfs_leaf_free_space(leaf
) < sizeof(struct btrfs_item
));
2267 leaf
= path
->nodes
[0];
2270 item
= btrfs_item_nr(path
->slots
[0]);
2271 orig_offset
= btrfs_item_offset(leaf
, item
);
2272 item_size
= btrfs_item_size(leaf
, item
);
2275 buf
= kmalloc(item_size
, GFP_NOFS
);
2277 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
2278 path
->slots
[0]), item_size
);
2279 slot
= path
->slots
[0] + 1;
2280 leaf
= path
->nodes
[0];
2282 nritems
= btrfs_header_nritems(leaf
);
2284 if (slot
< nritems
) {
2285 /* shift the items */
2286 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
2287 btrfs_item_nr_offset(slot
),
2288 (nritems
- slot
) * sizeof(struct btrfs_item
));
2292 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
2293 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2295 new_item
= btrfs_item_nr(slot
);
2297 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
2298 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
2300 btrfs_set_item_offset(leaf
, item
,
2301 orig_offset
+ item_size
- split_offset
);
2302 btrfs_set_item_size(leaf
, item
, split_offset
);
2304 btrfs_set_header_nritems(leaf
, nritems
+ 1);
2306 /* write the data for the start of the original item */
2307 write_extent_buffer(leaf
, buf
,
2308 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2311 /* write the data for the new item */
2312 write_extent_buffer(leaf
, buf
+ split_offset
,
2313 btrfs_item_ptr_offset(leaf
, slot
),
2314 item_size
- split_offset
);
2315 btrfs_mark_buffer_dirty(leaf
);
2318 if (btrfs_leaf_free_space(leaf
) < 0) {
2319 btrfs_print_leaf(leaf
);
2326 int btrfs_truncate_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2327 u32 new_size
, int from_end
)
2331 struct extent_buffer
*leaf
;
2332 struct btrfs_item
*item
;
2334 unsigned int data_end
;
2335 unsigned int old_data_start
;
2336 unsigned int old_size
;
2337 unsigned int size_diff
;
2340 leaf
= path
->nodes
[0];
2341 slot
= path
->slots
[0];
2343 old_size
= btrfs_item_size_nr(leaf
, slot
);
2344 if (old_size
== new_size
)
2347 nritems
= btrfs_header_nritems(leaf
);
2348 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2350 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
2352 size_diff
= old_size
- new_size
;
2355 BUG_ON(slot
>= nritems
);
2358 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2360 /* first correct the data pointers */
2361 for (i
= slot
; i
< nritems
; i
++) {
2363 item
= btrfs_item_nr(i
);
2364 ioff
= btrfs_item_offset(leaf
, item
);
2365 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
2368 /* shift the data */
2370 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2371 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2372 data_end
, old_data_start
+ new_size
- data_end
);
2374 struct btrfs_disk_key disk_key
;
2377 btrfs_item_key(leaf
, &disk_key
, slot
);
2379 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
2381 struct btrfs_file_extent_item
*fi
;
2383 fi
= btrfs_item_ptr(leaf
, slot
,
2384 struct btrfs_file_extent_item
);
2385 fi
= (struct btrfs_file_extent_item
*)(
2386 (unsigned long)fi
- size_diff
);
2388 if (btrfs_file_extent_type(leaf
, fi
) ==
2389 BTRFS_FILE_EXTENT_INLINE
) {
2390 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
2391 memmove_extent_buffer(leaf
, ptr
,
2393 offsetof(struct btrfs_file_extent_item
,
2398 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2399 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
2400 data_end
, old_data_start
- data_end
);
2402 offset
= btrfs_disk_key_offset(&disk_key
);
2403 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
2404 btrfs_set_item_key(leaf
, &disk_key
, slot
);
2406 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2409 item
= btrfs_item_nr(slot
);
2410 btrfs_set_item_size(leaf
, item
, new_size
);
2411 btrfs_mark_buffer_dirty(leaf
);
2414 if (btrfs_leaf_free_space(leaf
) < 0) {
2415 btrfs_print_leaf(leaf
);
2421 int btrfs_extend_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
2426 struct extent_buffer
*leaf
;
2427 struct btrfs_item
*item
;
2429 unsigned int data_end
;
2430 unsigned int old_data
;
2431 unsigned int old_size
;
2434 leaf
= path
->nodes
[0];
2436 nritems
= btrfs_header_nritems(leaf
);
2437 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2439 if (btrfs_leaf_free_space(leaf
) < data_size
) {
2440 btrfs_print_leaf(leaf
);
2443 slot
= path
->slots
[0];
2444 old_data
= btrfs_item_end_nr(leaf
, slot
);
2447 if (slot
>= nritems
) {
2448 btrfs_print_leaf(leaf
);
2449 printk("slot %d too large, nritems %d\n", slot
, nritems
);
2454 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2456 /* first correct the data pointers */
2457 for (i
= slot
; i
< nritems
; i
++) {
2459 item
= btrfs_item_nr(i
);
2460 ioff
= btrfs_item_offset(leaf
, item
);
2461 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
2464 /* shift the data */
2465 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2466 data_end
- data_size
, btrfs_leaf_data(leaf
) +
2467 data_end
, old_data
- data_end
);
2469 data_end
= old_data
;
2470 old_size
= btrfs_item_size_nr(leaf
, slot
);
2471 item
= btrfs_item_nr(slot
);
2472 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
2473 btrfs_mark_buffer_dirty(leaf
);
2476 if (btrfs_leaf_free_space(leaf
) < 0) {
2477 btrfs_print_leaf(leaf
);
2484 * Given a key and some data, insert an item into the tree.
2485 * This does all the path init required, making room in the tree if needed.
2487 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
2488 struct btrfs_root
*root
,
2489 struct btrfs_path
*path
,
2490 struct btrfs_key
*cpu_key
, u32
*data_size
,
2493 struct extent_buffer
*leaf
;
2494 struct btrfs_item
*item
;
2501 unsigned int data_end
;
2502 struct btrfs_disk_key disk_key
;
2504 for (i
= 0; i
< nr
; i
++) {
2505 total_data
+= data_size
[i
];
2508 /* create a root if there isn't one */
2512 total_size
= total_data
+ nr
* sizeof(struct btrfs_item
);
2513 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
2520 leaf
= path
->nodes
[0];
2522 nritems
= btrfs_header_nritems(leaf
);
2523 data_end
= leaf_data_end(root
->fs_info
, leaf
);
2525 if (btrfs_leaf_free_space(leaf
) < total_size
) {
2526 btrfs_print_leaf(leaf
);
2527 printk("not enough freespace need %u have %d\n",
2528 total_size
, btrfs_leaf_free_space(leaf
));
2532 slot
= path
->slots
[0];
2535 if (slot
< nritems
) {
2536 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
2538 if (old_data
< data_end
) {
2539 btrfs_print_leaf(leaf
);
2540 printk("slot %d old_data %d data_end %d\n",
2541 slot
, old_data
, data_end
);
2545 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2547 /* first correct the data pointers */
2548 for (i
= slot
; i
< nritems
; i
++) {
2551 item
= btrfs_item_nr(i
);
2552 ioff
= btrfs_item_offset(leaf
, item
);
2553 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
2556 /* shift the items */
2557 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
2558 btrfs_item_nr_offset(slot
),
2559 (nritems
- slot
) * sizeof(struct btrfs_item
));
2561 /* shift the data */
2562 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2563 data_end
- total_data
, btrfs_leaf_data(leaf
) +
2564 data_end
, old_data
- data_end
);
2565 data_end
= old_data
;
2568 /* setup the item for the new data */
2569 for (i
= 0; i
< nr
; i
++) {
2570 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
2571 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
2572 item
= btrfs_item_nr(slot
+ i
);
2573 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
2574 data_end
-= data_size
[i
];
2575 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
2577 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
2578 btrfs_mark_buffer_dirty(leaf
);
2582 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
2583 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2586 if (btrfs_leaf_free_space(leaf
) < 0) {
2587 btrfs_print_leaf(leaf
);
2596 * Given a key and some data, insert an item into the tree.
2597 * This does all the path init required, making room in the tree if needed.
2599 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
2600 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
2604 struct btrfs_path
*path
;
2605 struct extent_buffer
*leaf
;
2608 path
= btrfs_alloc_path();
2612 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
2614 leaf
= path
->nodes
[0];
2615 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
2616 write_extent_buffer(leaf
, data
, ptr
, data_size
);
2617 btrfs_mark_buffer_dirty(leaf
);
2619 btrfs_free_path(path
);
2624 * delete the pointer from a given node.
2626 * If the delete empties a node, the node is removed from the tree,
2627 * continuing all the way the root if required. The root is converted into
2628 * a leaf if all the nodes are emptied.
2630 int btrfs_del_ptr(struct btrfs_root
*root
, struct btrfs_path
*path
,
2631 int level
, int slot
)
2633 struct extent_buffer
*parent
= path
->nodes
[level
];
2637 nritems
= btrfs_header_nritems(parent
);
2638 if (slot
< nritems
- 1) {
2639 /* shift the items */
2640 memmove_extent_buffer(parent
,
2641 btrfs_node_key_ptr_offset(slot
),
2642 btrfs_node_key_ptr_offset(slot
+ 1),
2643 sizeof(struct btrfs_key_ptr
) *
2644 (nritems
- slot
- 1));
2647 btrfs_set_header_nritems(parent
, nritems
);
2648 if (nritems
== 0 && parent
== root
->node
) {
2649 BUG_ON(btrfs_header_level(root
->node
) != 1);
2650 /* just turn the root into a leaf and break */
2651 btrfs_set_header_level(root
->node
, 0);
2652 } else if (slot
== 0) {
2653 struct btrfs_disk_key disk_key
;
2655 btrfs_node_key(parent
, &disk_key
, 0);
2656 btrfs_fixup_low_keys(root
, path
, &disk_key
, level
+ 1);
2658 btrfs_mark_buffer_dirty(parent
);
2663 * a helper function to delete the leaf pointed to by path->slots[1] and
2666 * This deletes the pointer in path->nodes[1] and frees the leaf
2667 * block extent. zero is returned if it all worked out, < 0 otherwise.
2669 * The path must have already been setup for deleting the leaf, including
2670 * all the proper balancing. path->nodes[1] must be locked.
2672 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
2673 struct btrfs_root
*root
,
2674 struct btrfs_path
*path
,
2675 struct extent_buffer
*leaf
)
2679 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
2680 ret
= btrfs_del_ptr(root
, path
, 1, path
->slots
[1]);
2684 ret
= btrfs_free_extent(trans
, root
, leaf
->start
, leaf
->len
,
2685 0, root
->root_key
.objectid
, 0, 0);
2690 * delete the item at the leaf level in path. If that empties
2691 * the leaf, remove it from the tree
2693 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2694 struct btrfs_path
*path
, int slot
, int nr
)
2696 struct extent_buffer
*leaf
;
2697 struct btrfs_item
*item
;
2705 leaf
= path
->nodes
[0];
2706 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
2708 for (i
= 0; i
< nr
; i
++)
2709 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
2711 nritems
= btrfs_header_nritems(leaf
);
2713 if (slot
+ nr
!= nritems
) {
2714 int data_end
= leaf_data_end(root
->fs_info
, leaf
);
2716 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
2718 btrfs_leaf_data(leaf
) + data_end
,
2719 last_off
- data_end
);
2721 for (i
= slot
+ nr
; i
< nritems
; i
++) {
2724 item
= btrfs_item_nr(i
);
2725 ioff
= btrfs_item_offset(leaf
, item
);
2726 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
2729 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
2730 btrfs_item_nr_offset(slot
+ nr
),
2731 sizeof(struct btrfs_item
) *
2732 (nritems
- slot
- nr
));
2734 btrfs_set_header_nritems(leaf
, nritems
- nr
);
2737 /* delete the leaf if we've emptied it */
2739 if (leaf
== root
->node
) {
2740 btrfs_set_header_level(leaf
, 0);
2742 clean_tree_block(trans
, root
, leaf
);
2743 wret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2749 int used
= leaf_space_used(leaf
, 0, nritems
);
2751 struct btrfs_disk_key disk_key
;
2753 btrfs_item_key(leaf
, &disk_key
, 0);
2754 btrfs_fixup_low_keys(root
, path
, &disk_key
, 1);
2757 /* delete the leaf if it is mostly empty */
2758 if (used
< BTRFS_LEAF_DATA_SIZE(root
->fs_info
) / 4) {
2759 /* push_leaf_left fixes the path.
2760 * make sure the path still points to our leaf
2761 * for possible call to del_ptr below
2763 slot
= path
->slots
[1];
2764 extent_buffer_get(leaf
);
2766 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
2767 if (wret
< 0 && wret
!= -ENOSPC
)
2770 if (path
->nodes
[0] == leaf
&&
2771 btrfs_header_nritems(leaf
)) {
2772 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
2773 if (wret
< 0 && wret
!= -ENOSPC
)
2777 if (btrfs_header_nritems(leaf
) == 0) {
2778 clean_tree_block(trans
, root
, leaf
);
2779 path
->slots
[1] = slot
;
2780 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
2782 free_extent_buffer(leaf
);
2785 btrfs_mark_buffer_dirty(leaf
);
2786 free_extent_buffer(leaf
);
2789 btrfs_mark_buffer_dirty(leaf
);
2796 * walk up the tree as far as required to find the previous leaf.
2797 * returns 0 if it found something or 1 if there are no lesser leaves.
2798 * returns < 0 on io errors.
2800 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2804 struct extent_buffer
*c
;
2805 struct extent_buffer
*next
= NULL
;
2806 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2808 while(level
< BTRFS_MAX_LEVEL
) {
2809 if (!path
->nodes
[level
])
2812 slot
= path
->slots
[level
];
2813 c
= path
->nodes
[level
];
2816 if (level
== BTRFS_MAX_LEVEL
)
2822 next
= read_node_slot(fs_info
, c
, slot
);
2823 if (!extent_buffer_uptodate(next
)) {
2825 return PTR_ERR(next
);
2830 path
->slots
[level
] = slot
;
2833 c
= path
->nodes
[level
];
2834 free_extent_buffer(c
);
2835 slot
= btrfs_header_nritems(next
);
2838 path
->nodes
[level
] = next
;
2839 path
->slots
[level
] = slot
;
2842 next
= read_node_slot(fs_info
, next
, slot
);
2843 if (!extent_buffer_uptodate(next
)) {
2845 return PTR_ERR(next
);
2853 * walk up the tree as far as required to find the next leaf.
2854 * returns 0 if it found something or 1 if there are no greater leaves.
2855 * returns < 0 on io errors.
2857 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
2861 struct extent_buffer
*c
;
2862 struct extent_buffer
*next
= NULL
;
2863 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2865 while(level
< BTRFS_MAX_LEVEL
) {
2866 if (!path
->nodes
[level
])
2869 slot
= path
->slots
[level
] + 1;
2870 c
= path
->nodes
[level
];
2871 if (slot
>= btrfs_header_nritems(c
)) {
2873 if (level
== BTRFS_MAX_LEVEL
)
2879 reada_for_search(root
, path
, level
, slot
, 0);
2881 next
= read_node_slot(fs_info
, c
, slot
);
2882 if (!extent_buffer_uptodate(next
))
2886 path
->slots
[level
] = slot
;
2889 c
= path
->nodes
[level
];
2890 free_extent_buffer(c
);
2891 path
->nodes
[level
] = next
;
2892 path
->slots
[level
] = 0;
2896 reada_for_search(root
, path
, level
, 0, 0);
2897 next
= read_node_slot(fs_info
, next
, 0);
2898 if (!extent_buffer_uptodate(next
))
2904 int btrfs_previous_item(struct btrfs_root
*root
,
2905 struct btrfs_path
*path
, u64 min_objectid
,
2908 struct btrfs_key found_key
;
2909 struct extent_buffer
*leaf
;
2914 if (path
->slots
[0] == 0) {
2915 ret
= btrfs_prev_leaf(root
, path
);
2921 leaf
= path
->nodes
[0];
2922 nritems
= btrfs_header_nritems(leaf
);
2925 if (path
->slots
[0] == nritems
)
2928 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2929 if (found_key
.objectid
< min_objectid
)
2931 if (found_key
.type
== type
)
2933 if (found_key
.objectid
== min_objectid
&&
2934 found_key
.type
< type
)
2941 * search in extent tree to find a previous Metadata/Data extent item with
2944 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2946 int btrfs_previous_extent_item(struct btrfs_root
*root
,
2947 struct btrfs_path
*path
, u64 min_objectid
)
2949 struct btrfs_key found_key
;
2950 struct extent_buffer
*leaf
;
2955 if (path
->slots
[0] == 0) {
2956 ret
= btrfs_prev_leaf(root
, path
);
2962 leaf
= path
->nodes
[0];
2963 nritems
= btrfs_header_nritems(leaf
);
2966 if (path
->slots
[0] == nritems
)
2969 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2970 if (found_key
.objectid
< min_objectid
)
2972 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
2973 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)
2975 if (found_key
.objectid
== min_objectid
&&
2976 found_key
.type
< BTRFS_EXTENT_ITEM_KEY
)
2983 * Search in extent tree to found next meta/data extent
2984 * Caller needs to check for no-hole or skinny metadata features.
2986 int btrfs_next_extent_item(struct btrfs_root
*root
,
2987 struct btrfs_path
*path
, u64 max_objectid
)
2989 struct btrfs_key found_key
;
2993 ret
= btrfs_next_item(root
, path
);
2996 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2998 if (found_key
.objectid
> max_objectid
)
3000 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
3001 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)