Add btrfs-list for listing subvolumes
[btrfs-progs-unstable/devel.git] / ctree.c
blobf70e10cc9338b4ee1e6f303ab8ebc282a7f3cbea
1 /*
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.
18 #include "ctree.h"
19 #include "disk-io.h"
20 #include "transaction.h"
21 #include "print-tree.h"
23 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
24 *root, struct btrfs_path *path, int level);
25 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_key *ins_key,
27 struct btrfs_path *path, int data_size, int extend);
28 static int push_node_left(struct btrfs_trans_handle *trans,
29 struct btrfs_root *root, struct extent_buffer *dst,
30 struct extent_buffer *src, int empty);
31 static int balance_node_right(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root,
33 struct extent_buffer *dst_buf,
34 struct extent_buffer *src_buf);
35 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
36 struct btrfs_path *path, int level, int slot);
38 inline void btrfs_init_path(struct btrfs_path *p)
40 memset(p, 0, sizeof(*p));
43 struct btrfs_path *btrfs_alloc_path(void)
45 struct btrfs_path *path;
46 path = kmalloc(sizeof(struct btrfs_path), GFP_NOFS);
47 if (path) {
48 btrfs_init_path(path);
49 path->reada = 0;
51 return path;
54 void btrfs_free_path(struct btrfs_path *p)
56 btrfs_release_path(NULL, p);
57 kfree(p);
60 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
62 int i;
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
64 if (!p->nodes[i])
65 continue;
66 free_extent_buffer(p->nodes[i]);
68 memset(p, 0, sizeof(*p));
71 static void add_root_to_dirty_list(struct btrfs_root *root)
73 if (root->track_dirty && list_empty(&root->dirty_list)) {
74 list_add(&root->dirty_list,
75 &root->fs_info->dirty_cowonly_roots);
79 int btrfs_copy_root(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 struct extent_buffer *buf,
82 struct extent_buffer **cow_ret, u64 new_root_objectid)
84 struct extent_buffer *cow;
85 int ret = 0;
86 int level;
87 struct btrfs_root *new_root;
88 struct btrfs_disk_key disk_key;
90 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
91 if (!new_root)
92 return -ENOMEM;
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);
102 if (level == 0)
103 btrfs_item_key(buf, &disk_key, 0);
104 else
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);
109 if (IS_ERR(cow)) {
110 kfree(new_root);
111 return PTR_ERR(cow);
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);
122 else
123 btrfs_set_header_owner(cow, new_root_objectid);
125 write_extent_buffer(cow, root->fs_info->fsid,
126 (unsigned long)btrfs_header_fsid(cow),
127 BTRFS_FSID_SIZE);
129 WARN_ON(btrfs_header_generation(buf) > trans->transid);
130 ret = btrfs_inc_ref(trans, new_root, cow, 0);
131 kfree(new_root);
133 if (ret)
134 return ret;
136 btrfs_mark_buffer_dirty(cow);
137 *cow_ret = cow;
138 return 0;
142 * check if the tree block can be shared by multiple trees
144 int btrfs_block_can_be_shared(struct btrfs_root *root,
145 struct extent_buffer *buf)
148 * Tree blocks not in refernece counted trees and tree roots
149 * are never shared. If a block was allocated after the last
150 * snapshot and the block was not allocated by tree relocation,
151 * we know the block is not shared.
153 if (root->ref_cows &&
154 buf != root->node && buf != root->commit_root &&
155 (btrfs_header_generation(buf) <=
156 btrfs_root_last_snapshot(&root->root_item) ||
157 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
158 return 1;
159 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
160 if (root->ref_cows &&
161 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
162 return 1;
163 #endif
164 return 0;
167 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
168 struct btrfs_root *root,
169 struct extent_buffer *buf,
170 struct extent_buffer *cow)
172 u64 refs;
173 u64 owner;
174 u64 flags;
175 u64 new_flags = 0;
176 int ret;
179 * Backrefs update rules:
181 * Always use full backrefs for extent pointers in tree block
182 * allocated by tree relocation.
184 * If a shared tree block is no longer referenced by its owner
185 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
186 * use full backrefs for extent pointers in tree block.
188 * If a tree block is been relocating
189 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
190 * use full backrefs for extent pointers in tree block.
191 * The reason for this is some operations (such as drop tree)
192 * are only allowed for blocks use full backrefs.
195 if (btrfs_block_can_be_shared(root, buf)) {
196 ret = btrfs_lookup_extent_info(trans, root, buf->start,
197 buf->len, &refs, &flags);
198 BUG_ON(ret);
199 BUG_ON(refs == 0);
200 } else {
201 refs = 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;
205 else
206 flags = 0;
209 owner = btrfs_header_owner(buf);
210 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
211 owner == BTRFS_TREE_RELOC_OBJECTID);
213 if (refs > 1) {
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);
218 BUG_ON(ret);
220 if (root->root_key.objectid ==
221 BTRFS_TREE_RELOC_OBJECTID) {
222 ret = btrfs_dec_ref(trans, root, buf, 0);
223 BUG_ON(ret);
224 ret = btrfs_inc_ref(trans, root, cow, 1);
225 BUG_ON(ret);
227 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
228 } else {
230 if (root->root_key.objectid ==
231 BTRFS_TREE_RELOC_OBJECTID)
232 ret = btrfs_inc_ref(trans, root, cow, 1);
233 else
234 ret = btrfs_inc_ref(trans, root, cow, 0);
235 BUG_ON(ret);
237 if (new_flags != 0) {
238 ret = btrfs_set_block_flags(trans, root, buf->start,
239 buf->len, new_flags);
240 BUG_ON(ret);
242 } else {
243 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
244 if (root->root_key.objectid ==
245 BTRFS_TREE_RELOC_OBJECTID)
246 ret = btrfs_inc_ref(trans, root, cow, 1);
247 else
248 ret = btrfs_inc_ref(trans, root, cow, 0);
249 BUG_ON(ret);
250 ret = btrfs_dec_ref(trans, root, buf, 1);
251 BUG_ON(ret);
253 clean_tree_block(trans, root, buf);
255 return 0;
258 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
259 struct btrfs_root *root,
260 struct extent_buffer *buf,
261 struct extent_buffer *parent, int parent_slot,
262 struct extent_buffer **cow_ret,
263 u64 search_start, u64 empty_size)
265 u64 generation;
266 struct extent_buffer *cow;
267 struct btrfs_disk_key disk_key;
268 int level;
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);
275 generation = btrfs_header_generation(buf);
277 if (level == 0)
278 btrfs_item_key(buf, &disk_key, 0);
279 else
280 btrfs_node_key(buf, &disk_key, 0);
282 cow = btrfs_alloc_free_block(trans, root, buf->len,
283 root->root_key.objectid, &disk_key,
284 level, search_start, empty_size);
285 if (IS_ERR(cow))
286 return PTR_ERR(cow);
288 copy_extent_buffer(cow, buf, 0, 0, cow->len);
289 btrfs_set_header_bytenr(cow, cow->start);
290 btrfs_set_header_generation(cow, trans->transid);
291 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
292 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
293 BTRFS_HEADER_FLAG_RELOC);
294 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
295 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
296 else
297 btrfs_set_header_owner(cow, root->root_key.objectid);
299 write_extent_buffer(cow, root->fs_info->fsid,
300 (unsigned long)btrfs_header_fsid(cow),
301 BTRFS_FSID_SIZE);
303 WARN_ON(btrfs_header_generation(buf) > trans->transid);
305 update_ref_for_cow(trans, root, buf, cow);
307 if (buf == root->node) {
308 root->node = cow;
309 extent_buffer_get(cow);
311 btrfs_free_extent(trans, root, buf->start, buf->len,
312 0, root->root_key.objectid, level, 0);
313 free_extent_buffer(buf);
314 add_root_to_dirty_list(root);
315 } else {
316 btrfs_set_node_blockptr(parent, parent_slot,
317 cow->start);
318 WARN_ON(trans->transid == 0);
319 btrfs_set_node_ptr_generation(parent, parent_slot,
320 trans->transid);
321 btrfs_mark_buffer_dirty(parent);
322 WARN_ON(btrfs_header_generation(parent) != trans->transid);
324 btrfs_free_extent(trans, root, buf->start, buf->len,
325 0, root->root_key.objectid, level, 1);
327 free_extent_buffer(buf);
328 btrfs_mark_buffer_dirty(cow);
329 *cow_ret = cow;
330 return 0;
333 static inline int should_cow_block(struct btrfs_trans_handle *trans,
334 struct btrfs_root *root,
335 struct extent_buffer *buf)
337 if (btrfs_header_generation(buf) == trans->transid &&
338 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
339 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
340 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
341 return 0;
342 return 1;
345 int btrfs_cow_block(struct btrfs_trans_handle *trans,
346 struct btrfs_root *root, struct extent_buffer *buf,
347 struct extent_buffer *parent, int parent_slot,
348 struct extent_buffer **cow_ret)
350 u64 search_start;
351 int ret;
353 if (trans->transaction != root->fs_info->running_transaction) {
354 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
355 root->fs_info->running_transaction->transid);
356 WARN_ON(1);
359 if (trans->transid != root->fs_info->generation) {
360 printk(KERN_CRIT "trans %llu running %llu\n",
361 (unsigned long long)trans->transid,
362 (unsigned long long)root->fs_info->generation);
363 WARN_ON(1);
365 if (!should_cow_block(trans, root, buf)) {
366 *cow_ret = buf;
367 return 0;
370 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
371 ret = __btrfs_cow_block(trans, root, buf, parent,
372 parent_slot, cow_ret, search_start, 0);
373 return ret;
377 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
379 if (blocknr < other && other - (blocknr + blocksize) < 32768)
380 return 1;
381 if (blocknr > other && blocknr - (other + blocksize) < 32768)
382 return 1;
383 return 0;
388 * compare two keys in a memcmp fashion
390 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
392 struct btrfs_key k1;
394 btrfs_disk_key_to_cpu(&k1, disk);
396 if (k1.objectid > k2->objectid)
397 return 1;
398 if (k1.objectid < k2->objectid)
399 return -1;
400 if (k1.type > k2->type)
401 return 1;
402 if (k1.type < k2->type)
403 return -1;
404 if (k1.offset > k2->offset)
405 return 1;
406 if (k1.offset < k2->offset)
407 return -1;
408 return 0;
412 #if 0
413 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root, struct extent_buffer *parent,
415 int start_slot, int cache_only, u64 *last_ret,
416 struct btrfs_key *progress)
418 struct extent_buffer *cur;
419 struct extent_buffer *tmp;
420 u64 blocknr;
421 u64 gen;
422 u64 search_start = *last_ret;
423 u64 last_block = 0;
424 u64 other;
425 u32 parent_nritems;
426 int end_slot;
427 int i;
428 int err = 0;
429 int parent_level;
430 int uptodate;
431 u32 blocksize;
432 int progress_passed = 0;
433 struct btrfs_disk_key disk_key;
435 parent_level = btrfs_header_level(parent);
436 if (cache_only && parent_level != 1)
437 return 0;
439 if (trans->transaction != root->fs_info->running_transaction) {
440 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
441 root->fs_info->running_transaction->transid);
442 WARN_ON(1);
444 if (trans->transid != root->fs_info->generation) {
445 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
446 root->fs_info->generation);
447 WARN_ON(1);
450 parent_nritems = btrfs_header_nritems(parent);
451 blocksize = btrfs_level_size(root, parent_level - 1);
452 end_slot = parent_nritems;
454 if (parent_nritems == 1)
455 return 0;
457 for (i = start_slot; i < end_slot; i++) {
458 int close = 1;
460 if (!parent->map_token) {
461 map_extent_buffer(parent,
462 btrfs_node_key_ptr_offset(i),
463 sizeof(struct btrfs_key_ptr),
464 &parent->map_token, &parent->kaddr,
465 &parent->map_start, &parent->map_len,
466 KM_USER1);
468 btrfs_node_key(parent, &disk_key, i);
469 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
470 continue;
472 progress_passed = 1;
473 blocknr = btrfs_node_blockptr(parent, i);
474 gen = btrfs_node_ptr_generation(parent, i);
475 if (last_block == 0)
476 last_block = blocknr;
478 if (i > 0) {
479 other = btrfs_node_blockptr(parent, i - 1);
480 close = close_blocks(blocknr, other, blocksize);
482 if (close && i < end_slot - 2) {
483 other = btrfs_node_blockptr(parent, i + 1);
484 close = close_blocks(blocknr, other, blocksize);
486 if (close) {
487 last_block = blocknr;
488 continue;
490 if (parent->map_token) {
491 unmap_extent_buffer(parent, parent->map_token,
492 KM_USER1);
493 parent->map_token = NULL;
496 cur = btrfs_find_tree_block(root, blocknr, blocksize);
497 if (cur)
498 uptodate = btrfs_buffer_uptodate(cur, gen);
499 else
500 uptodate = 0;
501 if (!cur || !uptodate) {
502 if (cache_only) {
503 free_extent_buffer(cur);
504 continue;
506 if (!cur) {
507 cur = read_tree_block(root, blocknr,
508 blocksize, gen);
509 } else if (!uptodate) {
510 btrfs_read_buffer(cur, gen);
513 if (search_start == 0)
514 search_start = last_block;
516 err = __btrfs_cow_block(trans, root, cur, parent, i,
517 &tmp, search_start,
518 min(16 * blocksize,
519 (end_slot - i) * blocksize));
520 if (err) {
521 free_extent_buffer(cur);
522 break;
524 search_start = tmp->start;
525 last_block = tmp->start;
526 *last_ret = search_start;
527 if (parent_level == 1)
528 btrfs_clear_buffer_defrag(tmp);
529 free_extent_buffer(tmp);
531 if (parent->map_token) {
532 unmap_extent_buffer(parent, parent->map_token,
533 KM_USER1);
534 parent->map_token = NULL;
536 return err;
538 #endif
541 * The leaf data grows from end-to-front in the node.
542 * this returns the address of the start of the last item,
543 * which is the stop of the leaf data stack
545 static inline unsigned int leaf_data_end(struct btrfs_root *root,
546 struct extent_buffer *leaf)
548 u32 nr = btrfs_header_nritems(leaf);
549 if (nr == 0)
550 return BTRFS_LEAF_DATA_SIZE(root);
551 return btrfs_item_offset_nr(leaf, nr - 1);
554 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
555 int level)
557 struct extent_buffer *parent = NULL;
558 struct extent_buffer *node = path->nodes[level];
559 struct btrfs_disk_key parent_key;
560 struct btrfs_disk_key node_key;
561 int parent_slot;
562 int slot;
563 struct btrfs_key cpukey;
564 u32 nritems = btrfs_header_nritems(node);
566 if (path->nodes[level + 1])
567 parent = path->nodes[level + 1];
569 slot = path->slots[level];
570 BUG_ON(nritems == 0);
571 if (parent) {
572 parent_slot = path->slots[level + 1];
573 btrfs_node_key(parent, &parent_key, parent_slot);
574 btrfs_node_key(node, &node_key, 0);
575 BUG_ON(memcmp(&parent_key, &node_key,
576 sizeof(struct btrfs_disk_key)));
577 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
578 btrfs_header_bytenr(node));
580 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
581 if (slot != 0) {
582 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
583 btrfs_node_key(node, &node_key, slot);
584 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) <= 0);
586 if (slot < nritems - 1) {
587 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
588 btrfs_node_key(node, &node_key, slot);
589 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) >= 0);
591 return 0;
594 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
595 int level)
597 struct extent_buffer *leaf = path->nodes[level];
598 struct extent_buffer *parent = NULL;
599 int parent_slot;
600 struct btrfs_key cpukey;
601 struct btrfs_disk_key parent_key;
602 struct btrfs_disk_key leaf_key;
603 int slot = path->slots[0];
605 u32 nritems = btrfs_header_nritems(leaf);
607 if (path->nodes[level + 1])
608 parent = path->nodes[level + 1];
610 if (nritems == 0)
611 return 0;
613 if (parent) {
614 parent_slot = path->slots[level + 1];
615 btrfs_node_key(parent, &parent_key, parent_slot);
616 btrfs_item_key(leaf, &leaf_key, 0);
618 BUG_ON(memcmp(&parent_key, &leaf_key,
619 sizeof(struct btrfs_disk_key)));
620 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
621 btrfs_header_bytenr(leaf));
623 #if 0
624 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
625 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
626 btrfs_item_key(leaf, &leaf_key, i);
627 if (comp_keys(&leaf_key, &cpukey) >= 0) {
628 btrfs_print_leaf(root, leaf);
629 printk("slot %d offset bad key\n", i);
630 BUG_ON(1);
632 if (btrfs_item_offset_nr(leaf, i) !=
633 btrfs_item_end_nr(leaf, i + 1)) {
634 btrfs_print_leaf(root, leaf);
635 printk("slot %d offset bad\n", i);
636 BUG_ON(1);
638 if (i == 0) {
639 if (btrfs_item_offset_nr(leaf, i) +
640 btrfs_item_size_nr(leaf, i) !=
641 BTRFS_LEAF_DATA_SIZE(root)) {
642 btrfs_print_leaf(root, leaf);
643 printk("slot %d first offset bad\n", i);
644 BUG_ON(1);
648 if (nritems > 0) {
649 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
650 btrfs_print_leaf(root, leaf);
651 printk("slot %d bad size \n", nritems - 1);
652 BUG_ON(1);
655 #endif
656 if (slot != 0 && slot < nritems - 1) {
657 btrfs_item_key(leaf, &leaf_key, slot);
658 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
659 if (btrfs_comp_keys(&leaf_key, &cpukey) <= 0) {
660 btrfs_print_leaf(root, leaf);
661 printk("slot %d offset bad key\n", slot);
662 BUG_ON(1);
664 if (btrfs_item_offset_nr(leaf, slot - 1) !=
665 btrfs_item_end_nr(leaf, slot)) {
666 btrfs_print_leaf(root, leaf);
667 printk("slot %d offset bad\n", slot);
668 BUG_ON(1);
671 if (slot < nritems - 1) {
672 btrfs_item_key(leaf, &leaf_key, slot);
673 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
674 BUG_ON(btrfs_comp_keys(&leaf_key, &cpukey) >= 0);
675 if (btrfs_item_offset_nr(leaf, slot) !=
676 btrfs_item_end_nr(leaf, slot + 1)) {
677 btrfs_print_leaf(root, leaf);
678 printk("slot %d offset bad\n", slot);
679 BUG_ON(1);
682 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
683 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
684 return 0;
687 static int noinline check_block(struct btrfs_root *root,
688 struct btrfs_path *path, int level)
690 return 0;
691 #if 0
692 struct extent_buffer *buf = path->nodes[level];
694 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
695 (unsigned long)btrfs_header_fsid(buf),
696 BTRFS_FSID_SIZE)) {
697 printk("warning bad block %Lu\n", buf->start);
698 return 1;
700 #endif
701 if (level == 0)
702 return check_leaf(root, path, level);
703 return check_node(root, path, level);
707 * search for key in the extent_buffer. The items start at offset p,
708 * and they are item_size apart. There are 'max' items in p.
710 * the slot in the array is returned via slot, and it points to
711 * the place where you would insert key if it is not found in
712 * the array.
714 * slot may point to max if the key is bigger than all of the keys
716 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
717 int item_size, struct btrfs_key *key,
718 int max, int *slot)
720 int low = 0;
721 int high = max;
722 int mid;
723 int ret;
724 unsigned long offset;
725 struct btrfs_disk_key *tmp;
727 while(low < high) {
728 mid = (low + high) / 2;
729 offset = p + mid * item_size;
731 tmp = (struct btrfs_disk_key *)(eb->data + offset);
732 ret = btrfs_comp_keys(tmp, key);
734 if (ret < 0)
735 low = mid + 1;
736 else if (ret > 0)
737 high = mid;
738 else {
739 *slot = mid;
740 return 0;
743 *slot = low;
744 return 1;
748 * simple bin_search frontend that does the right thing for
749 * leaves vs nodes
751 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
752 int level, int *slot)
754 if (level == 0) {
755 return generic_bin_search(eb,
756 offsetof(struct btrfs_leaf, items),
757 sizeof(struct btrfs_item),
758 key, btrfs_header_nritems(eb),
759 slot);
760 } else {
761 return generic_bin_search(eb,
762 offsetof(struct btrfs_node, ptrs),
763 sizeof(struct btrfs_key_ptr),
764 key, btrfs_header_nritems(eb),
765 slot);
767 return -1;
770 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
771 struct extent_buffer *parent, int slot)
773 int level = btrfs_header_level(parent);
774 if (slot < 0)
775 return NULL;
776 if (slot >= btrfs_header_nritems(parent))
777 return NULL;
779 BUG_ON(level == 0);
781 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
782 btrfs_level_size(root, level - 1),
783 btrfs_node_ptr_generation(parent, slot));
786 static int balance_level(struct btrfs_trans_handle *trans,
787 struct btrfs_root *root,
788 struct btrfs_path *path, int level)
790 struct extent_buffer *right = NULL;
791 struct extent_buffer *mid;
792 struct extent_buffer *left = NULL;
793 struct extent_buffer *parent = NULL;
794 int ret = 0;
795 int wret;
796 int pslot;
797 int orig_slot = path->slots[level];
798 int err_on_enospc = 0;
799 u64 orig_ptr;
801 if (level == 0)
802 return 0;
804 mid = path->nodes[level];
805 WARN_ON(btrfs_header_generation(mid) != trans->transid);
807 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
809 if (level < BTRFS_MAX_LEVEL - 1)
810 parent = path->nodes[level + 1];
811 pslot = path->slots[level + 1];
814 * deal with the case where there is only one pointer in the root
815 * by promoting the node below to a root
817 if (!parent) {
818 struct extent_buffer *child;
820 if (btrfs_header_nritems(mid) != 1)
821 return 0;
823 /* promote the child to a root */
824 child = read_node_slot(root, mid, 0);
825 BUG_ON(!child);
826 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
827 BUG_ON(ret);
829 root->node = child;
830 add_root_to_dirty_list(root);
831 path->nodes[level] = NULL;
832 clean_tree_block(trans, root, mid);
833 wait_on_tree_block_writeback(root, mid);
834 /* once for the path */
835 free_extent_buffer(mid);
837 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
838 0, root->root_key.objectid,
839 level, 1);
840 /* once for the root ptr */
841 free_extent_buffer(mid);
842 return ret;
844 if (btrfs_header_nritems(mid) >
845 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
846 return 0;
848 if (btrfs_header_nritems(mid) < 2)
849 err_on_enospc = 1;
851 left = read_node_slot(root, parent, pslot - 1);
852 if (left) {
853 wret = btrfs_cow_block(trans, root, left,
854 parent, pslot - 1, &left);
855 if (wret) {
856 ret = wret;
857 goto enospc;
860 right = read_node_slot(root, parent, pslot + 1);
861 if (right) {
862 wret = btrfs_cow_block(trans, root, right,
863 parent, pslot + 1, &right);
864 if (wret) {
865 ret = wret;
866 goto enospc;
870 /* first, try to make some room in the middle buffer */
871 if (left) {
872 orig_slot += btrfs_header_nritems(left);
873 wret = push_node_left(trans, root, left, mid, 1);
874 if (wret < 0)
875 ret = wret;
876 if (btrfs_header_nritems(mid) < 2)
877 err_on_enospc = 1;
881 * then try to empty the right most buffer into the middle
883 if (right) {
884 wret = push_node_left(trans, root, mid, right, 1);
885 if (wret < 0 && wret != -ENOSPC)
886 ret = wret;
887 if (btrfs_header_nritems(right) == 0) {
888 u64 bytenr = right->start;
889 u32 blocksize = right->len;
891 clean_tree_block(trans, root, right);
892 wait_on_tree_block_writeback(root, right);
893 free_extent_buffer(right);
894 right = NULL;
895 wret = del_ptr(trans, root, path, level + 1, pslot +
897 if (wret)
898 ret = wret;
899 wret = btrfs_free_extent(trans, root, bytenr,
900 blocksize, 0,
901 root->root_key.objectid,
902 level, 0);
903 if (wret)
904 ret = wret;
905 } else {
906 struct btrfs_disk_key right_key;
907 btrfs_node_key(right, &right_key, 0);
908 btrfs_set_node_key(parent, &right_key, pslot + 1);
909 btrfs_mark_buffer_dirty(parent);
912 if (btrfs_header_nritems(mid) == 1) {
914 * we're not allowed to leave a node with one item in the
915 * tree during a delete. A deletion from lower in the tree
916 * could try to delete the only pointer in this node.
917 * So, pull some keys from the left.
918 * There has to be a left pointer at this point because
919 * otherwise we would have pulled some pointers from the
920 * right
922 BUG_ON(!left);
923 wret = balance_node_right(trans, root, mid, left);
924 if (wret < 0) {
925 ret = wret;
926 goto enospc;
928 if (wret == 1) {
929 wret = push_node_left(trans, root, left, mid, 1);
930 if (wret < 0)
931 ret = wret;
933 BUG_ON(wret == 1);
935 if (btrfs_header_nritems(mid) == 0) {
936 /* we've managed to empty the middle node, drop it */
937 u64 bytenr = mid->start;
938 u32 blocksize = mid->len;
939 clean_tree_block(trans, root, mid);
940 wait_on_tree_block_writeback(root, mid);
941 free_extent_buffer(mid);
942 mid = NULL;
943 wret = del_ptr(trans, root, path, level + 1, pslot);
944 if (wret)
945 ret = wret;
946 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
947 0, root->root_key.objectid,
948 level, 0);
949 if (wret)
950 ret = wret;
951 } else {
952 /* update the parent key to reflect our changes */
953 struct btrfs_disk_key mid_key;
954 btrfs_node_key(mid, &mid_key, 0);
955 btrfs_set_node_key(parent, &mid_key, pslot);
956 btrfs_mark_buffer_dirty(parent);
959 /* update the path */
960 if (left) {
961 if (btrfs_header_nritems(left) > orig_slot) {
962 extent_buffer_get(left);
963 path->nodes[level] = left;
964 path->slots[level + 1] -= 1;
965 path->slots[level] = orig_slot;
966 if (mid)
967 free_extent_buffer(mid);
968 } else {
969 orig_slot -= btrfs_header_nritems(left);
970 path->slots[level] = orig_slot;
973 /* double check we haven't messed things up */
974 check_block(root, path, level);
975 if (orig_ptr !=
976 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
977 BUG();
978 enospc:
979 if (right)
980 free_extent_buffer(right);
981 if (left)
982 free_extent_buffer(left);
983 return ret;
986 /* returns zero if the push worked, non-zero otherwise */
987 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
989 struct btrfs_path *path, int level)
991 struct extent_buffer *right = NULL;
992 struct extent_buffer *mid;
993 struct extent_buffer *left = NULL;
994 struct extent_buffer *parent = NULL;
995 int ret = 0;
996 int wret;
997 int pslot;
998 int orig_slot = path->slots[level];
999 u64 orig_ptr;
1001 if (level == 0)
1002 return 1;
1004 mid = path->nodes[level];
1005 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1006 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1008 if (level < BTRFS_MAX_LEVEL - 1)
1009 parent = path->nodes[level + 1];
1010 pslot = path->slots[level + 1];
1012 if (!parent)
1013 return 1;
1015 left = read_node_slot(root, parent, pslot - 1);
1017 /* first, try to make some room in the middle buffer */
1018 if (left) {
1019 u32 left_nr;
1020 left_nr = btrfs_header_nritems(left);
1021 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1022 wret = 1;
1023 } else {
1024 ret = btrfs_cow_block(trans, root, left, parent,
1025 pslot - 1, &left);
1026 if (ret)
1027 wret = 1;
1028 else {
1029 wret = push_node_left(trans, root,
1030 left, mid, 0);
1033 if (wret < 0)
1034 ret = wret;
1035 if (wret == 0) {
1036 struct btrfs_disk_key disk_key;
1037 orig_slot += left_nr;
1038 btrfs_node_key(mid, &disk_key, 0);
1039 btrfs_set_node_key(parent, &disk_key, pslot);
1040 btrfs_mark_buffer_dirty(parent);
1041 if (btrfs_header_nritems(left) > orig_slot) {
1042 path->nodes[level] = left;
1043 path->slots[level + 1] -= 1;
1044 path->slots[level] = orig_slot;
1045 free_extent_buffer(mid);
1046 } else {
1047 orig_slot -=
1048 btrfs_header_nritems(left);
1049 path->slots[level] = orig_slot;
1050 free_extent_buffer(left);
1052 return 0;
1054 free_extent_buffer(left);
1056 right= read_node_slot(root, parent, pslot + 1);
1059 * then try to empty the right most buffer into the middle
1061 if (right) {
1062 u32 right_nr;
1063 right_nr = btrfs_header_nritems(right);
1064 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1065 wret = 1;
1066 } else {
1067 ret = btrfs_cow_block(trans, root, right,
1068 parent, pslot + 1,
1069 &right);
1070 if (ret)
1071 wret = 1;
1072 else {
1073 wret = balance_node_right(trans, root,
1074 right, mid);
1077 if (wret < 0)
1078 ret = wret;
1079 if (wret == 0) {
1080 struct btrfs_disk_key disk_key;
1082 btrfs_node_key(right, &disk_key, 0);
1083 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1084 btrfs_mark_buffer_dirty(parent);
1086 if (btrfs_header_nritems(mid) <= orig_slot) {
1087 path->nodes[level] = right;
1088 path->slots[level + 1] += 1;
1089 path->slots[level] = orig_slot -
1090 btrfs_header_nritems(mid);
1091 free_extent_buffer(mid);
1092 } else {
1093 free_extent_buffer(right);
1095 return 0;
1097 free_extent_buffer(right);
1099 return 1;
1103 * readahead one full node of leaves
1105 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
1106 int level, int slot, u64 objectid)
1108 struct extent_buffer *node;
1109 struct btrfs_disk_key disk_key;
1110 u32 nritems;
1111 u64 search;
1112 u64 lowest_read;
1113 u64 highest_read;
1114 u64 nread = 0;
1115 int direction = path->reada;
1116 struct extent_buffer *eb;
1117 u32 nr;
1118 u32 blocksize;
1119 u32 nscan = 0;
1121 if (level != 1)
1122 return;
1124 if (!path->nodes[level])
1125 return;
1127 node = path->nodes[level];
1128 search = btrfs_node_blockptr(node, slot);
1129 blocksize = btrfs_level_size(root, level - 1);
1130 eb = btrfs_find_tree_block(root, search, blocksize);
1131 if (eb) {
1132 free_extent_buffer(eb);
1133 return;
1136 highest_read = search;
1137 lowest_read = search;
1139 nritems = btrfs_header_nritems(node);
1140 nr = slot;
1141 while(1) {
1142 if (direction < 0) {
1143 if (nr == 0)
1144 break;
1145 nr--;
1146 } else if (direction > 0) {
1147 nr++;
1148 if (nr >= nritems)
1149 break;
1151 if (path->reada < 0 && objectid) {
1152 btrfs_node_key(node, &disk_key, nr);
1153 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1154 break;
1156 search = btrfs_node_blockptr(node, nr);
1157 if ((search >= lowest_read && search <= highest_read) ||
1158 (search < lowest_read && lowest_read - search <= 32768) ||
1159 (search > highest_read && search - highest_read <= 32768)) {
1160 readahead_tree_block(root, search, blocksize,
1161 btrfs_node_ptr_generation(node, nr));
1162 nread += blocksize;
1164 nscan++;
1165 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1166 break;
1167 if(nread > (1024 * 1024) || nscan > 128)
1168 break;
1170 if (search < lowest_read)
1171 lowest_read = search;
1172 if (search > highest_read)
1173 highest_read = search;
1178 * look for key in the tree. path is filled in with nodes along the way
1179 * if key is found, we return zero and you can find the item in the leaf
1180 * level of the path (level 0)
1182 * If the key isn't found, the path points to the slot where it should
1183 * be inserted, and 1 is returned. If there are other errors during the
1184 * search a negative error number is returned.
1186 * if ins_len > 0, nodes and leaves will be split as we walk down the
1187 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1188 * possible)
1190 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1191 *root, struct btrfs_key *key, struct btrfs_path *p, int
1192 ins_len, int cow)
1194 struct extent_buffer *b;
1195 int slot;
1196 int ret;
1197 int level;
1198 int should_reada = p->reada;
1199 u8 lowest_level = 0;
1201 lowest_level = p->lowest_level;
1202 WARN_ON(lowest_level && ins_len);
1203 WARN_ON(p->nodes[0] != NULL);
1205 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1207 again:
1208 b = root->node;
1209 extent_buffer_get(b);
1210 while (b) {
1211 level = btrfs_header_level(b);
1212 if (cow) {
1213 int wret;
1214 wret = btrfs_cow_block(trans, root, b,
1215 p->nodes[level + 1],
1216 p->slots[level + 1],
1217 &b);
1218 if (wret) {
1219 free_extent_buffer(b);
1220 return wret;
1223 BUG_ON(!cow && ins_len);
1224 if (level != btrfs_header_level(b))
1225 WARN_ON(1);
1226 level = btrfs_header_level(b);
1227 p->nodes[level] = b;
1228 ret = check_block(root, p, level);
1229 if (ret)
1230 return -1;
1231 ret = bin_search(b, key, level, &slot);
1232 if (level != 0) {
1233 if (ret && slot > 0)
1234 slot -= 1;
1235 p->slots[level] = slot;
1236 if ((p->search_for_split || ins_len > 0) &&
1237 btrfs_header_nritems(b) >=
1238 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1239 int sret = split_node(trans, root, p, level);
1240 BUG_ON(sret > 0);
1241 if (sret)
1242 return sret;
1243 b = p->nodes[level];
1244 slot = p->slots[level];
1245 } else if (ins_len < 0) {
1246 int sret = balance_level(trans, root, p,
1247 level);
1248 if (sret)
1249 return sret;
1250 b = p->nodes[level];
1251 if (!b) {
1252 btrfs_release_path(NULL, p);
1253 goto again;
1255 slot = p->slots[level];
1256 BUG_ON(btrfs_header_nritems(b) == 1);
1258 /* this is only true while dropping a snapshot */
1259 if (level == lowest_level)
1260 break;
1262 if (should_reada)
1263 reada_for_search(root, p, level, slot,
1264 key->objectid);
1266 b = read_node_slot(root, b, slot);
1267 } else {
1268 p->slots[level] = slot;
1269 if (ins_len > 0 &&
1270 ins_len > btrfs_leaf_free_space(root, b)) {
1271 int sret = split_leaf(trans, root, key,
1272 p, ins_len, ret == 0);
1273 BUG_ON(sret > 0);
1274 if (sret)
1275 return sret;
1277 return ret;
1280 return 1;
1284 * adjust the pointers going up the tree, starting at level
1285 * making sure the right key of each node is points to 'key'.
1286 * This is used after shifting pointers to the left, so it stops
1287 * fixing up pointers when a given leaf/node is not in slot 0 of the
1288 * higher levels
1290 * If this fails to write a tree block, it returns -1, but continues
1291 * fixing up the blocks in ram so the tree is consistent.
1293 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1294 struct btrfs_root *root, struct btrfs_path *path,
1295 struct btrfs_disk_key *key, int level)
1297 int i;
1298 int ret = 0;
1299 struct extent_buffer *t;
1301 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1302 int tslot = path->slots[i];
1303 if (!path->nodes[i])
1304 break;
1305 t = path->nodes[i];
1306 btrfs_set_node_key(t, key, tslot);
1307 btrfs_mark_buffer_dirty(path->nodes[i]);
1308 if (tslot != 0)
1309 break;
1311 return ret;
1315 * update item key.
1317 * This function isn't completely safe. It's the caller's responsibility
1318 * that the new key won't break the order
1320 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1321 struct btrfs_root *root, struct btrfs_path *path,
1322 struct btrfs_key *new_key)
1324 struct btrfs_disk_key disk_key;
1325 struct extent_buffer *eb;
1326 int slot;
1328 eb = path->nodes[0];
1329 slot = path->slots[0];
1330 if (slot > 0) {
1331 btrfs_item_key(eb, &disk_key, slot - 1);
1332 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1333 return -1;
1335 if (slot < btrfs_header_nritems(eb) - 1) {
1336 btrfs_item_key(eb, &disk_key, slot + 1);
1337 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1338 return -1;
1341 btrfs_cpu_key_to_disk(&disk_key, new_key);
1342 btrfs_set_item_key(eb, &disk_key, slot);
1343 btrfs_mark_buffer_dirty(eb);
1344 if (slot == 0)
1345 fixup_low_keys(trans, root, path, &disk_key, 1);
1346 return 0;
1350 * try to push data from one node into the next node left in the
1351 * tree.
1353 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1354 * error, and > 0 if there was no room in the left hand block.
1356 static int push_node_left(struct btrfs_trans_handle *trans,
1357 struct btrfs_root *root, struct extent_buffer *dst,
1358 struct extent_buffer *src, int empty)
1360 int push_items = 0;
1361 int src_nritems;
1362 int dst_nritems;
1363 int ret = 0;
1365 src_nritems = btrfs_header_nritems(src);
1366 dst_nritems = btrfs_header_nritems(dst);
1367 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1368 WARN_ON(btrfs_header_generation(src) != trans->transid);
1369 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1371 if (!empty && src_nritems <= 8)
1372 return 1;
1374 if (push_items <= 0) {
1375 return 1;
1378 if (empty) {
1379 push_items = min(src_nritems, push_items);
1380 if (push_items < src_nritems) {
1381 /* leave at least 8 pointers in the node if
1382 * we aren't going to empty it
1384 if (src_nritems - push_items < 8) {
1385 if (push_items <= 8)
1386 return 1;
1387 push_items -= 8;
1390 } else
1391 push_items = min(src_nritems - 8, push_items);
1393 copy_extent_buffer(dst, src,
1394 btrfs_node_key_ptr_offset(dst_nritems),
1395 btrfs_node_key_ptr_offset(0),
1396 push_items * sizeof(struct btrfs_key_ptr));
1398 if (push_items < src_nritems) {
1399 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1400 btrfs_node_key_ptr_offset(push_items),
1401 (src_nritems - push_items) *
1402 sizeof(struct btrfs_key_ptr));
1404 btrfs_set_header_nritems(src, src_nritems - push_items);
1405 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1406 btrfs_mark_buffer_dirty(src);
1407 btrfs_mark_buffer_dirty(dst);
1409 return ret;
1413 * try to push data from one node into the next node right in the
1414 * tree.
1416 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1417 * error, and > 0 if there was no room in the right hand block.
1419 * this will only push up to 1/2 the contents of the left node over
1421 static int balance_node_right(struct btrfs_trans_handle *trans,
1422 struct btrfs_root *root,
1423 struct extent_buffer *dst,
1424 struct extent_buffer *src)
1426 int push_items = 0;
1427 int max_push;
1428 int src_nritems;
1429 int dst_nritems;
1430 int ret = 0;
1432 WARN_ON(btrfs_header_generation(src) != trans->transid);
1433 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1435 src_nritems = btrfs_header_nritems(src);
1436 dst_nritems = btrfs_header_nritems(dst);
1437 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1438 if (push_items <= 0) {
1439 return 1;
1442 if (src_nritems < 4) {
1443 return 1;
1446 max_push = src_nritems / 2 + 1;
1447 /* don't try to empty the node */
1448 if (max_push >= src_nritems) {
1449 return 1;
1452 if (max_push < push_items)
1453 push_items = max_push;
1455 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1456 btrfs_node_key_ptr_offset(0),
1457 (dst_nritems) *
1458 sizeof(struct btrfs_key_ptr));
1460 copy_extent_buffer(dst, src,
1461 btrfs_node_key_ptr_offset(0),
1462 btrfs_node_key_ptr_offset(src_nritems - push_items),
1463 push_items * sizeof(struct btrfs_key_ptr));
1465 btrfs_set_header_nritems(src, src_nritems - push_items);
1466 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1468 btrfs_mark_buffer_dirty(src);
1469 btrfs_mark_buffer_dirty(dst);
1471 return ret;
1475 * helper function to insert a new root level in the tree.
1476 * A new node is allocated, and a single item is inserted to
1477 * point to the existing root
1479 * returns zero on success or < 0 on failure.
1481 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root,
1483 struct btrfs_path *path, int level)
1485 u64 lower_gen;
1486 struct extent_buffer *lower;
1487 struct extent_buffer *c;
1488 struct extent_buffer *old;
1489 struct btrfs_disk_key lower_key;
1491 BUG_ON(path->nodes[level]);
1492 BUG_ON(path->nodes[level-1] != root->node);
1494 lower = path->nodes[level-1];
1495 if (level == 1)
1496 btrfs_item_key(lower, &lower_key, 0);
1497 else
1498 btrfs_node_key(lower, &lower_key, 0);
1500 c = btrfs_alloc_free_block(trans, root, root->nodesize,
1501 root->root_key.objectid, &lower_key,
1502 level, root->node->start, 0);
1504 if (IS_ERR(c))
1505 return PTR_ERR(c);
1507 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1508 btrfs_set_header_nritems(c, 1);
1509 btrfs_set_header_level(c, level);
1510 btrfs_set_header_bytenr(c, c->start);
1511 btrfs_set_header_generation(c, trans->transid);
1512 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1513 btrfs_set_header_owner(c, root->root_key.objectid);
1515 write_extent_buffer(c, root->fs_info->fsid,
1516 (unsigned long)btrfs_header_fsid(c),
1517 BTRFS_FSID_SIZE);
1519 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1520 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1521 BTRFS_UUID_SIZE);
1523 btrfs_set_node_key(c, &lower_key, 0);
1524 btrfs_set_node_blockptr(c, 0, lower->start);
1525 lower_gen = btrfs_header_generation(lower);
1526 WARN_ON(lower_gen != trans->transid);
1528 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1530 btrfs_mark_buffer_dirty(c);
1532 old = root->node;
1533 root->node = c;
1535 /* the super has an extra ref to root->node */
1536 free_extent_buffer(old);
1538 add_root_to_dirty_list(root);
1539 extent_buffer_get(c);
1540 path->nodes[level] = c;
1541 path->slots[level] = 0;
1542 return 0;
1546 * worker function to insert a single pointer in a node.
1547 * the node should have enough room for the pointer already
1549 * slot and level indicate where you want the key to go, and
1550 * blocknr is the block the key points to.
1552 * returns zero on success and < 0 on any error
1554 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1555 *root, struct btrfs_path *path, struct btrfs_disk_key
1556 *key, u64 bytenr, int slot, int level)
1558 struct extent_buffer *lower;
1559 int nritems;
1561 BUG_ON(!path->nodes[level]);
1562 lower = path->nodes[level];
1563 nritems = btrfs_header_nritems(lower);
1564 if (slot > nritems)
1565 BUG();
1566 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1567 BUG();
1568 if (slot != nritems) {
1569 memmove_extent_buffer(lower,
1570 btrfs_node_key_ptr_offset(slot + 1),
1571 btrfs_node_key_ptr_offset(slot),
1572 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1574 btrfs_set_node_key(lower, key, slot);
1575 btrfs_set_node_blockptr(lower, slot, bytenr);
1576 WARN_ON(trans->transid == 0);
1577 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1578 btrfs_set_header_nritems(lower, nritems + 1);
1579 btrfs_mark_buffer_dirty(lower);
1580 return 0;
1584 * split the node at the specified level in path in two.
1585 * The path is corrected to point to the appropriate node after the split
1587 * Before splitting this tries to make some room in the node by pushing
1588 * left and right, if either one works, it returns right away.
1590 * returns 0 on success and < 0 on failure
1592 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1593 *root, struct btrfs_path *path, int level)
1595 struct extent_buffer *c;
1596 struct extent_buffer *split;
1597 struct btrfs_disk_key disk_key;
1598 int mid;
1599 int ret;
1600 int wret;
1601 u32 c_nritems;
1603 c = path->nodes[level];
1604 WARN_ON(btrfs_header_generation(c) != trans->transid);
1605 if (c == root->node) {
1606 /* trying to split the root, lets make a new one */
1607 ret = insert_new_root(trans, root, path, level + 1);
1608 if (ret)
1609 return ret;
1610 } else {
1611 ret = push_nodes_for_insert(trans, root, path, level);
1612 c = path->nodes[level];
1613 if (!ret && btrfs_header_nritems(c) <
1614 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1615 return 0;
1616 if (ret < 0)
1617 return ret;
1620 c_nritems = btrfs_header_nritems(c);
1621 mid = (c_nritems + 1) / 2;
1622 btrfs_node_key(c, &disk_key, mid);
1624 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1625 root->root_key.objectid,
1626 &disk_key, level, c->start, 0);
1627 if (IS_ERR(split))
1628 return PTR_ERR(split);
1630 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1631 btrfs_set_header_level(split, btrfs_header_level(c));
1632 btrfs_set_header_bytenr(split, split->start);
1633 btrfs_set_header_generation(split, trans->transid);
1634 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1635 btrfs_set_header_owner(split, root->root_key.objectid);
1636 write_extent_buffer(split, root->fs_info->fsid,
1637 (unsigned long)btrfs_header_fsid(split),
1638 BTRFS_FSID_SIZE);
1639 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1640 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1641 BTRFS_UUID_SIZE);
1644 copy_extent_buffer(split, c,
1645 btrfs_node_key_ptr_offset(0),
1646 btrfs_node_key_ptr_offset(mid),
1647 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1648 btrfs_set_header_nritems(split, c_nritems - mid);
1649 btrfs_set_header_nritems(c, mid);
1650 ret = 0;
1652 btrfs_mark_buffer_dirty(c);
1653 btrfs_mark_buffer_dirty(split);
1655 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1656 path->slots[level + 1] + 1,
1657 level + 1);
1658 if (wret)
1659 ret = wret;
1661 if (path->slots[level] >= mid) {
1662 path->slots[level] -= mid;
1663 free_extent_buffer(c);
1664 path->nodes[level] = split;
1665 path->slots[level + 1] += 1;
1666 } else {
1667 free_extent_buffer(split);
1669 return ret;
1673 * how many bytes are required to store the items in a leaf. start
1674 * and nr indicate which items in the leaf to check. This totals up the
1675 * space used both by the item structs and the item data
1677 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1679 int data_len;
1680 int nritems = btrfs_header_nritems(l);
1681 int end = min(nritems, start + nr) - 1;
1683 if (!nr)
1684 return 0;
1685 data_len = btrfs_item_end_nr(l, start);
1686 data_len = data_len - btrfs_item_offset_nr(l, end);
1687 data_len += sizeof(struct btrfs_item) * nr;
1688 WARN_ON(data_len < 0);
1689 return data_len;
1693 * The space between the end of the leaf items and
1694 * the start of the leaf data. IOW, how much room
1695 * the leaf has left for both items and data
1697 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1699 int nritems = btrfs_header_nritems(leaf);
1700 int ret;
1701 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1702 if (ret < 0) {
1703 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1704 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1705 leaf_space_used(leaf, 0, nritems), nritems);
1707 return ret;
1711 * push some data in the path leaf to the right, trying to free up at
1712 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1714 * returns 1 if the push failed because the other node didn't have enough
1715 * room, 0 if everything worked out and < 0 if there were major errors.
1717 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1718 *root, struct btrfs_path *path, int data_size,
1719 int empty)
1721 struct extent_buffer *left = path->nodes[0];
1722 struct extent_buffer *right;
1723 struct extent_buffer *upper;
1724 struct btrfs_disk_key disk_key;
1725 int slot;
1726 u32 i;
1727 int free_space;
1728 int push_space = 0;
1729 int push_items = 0;
1730 struct btrfs_item *item;
1731 u32 left_nritems;
1732 u32 nr;
1733 u32 right_nritems;
1734 u32 data_end;
1735 u32 this_item_size;
1736 int ret;
1738 slot = path->slots[1];
1739 if (!path->nodes[1]) {
1740 return 1;
1742 upper = path->nodes[1];
1743 if (slot >= btrfs_header_nritems(upper) - 1)
1744 return 1;
1746 right = read_node_slot(root, upper, slot + 1);
1747 free_space = btrfs_leaf_free_space(root, right);
1748 if (free_space < data_size) {
1749 free_extent_buffer(right);
1750 return 1;
1753 /* cow and double check */
1754 ret = btrfs_cow_block(trans, root, right, upper,
1755 slot + 1, &right);
1756 if (ret) {
1757 free_extent_buffer(right);
1758 return 1;
1760 free_space = btrfs_leaf_free_space(root, right);
1761 if (free_space < data_size) {
1762 free_extent_buffer(right);
1763 return 1;
1766 left_nritems = btrfs_header_nritems(left);
1767 if (left_nritems == 0) {
1768 free_extent_buffer(right);
1769 return 1;
1772 if (empty)
1773 nr = 0;
1774 else
1775 nr = 1;
1777 i = left_nritems - 1;
1778 while (i >= nr) {
1779 item = btrfs_item_nr(left, i);
1781 if (path->slots[0] == i)
1782 push_space += data_size + sizeof(*item);
1784 this_item_size = btrfs_item_size(left, item);
1785 if (this_item_size + sizeof(*item) + push_space > free_space)
1786 break;
1787 push_items++;
1788 push_space += this_item_size + sizeof(*item);
1789 if (i == 0)
1790 break;
1791 i--;
1794 if (push_items == 0) {
1795 free_extent_buffer(right);
1796 return 1;
1799 if (!empty && push_items == left_nritems)
1800 WARN_ON(1);
1802 /* push left to right */
1803 right_nritems = btrfs_header_nritems(right);
1805 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1806 push_space -= leaf_data_end(root, left);
1808 /* make room in the right data area */
1809 data_end = leaf_data_end(root, right);
1810 memmove_extent_buffer(right,
1811 btrfs_leaf_data(right) + data_end - push_space,
1812 btrfs_leaf_data(right) + data_end,
1813 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1815 /* copy from the left data area */
1816 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1817 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1818 btrfs_leaf_data(left) + leaf_data_end(root, left),
1819 push_space);
1821 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1822 btrfs_item_nr_offset(0),
1823 right_nritems * sizeof(struct btrfs_item));
1825 /* copy the items from left to right */
1826 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1827 btrfs_item_nr_offset(left_nritems - push_items),
1828 push_items * sizeof(struct btrfs_item));
1830 /* update the item pointers */
1831 right_nritems += push_items;
1832 btrfs_set_header_nritems(right, right_nritems);
1833 push_space = BTRFS_LEAF_DATA_SIZE(root);
1834 for (i = 0; i < right_nritems; i++) {
1835 item = btrfs_item_nr(right, i);
1836 push_space -= btrfs_item_size(right, item);
1837 btrfs_set_item_offset(right, item, push_space);
1840 left_nritems -= push_items;
1841 btrfs_set_header_nritems(left, left_nritems);
1843 if (left_nritems)
1844 btrfs_mark_buffer_dirty(left);
1845 btrfs_mark_buffer_dirty(right);
1847 btrfs_item_key(right, &disk_key, 0);
1848 btrfs_set_node_key(upper, &disk_key, slot + 1);
1849 btrfs_mark_buffer_dirty(upper);
1851 /* then fixup the leaf pointer in the path */
1852 if (path->slots[0] >= left_nritems) {
1853 path->slots[0] -= left_nritems;
1854 free_extent_buffer(path->nodes[0]);
1855 path->nodes[0] = right;
1856 path->slots[1] += 1;
1857 } else {
1858 free_extent_buffer(right);
1860 return 0;
1863 * push some data in the path leaf to the left, trying to free up at
1864 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1866 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1867 *root, struct btrfs_path *path, int data_size,
1868 int empty)
1870 struct btrfs_disk_key disk_key;
1871 struct extent_buffer *right = path->nodes[0];
1872 struct extent_buffer *left;
1873 int slot;
1874 int i;
1875 int free_space;
1876 int push_space = 0;
1877 int push_items = 0;
1878 struct btrfs_item *item;
1879 u32 old_left_nritems;
1880 u32 right_nritems;
1881 u32 nr;
1882 int ret = 0;
1883 int wret;
1884 u32 this_item_size;
1885 u32 old_left_item_size;
1887 slot = path->slots[1];
1888 if (slot == 0)
1889 return 1;
1890 if (!path->nodes[1])
1891 return 1;
1893 right_nritems = btrfs_header_nritems(right);
1894 if (right_nritems == 0) {
1895 return 1;
1898 left = read_node_slot(root, path->nodes[1], slot - 1);
1899 free_space = btrfs_leaf_free_space(root, left);
1900 if (free_space < data_size) {
1901 free_extent_buffer(left);
1902 return 1;
1905 /* cow and double check */
1906 ret = btrfs_cow_block(trans, root, left,
1907 path->nodes[1], slot - 1, &left);
1908 if (ret) {
1909 /* we hit -ENOSPC, but it isn't fatal here */
1910 free_extent_buffer(left);
1911 return 1;
1914 free_space = btrfs_leaf_free_space(root, left);
1915 if (free_space < data_size) {
1916 free_extent_buffer(left);
1917 return 1;
1920 if (empty)
1921 nr = right_nritems;
1922 else
1923 nr = right_nritems - 1;
1925 for (i = 0; i < nr; i++) {
1926 item = btrfs_item_nr(right, i);
1928 if (path->slots[0] == i)
1929 push_space += data_size + sizeof(*item);
1931 this_item_size = btrfs_item_size(right, item);
1932 if (this_item_size + sizeof(*item) + push_space > free_space)
1933 break;
1935 push_items++;
1936 push_space += this_item_size + sizeof(*item);
1939 if (push_items == 0) {
1940 free_extent_buffer(left);
1941 return 1;
1943 if (!empty && push_items == btrfs_header_nritems(right))
1944 WARN_ON(1);
1946 /* push data from right to left */
1947 copy_extent_buffer(left, right,
1948 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1949 btrfs_item_nr_offset(0),
1950 push_items * sizeof(struct btrfs_item));
1952 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1953 btrfs_item_offset_nr(right, push_items -1);
1955 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1956 leaf_data_end(root, left) - push_space,
1957 btrfs_leaf_data(right) +
1958 btrfs_item_offset_nr(right, push_items - 1),
1959 push_space);
1960 old_left_nritems = btrfs_header_nritems(left);
1961 BUG_ON(old_left_nritems < 0);
1963 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1964 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1965 u32 ioff;
1967 item = btrfs_item_nr(left, i);
1968 ioff = btrfs_item_offset(left, item);
1969 btrfs_set_item_offset(left, item,
1970 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1972 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1974 /* fixup right node */
1975 if (push_items > right_nritems) {
1976 printk("push items %d nr %u\n", push_items, right_nritems);
1977 WARN_ON(1);
1980 if (push_items < right_nritems) {
1981 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1982 leaf_data_end(root, right);
1983 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1984 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1985 btrfs_leaf_data(right) +
1986 leaf_data_end(root, right), push_space);
1988 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1989 btrfs_item_nr_offset(push_items),
1990 (btrfs_header_nritems(right) - push_items) *
1991 sizeof(struct btrfs_item));
1993 right_nritems -= push_items;
1994 btrfs_set_header_nritems(right, right_nritems);
1995 push_space = BTRFS_LEAF_DATA_SIZE(root);
1996 for (i = 0; i < right_nritems; i++) {
1997 item = btrfs_item_nr(right, i);
1998 push_space = push_space - btrfs_item_size(right, item);
1999 btrfs_set_item_offset(right, item, push_space);
2002 btrfs_mark_buffer_dirty(left);
2003 if (right_nritems)
2004 btrfs_mark_buffer_dirty(right);
2006 btrfs_item_key(right, &disk_key, 0);
2007 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2008 if (wret)
2009 ret = wret;
2011 /* then fixup the leaf pointer in the path */
2012 if (path->slots[0] < push_items) {
2013 path->slots[0] += old_left_nritems;
2014 free_extent_buffer(path->nodes[0]);
2015 path->nodes[0] = left;
2016 path->slots[1] -= 1;
2017 } else {
2018 free_extent_buffer(left);
2019 path->slots[0] -= push_items;
2021 BUG_ON(path->slots[0] < 0);
2022 return ret;
2026 * split the path's leaf in two, making sure there is at least data_size
2027 * available for the resulting leaf level of the path.
2029 * returns 0 if all went well and < 0 on failure.
2031 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2032 struct btrfs_root *root,
2033 struct btrfs_path *path,
2034 struct extent_buffer *l,
2035 struct extent_buffer *right,
2036 int slot, int mid, int nritems)
2038 int data_copy_size;
2039 int rt_data_off;
2040 int i;
2041 int ret = 0;
2042 int wret;
2043 struct btrfs_disk_key disk_key;
2045 nritems = nritems - mid;
2046 btrfs_set_header_nritems(right, nritems);
2047 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2049 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2050 btrfs_item_nr_offset(mid),
2051 nritems * sizeof(struct btrfs_item));
2053 copy_extent_buffer(right, l,
2054 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2055 data_copy_size, btrfs_leaf_data(l) +
2056 leaf_data_end(root, l), data_copy_size);
2058 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2059 btrfs_item_end_nr(l, mid);
2061 for (i = 0; i < nritems; i++) {
2062 struct btrfs_item *item = btrfs_item_nr(right, i);
2063 u32 ioff = btrfs_item_offset(right, item);
2064 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2067 btrfs_set_header_nritems(l, mid);
2068 ret = 0;
2069 btrfs_item_key(right, &disk_key, 0);
2070 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2071 path->slots[1] + 1, 1);
2072 if (wret)
2073 ret = wret;
2075 btrfs_mark_buffer_dirty(right);
2076 btrfs_mark_buffer_dirty(l);
2077 BUG_ON(path->slots[0] != slot);
2079 if (mid <= slot) {
2080 free_extent_buffer(path->nodes[0]);
2081 path->nodes[0] = right;
2082 path->slots[0] -= mid;
2083 path->slots[1] += 1;
2084 } else {
2085 free_extent_buffer(right);
2088 BUG_ON(path->slots[0] < 0);
2090 return ret;
2094 * split the path's leaf in two, making sure there is at least data_size
2095 * available for the resulting leaf level of the path.
2097 * returns 0 if all went well and < 0 on failure.
2099 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2100 struct btrfs_root *root,
2101 struct btrfs_key *ins_key,
2102 struct btrfs_path *path, int data_size,
2103 int extend)
2105 struct btrfs_disk_key disk_key;
2106 struct extent_buffer *l;
2107 u32 nritems;
2108 int mid;
2109 int slot;
2110 struct extent_buffer *right;
2111 int ret = 0;
2112 int wret;
2113 int split;
2114 int num_doubles = 0;
2116 /* first try to make some room by pushing left and right */
2117 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2118 wret = push_leaf_right(trans, root, path, data_size, 0);
2119 if (wret < 0)
2120 return wret;
2121 if (wret) {
2122 wret = push_leaf_left(trans, root, path, data_size, 0);
2123 if (wret < 0)
2124 return wret;
2126 l = path->nodes[0];
2128 /* did the pushes work? */
2129 if (btrfs_leaf_free_space(root, l) >= data_size)
2130 return 0;
2133 if (!path->nodes[1]) {
2134 ret = insert_new_root(trans, root, path, 1);
2135 if (ret)
2136 return ret;
2138 again:
2139 split = 1;
2140 l = path->nodes[0];
2141 slot = path->slots[0];
2142 nritems = btrfs_header_nritems(l);
2143 mid = (nritems + 1) / 2;
2145 if (mid <= slot) {
2146 if (nritems == 1 ||
2147 leaf_space_used(l, mid, nritems - mid) + data_size >
2148 BTRFS_LEAF_DATA_SIZE(root)) {
2149 if (slot >= nritems) {
2150 split = 0;
2151 } else {
2152 mid = slot;
2153 if (mid != nritems &&
2154 leaf_space_used(l, mid, nritems - mid) +
2155 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2156 split = 2;
2160 } else {
2161 if (leaf_space_used(l, 0, mid) + data_size >
2162 BTRFS_LEAF_DATA_SIZE(root)) {
2163 if (!extend && data_size && slot == 0) {
2164 split = 0;
2165 } else if ((extend || !data_size) && slot == 0) {
2166 mid = 1;
2167 } else {
2168 mid = slot;
2169 if (mid != nritems &&
2170 leaf_space_used(l, mid, nritems - mid) +
2171 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2172 split = 2 ;
2178 if (split == 0)
2179 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2180 else
2181 btrfs_item_key(l, &disk_key, mid);
2183 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2184 root->root_key.objectid,
2185 &disk_key, 0, l->start, 0);
2186 if (IS_ERR(right)) {
2187 BUG_ON(1);
2188 return PTR_ERR(right);
2191 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2192 btrfs_set_header_bytenr(right, right->start);
2193 btrfs_set_header_generation(right, trans->transid);
2194 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2195 btrfs_set_header_owner(right, root->root_key.objectid);
2196 btrfs_set_header_level(right, 0);
2197 write_extent_buffer(right, root->fs_info->fsid,
2198 (unsigned long)btrfs_header_fsid(right),
2199 BTRFS_FSID_SIZE);
2201 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2202 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2203 BTRFS_UUID_SIZE);
2205 if (split == 0) {
2206 if (mid <= slot) {
2207 btrfs_set_header_nritems(right, 0);
2208 wret = insert_ptr(trans, root, path,
2209 &disk_key, right->start,
2210 path->slots[1] + 1, 1);
2211 if (wret)
2212 ret = wret;
2214 free_extent_buffer(path->nodes[0]);
2215 path->nodes[0] = right;
2216 path->slots[0] = 0;
2217 path->slots[1] += 1;
2218 } else {
2219 btrfs_set_header_nritems(right, 0);
2220 wret = insert_ptr(trans, root, path,
2221 &disk_key,
2222 right->start,
2223 path->slots[1], 1);
2224 if (wret)
2225 ret = wret;
2226 free_extent_buffer(path->nodes[0]);
2227 path->nodes[0] = right;
2228 path->slots[0] = 0;
2229 if (path->slots[1] == 0) {
2230 wret = fixup_low_keys(trans, root,
2231 path, &disk_key, 1);
2232 if (wret)
2233 ret = wret;
2236 btrfs_mark_buffer_dirty(right);
2237 return ret;
2240 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2241 BUG_ON(ret);
2243 if (split == 2) {
2244 BUG_ON(num_doubles != 0);
2245 num_doubles++;
2246 goto again;
2249 return ret;
2253 * This function splits a single item into two items,
2254 * giving 'new_key' to the new item and splitting the
2255 * old one at split_offset (from the start of the item).
2257 * The path may be released by this operation. After
2258 * the split, the path is pointing to the old item. The
2259 * new item is going to be in the same node as the old one.
2261 * Note, the item being split must be smaller enough to live alone on
2262 * a tree block with room for one extra struct btrfs_item
2264 * This allows us to split the item in place, keeping a lock on the
2265 * leaf the entire time.
2267 int btrfs_split_item(struct btrfs_trans_handle *trans,
2268 struct btrfs_root *root,
2269 struct btrfs_path *path,
2270 struct btrfs_key *new_key,
2271 unsigned long split_offset)
2273 u32 item_size;
2274 struct extent_buffer *leaf;
2275 struct btrfs_key orig_key;
2276 struct btrfs_item *item;
2277 struct btrfs_item *new_item;
2278 int ret = 0;
2279 int slot;
2280 u32 nritems;
2281 u32 orig_offset;
2282 struct btrfs_disk_key disk_key;
2283 char *buf;
2285 leaf = path->nodes[0];
2286 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2287 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2288 goto split;
2290 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2291 btrfs_release_path(root, path);
2293 path->search_for_split = 1;
2295 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2296 path->search_for_split = 0;
2298 /* if our item isn't there or got smaller, return now */
2299 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2300 path->slots[0])) {
2301 return -EAGAIN;
2304 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2305 BUG_ON(ret);
2307 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2308 leaf = path->nodes[0];
2310 split:
2311 item = btrfs_item_nr(leaf, path->slots[0]);
2312 orig_offset = btrfs_item_offset(leaf, item);
2313 item_size = btrfs_item_size(leaf, item);
2316 buf = kmalloc(item_size, GFP_NOFS);
2317 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2318 path->slots[0]), item_size);
2319 slot = path->slots[0] + 1;
2320 leaf = path->nodes[0];
2322 nritems = btrfs_header_nritems(leaf);
2324 if (slot != nritems) {
2325 /* shift the items */
2326 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2327 btrfs_item_nr_offset(slot),
2328 (nritems - slot) * sizeof(struct btrfs_item));
2332 btrfs_cpu_key_to_disk(&disk_key, new_key);
2333 btrfs_set_item_key(leaf, &disk_key, slot);
2335 new_item = btrfs_item_nr(leaf, slot);
2337 btrfs_set_item_offset(leaf, new_item, orig_offset);
2338 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2340 btrfs_set_item_offset(leaf, item,
2341 orig_offset + item_size - split_offset);
2342 btrfs_set_item_size(leaf, item, split_offset);
2344 btrfs_set_header_nritems(leaf, nritems + 1);
2346 /* write the data for the start of the original item */
2347 write_extent_buffer(leaf, buf,
2348 btrfs_item_ptr_offset(leaf, path->slots[0]),
2349 split_offset);
2351 /* write the data for the new item */
2352 write_extent_buffer(leaf, buf + split_offset,
2353 btrfs_item_ptr_offset(leaf, slot),
2354 item_size - split_offset);
2355 btrfs_mark_buffer_dirty(leaf);
2357 ret = 0;
2358 if (btrfs_leaf_free_space(root, leaf) < 0) {
2359 btrfs_print_leaf(root, leaf);
2360 BUG();
2362 kfree(buf);
2363 return ret;
2366 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2367 struct btrfs_root *root,
2368 struct btrfs_path *path,
2369 u32 new_size, int from_end)
2371 int ret = 0;
2372 int slot;
2373 int slot_orig;
2374 struct extent_buffer *leaf;
2375 struct btrfs_item *item;
2376 u32 nritems;
2377 unsigned int data_end;
2378 unsigned int old_data_start;
2379 unsigned int old_size;
2380 unsigned int size_diff;
2381 int i;
2383 slot_orig = path->slots[0];
2384 leaf = path->nodes[0];
2385 slot = path->slots[0];
2387 old_size = btrfs_item_size_nr(leaf, slot);
2388 if (old_size == new_size)
2389 return 0;
2391 nritems = btrfs_header_nritems(leaf);
2392 data_end = leaf_data_end(root, leaf);
2394 old_data_start = btrfs_item_offset_nr(leaf, slot);
2396 size_diff = old_size - new_size;
2398 BUG_ON(slot < 0);
2399 BUG_ON(slot >= nritems);
2402 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2404 /* first correct the data pointers */
2405 for (i = slot; i < nritems; i++) {
2406 u32 ioff;
2407 item = btrfs_item_nr(leaf, i);
2408 ioff = btrfs_item_offset(leaf, item);
2409 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2412 /* shift the data */
2413 if (from_end) {
2414 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2415 data_end + size_diff, btrfs_leaf_data(leaf) +
2416 data_end, old_data_start + new_size - data_end);
2417 } else {
2418 struct btrfs_disk_key disk_key;
2419 u64 offset;
2421 btrfs_item_key(leaf, &disk_key, slot);
2423 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2424 unsigned long ptr;
2425 struct btrfs_file_extent_item *fi;
2427 fi = btrfs_item_ptr(leaf, slot,
2428 struct btrfs_file_extent_item);
2429 fi = (struct btrfs_file_extent_item *)(
2430 (unsigned long)fi - size_diff);
2432 if (btrfs_file_extent_type(leaf, fi) ==
2433 BTRFS_FILE_EXTENT_INLINE) {
2434 ptr = btrfs_item_ptr_offset(leaf, slot);
2435 memmove_extent_buffer(leaf, ptr,
2436 (unsigned long)fi,
2437 offsetof(struct btrfs_file_extent_item,
2438 disk_bytenr));
2442 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2443 data_end + size_diff, btrfs_leaf_data(leaf) +
2444 data_end, old_data_start - data_end);
2446 offset = btrfs_disk_key_offset(&disk_key);
2447 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2448 btrfs_set_item_key(leaf, &disk_key, slot);
2449 if (slot == 0)
2450 fixup_low_keys(trans, root, path, &disk_key, 1);
2453 item = btrfs_item_nr(leaf, slot);
2454 btrfs_set_item_size(leaf, item, new_size);
2455 btrfs_mark_buffer_dirty(leaf);
2457 ret = 0;
2458 if (btrfs_leaf_free_space(root, leaf) < 0) {
2459 btrfs_print_leaf(root, leaf);
2460 BUG();
2462 return ret;
2465 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2466 struct btrfs_root *root, struct btrfs_path *path,
2467 u32 data_size)
2469 int ret = 0;
2470 int slot;
2471 int slot_orig;
2472 struct extent_buffer *leaf;
2473 struct btrfs_item *item;
2474 u32 nritems;
2475 unsigned int data_end;
2476 unsigned int old_data;
2477 unsigned int old_size;
2478 int i;
2480 slot_orig = path->slots[0];
2481 leaf = path->nodes[0];
2483 nritems = btrfs_header_nritems(leaf);
2484 data_end = leaf_data_end(root, leaf);
2486 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2487 btrfs_print_leaf(root, leaf);
2488 BUG();
2490 slot = path->slots[0];
2491 old_data = btrfs_item_end_nr(leaf, slot);
2493 BUG_ON(slot < 0);
2494 if (slot >= nritems) {
2495 btrfs_print_leaf(root, leaf);
2496 printk("slot %d too large, nritems %d\n", slot, nritems);
2497 BUG_ON(1);
2501 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2503 /* first correct the data pointers */
2504 for (i = slot; i < nritems; i++) {
2505 u32 ioff;
2506 item = btrfs_item_nr(leaf, i);
2507 ioff = btrfs_item_offset(leaf, item);
2508 btrfs_set_item_offset(leaf, item, ioff - data_size);
2511 /* shift the data */
2512 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2513 data_end - data_size, btrfs_leaf_data(leaf) +
2514 data_end, old_data - data_end);
2516 data_end = old_data;
2517 old_size = btrfs_item_size_nr(leaf, slot);
2518 item = btrfs_item_nr(leaf, slot);
2519 btrfs_set_item_size(leaf, item, old_size + data_size);
2520 btrfs_mark_buffer_dirty(leaf);
2522 ret = 0;
2523 if (btrfs_leaf_free_space(root, leaf) < 0) {
2524 btrfs_print_leaf(root, leaf);
2525 BUG();
2527 return ret;
2531 * Given a key and some data, insert an item into the tree.
2532 * This does all the path init required, making room in the tree if needed.
2534 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2535 struct btrfs_root *root,
2536 struct btrfs_path *path,
2537 struct btrfs_key *cpu_key, u32 *data_size,
2538 int nr)
2540 struct extent_buffer *leaf;
2541 struct btrfs_item *item;
2542 int ret = 0;
2543 int slot;
2544 int slot_orig;
2545 int i;
2546 u32 nritems;
2547 u32 total_size = 0;
2548 u32 total_data = 0;
2549 unsigned int data_end;
2550 struct btrfs_disk_key disk_key;
2552 for (i = 0; i < nr; i++) {
2553 total_data += data_size[i];
2556 /* create a root if there isn't one */
2557 if (!root->node)
2558 BUG();
2560 total_size = total_data + nr * sizeof(struct btrfs_item);
2561 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2562 if (ret == 0) {
2563 return -EEXIST;
2565 if (ret < 0)
2566 goto out;
2568 slot_orig = path->slots[0];
2569 leaf = path->nodes[0];
2571 nritems = btrfs_header_nritems(leaf);
2572 data_end = leaf_data_end(root, leaf);
2574 if (btrfs_leaf_free_space(root, leaf) < total_size) {
2575 btrfs_print_leaf(root, leaf);
2576 printk("not enough freespace need %u have %d\n",
2577 total_size, btrfs_leaf_free_space(root, leaf));
2578 BUG();
2581 slot = path->slots[0];
2582 BUG_ON(slot < 0);
2584 if (slot != nritems) {
2585 int i;
2586 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2588 if (old_data < data_end) {
2589 btrfs_print_leaf(root, leaf);
2590 printk("slot %d old_data %d data_end %d\n",
2591 slot, old_data, data_end);
2592 BUG_ON(1);
2595 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2597 /* first correct the data pointers */
2598 for (i = slot; i < nritems; i++) {
2599 u32 ioff;
2601 item = btrfs_item_nr(leaf, i);
2602 ioff = btrfs_item_offset(leaf, item);
2603 btrfs_set_item_offset(leaf, item, ioff - total_data);
2606 /* shift the items */
2607 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2608 btrfs_item_nr_offset(slot),
2609 (nritems - slot) * sizeof(struct btrfs_item));
2611 /* shift the data */
2612 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2613 data_end - total_data, btrfs_leaf_data(leaf) +
2614 data_end, old_data - data_end);
2615 data_end = old_data;
2618 /* setup the item for the new data */
2619 for (i = 0; i < nr; i++) {
2620 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2621 btrfs_set_item_key(leaf, &disk_key, slot + i);
2622 item = btrfs_item_nr(leaf, slot + i);
2623 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2624 data_end -= data_size[i];
2625 btrfs_set_item_size(leaf, item, data_size[i]);
2627 btrfs_set_header_nritems(leaf, nritems + nr);
2628 btrfs_mark_buffer_dirty(leaf);
2630 ret = 0;
2631 if (slot == 0) {
2632 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2633 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2636 if (btrfs_leaf_free_space(root, leaf) < 0) {
2637 btrfs_print_leaf(root, leaf);
2638 BUG();
2641 out:
2642 return ret;
2646 * Given a key and some data, insert an item into the tree.
2647 * This does all the path init required, making room in the tree if needed.
2649 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2650 *root, struct btrfs_key *cpu_key, void *data, u32
2651 data_size)
2653 int ret = 0;
2654 struct btrfs_path *path;
2655 struct extent_buffer *leaf;
2656 unsigned long ptr;
2658 path = btrfs_alloc_path();
2659 BUG_ON(!path);
2660 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2661 if (!ret) {
2662 leaf = path->nodes[0];
2663 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2664 write_extent_buffer(leaf, data, ptr, data_size);
2665 btrfs_mark_buffer_dirty(leaf);
2667 btrfs_free_path(path);
2668 return ret;
2672 * delete the pointer from a given node.
2674 * If the delete empties a node, the node is removed from the tree,
2675 * continuing all the way the root if required. The root is converted into
2676 * a leaf if all the nodes are emptied.
2678 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2679 struct btrfs_path *path, int level, int slot)
2681 struct extent_buffer *parent = path->nodes[level];
2682 u32 nritems;
2683 int ret = 0;
2684 int wret;
2686 nritems = btrfs_header_nritems(parent);
2687 if (slot != nritems -1) {
2688 memmove_extent_buffer(parent,
2689 btrfs_node_key_ptr_offset(slot),
2690 btrfs_node_key_ptr_offset(slot + 1),
2691 sizeof(struct btrfs_key_ptr) *
2692 (nritems - slot - 1));
2694 nritems--;
2695 btrfs_set_header_nritems(parent, nritems);
2696 if (nritems == 0 && parent == root->node) {
2697 BUG_ON(btrfs_header_level(root->node) != 1);
2698 /* just turn the root into a leaf and break */
2699 btrfs_set_header_level(root->node, 0);
2700 } else if (slot == 0) {
2701 struct btrfs_disk_key disk_key;
2703 btrfs_node_key(parent, &disk_key, 0);
2704 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2705 if (wret)
2706 ret = wret;
2708 btrfs_mark_buffer_dirty(parent);
2709 return ret;
2713 * a helper function to delete the leaf pointed to by path->slots[1] and
2714 * path->nodes[1].
2716 * This deletes the pointer in path->nodes[1] and frees the leaf
2717 * block extent. zero is returned if it all worked out, < 0 otherwise.
2719 * The path must have already been setup for deleting the leaf, including
2720 * all the proper balancing. path->nodes[1] must be locked.
2722 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2723 struct btrfs_root *root,
2724 struct btrfs_path *path,
2725 struct extent_buffer *leaf)
2727 int ret;
2729 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2730 ret = del_ptr(trans, root, path, 1, path->slots[1]);
2731 if (ret)
2732 return ret;
2734 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2735 0, root->root_key.objectid, 0, 0);
2736 return ret;
2740 * delete the item at the leaf level in path. If that empties
2741 * the leaf, remove it from the tree
2743 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2744 struct btrfs_path *path, int slot, int nr)
2746 struct extent_buffer *leaf;
2747 struct btrfs_item *item;
2748 int last_off;
2749 int dsize = 0;
2750 int ret = 0;
2751 int wret;
2752 int i;
2753 u32 nritems;
2755 leaf = path->nodes[0];
2756 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2758 for (i = 0; i < nr; i++)
2759 dsize += btrfs_item_size_nr(leaf, slot + i);
2761 nritems = btrfs_header_nritems(leaf);
2763 if (slot + nr != nritems) {
2764 int i;
2765 int data_end = leaf_data_end(root, leaf);
2767 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2768 data_end + dsize,
2769 btrfs_leaf_data(leaf) + data_end,
2770 last_off - data_end);
2772 for (i = slot + nr; i < nritems; i++) {
2773 u32 ioff;
2775 item = btrfs_item_nr(leaf, i);
2776 ioff = btrfs_item_offset(leaf, item);
2777 btrfs_set_item_offset(leaf, item, ioff + dsize);
2780 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2781 btrfs_item_nr_offset(slot + nr),
2782 sizeof(struct btrfs_item) *
2783 (nritems - slot - nr));
2785 btrfs_set_header_nritems(leaf, nritems - nr);
2786 nritems -= nr;
2788 /* delete the leaf if we've emptied it */
2789 if (nritems == 0) {
2790 if (leaf == root->node) {
2791 btrfs_set_header_level(leaf, 0);
2792 } else {
2793 clean_tree_block(trans, root, leaf);
2794 wait_on_tree_block_writeback(root, leaf);
2796 wret = btrfs_del_leaf(trans, root, path, leaf);
2797 BUG_ON(ret);
2798 if (wret)
2799 ret = wret;
2801 } else {
2802 int used = leaf_space_used(leaf, 0, nritems);
2803 if (slot == 0) {
2804 struct btrfs_disk_key disk_key;
2806 btrfs_item_key(leaf, &disk_key, 0);
2807 wret = fixup_low_keys(trans, root, path,
2808 &disk_key, 1);
2809 if (wret)
2810 ret = wret;
2813 /* delete the leaf if it is mostly empty */
2814 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2815 /* push_leaf_left fixes the path.
2816 * make sure the path still points to our leaf
2817 * for possible call to del_ptr below
2819 slot = path->slots[1];
2820 extent_buffer_get(leaf);
2822 wret = push_leaf_left(trans, root, path, 1, 1);
2823 if (wret < 0 && wret != -ENOSPC)
2824 ret = wret;
2826 if (path->nodes[0] == leaf &&
2827 btrfs_header_nritems(leaf)) {
2828 wret = push_leaf_right(trans, root, path, 1, 1);
2829 if (wret < 0 && wret != -ENOSPC)
2830 ret = wret;
2833 if (btrfs_header_nritems(leaf) == 0) {
2834 clean_tree_block(trans, root, leaf);
2835 wait_on_tree_block_writeback(root, leaf);
2837 path->slots[1] = slot;
2838 ret = btrfs_del_leaf(trans, root, path, leaf);
2839 BUG_ON(ret);
2840 free_extent_buffer(leaf);
2842 } else {
2843 btrfs_mark_buffer_dirty(leaf);
2844 free_extent_buffer(leaf);
2846 } else {
2847 btrfs_mark_buffer_dirty(leaf);
2850 return ret;
2854 * walk up the tree as far as required to find the previous leaf.
2855 * returns 0 if it found something or 1 if there are no lesser leaves.
2856 * returns < 0 on io errors.
2858 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2860 int slot;
2861 int level = 1;
2862 struct extent_buffer *c;
2863 struct extent_buffer *next = NULL;
2865 while(level < BTRFS_MAX_LEVEL) {
2866 if (!path->nodes[level])
2867 return 1;
2869 slot = path->slots[level];
2870 c = path->nodes[level];
2871 if (slot == 0) {
2872 level++;
2873 if (level == BTRFS_MAX_LEVEL)
2874 return 1;
2875 continue;
2877 slot--;
2879 if (next)
2880 free_extent_buffer(next);
2882 next = read_node_slot(root, c, slot);
2883 break;
2885 path->slots[level] = slot;
2886 while(1) {
2887 level--;
2888 c = path->nodes[level];
2889 free_extent_buffer(c);
2890 slot = btrfs_header_nritems(next);
2891 if (slot != 0)
2892 slot--;
2893 path->nodes[level] = next;
2894 path->slots[level] = slot;
2895 if (!level)
2896 break;
2897 next = read_node_slot(root, next, slot);
2899 return 0;
2903 * walk up the tree as far as required to find the next leaf.
2904 * returns 0 if it found something or 1 if there are no greater leaves.
2905 * returns < 0 on io errors.
2907 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2909 int slot;
2910 int level = 1;
2911 struct extent_buffer *c;
2912 struct extent_buffer *next = NULL;
2914 while(level < BTRFS_MAX_LEVEL) {
2915 if (!path->nodes[level])
2916 return 1;
2918 slot = path->slots[level] + 1;
2919 c = path->nodes[level];
2920 if (slot >= btrfs_header_nritems(c)) {
2921 level++;
2922 if (level == BTRFS_MAX_LEVEL)
2923 return 1;
2924 continue;
2927 if (next)
2928 free_extent_buffer(next);
2930 if (path->reada)
2931 reada_for_search(root, path, level, slot, 0);
2933 next = read_node_slot(root, c, slot);
2934 break;
2936 path->slots[level] = slot;
2937 while(1) {
2938 level--;
2939 c = path->nodes[level];
2940 free_extent_buffer(c);
2941 path->nodes[level] = next;
2942 path->slots[level] = 0;
2943 if (!level)
2944 break;
2945 if (path->reada)
2946 reada_for_search(root, path, level, 0, 0);
2947 next = read_node_slot(root, next, 0);
2949 return 0;
2952 int btrfs_previous_item(struct btrfs_root *root,
2953 struct btrfs_path *path, u64 min_objectid,
2954 int type)
2956 struct btrfs_key found_key;
2957 struct extent_buffer *leaf;
2958 int ret;
2960 while(1) {
2961 if (path->slots[0] == 0) {
2962 ret = btrfs_prev_leaf(root, path);
2963 if (ret != 0)
2964 return ret;
2965 } else {
2966 path->slots[0]--;
2968 leaf = path->nodes[0];
2969 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2970 if (found_key.type == type)
2971 return 0;
2973 return 1;