btrfs-progs: remove stale dir-test
[btrfs-progs-unstable/devel.git] / ctree.c
blob2c51580fec65ca0f6954a8a9399aa35a0c3c3945
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"
25 #include "messages.h"
27 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30 *root, struct btrfs_key *ins_key,
31 struct btrfs_path *path, int data_size, int extend);
32 static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
34 struct extent_buffer *src, int empty);
35 static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
40 inline void btrfs_init_path(struct btrfs_path *p)
42 memset(p, 0, sizeof(*p));
45 struct btrfs_path *btrfs_alloc_path(void)
47 struct btrfs_path *path;
48 path = kzalloc(sizeof(struct btrfs_path), GFP_NOFS);
49 return path;
52 void btrfs_free_path(struct btrfs_path *p)
54 if (!p)
55 return;
56 btrfs_release_path(p);
57 kfree(p);
60 void btrfs_release_path(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 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 btrfs_header_fsid(), BTRFS_FSID_SIZE);
128 WARN_ON(btrfs_header_generation(buf) > trans->transid);
129 ret = btrfs_inc_ref(trans, new_root, cow, 0);
130 kfree(new_root);
132 if (ret)
133 return ret;
135 btrfs_mark_buffer_dirty(cow);
136 *cow_ret = cow;
137 return 0;
141 * check if the tree block can be shared by multiple trees
143 static int btrfs_block_can_be_shared(struct btrfs_root *root,
144 struct extent_buffer *buf)
147 * Tree blocks not in reference counted trees and tree roots
148 * are never shared. If a block was allocated after the last
149 * snapshot and the block was not allocated by tree relocation,
150 * we know the block is not shared.
152 if (root->ref_cows &&
153 buf != root->node && buf != root->commit_root &&
154 (btrfs_header_generation(buf) <=
155 btrfs_root_last_snapshot(&root->root_item) ||
156 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
157 return 1;
158 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
159 if (root->ref_cows &&
160 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
161 return 1;
162 #endif
163 return 0;
166 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
167 struct btrfs_root *root,
168 struct extent_buffer *buf,
169 struct extent_buffer *cow)
171 u64 refs;
172 u64 owner;
173 u64 flags;
174 u64 new_flags = 0;
175 int ret;
178 * Backrefs update rules:
180 * Always use full backrefs for extent pointers in tree block
181 * allocated by tree relocation.
183 * If a shared tree block is no longer referenced by its owner
184 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
185 * use full backrefs for extent pointers in tree block.
187 * If a tree block is been relocating
188 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
189 * use full backrefs for extent pointers in tree block.
190 * The reason for this is some operations (such as drop tree)
191 * are only allowed for blocks use full backrefs.
194 if (btrfs_block_can_be_shared(root, buf)) {
195 ret = btrfs_lookup_extent_info(trans, trans->fs_info,
196 buf->start,
197 btrfs_header_level(buf), 1,
198 &refs, &flags);
199 BUG_ON(ret);
200 BUG_ON(refs == 0);
201 } else {
202 refs = 1;
203 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
204 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
205 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
206 else
207 flags = 0;
210 owner = btrfs_header_owner(buf);
211 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
212 owner == BTRFS_TREE_RELOC_OBJECTID);
214 if (refs > 1) {
215 if ((owner == root->root_key.objectid ||
216 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
217 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
218 ret = btrfs_inc_ref(trans, root, buf, 1);
219 BUG_ON(ret);
221 if (root->root_key.objectid ==
222 BTRFS_TREE_RELOC_OBJECTID) {
223 ret = btrfs_dec_ref(trans, root, buf, 0);
224 BUG_ON(ret);
225 ret = btrfs_inc_ref(trans, root, cow, 1);
226 BUG_ON(ret);
228 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
229 } else {
231 if (root->root_key.objectid ==
232 BTRFS_TREE_RELOC_OBJECTID)
233 ret = btrfs_inc_ref(trans, root, cow, 1);
234 else
235 ret = btrfs_inc_ref(trans, root, cow, 0);
236 BUG_ON(ret);
238 if (new_flags != 0) {
239 ret = btrfs_set_block_flags(trans, buf->start,
240 btrfs_header_level(buf),
241 new_flags);
242 BUG_ON(ret);
244 } else {
245 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
246 if (root->root_key.objectid ==
247 BTRFS_TREE_RELOC_OBJECTID)
248 ret = btrfs_inc_ref(trans, root, cow, 1);
249 else
250 ret = btrfs_inc_ref(trans, root, cow, 0);
251 BUG_ON(ret);
252 ret = btrfs_dec_ref(trans, root, buf, 1);
253 BUG_ON(ret);
255 clean_tree_block(buf);
257 return 0;
260 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
261 struct btrfs_root *root,
262 struct extent_buffer *buf,
263 struct extent_buffer *parent, int parent_slot,
264 struct extent_buffer **cow_ret,
265 u64 search_start, u64 empty_size)
267 struct extent_buffer *cow;
268 struct btrfs_disk_key disk_key;
269 int level;
271 WARN_ON(root->ref_cows && trans->transid !=
272 root->fs_info->running_transaction->transid);
273 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
275 level = btrfs_header_level(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 btrfs_header_fsid(), BTRFS_FSID_SIZE);
302 WARN_ON(!(buf->flags & EXTENT_BAD_TRANSID) &&
303 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 if (!list_empty(&buf->recow)) {
328 list_del_init(&buf->recow);
329 free_extent_buffer(buf);
331 free_extent_buffer(buf);
332 btrfs_mark_buffer_dirty(cow);
333 *cow_ret = cow;
334 return 0;
337 static inline int should_cow_block(struct btrfs_trans_handle *trans,
338 struct btrfs_root *root,
339 struct extent_buffer *buf)
341 if (btrfs_header_generation(buf) == trans->transid &&
342 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
343 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
344 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
345 return 0;
346 return 1;
349 int btrfs_cow_block(struct btrfs_trans_handle *trans,
350 struct btrfs_root *root, struct extent_buffer *buf,
351 struct extent_buffer *parent, int parent_slot,
352 struct extent_buffer **cow_ret)
354 u64 search_start;
355 int ret;
357 if (trans->transaction != root->fs_info->running_transaction) {
358 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
359 root->fs_info->running_transaction->transid);
360 WARN_ON(1);
363 if (trans->transid != root->fs_info->generation) {
364 printk(KERN_CRIT "trans %llu running %llu\n",
365 (unsigned long long)trans->transid,
366 (unsigned long long)root->fs_info->generation);
367 WARN_ON(1);
369 if (!should_cow_block(trans, root, buf)) {
370 *cow_ret = buf;
371 return 0;
374 search_start = buf->start & ~((u64)SZ_1G - 1);
375 ret = __btrfs_cow_block(trans, root, buf, parent,
376 parent_slot, cow_ret, search_start, 0);
377 return ret;
380 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
382 if (k1->objectid > k2->objectid)
383 return 1;
384 if (k1->objectid < k2->objectid)
385 return -1;
386 if (k1->type > k2->type)
387 return 1;
388 if (k1->type < k2->type)
389 return -1;
390 if (k1->offset > k2->offset)
391 return 1;
392 if (k1->offset < k2->offset)
393 return -1;
394 return 0;
398 * compare two keys in a memcmp fashion
400 static int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
402 struct btrfs_key k1;
404 btrfs_disk_key_to_cpu(&k1, disk);
405 return btrfs_comp_cpu_keys(&k1, k2);
409 * The leaf data grows from end-to-front in the node.
410 * this returns the address of the start of the last item,
411 * which is the stop of the leaf data stack
413 static inline unsigned int leaf_data_end(const struct btrfs_fs_info *fs_info,
414 const struct extent_buffer *leaf)
416 u32 nr = btrfs_header_nritems(leaf);
417 if (nr == 0)
418 return BTRFS_LEAF_DATA_SIZE(fs_info);
419 return btrfs_item_offset_nr(leaf, nr - 1);
422 enum btrfs_tree_block_status
423 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
424 struct extent_buffer *buf)
426 int i;
427 struct btrfs_key cpukey;
428 struct btrfs_disk_key key;
429 u32 nritems = btrfs_header_nritems(buf);
430 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
432 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
433 goto fail;
435 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
436 if (parent_key && parent_key->type) {
437 btrfs_node_key(buf, &key, 0);
438 if (memcmp(parent_key, &key, sizeof(key)))
439 goto fail;
441 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
442 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
443 btrfs_node_key(buf, &key, i);
444 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
445 if (btrfs_comp_keys(&key, &cpukey) >= 0)
446 goto fail;
448 return BTRFS_TREE_BLOCK_CLEAN;
449 fail:
450 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
451 if (parent_key)
452 btrfs_disk_key_to_cpu(&cpukey, parent_key);
453 else
454 btrfs_node_key_to_cpu(buf, &cpukey, 0);
455 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
456 buf->start, buf->len,
457 btrfs_header_level(buf));
459 return ret;
462 enum btrfs_tree_block_status
463 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
464 struct extent_buffer *buf)
466 int i;
467 struct btrfs_key cpukey;
468 struct btrfs_disk_key key;
469 u32 nritems = btrfs_header_nritems(buf);
470 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
472 if (nritems * sizeof(struct btrfs_item) > buf->len) {
473 fprintf(stderr, "invalid number of items %llu\n",
474 (unsigned long long)buf->start);
475 goto fail;
478 if (btrfs_header_level(buf) != 0) {
479 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
480 fprintf(stderr, "leaf is not a leaf %llu\n",
481 (unsigned long long)btrfs_header_bytenr(buf));
482 goto fail;
484 if (btrfs_leaf_free_space(buf) < 0) {
485 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
486 fprintf(stderr, "leaf free space incorrect %llu %d\n",
487 (unsigned long long)btrfs_header_bytenr(buf),
488 btrfs_leaf_free_space(buf));
489 goto fail;
492 if (nritems == 0)
493 return BTRFS_TREE_BLOCK_CLEAN;
495 btrfs_item_key(buf, &key, 0);
496 if (parent_key && parent_key->type &&
497 memcmp(parent_key, &key, sizeof(key))) {
498 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
499 fprintf(stderr, "leaf parent key incorrect %llu\n",
500 (unsigned long long)btrfs_header_bytenr(buf));
501 goto fail;
503 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
504 btrfs_item_key(buf, &key, i);
505 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
506 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
507 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
508 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
509 goto fail;
511 if (btrfs_item_offset_nr(buf, i) !=
512 btrfs_item_end_nr(buf, i + 1)) {
513 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
514 fprintf(stderr, "incorrect offsets %u %u\n",
515 btrfs_item_offset_nr(buf, i),
516 btrfs_item_end_nr(buf, i + 1));
517 goto fail;
519 if (i == 0 && btrfs_item_end_nr(buf, i) !=
520 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
521 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
522 fprintf(stderr, "bad item end %u wanted %u\n",
523 btrfs_item_end_nr(buf, i),
524 (unsigned)BTRFS_LEAF_DATA_SIZE(root->fs_info));
525 goto fail;
529 for (i = 0; i < nritems; i++) {
530 if (btrfs_item_end_nr(buf, i) >
531 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
532 btrfs_item_key(buf, &key, 0);
533 btrfs_print_key(&key);
534 fflush(stdout);
535 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
536 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
537 (unsigned long long)btrfs_item_end_nr(buf, i),
538 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
539 root->fs_info));
540 goto fail;
544 return BTRFS_TREE_BLOCK_CLEAN;
545 fail:
546 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
547 if (parent_key)
548 btrfs_disk_key_to_cpu(&cpukey, parent_key);
549 else
550 btrfs_item_key_to_cpu(buf, &cpukey, 0);
552 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
553 buf->start, buf->len, 0);
555 return ret;
558 static int noinline check_block(struct btrfs_root *root,
559 struct btrfs_path *path, int level)
561 struct btrfs_disk_key key;
562 struct btrfs_disk_key *key_ptr = NULL;
563 struct extent_buffer *parent;
564 enum btrfs_tree_block_status ret;
566 if (path->skip_check_block)
567 return 0;
568 if (path->nodes[level + 1]) {
569 parent = path->nodes[level + 1];
570 btrfs_node_key(parent, &key, path->slots[level + 1]);
571 key_ptr = &key;
573 if (level == 0)
574 ret = btrfs_check_leaf(root, key_ptr, path->nodes[0]);
575 else
576 ret = btrfs_check_node(root, key_ptr, path->nodes[level]);
577 if (ret == BTRFS_TREE_BLOCK_CLEAN)
578 return 0;
579 return -EIO;
583 * search for key in the extent_buffer. The items start at offset p,
584 * and they are item_size apart. There are 'max' items in p.
586 * the slot in the array is returned via slot, and it points to
587 * the place where you would insert key if it is not found in
588 * the array.
590 * slot may point to max if the key is bigger than all of the keys
592 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
593 int item_size, struct btrfs_key *key,
594 int max, int *slot)
596 int low = 0;
597 int high = max;
598 int mid;
599 int ret;
600 unsigned long offset;
601 struct btrfs_disk_key *tmp;
603 while(low < high) {
604 mid = (low + high) / 2;
605 offset = p + mid * item_size;
607 tmp = (struct btrfs_disk_key *)(eb->data + offset);
608 ret = btrfs_comp_keys(tmp, key);
610 if (ret < 0)
611 low = mid + 1;
612 else if (ret > 0)
613 high = mid;
614 else {
615 *slot = mid;
616 return 0;
619 *slot = low;
620 return 1;
624 * simple bin_search frontend that does the right thing for
625 * leaves vs nodes
627 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
628 int level, int *slot)
630 if (level == 0)
631 return generic_bin_search(eb,
632 offsetof(struct btrfs_leaf, items),
633 sizeof(struct btrfs_item),
634 key, btrfs_header_nritems(eb),
635 slot);
636 else
637 return generic_bin_search(eb,
638 offsetof(struct btrfs_node, ptrs),
639 sizeof(struct btrfs_key_ptr),
640 key, btrfs_header_nritems(eb),
641 slot);
644 struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
645 struct extent_buffer *parent, int slot)
647 struct extent_buffer *ret;
648 int level = btrfs_header_level(parent);
650 if (slot < 0)
651 return NULL;
652 if (slot >= btrfs_header_nritems(parent))
653 return NULL;
655 if (level == 0)
656 return NULL;
658 ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
659 btrfs_node_ptr_generation(parent, slot));
660 if (!extent_buffer_uptodate(ret))
661 return ERR_PTR(-EIO);
663 if (btrfs_header_level(ret) != level - 1) {
664 error(
665 "child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
666 btrfs_header_bytenr(parent), slot,
667 btrfs_header_level(parent), btrfs_header_level(ret));
668 free_extent_buffer(ret);
669 return ERR_PTR(-EIO);
671 return ret;
674 static int balance_level(struct btrfs_trans_handle *trans,
675 struct btrfs_root *root,
676 struct btrfs_path *path, int level)
678 struct extent_buffer *right = NULL;
679 struct extent_buffer *mid;
680 struct extent_buffer *left = NULL;
681 struct extent_buffer *parent = NULL;
682 struct btrfs_fs_info *fs_info = root->fs_info;
683 int ret = 0;
684 int wret;
685 int pslot;
686 int orig_slot = path->slots[level];
687 u64 orig_ptr;
689 if (level == 0)
690 return 0;
692 mid = path->nodes[level];
693 WARN_ON(btrfs_header_generation(mid) != trans->transid);
695 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
697 if (level < BTRFS_MAX_LEVEL - 1) {
698 parent = path->nodes[level + 1];
699 pslot = path->slots[level + 1];
703 * deal with the case where there is only one pointer in the root
704 * by promoting the node below to a root
706 if (!parent) {
707 struct extent_buffer *child;
709 if (btrfs_header_nritems(mid) != 1)
710 return 0;
712 /* promote the child to a root */
713 child = read_node_slot(fs_info, mid, 0);
714 BUG_ON(!extent_buffer_uptodate(child));
715 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
716 BUG_ON(ret);
718 root->node = child;
719 add_root_to_dirty_list(root);
720 path->nodes[level] = NULL;
721 clean_tree_block(mid);
722 /* once for the path */
723 free_extent_buffer(mid);
725 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
726 0, root->root_key.objectid,
727 level, 1);
728 /* once for the root ptr */
729 free_extent_buffer(mid);
730 return ret;
732 if (btrfs_header_nritems(mid) >
733 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
734 return 0;
736 left = read_node_slot(fs_info, parent, pslot - 1);
737 if (extent_buffer_uptodate(left)) {
738 wret = btrfs_cow_block(trans, root, left,
739 parent, pslot - 1, &left);
740 if (wret) {
741 ret = wret;
742 goto enospc;
745 right = read_node_slot(fs_info, parent, pslot + 1);
746 if (extent_buffer_uptodate(right)) {
747 wret = btrfs_cow_block(trans, root, right,
748 parent, pslot + 1, &right);
749 if (wret) {
750 ret = wret;
751 goto enospc;
755 /* first, try to make some room in the middle buffer */
756 if (left) {
757 orig_slot += btrfs_header_nritems(left);
758 wret = push_node_left(trans, root, left, mid, 1);
759 if (wret < 0)
760 ret = wret;
764 * then try to empty the right most buffer into the middle
766 if (right) {
767 wret = push_node_left(trans, root, mid, right, 1);
768 if (wret < 0 && wret != -ENOSPC)
769 ret = wret;
770 if (btrfs_header_nritems(right) == 0) {
771 u64 bytenr = right->start;
772 u32 blocksize = right->len;
774 clean_tree_block(right);
775 free_extent_buffer(right);
776 right = NULL;
777 wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
778 if (wret)
779 ret = wret;
780 wret = btrfs_free_extent(trans, root, bytenr,
781 blocksize, 0,
782 root->root_key.objectid,
783 level, 0);
784 if (wret)
785 ret = wret;
786 } else {
787 struct btrfs_disk_key right_key;
788 btrfs_node_key(right, &right_key, 0);
789 btrfs_set_node_key(parent, &right_key, pslot + 1);
790 btrfs_mark_buffer_dirty(parent);
793 if (btrfs_header_nritems(mid) == 1) {
795 * we're not allowed to leave a node with one item in the
796 * tree during a delete. A deletion from lower in the tree
797 * could try to delete the only pointer in this node.
798 * So, pull some keys from the left.
799 * There has to be a left pointer at this point because
800 * otherwise we would have pulled some pointers from the
801 * right
803 BUG_ON(!left);
804 wret = balance_node_right(trans, root, mid, left);
805 if (wret < 0) {
806 ret = wret;
807 goto enospc;
809 if (wret == 1) {
810 wret = push_node_left(trans, root, left, mid, 1);
811 if (wret < 0)
812 ret = wret;
814 BUG_ON(wret == 1);
816 if (btrfs_header_nritems(mid) == 0) {
817 /* we've managed to empty the middle node, drop it */
818 u64 bytenr = mid->start;
819 u32 blocksize = mid->len;
820 clean_tree_block(mid);
821 free_extent_buffer(mid);
822 mid = NULL;
823 wret = btrfs_del_ptr(root, path, level + 1, pslot);
824 if (wret)
825 ret = wret;
826 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
827 0, root->root_key.objectid,
828 level, 0);
829 if (wret)
830 ret = wret;
831 } else {
832 /* update the parent key to reflect our changes */
833 struct btrfs_disk_key mid_key;
834 btrfs_node_key(mid, &mid_key, 0);
835 btrfs_set_node_key(parent, &mid_key, pslot);
836 btrfs_mark_buffer_dirty(parent);
839 /* update the path */
840 if (left) {
841 if (btrfs_header_nritems(left) > orig_slot) {
842 extent_buffer_get(left);
843 path->nodes[level] = left;
844 path->slots[level + 1] -= 1;
845 path->slots[level] = orig_slot;
846 if (mid)
847 free_extent_buffer(mid);
848 } else {
849 orig_slot -= btrfs_header_nritems(left);
850 path->slots[level] = orig_slot;
853 /* double check we haven't messed things up */
854 check_block(root, path, level);
855 if (orig_ptr !=
856 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
857 BUG();
858 enospc:
859 if (right)
860 free_extent_buffer(right);
861 if (left)
862 free_extent_buffer(left);
863 return ret;
866 /* returns zero if the push worked, non-zero otherwise */
867 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
868 struct btrfs_root *root,
869 struct btrfs_path *path, int level)
871 struct extent_buffer *right = NULL;
872 struct extent_buffer *mid;
873 struct extent_buffer *left = NULL;
874 struct extent_buffer *parent = NULL;
875 struct btrfs_fs_info *fs_info = root->fs_info;
876 int ret = 0;
877 int wret;
878 int pslot;
879 int orig_slot = path->slots[level];
881 if (level == 0)
882 return 1;
884 mid = path->nodes[level];
885 WARN_ON(btrfs_header_generation(mid) != trans->transid);
887 if (level < BTRFS_MAX_LEVEL - 1) {
888 parent = path->nodes[level + 1];
889 pslot = path->slots[level + 1];
892 if (!parent)
893 return 1;
895 left = read_node_slot(fs_info, parent, pslot - 1);
897 /* first, try to make some room in the middle buffer */
898 if (extent_buffer_uptodate(left)) {
899 u32 left_nr;
900 left_nr = btrfs_header_nritems(left);
901 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
902 wret = 1;
903 } else {
904 ret = btrfs_cow_block(trans, root, left, parent,
905 pslot - 1, &left);
906 if (ret)
907 wret = 1;
908 else {
909 wret = push_node_left(trans, root,
910 left, mid, 0);
913 if (wret < 0)
914 ret = wret;
915 if (wret == 0) {
916 struct btrfs_disk_key disk_key;
917 orig_slot += left_nr;
918 btrfs_node_key(mid, &disk_key, 0);
919 btrfs_set_node_key(parent, &disk_key, pslot);
920 btrfs_mark_buffer_dirty(parent);
921 if (btrfs_header_nritems(left) > orig_slot) {
922 path->nodes[level] = left;
923 path->slots[level + 1] -= 1;
924 path->slots[level] = orig_slot;
925 free_extent_buffer(mid);
926 } else {
927 orig_slot -=
928 btrfs_header_nritems(left);
929 path->slots[level] = orig_slot;
930 free_extent_buffer(left);
932 return 0;
934 free_extent_buffer(left);
936 right= read_node_slot(fs_info, parent, pslot + 1);
939 * then try to empty the right most buffer into the middle
941 if (extent_buffer_uptodate(right)) {
942 u32 right_nr;
943 right_nr = btrfs_header_nritems(right);
944 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 1) {
945 wret = 1;
946 } else {
947 ret = btrfs_cow_block(trans, root, right,
948 parent, pslot + 1,
949 &right);
950 if (ret)
951 wret = 1;
952 else {
953 wret = balance_node_right(trans, root,
954 right, mid);
957 if (wret < 0)
958 ret = wret;
959 if (wret == 0) {
960 struct btrfs_disk_key disk_key;
962 btrfs_node_key(right, &disk_key, 0);
963 btrfs_set_node_key(parent, &disk_key, pslot + 1);
964 btrfs_mark_buffer_dirty(parent);
966 if (btrfs_header_nritems(mid) <= orig_slot) {
967 path->nodes[level] = right;
968 path->slots[level + 1] += 1;
969 path->slots[level] = orig_slot -
970 btrfs_header_nritems(mid);
971 free_extent_buffer(mid);
972 } else {
973 free_extent_buffer(right);
975 return 0;
977 free_extent_buffer(right);
979 return 1;
983 * readahead one full node of leaves
985 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
986 int level, int slot, u64 objectid)
988 struct btrfs_fs_info *fs_info = root->fs_info;
989 struct extent_buffer *node;
990 struct btrfs_disk_key disk_key;
991 u32 nritems;
992 u64 search;
993 u64 lowest_read;
994 u64 highest_read;
995 u64 nread = 0;
996 int direction = path->reada;
997 struct extent_buffer *eb;
998 u32 nr;
999 u32 nscan = 0;
1001 if (level != 1)
1002 return;
1004 if (!path->nodes[level])
1005 return;
1007 node = path->nodes[level];
1008 search = btrfs_node_blockptr(node, slot);
1009 eb = btrfs_find_tree_block(fs_info, search, fs_info->nodesize);
1010 if (eb) {
1011 free_extent_buffer(eb);
1012 return;
1015 highest_read = search;
1016 lowest_read = search;
1018 nritems = btrfs_header_nritems(node);
1019 nr = slot;
1020 while(1) {
1021 if (direction < 0) {
1022 if (nr == 0)
1023 break;
1024 nr--;
1025 } else if (direction > 0) {
1026 nr++;
1027 if (nr >= nritems)
1028 break;
1030 if (path->reada < 0 && objectid) {
1031 btrfs_node_key(node, &disk_key, nr);
1032 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1033 break;
1035 search = btrfs_node_blockptr(node, nr);
1036 if ((search >= lowest_read && search <= highest_read) ||
1037 (search < lowest_read && lowest_read - search <= 32768) ||
1038 (search > highest_read && search - highest_read <= 32768)) {
1039 readahead_tree_block(fs_info, search,
1040 btrfs_node_ptr_generation(node, nr));
1041 nread += fs_info->nodesize;
1043 nscan++;
1044 if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
1045 break;
1046 if(nread > SZ_1M || nscan > 128)
1047 break;
1049 if (search < lowest_read)
1050 lowest_read = search;
1051 if (search > highest_read)
1052 highest_read = search;
1056 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1057 u64 iobjectid, u64 ioff, u8 key_type,
1058 struct btrfs_key *found_key)
1060 int ret;
1061 struct btrfs_key key;
1062 struct extent_buffer *eb;
1063 struct btrfs_path *path;
1065 key.type = key_type;
1066 key.objectid = iobjectid;
1067 key.offset = ioff;
1069 if (found_path == NULL) {
1070 path = btrfs_alloc_path();
1071 if (!path)
1072 return -ENOMEM;
1073 } else
1074 path = found_path;
1076 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1077 if ((ret < 0) || (found_key == NULL))
1078 goto out;
1080 eb = path->nodes[0];
1081 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1082 ret = btrfs_next_leaf(fs_root, path);
1083 if (ret)
1084 goto out;
1085 eb = path->nodes[0];
1088 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1089 if (found_key->type != key.type ||
1090 found_key->objectid != key.objectid) {
1091 ret = 1;
1092 goto out;
1095 out:
1096 if (path != found_path)
1097 btrfs_free_path(path);
1098 return ret;
1102 * look for key in the tree. path is filled in with nodes along the way
1103 * if key is found, we return zero and you can find the item in the leaf
1104 * level of the path (level 0)
1106 * If the key isn't found, the path points to the slot where it should
1107 * be inserted, and 1 is returned. If there are other errors during the
1108 * search a negative error number is returned.
1110 * if ins_len > 0, nodes and leaves will be split as we walk down the
1111 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1112 * possible)
1114 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1115 *root, struct btrfs_key *key, struct btrfs_path *p, int
1116 ins_len, int cow)
1118 struct extent_buffer *b;
1119 int slot;
1120 int ret;
1121 int level;
1122 int should_reada = p->reada;
1123 struct btrfs_fs_info *fs_info = root->fs_info;
1124 u8 lowest_level = 0;
1126 lowest_level = p->lowest_level;
1127 WARN_ON(lowest_level && ins_len > 0);
1128 WARN_ON(p->nodes[0] != NULL);
1130 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1132 again:
1133 b = root->node;
1134 extent_buffer_get(b);
1135 while (b) {
1136 level = btrfs_header_level(b);
1137 if (cow) {
1138 int wret;
1139 wret = btrfs_cow_block(trans, root, b,
1140 p->nodes[level + 1],
1141 p->slots[level + 1],
1142 &b);
1143 if (wret) {
1144 free_extent_buffer(b);
1145 return wret;
1148 BUG_ON(!cow && ins_len);
1149 if (level != btrfs_header_level(b))
1150 WARN_ON(1);
1151 level = btrfs_header_level(b);
1152 p->nodes[level] = b;
1153 ret = check_block(root, p, level);
1154 if (ret)
1155 return -1;
1156 ret = bin_search(b, key, level, &slot);
1157 if (level != 0) {
1158 if (ret && slot > 0)
1159 slot -= 1;
1160 p->slots[level] = slot;
1161 if ((p->search_for_split || ins_len > 0) &&
1162 btrfs_header_nritems(b) >=
1163 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1164 int sret = split_node(trans, root, p, level);
1165 BUG_ON(sret > 0);
1166 if (sret)
1167 return sret;
1168 b = p->nodes[level];
1169 slot = p->slots[level];
1170 } else if (ins_len < 0) {
1171 int sret = balance_level(trans, root, p,
1172 level);
1173 if (sret)
1174 return sret;
1175 b = p->nodes[level];
1176 if (!b) {
1177 btrfs_release_path(p);
1178 goto again;
1180 slot = p->slots[level];
1181 BUG_ON(btrfs_header_nritems(b) == 1);
1183 /* this is only true while dropping a snapshot */
1184 if (level == lowest_level)
1185 break;
1187 if (should_reada)
1188 reada_for_search(root, p, level, slot,
1189 key->objectid);
1191 b = read_node_slot(fs_info, b, slot);
1192 if (!extent_buffer_uptodate(b))
1193 return -EIO;
1194 } else {
1195 p->slots[level] = slot;
1196 if (ins_len > 0 &&
1197 ins_len > btrfs_leaf_free_space(b)) {
1198 int sret = split_leaf(trans, root, key,
1199 p, ins_len, ret == 0);
1200 BUG_ON(sret > 0);
1201 if (sret)
1202 return sret;
1204 return ret;
1207 return 1;
1211 * adjust the pointers going up the tree, starting at level
1212 * making sure the right key of each node is points to 'key'.
1213 * This is used after shifting pointers to the left, so it stops
1214 * fixing up pointers when a given leaf/node is not in slot 0 of the
1215 * higher levels
1217 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
1218 struct btrfs_disk_key *key, int level)
1220 int i;
1221 struct extent_buffer *t;
1223 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1224 int tslot = path->slots[i];
1225 if (!path->nodes[i])
1226 break;
1227 t = path->nodes[i];
1228 btrfs_set_node_key(t, key, tslot);
1229 btrfs_mark_buffer_dirty(path->nodes[i]);
1230 if (tslot != 0)
1231 break;
1236 * update item key.
1238 * This function isn't completely safe. It's the caller's responsibility
1239 * that the new key won't break the order
1241 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
1242 struct btrfs_key *new_key)
1244 struct btrfs_disk_key disk_key;
1245 struct extent_buffer *eb;
1246 int slot;
1248 eb = path->nodes[0];
1249 slot = path->slots[0];
1250 if (slot > 0) {
1251 btrfs_item_key(eb, &disk_key, slot - 1);
1252 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1253 return -1;
1255 if (slot < btrfs_header_nritems(eb) - 1) {
1256 btrfs_item_key(eb, &disk_key, slot + 1);
1257 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1258 return -1;
1261 btrfs_cpu_key_to_disk(&disk_key, new_key);
1262 btrfs_set_item_key(eb, &disk_key, slot);
1263 btrfs_mark_buffer_dirty(eb);
1264 if (slot == 0)
1265 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1266 return 0;
1270 * update an item key without the safety checks. This is meant to be called by
1271 * fsck only.
1273 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
1274 struct btrfs_path *path,
1275 struct btrfs_key *new_key)
1277 struct btrfs_disk_key disk_key;
1278 struct extent_buffer *eb;
1279 int slot;
1281 eb = path->nodes[0];
1282 slot = path->slots[0];
1284 btrfs_cpu_key_to_disk(&disk_key, new_key);
1285 btrfs_set_item_key(eb, &disk_key, slot);
1286 btrfs_mark_buffer_dirty(eb);
1287 if (slot == 0)
1288 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1292 * try to push data from one node into the next node left in the
1293 * tree.
1295 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1296 * error, and > 0 if there was no room in the left hand block.
1298 static int push_node_left(struct btrfs_trans_handle *trans,
1299 struct btrfs_root *root, struct extent_buffer *dst,
1300 struct extent_buffer *src, int empty)
1302 int push_items = 0;
1303 int src_nritems;
1304 int dst_nritems;
1305 int ret = 0;
1307 src_nritems = btrfs_header_nritems(src);
1308 dst_nritems = btrfs_header_nritems(dst);
1309 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1310 WARN_ON(btrfs_header_generation(src) != trans->transid);
1311 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1313 if (!empty && src_nritems <= 8)
1314 return 1;
1316 if (push_items <= 0) {
1317 return 1;
1320 if (empty) {
1321 push_items = min(src_nritems, push_items);
1322 if (push_items < src_nritems) {
1323 /* leave at least 8 pointers in the node if
1324 * we aren't going to empty it
1326 if (src_nritems - push_items < 8) {
1327 if (push_items <= 8)
1328 return 1;
1329 push_items -= 8;
1332 } else
1333 push_items = min(src_nritems - 8, push_items);
1335 copy_extent_buffer(dst, src,
1336 btrfs_node_key_ptr_offset(dst_nritems),
1337 btrfs_node_key_ptr_offset(0),
1338 push_items * sizeof(struct btrfs_key_ptr));
1340 if (push_items < src_nritems) {
1341 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1342 btrfs_node_key_ptr_offset(push_items),
1343 (src_nritems - push_items) *
1344 sizeof(struct btrfs_key_ptr));
1346 btrfs_set_header_nritems(src, src_nritems - push_items);
1347 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1348 btrfs_mark_buffer_dirty(src);
1349 btrfs_mark_buffer_dirty(dst);
1351 return ret;
1355 * try to push data from one node into the next node right in the
1356 * tree.
1358 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1359 * error, and > 0 if there was no room in the right hand block.
1361 * this will only push up to 1/2 the contents of the left node over
1363 static int balance_node_right(struct btrfs_trans_handle *trans,
1364 struct btrfs_root *root,
1365 struct extent_buffer *dst,
1366 struct extent_buffer *src)
1368 int push_items = 0;
1369 int max_push;
1370 int src_nritems;
1371 int dst_nritems;
1372 int ret = 0;
1374 WARN_ON(btrfs_header_generation(src) != trans->transid);
1375 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1377 src_nritems = btrfs_header_nritems(src);
1378 dst_nritems = btrfs_header_nritems(dst);
1379 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1380 if (push_items <= 0) {
1381 return 1;
1384 if (src_nritems < 4) {
1385 return 1;
1388 max_push = src_nritems / 2 + 1;
1389 /* don't try to empty the node */
1390 if (max_push >= src_nritems) {
1391 return 1;
1394 if (max_push < push_items)
1395 push_items = max_push;
1397 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1398 btrfs_node_key_ptr_offset(0),
1399 (dst_nritems) *
1400 sizeof(struct btrfs_key_ptr));
1402 copy_extent_buffer(dst, src,
1403 btrfs_node_key_ptr_offset(0),
1404 btrfs_node_key_ptr_offset(src_nritems - push_items),
1405 push_items * sizeof(struct btrfs_key_ptr));
1407 btrfs_set_header_nritems(src, src_nritems - push_items);
1408 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1410 btrfs_mark_buffer_dirty(src);
1411 btrfs_mark_buffer_dirty(dst);
1413 return ret;
1417 * helper function to insert a new root level in the tree.
1418 * A new node is allocated, and a single item is inserted to
1419 * point to the existing root
1421 * returns zero on success or < 0 on failure.
1423 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1424 struct btrfs_root *root,
1425 struct btrfs_path *path, int level)
1427 u64 lower_gen;
1428 struct extent_buffer *lower;
1429 struct extent_buffer *c;
1430 struct extent_buffer *old;
1431 struct btrfs_disk_key lower_key;
1433 BUG_ON(path->nodes[level]);
1434 BUG_ON(path->nodes[level-1] != root->node);
1436 lower = path->nodes[level-1];
1437 if (level == 1)
1438 btrfs_item_key(lower, &lower_key, 0);
1439 else
1440 btrfs_node_key(lower, &lower_key, 0);
1442 c = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1443 root->root_key.objectid, &lower_key,
1444 level, root->node->start, 0);
1446 if (IS_ERR(c))
1447 return PTR_ERR(c);
1449 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1450 btrfs_set_header_nritems(c, 1);
1451 btrfs_set_header_level(c, level);
1452 btrfs_set_header_bytenr(c, c->start);
1453 btrfs_set_header_generation(c, trans->transid);
1454 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1455 btrfs_set_header_owner(c, root->root_key.objectid);
1457 write_extent_buffer(c, root->fs_info->fsid,
1458 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1460 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1461 btrfs_header_chunk_tree_uuid(c),
1462 BTRFS_UUID_SIZE);
1464 btrfs_set_node_key(c, &lower_key, 0);
1465 btrfs_set_node_blockptr(c, 0, lower->start);
1466 lower_gen = btrfs_header_generation(lower);
1467 WARN_ON(lower_gen != trans->transid);
1469 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1471 btrfs_mark_buffer_dirty(c);
1473 old = root->node;
1474 root->node = c;
1476 /* the super has an extra ref to root->node */
1477 free_extent_buffer(old);
1479 add_root_to_dirty_list(root);
1480 extent_buffer_get(c);
1481 path->nodes[level] = c;
1482 path->slots[level] = 0;
1483 return 0;
1487 * worker function to insert a single pointer in a node.
1488 * the node should have enough room for the pointer already
1490 * slot and level indicate where you want the key to go, and
1491 * blocknr is the block the key points to.
1493 * returns zero on success and < 0 on any error
1495 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1496 *root, struct btrfs_path *path, struct btrfs_disk_key
1497 *key, u64 bytenr, int slot, int level)
1499 struct extent_buffer *lower;
1500 int nritems;
1502 BUG_ON(!path->nodes[level]);
1503 lower = path->nodes[level];
1504 nritems = btrfs_header_nritems(lower);
1505 if (slot > nritems)
1506 BUG();
1507 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
1508 BUG();
1509 if (slot < nritems) {
1510 /* shift the items */
1511 memmove_extent_buffer(lower,
1512 btrfs_node_key_ptr_offset(slot + 1),
1513 btrfs_node_key_ptr_offset(slot),
1514 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1516 btrfs_set_node_key(lower, key, slot);
1517 btrfs_set_node_blockptr(lower, slot, bytenr);
1518 WARN_ON(trans->transid == 0);
1519 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1520 btrfs_set_header_nritems(lower, nritems + 1);
1521 btrfs_mark_buffer_dirty(lower);
1522 return 0;
1526 * split the node at the specified level in path in two.
1527 * The path is corrected to point to the appropriate node after the split
1529 * Before splitting this tries to make some room in the node by pushing
1530 * left and right, if either one works, it returns right away.
1532 * returns 0 on success and < 0 on failure
1534 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1535 *root, struct btrfs_path *path, int level)
1537 struct extent_buffer *c;
1538 struct extent_buffer *split;
1539 struct btrfs_disk_key disk_key;
1540 int mid;
1541 int ret;
1542 int wret;
1543 u32 c_nritems;
1545 c = path->nodes[level];
1546 WARN_ON(btrfs_header_generation(c) != trans->transid);
1547 if (c == root->node) {
1548 /* trying to split the root, lets make a new one */
1549 ret = insert_new_root(trans, root, path, level + 1);
1550 if (ret)
1551 return ret;
1552 } else {
1553 ret = push_nodes_for_insert(trans, root, path, level);
1554 c = path->nodes[level];
1555 if (!ret && btrfs_header_nritems(c) <
1556 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 3)
1557 return 0;
1558 if (ret < 0)
1559 return ret;
1562 c_nritems = btrfs_header_nritems(c);
1563 mid = (c_nritems + 1) / 2;
1564 btrfs_node_key(c, &disk_key, mid);
1566 split = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1567 root->root_key.objectid,
1568 &disk_key, level, c->start, 0);
1569 if (IS_ERR(split))
1570 return PTR_ERR(split);
1572 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1573 btrfs_set_header_level(split, btrfs_header_level(c));
1574 btrfs_set_header_bytenr(split, split->start);
1575 btrfs_set_header_generation(split, trans->transid);
1576 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1577 btrfs_set_header_owner(split, root->root_key.objectid);
1578 write_extent_buffer(split, root->fs_info->fsid,
1579 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1580 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1581 btrfs_header_chunk_tree_uuid(split),
1582 BTRFS_UUID_SIZE);
1585 copy_extent_buffer(split, c,
1586 btrfs_node_key_ptr_offset(0),
1587 btrfs_node_key_ptr_offset(mid),
1588 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1589 btrfs_set_header_nritems(split, c_nritems - mid);
1590 btrfs_set_header_nritems(c, mid);
1591 ret = 0;
1593 btrfs_mark_buffer_dirty(c);
1594 btrfs_mark_buffer_dirty(split);
1596 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1597 path->slots[level + 1] + 1,
1598 level + 1);
1599 if (wret)
1600 ret = wret;
1602 if (path->slots[level] >= mid) {
1603 path->slots[level] -= mid;
1604 free_extent_buffer(c);
1605 path->nodes[level] = split;
1606 path->slots[level + 1] += 1;
1607 } else {
1608 free_extent_buffer(split);
1610 return ret;
1614 * how many bytes are required to store the items in a leaf. start
1615 * and nr indicate which items in the leaf to check. This totals up the
1616 * space used both by the item structs and the item data
1618 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1620 int data_len;
1621 int nritems = btrfs_header_nritems(l);
1622 int end = min(nritems, start + nr) - 1;
1624 if (!nr)
1625 return 0;
1626 data_len = btrfs_item_end_nr(l, start);
1627 data_len = data_len - btrfs_item_offset_nr(l, end);
1628 data_len += sizeof(struct btrfs_item) * nr;
1629 WARN_ON(data_len < 0);
1630 return data_len;
1634 * The space between the end of the leaf items and
1635 * the start of the leaf data. IOW, how much room
1636 * the leaf has left for both items and data
1638 int btrfs_leaf_free_space(struct extent_buffer *leaf)
1640 int nritems = btrfs_header_nritems(leaf);
1641 u32 leaf_data_size;
1642 int ret;
1644 BUG_ON(leaf->fs_info && leaf->fs_info->nodesize != leaf->len);
1645 leaf_data_size = __BTRFS_LEAF_DATA_SIZE(leaf->len);
1646 ret = leaf_data_size - leaf_space_used(leaf, 0 ,nritems);
1647 if (ret < 0) {
1648 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1649 ret, leaf_data_size, leaf_space_used(leaf, 0, nritems),
1650 nritems);
1652 return ret;
1656 * push some data in the path leaf to the right, trying to free up at
1657 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1659 * returns 1 if the push failed because the other node didn't have enough
1660 * room, 0 if everything worked out and < 0 if there were major errors.
1662 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1663 *root, struct btrfs_path *path, int data_size,
1664 int empty)
1666 struct extent_buffer *left = path->nodes[0];
1667 struct extent_buffer *right;
1668 struct extent_buffer *upper;
1669 struct btrfs_disk_key disk_key;
1670 struct btrfs_fs_info *fs_info = root->fs_info;
1671 int slot;
1672 u32 i;
1673 int free_space;
1674 int push_space = 0;
1675 int push_items = 0;
1676 struct btrfs_item *item;
1677 u32 left_nritems;
1678 u32 nr;
1679 u32 right_nritems;
1680 u32 data_end;
1681 u32 this_item_size;
1682 int ret;
1684 slot = path->slots[1];
1685 if (!path->nodes[1]) {
1686 return 1;
1688 upper = path->nodes[1];
1689 if (slot >= btrfs_header_nritems(upper) - 1)
1690 return 1;
1692 right = read_node_slot(fs_info, upper, slot + 1);
1693 if (!extent_buffer_uptodate(right)) {
1694 if (IS_ERR(right))
1695 return PTR_ERR(right);
1696 return -EIO;
1698 free_space = btrfs_leaf_free_space(right);
1699 if (free_space < data_size) {
1700 free_extent_buffer(right);
1701 return 1;
1704 /* cow and double check */
1705 ret = btrfs_cow_block(trans, root, right, upper,
1706 slot + 1, &right);
1707 if (ret) {
1708 free_extent_buffer(right);
1709 return 1;
1711 free_space = btrfs_leaf_free_space(right);
1712 if (free_space < data_size) {
1713 free_extent_buffer(right);
1714 return 1;
1717 left_nritems = btrfs_header_nritems(left);
1718 if (left_nritems == 0) {
1719 free_extent_buffer(right);
1720 return 1;
1723 if (empty)
1724 nr = 0;
1725 else
1726 nr = 1;
1728 i = left_nritems - 1;
1729 while (i >= nr) {
1730 item = btrfs_item_nr(i);
1732 if (path->slots[0] == i)
1733 push_space += data_size + sizeof(*item);
1735 this_item_size = btrfs_item_size(left, item);
1736 if (this_item_size + sizeof(*item) + push_space > free_space)
1737 break;
1738 push_items++;
1739 push_space += this_item_size + sizeof(*item);
1740 if (i == 0)
1741 break;
1742 i--;
1745 if (push_items == 0) {
1746 free_extent_buffer(right);
1747 return 1;
1750 if (!empty && push_items == left_nritems)
1751 WARN_ON(1);
1753 /* push left to right */
1754 right_nritems = btrfs_header_nritems(right);
1756 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1757 push_space -= leaf_data_end(fs_info, left);
1759 /* make room in the right data area */
1760 data_end = leaf_data_end(fs_info, right);
1761 memmove_extent_buffer(right,
1762 btrfs_leaf_data(right) + data_end - push_space,
1763 btrfs_leaf_data(right) + data_end,
1764 BTRFS_LEAF_DATA_SIZE(root->fs_info) - data_end);
1766 /* copy from the left data area */
1767 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1768 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space,
1769 btrfs_leaf_data(left) + leaf_data_end(fs_info, left),
1770 push_space);
1772 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1773 btrfs_item_nr_offset(0),
1774 right_nritems * sizeof(struct btrfs_item));
1776 /* copy the items from left to right */
1777 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1778 btrfs_item_nr_offset(left_nritems - push_items),
1779 push_items * sizeof(struct btrfs_item));
1781 /* update the item pointers */
1782 right_nritems += push_items;
1783 btrfs_set_header_nritems(right, right_nritems);
1784 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1785 for (i = 0; i < right_nritems; i++) {
1786 item = btrfs_item_nr(i);
1787 push_space -= btrfs_item_size(right, item);
1788 btrfs_set_item_offset(right, item, push_space);
1791 left_nritems -= push_items;
1792 btrfs_set_header_nritems(left, left_nritems);
1794 if (left_nritems)
1795 btrfs_mark_buffer_dirty(left);
1796 btrfs_mark_buffer_dirty(right);
1798 btrfs_item_key(right, &disk_key, 0);
1799 btrfs_set_node_key(upper, &disk_key, slot + 1);
1800 btrfs_mark_buffer_dirty(upper);
1802 /* then fixup the leaf pointer in the path */
1803 if (path->slots[0] >= left_nritems) {
1804 path->slots[0] -= left_nritems;
1805 free_extent_buffer(path->nodes[0]);
1806 path->nodes[0] = right;
1807 path->slots[1] += 1;
1808 } else {
1809 free_extent_buffer(right);
1811 return 0;
1814 * push some data in the path leaf to the left, trying to free up at
1815 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1817 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1818 *root, struct btrfs_path *path, int data_size,
1819 int empty)
1821 struct btrfs_disk_key disk_key;
1822 struct extent_buffer *right = path->nodes[0];
1823 struct extent_buffer *left;
1824 struct btrfs_fs_info *fs_info = root->fs_info;
1825 int slot;
1826 int i;
1827 int free_space;
1828 int push_space = 0;
1829 int push_items = 0;
1830 struct btrfs_item *item;
1831 u32 old_left_nritems;
1832 u32 right_nritems;
1833 u32 nr;
1834 int ret = 0;
1835 u32 this_item_size;
1836 u32 old_left_item_size;
1838 slot = path->slots[1];
1839 if (slot == 0)
1840 return 1;
1841 if (!path->nodes[1])
1842 return 1;
1844 right_nritems = btrfs_header_nritems(right);
1845 if (right_nritems == 0) {
1846 return 1;
1849 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
1850 free_space = btrfs_leaf_free_space(left);
1851 if (free_space < data_size) {
1852 free_extent_buffer(left);
1853 return 1;
1856 /* cow and double check */
1857 ret = btrfs_cow_block(trans, root, left,
1858 path->nodes[1], slot - 1, &left);
1859 if (ret) {
1860 /* we hit -ENOSPC, but it isn't fatal here */
1861 free_extent_buffer(left);
1862 return 1;
1865 free_space = btrfs_leaf_free_space(left);
1866 if (free_space < data_size) {
1867 free_extent_buffer(left);
1868 return 1;
1871 if (empty)
1872 nr = right_nritems;
1873 else
1874 nr = right_nritems - 1;
1876 for (i = 0; i < nr; i++) {
1877 item = btrfs_item_nr(i);
1879 if (path->slots[0] == i)
1880 push_space += data_size + sizeof(*item);
1882 this_item_size = btrfs_item_size(right, item);
1883 if (this_item_size + sizeof(*item) + push_space > free_space)
1884 break;
1886 push_items++;
1887 push_space += this_item_size + sizeof(*item);
1890 if (push_items == 0) {
1891 free_extent_buffer(left);
1892 return 1;
1894 if (!empty && push_items == btrfs_header_nritems(right))
1895 WARN_ON(1);
1897 /* push data from right to left */
1898 copy_extent_buffer(left, right,
1899 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1900 btrfs_item_nr_offset(0),
1901 push_items * sizeof(struct btrfs_item));
1903 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1904 btrfs_item_offset_nr(right, push_items -1);
1906 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1907 leaf_data_end(fs_info, left) - push_space,
1908 btrfs_leaf_data(right) +
1909 btrfs_item_offset_nr(right, push_items - 1),
1910 push_space);
1911 old_left_nritems = btrfs_header_nritems(left);
1912 BUG_ON(old_left_nritems == 0);
1914 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1915 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1916 u32 ioff;
1918 item = btrfs_item_nr(i);
1919 ioff = btrfs_item_offset(left, item);
1920 btrfs_set_item_offset(left, item,
1921 ioff - (BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1922 old_left_item_size));
1924 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1926 /* fixup right node */
1927 if (push_items > right_nritems) {
1928 printk("push items %d nr %u\n", push_items, right_nritems);
1929 WARN_ON(1);
1932 if (push_items < right_nritems) {
1933 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1934 leaf_data_end(fs_info, right);
1935 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1936 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1937 push_space,
1938 btrfs_leaf_data(right) +
1939 leaf_data_end(fs_info, right),
1940 push_space);
1942 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1943 btrfs_item_nr_offset(push_items),
1944 (btrfs_header_nritems(right) - push_items) *
1945 sizeof(struct btrfs_item));
1947 right_nritems -= push_items;
1948 btrfs_set_header_nritems(right, right_nritems);
1949 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1950 for (i = 0; i < right_nritems; i++) {
1951 item = btrfs_item_nr(i);
1952 push_space = push_space - btrfs_item_size(right, item);
1953 btrfs_set_item_offset(right, item, push_space);
1956 btrfs_mark_buffer_dirty(left);
1957 if (right_nritems)
1958 btrfs_mark_buffer_dirty(right);
1960 btrfs_item_key(right, &disk_key, 0);
1961 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1963 /* then fixup the leaf pointer in the path */
1964 if (path->slots[0] < push_items) {
1965 path->slots[0] += old_left_nritems;
1966 free_extent_buffer(path->nodes[0]);
1967 path->nodes[0] = left;
1968 path->slots[1] -= 1;
1969 } else {
1970 free_extent_buffer(left);
1971 path->slots[0] -= push_items;
1973 BUG_ON(path->slots[0] < 0);
1974 return ret;
1978 * split the path's leaf in two, making sure there is at least data_size
1979 * available for the resulting leaf level of the path.
1981 * returns 0 if all went well and < 0 on failure.
1983 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
1984 struct btrfs_root *root,
1985 struct btrfs_path *path,
1986 struct extent_buffer *l,
1987 struct extent_buffer *right,
1988 int slot, int mid, int nritems)
1990 int data_copy_size;
1991 int rt_data_off;
1992 int i;
1993 int ret = 0;
1994 int wret;
1995 struct btrfs_disk_key disk_key;
1997 nritems = nritems - mid;
1998 btrfs_set_header_nritems(right, nritems);
1999 data_copy_size = btrfs_item_end_nr(l, mid) -
2000 leaf_data_end(root->fs_info, l);
2002 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2003 btrfs_item_nr_offset(mid),
2004 nritems * sizeof(struct btrfs_item));
2006 copy_extent_buffer(right, l,
2007 btrfs_leaf_data(right) +
2008 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2009 data_copy_size, btrfs_leaf_data(l) +
2010 leaf_data_end(root->fs_info, l), data_copy_size);
2012 rt_data_off = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2013 btrfs_item_end_nr(l, mid);
2015 for (i = 0; i < nritems; i++) {
2016 struct btrfs_item *item = btrfs_item_nr(i);
2017 u32 ioff = btrfs_item_offset(right, item);
2018 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2021 btrfs_set_header_nritems(l, mid);
2022 ret = 0;
2023 btrfs_item_key(right, &disk_key, 0);
2024 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2025 path->slots[1] + 1, 1);
2026 if (wret)
2027 ret = wret;
2029 btrfs_mark_buffer_dirty(right);
2030 btrfs_mark_buffer_dirty(l);
2031 BUG_ON(path->slots[0] != slot);
2033 if (mid <= slot) {
2034 free_extent_buffer(path->nodes[0]);
2035 path->nodes[0] = right;
2036 path->slots[0] -= mid;
2037 path->slots[1] += 1;
2038 } else {
2039 free_extent_buffer(right);
2042 BUG_ON(path->slots[0] < 0);
2044 return ret;
2048 * split the path's leaf in two, making sure there is at least data_size
2049 * available for the resulting leaf level of the path.
2051 * returns 0 if all went well and < 0 on failure.
2053 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2054 struct btrfs_root *root,
2055 struct btrfs_key *ins_key,
2056 struct btrfs_path *path, int data_size,
2057 int extend)
2059 struct btrfs_disk_key disk_key;
2060 struct extent_buffer *l;
2061 u32 nritems;
2062 int mid;
2063 int slot;
2064 struct extent_buffer *right;
2065 int ret = 0;
2066 int wret;
2067 int split;
2068 int num_doubles = 0;
2070 l = path->nodes[0];
2071 slot = path->slots[0];
2072 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2073 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info))
2074 return -EOVERFLOW;
2076 /* first try to make some room by pushing left and right */
2077 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2078 wret = push_leaf_right(trans, root, path, data_size, 0);
2079 if (wret < 0)
2080 return wret;
2081 if (wret) {
2082 wret = push_leaf_left(trans, root, path, data_size, 0);
2083 if (wret < 0)
2084 return wret;
2086 l = path->nodes[0];
2088 /* did the pushes work? */
2089 if (btrfs_leaf_free_space(l) >= data_size)
2090 return 0;
2093 if (!path->nodes[1]) {
2094 ret = insert_new_root(trans, root, path, 1);
2095 if (ret)
2096 return ret;
2098 again:
2099 split = 1;
2100 l = path->nodes[0];
2101 slot = path->slots[0];
2102 nritems = btrfs_header_nritems(l);
2103 mid = (nritems + 1) / 2;
2105 if (mid <= slot) {
2106 if (nritems == 1 ||
2107 leaf_space_used(l, mid, nritems - mid) + data_size >
2108 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2109 if (slot >= nritems) {
2110 split = 0;
2111 } else {
2112 mid = slot;
2113 if (mid != nritems &&
2114 leaf_space_used(l, mid, nritems - mid) +
2115 data_size >
2116 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2117 split = 2;
2121 } else {
2122 if (leaf_space_used(l, 0, mid) + data_size >
2123 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2124 if (!extend && data_size && slot == 0) {
2125 split = 0;
2126 } else if ((extend || !data_size) && slot == 0) {
2127 mid = 1;
2128 } else {
2129 mid = slot;
2130 if (mid != nritems &&
2131 leaf_space_used(l, mid, nritems - mid) +
2132 data_size >
2133 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2134 split = 2 ;
2140 if (split == 0)
2141 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2142 else
2143 btrfs_item_key(l, &disk_key, mid);
2145 right = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
2146 root->root_key.objectid,
2147 &disk_key, 0, l->start, 0);
2148 if (IS_ERR(right)) {
2149 BUG_ON(1);
2150 return PTR_ERR(right);
2153 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2154 btrfs_set_header_bytenr(right, right->start);
2155 btrfs_set_header_generation(right, trans->transid);
2156 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2157 btrfs_set_header_owner(right, root->root_key.objectid);
2158 btrfs_set_header_level(right, 0);
2159 write_extent_buffer(right, root->fs_info->fsid,
2160 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2162 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2163 btrfs_header_chunk_tree_uuid(right),
2164 BTRFS_UUID_SIZE);
2166 if (split == 0) {
2167 if (mid <= slot) {
2168 btrfs_set_header_nritems(right, 0);
2169 wret = insert_ptr(trans, root, path,
2170 &disk_key, right->start,
2171 path->slots[1] + 1, 1);
2172 if (wret)
2173 ret = wret;
2175 free_extent_buffer(path->nodes[0]);
2176 path->nodes[0] = right;
2177 path->slots[0] = 0;
2178 path->slots[1] += 1;
2179 } else {
2180 btrfs_set_header_nritems(right, 0);
2181 wret = insert_ptr(trans, root, path,
2182 &disk_key,
2183 right->start,
2184 path->slots[1], 1);
2185 if (wret)
2186 ret = wret;
2187 free_extent_buffer(path->nodes[0]);
2188 path->nodes[0] = right;
2189 path->slots[0] = 0;
2190 if (path->slots[1] == 0) {
2191 btrfs_fixup_low_keys(root, path,
2192 &disk_key, 1);
2195 btrfs_mark_buffer_dirty(right);
2196 return ret;
2199 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2200 BUG_ON(ret);
2202 if (split == 2) {
2203 BUG_ON(num_doubles != 0);
2204 num_doubles++;
2205 goto again;
2208 return ret;
2212 * This function splits a single item into two items,
2213 * giving 'new_key' to the new item and splitting the
2214 * old one at split_offset (from the start of the item).
2216 * The path may be released by this operation. After
2217 * the split, the path is pointing to the old item. The
2218 * new item is going to be in the same node as the old one.
2220 * Note, the item being split must be smaller enough to live alone on
2221 * a tree block with room for one extra struct btrfs_item
2223 * This allows us to split the item in place, keeping a lock on the
2224 * leaf the entire time.
2226 int btrfs_split_item(struct btrfs_trans_handle *trans,
2227 struct btrfs_root *root,
2228 struct btrfs_path *path,
2229 struct btrfs_key *new_key,
2230 unsigned long split_offset)
2232 u32 item_size;
2233 struct extent_buffer *leaf;
2234 struct btrfs_key orig_key;
2235 struct btrfs_item *item;
2236 struct btrfs_item *new_item;
2237 int ret = 0;
2238 int slot;
2239 u32 nritems;
2240 u32 orig_offset;
2241 struct btrfs_disk_key disk_key;
2242 char *buf;
2244 leaf = path->nodes[0];
2245 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2246 if (btrfs_leaf_free_space(leaf) >=
2247 sizeof(struct btrfs_item))
2248 goto split;
2250 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2251 btrfs_release_path(path);
2253 path->search_for_split = 1;
2255 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2256 path->search_for_split = 0;
2258 /* if our item isn't there or got smaller, return now */
2259 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2260 path->slots[0])) {
2261 return -EAGAIN;
2264 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2265 BUG_ON(ret);
2267 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
2268 leaf = path->nodes[0];
2270 split:
2271 item = btrfs_item_nr(path->slots[0]);
2272 orig_offset = btrfs_item_offset(leaf, item);
2273 item_size = btrfs_item_size(leaf, item);
2276 buf = kmalloc(item_size, GFP_NOFS);
2277 BUG_ON(!buf);
2278 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2279 path->slots[0]), item_size);
2280 slot = path->slots[0] + 1;
2281 leaf = path->nodes[0];
2283 nritems = btrfs_header_nritems(leaf);
2285 if (slot < nritems) {
2286 /* shift the items */
2287 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2288 btrfs_item_nr_offset(slot),
2289 (nritems - slot) * sizeof(struct btrfs_item));
2293 btrfs_cpu_key_to_disk(&disk_key, new_key);
2294 btrfs_set_item_key(leaf, &disk_key, slot);
2296 new_item = btrfs_item_nr(slot);
2298 btrfs_set_item_offset(leaf, new_item, orig_offset);
2299 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2301 btrfs_set_item_offset(leaf, item,
2302 orig_offset + item_size - split_offset);
2303 btrfs_set_item_size(leaf, item, split_offset);
2305 btrfs_set_header_nritems(leaf, nritems + 1);
2307 /* write the data for the start of the original item */
2308 write_extent_buffer(leaf, buf,
2309 btrfs_item_ptr_offset(leaf, path->slots[0]),
2310 split_offset);
2312 /* write the data for the new item */
2313 write_extent_buffer(leaf, buf + split_offset,
2314 btrfs_item_ptr_offset(leaf, slot),
2315 item_size - split_offset);
2316 btrfs_mark_buffer_dirty(leaf);
2318 ret = 0;
2319 if (btrfs_leaf_free_space(leaf) < 0) {
2320 btrfs_print_leaf(leaf);
2321 BUG();
2323 kfree(buf);
2324 return ret;
2327 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2328 u32 new_size, int from_end)
2330 int ret = 0;
2331 int slot;
2332 struct extent_buffer *leaf;
2333 struct btrfs_item *item;
2334 u32 nritems;
2335 unsigned int data_end;
2336 unsigned int old_data_start;
2337 unsigned int old_size;
2338 unsigned int size_diff;
2339 int i;
2341 leaf = path->nodes[0];
2342 slot = path->slots[0];
2344 old_size = btrfs_item_size_nr(leaf, slot);
2345 if (old_size == new_size)
2346 return 0;
2348 nritems = btrfs_header_nritems(leaf);
2349 data_end = leaf_data_end(root->fs_info, leaf);
2351 old_data_start = btrfs_item_offset_nr(leaf, slot);
2353 size_diff = old_size - new_size;
2355 BUG_ON(slot < 0);
2356 BUG_ON(slot >= nritems);
2359 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2361 /* first correct the data pointers */
2362 for (i = slot; i < nritems; i++) {
2363 u32 ioff;
2364 item = btrfs_item_nr(i);
2365 ioff = btrfs_item_offset(leaf, item);
2366 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2369 /* shift the data */
2370 if (from_end) {
2371 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2372 data_end + size_diff, btrfs_leaf_data(leaf) +
2373 data_end, old_data_start + new_size - data_end);
2374 } else {
2375 struct btrfs_disk_key disk_key;
2376 u64 offset;
2378 btrfs_item_key(leaf, &disk_key, slot);
2380 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2381 unsigned long ptr;
2382 struct btrfs_file_extent_item *fi;
2384 fi = btrfs_item_ptr(leaf, slot,
2385 struct btrfs_file_extent_item);
2386 fi = (struct btrfs_file_extent_item *)(
2387 (unsigned long)fi - size_diff);
2389 if (btrfs_file_extent_type(leaf, fi) ==
2390 BTRFS_FILE_EXTENT_INLINE) {
2391 ptr = btrfs_item_ptr_offset(leaf, slot);
2392 memmove_extent_buffer(leaf, ptr,
2393 (unsigned long)fi,
2394 offsetof(struct btrfs_file_extent_item,
2395 disk_bytenr));
2399 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2400 data_end + size_diff, btrfs_leaf_data(leaf) +
2401 data_end, old_data_start - data_end);
2403 offset = btrfs_disk_key_offset(&disk_key);
2404 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2405 btrfs_set_item_key(leaf, &disk_key, slot);
2406 if (slot == 0)
2407 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2410 item = btrfs_item_nr(slot);
2411 btrfs_set_item_size(leaf, item, new_size);
2412 btrfs_mark_buffer_dirty(leaf);
2414 ret = 0;
2415 if (btrfs_leaf_free_space(leaf) < 0) {
2416 btrfs_print_leaf(leaf);
2417 BUG();
2419 return ret;
2422 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2423 u32 data_size)
2425 int ret = 0;
2426 int slot;
2427 struct extent_buffer *leaf;
2428 struct btrfs_item *item;
2429 u32 nritems;
2430 unsigned int data_end;
2431 unsigned int old_data;
2432 unsigned int old_size;
2433 int i;
2435 leaf = path->nodes[0];
2437 nritems = btrfs_header_nritems(leaf);
2438 data_end = leaf_data_end(root->fs_info, leaf);
2440 if (btrfs_leaf_free_space(leaf) < data_size) {
2441 btrfs_print_leaf(leaf);
2442 BUG();
2444 slot = path->slots[0];
2445 old_data = btrfs_item_end_nr(leaf, slot);
2447 BUG_ON(slot < 0);
2448 if (slot >= nritems) {
2449 btrfs_print_leaf(leaf);
2450 printk("slot %d too large, nritems %d\n", slot, nritems);
2451 BUG_ON(1);
2455 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2457 /* first correct the data pointers */
2458 for (i = slot; i < nritems; i++) {
2459 u32 ioff;
2460 item = btrfs_item_nr(i);
2461 ioff = btrfs_item_offset(leaf, item);
2462 btrfs_set_item_offset(leaf, item, ioff - data_size);
2465 /* shift the data */
2466 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2467 data_end - data_size, btrfs_leaf_data(leaf) +
2468 data_end, old_data - data_end);
2470 data_end = old_data;
2471 old_size = btrfs_item_size_nr(leaf, slot);
2472 item = btrfs_item_nr(slot);
2473 btrfs_set_item_size(leaf, item, old_size + data_size);
2474 btrfs_mark_buffer_dirty(leaf);
2476 ret = 0;
2477 if (btrfs_leaf_free_space(leaf) < 0) {
2478 btrfs_print_leaf(leaf);
2479 BUG();
2481 return ret;
2485 * Given a key and some data, insert an item into the tree.
2486 * This does all the path init required, making room in the tree if needed.
2488 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2489 struct btrfs_root *root,
2490 struct btrfs_path *path,
2491 struct btrfs_key *cpu_key, u32 *data_size,
2492 int nr)
2494 struct extent_buffer *leaf;
2495 struct btrfs_item *item;
2496 int ret = 0;
2497 int slot;
2498 int i;
2499 u32 nritems;
2500 u32 total_size = 0;
2501 u32 total_data = 0;
2502 unsigned int data_end;
2503 struct btrfs_disk_key disk_key;
2505 for (i = 0; i < nr; i++) {
2506 total_data += data_size[i];
2509 /* create a root if there isn't one */
2510 if (!root->node)
2511 BUG();
2513 total_size = total_data + nr * sizeof(struct btrfs_item);
2514 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2515 if (ret == 0) {
2516 return -EEXIST;
2518 if (ret < 0)
2519 goto out;
2521 leaf = path->nodes[0];
2523 nritems = btrfs_header_nritems(leaf);
2524 data_end = leaf_data_end(root->fs_info, leaf);
2526 if (btrfs_leaf_free_space(leaf) < total_size) {
2527 btrfs_print_leaf(leaf);
2528 printk("not enough freespace need %u have %d\n",
2529 total_size, btrfs_leaf_free_space(leaf));
2530 BUG();
2533 slot = path->slots[0];
2534 BUG_ON(slot < 0);
2536 if (slot < nritems) {
2537 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2539 if (old_data < data_end) {
2540 btrfs_print_leaf(leaf);
2541 printk("slot %d old_data %d data_end %d\n",
2542 slot, old_data, data_end);
2543 BUG_ON(1);
2546 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2548 /* first correct the data pointers */
2549 for (i = slot; i < nritems; i++) {
2550 u32 ioff;
2552 item = btrfs_item_nr(i);
2553 ioff = btrfs_item_offset(leaf, item);
2554 btrfs_set_item_offset(leaf, item, ioff - total_data);
2557 /* shift the items */
2558 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2559 btrfs_item_nr_offset(slot),
2560 (nritems - slot) * sizeof(struct btrfs_item));
2562 /* shift the data */
2563 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2564 data_end - total_data, btrfs_leaf_data(leaf) +
2565 data_end, old_data - data_end);
2566 data_end = old_data;
2569 /* setup the item for the new data */
2570 for (i = 0; i < nr; i++) {
2571 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2572 btrfs_set_item_key(leaf, &disk_key, slot + i);
2573 item = btrfs_item_nr(slot + i);
2574 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2575 data_end -= data_size[i];
2576 btrfs_set_item_size(leaf, item, data_size[i]);
2578 btrfs_set_header_nritems(leaf, nritems + nr);
2579 btrfs_mark_buffer_dirty(leaf);
2581 ret = 0;
2582 if (slot == 0) {
2583 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2584 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2587 if (btrfs_leaf_free_space(leaf) < 0) {
2588 btrfs_print_leaf(leaf);
2589 BUG();
2592 out:
2593 return ret;
2597 * Given a key and some data, insert an item into the tree.
2598 * This does all the path init required, making room in the tree if needed.
2600 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2601 *root, struct btrfs_key *cpu_key, void *data, u32
2602 data_size)
2604 int ret = 0;
2605 struct btrfs_path *path;
2606 struct extent_buffer *leaf;
2607 unsigned long ptr;
2609 path = btrfs_alloc_path();
2610 if (!path)
2611 return -ENOMEM;
2613 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2614 if (!ret) {
2615 leaf = path->nodes[0];
2616 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2617 write_extent_buffer(leaf, data, ptr, data_size);
2618 btrfs_mark_buffer_dirty(leaf);
2620 btrfs_free_path(path);
2621 return ret;
2625 * delete the pointer from a given node.
2627 * If the delete empties a node, the node is removed from the tree,
2628 * continuing all the way the root if required. The root is converted into
2629 * a leaf if all the nodes are emptied.
2631 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2632 int level, int slot)
2634 struct extent_buffer *parent = path->nodes[level];
2635 u32 nritems;
2636 int ret = 0;
2638 nritems = btrfs_header_nritems(parent);
2639 if (slot < nritems - 1) {
2640 /* shift the items */
2641 memmove_extent_buffer(parent,
2642 btrfs_node_key_ptr_offset(slot),
2643 btrfs_node_key_ptr_offset(slot + 1),
2644 sizeof(struct btrfs_key_ptr) *
2645 (nritems - slot - 1));
2647 nritems--;
2648 btrfs_set_header_nritems(parent, nritems);
2649 if (nritems == 0 && parent == root->node) {
2650 BUG_ON(btrfs_header_level(root->node) != 1);
2651 /* just turn the root into a leaf and break */
2652 btrfs_set_header_level(root->node, 0);
2653 } else if (slot == 0) {
2654 struct btrfs_disk_key disk_key;
2656 btrfs_node_key(parent, &disk_key, 0);
2657 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2659 btrfs_mark_buffer_dirty(parent);
2660 return ret;
2664 * a helper function to delete the leaf pointed to by path->slots[1] and
2665 * path->nodes[1].
2667 * This deletes the pointer in path->nodes[1] and frees the leaf
2668 * block extent. zero is returned if it all worked out, < 0 otherwise.
2670 * The path must have already been setup for deleting the leaf, including
2671 * all the proper balancing. path->nodes[1] must be locked.
2673 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2674 struct btrfs_root *root,
2675 struct btrfs_path *path,
2676 struct extent_buffer *leaf)
2678 int ret;
2680 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2681 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2682 if (ret)
2683 return ret;
2685 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2686 0, root->root_key.objectid, 0, 0);
2687 return ret;
2691 * delete the item at the leaf level in path. If that empties
2692 * the leaf, remove it from the tree
2694 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2695 struct btrfs_path *path, int slot, int nr)
2697 struct extent_buffer *leaf;
2698 struct btrfs_item *item;
2699 int last_off;
2700 int dsize = 0;
2701 int ret = 0;
2702 int wret;
2703 int i;
2704 u32 nritems;
2706 leaf = path->nodes[0];
2707 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2709 for (i = 0; i < nr; i++)
2710 dsize += btrfs_item_size_nr(leaf, slot + i);
2712 nritems = btrfs_header_nritems(leaf);
2714 if (slot + nr != nritems) {
2715 int data_end = leaf_data_end(root->fs_info, leaf);
2717 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2718 data_end + dsize,
2719 btrfs_leaf_data(leaf) + data_end,
2720 last_off - data_end);
2722 for (i = slot + nr; i < nritems; i++) {
2723 u32 ioff;
2725 item = btrfs_item_nr(i);
2726 ioff = btrfs_item_offset(leaf, item);
2727 btrfs_set_item_offset(leaf, item, ioff + dsize);
2730 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2731 btrfs_item_nr_offset(slot + nr),
2732 sizeof(struct btrfs_item) *
2733 (nritems - slot - nr));
2735 btrfs_set_header_nritems(leaf, nritems - nr);
2736 nritems -= nr;
2738 /* delete the leaf if we've emptied it */
2739 if (nritems == 0) {
2740 if (leaf == root->node) {
2741 btrfs_set_header_level(leaf, 0);
2742 } else {
2743 clean_tree_block(leaf);
2744 wret = btrfs_del_leaf(trans, root, path, leaf);
2745 BUG_ON(ret);
2746 if (wret)
2747 ret = wret;
2749 } else {
2750 int used = leaf_space_used(leaf, 0, nritems);
2751 if (slot == 0) {
2752 struct btrfs_disk_key disk_key;
2754 btrfs_item_key(leaf, &disk_key, 0);
2755 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2758 /* delete the leaf if it is mostly empty */
2759 if (used < BTRFS_LEAF_DATA_SIZE(root->fs_info) / 4) {
2760 /* push_leaf_left fixes the path.
2761 * make sure the path still points to our leaf
2762 * for possible call to del_ptr below
2764 slot = path->slots[1];
2765 extent_buffer_get(leaf);
2767 wret = push_leaf_left(trans, root, path, 1, 1);
2768 if (wret < 0 && wret != -ENOSPC)
2769 ret = wret;
2771 if (path->nodes[0] == leaf &&
2772 btrfs_header_nritems(leaf)) {
2773 wret = push_leaf_right(trans, root, path, 1, 1);
2774 if (wret < 0 && wret != -ENOSPC)
2775 ret = wret;
2778 if (btrfs_header_nritems(leaf) == 0) {
2779 clean_tree_block(leaf);
2780 path->slots[1] = slot;
2781 ret = btrfs_del_leaf(trans, root, path, leaf);
2782 BUG_ON(ret);
2783 free_extent_buffer(leaf);
2785 } else {
2786 btrfs_mark_buffer_dirty(leaf);
2787 free_extent_buffer(leaf);
2789 } else {
2790 btrfs_mark_buffer_dirty(leaf);
2793 return ret;
2797 * walk up the tree as far as required to find the previous leaf.
2798 * returns 0 if it found something or 1 if there are no lesser leaves.
2799 * returns < 0 on io errors.
2801 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2803 int slot;
2804 int level = 1;
2805 struct extent_buffer *c;
2806 struct extent_buffer *next = NULL;
2807 struct btrfs_fs_info *fs_info = root->fs_info;
2809 while(level < BTRFS_MAX_LEVEL) {
2810 if (!path->nodes[level])
2811 return 1;
2813 slot = path->slots[level];
2814 c = path->nodes[level];
2815 if (slot == 0) {
2816 level++;
2817 if (level == BTRFS_MAX_LEVEL)
2818 return 1;
2819 continue;
2821 slot--;
2823 next = read_node_slot(fs_info, c, slot);
2824 if (!extent_buffer_uptodate(next)) {
2825 if (IS_ERR(next))
2826 return PTR_ERR(next);
2827 return -EIO;
2829 break;
2831 path->slots[level] = slot;
2832 while(1) {
2833 level--;
2834 c = path->nodes[level];
2835 free_extent_buffer(c);
2836 slot = btrfs_header_nritems(next);
2837 if (slot != 0)
2838 slot--;
2839 path->nodes[level] = next;
2840 path->slots[level] = slot;
2841 if (!level)
2842 break;
2843 next = read_node_slot(fs_info, next, slot);
2844 if (!extent_buffer_uptodate(next)) {
2845 if (IS_ERR(next))
2846 return PTR_ERR(next);
2847 return -EIO;
2850 return 0;
2854 * walk up the tree as far as required to find the next leaf.
2855 * returns 0 if it found something or 1 if there are no greater leaves.
2856 * returns < 0 on io errors.
2858 int btrfs_next_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;
2864 struct btrfs_fs_info *fs_info = root->fs_info;
2866 while(level < BTRFS_MAX_LEVEL) {
2867 if (!path->nodes[level])
2868 return 1;
2870 slot = path->slots[level] + 1;
2871 c = path->nodes[level];
2872 if (slot >= btrfs_header_nritems(c)) {
2873 level++;
2874 if (level == BTRFS_MAX_LEVEL)
2875 return 1;
2876 continue;
2879 if (path->reada)
2880 reada_for_search(root, path, level, slot, 0);
2882 next = read_node_slot(fs_info, c, slot);
2883 if (!extent_buffer_uptodate(next))
2884 return -EIO;
2885 break;
2887 path->slots[level] = slot;
2888 while(1) {
2889 level--;
2890 c = path->nodes[level];
2891 free_extent_buffer(c);
2892 path->nodes[level] = next;
2893 path->slots[level] = 0;
2894 if (!level)
2895 break;
2896 if (path->reada)
2897 reada_for_search(root, path, level, 0, 0);
2898 next = read_node_slot(fs_info, next, 0);
2899 if (!extent_buffer_uptodate(next))
2900 return -EIO;
2902 return 0;
2905 int btrfs_previous_item(struct btrfs_root *root,
2906 struct btrfs_path *path, u64 min_objectid,
2907 int type)
2909 struct btrfs_key found_key;
2910 struct extent_buffer *leaf;
2911 u32 nritems;
2912 int ret;
2914 while(1) {
2915 if (path->slots[0] == 0) {
2916 ret = btrfs_prev_leaf(root, path);
2917 if (ret != 0)
2918 return ret;
2919 } else {
2920 path->slots[0]--;
2922 leaf = path->nodes[0];
2923 nritems = btrfs_header_nritems(leaf);
2924 if (nritems == 0)
2925 return 1;
2926 if (path->slots[0] == nritems)
2927 path->slots[0]--;
2929 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2930 if (found_key.objectid < min_objectid)
2931 break;
2932 if (found_key.type == type)
2933 return 0;
2934 if (found_key.objectid == min_objectid &&
2935 found_key.type < type)
2936 break;
2938 return 1;
2942 * search in extent tree to find a previous Metadata/Data extent item with
2943 * min objecitd.
2945 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2947 int btrfs_previous_extent_item(struct btrfs_root *root,
2948 struct btrfs_path *path, u64 min_objectid)
2950 struct btrfs_key found_key;
2951 struct extent_buffer *leaf;
2952 u32 nritems;
2953 int ret;
2955 while (1) {
2956 if (path->slots[0] == 0) {
2957 ret = btrfs_prev_leaf(root, path);
2958 if (ret != 0)
2959 return ret;
2960 } else {
2961 path->slots[0]--;
2963 leaf = path->nodes[0];
2964 nritems = btrfs_header_nritems(leaf);
2965 if (nritems == 0)
2966 return 1;
2967 if (path->slots[0] == nritems)
2968 path->slots[0]--;
2970 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2971 if (found_key.objectid < min_objectid)
2972 break;
2973 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2974 found_key.type == BTRFS_METADATA_ITEM_KEY)
2975 return 0;
2976 if (found_key.objectid == min_objectid &&
2977 found_key.type < BTRFS_EXTENT_ITEM_KEY)
2978 break;
2980 return 1;
2984 * Search in extent tree to found next meta/data extent
2985 * Caller needs to check for no-hole or skinny metadata features.
2987 int btrfs_next_extent_item(struct btrfs_root *root,
2988 struct btrfs_path *path, u64 max_objectid)
2990 struct btrfs_key found_key;
2991 int ret;
2993 while (1) {
2994 ret = btrfs_next_item(root, path);
2995 if (ret)
2996 return ret;
2997 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2998 path->slots[0]);
2999 if (found_key.objectid > max_objectid)
3000 return 1;
3001 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3002 found_key.type == BTRFS_METADATA_ITEM_KEY)
3003 return 0;