Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / ctree.c
blobd8a6883aa85fcdb9d7dabc0c3b166194987799fc
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 static void root_add_used(struct btrfs_root *root, u32 size)
81 btrfs_set_root_used(&root->root_item,
82 btrfs_root_used(&root->root_item) + size);
85 static void root_sub_used(struct btrfs_root *root, u32 size)
87 btrfs_set_root_used(&root->root_item,
88 btrfs_root_used(&root->root_item) - size);
91 int btrfs_copy_root(struct btrfs_trans_handle *trans,
92 struct btrfs_root *root,
93 struct extent_buffer *buf,
94 struct extent_buffer **cow_ret, u64 new_root_objectid)
96 struct extent_buffer *cow;
97 int ret = 0;
98 int level;
99 struct btrfs_root *new_root;
100 struct btrfs_disk_key disk_key;
102 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
103 if (!new_root)
104 return -ENOMEM;
106 memcpy(new_root, root, sizeof(*new_root));
107 new_root->root_key.objectid = new_root_objectid;
109 WARN_ON(root->ref_cows && trans->transid !=
110 root->fs_info->running_transaction->transid);
111 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
113 level = btrfs_header_level(buf);
114 if (level == 0)
115 btrfs_item_key(buf, &disk_key, 0);
116 else
117 btrfs_node_key(buf, &disk_key, 0);
118 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
119 new_root_objectid, &disk_key,
120 level, buf->start, 0);
121 if (IS_ERR(cow)) {
122 kfree(new_root);
123 return PTR_ERR(cow);
126 copy_extent_buffer(cow, buf, 0, 0, cow->len);
127 btrfs_set_header_bytenr(cow, cow->start);
128 btrfs_set_header_generation(cow, trans->transid);
129 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
130 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
131 BTRFS_HEADER_FLAG_RELOC);
132 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
133 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
134 else
135 btrfs_set_header_owner(cow, new_root_objectid);
137 write_extent_buffer(cow, root->fs_info->fsid,
138 btrfs_header_fsid(), BTRFS_FSID_SIZE);
140 WARN_ON(btrfs_header_generation(buf) > trans->transid);
141 ret = btrfs_inc_ref(trans, new_root, cow, 0);
142 kfree(new_root);
144 if (ret)
145 return ret;
147 btrfs_mark_buffer_dirty(cow);
148 *cow_ret = cow;
149 return 0;
153 * check if the tree block can be shared by multiple trees
155 static int btrfs_block_can_be_shared(struct btrfs_root *root,
156 struct extent_buffer *buf)
159 * Tree blocks not in reference counted trees and tree roots
160 * are never shared. If a block was allocated after the last
161 * snapshot and the block was not allocated by tree relocation,
162 * we know the block is not shared.
164 if (root->ref_cows &&
165 buf != root->node && buf != root->commit_root &&
166 (btrfs_header_generation(buf) <=
167 btrfs_root_last_snapshot(&root->root_item) ||
168 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
169 return 1;
170 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
171 if (root->ref_cows &&
172 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
173 return 1;
174 #endif
175 return 0;
178 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
179 struct btrfs_root *root,
180 struct extent_buffer *buf,
181 struct extent_buffer *cow)
183 u64 refs;
184 u64 owner;
185 u64 flags;
186 u64 new_flags = 0;
187 int ret;
190 * Backrefs update rules:
192 * Always use full backrefs for extent pointers in tree block
193 * allocated by tree relocation.
195 * If a shared tree block is no longer referenced by its owner
196 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
197 * use full backrefs for extent pointers in tree block.
199 * If a tree block is been relocating
200 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
201 * use full backrefs for extent pointers in tree block.
202 * The reason for this is some operations (such as drop tree)
203 * are only allowed for blocks use full backrefs.
206 if (btrfs_block_can_be_shared(root, buf)) {
207 ret = btrfs_lookup_extent_info(trans, trans->fs_info,
208 buf->start,
209 btrfs_header_level(buf), 1,
210 &refs, &flags);
211 BUG_ON(ret);
212 BUG_ON(refs == 0);
213 } else {
214 refs = 1;
215 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
216 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
217 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
218 else
219 flags = 0;
222 owner = btrfs_header_owner(buf);
223 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
224 owner == BTRFS_TREE_RELOC_OBJECTID);
226 if (refs > 1) {
227 if ((owner == root->root_key.objectid ||
228 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
229 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
230 ret = btrfs_inc_ref(trans, root, buf, 1);
231 BUG_ON(ret);
233 if (root->root_key.objectid ==
234 BTRFS_TREE_RELOC_OBJECTID) {
235 ret = btrfs_dec_ref(trans, root, buf, 0);
236 BUG_ON(ret);
237 ret = btrfs_inc_ref(trans, root, cow, 1);
238 BUG_ON(ret);
240 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
241 } else {
243 if (root->root_key.objectid ==
244 BTRFS_TREE_RELOC_OBJECTID)
245 ret = btrfs_inc_ref(trans, root, cow, 1);
246 else
247 ret = btrfs_inc_ref(trans, root, cow, 0);
248 BUG_ON(ret);
250 if (new_flags != 0) {
251 ret = btrfs_set_block_flags(trans, buf->start,
252 btrfs_header_level(buf),
253 new_flags);
254 BUG_ON(ret);
256 } else {
257 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
258 if (root->root_key.objectid ==
259 BTRFS_TREE_RELOC_OBJECTID)
260 ret = btrfs_inc_ref(trans, root, cow, 1);
261 else
262 ret = btrfs_inc_ref(trans, root, cow, 0);
263 BUG_ON(ret);
264 ret = btrfs_dec_ref(trans, root, buf, 1);
265 BUG_ON(ret);
267 clean_tree_block(buf);
269 return 0;
272 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
273 struct btrfs_root *root,
274 struct extent_buffer *buf,
275 struct extent_buffer *parent, int parent_slot,
276 struct extent_buffer **cow_ret,
277 u64 search_start, u64 empty_size)
279 struct extent_buffer *cow;
280 struct btrfs_disk_key disk_key;
281 int level;
283 WARN_ON(root->ref_cows && trans->transid !=
284 root->fs_info->running_transaction->transid);
285 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
287 level = btrfs_header_level(buf);
289 if (level == 0)
290 btrfs_item_key(buf, &disk_key, 0);
291 else
292 btrfs_node_key(buf, &disk_key, 0);
294 cow = btrfs_alloc_free_block(trans, root, buf->len,
295 root->root_key.objectid, &disk_key,
296 level, search_start, empty_size);
297 if (IS_ERR(cow))
298 return PTR_ERR(cow);
300 copy_extent_buffer(cow, buf, 0, 0, cow->len);
301 btrfs_set_header_bytenr(cow, cow->start);
302 btrfs_set_header_generation(cow, trans->transid);
303 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
304 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
305 BTRFS_HEADER_FLAG_RELOC);
306 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
307 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
308 else
309 btrfs_set_header_owner(cow, root->root_key.objectid);
311 write_extent_buffer(cow, root->fs_info->fsid,
312 btrfs_header_fsid(), BTRFS_FSID_SIZE);
314 WARN_ON(!(buf->flags & EXTENT_BAD_TRANSID) &&
315 btrfs_header_generation(buf) > trans->transid);
317 update_ref_for_cow(trans, root, buf, cow);
319 if (buf == root->node) {
320 root->node = cow;
321 extent_buffer_get(cow);
323 btrfs_free_extent(trans, root, buf->start, buf->len,
324 0, root->root_key.objectid, level, 0);
325 free_extent_buffer(buf);
326 add_root_to_dirty_list(root);
327 } else {
328 btrfs_set_node_blockptr(parent, parent_slot,
329 cow->start);
330 WARN_ON(trans->transid == 0);
331 btrfs_set_node_ptr_generation(parent, parent_slot,
332 trans->transid);
333 btrfs_mark_buffer_dirty(parent);
334 WARN_ON(btrfs_header_generation(parent) != trans->transid);
336 btrfs_free_extent(trans, root, buf->start, buf->len,
337 0, root->root_key.objectid, level, 0);
339 if (!list_empty(&buf->recow)) {
340 list_del_init(&buf->recow);
341 free_extent_buffer(buf);
343 free_extent_buffer(buf);
344 btrfs_mark_buffer_dirty(cow);
345 *cow_ret = cow;
346 return 0;
349 static inline int should_cow_block(struct btrfs_trans_handle *trans,
350 struct btrfs_root *root,
351 struct extent_buffer *buf)
353 if (btrfs_header_generation(buf) == trans->transid &&
354 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
355 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
356 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
357 return 0;
358 return 1;
361 int btrfs_cow_block(struct btrfs_trans_handle *trans,
362 struct btrfs_root *root, struct extent_buffer *buf,
363 struct extent_buffer *parent, int parent_slot,
364 struct extent_buffer **cow_ret)
366 u64 search_start;
367 int ret;
369 if (trans->transaction != root->fs_info->running_transaction) {
370 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
371 root->fs_info->running_transaction->transid);
372 WARN_ON(1);
375 if (trans->transid != root->fs_info->generation) {
376 printk(KERN_CRIT "trans %llu running %llu\n",
377 (unsigned long long)trans->transid,
378 (unsigned long long)root->fs_info->generation);
379 WARN_ON(1);
381 if (!should_cow_block(trans, root, buf)) {
382 *cow_ret = buf;
383 return 0;
386 search_start = buf->start & ~((u64)SZ_1G - 1);
387 ret = __btrfs_cow_block(trans, root, buf, parent,
388 parent_slot, cow_ret, search_start, 0);
389 return ret;
392 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
394 if (k1->objectid > k2->objectid)
395 return 1;
396 if (k1->objectid < k2->objectid)
397 return -1;
398 if (k1->type > k2->type)
399 return 1;
400 if (k1->type < k2->type)
401 return -1;
402 if (k1->offset > k2->offset)
403 return 1;
404 if (k1->offset < k2->offset)
405 return -1;
406 return 0;
410 * compare two keys in a memcmp fashion
412 static int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
414 struct btrfs_key k1;
416 btrfs_disk_key_to_cpu(&k1, disk);
417 return btrfs_comp_cpu_keys(&k1, k2);
421 * The leaf data grows from end-to-front in the node.
422 * this returns the address of the start of the last item,
423 * which is the stop of the leaf data stack
425 static inline unsigned int leaf_data_end(const struct btrfs_fs_info *fs_info,
426 const struct extent_buffer *leaf)
428 u32 nr = btrfs_header_nritems(leaf);
429 if (nr == 0)
430 return BTRFS_LEAF_DATA_SIZE(fs_info);
431 return btrfs_item_offset_nr(leaf, nr - 1);
434 enum btrfs_tree_block_status
435 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
436 struct extent_buffer *buf)
438 int i;
439 struct btrfs_key cpukey;
440 struct btrfs_disk_key key;
441 u32 nritems = btrfs_header_nritems(buf);
442 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
444 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
445 goto fail;
447 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
448 if (parent_key && parent_key->type) {
449 btrfs_node_key(buf, &key, 0);
450 if (memcmp(parent_key, &key, sizeof(key)))
451 goto fail;
453 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
454 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
455 btrfs_node_key(buf, &key, i);
456 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
457 if (btrfs_comp_keys(&key, &cpukey) >= 0)
458 goto fail;
460 return BTRFS_TREE_BLOCK_CLEAN;
461 fail:
462 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
463 if (parent_key)
464 btrfs_disk_key_to_cpu(&cpukey, parent_key);
465 else
466 btrfs_node_key_to_cpu(buf, &cpukey, 0);
467 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
468 buf->start, buf->len,
469 btrfs_header_level(buf));
471 return ret;
474 enum btrfs_tree_block_status
475 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
476 struct extent_buffer *buf)
478 int i;
479 struct btrfs_key cpukey;
480 struct btrfs_disk_key key;
481 u32 nritems = btrfs_header_nritems(buf);
482 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
484 if (nritems * sizeof(struct btrfs_item) > buf->len) {
485 fprintf(stderr, "invalid number of items %llu\n",
486 (unsigned long long)buf->start);
487 goto fail;
490 if (btrfs_header_level(buf) != 0) {
491 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
492 fprintf(stderr, "leaf is not a leaf %llu\n",
493 (unsigned long long)btrfs_header_bytenr(buf));
494 goto fail;
496 if (btrfs_leaf_free_space(buf) < 0) {
497 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
498 fprintf(stderr, "leaf free space incorrect %llu %d\n",
499 (unsigned long long)btrfs_header_bytenr(buf),
500 btrfs_leaf_free_space(buf));
501 goto fail;
504 if (nritems == 0)
505 return BTRFS_TREE_BLOCK_CLEAN;
507 btrfs_item_key(buf, &key, 0);
508 if (parent_key && parent_key->type &&
509 memcmp(parent_key, &key, sizeof(key))) {
510 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
511 fprintf(stderr, "leaf parent key incorrect %llu\n",
512 (unsigned long long)btrfs_header_bytenr(buf));
513 goto fail;
515 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
516 btrfs_item_key(buf, &key, i);
517 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
518 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
519 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
520 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
521 goto fail;
523 if (btrfs_item_offset_nr(buf, i) !=
524 btrfs_item_end_nr(buf, i + 1)) {
525 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
526 fprintf(stderr, "incorrect offsets %u %u\n",
527 btrfs_item_offset_nr(buf, i),
528 btrfs_item_end_nr(buf, i + 1));
529 goto fail;
531 if (i == 0 && btrfs_item_end_nr(buf, i) !=
532 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
533 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
534 fprintf(stderr, "bad item end %u wanted %u\n",
535 btrfs_item_end_nr(buf, i),
536 (unsigned)BTRFS_LEAF_DATA_SIZE(root->fs_info));
537 goto fail;
541 for (i = 0; i < nritems; i++) {
542 if (btrfs_item_end_nr(buf, i) >
543 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
544 btrfs_item_key(buf, &key, 0);
545 btrfs_print_key(&key);
546 fflush(stdout);
547 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
548 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
549 (unsigned long long)btrfs_item_end_nr(buf, i),
550 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
551 root->fs_info));
552 goto fail;
556 return BTRFS_TREE_BLOCK_CLEAN;
557 fail:
558 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
559 if (parent_key)
560 btrfs_disk_key_to_cpu(&cpukey, parent_key);
561 else
562 btrfs_item_key_to_cpu(buf, &cpukey, 0);
564 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
565 buf->start, buf->len, 0);
567 return ret;
570 static int noinline check_block(struct btrfs_root *root,
571 struct btrfs_path *path, int level)
573 struct btrfs_disk_key key;
574 struct btrfs_disk_key *key_ptr = NULL;
575 struct extent_buffer *parent;
576 enum btrfs_tree_block_status ret;
578 if (path->skip_check_block)
579 return 0;
580 if (path->nodes[level + 1]) {
581 parent = path->nodes[level + 1];
582 btrfs_node_key(parent, &key, path->slots[level + 1]);
583 key_ptr = &key;
585 if (level == 0)
586 ret = btrfs_check_leaf(root, key_ptr, path->nodes[0]);
587 else
588 ret = btrfs_check_node(root, key_ptr, path->nodes[level]);
589 if (ret == BTRFS_TREE_BLOCK_CLEAN)
590 return 0;
591 return -EIO;
595 * search for key in the extent_buffer. The items start at offset p,
596 * and they are item_size apart. There are 'max' items in p.
598 * the slot in the array is returned via slot, and it points to
599 * the place where you would insert key if it is not found in
600 * the array.
602 * slot may point to max if the key is bigger than all of the keys
604 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
605 int item_size, struct btrfs_key *key,
606 int max, int *slot)
608 int low = 0;
609 int high = max;
610 int mid;
611 int ret;
612 unsigned long offset;
613 struct btrfs_disk_key *tmp;
615 while(low < high) {
616 mid = (low + high) / 2;
617 offset = p + mid * item_size;
619 tmp = (struct btrfs_disk_key *)(eb->data + offset);
620 ret = btrfs_comp_keys(tmp, key);
622 if (ret < 0)
623 low = mid + 1;
624 else if (ret > 0)
625 high = mid;
626 else {
627 *slot = mid;
628 return 0;
631 *slot = low;
632 return 1;
636 * simple bin_search frontend that does the right thing for
637 * leaves vs nodes
639 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
640 int level, int *slot)
642 if (level == 0)
643 return generic_bin_search(eb,
644 offsetof(struct btrfs_leaf, items),
645 sizeof(struct btrfs_item),
646 key, btrfs_header_nritems(eb),
647 slot);
648 else
649 return generic_bin_search(eb,
650 offsetof(struct btrfs_node, ptrs),
651 sizeof(struct btrfs_key_ptr),
652 key, btrfs_header_nritems(eb),
653 slot);
656 struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
657 struct extent_buffer *parent, int slot)
659 struct extent_buffer *ret;
660 int level = btrfs_header_level(parent);
662 if (slot < 0)
663 return NULL;
664 if (slot >= btrfs_header_nritems(parent))
665 return NULL;
667 if (level == 0)
668 return NULL;
670 ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
671 btrfs_node_ptr_generation(parent, slot));
672 if (!extent_buffer_uptodate(ret))
673 return ERR_PTR(-EIO);
675 if (btrfs_header_level(ret) != level - 1) {
676 error(
677 "child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
678 btrfs_header_bytenr(parent), slot,
679 btrfs_header_level(parent), btrfs_header_level(ret));
680 free_extent_buffer(ret);
681 return ERR_PTR(-EIO);
683 return ret;
686 static int balance_level(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root,
688 struct btrfs_path *path, int level)
690 struct extent_buffer *right = NULL;
691 struct extent_buffer *mid;
692 struct extent_buffer *left = NULL;
693 struct extent_buffer *parent = NULL;
694 struct btrfs_fs_info *fs_info = root->fs_info;
695 int ret = 0;
696 int wret;
697 int pslot;
698 int orig_slot = path->slots[level];
699 u64 orig_ptr;
701 if (level == 0)
702 return 0;
704 mid = path->nodes[level];
705 WARN_ON(btrfs_header_generation(mid) != trans->transid);
707 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
709 if (level < BTRFS_MAX_LEVEL - 1) {
710 parent = path->nodes[level + 1];
711 pslot = path->slots[level + 1];
715 * deal with the case where there is only one pointer in the root
716 * by promoting the node below to a root
718 if (!parent) {
719 struct extent_buffer *child;
721 if (btrfs_header_nritems(mid) != 1)
722 return 0;
724 /* promote the child to a root */
725 child = read_node_slot(fs_info, mid, 0);
726 BUG_ON(!extent_buffer_uptodate(child));
727 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
728 BUG_ON(ret);
730 root->node = child;
731 add_root_to_dirty_list(root);
732 path->nodes[level] = NULL;
733 clean_tree_block(mid);
734 /* once for the path */
735 free_extent_buffer(mid);
737 root_sub_used(root, mid->len);
739 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
740 0, root->root_key.objectid,
741 level, 0);
742 /* once for the root ptr */
743 free_extent_buffer(mid);
744 return ret;
746 if (btrfs_header_nritems(mid) >
747 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
748 return 0;
750 left = read_node_slot(fs_info, parent, pslot - 1);
751 if (extent_buffer_uptodate(left)) {
752 wret = btrfs_cow_block(trans, root, left,
753 parent, pslot - 1, &left);
754 if (wret) {
755 ret = wret;
756 goto enospc;
759 right = read_node_slot(fs_info, parent, pslot + 1);
760 if (extent_buffer_uptodate(right)) {
761 wret = btrfs_cow_block(trans, root, right,
762 parent, pslot + 1, &right);
763 if (wret) {
764 ret = wret;
765 goto enospc;
769 /* first, try to make some room in the middle buffer */
770 if (left) {
771 orig_slot += btrfs_header_nritems(left);
772 wret = push_node_left(trans, root, left, mid, 1);
773 if (wret < 0)
774 ret = wret;
778 * then try to empty the right most buffer into the middle
780 if (right) {
781 wret = push_node_left(trans, root, mid, right, 1);
782 if (wret < 0 && wret != -ENOSPC)
783 ret = wret;
784 if (btrfs_header_nritems(right) == 0) {
785 u64 bytenr = right->start;
786 u32 blocksize = right->len;
788 clean_tree_block(right);
789 free_extent_buffer(right);
790 right = NULL;
791 wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
792 if (wret)
793 ret = wret;
795 root_sub_used(root, right->len);
796 wret = btrfs_free_extent(trans, root, bytenr,
797 blocksize, 0,
798 root->root_key.objectid,
799 level, 0);
800 if (wret)
801 ret = wret;
802 } else {
803 struct btrfs_disk_key right_key;
804 btrfs_node_key(right, &right_key, 0);
805 btrfs_set_node_key(parent, &right_key, pslot + 1);
806 btrfs_mark_buffer_dirty(parent);
809 if (btrfs_header_nritems(mid) == 1) {
811 * we're not allowed to leave a node with one item in the
812 * tree during a delete. A deletion from lower in the tree
813 * could try to delete the only pointer in this node.
814 * So, pull some keys from the left.
815 * There has to be a left pointer at this point because
816 * otherwise we would have pulled some pointers from the
817 * right
819 BUG_ON(!left);
820 wret = balance_node_right(trans, root, mid, left);
821 if (wret < 0) {
822 ret = wret;
823 goto enospc;
825 if (wret == 1) {
826 wret = push_node_left(trans, root, left, mid, 1);
827 if (wret < 0)
828 ret = wret;
830 BUG_ON(wret == 1);
832 if (btrfs_header_nritems(mid) == 0) {
833 /* we've managed to empty the middle node, drop it */
834 u64 bytenr = mid->start;
835 u32 blocksize = mid->len;
836 clean_tree_block(mid);
837 free_extent_buffer(mid);
838 mid = NULL;
839 wret = btrfs_del_ptr(root, path, level + 1, pslot);
840 if (wret)
841 ret = wret;
843 root_sub_used(root, blocksize);
844 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
845 0, root->root_key.objectid,
846 level, 0);
847 if (wret)
848 ret = wret;
849 } else {
850 /* update the parent key to reflect our changes */
851 struct btrfs_disk_key mid_key;
852 btrfs_node_key(mid, &mid_key, 0);
853 btrfs_set_node_key(parent, &mid_key, pslot);
854 btrfs_mark_buffer_dirty(parent);
857 /* update the path */
858 if (left) {
859 if (btrfs_header_nritems(left) > orig_slot) {
860 extent_buffer_get(left);
861 path->nodes[level] = left;
862 path->slots[level + 1] -= 1;
863 path->slots[level] = orig_slot;
864 if (mid)
865 free_extent_buffer(mid);
866 } else {
867 orig_slot -= btrfs_header_nritems(left);
868 path->slots[level] = orig_slot;
871 /* double check we haven't messed things up */
872 check_block(root, path, level);
873 if (orig_ptr !=
874 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
875 BUG();
876 enospc:
877 if (right)
878 free_extent_buffer(right);
879 if (left)
880 free_extent_buffer(left);
881 return ret;
884 /* returns zero if the push worked, non-zero otherwise */
885 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root,
887 struct btrfs_path *path, int level)
889 struct extent_buffer *right = NULL;
890 struct extent_buffer *mid;
891 struct extent_buffer *left = NULL;
892 struct extent_buffer *parent = NULL;
893 struct btrfs_fs_info *fs_info = root->fs_info;
894 int ret = 0;
895 int wret;
896 int pslot;
897 int orig_slot = path->slots[level];
899 if (level == 0)
900 return 1;
902 mid = path->nodes[level];
903 WARN_ON(btrfs_header_generation(mid) != trans->transid);
905 if (level < BTRFS_MAX_LEVEL - 1) {
906 parent = path->nodes[level + 1];
907 pslot = path->slots[level + 1];
910 if (!parent)
911 return 1;
913 left = read_node_slot(fs_info, parent, pslot - 1);
915 /* first, try to make some room in the middle buffer */
916 if (extent_buffer_uptodate(left)) {
917 u32 left_nr;
918 left_nr = btrfs_header_nritems(left);
919 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
920 wret = 1;
921 } else {
922 ret = btrfs_cow_block(trans, root, left, parent,
923 pslot - 1, &left);
924 if (ret)
925 wret = 1;
926 else {
927 wret = push_node_left(trans, root,
928 left, mid, 0);
931 if (wret < 0)
932 ret = wret;
933 if (wret == 0) {
934 struct btrfs_disk_key disk_key;
935 orig_slot += left_nr;
936 btrfs_node_key(mid, &disk_key, 0);
937 btrfs_set_node_key(parent, &disk_key, pslot);
938 btrfs_mark_buffer_dirty(parent);
939 if (btrfs_header_nritems(left) > orig_slot) {
940 path->nodes[level] = left;
941 path->slots[level + 1] -= 1;
942 path->slots[level] = orig_slot;
943 free_extent_buffer(mid);
944 } else {
945 orig_slot -=
946 btrfs_header_nritems(left);
947 path->slots[level] = orig_slot;
948 free_extent_buffer(left);
950 return 0;
952 free_extent_buffer(left);
954 right= read_node_slot(fs_info, parent, pslot + 1);
957 * then try to empty the right most buffer into the middle
959 if (extent_buffer_uptodate(right)) {
960 u32 right_nr;
961 right_nr = btrfs_header_nritems(right);
962 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 1) {
963 wret = 1;
964 } else {
965 ret = btrfs_cow_block(trans, root, right,
966 parent, pslot + 1,
967 &right);
968 if (ret)
969 wret = 1;
970 else {
971 wret = balance_node_right(trans, root,
972 right, mid);
975 if (wret < 0)
976 ret = wret;
977 if (wret == 0) {
978 struct btrfs_disk_key disk_key;
980 btrfs_node_key(right, &disk_key, 0);
981 btrfs_set_node_key(parent, &disk_key, pslot + 1);
982 btrfs_mark_buffer_dirty(parent);
984 if (btrfs_header_nritems(mid) <= orig_slot) {
985 path->nodes[level] = right;
986 path->slots[level + 1] += 1;
987 path->slots[level] = orig_slot -
988 btrfs_header_nritems(mid);
989 free_extent_buffer(mid);
990 } else {
991 free_extent_buffer(right);
993 return 0;
995 free_extent_buffer(right);
997 return 1;
1001 * readahead one full node of leaves
1003 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
1004 int level, int slot, u64 objectid)
1006 struct btrfs_fs_info *fs_info = root->fs_info;
1007 struct extent_buffer *node;
1008 struct btrfs_disk_key disk_key;
1009 u32 nritems;
1010 u64 search;
1011 u64 lowest_read;
1012 u64 highest_read;
1013 u64 nread = 0;
1014 int direction = path->reada;
1015 struct extent_buffer *eb;
1016 u32 nr;
1017 u32 nscan = 0;
1019 if (level != 1)
1020 return;
1022 if (!path->nodes[level])
1023 return;
1025 node = path->nodes[level];
1026 search = btrfs_node_blockptr(node, slot);
1027 eb = btrfs_find_tree_block(fs_info, search, fs_info->nodesize);
1028 if (eb) {
1029 free_extent_buffer(eb);
1030 return;
1033 highest_read = search;
1034 lowest_read = search;
1036 nritems = btrfs_header_nritems(node);
1037 nr = slot;
1038 while(1) {
1039 if (direction < 0) {
1040 if (nr == 0)
1041 break;
1042 nr--;
1043 } else if (direction > 0) {
1044 nr++;
1045 if (nr >= nritems)
1046 break;
1048 if (path->reada < 0 && objectid) {
1049 btrfs_node_key(node, &disk_key, nr);
1050 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1051 break;
1053 search = btrfs_node_blockptr(node, nr);
1054 if ((search >= lowest_read && search <= highest_read) ||
1055 (search < lowest_read && lowest_read - search <= 32768) ||
1056 (search > highest_read && search - highest_read <= 32768)) {
1057 readahead_tree_block(fs_info, search,
1058 btrfs_node_ptr_generation(node, nr));
1059 nread += fs_info->nodesize;
1061 nscan++;
1062 if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
1063 break;
1064 if(nread > SZ_1M || nscan > 128)
1065 break;
1067 if (search < lowest_read)
1068 lowest_read = search;
1069 if (search > highest_read)
1070 highest_read = search;
1074 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1075 u64 iobjectid, u64 ioff, u8 key_type,
1076 struct btrfs_key *found_key)
1078 int ret;
1079 struct btrfs_key key;
1080 struct extent_buffer *eb;
1081 struct btrfs_path *path;
1083 key.type = key_type;
1084 key.objectid = iobjectid;
1085 key.offset = ioff;
1087 if (found_path == NULL) {
1088 path = btrfs_alloc_path();
1089 if (!path)
1090 return -ENOMEM;
1091 } else
1092 path = found_path;
1094 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1095 if ((ret < 0) || (found_key == NULL))
1096 goto out;
1098 eb = path->nodes[0];
1099 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1100 ret = btrfs_next_leaf(fs_root, path);
1101 if (ret)
1102 goto out;
1103 eb = path->nodes[0];
1106 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1107 if (found_key->type != key.type ||
1108 found_key->objectid != key.objectid) {
1109 ret = 1;
1110 goto out;
1113 out:
1114 if (path != found_path)
1115 btrfs_free_path(path);
1116 return ret;
1120 * look for key in the tree. path is filled in with nodes along the way
1121 * if key is found, we return zero and you can find the item in the leaf
1122 * level of the path (level 0)
1124 * If the key isn't found, the path points to the slot where it should
1125 * be inserted, and 1 is returned. If there are other errors during the
1126 * search a negative error number is returned.
1128 * if ins_len > 0, nodes and leaves will be split as we walk down the
1129 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1130 * possible)
1132 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1133 *root, struct btrfs_key *key, struct btrfs_path *p, int
1134 ins_len, int cow)
1136 struct extent_buffer *b;
1137 int slot;
1138 int ret;
1139 int level;
1140 int should_reada = p->reada;
1141 struct btrfs_fs_info *fs_info = root->fs_info;
1142 u8 lowest_level = 0;
1144 lowest_level = p->lowest_level;
1145 WARN_ON(lowest_level && ins_len > 0);
1146 WARN_ON(p->nodes[0] != NULL);
1148 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1150 again:
1151 b = root->node;
1152 extent_buffer_get(b);
1153 while (b) {
1154 level = btrfs_header_level(b);
1155 if (cow) {
1156 int wret;
1157 wret = btrfs_cow_block(trans, root, b,
1158 p->nodes[level + 1],
1159 p->slots[level + 1],
1160 &b);
1161 if (wret) {
1162 free_extent_buffer(b);
1163 return wret;
1166 BUG_ON(!cow && ins_len);
1167 if (level != btrfs_header_level(b))
1168 WARN_ON(1);
1169 level = btrfs_header_level(b);
1170 p->nodes[level] = b;
1171 ret = check_block(root, p, level);
1172 if (ret)
1173 return -1;
1174 ret = bin_search(b, key, level, &slot);
1175 if (level != 0) {
1176 if (ret && slot > 0)
1177 slot -= 1;
1178 p->slots[level] = slot;
1179 if ((p->search_for_split || ins_len > 0) &&
1180 btrfs_header_nritems(b) >=
1181 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1182 int sret = split_node(trans, root, p, level);
1183 BUG_ON(sret > 0);
1184 if (sret)
1185 return sret;
1186 b = p->nodes[level];
1187 slot = p->slots[level];
1188 } else if (ins_len < 0) {
1189 int sret = balance_level(trans, root, p,
1190 level);
1191 if (sret)
1192 return sret;
1193 b = p->nodes[level];
1194 if (!b) {
1195 btrfs_release_path(p);
1196 goto again;
1198 slot = p->slots[level];
1199 BUG_ON(btrfs_header_nritems(b) == 1);
1201 /* this is only true while dropping a snapshot */
1202 if (level == lowest_level)
1203 break;
1205 if (should_reada)
1206 reada_for_search(root, p, level, slot,
1207 key->objectid);
1209 b = read_node_slot(fs_info, b, slot);
1210 if (!extent_buffer_uptodate(b))
1211 return -EIO;
1212 } else {
1213 p->slots[level] = slot;
1214 if (ins_len > 0 &&
1215 ins_len > btrfs_leaf_free_space(b)) {
1216 int sret = split_leaf(trans, root, key,
1217 p, ins_len, ret == 0);
1218 BUG_ON(sret > 0);
1219 if (sret)
1220 return sret;
1222 return ret;
1225 return 1;
1229 * adjust the pointers going up the tree, starting at level
1230 * making sure the right key of each node is points to 'key'.
1231 * This is used after shifting pointers to the left, so it stops
1232 * fixing up pointers when a given leaf/node is not in slot 0 of the
1233 * higher levels
1235 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
1236 struct btrfs_disk_key *key, int level)
1238 int i;
1239 struct extent_buffer *t;
1241 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1242 int tslot = path->slots[i];
1243 if (!path->nodes[i])
1244 break;
1245 t = path->nodes[i];
1246 btrfs_set_node_key(t, key, tslot);
1247 btrfs_mark_buffer_dirty(path->nodes[i]);
1248 if (tslot != 0)
1249 break;
1254 * update item key.
1256 * This function isn't completely safe. It's the caller's responsibility
1257 * that the new key won't break the order
1259 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
1260 struct btrfs_key *new_key)
1262 struct btrfs_disk_key disk_key;
1263 struct extent_buffer *eb;
1264 int slot;
1266 eb = path->nodes[0];
1267 slot = path->slots[0];
1268 if (slot > 0) {
1269 btrfs_item_key(eb, &disk_key, slot - 1);
1270 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1271 return -1;
1273 if (slot < btrfs_header_nritems(eb) - 1) {
1274 btrfs_item_key(eb, &disk_key, slot + 1);
1275 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1276 return -1;
1279 btrfs_cpu_key_to_disk(&disk_key, new_key);
1280 btrfs_set_item_key(eb, &disk_key, slot);
1281 btrfs_mark_buffer_dirty(eb);
1282 if (slot == 0)
1283 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1284 return 0;
1288 * update an item key without the safety checks. This is meant to be called by
1289 * fsck only.
1291 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
1292 struct btrfs_path *path,
1293 struct btrfs_key *new_key)
1295 struct btrfs_disk_key disk_key;
1296 struct extent_buffer *eb;
1297 int slot;
1299 eb = path->nodes[0];
1300 slot = path->slots[0];
1302 btrfs_cpu_key_to_disk(&disk_key, new_key);
1303 btrfs_set_item_key(eb, &disk_key, slot);
1304 btrfs_mark_buffer_dirty(eb);
1305 if (slot == 0)
1306 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1310 * try to push data from one node into the next node left in the
1311 * tree.
1313 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1314 * error, and > 0 if there was no room in the left hand block.
1316 static int push_node_left(struct btrfs_trans_handle *trans,
1317 struct btrfs_root *root, struct extent_buffer *dst,
1318 struct extent_buffer *src, int empty)
1320 int push_items = 0;
1321 int src_nritems;
1322 int dst_nritems;
1323 int ret = 0;
1325 src_nritems = btrfs_header_nritems(src);
1326 dst_nritems = btrfs_header_nritems(dst);
1327 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1328 WARN_ON(btrfs_header_generation(src) != trans->transid);
1329 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1331 if (!empty && src_nritems <= 8)
1332 return 1;
1334 if (push_items <= 0) {
1335 return 1;
1338 if (empty) {
1339 push_items = min(src_nritems, push_items);
1340 if (push_items < src_nritems) {
1341 /* leave at least 8 pointers in the node if
1342 * we aren't going to empty it
1344 if (src_nritems - push_items < 8) {
1345 if (push_items <= 8)
1346 return 1;
1347 push_items -= 8;
1350 } else
1351 push_items = min(src_nritems - 8, push_items);
1353 copy_extent_buffer(dst, src,
1354 btrfs_node_key_ptr_offset(dst_nritems),
1355 btrfs_node_key_ptr_offset(0),
1356 push_items * sizeof(struct btrfs_key_ptr));
1358 if (push_items < src_nritems) {
1359 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1360 btrfs_node_key_ptr_offset(push_items),
1361 (src_nritems - push_items) *
1362 sizeof(struct btrfs_key_ptr));
1364 btrfs_set_header_nritems(src, src_nritems - push_items);
1365 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1366 btrfs_mark_buffer_dirty(src);
1367 btrfs_mark_buffer_dirty(dst);
1369 return ret;
1373 * try to push data from one node into the next node right in the
1374 * tree.
1376 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1377 * error, and > 0 if there was no room in the right hand block.
1379 * this will only push up to 1/2 the contents of the left node over
1381 static int balance_node_right(struct btrfs_trans_handle *trans,
1382 struct btrfs_root *root,
1383 struct extent_buffer *dst,
1384 struct extent_buffer *src)
1386 int push_items = 0;
1387 int max_push;
1388 int src_nritems;
1389 int dst_nritems;
1390 int ret = 0;
1392 WARN_ON(btrfs_header_generation(src) != trans->transid);
1393 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1395 src_nritems = btrfs_header_nritems(src);
1396 dst_nritems = btrfs_header_nritems(dst);
1397 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1398 if (push_items <= 0) {
1399 return 1;
1402 if (src_nritems < 4) {
1403 return 1;
1406 max_push = src_nritems / 2 + 1;
1407 /* don't try to empty the node */
1408 if (max_push >= src_nritems) {
1409 return 1;
1412 if (max_push < push_items)
1413 push_items = max_push;
1415 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1416 btrfs_node_key_ptr_offset(0),
1417 (dst_nritems) *
1418 sizeof(struct btrfs_key_ptr));
1420 copy_extent_buffer(dst, src,
1421 btrfs_node_key_ptr_offset(0),
1422 btrfs_node_key_ptr_offset(src_nritems - push_items),
1423 push_items * sizeof(struct btrfs_key_ptr));
1425 btrfs_set_header_nritems(src, src_nritems - push_items);
1426 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1428 btrfs_mark_buffer_dirty(src);
1429 btrfs_mark_buffer_dirty(dst);
1431 return ret;
1435 * helper function to insert a new root level in the tree.
1436 * A new node is allocated, and a single item is inserted to
1437 * point to the existing root
1439 * returns zero on success or < 0 on failure.
1441 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1442 struct btrfs_root *root,
1443 struct btrfs_path *path, int level)
1445 u64 lower_gen;
1446 struct extent_buffer *lower;
1447 struct extent_buffer *c;
1448 struct extent_buffer *old;
1449 struct btrfs_disk_key lower_key;
1451 BUG_ON(path->nodes[level]);
1452 BUG_ON(path->nodes[level-1] != root->node);
1454 lower = path->nodes[level-1];
1455 if (level == 1)
1456 btrfs_item_key(lower, &lower_key, 0);
1457 else
1458 btrfs_node_key(lower, &lower_key, 0);
1460 c = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1461 root->root_key.objectid, &lower_key,
1462 level, root->node->start, 0);
1464 if (IS_ERR(c))
1465 return PTR_ERR(c);
1467 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1468 btrfs_set_header_nritems(c, 1);
1469 btrfs_set_header_level(c, level);
1470 btrfs_set_header_bytenr(c, c->start);
1471 btrfs_set_header_generation(c, trans->transid);
1472 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1473 btrfs_set_header_owner(c, root->root_key.objectid);
1475 root_add_used(root, root->fs_info->nodesize);
1477 write_extent_buffer(c, root->fs_info->fsid,
1478 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1480 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1481 btrfs_header_chunk_tree_uuid(c),
1482 BTRFS_UUID_SIZE);
1484 btrfs_set_node_key(c, &lower_key, 0);
1485 btrfs_set_node_blockptr(c, 0, lower->start);
1486 lower_gen = btrfs_header_generation(lower);
1487 WARN_ON(lower_gen != trans->transid);
1489 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1491 btrfs_mark_buffer_dirty(c);
1493 old = root->node;
1494 root->node = c;
1496 /* the super has an extra ref to root->node */
1497 free_extent_buffer(old);
1499 add_root_to_dirty_list(root);
1500 extent_buffer_get(c);
1501 path->nodes[level] = c;
1502 path->slots[level] = 0;
1503 return 0;
1507 * worker function to insert a single pointer in a node.
1508 * the node should have enough room for the pointer already
1510 * slot and level indicate where you want the key to go, and
1511 * blocknr is the block the key points to.
1513 * returns zero on success and < 0 on any error
1515 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1516 *root, struct btrfs_path *path, struct btrfs_disk_key
1517 *key, u64 bytenr, int slot, int level)
1519 struct extent_buffer *lower;
1520 int nritems;
1522 BUG_ON(!path->nodes[level]);
1523 lower = path->nodes[level];
1524 nritems = btrfs_header_nritems(lower);
1525 if (slot > nritems)
1526 BUG();
1527 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
1528 BUG();
1529 if (slot < nritems) {
1530 /* shift the items */
1531 memmove_extent_buffer(lower,
1532 btrfs_node_key_ptr_offset(slot + 1),
1533 btrfs_node_key_ptr_offset(slot),
1534 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1536 btrfs_set_node_key(lower, key, slot);
1537 btrfs_set_node_blockptr(lower, slot, bytenr);
1538 WARN_ON(trans->transid == 0);
1539 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1540 btrfs_set_header_nritems(lower, nritems + 1);
1541 btrfs_mark_buffer_dirty(lower);
1542 return 0;
1546 * split the node at the specified level in path in two.
1547 * The path is corrected to point to the appropriate node after the split
1549 * Before splitting this tries to make some room in the node by pushing
1550 * left and right, if either one works, it returns right away.
1552 * returns 0 on success and < 0 on failure
1554 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1555 *root, struct btrfs_path *path, int level)
1557 struct extent_buffer *c;
1558 struct extent_buffer *split;
1559 struct btrfs_disk_key disk_key;
1560 int mid;
1561 int ret;
1562 int wret;
1563 u32 c_nritems;
1565 c = path->nodes[level];
1566 WARN_ON(btrfs_header_generation(c) != trans->transid);
1567 if (c == root->node) {
1568 /* trying to split the root, lets make a new one */
1569 ret = insert_new_root(trans, root, path, level + 1);
1570 if (ret)
1571 return ret;
1572 } else {
1573 ret = push_nodes_for_insert(trans, root, path, level);
1574 c = path->nodes[level];
1575 if (!ret && btrfs_header_nritems(c) <
1576 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 3)
1577 return 0;
1578 if (ret < 0)
1579 return ret;
1582 c_nritems = btrfs_header_nritems(c);
1583 mid = (c_nritems + 1) / 2;
1584 btrfs_node_key(c, &disk_key, mid);
1586 split = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1587 root->root_key.objectid,
1588 &disk_key, level, c->start, 0);
1589 if (IS_ERR(split))
1590 return PTR_ERR(split);
1592 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1593 btrfs_set_header_level(split, btrfs_header_level(c));
1594 btrfs_set_header_bytenr(split, split->start);
1595 btrfs_set_header_generation(split, trans->transid);
1596 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1597 btrfs_set_header_owner(split, root->root_key.objectid);
1598 write_extent_buffer(split, root->fs_info->fsid,
1599 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1600 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1601 btrfs_header_chunk_tree_uuid(split),
1602 BTRFS_UUID_SIZE);
1604 root_add_used(root, root->fs_info->nodesize);
1606 copy_extent_buffer(split, c,
1607 btrfs_node_key_ptr_offset(0),
1608 btrfs_node_key_ptr_offset(mid),
1609 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1610 btrfs_set_header_nritems(split, c_nritems - mid);
1611 btrfs_set_header_nritems(c, mid);
1612 ret = 0;
1614 btrfs_mark_buffer_dirty(c);
1615 btrfs_mark_buffer_dirty(split);
1617 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1618 path->slots[level + 1] + 1,
1619 level + 1);
1620 if (wret)
1621 ret = wret;
1623 if (path->slots[level] >= mid) {
1624 path->slots[level] -= mid;
1625 free_extent_buffer(c);
1626 path->nodes[level] = split;
1627 path->slots[level + 1] += 1;
1628 } else {
1629 free_extent_buffer(split);
1631 return ret;
1635 * how many bytes are required to store the items in a leaf. start
1636 * and nr indicate which items in the leaf to check. This totals up the
1637 * space used both by the item structs and the item data
1639 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1641 int data_len;
1642 int nritems = btrfs_header_nritems(l);
1643 int end = min(nritems, start + nr) - 1;
1645 if (!nr)
1646 return 0;
1647 data_len = btrfs_item_end_nr(l, start);
1648 data_len = data_len - btrfs_item_offset_nr(l, end);
1649 data_len += sizeof(struct btrfs_item) * nr;
1650 WARN_ON(data_len < 0);
1651 return data_len;
1655 * The space between the end of the leaf items and
1656 * the start of the leaf data. IOW, how much room
1657 * the leaf has left for both items and data
1659 int btrfs_leaf_free_space(struct extent_buffer *leaf)
1661 int nritems = btrfs_header_nritems(leaf);
1662 u32 leaf_data_size;
1663 int ret;
1665 BUG_ON(leaf->fs_info && leaf->fs_info->nodesize != leaf->len);
1666 leaf_data_size = __BTRFS_LEAF_DATA_SIZE(leaf->len);
1667 ret = leaf_data_size - leaf_space_used(leaf, 0 ,nritems);
1668 if (ret < 0) {
1669 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1670 ret, leaf_data_size, leaf_space_used(leaf, 0, nritems),
1671 nritems);
1673 return ret;
1677 * push some data in the path leaf to the right, trying to free up at
1678 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1680 * returns 1 if the push failed because the other node didn't have enough
1681 * room, 0 if everything worked out and < 0 if there were major errors.
1683 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1684 *root, struct btrfs_path *path, int data_size,
1685 int empty)
1687 struct extent_buffer *left = path->nodes[0];
1688 struct extent_buffer *right;
1689 struct extent_buffer *upper;
1690 struct btrfs_disk_key disk_key;
1691 struct btrfs_fs_info *fs_info = root->fs_info;
1692 int slot;
1693 u32 i;
1694 int free_space;
1695 int push_space = 0;
1696 int push_items = 0;
1697 struct btrfs_item *item;
1698 u32 left_nritems;
1699 u32 nr;
1700 u32 right_nritems;
1701 u32 data_end;
1702 u32 this_item_size;
1703 int ret;
1705 slot = path->slots[1];
1706 if (!path->nodes[1]) {
1707 return 1;
1709 upper = path->nodes[1];
1710 if (slot >= btrfs_header_nritems(upper) - 1)
1711 return 1;
1713 right = read_node_slot(fs_info, upper, slot + 1);
1714 if (!extent_buffer_uptodate(right)) {
1715 if (IS_ERR(right))
1716 return PTR_ERR(right);
1717 return -EIO;
1719 free_space = btrfs_leaf_free_space(right);
1720 if (free_space < data_size) {
1721 free_extent_buffer(right);
1722 return 1;
1725 /* cow and double check */
1726 ret = btrfs_cow_block(trans, root, right, upper,
1727 slot + 1, &right);
1728 if (ret) {
1729 free_extent_buffer(right);
1730 return 1;
1732 free_space = btrfs_leaf_free_space(right);
1733 if (free_space < data_size) {
1734 free_extent_buffer(right);
1735 return 1;
1738 left_nritems = btrfs_header_nritems(left);
1739 if (left_nritems == 0) {
1740 free_extent_buffer(right);
1741 return 1;
1744 if (empty)
1745 nr = 0;
1746 else
1747 nr = 1;
1749 i = left_nritems - 1;
1750 while (i >= nr) {
1751 item = btrfs_item_nr(i);
1753 if (path->slots[0] == i)
1754 push_space += data_size + sizeof(*item);
1756 this_item_size = btrfs_item_size(left, item);
1757 if (this_item_size + sizeof(*item) + push_space > free_space)
1758 break;
1759 push_items++;
1760 push_space += this_item_size + sizeof(*item);
1761 if (i == 0)
1762 break;
1763 i--;
1766 if (push_items == 0) {
1767 free_extent_buffer(right);
1768 return 1;
1771 if (!empty && push_items == left_nritems)
1772 WARN_ON(1);
1774 /* push left to right */
1775 right_nritems = btrfs_header_nritems(right);
1777 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1778 push_space -= leaf_data_end(fs_info, left);
1780 /* make room in the right data area */
1781 data_end = leaf_data_end(fs_info, right);
1782 memmove_extent_buffer(right,
1783 btrfs_leaf_data(right) + data_end - push_space,
1784 btrfs_leaf_data(right) + data_end,
1785 BTRFS_LEAF_DATA_SIZE(root->fs_info) - data_end);
1787 /* copy from the left data area */
1788 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1789 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space,
1790 btrfs_leaf_data(left) + leaf_data_end(fs_info, left),
1791 push_space);
1793 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1794 btrfs_item_nr_offset(0),
1795 right_nritems * sizeof(struct btrfs_item));
1797 /* copy the items from left to right */
1798 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1799 btrfs_item_nr_offset(left_nritems - push_items),
1800 push_items * sizeof(struct btrfs_item));
1802 /* update the item pointers */
1803 right_nritems += push_items;
1804 btrfs_set_header_nritems(right, right_nritems);
1805 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1806 for (i = 0; i < right_nritems; i++) {
1807 item = btrfs_item_nr(i);
1808 push_space -= btrfs_item_size(right, item);
1809 btrfs_set_item_offset(right, item, push_space);
1812 left_nritems -= push_items;
1813 btrfs_set_header_nritems(left, left_nritems);
1815 if (left_nritems)
1816 btrfs_mark_buffer_dirty(left);
1817 btrfs_mark_buffer_dirty(right);
1819 btrfs_item_key(right, &disk_key, 0);
1820 btrfs_set_node_key(upper, &disk_key, slot + 1);
1821 btrfs_mark_buffer_dirty(upper);
1823 /* then fixup the leaf pointer in the path */
1824 if (path->slots[0] >= left_nritems) {
1825 path->slots[0] -= left_nritems;
1826 free_extent_buffer(path->nodes[0]);
1827 path->nodes[0] = right;
1828 path->slots[1] += 1;
1829 } else {
1830 free_extent_buffer(right);
1832 return 0;
1835 * push some data in the path leaf to the left, trying to free up at
1836 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1838 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1839 *root, struct btrfs_path *path, int data_size,
1840 int empty)
1842 struct btrfs_disk_key disk_key;
1843 struct extent_buffer *right = path->nodes[0];
1844 struct extent_buffer *left;
1845 struct btrfs_fs_info *fs_info = root->fs_info;
1846 int slot;
1847 int i;
1848 int free_space;
1849 int push_space = 0;
1850 int push_items = 0;
1851 struct btrfs_item *item;
1852 u32 old_left_nritems;
1853 u32 right_nritems;
1854 u32 nr;
1855 int ret = 0;
1856 u32 this_item_size;
1857 u32 old_left_item_size;
1859 slot = path->slots[1];
1860 if (slot == 0)
1861 return 1;
1862 if (!path->nodes[1])
1863 return 1;
1865 right_nritems = btrfs_header_nritems(right);
1866 if (right_nritems == 0) {
1867 return 1;
1870 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
1871 free_space = btrfs_leaf_free_space(left);
1872 if (free_space < data_size) {
1873 free_extent_buffer(left);
1874 return 1;
1877 /* cow and double check */
1878 ret = btrfs_cow_block(trans, root, left,
1879 path->nodes[1], slot - 1, &left);
1880 if (ret) {
1881 /* we hit -ENOSPC, but it isn't fatal here */
1882 free_extent_buffer(left);
1883 return 1;
1886 free_space = btrfs_leaf_free_space(left);
1887 if (free_space < data_size) {
1888 free_extent_buffer(left);
1889 return 1;
1892 if (empty)
1893 nr = right_nritems;
1894 else
1895 nr = right_nritems - 1;
1897 for (i = 0; i < nr; i++) {
1898 item = btrfs_item_nr(i);
1900 if (path->slots[0] == i)
1901 push_space += data_size + sizeof(*item);
1903 this_item_size = btrfs_item_size(right, item);
1904 if (this_item_size + sizeof(*item) + push_space > free_space)
1905 break;
1907 push_items++;
1908 push_space += this_item_size + sizeof(*item);
1911 if (push_items == 0) {
1912 free_extent_buffer(left);
1913 return 1;
1915 if (!empty && push_items == btrfs_header_nritems(right))
1916 WARN_ON(1);
1918 /* push data from right to left */
1919 copy_extent_buffer(left, right,
1920 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1921 btrfs_item_nr_offset(0),
1922 push_items * sizeof(struct btrfs_item));
1924 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1925 btrfs_item_offset_nr(right, push_items -1);
1927 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1928 leaf_data_end(fs_info, left) - push_space,
1929 btrfs_leaf_data(right) +
1930 btrfs_item_offset_nr(right, push_items - 1),
1931 push_space);
1932 old_left_nritems = btrfs_header_nritems(left);
1933 BUG_ON(old_left_nritems == 0);
1935 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1936 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1937 u32 ioff;
1939 item = btrfs_item_nr(i);
1940 ioff = btrfs_item_offset(left, item);
1941 btrfs_set_item_offset(left, item,
1942 ioff - (BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1943 old_left_item_size));
1945 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1947 /* fixup right node */
1948 if (push_items > right_nritems) {
1949 printk("push items %d nr %u\n", push_items, right_nritems);
1950 WARN_ON(1);
1953 if (push_items < right_nritems) {
1954 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1955 leaf_data_end(fs_info, right);
1956 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1957 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1958 push_space,
1959 btrfs_leaf_data(right) +
1960 leaf_data_end(fs_info, right),
1961 push_space);
1963 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1964 btrfs_item_nr_offset(push_items),
1965 (btrfs_header_nritems(right) - push_items) *
1966 sizeof(struct btrfs_item));
1968 right_nritems -= push_items;
1969 btrfs_set_header_nritems(right, right_nritems);
1970 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1971 for (i = 0; i < right_nritems; i++) {
1972 item = btrfs_item_nr(i);
1973 push_space = push_space - btrfs_item_size(right, item);
1974 btrfs_set_item_offset(right, item, push_space);
1977 btrfs_mark_buffer_dirty(left);
1978 if (right_nritems)
1979 btrfs_mark_buffer_dirty(right);
1981 btrfs_item_key(right, &disk_key, 0);
1982 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1984 /* then fixup the leaf pointer in the path */
1985 if (path->slots[0] < push_items) {
1986 path->slots[0] += old_left_nritems;
1987 free_extent_buffer(path->nodes[0]);
1988 path->nodes[0] = left;
1989 path->slots[1] -= 1;
1990 } else {
1991 free_extent_buffer(left);
1992 path->slots[0] -= push_items;
1994 BUG_ON(path->slots[0] < 0);
1995 return ret;
1999 * split the path's leaf in two, making sure there is at least data_size
2000 * available for the resulting leaf level of the path.
2002 * returns 0 if all went well and < 0 on failure.
2004 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2005 struct btrfs_root *root,
2006 struct btrfs_path *path,
2007 struct extent_buffer *l,
2008 struct extent_buffer *right,
2009 int slot, int mid, int nritems)
2011 int data_copy_size;
2012 int rt_data_off;
2013 int i;
2014 int ret = 0;
2015 int wret;
2016 struct btrfs_disk_key disk_key;
2018 nritems = nritems - mid;
2019 btrfs_set_header_nritems(right, nritems);
2020 data_copy_size = btrfs_item_end_nr(l, mid) -
2021 leaf_data_end(root->fs_info, l);
2023 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2024 btrfs_item_nr_offset(mid),
2025 nritems * sizeof(struct btrfs_item));
2027 copy_extent_buffer(right, l,
2028 btrfs_leaf_data(right) +
2029 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2030 data_copy_size, btrfs_leaf_data(l) +
2031 leaf_data_end(root->fs_info, l), data_copy_size);
2033 rt_data_off = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2034 btrfs_item_end_nr(l, mid);
2036 for (i = 0; i < nritems; i++) {
2037 struct btrfs_item *item = btrfs_item_nr(i);
2038 u32 ioff = btrfs_item_offset(right, item);
2039 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2042 btrfs_set_header_nritems(l, mid);
2043 ret = 0;
2044 btrfs_item_key(right, &disk_key, 0);
2045 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2046 path->slots[1] + 1, 1);
2047 if (wret)
2048 ret = wret;
2050 btrfs_mark_buffer_dirty(right);
2051 btrfs_mark_buffer_dirty(l);
2052 BUG_ON(path->slots[0] != slot);
2054 if (mid <= slot) {
2055 free_extent_buffer(path->nodes[0]);
2056 path->nodes[0] = right;
2057 path->slots[0] -= mid;
2058 path->slots[1] += 1;
2059 } else {
2060 free_extent_buffer(right);
2063 BUG_ON(path->slots[0] < 0);
2065 return ret;
2069 * split the path's leaf in two, making sure there is at least data_size
2070 * available for the resulting leaf level of the path.
2072 * returns 0 if all went well and < 0 on failure.
2074 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2075 struct btrfs_root *root,
2076 struct btrfs_key *ins_key,
2077 struct btrfs_path *path, int data_size,
2078 int extend)
2080 struct btrfs_disk_key disk_key;
2081 struct extent_buffer *l;
2082 u32 nritems;
2083 int mid;
2084 int slot;
2085 struct extent_buffer *right;
2086 int ret = 0;
2087 int wret;
2088 int split;
2089 int num_doubles = 0;
2091 l = path->nodes[0];
2092 slot = path->slots[0];
2093 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2094 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info))
2095 return -EOVERFLOW;
2097 /* first try to make some room by pushing left and right */
2098 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2099 wret = push_leaf_right(trans, root, path, data_size, 0);
2100 if (wret < 0)
2101 return wret;
2102 if (wret) {
2103 wret = push_leaf_left(trans, root, path, data_size, 0);
2104 if (wret < 0)
2105 return wret;
2107 l = path->nodes[0];
2109 /* did the pushes work? */
2110 if (btrfs_leaf_free_space(l) >= data_size)
2111 return 0;
2114 if (!path->nodes[1]) {
2115 ret = insert_new_root(trans, root, path, 1);
2116 if (ret)
2117 return ret;
2119 again:
2120 split = 1;
2121 l = path->nodes[0];
2122 slot = path->slots[0];
2123 nritems = btrfs_header_nritems(l);
2124 mid = (nritems + 1) / 2;
2126 if (mid <= slot) {
2127 if (nritems == 1 ||
2128 leaf_space_used(l, mid, nritems - mid) + data_size >
2129 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2130 if (slot >= nritems) {
2131 split = 0;
2132 } else {
2133 mid = slot;
2134 if (mid != nritems &&
2135 leaf_space_used(l, mid, nritems - mid) +
2136 data_size >
2137 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2138 split = 2;
2142 } else {
2143 if (leaf_space_used(l, 0, mid) + data_size >
2144 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2145 if (!extend && data_size && slot == 0) {
2146 split = 0;
2147 } else if ((extend || !data_size) && slot == 0) {
2148 mid = 1;
2149 } else {
2150 mid = slot;
2151 if (mid != nritems &&
2152 leaf_space_used(l, mid, nritems - mid) +
2153 data_size >
2154 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2155 split = 2 ;
2161 if (split == 0)
2162 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2163 else
2164 btrfs_item_key(l, &disk_key, mid);
2166 right = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
2167 root->root_key.objectid,
2168 &disk_key, 0, l->start, 0);
2169 if (IS_ERR(right)) {
2170 BUG_ON(1);
2171 return PTR_ERR(right);
2174 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2175 btrfs_set_header_bytenr(right, right->start);
2176 btrfs_set_header_generation(right, trans->transid);
2177 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2178 btrfs_set_header_owner(right, root->root_key.objectid);
2179 btrfs_set_header_level(right, 0);
2180 write_extent_buffer(right, root->fs_info->fsid,
2181 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2183 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2184 btrfs_header_chunk_tree_uuid(right),
2185 BTRFS_UUID_SIZE);
2187 root_add_used(root, root->fs_info->nodesize);
2189 if (split == 0) {
2190 if (mid <= slot) {
2191 btrfs_set_header_nritems(right, 0);
2192 wret = insert_ptr(trans, root, path,
2193 &disk_key, right->start,
2194 path->slots[1] + 1, 1);
2195 if (wret)
2196 ret = wret;
2198 free_extent_buffer(path->nodes[0]);
2199 path->nodes[0] = right;
2200 path->slots[0] = 0;
2201 path->slots[1] += 1;
2202 } else {
2203 btrfs_set_header_nritems(right, 0);
2204 wret = insert_ptr(trans, root, path,
2205 &disk_key,
2206 right->start,
2207 path->slots[1], 1);
2208 if (wret)
2209 ret = wret;
2210 free_extent_buffer(path->nodes[0]);
2211 path->nodes[0] = right;
2212 path->slots[0] = 0;
2213 if (path->slots[1] == 0) {
2214 btrfs_fixup_low_keys(root, path,
2215 &disk_key, 1);
2218 btrfs_mark_buffer_dirty(right);
2219 return ret;
2222 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2223 BUG_ON(ret);
2225 if (split == 2) {
2226 BUG_ON(num_doubles != 0);
2227 num_doubles++;
2228 goto again;
2231 return ret;
2235 * This function splits a single item into two items,
2236 * giving 'new_key' to the new item and splitting the
2237 * old one at split_offset (from the start of the item).
2239 * The path may be released by this operation. After
2240 * the split, the path is pointing to the old item. The
2241 * new item is going to be in the same node as the old one.
2243 * Note, the item being split must be smaller enough to live alone on
2244 * a tree block with room for one extra struct btrfs_item
2246 * This allows us to split the item in place, keeping a lock on the
2247 * leaf the entire time.
2249 int btrfs_split_item(struct btrfs_trans_handle *trans,
2250 struct btrfs_root *root,
2251 struct btrfs_path *path,
2252 struct btrfs_key *new_key,
2253 unsigned long split_offset)
2255 u32 item_size;
2256 struct extent_buffer *leaf;
2257 struct btrfs_key orig_key;
2258 struct btrfs_item *item;
2259 struct btrfs_item *new_item;
2260 int ret = 0;
2261 int slot;
2262 u32 nritems;
2263 u32 orig_offset;
2264 struct btrfs_disk_key disk_key;
2265 char *buf;
2267 leaf = path->nodes[0];
2268 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2269 if (btrfs_leaf_free_space(leaf) >=
2270 sizeof(struct btrfs_item))
2271 goto split;
2273 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2274 btrfs_release_path(path);
2276 path->search_for_split = 1;
2278 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2279 path->search_for_split = 0;
2281 /* if our item isn't there or got smaller, return now */
2282 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2283 path->slots[0])) {
2284 return -EAGAIN;
2287 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2288 BUG_ON(ret);
2290 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
2291 leaf = path->nodes[0];
2293 split:
2294 item = btrfs_item_nr(path->slots[0]);
2295 orig_offset = btrfs_item_offset(leaf, item);
2296 item_size = btrfs_item_size(leaf, item);
2299 buf = kmalloc(item_size, GFP_NOFS);
2300 BUG_ON(!buf);
2301 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2302 path->slots[0]), item_size);
2303 slot = path->slots[0] + 1;
2304 leaf = path->nodes[0];
2306 nritems = btrfs_header_nritems(leaf);
2308 if (slot < nritems) {
2309 /* shift the items */
2310 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2311 btrfs_item_nr_offset(slot),
2312 (nritems - slot) * sizeof(struct btrfs_item));
2316 btrfs_cpu_key_to_disk(&disk_key, new_key);
2317 btrfs_set_item_key(leaf, &disk_key, slot);
2319 new_item = btrfs_item_nr(slot);
2321 btrfs_set_item_offset(leaf, new_item, orig_offset);
2322 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2324 btrfs_set_item_offset(leaf, item,
2325 orig_offset + item_size - split_offset);
2326 btrfs_set_item_size(leaf, item, split_offset);
2328 btrfs_set_header_nritems(leaf, nritems + 1);
2330 /* write the data for the start of the original item */
2331 write_extent_buffer(leaf, buf,
2332 btrfs_item_ptr_offset(leaf, path->slots[0]),
2333 split_offset);
2335 /* write the data for the new item */
2336 write_extent_buffer(leaf, buf + split_offset,
2337 btrfs_item_ptr_offset(leaf, slot),
2338 item_size - split_offset);
2339 btrfs_mark_buffer_dirty(leaf);
2341 ret = 0;
2342 if (btrfs_leaf_free_space(leaf) < 0) {
2343 btrfs_print_leaf(leaf);
2344 BUG();
2346 kfree(buf);
2347 return ret;
2350 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2351 u32 new_size, int from_end)
2353 int ret = 0;
2354 int slot;
2355 struct extent_buffer *leaf;
2356 struct btrfs_item *item;
2357 u32 nritems;
2358 unsigned int data_end;
2359 unsigned int old_data_start;
2360 unsigned int old_size;
2361 unsigned int size_diff;
2362 int i;
2364 leaf = path->nodes[0];
2365 slot = path->slots[0];
2367 old_size = btrfs_item_size_nr(leaf, slot);
2368 if (old_size == new_size)
2369 return 0;
2371 nritems = btrfs_header_nritems(leaf);
2372 data_end = leaf_data_end(root->fs_info, leaf);
2374 old_data_start = btrfs_item_offset_nr(leaf, slot);
2376 size_diff = old_size - new_size;
2378 BUG_ON(slot < 0);
2379 BUG_ON(slot >= nritems);
2382 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2384 /* first correct the data pointers */
2385 for (i = slot; i < nritems; i++) {
2386 u32 ioff;
2387 item = btrfs_item_nr(i);
2388 ioff = btrfs_item_offset(leaf, item);
2389 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2392 /* shift the data */
2393 if (from_end) {
2394 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2395 data_end + size_diff, btrfs_leaf_data(leaf) +
2396 data_end, old_data_start + new_size - data_end);
2397 } else {
2398 struct btrfs_disk_key disk_key;
2399 u64 offset;
2401 btrfs_item_key(leaf, &disk_key, slot);
2403 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2404 unsigned long ptr;
2405 struct btrfs_file_extent_item *fi;
2407 fi = btrfs_item_ptr(leaf, slot,
2408 struct btrfs_file_extent_item);
2409 fi = (struct btrfs_file_extent_item *)(
2410 (unsigned long)fi - size_diff);
2412 if (btrfs_file_extent_type(leaf, fi) ==
2413 BTRFS_FILE_EXTENT_INLINE) {
2414 ptr = btrfs_item_ptr_offset(leaf, slot);
2415 memmove_extent_buffer(leaf, ptr,
2416 (unsigned long)fi,
2417 offsetof(struct btrfs_file_extent_item,
2418 disk_bytenr));
2422 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2423 data_end + size_diff, btrfs_leaf_data(leaf) +
2424 data_end, old_data_start - data_end);
2426 offset = btrfs_disk_key_offset(&disk_key);
2427 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2428 btrfs_set_item_key(leaf, &disk_key, slot);
2429 if (slot == 0)
2430 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2433 item = btrfs_item_nr(slot);
2434 btrfs_set_item_size(leaf, item, new_size);
2435 btrfs_mark_buffer_dirty(leaf);
2437 ret = 0;
2438 if (btrfs_leaf_free_space(leaf) < 0) {
2439 btrfs_print_leaf(leaf);
2440 BUG();
2442 return ret;
2445 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2446 u32 data_size)
2448 int ret = 0;
2449 int slot;
2450 struct extent_buffer *leaf;
2451 struct btrfs_item *item;
2452 u32 nritems;
2453 unsigned int data_end;
2454 unsigned int old_data;
2455 unsigned int old_size;
2456 int i;
2458 leaf = path->nodes[0];
2460 nritems = btrfs_header_nritems(leaf);
2461 data_end = leaf_data_end(root->fs_info, leaf);
2463 if (btrfs_leaf_free_space(leaf) < data_size) {
2464 btrfs_print_leaf(leaf);
2465 BUG();
2467 slot = path->slots[0];
2468 old_data = btrfs_item_end_nr(leaf, slot);
2470 BUG_ON(slot < 0);
2471 if (slot >= nritems) {
2472 btrfs_print_leaf(leaf);
2473 printk("slot %d too large, nritems %d\n", slot, nritems);
2474 BUG_ON(1);
2478 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2480 /* first correct the data pointers */
2481 for (i = slot; i < nritems; i++) {
2482 u32 ioff;
2483 item = btrfs_item_nr(i);
2484 ioff = btrfs_item_offset(leaf, item);
2485 btrfs_set_item_offset(leaf, item, ioff - data_size);
2488 /* shift the data */
2489 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2490 data_end - data_size, btrfs_leaf_data(leaf) +
2491 data_end, old_data - data_end);
2493 data_end = old_data;
2494 old_size = btrfs_item_size_nr(leaf, slot);
2495 item = btrfs_item_nr(slot);
2496 btrfs_set_item_size(leaf, item, old_size + data_size);
2497 btrfs_mark_buffer_dirty(leaf);
2499 ret = 0;
2500 if (btrfs_leaf_free_space(leaf) < 0) {
2501 btrfs_print_leaf(leaf);
2502 BUG();
2504 return ret;
2508 * Given a key and some data, insert an item into the tree.
2509 * This does all the path init required, making room in the tree if needed.
2511 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2512 struct btrfs_root *root,
2513 struct btrfs_path *path,
2514 struct btrfs_key *cpu_key, u32 *data_size,
2515 int nr)
2517 struct extent_buffer *leaf;
2518 struct btrfs_item *item;
2519 int ret = 0;
2520 int slot;
2521 int i;
2522 u32 nritems;
2523 u32 total_size = 0;
2524 u32 total_data = 0;
2525 unsigned int data_end;
2526 struct btrfs_disk_key disk_key;
2528 for (i = 0; i < nr; i++) {
2529 total_data += data_size[i];
2532 /* create a root if there isn't one */
2533 if (!root->node)
2534 BUG();
2536 total_size = total_data + nr * sizeof(struct btrfs_item);
2537 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2538 if (ret == 0) {
2539 return -EEXIST;
2541 if (ret < 0)
2542 goto out;
2544 leaf = path->nodes[0];
2546 nritems = btrfs_header_nritems(leaf);
2547 data_end = leaf_data_end(root->fs_info, leaf);
2549 if (btrfs_leaf_free_space(leaf) < total_size) {
2550 btrfs_print_leaf(leaf);
2551 printk("not enough freespace need %u have %d\n",
2552 total_size, btrfs_leaf_free_space(leaf));
2553 BUG();
2556 slot = path->slots[0];
2557 BUG_ON(slot < 0);
2559 if (slot < nritems) {
2560 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2562 if (old_data < data_end) {
2563 btrfs_print_leaf(leaf);
2564 printk("slot %d old_data %d data_end %d\n",
2565 slot, old_data, data_end);
2566 BUG_ON(1);
2569 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2571 /* first correct the data pointers */
2572 for (i = slot; i < nritems; i++) {
2573 u32 ioff;
2575 item = btrfs_item_nr(i);
2576 ioff = btrfs_item_offset(leaf, item);
2577 btrfs_set_item_offset(leaf, item, ioff - total_data);
2580 /* shift the items */
2581 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2582 btrfs_item_nr_offset(slot),
2583 (nritems - slot) * sizeof(struct btrfs_item));
2585 /* shift the data */
2586 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2587 data_end - total_data, btrfs_leaf_data(leaf) +
2588 data_end, old_data - data_end);
2589 data_end = old_data;
2592 /* setup the item for the new data */
2593 for (i = 0; i < nr; i++) {
2594 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2595 btrfs_set_item_key(leaf, &disk_key, slot + i);
2596 item = btrfs_item_nr(slot + i);
2597 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2598 data_end -= data_size[i];
2599 btrfs_set_item_size(leaf, item, data_size[i]);
2601 btrfs_set_header_nritems(leaf, nritems + nr);
2602 btrfs_mark_buffer_dirty(leaf);
2604 ret = 0;
2605 if (slot == 0) {
2606 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2607 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2610 if (btrfs_leaf_free_space(leaf) < 0) {
2611 btrfs_print_leaf(leaf);
2612 BUG();
2615 out:
2616 return ret;
2620 * Given a key and some data, insert an item into the tree.
2621 * This does all the path init required, making room in the tree if needed.
2623 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2624 *root, struct btrfs_key *cpu_key, void *data, u32
2625 data_size)
2627 int ret = 0;
2628 struct btrfs_path *path;
2629 struct extent_buffer *leaf;
2630 unsigned long ptr;
2632 path = btrfs_alloc_path();
2633 if (!path)
2634 return -ENOMEM;
2636 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2637 if (!ret) {
2638 leaf = path->nodes[0];
2639 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2640 write_extent_buffer(leaf, data, ptr, data_size);
2641 btrfs_mark_buffer_dirty(leaf);
2643 btrfs_free_path(path);
2644 return ret;
2648 * delete the pointer from a given node.
2650 * If the delete empties a node, the node is removed from the tree,
2651 * continuing all the way the root if required. The root is converted into
2652 * a leaf if all the nodes are emptied.
2654 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2655 int level, int slot)
2657 struct extent_buffer *parent = path->nodes[level];
2658 u32 nritems;
2659 int ret = 0;
2661 nritems = btrfs_header_nritems(parent);
2662 if (slot < nritems - 1) {
2663 /* shift the items */
2664 memmove_extent_buffer(parent,
2665 btrfs_node_key_ptr_offset(slot),
2666 btrfs_node_key_ptr_offset(slot + 1),
2667 sizeof(struct btrfs_key_ptr) *
2668 (nritems - slot - 1));
2670 nritems--;
2671 btrfs_set_header_nritems(parent, nritems);
2672 if (nritems == 0 && parent == root->node) {
2673 BUG_ON(btrfs_header_level(root->node) != 1);
2674 /* just turn the root into a leaf and break */
2675 btrfs_set_header_level(root->node, 0);
2676 } else if (slot == 0) {
2677 struct btrfs_disk_key disk_key;
2679 btrfs_node_key(parent, &disk_key, 0);
2680 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2682 btrfs_mark_buffer_dirty(parent);
2683 return ret;
2687 * a helper function to delete the leaf pointed to by path->slots[1] and
2688 * path->nodes[1].
2690 * This deletes the pointer in path->nodes[1] and frees the leaf
2691 * block extent. zero is returned if it all worked out, < 0 otherwise.
2693 * The path must have already been setup for deleting the leaf, including
2694 * all the proper balancing. path->nodes[1] must be locked.
2696 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2697 struct btrfs_root *root,
2698 struct btrfs_path *path,
2699 struct extent_buffer *leaf)
2701 int ret;
2703 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2704 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2705 if (ret)
2706 return ret;
2708 root_sub_used(root, leaf->len);
2710 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2711 0, root->root_key.objectid, 0, 0);
2712 return ret;
2716 * delete the item at the leaf level in path. If that empties
2717 * the leaf, remove it from the tree
2719 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2720 struct btrfs_path *path, int slot, int nr)
2722 struct extent_buffer *leaf;
2723 struct btrfs_item *item;
2724 int last_off;
2725 int dsize = 0;
2726 int ret = 0;
2727 int wret;
2728 int i;
2729 u32 nritems;
2731 leaf = path->nodes[0];
2732 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2734 for (i = 0; i < nr; i++)
2735 dsize += btrfs_item_size_nr(leaf, slot + i);
2737 nritems = btrfs_header_nritems(leaf);
2739 if (slot + nr != nritems) {
2740 int data_end = leaf_data_end(root->fs_info, leaf);
2742 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2743 data_end + dsize,
2744 btrfs_leaf_data(leaf) + data_end,
2745 last_off - data_end);
2747 for (i = slot + nr; i < nritems; i++) {
2748 u32 ioff;
2750 item = btrfs_item_nr(i);
2751 ioff = btrfs_item_offset(leaf, item);
2752 btrfs_set_item_offset(leaf, item, ioff + dsize);
2755 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2756 btrfs_item_nr_offset(slot + nr),
2757 sizeof(struct btrfs_item) *
2758 (nritems - slot - nr));
2760 btrfs_set_header_nritems(leaf, nritems - nr);
2761 nritems -= nr;
2763 /* delete the leaf if we've emptied it */
2764 if (nritems == 0) {
2765 if (leaf == root->node) {
2766 btrfs_set_header_level(leaf, 0);
2767 } else {
2768 clean_tree_block(leaf);
2769 wret = btrfs_del_leaf(trans, root, path, leaf);
2770 BUG_ON(ret);
2771 if (wret)
2772 ret = wret;
2774 } else {
2775 int used = leaf_space_used(leaf, 0, nritems);
2776 if (slot == 0) {
2777 struct btrfs_disk_key disk_key;
2779 btrfs_item_key(leaf, &disk_key, 0);
2780 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2783 /* delete the leaf if it is mostly empty */
2784 if (used < BTRFS_LEAF_DATA_SIZE(root->fs_info) / 4) {
2785 /* push_leaf_left fixes the path.
2786 * make sure the path still points to our leaf
2787 * for possible call to del_ptr below
2789 slot = path->slots[1];
2790 extent_buffer_get(leaf);
2792 wret = push_leaf_left(trans, root, path, 1, 1);
2793 if (wret < 0 && wret != -ENOSPC)
2794 ret = wret;
2796 if (path->nodes[0] == leaf &&
2797 btrfs_header_nritems(leaf)) {
2798 wret = push_leaf_right(trans, root, path, 1, 1);
2799 if (wret < 0 && wret != -ENOSPC)
2800 ret = wret;
2803 if (btrfs_header_nritems(leaf) == 0) {
2804 clean_tree_block(leaf);
2805 path->slots[1] = slot;
2806 ret = btrfs_del_leaf(trans, root, path, leaf);
2807 BUG_ON(ret);
2808 free_extent_buffer(leaf);
2810 } else {
2811 btrfs_mark_buffer_dirty(leaf);
2812 free_extent_buffer(leaf);
2814 } else {
2815 btrfs_mark_buffer_dirty(leaf);
2818 return ret;
2822 * walk up the tree as far as required to find the previous leaf.
2823 * returns 0 if it found something or 1 if there are no lesser leaves.
2824 * returns < 0 on io errors.
2826 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2828 int slot;
2829 int level = 1;
2830 struct extent_buffer *c;
2831 struct extent_buffer *next = NULL;
2832 struct btrfs_fs_info *fs_info = root->fs_info;
2834 while(level < BTRFS_MAX_LEVEL) {
2835 if (!path->nodes[level])
2836 return 1;
2838 slot = path->slots[level];
2839 c = path->nodes[level];
2840 if (slot == 0) {
2841 level++;
2842 if (level == BTRFS_MAX_LEVEL)
2843 return 1;
2844 continue;
2846 slot--;
2848 next = read_node_slot(fs_info, c, slot);
2849 if (!extent_buffer_uptodate(next)) {
2850 if (IS_ERR(next))
2851 return PTR_ERR(next);
2852 return -EIO;
2854 break;
2856 path->slots[level] = slot;
2857 while(1) {
2858 level--;
2859 c = path->nodes[level];
2860 free_extent_buffer(c);
2861 slot = btrfs_header_nritems(next);
2862 if (slot != 0)
2863 slot--;
2864 path->nodes[level] = next;
2865 path->slots[level] = slot;
2866 if (!level)
2867 break;
2868 next = read_node_slot(fs_info, next, slot);
2869 if (!extent_buffer_uptodate(next)) {
2870 if (IS_ERR(next))
2871 return PTR_ERR(next);
2872 return -EIO;
2875 return 0;
2879 * walk up the tree as far as required to find the next leaf.
2880 * returns 0 if it found something or 1 if there are no greater leaves.
2881 * returns < 0 on io errors.
2883 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2885 int slot;
2886 int level = 1;
2887 struct extent_buffer *c;
2888 struct extent_buffer *next = NULL;
2889 struct btrfs_fs_info *fs_info = root->fs_info;
2891 while(level < BTRFS_MAX_LEVEL) {
2892 if (!path->nodes[level])
2893 return 1;
2895 slot = path->slots[level] + 1;
2896 c = path->nodes[level];
2897 if (slot >= btrfs_header_nritems(c)) {
2898 level++;
2899 if (level == BTRFS_MAX_LEVEL)
2900 return 1;
2901 continue;
2904 if (path->reada)
2905 reada_for_search(root, path, level, slot, 0);
2907 next = read_node_slot(fs_info, c, slot);
2908 if (!extent_buffer_uptodate(next))
2909 return -EIO;
2910 break;
2912 path->slots[level] = slot;
2913 while(1) {
2914 level--;
2915 c = path->nodes[level];
2916 free_extent_buffer(c);
2917 path->nodes[level] = next;
2918 path->slots[level] = 0;
2919 if (!level)
2920 break;
2921 if (path->reada)
2922 reada_for_search(root, path, level, 0, 0);
2923 next = read_node_slot(fs_info, next, 0);
2924 if (!extent_buffer_uptodate(next))
2925 return -EIO;
2927 return 0;
2930 int btrfs_previous_item(struct btrfs_root *root,
2931 struct btrfs_path *path, u64 min_objectid,
2932 int type)
2934 struct btrfs_key found_key;
2935 struct extent_buffer *leaf;
2936 u32 nritems;
2937 int ret;
2939 while(1) {
2940 if (path->slots[0] == 0) {
2941 ret = btrfs_prev_leaf(root, path);
2942 if (ret != 0)
2943 return ret;
2944 } else {
2945 path->slots[0]--;
2947 leaf = path->nodes[0];
2948 nritems = btrfs_header_nritems(leaf);
2949 if (nritems == 0)
2950 return 1;
2951 if (path->slots[0] == nritems)
2952 path->slots[0]--;
2954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2955 if (found_key.objectid < min_objectid)
2956 break;
2957 if (found_key.type == type)
2958 return 0;
2959 if (found_key.objectid == min_objectid &&
2960 found_key.type < type)
2961 break;
2963 return 1;
2967 * search in extent tree to find a previous Metadata/Data extent item with
2968 * min objecitd.
2970 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2972 int btrfs_previous_extent_item(struct btrfs_root *root,
2973 struct btrfs_path *path, u64 min_objectid)
2975 struct btrfs_key found_key;
2976 struct extent_buffer *leaf;
2977 u32 nritems;
2978 int ret;
2980 while (1) {
2981 if (path->slots[0] == 0) {
2982 ret = btrfs_prev_leaf(root, path);
2983 if (ret != 0)
2984 return ret;
2985 } else {
2986 path->slots[0]--;
2988 leaf = path->nodes[0];
2989 nritems = btrfs_header_nritems(leaf);
2990 if (nritems == 0)
2991 return 1;
2992 if (path->slots[0] == nritems)
2993 path->slots[0]--;
2995 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2996 if (found_key.objectid < min_objectid)
2997 break;
2998 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2999 found_key.type == BTRFS_METADATA_ITEM_KEY)
3000 return 0;
3001 if (found_key.objectid == min_objectid &&
3002 found_key.type < BTRFS_EXTENT_ITEM_KEY)
3003 break;
3005 return 1;
3009 * Search in extent tree to found next meta/data extent
3010 * Caller needs to check for no-hole or skinny metadata features.
3012 int btrfs_next_extent_item(struct btrfs_root *root,
3013 struct btrfs_path *path, u64 max_objectid)
3015 struct btrfs_key found_key;
3016 int ret;
3018 while (1) {
3019 ret = btrfs_next_item(root, path);
3020 if (ret)
3021 return ret;
3022 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3023 path->slots[0]);
3024 if (found_key.objectid > max_objectid)
3025 return 1;
3026 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3027 found_key.type == BTRFS_METADATA_ITEM_KEY)
3028 return 0;