btrfs-progs: check: move reada_walk_down to check/common.c
[btrfs-progs-unstable/devel.git] / ctree.c
blob45b368ce2f318a9c174df2127a3a415fc3da1e4b
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"
22 #include "repair.h"
23 #include "internal.h"
24 #include "sizes.h"
26 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_key *ins_key,
30 struct btrfs_path *path, int data_size, int extend);
31 static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
33 struct extent_buffer *src, int empty);
34 static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
39 inline void btrfs_init_path(struct btrfs_path *p)
41 memset(p, 0, sizeof(*p));
44 struct btrfs_path *btrfs_alloc_path(void)
46 struct btrfs_path *path;
47 path = kzalloc(sizeof(struct btrfs_path), GFP_NOFS);
48 return path;
51 void btrfs_free_path(struct btrfs_path *p)
53 if (!p)
54 return;
55 btrfs_release_path(p);
56 kfree(p);
59 void btrfs_release_path(struct btrfs_path *p)
61 int i;
62 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
63 if (!p->nodes[i])
64 continue;
65 free_extent_buffer(p->nodes[i]);
67 memset(p, 0, sizeof(*p));
70 void add_root_to_dirty_list(struct btrfs_root *root)
72 if (root->track_dirty && list_empty(&root->dirty_list)) {
73 list_add(&root->dirty_list,
74 &root->fs_info->dirty_cowonly_roots);
78 int btrfs_copy_root(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root,
80 struct extent_buffer *buf,
81 struct extent_buffer **cow_ret, u64 new_root_objectid)
83 struct extent_buffer *cow;
84 int ret = 0;
85 int level;
86 struct btrfs_root *new_root;
87 struct btrfs_disk_key disk_key;
89 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
90 if (!new_root)
91 return -ENOMEM;
93 memcpy(new_root, root, sizeof(*new_root));
94 new_root->root_key.objectid = new_root_objectid;
96 WARN_ON(root->ref_cows && trans->transid !=
97 root->fs_info->running_transaction->transid);
98 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
100 level = btrfs_header_level(buf);
101 if (level == 0)
102 btrfs_item_key(buf, &disk_key, 0);
103 else
104 btrfs_node_key(buf, &disk_key, 0);
105 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
106 new_root_objectid, &disk_key,
107 level, buf->start, 0);
108 if (IS_ERR(cow)) {
109 kfree(new_root);
110 return PTR_ERR(cow);
113 copy_extent_buffer(cow, buf, 0, 0, cow->len);
114 btrfs_set_header_bytenr(cow, cow->start);
115 btrfs_set_header_generation(cow, trans->transid);
116 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
117 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
118 BTRFS_HEADER_FLAG_RELOC);
119 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
120 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
121 else
122 btrfs_set_header_owner(cow, new_root_objectid);
124 write_extent_buffer(cow, root->fs_info->fsid,
125 btrfs_header_fsid(), BTRFS_FSID_SIZE);
127 WARN_ON(btrfs_header_generation(buf) > trans->transid);
128 ret = btrfs_inc_ref(trans, new_root, cow, 0);
129 kfree(new_root);
131 if (ret)
132 return ret;
134 btrfs_mark_buffer_dirty(cow);
135 *cow_ret = cow;
136 return 0;
140 * check if the tree block can be shared by multiple trees
142 static int btrfs_block_can_be_shared(struct btrfs_root *root,
143 struct extent_buffer *buf)
146 * Tree blocks not in reference counted trees and tree roots
147 * are never shared. If a block was allocated after the last
148 * snapshot and the block was not allocated by tree relocation,
149 * we know the block is not shared.
151 if (root->ref_cows &&
152 buf != root->node && buf != root->commit_root &&
153 (btrfs_header_generation(buf) <=
154 btrfs_root_last_snapshot(&root->root_item) ||
155 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
156 return 1;
157 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
158 if (root->ref_cows &&
159 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
160 return 1;
161 #endif
162 return 0;
165 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
166 struct btrfs_root *root,
167 struct extent_buffer *buf,
168 struct extent_buffer *cow)
170 u64 refs;
171 u64 owner;
172 u64 flags;
173 u64 new_flags = 0;
174 int ret;
177 * Backrefs update rules:
179 * Always use full backrefs for extent pointers in tree block
180 * allocated by tree relocation.
182 * If a shared tree block is no longer referenced by its owner
183 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
184 * use full backrefs for extent pointers in tree block.
186 * If a tree block is been relocating
187 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
188 * use full backrefs for extent pointers in tree block.
189 * The reason for this is some operations (such as drop tree)
190 * are only allowed for blocks use full backrefs.
193 if (btrfs_block_can_be_shared(root, buf)) {
194 ret = btrfs_lookup_extent_info(trans, root, buf->start,
195 btrfs_header_level(buf), 1,
196 &refs, &flags);
197 BUG_ON(ret);
198 BUG_ON(refs == 0);
199 } else {
200 refs = 1;
201 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
202 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
203 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
204 else
205 flags = 0;
208 owner = btrfs_header_owner(buf);
209 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
210 owner == BTRFS_TREE_RELOC_OBJECTID);
212 if (refs > 1) {
213 if ((owner == root->root_key.objectid ||
214 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
215 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
216 ret = btrfs_inc_ref(trans, root, buf, 1);
217 BUG_ON(ret);
219 if (root->root_key.objectid ==
220 BTRFS_TREE_RELOC_OBJECTID) {
221 ret = btrfs_dec_ref(trans, root, buf, 0);
222 BUG_ON(ret);
223 ret = btrfs_inc_ref(trans, root, cow, 1);
224 BUG_ON(ret);
226 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
227 } else {
229 if (root->root_key.objectid ==
230 BTRFS_TREE_RELOC_OBJECTID)
231 ret = btrfs_inc_ref(trans, root, cow, 1);
232 else
233 ret = btrfs_inc_ref(trans, root, cow, 0);
234 BUG_ON(ret);
236 if (new_flags != 0) {
237 ret = btrfs_set_block_flags(trans, root, buf->start,
238 btrfs_header_level(buf),
239 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 struct extent_buffer *cow;
266 struct btrfs_disk_key disk_key;
267 int level;
269 WARN_ON(root->ref_cows && trans->transid !=
270 root->fs_info->running_transaction->transid);
271 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
273 level = btrfs_header_level(buf);
275 if (level == 0)
276 btrfs_item_key(buf, &disk_key, 0);
277 else
278 btrfs_node_key(buf, &disk_key, 0);
280 cow = btrfs_alloc_free_block(trans, root, buf->len,
281 root->root_key.objectid, &disk_key,
282 level, search_start, empty_size);
283 if (IS_ERR(cow))
284 return PTR_ERR(cow);
286 copy_extent_buffer(cow, buf, 0, 0, cow->len);
287 btrfs_set_header_bytenr(cow, cow->start);
288 btrfs_set_header_generation(cow, trans->transid);
289 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
290 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
291 BTRFS_HEADER_FLAG_RELOC);
292 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
293 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
294 else
295 btrfs_set_header_owner(cow, root->root_key.objectid);
297 write_extent_buffer(cow, root->fs_info->fsid,
298 btrfs_header_fsid(), BTRFS_FSID_SIZE);
300 WARN_ON(!(buf->flags & EXTENT_BAD_TRANSID) &&
301 btrfs_header_generation(buf) > trans->transid);
303 update_ref_for_cow(trans, root, buf, cow);
305 if (buf == root->node) {
306 root->node = cow;
307 extent_buffer_get(cow);
309 btrfs_free_extent(trans, root, buf->start, buf->len,
310 0, root->root_key.objectid, level, 0);
311 free_extent_buffer(buf);
312 add_root_to_dirty_list(root);
313 } else {
314 btrfs_set_node_blockptr(parent, parent_slot,
315 cow->start);
316 WARN_ON(trans->transid == 0);
317 btrfs_set_node_ptr_generation(parent, parent_slot,
318 trans->transid);
319 btrfs_mark_buffer_dirty(parent);
320 WARN_ON(btrfs_header_generation(parent) != trans->transid);
322 btrfs_free_extent(trans, root, buf->start, buf->len,
323 0, root->root_key.objectid, level, 1);
325 if (!list_empty(&buf->recow)) {
326 list_del_init(&buf->recow);
327 free_extent_buffer(buf);
329 free_extent_buffer(buf);
330 btrfs_mark_buffer_dirty(cow);
331 *cow_ret = cow;
332 return 0;
335 static inline int should_cow_block(struct btrfs_trans_handle *trans,
336 struct btrfs_root *root,
337 struct extent_buffer *buf)
339 if (btrfs_header_generation(buf) == trans->transid &&
340 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
341 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
342 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
343 return 0;
344 return 1;
347 int btrfs_cow_block(struct btrfs_trans_handle *trans,
348 struct btrfs_root *root, struct extent_buffer *buf,
349 struct extent_buffer *parent, int parent_slot,
350 struct extent_buffer **cow_ret)
352 u64 search_start;
353 int ret;
355 if (trans->transaction != root->fs_info->running_transaction) {
356 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
357 root->fs_info->running_transaction->transid);
358 WARN_ON(1);
361 if (trans->transid != root->fs_info->generation) {
362 printk(KERN_CRIT "trans %llu running %llu\n",
363 (unsigned long long)trans->transid,
364 (unsigned long long)root->fs_info->generation);
365 WARN_ON(1);
367 if (!should_cow_block(trans, root, buf)) {
368 *cow_ret = buf;
369 return 0;
372 search_start = buf->start & ~((u64)SZ_1G - 1);
373 ret = __btrfs_cow_block(trans, root, buf, parent,
374 parent_slot, cow_ret, search_start, 0);
375 return ret;
378 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
380 if (k1->objectid > k2->objectid)
381 return 1;
382 if (k1->objectid < k2->objectid)
383 return -1;
384 if (k1->type > k2->type)
385 return 1;
386 if (k1->type < k2->type)
387 return -1;
388 if (k1->offset > k2->offset)
389 return 1;
390 if (k1->offset < k2->offset)
391 return -1;
392 return 0;
396 * compare two keys in a memcmp fashion
398 static int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
400 struct btrfs_key k1;
402 btrfs_disk_key_to_cpu(&k1, disk);
403 return btrfs_comp_cpu_keys(&k1, k2);
407 * The leaf data grows from end-to-front in the node.
408 * this returns the address of the start of the last item,
409 * which is the stop of the leaf data stack
411 static inline unsigned int leaf_data_end(const struct btrfs_fs_info *fs_info,
412 const struct extent_buffer *leaf)
414 u32 nr = btrfs_header_nritems(leaf);
415 if (nr == 0)
416 return BTRFS_LEAF_DATA_SIZE(fs_info);
417 return btrfs_item_offset_nr(leaf, nr - 1);
420 enum btrfs_tree_block_status
421 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
422 struct extent_buffer *buf)
424 int i;
425 struct btrfs_key cpukey;
426 struct btrfs_disk_key key;
427 u32 nritems = btrfs_header_nritems(buf);
428 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
430 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
431 goto fail;
433 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
434 if (parent_key && parent_key->type) {
435 btrfs_node_key(buf, &key, 0);
436 if (memcmp(parent_key, &key, sizeof(key)))
437 goto fail;
439 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
440 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
441 btrfs_node_key(buf, &key, i);
442 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
443 if (btrfs_comp_keys(&key, &cpukey) >= 0)
444 goto fail;
446 return BTRFS_TREE_BLOCK_CLEAN;
447 fail:
448 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
449 if (parent_key)
450 btrfs_disk_key_to_cpu(&cpukey, parent_key);
451 else
452 btrfs_node_key_to_cpu(buf, &cpukey, 0);
453 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
454 buf->start, buf->len,
455 btrfs_header_level(buf));
457 return ret;
460 enum btrfs_tree_block_status
461 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
462 struct extent_buffer *buf)
464 int i;
465 struct btrfs_key cpukey;
466 struct btrfs_disk_key key;
467 u32 nritems = btrfs_header_nritems(buf);
468 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
470 if (nritems * sizeof(struct btrfs_item) > buf->len) {
471 fprintf(stderr, "invalid number of items %llu\n",
472 (unsigned long long)buf->start);
473 goto fail;
476 if (btrfs_header_level(buf) != 0) {
477 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
478 fprintf(stderr, "leaf is not a leaf %llu\n",
479 (unsigned long long)btrfs_header_bytenr(buf));
480 goto fail;
482 if (btrfs_leaf_free_space(root, buf) < 0) {
483 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
484 fprintf(stderr, "leaf free space incorrect %llu %d\n",
485 (unsigned long long)btrfs_header_bytenr(buf),
486 btrfs_leaf_free_space(root, buf));
487 goto fail;
490 if (nritems == 0)
491 return BTRFS_TREE_BLOCK_CLEAN;
493 btrfs_item_key(buf, &key, 0);
494 if (parent_key && parent_key->type &&
495 memcmp(parent_key, &key, sizeof(key))) {
496 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
497 fprintf(stderr, "leaf parent key incorrect %llu\n",
498 (unsigned long long)btrfs_header_bytenr(buf));
499 goto fail;
501 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
502 btrfs_item_key(buf, &key, i);
503 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
504 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
505 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
506 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
507 goto fail;
509 if (btrfs_item_offset_nr(buf, i) !=
510 btrfs_item_end_nr(buf, i + 1)) {
511 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
512 fprintf(stderr, "incorrect offsets %u %u\n",
513 btrfs_item_offset_nr(buf, i),
514 btrfs_item_end_nr(buf, i + 1));
515 goto fail;
517 if (i == 0 && btrfs_item_end_nr(buf, i) !=
518 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
519 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
520 fprintf(stderr, "bad item end %u wanted %u\n",
521 btrfs_item_end_nr(buf, i),
522 (unsigned)BTRFS_LEAF_DATA_SIZE(root->fs_info));
523 goto fail;
527 for (i = 0; i < nritems; i++) {
528 if (btrfs_item_end_nr(buf, i) >
529 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
530 btrfs_item_key(buf, &key, 0);
531 btrfs_print_key(&key);
532 fflush(stdout);
533 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
534 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
535 (unsigned long long)btrfs_item_end_nr(buf, i),
536 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
537 root->fs_info));
538 goto fail;
542 return BTRFS_TREE_BLOCK_CLEAN;
543 fail:
544 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
545 if (parent_key)
546 btrfs_disk_key_to_cpu(&cpukey, parent_key);
547 else
548 btrfs_item_key_to_cpu(buf, &cpukey, 0);
550 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
551 buf->start, buf->len, 0);
553 return ret;
556 static int noinline check_block(struct btrfs_root *root,
557 struct btrfs_path *path, int level)
559 struct btrfs_disk_key key;
560 struct btrfs_disk_key *key_ptr = NULL;
561 struct extent_buffer *parent;
562 enum btrfs_tree_block_status ret;
564 if (path->skip_check_block)
565 return 0;
566 if (path->nodes[level + 1]) {
567 parent = path->nodes[level + 1];
568 btrfs_node_key(parent, &key, path->slots[level + 1]);
569 key_ptr = &key;
571 if (level == 0)
572 ret = btrfs_check_leaf(root, key_ptr, path->nodes[0]);
573 else
574 ret = btrfs_check_node(root, key_ptr, path->nodes[level]);
575 if (ret == BTRFS_TREE_BLOCK_CLEAN)
576 return 0;
577 return -EIO;
581 * search for key in the extent_buffer. The items start at offset p,
582 * and they are item_size apart. There are 'max' items in p.
584 * the slot in the array is returned via slot, and it points to
585 * the place where you would insert key if it is not found in
586 * the array.
588 * slot may point to max if the key is bigger than all of the keys
590 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
591 int item_size, struct btrfs_key *key,
592 int max, int *slot)
594 int low = 0;
595 int high = max;
596 int mid;
597 int ret;
598 unsigned long offset;
599 struct btrfs_disk_key *tmp;
601 while(low < high) {
602 mid = (low + high) / 2;
603 offset = p + mid * item_size;
605 tmp = (struct btrfs_disk_key *)(eb->data + offset);
606 ret = btrfs_comp_keys(tmp, key);
608 if (ret < 0)
609 low = mid + 1;
610 else if (ret > 0)
611 high = mid;
612 else {
613 *slot = mid;
614 return 0;
617 *slot = low;
618 return 1;
622 * simple bin_search frontend that does the right thing for
623 * leaves vs nodes
625 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
626 int level, int *slot)
628 if (level == 0)
629 return generic_bin_search(eb,
630 offsetof(struct btrfs_leaf, items),
631 sizeof(struct btrfs_item),
632 key, btrfs_header_nritems(eb),
633 slot);
634 else
635 return generic_bin_search(eb,
636 offsetof(struct btrfs_node, ptrs),
637 sizeof(struct btrfs_key_ptr),
638 key, btrfs_header_nritems(eb),
639 slot);
642 struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
643 struct extent_buffer *parent, int slot)
645 int level = btrfs_header_level(parent);
646 if (slot < 0)
647 return NULL;
648 if (slot >= btrfs_header_nritems(parent))
649 return NULL;
651 if (level == 0)
652 return NULL;
654 return read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
655 btrfs_node_ptr_generation(parent, slot));
658 static int balance_level(struct btrfs_trans_handle *trans,
659 struct btrfs_root *root,
660 struct btrfs_path *path, int level)
662 struct extent_buffer *right = NULL;
663 struct extent_buffer *mid;
664 struct extent_buffer *left = NULL;
665 struct extent_buffer *parent = NULL;
666 struct btrfs_fs_info *fs_info = root->fs_info;
667 int ret = 0;
668 int wret;
669 int pslot;
670 int orig_slot = path->slots[level];
671 u64 orig_ptr;
673 if (level == 0)
674 return 0;
676 mid = path->nodes[level];
677 WARN_ON(btrfs_header_generation(mid) != trans->transid);
679 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
681 if (level < BTRFS_MAX_LEVEL - 1) {
682 parent = path->nodes[level + 1];
683 pslot = path->slots[level + 1];
687 * deal with the case where there is only one pointer in the root
688 * by promoting the node below to a root
690 if (!parent) {
691 struct extent_buffer *child;
693 if (btrfs_header_nritems(mid) != 1)
694 return 0;
696 /* promote the child to a root */
697 child = read_node_slot(fs_info, mid, 0);
698 BUG_ON(!extent_buffer_uptodate(child));
699 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
700 BUG_ON(ret);
702 root->node = child;
703 add_root_to_dirty_list(root);
704 path->nodes[level] = NULL;
705 clean_tree_block(trans, root, mid);
706 /* once for the path */
707 free_extent_buffer(mid);
709 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
710 0, root->root_key.objectid,
711 level, 1);
712 /* once for the root ptr */
713 free_extent_buffer(mid);
714 return ret;
716 if (btrfs_header_nritems(mid) >
717 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
718 return 0;
720 left = read_node_slot(fs_info, parent, pslot - 1);
721 if (extent_buffer_uptodate(left)) {
722 wret = btrfs_cow_block(trans, root, left,
723 parent, pslot - 1, &left);
724 if (wret) {
725 ret = wret;
726 goto enospc;
729 right = read_node_slot(fs_info, parent, pslot + 1);
730 if (extent_buffer_uptodate(right)) {
731 wret = btrfs_cow_block(trans, root, right,
732 parent, pslot + 1, &right);
733 if (wret) {
734 ret = wret;
735 goto enospc;
739 /* first, try to make some room in the middle buffer */
740 if (left) {
741 orig_slot += btrfs_header_nritems(left);
742 wret = push_node_left(trans, root, left, mid, 1);
743 if (wret < 0)
744 ret = wret;
748 * then try to empty the right most buffer into the middle
750 if (right) {
751 wret = push_node_left(trans, root, mid, right, 1);
752 if (wret < 0 && wret != -ENOSPC)
753 ret = wret;
754 if (btrfs_header_nritems(right) == 0) {
755 u64 bytenr = right->start;
756 u32 blocksize = right->len;
758 clean_tree_block(trans, root, right);
759 free_extent_buffer(right);
760 right = NULL;
761 wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
762 if (wret)
763 ret = wret;
764 wret = btrfs_free_extent(trans, root, bytenr,
765 blocksize, 0,
766 root->root_key.objectid,
767 level, 0);
768 if (wret)
769 ret = wret;
770 } else {
771 struct btrfs_disk_key right_key;
772 btrfs_node_key(right, &right_key, 0);
773 btrfs_set_node_key(parent, &right_key, pslot + 1);
774 btrfs_mark_buffer_dirty(parent);
777 if (btrfs_header_nritems(mid) == 1) {
779 * we're not allowed to leave a node with one item in the
780 * tree during a delete. A deletion from lower in the tree
781 * could try to delete the only pointer in this node.
782 * So, pull some keys from the left.
783 * There has to be a left pointer at this point because
784 * otherwise we would have pulled some pointers from the
785 * right
787 BUG_ON(!left);
788 wret = balance_node_right(trans, root, mid, left);
789 if (wret < 0) {
790 ret = wret;
791 goto enospc;
793 if (wret == 1) {
794 wret = push_node_left(trans, root, left, mid, 1);
795 if (wret < 0)
796 ret = wret;
798 BUG_ON(wret == 1);
800 if (btrfs_header_nritems(mid) == 0) {
801 /* we've managed to empty the middle node, drop it */
802 u64 bytenr = mid->start;
803 u32 blocksize = mid->len;
804 clean_tree_block(trans, root, mid);
805 free_extent_buffer(mid);
806 mid = NULL;
807 wret = btrfs_del_ptr(root, path, level + 1, pslot);
808 if (wret)
809 ret = wret;
810 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
811 0, root->root_key.objectid,
812 level, 0);
813 if (wret)
814 ret = wret;
815 } else {
816 /* update the parent key to reflect our changes */
817 struct btrfs_disk_key mid_key;
818 btrfs_node_key(mid, &mid_key, 0);
819 btrfs_set_node_key(parent, &mid_key, pslot);
820 btrfs_mark_buffer_dirty(parent);
823 /* update the path */
824 if (left) {
825 if (btrfs_header_nritems(left) > orig_slot) {
826 extent_buffer_get(left);
827 path->nodes[level] = left;
828 path->slots[level + 1] -= 1;
829 path->slots[level] = orig_slot;
830 if (mid)
831 free_extent_buffer(mid);
832 } else {
833 orig_slot -= btrfs_header_nritems(left);
834 path->slots[level] = orig_slot;
837 /* double check we haven't messed things up */
838 check_block(root, path, level);
839 if (orig_ptr !=
840 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
841 BUG();
842 enospc:
843 if (right)
844 free_extent_buffer(right);
845 if (left)
846 free_extent_buffer(left);
847 return ret;
850 /* returns zero if the push worked, non-zero otherwise */
851 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
852 struct btrfs_root *root,
853 struct btrfs_path *path, int level)
855 struct extent_buffer *right = NULL;
856 struct extent_buffer *mid;
857 struct extent_buffer *left = NULL;
858 struct extent_buffer *parent = NULL;
859 struct btrfs_fs_info *fs_info = root->fs_info;
860 int ret = 0;
861 int wret;
862 int pslot;
863 int orig_slot = path->slots[level];
865 if (level == 0)
866 return 1;
868 mid = path->nodes[level];
869 WARN_ON(btrfs_header_generation(mid) != trans->transid);
871 if (level < BTRFS_MAX_LEVEL - 1) {
872 parent = path->nodes[level + 1];
873 pslot = path->slots[level + 1];
876 if (!parent)
877 return 1;
879 left = read_node_slot(fs_info, parent, pslot - 1);
881 /* first, try to make some room in the middle buffer */
882 if (extent_buffer_uptodate(left)) {
883 u32 left_nr;
884 left_nr = btrfs_header_nritems(left);
885 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
886 wret = 1;
887 } else {
888 ret = btrfs_cow_block(trans, root, left, parent,
889 pslot - 1, &left);
890 if (ret)
891 wret = 1;
892 else {
893 wret = push_node_left(trans, root,
894 left, mid, 0);
897 if (wret < 0)
898 ret = wret;
899 if (wret == 0) {
900 struct btrfs_disk_key disk_key;
901 orig_slot += left_nr;
902 btrfs_node_key(mid, &disk_key, 0);
903 btrfs_set_node_key(parent, &disk_key, pslot);
904 btrfs_mark_buffer_dirty(parent);
905 if (btrfs_header_nritems(left) > orig_slot) {
906 path->nodes[level] = left;
907 path->slots[level + 1] -= 1;
908 path->slots[level] = orig_slot;
909 free_extent_buffer(mid);
910 } else {
911 orig_slot -=
912 btrfs_header_nritems(left);
913 path->slots[level] = orig_slot;
914 free_extent_buffer(left);
916 return 0;
918 free_extent_buffer(left);
920 right= read_node_slot(fs_info, parent, pslot + 1);
923 * then try to empty the right most buffer into the middle
925 if (extent_buffer_uptodate(right)) {
926 u32 right_nr;
927 right_nr = btrfs_header_nritems(right);
928 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 1) {
929 wret = 1;
930 } else {
931 ret = btrfs_cow_block(trans, root, right,
932 parent, pslot + 1,
933 &right);
934 if (ret)
935 wret = 1;
936 else {
937 wret = balance_node_right(trans, root,
938 right, mid);
941 if (wret < 0)
942 ret = wret;
943 if (wret == 0) {
944 struct btrfs_disk_key disk_key;
946 btrfs_node_key(right, &disk_key, 0);
947 btrfs_set_node_key(parent, &disk_key, pslot + 1);
948 btrfs_mark_buffer_dirty(parent);
950 if (btrfs_header_nritems(mid) <= orig_slot) {
951 path->nodes[level] = right;
952 path->slots[level + 1] += 1;
953 path->slots[level] = orig_slot -
954 btrfs_header_nritems(mid);
955 free_extent_buffer(mid);
956 } else {
957 free_extent_buffer(right);
959 return 0;
961 free_extent_buffer(right);
963 return 1;
967 * readahead one full node of leaves
969 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
970 int level, int slot, u64 objectid)
972 struct btrfs_fs_info *fs_info = root->fs_info;
973 struct extent_buffer *node;
974 struct btrfs_disk_key disk_key;
975 u32 nritems;
976 u64 search;
977 u64 lowest_read;
978 u64 highest_read;
979 u64 nread = 0;
980 int direction = path->reada;
981 struct extent_buffer *eb;
982 u32 nr;
983 u32 nscan = 0;
985 if (level != 1)
986 return;
988 if (!path->nodes[level])
989 return;
991 node = path->nodes[level];
992 search = btrfs_node_blockptr(node, slot);
993 eb = btrfs_find_tree_block(fs_info, search, fs_info->nodesize);
994 if (eb) {
995 free_extent_buffer(eb);
996 return;
999 highest_read = search;
1000 lowest_read = search;
1002 nritems = btrfs_header_nritems(node);
1003 nr = slot;
1004 while(1) {
1005 if (direction < 0) {
1006 if (nr == 0)
1007 break;
1008 nr--;
1009 } else if (direction > 0) {
1010 nr++;
1011 if (nr >= nritems)
1012 break;
1014 if (path->reada < 0 && objectid) {
1015 btrfs_node_key(node, &disk_key, nr);
1016 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1017 break;
1019 search = btrfs_node_blockptr(node, nr);
1020 if ((search >= lowest_read && search <= highest_read) ||
1021 (search < lowest_read && lowest_read - search <= 32768) ||
1022 (search > highest_read && search - highest_read <= 32768)) {
1023 readahead_tree_block(fs_info, search,
1024 btrfs_node_ptr_generation(node, nr));
1025 nread += fs_info->nodesize;
1027 nscan++;
1028 if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
1029 break;
1030 if(nread > SZ_1M || nscan > 128)
1031 break;
1033 if (search < lowest_read)
1034 lowest_read = search;
1035 if (search > highest_read)
1036 highest_read = search;
1040 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1041 u64 iobjectid, u64 ioff, u8 key_type,
1042 struct btrfs_key *found_key)
1044 int ret;
1045 struct btrfs_key key;
1046 struct extent_buffer *eb;
1047 struct btrfs_path *path;
1049 key.type = key_type;
1050 key.objectid = iobjectid;
1051 key.offset = ioff;
1053 if (found_path == NULL) {
1054 path = btrfs_alloc_path();
1055 if (!path)
1056 return -ENOMEM;
1057 } else
1058 path = found_path;
1060 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1061 if ((ret < 0) || (found_key == NULL))
1062 goto out;
1064 eb = path->nodes[0];
1065 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1066 ret = btrfs_next_leaf(fs_root, path);
1067 if (ret)
1068 goto out;
1069 eb = path->nodes[0];
1072 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1073 if (found_key->type != key.type ||
1074 found_key->objectid != key.objectid) {
1075 ret = 1;
1076 goto out;
1079 out:
1080 if (path != found_path)
1081 btrfs_free_path(path);
1082 return ret;
1086 * look for key in the tree. path is filled in with nodes along the way
1087 * if key is found, we return zero and you can find the item in the leaf
1088 * level of the path (level 0)
1090 * If the key isn't found, the path points to the slot where it should
1091 * be inserted, and 1 is returned. If there are other errors during the
1092 * search a negative error number is returned.
1094 * if ins_len > 0, nodes and leaves will be split as we walk down the
1095 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1096 * possible)
1098 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1099 *root, struct btrfs_key *key, struct btrfs_path *p, int
1100 ins_len, int cow)
1102 struct extent_buffer *b;
1103 int slot;
1104 int ret;
1105 int level;
1106 int should_reada = p->reada;
1107 struct btrfs_fs_info *fs_info = root->fs_info;
1108 u8 lowest_level = 0;
1110 lowest_level = p->lowest_level;
1111 WARN_ON(lowest_level && ins_len > 0);
1112 WARN_ON(p->nodes[0] != NULL);
1114 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1116 again:
1117 b = root->node;
1118 extent_buffer_get(b);
1119 while (b) {
1120 level = btrfs_header_level(b);
1121 if (cow) {
1122 int wret;
1123 wret = btrfs_cow_block(trans, root, b,
1124 p->nodes[level + 1],
1125 p->slots[level + 1],
1126 &b);
1127 if (wret) {
1128 free_extent_buffer(b);
1129 return wret;
1132 BUG_ON(!cow && ins_len);
1133 if (level != btrfs_header_level(b))
1134 WARN_ON(1);
1135 level = btrfs_header_level(b);
1136 p->nodes[level] = b;
1137 ret = check_block(root, p, level);
1138 if (ret)
1139 return -1;
1140 ret = bin_search(b, key, level, &slot);
1141 if (level != 0) {
1142 if (ret && slot > 0)
1143 slot -= 1;
1144 p->slots[level] = slot;
1145 if ((p->search_for_split || ins_len > 0) &&
1146 btrfs_header_nritems(b) >=
1147 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1148 int sret = split_node(trans, root, p, level);
1149 BUG_ON(sret > 0);
1150 if (sret)
1151 return sret;
1152 b = p->nodes[level];
1153 slot = p->slots[level];
1154 } else if (ins_len < 0) {
1155 int sret = balance_level(trans, root, p,
1156 level);
1157 if (sret)
1158 return sret;
1159 b = p->nodes[level];
1160 if (!b) {
1161 btrfs_release_path(p);
1162 goto again;
1164 slot = p->slots[level];
1165 BUG_ON(btrfs_header_nritems(b) == 1);
1167 /* this is only true while dropping a snapshot */
1168 if (level == lowest_level)
1169 break;
1171 if (should_reada)
1172 reada_for_search(root, p, level, slot,
1173 key->objectid);
1175 b = read_node_slot(fs_info, b, slot);
1176 if (!extent_buffer_uptodate(b))
1177 return -EIO;
1178 } else {
1179 p->slots[level] = slot;
1180 if (ins_len > 0 &&
1181 ins_len > btrfs_leaf_free_space(root, b)) {
1182 int sret = split_leaf(trans, root, key,
1183 p, ins_len, ret == 0);
1184 BUG_ON(sret > 0);
1185 if (sret)
1186 return sret;
1188 return ret;
1191 return 1;
1195 * adjust the pointers going up the tree, starting at level
1196 * making sure the right key of each node is points to 'key'.
1197 * This is used after shifting pointers to the left, so it stops
1198 * fixing up pointers when a given leaf/node is not in slot 0 of the
1199 * higher levels
1201 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
1202 struct btrfs_disk_key *key, int level)
1204 int i;
1205 struct extent_buffer *t;
1207 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1208 int tslot = path->slots[i];
1209 if (!path->nodes[i])
1210 break;
1211 t = path->nodes[i];
1212 btrfs_set_node_key(t, key, tslot);
1213 btrfs_mark_buffer_dirty(path->nodes[i]);
1214 if (tslot != 0)
1215 break;
1220 * update item key.
1222 * This function isn't completely safe. It's the caller's responsibility
1223 * that the new key won't break the order
1225 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
1226 struct btrfs_key *new_key)
1228 struct btrfs_disk_key disk_key;
1229 struct extent_buffer *eb;
1230 int slot;
1232 eb = path->nodes[0];
1233 slot = path->slots[0];
1234 if (slot > 0) {
1235 btrfs_item_key(eb, &disk_key, slot - 1);
1236 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1237 return -1;
1239 if (slot < btrfs_header_nritems(eb) - 1) {
1240 btrfs_item_key(eb, &disk_key, slot + 1);
1241 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1242 return -1;
1245 btrfs_cpu_key_to_disk(&disk_key, new_key);
1246 btrfs_set_item_key(eb, &disk_key, slot);
1247 btrfs_mark_buffer_dirty(eb);
1248 if (slot == 0)
1249 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1250 return 0;
1254 * update an item key without the safety checks. This is meant to be called by
1255 * fsck only.
1257 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
1258 struct btrfs_path *path,
1259 struct btrfs_key *new_key)
1261 struct btrfs_disk_key disk_key;
1262 struct extent_buffer *eb;
1263 int slot;
1265 eb = path->nodes[0];
1266 slot = path->slots[0];
1268 btrfs_cpu_key_to_disk(&disk_key, new_key);
1269 btrfs_set_item_key(eb, &disk_key, slot);
1270 btrfs_mark_buffer_dirty(eb);
1271 if (slot == 0)
1272 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1276 * try to push data from one node into the next node left in the
1277 * tree.
1279 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1280 * error, and > 0 if there was no room in the left hand block.
1282 static int push_node_left(struct btrfs_trans_handle *trans,
1283 struct btrfs_root *root, struct extent_buffer *dst,
1284 struct extent_buffer *src, int empty)
1286 int push_items = 0;
1287 int src_nritems;
1288 int dst_nritems;
1289 int ret = 0;
1291 src_nritems = btrfs_header_nritems(src);
1292 dst_nritems = btrfs_header_nritems(dst);
1293 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1294 WARN_ON(btrfs_header_generation(src) != trans->transid);
1295 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1297 if (!empty && src_nritems <= 8)
1298 return 1;
1300 if (push_items <= 0) {
1301 return 1;
1304 if (empty) {
1305 push_items = min(src_nritems, push_items);
1306 if (push_items < src_nritems) {
1307 /* leave at least 8 pointers in the node if
1308 * we aren't going to empty it
1310 if (src_nritems - push_items < 8) {
1311 if (push_items <= 8)
1312 return 1;
1313 push_items -= 8;
1316 } else
1317 push_items = min(src_nritems - 8, push_items);
1319 copy_extent_buffer(dst, src,
1320 btrfs_node_key_ptr_offset(dst_nritems),
1321 btrfs_node_key_ptr_offset(0),
1322 push_items * sizeof(struct btrfs_key_ptr));
1324 if (push_items < src_nritems) {
1325 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1326 btrfs_node_key_ptr_offset(push_items),
1327 (src_nritems - push_items) *
1328 sizeof(struct btrfs_key_ptr));
1330 btrfs_set_header_nritems(src, src_nritems - push_items);
1331 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1332 btrfs_mark_buffer_dirty(src);
1333 btrfs_mark_buffer_dirty(dst);
1335 return ret;
1339 * try to push data from one node into the next node right in the
1340 * tree.
1342 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1343 * error, and > 0 if there was no room in the right hand block.
1345 * this will only push up to 1/2 the contents of the left node over
1347 static int balance_node_right(struct btrfs_trans_handle *trans,
1348 struct btrfs_root *root,
1349 struct extent_buffer *dst,
1350 struct extent_buffer *src)
1352 int push_items = 0;
1353 int max_push;
1354 int src_nritems;
1355 int dst_nritems;
1356 int ret = 0;
1358 WARN_ON(btrfs_header_generation(src) != trans->transid);
1359 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1361 src_nritems = btrfs_header_nritems(src);
1362 dst_nritems = btrfs_header_nritems(dst);
1363 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1364 if (push_items <= 0) {
1365 return 1;
1368 if (src_nritems < 4) {
1369 return 1;
1372 max_push = src_nritems / 2 + 1;
1373 /* don't try to empty the node */
1374 if (max_push >= src_nritems) {
1375 return 1;
1378 if (max_push < push_items)
1379 push_items = max_push;
1381 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1382 btrfs_node_key_ptr_offset(0),
1383 (dst_nritems) *
1384 sizeof(struct btrfs_key_ptr));
1386 copy_extent_buffer(dst, src,
1387 btrfs_node_key_ptr_offset(0),
1388 btrfs_node_key_ptr_offset(src_nritems - push_items),
1389 push_items * sizeof(struct btrfs_key_ptr));
1391 btrfs_set_header_nritems(src, src_nritems - push_items);
1392 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1394 btrfs_mark_buffer_dirty(src);
1395 btrfs_mark_buffer_dirty(dst);
1397 return ret;
1401 * helper function to insert a new root level in the tree.
1402 * A new node is allocated, and a single item is inserted to
1403 * point to the existing root
1405 * returns zero on success or < 0 on failure.
1407 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *root,
1409 struct btrfs_path *path, int level)
1411 u64 lower_gen;
1412 struct extent_buffer *lower;
1413 struct extent_buffer *c;
1414 struct extent_buffer *old;
1415 struct btrfs_disk_key lower_key;
1417 BUG_ON(path->nodes[level]);
1418 BUG_ON(path->nodes[level-1] != root->node);
1420 lower = path->nodes[level-1];
1421 if (level == 1)
1422 btrfs_item_key(lower, &lower_key, 0);
1423 else
1424 btrfs_node_key(lower, &lower_key, 0);
1426 c = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1427 root->root_key.objectid, &lower_key,
1428 level, root->node->start, 0);
1430 if (IS_ERR(c))
1431 return PTR_ERR(c);
1433 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1434 btrfs_set_header_nritems(c, 1);
1435 btrfs_set_header_level(c, level);
1436 btrfs_set_header_bytenr(c, c->start);
1437 btrfs_set_header_generation(c, trans->transid);
1438 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1439 btrfs_set_header_owner(c, root->root_key.objectid);
1441 write_extent_buffer(c, root->fs_info->fsid,
1442 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1444 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1445 btrfs_header_chunk_tree_uuid(c),
1446 BTRFS_UUID_SIZE);
1448 btrfs_set_node_key(c, &lower_key, 0);
1449 btrfs_set_node_blockptr(c, 0, lower->start);
1450 lower_gen = btrfs_header_generation(lower);
1451 WARN_ON(lower_gen != trans->transid);
1453 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1455 btrfs_mark_buffer_dirty(c);
1457 old = root->node;
1458 root->node = c;
1460 /* the super has an extra ref to root->node */
1461 free_extent_buffer(old);
1463 add_root_to_dirty_list(root);
1464 extent_buffer_get(c);
1465 path->nodes[level] = c;
1466 path->slots[level] = 0;
1467 return 0;
1471 * worker function to insert a single pointer in a node.
1472 * the node should have enough room for the pointer already
1474 * slot and level indicate where you want the key to go, and
1475 * blocknr is the block the key points to.
1477 * returns zero on success and < 0 on any error
1479 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1480 *root, struct btrfs_path *path, struct btrfs_disk_key
1481 *key, u64 bytenr, int slot, int level)
1483 struct extent_buffer *lower;
1484 int nritems;
1486 BUG_ON(!path->nodes[level]);
1487 lower = path->nodes[level];
1488 nritems = btrfs_header_nritems(lower);
1489 if (slot > nritems)
1490 BUG();
1491 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
1492 BUG();
1493 if (slot < nritems) {
1494 /* shift the items */
1495 memmove_extent_buffer(lower,
1496 btrfs_node_key_ptr_offset(slot + 1),
1497 btrfs_node_key_ptr_offset(slot),
1498 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1500 btrfs_set_node_key(lower, key, slot);
1501 btrfs_set_node_blockptr(lower, slot, bytenr);
1502 WARN_ON(trans->transid == 0);
1503 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1504 btrfs_set_header_nritems(lower, nritems + 1);
1505 btrfs_mark_buffer_dirty(lower);
1506 return 0;
1510 * split the node at the specified level in path in two.
1511 * The path is corrected to point to the appropriate node after the split
1513 * Before splitting this tries to make some room in the node by pushing
1514 * left and right, if either one works, it returns right away.
1516 * returns 0 on success and < 0 on failure
1518 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1519 *root, struct btrfs_path *path, int level)
1521 struct extent_buffer *c;
1522 struct extent_buffer *split;
1523 struct btrfs_disk_key disk_key;
1524 int mid;
1525 int ret;
1526 int wret;
1527 u32 c_nritems;
1529 c = path->nodes[level];
1530 WARN_ON(btrfs_header_generation(c) != trans->transid);
1531 if (c == root->node) {
1532 /* trying to split the root, lets make a new one */
1533 ret = insert_new_root(trans, root, path, level + 1);
1534 if (ret)
1535 return ret;
1536 } else {
1537 ret = push_nodes_for_insert(trans, root, path, level);
1538 c = path->nodes[level];
1539 if (!ret && btrfs_header_nritems(c) <
1540 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 3)
1541 return 0;
1542 if (ret < 0)
1543 return ret;
1546 c_nritems = btrfs_header_nritems(c);
1547 mid = (c_nritems + 1) / 2;
1548 btrfs_node_key(c, &disk_key, mid);
1550 split = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1551 root->root_key.objectid,
1552 &disk_key, level, c->start, 0);
1553 if (IS_ERR(split))
1554 return PTR_ERR(split);
1556 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1557 btrfs_set_header_level(split, btrfs_header_level(c));
1558 btrfs_set_header_bytenr(split, split->start);
1559 btrfs_set_header_generation(split, trans->transid);
1560 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1561 btrfs_set_header_owner(split, root->root_key.objectid);
1562 write_extent_buffer(split, root->fs_info->fsid,
1563 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1564 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1565 btrfs_header_chunk_tree_uuid(split),
1566 BTRFS_UUID_SIZE);
1569 copy_extent_buffer(split, c,
1570 btrfs_node_key_ptr_offset(0),
1571 btrfs_node_key_ptr_offset(mid),
1572 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1573 btrfs_set_header_nritems(split, c_nritems - mid);
1574 btrfs_set_header_nritems(c, mid);
1575 ret = 0;
1577 btrfs_mark_buffer_dirty(c);
1578 btrfs_mark_buffer_dirty(split);
1580 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1581 path->slots[level + 1] + 1,
1582 level + 1);
1583 if (wret)
1584 ret = wret;
1586 if (path->slots[level] >= mid) {
1587 path->slots[level] -= mid;
1588 free_extent_buffer(c);
1589 path->nodes[level] = split;
1590 path->slots[level + 1] += 1;
1591 } else {
1592 free_extent_buffer(split);
1594 return ret;
1598 * how many bytes are required to store the items in a leaf. start
1599 * and nr indicate which items in the leaf to check. This totals up the
1600 * space used both by the item structs and the item data
1602 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1604 int data_len;
1605 int nritems = btrfs_header_nritems(l);
1606 int end = min(nritems, start + nr) - 1;
1608 if (!nr)
1609 return 0;
1610 data_len = btrfs_item_end_nr(l, start);
1611 data_len = data_len - btrfs_item_offset_nr(l, end);
1612 data_len += sizeof(struct btrfs_item) * nr;
1613 WARN_ON(data_len < 0);
1614 return data_len;
1618 * The space between the end of the leaf items and
1619 * the start of the leaf data. IOW, how much room
1620 * the leaf has left for both items and data
1622 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1624 u32 nodesize = (root ? BTRFS_LEAF_DATA_SIZE(root->fs_info) : leaf->len);
1625 int nritems = btrfs_header_nritems(leaf);
1626 int ret;
1627 ret = nodesize - leaf_space_used(leaf, 0, nritems);
1628 if (ret < 0) {
1629 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1630 ret, nodesize, leaf_space_used(leaf, 0, nritems),
1631 nritems);
1633 return ret;
1637 * push some data in the path leaf to the right, trying to free up at
1638 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1640 * returns 1 if the push failed because the other node didn't have enough
1641 * room, 0 if everything worked out and < 0 if there were major errors.
1643 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1644 *root, struct btrfs_path *path, int data_size,
1645 int empty)
1647 struct extent_buffer *left = path->nodes[0];
1648 struct extent_buffer *right;
1649 struct extent_buffer *upper;
1650 struct btrfs_disk_key disk_key;
1651 struct btrfs_fs_info *fs_info = root->fs_info;
1652 int slot;
1653 u32 i;
1654 int free_space;
1655 int push_space = 0;
1656 int push_items = 0;
1657 struct btrfs_item *item;
1658 u32 left_nritems;
1659 u32 nr;
1660 u32 right_nritems;
1661 u32 data_end;
1662 u32 this_item_size;
1663 int ret;
1665 slot = path->slots[1];
1666 if (!path->nodes[1]) {
1667 return 1;
1669 upper = path->nodes[1];
1670 if (slot >= btrfs_header_nritems(upper) - 1)
1671 return 1;
1673 right = read_node_slot(fs_info, upper, slot + 1);
1674 if (!extent_buffer_uptodate(right)) {
1675 if (IS_ERR(right))
1676 return PTR_ERR(right);
1677 return -EIO;
1679 free_space = btrfs_leaf_free_space(root, right);
1680 if (free_space < data_size) {
1681 free_extent_buffer(right);
1682 return 1;
1685 /* cow and double check */
1686 ret = btrfs_cow_block(trans, root, right, upper,
1687 slot + 1, &right);
1688 if (ret) {
1689 free_extent_buffer(right);
1690 return 1;
1692 free_space = btrfs_leaf_free_space(root, right);
1693 if (free_space < data_size) {
1694 free_extent_buffer(right);
1695 return 1;
1698 left_nritems = btrfs_header_nritems(left);
1699 if (left_nritems == 0) {
1700 free_extent_buffer(right);
1701 return 1;
1704 if (empty)
1705 nr = 0;
1706 else
1707 nr = 1;
1709 i = left_nritems - 1;
1710 while (i >= nr) {
1711 item = btrfs_item_nr(i);
1713 if (path->slots[0] == i)
1714 push_space += data_size + sizeof(*item);
1716 this_item_size = btrfs_item_size(left, item);
1717 if (this_item_size + sizeof(*item) + push_space > free_space)
1718 break;
1719 push_items++;
1720 push_space += this_item_size + sizeof(*item);
1721 if (i == 0)
1722 break;
1723 i--;
1726 if (push_items == 0) {
1727 free_extent_buffer(right);
1728 return 1;
1731 if (!empty && push_items == left_nritems)
1732 WARN_ON(1);
1734 /* push left to right */
1735 right_nritems = btrfs_header_nritems(right);
1737 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1738 push_space -= leaf_data_end(fs_info, left);
1740 /* make room in the right data area */
1741 data_end = leaf_data_end(fs_info, right);
1742 memmove_extent_buffer(right,
1743 btrfs_leaf_data(right) + data_end - push_space,
1744 btrfs_leaf_data(right) + data_end,
1745 BTRFS_LEAF_DATA_SIZE(root->fs_info) - data_end);
1747 /* copy from the left data area */
1748 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1749 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space,
1750 btrfs_leaf_data(left) + leaf_data_end(fs_info, left),
1751 push_space);
1753 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1754 btrfs_item_nr_offset(0),
1755 right_nritems * sizeof(struct btrfs_item));
1757 /* copy the items from left to right */
1758 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1759 btrfs_item_nr_offset(left_nritems - push_items),
1760 push_items * sizeof(struct btrfs_item));
1762 /* update the item pointers */
1763 right_nritems += push_items;
1764 btrfs_set_header_nritems(right, right_nritems);
1765 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1766 for (i = 0; i < right_nritems; i++) {
1767 item = btrfs_item_nr(i);
1768 push_space -= btrfs_item_size(right, item);
1769 btrfs_set_item_offset(right, item, push_space);
1772 left_nritems -= push_items;
1773 btrfs_set_header_nritems(left, left_nritems);
1775 if (left_nritems)
1776 btrfs_mark_buffer_dirty(left);
1777 btrfs_mark_buffer_dirty(right);
1779 btrfs_item_key(right, &disk_key, 0);
1780 btrfs_set_node_key(upper, &disk_key, slot + 1);
1781 btrfs_mark_buffer_dirty(upper);
1783 /* then fixup the leaf pointer in the path */
1784 if (path->slots[0] >= left_nritems) {
1785 path->slots[0] -= left_nritems;
1786 free_extent_buffer(path->nodes[0]);
1787 path->nodes[0] = right;
1788 path->slots[1] += 1;
1789 } else {
1790 free_extent_buffer(right);
1792 return 0;
1795 * push some data in the path leaf to the left, trying to free up at
1796 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1798 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1799 *root, struct btrfs_path *path, int data_size,
1800 int empty)
1802 struct btrfs_disk_key disk_key;
1803 struct extent_buffer *right = path->nodes[0];
1804 struct extent_buffer *left;
1805 struct btrfs_fs_info *fs_info = root->fs_info;
1806 int slot;
1807 int i;
1808 int free_space;
1809 int push_space = 0;
1810 int push_items = 0;
1811 struct btrfs_item *item;
1812 u32 old_left_nritems;
1813 u32 right_nritems;
1814 u32 nr;
1815 int ret = 0;
1816 u32 this_item_size;
1817 u32 old_left_item_size;
1819 slot = path->slots[1];
1820 if (slot == 0)
1821 return 1;
1822 if (!path->nodes[1])
1823 return 1;
1825 right_nritems = btrfs_header_nritems(right);
1826 if (right_nritems == 0) {
1827 return 1;
1830 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
1831 free_space = btrfs_leaf_free_space(root, left);
1832 if (free_space < data_size) {
1833 free_extent_buffer(left);
1834 return 1;
1837 /* cow and double check */
1838 ret = btrfs_cow_block(trans, root, left,
1839 path->nodes[1], slot - 1, &left);
1840 if (ret) {
1841 /* we hit -ENOSPC, but it isn't fatal here */
1842 free_extent_buffer(left);
1843 return 1;
1846 free_space = btrfs_leaf_free_space(root, left);
1847 if (free_space < data_size) {
1848 free_extent_buffer(left);
1849 return 1;
1852 if (empty)
1853 nr = right_nritems;
1854 else
1855 nr = right_nritems - 1;
1857 for (i = 0; i < nr; i++) {
1858 item = btrfs_item_nr(i);
1860 if (path->slots[0] == i)
1861 push_space += data_size + sizeof(*item);
1863 this_item_size = btrfs_item_size(right, item);
1864 if (this_item_size + sizeof(*item) + push_space > free_space)
1865 break;
1867 push_items++;
1868 push_space += this_item_size + sizeof(*item);
1871 if (push_items == 0) {
1872 free_extent_buffer(left);
1873 return 1;
1875 if (!empty && push_items == btrfs_header_nritems(right))
1876 WARN_ON(1);
1878 /* push data from right to left */
1879 copy_extent_buffer(left, right,
1880 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1881 btrfs_item_nr_offset(0),
1882 push_items * sizeof(struct btrfs_item));
1884 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1885 btrfs_item_offset_nr(right, push_items -1);
1887 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1888 leaf_data_end(fs_info, left) - push_space,
1889 btrfs_leaf_data(right) +
1890 btrfs_item_offset_nr(right, push_items - 1),
1891 push_space);
1892 old_left_nritems = btrfs_header_nritems(left);
1893 BUG_ON(old_left_nritems == 0);
1895 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1896 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1897 u32 ioff;
1899 item = btrfs_item_nr(i);
1900 ioff = btrfs_item_offset(left, item);
1901 btrfs_set_item_offset(left, item,
1902 ioff - (BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1903 old_left_item_size));
1905 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1907 /* fixup right node */
1908 if (push_items > right_nritems) {
1909 printk("push items %d nr %u\n", push_items, right_nritems);
1910 WARN_ON(1);
1913 if (push_items < right_nritems) {
1914 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1915 leaf_data_end(fs_info, right);
1916 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1917 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1918 push_space,
1919 btrfs_leaf_data(right) +
1920 leaf_data_end(fs_info, right),
1921 push_space);
1923 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1924 btrfs_item_nr_offset(push_items),
1925 (btrfs_header_nritems(right) - push_items) *
1926 sizeof(struct btrfs_item));
1928 right_nritems -= push_items;
1929 btrfs_set_header_nritems(right, right_nritems);
1930 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1931 for (i = 0; i < right_nritems; i++) {
1932 item = btrfs_item_nr(i);
1933 push_space = push_space - btrfs_item_size(right, item);
1934 btrfs_set_item_offset(right, item, push_space);
1937 btrfs_mark_buffer_dirty(left);
1938 if (right_nritems)
1939 btrfs_mark_buffer_dirty(right);
1941 btrfs_item_key(right, &disk_key, 0);
1942 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1944 /* then fixup the leaf pointer in the path */
1945 if (path->slots[0] < push_items) {
1946 path->slots[0] += old_left_nritems;
1947 free_extent_buffer(path->nodes[0]);
1948 path->nodes[0] = left;
1949 path->slots[1] -= 1;
1950 } else {
1951 free_extent_buffer(left);
1952 path->slots[0] -= push_items;
1954 BUG_ON(path->slots[0] < 0);
1955 return ret;
1959 * split the path's leaf in two, making sure there is at least data_size
1960 * available for the resulting leaf level of the path.
1962 * returns 0 if all went well and < 0 on failure.
1964 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
1965 struct btrfs_root *root,
1966 struct btrfs_path *path,
1967 struct extent_buffer *l,
1968 struct extent_buffer *right,
1969 int slot, int mid, int nritems)
1971 int data_copy_size;
1972 int rt_data_off;
1973 int i;
1974 int ret = 0;
1975 int wret;
1976 struct btrfs_disk_key disk_key;
1978 nritems = nritems - mid;
1979 btrfs_set_header_nritems(right, nritems);
1980 data_copy_size = btrfs_item_end_nr(l, mid) -
1981 leaf_data_end(root->fs_info, l);
1983 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1984 btrfs_item_nr_offset(mid),
1985 nritems * sizeof(struct btrfs_item));
1987 copy_extent_buffer(right, l,
1988 btrfs_leaf_data(right) +
1989 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1990 data_copy_size, btrfs_leaf_data(l) +
1991 leaf_data_end(root->fs_info, l), data_copy_size);
1993 rt_data_off = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1994 btrfs_item_end_nr(l, mid);
1996 for (i = 0; i < nritems; i++) {
1997 struct btrfs_item *item = btrfs_item_nr(i);
1998 u32 ioff = btrfs_item_offset(right, item);
1999 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2002 btrfs_set_header_nritems(l, mid);
2003 ret = 0;
2004 btrfs_item_key(right, &disk_key, 0);
2005 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2006 path->slots[1] + 1, 1);
2007 if (wret)
2008 ret = wret;
2010 btrfs_mark_buffer_dirty(right);
2011 btrfs_mark_buffer_dirty(l);
2012 BUG_ON(path->slots[0] != slot);
2014 if (mid <= slot) {
2015 free_extent_buffer(path->nodes[0]);
2016 path->nodes[0] = right;
2017 path->slots[0] -= mid;
2018 path->slots[1] += 1;
2019 } else {
2020 free_extent_buffer(right);
2023 BUG_ON(path->slots[0] < 0);
2025 return ret;
2029 * split the path's leaf in two, making sure there is at least data_size
2030 * available for the resulting leaf level of the path.
2032 * returns 0 if all went well and < 0 on failure.
2034 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2035 struct btrfs_root *root,
2036 struct btrfs_key *ins_key,
2037 struct btrfs_path *path, int data_size,
2038 int extend)
2040 struct btrfs_disk_key disk_key;
2041 struct extent_buffer *l;
2042 u32 nritems;
2043 int mid;
2044 int slot;
2045 struct extent_buffer *right;
2046 int ret = 0;
2047 int wret;
2048 int split;
2049 int num_doubles = 0;
2051 l = path->nodes[0];
2052 slot = path->slots[0];
2053 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2054 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info))
2055 return -EOVERFLOW;
2057 /* first try to make some room by pushing left and right */
2058 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2059 wret = push_leaf_right(trans, root, path, data_size, 0);
2060 if (wret < 0)
2061 return wret;
2062 if (wret) {
2063 wret = push_leaf_left(trans, root, path, data_size, 0);
2064 if (wret < 0)
2065 return wret;
2067 l = path->nodes[0];
2069 /* did the pushes work? */
2070 if (btrfs_leaf_free_space(root, l) >= data_size)
2071 return 0;
2074 if (!path->nodes[1]) {
2075 ret = insert_new_root(trans, root, path, 1);
2076 if (ret)
2077 return ret;
2079 again:
2080 split = 1;
2081 l = path->nodes[0];
2082 slot = path->slots[0];
2083 nritems = btrfs_header_nritems(l);
2084 mid = (nritems + 1) / 2;
2086 if (mid <= slot) {
2087 if (nritems == 1 ||
2088 leaf_space_used(l, mid, nritems - mid) + data_size >
2089 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2090 if (slot >= nritems) {
2091 split = 0;
2092 } else {
2093 mid = slot;
2094 if (mid != nritems &&
2095 leaf_space_used(l, mid, nritems - mid) +
2096 data_size >
2097 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2098 split = 2;
2102 } else {
2103 if (leaf_space_used(l, 0, mid) + data_size >
2104 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2105 if (!extend && data_size && slot == 0) {
2106 split = 0;
2107 } else if ((extend || !data_size) && slot == 0) {
2108 mid = 1;
2109 } else {
2110 mid = slot;
2111 if (mid != nritems &&
2112 leaf_space_used(l, mid, nritems - mid) +
2113 data_size >
2114 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2115 split = 2 ;
2121 if (split == 0)
2122 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2123 else
2124 btrfs_item_key(l, &disk_key, mid);
2126 right = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
2127 root->root_key.objectid,
2128 &disk_key, 0, l->start, 0);
2129 if (IS_ERR(right)) {
2130 BUG_ON(1);
2131 return PTR_ERR(right);
2134 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2135 btrfs_set_header_bytenr(right, right->start);
2136 btrfs_set_header_generation(right, trans->transid);
2137 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2138 btrfs_set_header_owner(right, root->root_key.objectid);
2139 btrfs_set_header_level(right, 0);
2140 write_extent_buffer(right, root->fs_info->fsid,
2141 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2143 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2144 btrfs_header_chunk_tree_uuid(right),
2145 BTRFS_UUID_SIZE);
2147 if (split == 0) {
2148 if (mid <= slot) {
2149 btrfs_set_header_nritems(right, 0);
2150 wret = insert_ptr(trans, root, path,
2151 &disk_key, right->start,
2152 path->slots[1] + 1, 1);
2153 if (wret)
2154 ret = wret;
2156 free_extent_buffer(path->nodes[0]);
2157 path->nodes[0] = right;
2158 path->slots[0] = 0;
2159 path->slots[1] += 1;
2160 } else {
2161 btrfs_set_header_nritems(right, 0);
2162 wret = insert_ptr(trans, root, path,
2163 &disk_key,
2164 right->start,
2165 path->slots[1], 1);
2166 if (wret)
2167 ret = wret;
2168 free_extent_buffer(path->nodes[0]);
2169 path->nodes[0] = right;
2170 path->slots[0] = 0;
2171 if (path->slots[1] == 0) {
2172 btrfs_fixup_low_keys(root, path,
2173 &disk_key, 1);
2176 btrfs_mark_buffer_dirty(right);
2177 return ret;
2180 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2181 BUG_ON(ret);
2183 if (split == 2) {
2184 BUG_ON(num_doubles != 0);
2185 num_doubles++;
2186 goto again;
2189 return ret;
2193 * This function splits a single item into two items,
2194 * giving 'new_key' to the new item and splitting the
2195 * old one at split_offset (from the start of the item).
2197 * The path may be released by this operation. After
2198 * the split, the path is pointing to the old item. The
2199 * new item is going to be in the same node as the old one.
2201 * Note, the item being split must be smaller enough to live alone on
2202 * a tree block with room for one extra struct btrfs_item
2204 * This allows us to split the item in place, keeping a lock on the
2205 * leaf the entire time.
2207 int btrfs_split_item(struct btrfs_trans_handle *trans,
2208 struct btrfs_root *root,
2209 struct btrfs_path *path,
2210 struct btrfs_key *new_key,
2211 unsigned long split_offset)
2213 u32 item_size;
2214 struct extent_buffer *leaf;
2215 struct btrfs_key orig_key;
2216 struct btrfs_item *item;
2217 struct btrfs_item *new_item;
2218 int ret = 0;
2219 int slot;
2220 u32 nritems;
2221 u32 orig_offset;
2222 struct btrfs_disk_key disk_key;
2223 char *buf;
2225 leaf = path->nodes[0];
2226 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2227 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2228 goto split;
2230 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2231 btrfs_release_path(path);
2233 path->search_for_split = 1;
2235 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2236 path->search_for_split = 0;
2238 /* if our item isn't there or got smaller, return now */
2239 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2240 path->slots[0])) {
2241 return -EAGAIN;
2244 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2245 BUG_ON(ret);
2247 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2248 leaf = path->nodes[0];
2250 split:
2251 item = btrfs_item_nr(path->slots[0]);
2252 orig_offset = btrfs_item_offset(leaf, item);
2253 item_size = btrfs_item_size(leaf, item);
2256 buf = kmalloc(item_size, GFP_NOFS);
2257 BUG_ON(!buf);
2258 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2259 path->slots[0]), item_size);
2260 slot = path->slots[0] + 1;
2261 leaf = path->nodes[0];
2263 nritems = btrfs_header_nritems(leaf);
2265 if (slot < nritems) {
2266 /* shift the items */
2267 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2268 btrfs_item_nr_offset(slot),
2269 (nritems - slot) * sizeof(struct btrfs_item));
2273 btrfs_cpu_key_to_disk(&disk_key, new_key);
2274 btrfs_set_item_key(leaf, &disk_key, slot);
2276 new_item = btrfs_item_nr(slot);
2278 btrfs_set_item_offset(leaf, new_item, orig_offset);
2279 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2281 btrfs_set_item_offset(leaf, item,
2282 orig_offset + item_size - split_offset);
2283 btrfs_set_item_size(leaf, item, split_offset);
2285 btrfs_set_header_nritems(leaf, nritems + 1);
2287 /* write the data for the start of the original item */
2288 write_extent_buffer(leaf, buf,
2289 btrfs_item_ptr_offset(leaf, path->slots[0]),
2290 split_offset);
2292 /* write the data for the new item */
2293 write_extent_buffer(leaf, buf + split_offset,
2294 btrfs_item_ptr_offset(leaf, slot),
2295 item_size - split_offset);
2296 btrfs_mark_buffer_dirty(leaf);
2298 ret = 0;
2299 if (btrfs_leaf_free_space(root, leaf) < 0) {
2300 btrfs_print_leaf(root, leaf);
2301 BUG();
2303 kfree(buf);
2304 return ret;
2307 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2308 u32 new_size, int from_end)
2310 int ret = 0;
2311 int slot;
2312 struct extent_buffer *leaf;
2313 struct btrfs_item *item;
2314 u32 nritems;
2315 unsigned int data_end;
2316 unsigned int old_data_start;
2317 unsigned int old_size;
2318 unsigned int size_diff;
2319 int i;
2321 leaf = path->nodes[0];
2322 slot = path->slots[0];
2324 old_size = btrfs_item_size_nr(leaf, slot);
2325 if (old_size == new_size)
2326 return 0;
2328 nritems = btrfs_header_nritems(leaf);
2329 data_end = leaf_data_end(root->fs_info, leaf);
2331 old_data_start = btrfs_item_offset_nr(leaf, slot);
2333 size_diff = old_size - new_size;
2335 BUG_ON(slot < 0);
2336 BUG_ON(slot >= nritems);
2339 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2341 /* first correct the data pointers */
2342 for (i = slot; i < nritems; i++) {
2343 u32 ioff;
2344 item = btrfs_item_nr(i);
2345 ioff = btrfs_item_offset(leaf, item);
2346 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2349 /* shift the data */
2350 if (from_end) {
2351 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2352 data_end + size_diff, btrfs_leaf_data(leaf) +
2353 data_end, old_data_start + new_size - data_end);
2354 } else {
2355 struct btrfs_disk_key disk_key;
2356 u64 offset;
2358 btrfs_item_key(leaf, &disk_key, slot);
2360 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2361 unsigned long ptr;
2362 struct btrfs_file_extent_item *fi;
2364 fi = btrfs_item_ptr(leaf, slot,
2365 struct btrfs_file_extent_item);
2366 fi = (struct btrfs_file_extent_item *)(
2367 (unsigned long)fi - size_diff);
2369 if (btrfs_file_extent_type(leaf, fi) ==
2370 BTRFS_FILE_EXTENT_INLINE) {
2371 ptr = btrfs_item_ptr_offset(leaf, slot);
2372 memmove_extent_buffer(leaf, ptr,
2373 (unsigned long)fi,
2374 offsetof(struct btrfs_file_extent_item,
2375 disk_bytenr));
2379 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2380 data_end + size_diff, btrfs_leaf_data(leaf) +
2381 data_end, old_data_start - data_end);
2383 offset = btrfs_disk_key_offset(&disk_key);
2384 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2385 btrfs_set_item_key(leaf, &disk_key, slot);
2386 if (slot == 0)
2387 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2390 item = btrfs_item_nr(slot);
2391 btrfs_set_item_size(leaf, item, new_size);
2392 btrfs_mark_buffer_dirty(leaf);
2394 ret = 0;
2395 if (btrfs_leaf_free_space(root, leaf) < 0) {
2396 btrfs_print_leaf(root, leaf);
2397 BUG();
2399 return ret;
2402 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2403 u32 data_size)
2405 int ret = 0;
2406 int slot;
2407 struct extent_buffer *leaf;
2408 struct btrfs_item *item;
2409 u32 nritems;
2410 unsigned int data_end;
2411 unsigned int old_data;
2412 unsigned int old_size;
2413 int i;
2415 leaf = path->nodes[0];
2417 nritems = btrfs_header_nritems(leaf);
2418 data_end = leaf_data_end(root->fs_info, leaf);
2420 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2421 btrfs_print_leaf(root, leaf);
2422 BUG();
2424 slot = path->slots[0];
2425 old_data = btrfs_item_end_nr(leaf, slot);
2427 BUG_ON(slot < 0);
2428 if (slot >= nritems) {
2429 btrfs_print_leaf(root, leaf);
2430 printk("slot %d too large, nritems %d\n", slot, nritems);
2431 BUG_ON(1);
2435 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2437 /* first correct the data pointers */
2438 for (i = slot; i < nritems; i++) {
2439 u32 ioff;
2440 item = btrfs_item_nr(i);
2441 ioff = btrfs_item_offset(leaf, item);
2442 btrfs_set_item_offset(leaf, item, ioff - data_size);
2445 /* shift the data */
2446 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2447 data_end - data_size, btrfs_leaf_data(leaf) +
2448 data_end, old_data - data_end);
2450 data_end = old_data;
2451 old_size = btrfs_item_size_nr(leaf, slot);
2452 item = btrfs_item_nr(slot);
2453 btrfs_set_item_size(leaf, item, old_size + data_size);
2454 btrfs_mark_buffer_dirty(leaf);
2456 ret = 0;
2457 if (btrfs_leaf_free_space(root, leaf) < 0) {
2458 btrfs_print_leaf(root, leaf);
2459 BUG();
2461 return ret;
2465 * Given a key and some data, insert an item into the tree.
2466 * This does all the path init required, making room in the tree if needed.
2468 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2469 struct btrfs_root *root,
2470 struct btrfs_path *path,
2471 struct btrfs_key *cpu_key, u32 *data_size,
2472 int nr)
2474 struct extent_buffer *leaf;
2475 struct btrfs_item *item;
2476 int ret = 0;
2477 int slot;
2478 int i;
2479 u32 nritems;
2480 u32 total_size = 0;
2481 u32 total_data = 0;
2482 unsigned int data_end;
2483 struct btrfs_disk_key disk_key;
2485 for (i = 0; i < nr; i++) {
2486 total_data += data_size[i];
2489 /* create a root if there isn't one */
2490 if (!root->node)
2491 BUG();
2493 total_size = total_data + nr * sizeof(struct btrfs_item);
2494 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2495 if (ret == 0) {
2496 return -EEXIST;
2498 if (ret < 0)
2499 goto out;
2501 leaf = path->nodes[0];
2503 nritems = btrfs_header_nritems(leaf);
2504 data_end = leaf_data_end(root->fs_info, leaf);
2506 if (btrfs_leaf_free_space(root, leaf) < total_size) {
2507 btrfs_print_leaf(root, leaf);
2508 printk("not enough freespace need %u have %d\n",
2509 total_size, btrfs_leaf_free_space(root, leaf));
2510 BUG();
2513 slot = path->slots[0];
2514 BUG_ON(slot < 0);
2516 if (slot < nritems) {
2517 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2519 if (old_data < data_end) {
2520 btrfs_print_leaf(root, leaf);
2521 printk("slot %d old_data %d data_end %d\n",
2522 slot, old_data, data_end);
2523 BUG_ON(1);
2526 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2528 /* first correct the data pointers */
2529 for (i = slot; i < nritems; i++) {
2530 u32 ioff;
2532 item = btrfs_item_nr(i);
2533 ioff = btrfs_item_offset(leaf, item);
2534 btrfs_set_item_offset(leaf, item, ioff - total_data);
2537 /* shift the items */
2538 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2539 btrfs_item_nr_offset(slot),
2540 (nritems - slot) * sizeof(struct btrfs_item));
2542 /* shift the data */
2543 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2544 data_end - total_data, btrfs_leaf_data(leaf) +
2545 data_end, old_data - data_end);
2546 data_end = old_data;
2549 /* setup the item for the new data */
2550 for (i = 0; i < nr; i++) {
2551 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2552 btrfs_set_item_key(leaf, &disk_key, slot + i);
2553 item = btrfs_item_nr(slot + i);
2554 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2555 data_end -= data_size[i];
2556 btrfs_set_item_size(leaf, item, data_size[i]);
2558 btrfs_set_header_nritems(leaf, nritems + nr);
2559 btrfs_mark_buffer_dirty(leaf);
2561 ret = 0;
2562 if (slot == 0) {
2563 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2564 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2567 if (btrfs_leaf_free_space(root, leaf) < 0) {
2568 btrfs_print_leaf(root, leaf);
2569 BUG();
2572 out:
2573 return ret;
2577 * Given a key and some data, insert an item into the tree.
2578 * This does all the path init required, making room in the tree if needed.
2580 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2581 *root, struct btrfs_key *cpu_key, void *data, u32
2582 data_size)
2584 int ret = 0;
2585 struct btrfs_path *path;
2586 struct extent_buffer *leaf;
2587 unsigned long ptr;
2589 path = btrfs_alloc_path();
2590 if (!path)
2591 return -ENOMEM;
2593 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2594 if (!ret) {
2595 leaf = path->nodes[0];
2596 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2597 write_extent_buffer(leaf, data, ptr, data_size);
2598 btrfs_mark_buffer_dirty(leaf);
2600 btrfs_free_path(path);
2601 return ret;
2605 * delete the pointer from a given node.
2607 * If the delete empties a node, the node is removed from the tree,
2608 * continuing all the way the root if required. The root is converted into
2609 * a leaf if all the nodes are emptied.
2611 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2612 int level, int slot)
2614 struct extent_buffer *parent = path->nodes[level];
2615 u32 nritems;
2616 int ret = 0;
2618 nritems = btrfs_header_nritems(parent);
2619 if (slot < nritems - 1) {
2620 /* shift the items */
2621 memmove_extent_buffer(parent,
2622 btrfs_node_key_ptr_offset(slot),
2623 btrfs_node_key_ptr_offset(slot + 1),
2624 sizeof(struct btrfs_key_ptr) *
2625 (nritems - slot - 1));
2627 nritems--;
2628 btrfs_set_header_nritems(parent, nritems);
2629 if (nritems == 0 && parent == root->node) {
2630 BUG_ON(btrfs_header_level(root->node) != 1);
2631 /* just turn the root into a leaf and break */
2632 btrfs_set_header_level(root->node, 0);
2633 } else if (slot == 0) {
2634 struct btrfs_disk_key disk_key;
2636 btrfs_node_key(parent, &disk_key, 0);
2637 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2639 btrfs_mark_buffer_dirty(parent);
2640 return ret;
2644 * a helper function to delete the leaf pointed to by path->slots[1] and
2645 * path->nodes[1].
2647 * This deletes the pointer in path->nodes[1] and frees the leaf
2648 * block extent. zero is returned if it all worked out, < 0 otherwise.
2650 * The path must have already been setup for deleting the leaf, including
2651 * all the proper balancing. path->nodes[1] must be locked.
2653 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2654 struct btrfs_root *root,
2655 struct btrfs_path *path,
2656 struct extent_buffer *leaf)
2658 int ret;
2660 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2661 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2662 if (ret)
2663 return ret;
2665 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2666 0, root->root_key.objectid, 0, 0);
2667 return ret;
2671 * delete the item at the leaf level in path. If that empties
2672 * the leaf, remove it from the tree
2674 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2675 struct btrfs_path *path, int slot, int nr)
2677 struct extent_buffer *leaf;
2678 struct btrfs_item *item;
2679 int last_off;
2680 int dsize = 0;
2681 int ret = 0;
2682 int wret;
2683 int i;
2684 u32 nritems;
2686 leaf = path->nodes[0];
2687 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2689 for (i = 0; i < nr; i++)
2690 dsize += btrfs_item_size_nr(leaf, slot + i);
2692 nritems = btrfs_header_nritems(leaf);
2694 if (slot + nr != nritems) {
2695 int data_end = leaf_data_end(root->fs_info, leaf);
2697 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2698 data_end + dsize,
2699 btrfs_leaf_data(leaf) + data_end,
2700 last_off - data_end);
2702 for (i = slot + nr; i < nritems; i++) {
2703 u32 ioff;
2705 item = btrfs_item_nr(i);
2706 ioff = btrfs_item_offset(leaf, item);
2707 btrfs_set_item_offset(leaf, item, ioff + dsize);
2710 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2711 btrfs_item_nr_offset(slot + nr),
2712 sizeof(struct btrfs_item) *
2713 (nritems - slot - nr));
2715 btrfs_set_header_nritems(leaf, nritems - nr);
2716 nritems -= nr;
2718 /* delete the leaf if we've emptied it */
2719 if (nritems == 0) {
2720 if (leaf == root->node) {
2721 btrfs_set_header_level(leaf, 0);
2722 } else {
2723 clean_tree_block(trans, root, leaf);
2724 wret = btrfs_del_leaf(trans, root, path, leaf);
2725 BUG_ON(ret);
2726 if (wret)
2727 ret = wret;
2729 } else {
2730 int used = leaf_space_used(leaf, 0, nritems);
2731 if (slot == 0) {
2732 struct btrfs_disk_key disk_key;
2734 btrfs_item_key(leaf, &disk_key, 0);
2735 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2738 /* delete the leaf if it is mostly empty */
2739 if (used < BTRFS_LEAF_DATA_SIZE(root->fs_info) / 4) {
2740 /* push_leaf_left fixes the path.
2741 * make sure the path still points to our leaf
2742 * for possible call to del_ptr below
2744 slot = path->slots[1];
2745 extent_buffer_get(leaf);
2747 wret = push_leaf_left(trans, root, path, 1, 1);
2748 if (wret < 0 && wret != -ENOSPC)
2749 ret = wret;
2751 if (path->nodes[0] == leaf &&
2752 btrfs_header_nritems(leaf)) {
2753 wret = push_leaf_right(trans, root, path, 1, 1);
2754 if (wret < 0 && wret != -ENOSPC)
2755 ret = wret;
2758 if (btrfs_header_nritems(leaf) == 0) {
2759 clean_tree_block(trans, root, leaf);
2760 path->slots[1] = slot;
2761 ret = btrfs_del_leaf(trans, root, path, leaf);
2762 BUG_ON(ret);
2763 free_extent_buffer(leaf);
2765 } else {
2766 btrfs_mark_buffer_dirty(leaf);
2767 free_extent_buffer(leaf);
2769 } else {
2770 btrfs_mark_buffer_dirty(leaf);
2773 return ret;
2777 * walk up the tree as far as required to find the previous leaf.
2778 * returns 0 if it found something or 1 if there are no lesser leaves.
2779 * returns < 0 on io errors.
2781 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2783 int slot;
2784 int level = 1;
2785 struct extent_buffer *c;
2786 struct extent_buffer *next = NULL;
2787 struct btrfs_fs_info *fs_info = root->fs_info;
2789 while(level < BTRFS_MAX_LEVEL) {
2790 if (!path->nodes[level])
2791 return 1;
2793 slot = path->slots[level];
2794 c = path->nodes[level];
2795 if (slot == 0) {
2796 level++;
2797 if (level == BTRFS_MAX_LEVEL)
2798 return 1;
2799 continue;
2801 slot--;
2803 next = read_node_slot(fs_info, c, slot);
2804 if (!extent_buffer_uptodate(next)) {
2805 if (IS_ERR(next))
2806 return PTR_ERR(next);
2807 return -EIO;
2809 break;
2811 path->slots[level] = slot;
2812 while(1) {
2813 level--;
2814 c = path->nodes[level];
2815 free_extent_buffer(c);
2816 slot = btrfs_header_nritems(next);
2817 if (slot != 0)
2818 slot--;
2819 path->nodes[level] = next;
2820 path->slots[level] = slot;
2821 if (!level)
2822 break;
2823 next = read_node_slot(fs_info, next, slot);
2824 if (!extent_buffer_uptodate(next)) {
2825 if (IS_ERR(next))
2826 return PTR_ERR(next);
2827 return -EIO;
2830 return 0;
2834 * walk up the tree as far as required to find the next leaf.
2835 * returns 0 if it found something or 1 if there are no greater leaves.
2836 * returns < 0 on io errors.
2838 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2840 int slot;
2841 int level = 1;
2842 struct extent_buffer *c;
2843 struct extent_buffer *next = NULL;
2844 struct btrfs_fs_info *fs_info = root->fs_info;
2846 while(level < BTRFS_MAX_LEVEL) {
2847 if (!path->nodes[level])
2848 return 1;
2850 slot = path->slots[level] + 1;
2851 c = path->nodes[level];
2852 if (slot >= btrfs_header_nritems(c)) {
2853 level++;
2854 if (level == BTRFS_MAX_LEVEL)
2855 return 1;
2856 continue;
2859 if (path->reada)
2860 reada_for_search(root, path, level, slot, 0);
2862 next = read_node_slot(fs_info, c, slot);
2863 if (!extent_buffer_uptodate(next))
2864 return -EIO;
2865 break;
2867 path->slots[level] = slot;
2868 while(1) {
2869 level--;
2870 c = path->nodes[level];
2871 free_extent_buffer(c);
2872 path->nodes[level] = next;
2873 path->slots[level] = 0;
2874 if (!level)
2875 break;
2876 if (path->reada)
2877 reada_for_search(root, path, level, 0, 0);
2878 next = read_node_slot(fs_info, next, 0);
2879 if (!extent_buffer_uptodate(next))
2880 return -EIO;
2882 return 0;
2885 int btrfs_previous_item(struct btrfs_root *root,
2886 struct btrfs_path *path, u64 min_objectid,
2887 int type)
2889 struct btrfs_key found_key;
2890 struct extent_buffer *leaf;
2891 u32 nritems;
2892 int ret;
2894 while(1) {
2895 if (path->slots[0] == 0) {
2896 ret = btrfs_prev_leaf(root, path);
2897 if (ret != 0)
2898 return ret;
2899 } else {
2900 path->slots[0]--;
2902 leaf = path->nodes[0];
2903 nritems = btrfs_header_nritems(leaf);
2904 if (nritems == 0)
2905 return 1;
2906 if (path->slots[0] == nritems)
2907 path->slots[0]--;
2909 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2910 if (found_key.objectid < min_objectid)
2911 break;
2912 if (found_key.type == type)
2913 return 0;
2914 if (found_key.objectid == min_objectid &&
2915 found_key.type < type)
2916 break;
2918 return 1;
2922 * search in extent tree to find a previous Metadata/Data extent item with
2923 * min objecitd.
2925 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2927 int btrfs_previous_extent_item(struct btrfs_root *root,
2928 struct btrfs_path *path, u64 min_objectid)
2930 struct btrfs_key found_key;
2931 struct extent_buffer *leaf;
2932 u32 nritems;
2933 int ret;
2935 while (1) {
2936 if (path->slots[0] == 0) {
2937 ret = btrfs_prev_leaf(root, path);
2938 if (ret != 0)
2939 return ret;
2940 } else {
2941 path->slots[0]--;
2943 leaf = path->nodes[0];
2944 nritems = btrfs_header_nritems(leaf);
2945 if (nritems == 0)
2946 return 1;
2947 if (path->slots[0] == nritems)
2948 path->slots[0]--;
2950 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2951 if (found_key.objectid < min_objectid)
2952 break;
2953 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2954 found_key.type == BTRFS_METADATA_ITEM_KEY)
2955 return 0;
2956 if (found_key.objectid == min_objectid &&
2957 found_key.type < BTRFS_EXTENT_ITEM_KEY)
2958 break;
2960 return 1;
2964 * Search in extent tree to found next meta/data extent
2965 * Caller needs to check for no-hole or skinny metadata features.
2967 int btrfs_next_extent_item(struct btrfs_root *root,
2968 struct btrfs_path *path, u64 max_objectid)
2970 struct btrfs_key found_key;
2971 int ret;
2973 while (1) {
2974 ret = btrfs_next_item(root, path);
2975 if (ret)
2976 return ret;
2977 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2978 path->slots[0]);
2979 if (found_key.objectid > max_objectid)
2980 return 1;
2981 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2982 found_key.type == BTRFS_METADATA_ITEM_KEY)
2983 return 0;