btrfs-progs: do not merge tree block refs have different root_id
[btrfs-progs-unstable/devel.git] / ctree.c
blob2c3ba70b000c28e38257dd3606710f8389bcc688
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, root, buf->start,
196 btrfs_header_level(buf), 1,
197 &refs, &flags);
198 BUG_ON(ret);
199 BUG_ON(refs == 0);
200 } else {
201 refs = 1;
202 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
203 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
204 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
205 else
206 flags = 0;
209 owner = btrfs_header_owner(buf);
210 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
211 owner == BTRFS_TREE_RELOC_OBJECTID);
213 if (refs > 1) {
214 if ((owner == root->root_key.objectid ||
215 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
216 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
217 ret = btrfs_inc_ref(trans, root, buf, 1);
218 BUG_ON(ret);
220 if (root->root_key.objectid ==
221 BTRFS_TREE_RELOC_OBJECTID) {
222 ret = btrfs_dec_ref(trans, root, buf, 0);
223 BUG_ON(ret);
224 ret = btrfs_inc_ref(trans, root, cow, 1);
225 BUG_ON(ret);
227 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
228 } else {
230 if (root->root_key.objectid ==
231 BTRFS_TREE_RELOC_OBJECTID)
232 ret = btrfs_inc_ref(trans, root, cow, 1);
233 else
234 ret = btrfs_inc_ref(trans, root, cow, 0);
235 BUG_ON(ret);
237 if (new_flags != 0) {
238 ret = btrfs_set_block_flags(trans, root, buf->start,
239 btrfs_header_level(buf),
240 new_flags);
241 BUG_ON(ret);
243 } else {
244 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
245 if (root->root_key.objectid ==
246 BTRFS_TREE_RELOC_OBJECTID)
247 ret = btrfs_inc_ref(trans, root, cow, 1);
248 else
249 ret = btrfs_inc_ref(trans, root, cow, 0);
250 BUG_ON(ret);
251 ret = btrfs_dec_ref(trans, root, buf, 1);
252 BUG_ON(ret);
254 clean_tree_block(trans, root, buf);
256 return 0;
259 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root,
261 struct extent_buffer *buf,
262 struct extent_buffer *parent, int parent_slot,
263 struct extent_buffer **cow_ret,
264 u64 search_start, u64 empty_size)
266 struct extent_buffer *cow;
267 struct btrfs_disk_key disk_key;
268 int level;
270 WARN_ON(root->ref_cows && trans->transid !=
271 root->fs_info->running_transaction->transid);
272 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
274 level = btrfs_header_level(buf);
276 if (level == 0)
277 btrfs_item_key(buf, &disk_key, 0);
278 else
279 btrfs_node_key(buf, &disk_key, 0);
281 cow = btrfs_alloc_free_block(trans, root, buf->len,
282 root->root_key.objectid, &disk_key,
283 level, search_start, empty_size);
284 if (IS_ERR(cow))
285 return PTR_ERR(cow);
287 copy_extent_buffer(cow, buf, 0, 0, cow->len);
288 btrfs_set_header_bytenr(cow, cow->start);
289 btrfs_set_header_generation(cow, trans->transid);
290 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
291 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
292 BTRFS_HEADER_FLAG_RELOC);
293 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
294 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
295 else
296 btrfs_set_header_owner(cow, root->root_key.objectid);
298 write_extent_buffer(cow, root->fs_info->fsid,
299 btrfs_header_fsid(), BTRFS_FSID_SIZE);
301 WARN_ON(!(buf->flags & EXTENT_BAD_TRANSID) &&
302 btrfs_header_generation(buf) > trans->transid);
304 update_ref_for_cow(trans, root, buf, cow);
306 if (buf == root->node) {
307 root->node = cow;
308 extent_buffer_get(cow);
310 btrfs_free_extent(trans, root, buf->start, buf->len,
311 0, root->root_key.objectid, level, 0);
312 free_extent_buffer(buf);
313 add_root_to_dirty_list(root);
314 } else {
315 btrfs_set_node_blockptr(parent, parent_slot,
316 cow->start);
317 WARN_ON(trans->transid == 0);
318 btrfs_set_node_ptr_generation(parent, parent_slot,
319 trans->transid);
320 btrfs_mark_buffer_dirty(parent);
321 WARN_ON(btrfs_header_generation(parent) != trans->transid);
323 btrfs_free_extent(trans, root, buf->start, buf->len,
324 0, root->root_key.objectid, level, 1);
326 if (!list_empty(&buf->recow)) {
327 list_del_init(&buf->recow);
328 free_extent_buffer(buf);
330 free_extent_buffer(buf);
331 btrfs_mark_buffer_dirty(cow);
332 *cow_ret = cow;
333 return 0;
336 static inline int should_cow_block(struct btrfs_trans_handle *trans,
337 struct btrfs_root *root,
338 struct extent_buffer *buf)
340 if (btrfs_header_generation(buf) == trans->transid &&
341 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
342 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
343 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
344 return 0;
345 return 1;
348 int btrfs_cow_block(struct btrfs_trans_handle *trans,
349 struct btrfs_root *root, struct extent_buffer *buf,
350 struct extent_buffer *parent, int parent_slot,
351 struct extent_buffer **cow_ret)
353 u64 search_start;
354 int ret;
356 if (trans->transaction != root->fs_info->running_transaction) {
357 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
358 root->fs_info->running_transaction->transid);
359 WARN_ON(1);
362 if (trans->transid != root->fs_info->generation) {
363 printk(KERN_CRIT "trans %llu running %llu\n",
364 (unsigned long long)trans->transid,
365 (unsigned long long)root->fs_info->generation);
366 WARN_ON(1);
368 if (!should_cow_block(trans, root, buf)) {
369 *cow_ret = buf;
370 return 0;
373 search_start = buf->start & ~((u64)SZ_1G - 1);
374 ret = __btrfs_cow_block(trans, root, buf, parent,
375 parent_slot, cow_ret, search_start, 0);
376 return ret;
379 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
381 if (k1->objectid > k2->objectid)
382 return 1;
383 if (k1->objectid < k2->objectid)
384 return -1;
385 if (k1->type > k2->type)
386 return 1;
387 if (k1->type < k2->type)
388 return -1;
389 if (k1->offset > k2->offset)
390 return 1;
391 if (k1->offset < k2->offset)
392 return -1;
393 return 0;
397 * compare two keys in a memcmp fashion
399 static int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
401 struct btrfs_key k1;
403 btrfs_disk_key_to_cpu(&k1, disk);
404 return btrfs_comp_cpu_keys(&k1, k2);
408 * The leaf data grows from end-to-front in the node.
409 * this returns the address of the start of the last item,
410 * which is the stop of the leaf data stack
412 static inline unsigned int leaf_data_end(const struct btrfs_fs_info *fs_info,
413 const struct extent_buffer *leaf)
415 u32 nr = btrfs_header_nritems(leaf);
416 if (nr == 0)
417 return BTRFS_LEAF_DATA_SIZE(fs_info);
418 return btrfs_item_offset_nr(leaf, nr - 1);
421 enum btrfs_tree_block_status
422 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
423 struct extent_buffer *buf)
425 int i;
426 struct btrfs_key cpukey;
427 struct btrfs_disk_key key;
428 u32 nritems = btrfs_header_nritems(buf);
429 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
431 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
432 goto fail;
434 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
435 if (parent_key && parent_key->type) {
436 btrfs_node_key(buf, &key, 0);
437 if (memcmp(parent_key, &key, sizeof(key)))
438 goto fail;
440 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
441 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
442 btrfs_node_key(buf, &key, i);
443 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
444 if (btrfs_comp_keys(&key, &cpukey) >= 0)
445 goto fail;
447 return BTRFS_TREE_BLOCK_CLEAN;
448 fail:
449 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
450 if (parent_key)
451 btrfs_disk_key_to_cpu(&cpukey, parent_key);
452 else
453 btrfs_node_key_to_cpu(buf, &cpukey, 0);
454 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
455 buf->start, buf->len,
456 btrfs_header_level(buf));
458 return ret;
461 enum btrfs_tree_block_status
462 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
463 struct extent_buffer *buf)
465 int i;
466 struct btrfs_key cpukey;
467 struct btrfs_disk_key key;
468 u32 nritems = btrfs_header_nritems(buf);
469 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
471 if (nritems * sizeof(struct btrfs_item) > buf->len) {
472 fprintf(stderr, "invalid number of items %llu\n",
473 (unsigned long long)buf->start);
474 goto fail;
477 if (btrfs_header_level(buf) != 0) {
478 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
479 fprintf(stderr, "leaf is not a leaf %llu\n",
480 (unsigned long long)btrfs_header_bytenr(buf));
481 goto fail;
483 if (btrfs_leaf_free_space(buf) < 0) {
484 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
485 fprintf(stderr, "leaf free space incorrect %llu %d\n",
486 (unsigned long long)btrfs_header_bytenr(buf),
487 btrfs_leaf_free_space(buf));
488 goto fail;
491 if (nritems == 0)
492 return BTRFS_TREE_BLOCK_CLEAN;
494 btrfs_item_key(buf, &key, 0);
495 if (parent_key && parent_key->type &&
496 memcmp(parent_key, &key, sizeof(key))) {
497 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
498 fprintf(stderr, "leaf parent key incorrect %llu\n",
499 (unsigned long long)btrfs_header_bytenr(buf));
500 goto fail;
502 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
503 btrfs_item_key(buf, &key, i);
504 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
505 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
506 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
507 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
508 goto fail;
510 if (btrfs_item_offset_nr(buf, i) !=
511 btrfs_item_end_nr(buf, i + 1)) {
512 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
513 fprintf(stderr, "incorrect offsets %u %u\n",
514 btrfs_item_offset_nr(buf, i),
515 btrfs_item_end_nr(buf, i + 1));
516 goto fail;
518 if (i == 0 && btrfs_item_end_nr(buf, i) !=
519 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
520 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
521 fprintf(stderr, "bad item end %u wanted %u\n",
522 btrfs_item_end_nr(buf, i),
523 (unsigned)BTRFS_LEAF_DATA_SIZE(root->fs_info));
524 goto fail;
528 for (i = 0; i < nritems; i++) {
529 if (btrfs_item_end_nr(buf, i) >
530 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
531 btrfs_item_key(buf, &key, 0);
532 btrfs_print_key(&key);
533 fflush(stdout);
534 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
535 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
536 (unsigned long long)btrfs_item_end_nr(buf, i),
537 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
538 root->fs_info));
539 goto fail;
543 return BTRFS_TREE_BLOCK_CLEAN;
544 fail:
545 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
546 if (parent_key)
547 btrfs_disk_key_to_cpu(&cpukey, parent_key);
548 else
549 btrfs_item_key_to_cpu(buf, &cpukey, 0);
551 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
552 buf->start, buf->len, 0);
554 return ret;
557 static int noinline check_block(struct btrfs_root *root,
558 struct btrfs_path *path, int level)
560 struct btrfs_disk_key key;
561 struct btrfs_disk_key *key_ptr = NULL;
562 struct extent_buffer *parent;
563 enum btrfs_tree_block_status ret;
565 if (path->skip_check_block)
566 return 0;
567 if (path->nodes[level + 1]) {
568 parent = path->nodes[level + 1];
569 btrfs_node_key(parent, &key, path->slots[level + 1]);
570 key_ptr = &key;
572 if (level == 0)
573 ret = btrfs_check_leaf(root, key_ptr, path->nodes[0]);
574 else
575 ret = btrfs_check_node(root, key_ptr, path->nodes[level]);
576 if (ret == BTRFS_TREE_BLOCK_CLEAN)
577 return 0;
578 return -EIO;
582 * search for key in the extent_buffer. The items start at offset p,
583 * and they are item_size apart. There are 'max' items in p.
585 * the slot in the array is returned via slot, and it points to
586 * the place where you would insert key if it is not found in
587 * the array.
589 * slot may point to max if the key is bigger than all of the keys
591 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
592 int item_size, struct btrfs_key *key,
593 int max, int *slot)
595 int low = 0;
596 int high = max;
597 int mid;
598 int ret;
599 unsigned long offset;
600 struct btrfs_disk_key *tmp;
602 while(low < high) {
603 mid = (low + high) / 2;
604 offset = p + mid * item_size;
606 tmp = (struct btrfs_disk_key *)(eb->data + offset);
607 ret = btrfs_comp_keys(tmp, key);
609 if (ret < 0)
610 low = mid + 1;
611 else if (ret > 0)
612 high = mid;
613 else {
614 *slot = mid;
615 return 0;
618 *slot = low;
619 return 1;
623 * simple bin_search frontend that does the right thing for
624 * leaves vs nodes
626 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
627 int level, int *slot)
629 if (level == 0)
630 return generic_bin_search(eb,
631 offsetof(struct btrfs_leaf, items),
632 sizeof(struct btrfs_item),
633 key, btrfs_header_nritems(eb),
634 slot);
635 else
636 return generic_bin_search(eb,
637 offsetof(struct btrfs_node, ptrs),
638 sizeof(struct btrfs_key_ptr),
639 key, btrfs_header_nritems(eb),
640 slot);
643 struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
644 struct extent_buffer *parent, int slot)
646 struct extent_buffer *ret;
647 int level = btrfs_header_level(parent);
649 if (slot < 0)
650 return NULL;
651 if (slot >= btrfs_header_nritems(parent))
652 return NULL;
654 if (level == 0)
655 return NULL;
657 ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
658 btrfs_node_ptr_generation(parent, slot));
659 if (!extent_buffer_uptodate(ret))
660 return ERR_PTR(-EIO);
662 if (btrfs_header_level(ret) != level - 1) {
663 error(
664 "child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
665 btrfs_header_bytenr(parent), slot,
666 btrfs_header_level(parent), btrfs_header_level(ret));
667 free_extent_buffer(ret);
668 return ERR_PTR(-EIO);
670 return ret;
673 static int balance_level(struct btrfs_trans_handle *trans,
674 struct btrfs_root *root,
675 struct btrfs_path *path, int level)
677 struct extent_buffer *right = NULL;
678 struct extent_buffer *mid;
679 struct extent_buffer *left = NULL;
680 struct extent_buffer *parent = NULL;
681 struct btrfs_fs_info *fs_info = root->fs_info;
682 int ret = 0;
683 int wret;
684 int pslot;
685 int orig_slot = path->slots[level];
686 u64 orig_ptr;
688 if (level == 0)
689 return 0;
691 mid = path->nodes[level];
692 WARN_ON(btrfs_header_generation(mid) != trans->transid);
694 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
696 if (level < BTRFS_MAX_LEVEL - 1) {
697 parent = path->nodes[level + 1];
698 pslot = path->slots[level + 1];
702 * deal with the case where there is only one pointer in the root
703 * by promoting the node below to a root
705 if (!parent) {
706 struct extent_buffer *child;
708 if (btrfs_header_nritems(mid) != 1)
709 return 0;
711 /* promote the child to a root */
712 child = read_node_slot(fs_info, mid, 0);
713 BUG_ON(!extent_buffer_uptodate(child));
714 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
715 BUG_ON(ret);
717 root->node = child;
718 add_root_to_dirty_list(root);
719 path->nodes[level] = NULL;
720 clean_tree_block(trans, root, mid);
721 /* once for the path */
722 free_extent_buffer(mid);
724 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
725 0, root->root_key.objectid,
726 level, 1);
727 /* once for the root ptr */
728 free_extent_buffer(mid);
729 return ret;
731 if (btrfs_header_nritems(mid) >
732 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
733 return 0;
735 left = read_node_slot(fs_info, parent, pslot - 1);
736 if (extent_buffer_uptodate(left)) {
737 wret = btrfs_cow_block(trans, root, left,
738 parent, pslot - 1, &left);
739 if (wret) {
740 ret = wret;
741 goto enospc;
744 right = read_node_slot(fs_info, parent, pslot + 1);
745 if (extent_buffer_uptodate(right)) {
746 wret = btrfs_cow_block(trans, root, right,
747 parent, pslot + 1, &right);
748 if (wret) {
749 ret = wret;
750 goto enospc;
754 /* first, try to make some room in the middle buffer */
755 if (left) {
756 orig_slot += btrfs_header_nritems(left);
757 wret = push_node_left(trans, root, left, mid, 1);
758 if (wret < 0)
759 ret = wret;
763 * then try to empty the right most buffer into the middle
765 if (right) {
766 wret = push_node_left(trans, root, mid, right, 1);
767 if (wret < 0 && wret != -ENOSPC)
768 ret = wret;
769 if (btrfs_header_nritems(right) == 0) {
770 u64 bytenr = right->start;
771 u32 blocksize = right->len;
773 clean_tree_block(trans, root, right);
774 free_extent_buffer(right);
775 right = NULL;
776 wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
777 if (wret)
778 ret = wret;
779 wret = btrfs_free_extent(trans, root, bytenr,
780 blocksize, 0,
781 root->root_key.objectid,
782 level, 0);
783 if (wret)
784 ret = wret;
785 } else {
786 struct btrfs_disk_key right_key;
787 btrfs_node_key(right, &right_key, 0);
788 btrfs_set_node_key(parent, &right_key, pslot + 1);
789 btrfs_mark_buffer_dirty(parent);
792 if (btrfs_header_nritems(mid) == 1) {
794 * we're not allowed to leave a node with one item in the
795 * tree during a delete. A deletion from lower in the tree
796 * could try to delete the only pointer in this node.
797 * So, pull some keys from the left.
798 * There has to be a left pointer at this point because
799 * otherwise we would have pulled some pointers from the
800 * right
802 BUG_ON(!left);
803 wret = balance_node_right(trans, root, mid, left);
804 if (wret < 0) {
805 ret = wret;
806 goto enospc;
808 if (wret == 1) {
809 wret = push_node_left(trans, root, left, mid, 1);
810 if (wret < 0)
811 ret = wret;
813 BUG_ON(wret == 1);
815 if (btrfs_header_nritems(mid) == 0) {
816 /* we've managed to empty the middle node, drop it */
817 u64 bytenr = mid->start;
818 u32 blocksize = mid->len;
819 clean_tree_block(trans, root, mid);
820 free_extent_buffer(mid);
821 mid = NULL;
822 wret = btrfs_del_ptr(root, path, level + 1, pslot);
823 if (wret)
824 ret = wret;
825 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
826 0, root->root_key.objectid,
827 level, 0);
828 if (wret)
829 ret = wret;
830 } else {
831 /* update the parent key to reflect our changes */
832 struct btrfs_disk_key mid_key;
833 btrfs_node_key(mid, &mid_key, 0);
834 btrfs_set_node_key(parent, &mid_key, pslot);
835 btrfs_mark_buffer_dirty(parent);
838 /* update the path */
839 if (left) {
840 if (btrfs_header_nritems(left) > orig_slot) {
841 extent_buffer_get(left);
842 path->nodes[level] = left;
843 path->slots[level + 1] -= 1;
844 path->slots[level] = orig_slot;
845 if (mid)
846 free_extent_buffer(mid);
847 } else {
848 orig_slot -= btrfs_header_nritems(left);
849 path->slots[level] = orig_slot;
852 /* double check we haven't messed things up */
853 check_block(root, path, level);
854 if (orig_ptr !=
855 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
856 BUG();
857 enospc:
858 if (right)
859 free_extent_buffer(right);
860 if (left)
861 free_extent_buffer(left);
862 return ret;
865 /* returns zero if the push worked, non-zero otherwise */
866 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
867 struct btrfs_root *root,
868 struct btrfs_path *path, int level)
870 struct extent_buffer *right = NULL;
871 struct extent_buffer *mid;
872 struct extent_buffer *left = NULL;
873 struct extent_buffer *parent = NULL;
874 struct btrfs_fs_info *fs_info = root->fs_info;
875 int ret = 0;
876 int wret;
877 int pslot;
878 int orig_slot = path->slots[level];
880 if (level == 0)
881 return 1;
883 mid = path->nodes[level];
884 WARN_ON(btrfs_header_generation(mid) != trans->transid);
886 if (level < BTRFS_MAX_LEVEL - 1) {
887 parent = path->nodes[level + 1];
888 pslot = path->slots[level + 1];
891 if (!parent)
892 return 1;
894 left = read_node_slot(fs_info, parent, pslot - 1);
896 /* first, try to make some room in the middle buffer */
897 if (extent_buffer_uptodate(left)) {
898 u32 left_nr;
899 left_nr = btrfs_header_nritems(left);
900 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
901 wret = 1;
902 } else {
903 ret = btrfs_cow_block(trans, root, left, parent,
904 pslot - 1, &left);
905 if (ret)
906 wret = 1;
907 else {
908 wret = push_node_left(trans, root,
909 left, mid, 0);
912 if (wret < 0)
913 ret = wret;
914 if (wret == 0) {
915 struct btrfs_disk_key disk_key;
916 orig_slot += left_nr;
917 btrfs_node_key(mid, &disk_key, 0);
918 btrfs_set_node_key(parent, &disk_key, pslot);
919 btrfs_mark_buffer_dirty(parent);
920 if (btrfs_header_nritems(left) > orig_slot) {
921 path->nodes[level] = left;
922 path->slots[level + 1] -= 1;
923 path->slots[level] = orig_slot;
924 free_extent_buffer(mid);
925 } else {
926 orig_slot -=
927 btrfs_header_nritems(left);
928 path->slots[level] = orig_slot;
929 free_extent_buffer(left);
931 return 0;
933 free_extent_buffer(left);
935 right= read_node_slot(fs_info, parent, pslot + 1);
938 * then try to empty the right most buffer into the middle
940 if (extent_buffer_uptodate(right)) {
941 u32 right_nr;
942 right_nr = btrfs_header_nritems(right);
943 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 1) {
944 wret = 1;
945 } else {
946 ret = btrfs_cow_block(trans, root, right,
947 parent, pslot + 1,
948 &right);
949 if (ret)
950 wret = 1;
951 else {
952 wret = balance_node_right(trans, root,
953 right, mid);
956 if (wret < 0)
957 ret = wret;
958 if (wret == 0) {
959 struct btrfs_disk_key disk_key;
961 btrfs_node_key(right, &disk_key, 0);
962 btrfs_set_node_key(parent, &disk_key, pslot + 1);
963 btrfs_mark_buffer_dirty(parent);
965 if (btrfs_header_nritems(mid) <= orig_slot) {
966 path->nodes[level] = right;
967 path->slots[level + 1] += 1;
968 path->slots[level] = orig_slot -
969 btrfs_header_nritems(mid);
970 free_extent_buffer(mid);
971 } else {
972 free_extent_buffer(right);
974 return 0;
976 free_extent_buffer(right);
978 return 1;
982 * readahead one full node of leaves
984 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
985 int level, int slot, u64 objectid)
987 struct btrfs_fs_info *fs_info = root->fs_info;
988 struct extent_buffer *node;
989 struct btrfs_disk_key disk_key;
990 u32 nritems;
991 u64 search;
992 u64 lowest_read;
993 u64 highest_read;
994 u64 nread = 0;
995 int direction = path->reada;
996 struct extent_buffer *eb;
997 u32 nr;
998 u32 nscan = 0;
1000 if (level != 1)
1001 return;
1003 if (!path->nodes[level])
1004 return;
1006 node = path->nodes[level];
1007 search = btrfs_node_blockptr(node, slot);
1008 eb = btrfs_find_tree_block(fs_info, search, fs_info->nodesize);
1009 if (eb) {
1010 free_extent_buffer(eb);
1011 return;
1014 highest_read = search;
1015 lowest_read = search;
1017 nritems = btrfs_header_nritems(node);
1018 nr = slot;
1019 while(1) {
1020 if (direction < 0) {
1021 if (nr == 0)
1022 break;
1023 nr--;
1024 } else if (direction > 0) {
1025 nr++;
1026 if (nr >= nritems)
1027 break;
1029 if (path->reada < 0 && objectid) {
1030 btrfs_node_key(node, &disk_key, nr);
1031 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1032 break;
1034 search = btrfs_node_blockptr(node, nr);
1035 if ((search >= lowest_read && search <= highest_read) ||
1036 (search < lowest_read && lowest_read - search <= 32768) ||
1037 (search > highest_read && search - highest_read <= 32768)) {
1038 readahead_tree_block(fs_info, search,
1039 btrfs_node_ptr_generation(node, nr));
1040 nread += fs_info->nodesize;
1042 nscan++;
1043 if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
1044 break;
1045 if(nread > SZ_1M || nscan > 128)
1046 break;
1048 if (search < lowest_read)
1049 lowest_read = search;
1050 if (search > highest_read)
1051 highest_read = search;
1055 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1056 u64 iobjectid, u64 ioff, u8 key_type,
1057 struct btrfs_key *found_key)
1059 int ret;
1060 struct btrfs_key key;
1061 struct extent_buffer *eb;
1062 struct btrfs_path *path;
1064 key.type = key_type;
1065 key.objectid = iobjectid;
1066 key.offset = ioff;
1068 if (found_path == NULL) {
1069 path = btrfs_alloc_path();
1070 if (!path)
1071 return -ENOMEM;
1072 } else
1073 path = found_path;
1075 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1076 if ((ret < 0) || (found_key == NULL))
1077 goto out;
1079 eb = path->nodes[0];
1080 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1081 ret = btrfs_next_leaf(fs_root, path);
1082 if (ret)
1083 goto out;
1084 eb = path->nodes[0];
1087 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1088 if (found_key->type != key.type ||
1089 found_key->objectid != key.objectid) {
1090 ret = 1;
1091 goto out;
1094 out:
1095 if (path != found_path)
1096 btrfs_free_path(path);
1097 return ret;
1101 * look for key in the tree. path is filled in with nodes along the way
1102 * if key is found, we return zero and you can find the item in the leaf
1103 * level of the path (level 0)
1105 * If the key isn't found, the path points to the slot where it should
1106 * be inserted, and 1 is returned. If there are other errors during the
1107 * search a negative error number is returned.
1109 * if ins_len > 0, nodes and leaves will be split as we walk down the
1110 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1111 * possible)
1113 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1114 *root, struct btrfs_key *key, struct btrfs_path *p, int
1115 ins_len, int cow)
1117 struct extent_buffer *b;
1118 int slot;
1119 int ret;
1120 int level;
1121 int should_reada = p->reada;
1122 struct btrfs_fs_info *fs_info = root->fs_info;
1123 u8 lowest_level = 0;
1125 lowest_level = p->lowest_level;
1126 WARN_ON(lowest_level && ins_len > 0);
1127 WARN_ON(p->nodes[0] != NULL);
1129 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1131 again:
1132 b = root->node;
1133 extent_buffer_get(b);
1134 while (b) {
1135 level = btrfs_header_level(b);
1136 if (cow) {
1137 int wret;
1138 wret = btrfs_cow_block(trans, root, b,
1139 p->nodes[level + 1],
1140 p->slots[level + 1],
1141 &b);
1142 if (wret) {
1143 free_extent_buffer(b);
1144 return wret;
1147 BUG_ON(!cow && ins_len);
1148 if (level != btrfs_header_level(b))
1149 WARN_ON(1);
1150 level = btrfs_header_level(b);
1151 p->nodes[level] = b;
1152 ret = check_block(root, p, level);
1153 if (ret)
1154 return -1;
1155 ret = bin_search(b, key, level, &slot);
1156 if (level != 0) {
1157 if (ret && slot > 0)
1158 slot -= 1;
1159 p->slots[level] = slot;
1160 if ((p->search_for_split || ins_len > 0) &&
1161 btrfs_header_nritems(b) >=
1162 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1163 int sret = split_node(trans, root, p, level);
1164 BUG_ON(sret > 0);
1165 if (sret)
1166 return sret;
1167 b = p->nodes[level];
1168 slot = p->slots[level];
1169 } else if (ins_len < 0) {
1170 int sret = balance_level(trans, root, p,
1171 level);
1172 if (sret)
1173 return sret;
1174 b = p->nodes[level];
1175 if (!b) {
1176 btrfs_release_path(p);
1177 goto again;
1179 slot = p->slots[level];
1180 BUG_ON(btrfs_header_nritems(b) == 1);
1182 /* this is only true while dropping a snapshot */
1183 if (level == lowest_level)
1184 break;
1186 if (should_reada)
1187 reada_for_search(root, p, level, slot,
1188 key->objectid);
1190 b = read_node_slot(fs_info, b, slot);
1191 if (!extent_buffer_uptodate(b))
1192 return -EIO;
1193 } else {
1194 p->slots[level] = slot;
1195 if (ins_len > 0 &&
1196 ins_len > btrfs_leaf_free_space(b)) {
1197 int sret = split_leaf(trans, root, key,
1198 p, ins_len, ret == 0);
1199 BUG_ON(sret > 0);
1200 if (sret)
1201 return sret;
1203 return ret;
1206 return 1;
1210 * adjust the pointers going up the tree, starting at level
1211 * making sure the right key of each node is points to 'key'.
1212 * This is used after shifting pointers to the left, so it stops
1213 * fixing up pointers when a given leaf/node is not in slot 0 of the
1214 * higher levels
1216 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
1217 struct btrfs_disk_key *key, int level)
1219 int i;
1220 struct extent_buffer *t;
1222 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1223 int tslot = path->slots[i];
1224 if (!path->nodes[i])
1225 break;
1226 t = path->nodes[i];
1227 btrfs_set_node_key(t, key, tslot);
1228 btrfs_mark_buffer_dirty(path->nodes[i]);
1229 if (tslot != 0)
1230 break;
1235 * update item key.
1237 * This function isn't completely safe. It's the caller's responsibility
1238 * that the new key won't break the order
1240 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
1241 struct btrfs_key *new_key)
1243 struct btrfs_disk_key disk_key;
1244 struct extent_buffer *eb;
1245 int slot;
1247 eb = path->nodes[0];
1248 slot = path->slots[0];
1249 if (slot > 0) {
1250 btrfs_item_key(eb, &disk_key, slot - 1);
1251 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1252 return -1;
1254 if (slot < btrfs_header_nritems(eb) - 1) {
1255 btrfs_item_key(eb, &disk_key, slot + 1);
1256 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1257 return -1;
1260 btrfs_cpu_key_to_disk(&disk_key, new_key);
1261 btrfs_set_item_key(eb, &disk_key, slot);
1262 btrfs_mark_buffer_dirty(eb);
1263 if (slot == 0)
1264 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1265 return 0;
1269 * update an item key without the safety checks. This is meant to be called by
1270 * fsck only.
1272 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
1273 struct btrfs_path *path,
1274 struct btrfs_key *new_key)
1276 struct btrfs_disk_key disk_key;
1277 struct extent_buffer *eb;
1278 int slot;
1280 eb = path->nodes[0];
1281 slot = path->slots[0];
1283 btrfs_cpu_key_to_disk(&disk_key, new_key);
1284 btrfs_set_item_key(eb, &disk_key, slot);
1285 btrfs_mark_buffer_dirty(eb);
1286 if (slot == 0)
1287 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1291 * try to push data from one node into the next node left in the
1292 * tree.
1294 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1295 * error, and > 0 if there was no room in the left hand block.
1297 static int push_node_left(struct btrfs_trans_handle *trans,
1298 struct btrfs_root *root, struct extent_buffer *dst,
1299 struct extent_buffer *src, int empty)
1301 int push_items = 0;
1302 int src_nritems;
1303 int dst_nritems;
1304 int ret = 0;
1306 src_nritems = btrfs_header_nritems(src);
1307 dst_nritems = btrfs_header_nritems(dst);
1308 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1309 WARN_ON(btrfs_header_generation(src) != trans->transid);
1310 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1312 if (!empty && src_nritems <= 8)
1313 return 1;
1315 if (push_items <= 0) {
1316 return 1;
1319 if (empty) {
1320 push_items = min(src_nritems, push_items);
1321 if (push_items < src_nritems) {
1322 /* leave at least 8 pointers in the node if
1323 * we aren't going to empty it
1325 if (src_nritems - push_items < 8) {
1326 if (push_items <= 8)
1327 return 1;
1328 push_items -= 8;
1331 } else
1332 push_items = min(src_nritems - 8, push_items);
1334 copy_extent_buffer(dst, src,
1335 btrfs_node_key_ptr_offset(dst_nritems),
1336 btrfs_node_key_ptr_offset(0),
1337 push_items * sizeof(struct btrfs_key_ptr));
1339 if (push_items < src_nritems) {
1340 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1341 btrfs_node_key_ptr_offset(push_items),
1342 (src_nritems - push_items) *
1343 sizeof(struct btrfs_key_ptr));
1345 btrfs_set_header_nritems(src, src_nritems - push_items);
1346 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1347 btrfs_mark_buffer_dirty(src);
1348 btrfs_mark_buffer_dirty(dst);
1350 return ret;
1354 * try to push data from one node into the next node right in the
1355 * tree.
1357 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1358 * error, and > 0 if there was no room in the right hand block.
1360 * this will only push up to 1/2 the contents of the left node over
1362 static int balance_node_right(struct btrfs_trans_handle *trans,
1363 struct btrfs_root *root,
1364 struct extent_buffer *dst,
1365 struct extent_buffer *src)
1367 int push_items = 0;
1368 int max_push;
1369 int src_nritems;
1370 int dst_nritems;
1371 int ret = 0;
1373 WARN_ON(btrfs_header_generation(src) != trans->transid);
1374 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1376 src_nritems = btrfs_header_nritems(src);
1377 dst_nritems = btrfs_header_nritems(dst);
1378 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems;
1379 if (push_items <= 0) {
1380 return 1;
1383 if (src_nritems < 4) {
1384 return 1;
1387 max_push = src_nritems / 2 + 1;
1388 /* don't try to empty the node */
1389 if (max_push >= src_nritems) {
1390 return 1;
1393 if (max_push < push_items)
1394 push_items = max_push;
1396 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1397 btrfs_node_key_ptr_offset(0),
1398 (dst_nritems) *
1399 sizeof(struct btrfs_key_ptr));
1401 copy_extent_buffer(dst, src,
1402 btrfs_node_key_ptr_offset(0),
1403 btrfs_node_key_ptr_offset(src_nritems - push_items),
1404 push_items * sizeof(struct btrfs_key_ptr));
1406 btrfs_set_header_nritems(src, src_nritems - push_items);
1407 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1409 btrfs_mark_buffer_dirty(src);
1410 btrfs_mark_buffer_dirty(dst);
1412 return ret;
1416 * helper function to insert a new root level in the tree.
1417 * A new node is allocated, and a single item is inserted to
1418 * point to the existing root
1420 * returns zero on success or < 0 on failure.
1422 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1423 struct btrfs_root *root,
1424 struct btrfs_path *path, int level)
1426 u64 lower_gen;
1427 struct extent_buffer *lower;
1428 struct extent_buffer *c;
1429 struct extent_buffer *old;
1430 struct btrfs_disk_key lower_key;
1432 BUG_ON(path->nodes[level]);
1433 BUG_ON(path->nodes[level-1] != root->node);
1435 lower = path->nodes[level-1];
1436 if (level == 1)
1437 btrfs_item_key(lower, &lower_key, 0);
1438 else
1439 btrfs_node_key(lower, &lower_key, 0);
1441 c = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1442 root->root_key.objectid, &lower_key,
1443 level, root->node->start, 0);
1445 if (IS_ERR(c))
1446 return PTR_ERR(c);
1448 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1449 btrfs_set_header_nritems(c, 1);
1450 btrfs_set_header_level(c, level);
1451 btrfs_set_header_bytenr(c, c->start);
1452 btrfs_set_header_generation(c, trans->transid);
1453 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1454 btrfs_set_header_owner(c, root->root_key.objectid);
1456 write_extent_buffer(c, root->fs_info->fsid,
1457 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1459 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1460 btrfs_header_chunk_tree_uuid(c),
1461 BTRFS_UUID_SIZE);
1463 btrfs_set_node_key(c, &lower_key, 0);
1464 btrfs_set_node_blockptr(c, 0, lower->start);
1465 lower_gen = btrfs_header_generation(lower);
1466 WARN_ON(lower_gen != trans->transid);
1468 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1470 btrfs_mark_buffer_dirty(c);
1472 old = root->node;
1473 root->node = c;
1475 /* the super has an extra ref to root->node */
1476 free_extent_buffer(old);
1478 add_root_to_dirty_list(root);
1479 extent_buffer_get(c);
1480 path->nodes[level] = c;
1481 path->slots[level] = 0;
1482 return 0;
1486 * worker function to insert a single pointer in a node.
1487 * the node should have enough room for the pointer already
1489 * slot and level indicate where you want the key to go, and
1490 * blocknr is the block the key points to.
1492 * returns zero on success and < 0 on any error
1494 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1495 *root, struct btrfs_path *path, struct btrfs_disk_key
1496 *key, u64 bytenr, int slot, int level)
1498 struct extent_buffer *lower;
1499 int nritems;
1501 BUG_ON(!path->nodes[level]);
1502 lower = path->nodes[level];
1503 nritems = btrfs_header_nritems(lower);
1504 if (slot > nritems)
1505 BUG();
1506 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root->fs_info))
1507 BUG();
1508 if (slot < nritems) {
1509 /* shift the items */
1510 memmove_extent_buffer(lower,
1511 btrfs_node_key_ptr_offset(slot + 1),
1512 btrfs_node_key_ptr_offset(slot),
1513 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1515 btrfs_set_node_key(lower, key, slot);
1516 btrfs_set_node_blockptr(lower, slot, bytenr);
1517 WARN_ON(trans->transid == 0);
1518 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1519 btrfs_set_header_nritems(lower, nritems + 1);
1520 btrfs_mark_buffer_dirty(lower);
1521 return 0;
1525 * split the node at the specified level in path in two.
1526 * The path is corrected to point to the appropriate node after the split
1528 * Before splitting this tries to make some room in the node by pushing
1529 * left and right, if either one works, it returns right away.
1531 * returns 0 on success and < 0 on failure
1533 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1534 *root, struct btrfs_path *path, int level)
1536 struct extent_buffer *c;
1537 struct extent_buffer *split;
1538 struct btrfs_disk_key disk_key;
1539 int mid;
1540 int ret;
1541 int wret;
1542 u32 c_nritems;
1544 c = path->nodes[level];
1545 WARN_ON(btrfs_header_generation(c) != trans->transid);
1546 if (c == root->node) {
1547 /* trying to split the root, lets make a new one */
1548 ret = insert_new_root(trans, root, path, level + 1);
1549 if (ret)
1550 return ret;
1551 } else {
1552 ret = push_nodes_for_insert(trans, root, path, level);
1553 c = path->nodes[level];
1554 if (!ret && btrfs_header_nritems(c) <
1555 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 3)
1556 return 0;
1557 if (ret < 0)
1558 return ret;
1561 c_nritems = btrfs_header_nritems(c);
1562 mid = (c_nritems + 1) / 2;
1563 btrfs_node_key(c, &disk_key, mid);
1565 split = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1566 root->root_key.objectid,
1567 &disk_key, level, c->start, 0);
1568 if (IS_ERR(split))
1569 return PTR_ERR(split);
1571 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1572 btrfs_set_header_level(split, btrfs_header_level(c));
1573 btrfs_set_header_bytenr(split, split->start);
1574 btrfs_set_header_generation(split, trans->transid);
1575 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1576 btrfs_set_header_owner(split, root->root_key.objectid);
1577 write_extent_buffer(split, root->fs_info->fsid,
1578 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1579 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1580 btrfs_header_chunk_tree_uuid(split),
1581 BTRFS_UUID_SIZE);
1584 copy_extent_buffer(split, c,
1585 btrfs_node_key_ptr_offset(0),
1586 btrfs_node_key_ptr_offset(mid),
1587 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1588 btrfs_set_header_nritems(split, c_nritems - mid);
1589 btrfs_set_header_nritems(c, mid);
1590 ret = 0;
1592 btrfs_mark_buffer_dirty(c);
1593 btrfs_mark_buffer_dirty(split);
1595 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1596 path->slots[level + 1] + 1,
1597 level + 1);
1598 if (wret)
1599 ret = wret;
1601 if (path->slots[level] >= mid) {
1602 path->slots[level] -= mid;
1603 free_extent_buffer(c);
1604 path->nodes[level] = split;
1605 path->slots[level + 1] += 1;
1606 } else {
1607 free_extent_buffer(split);
1609 return ret;
1613 * how many bytes are required to store the items in a leaf. start
1614 * and nr indicate which items in the leaf to check. This totals up the
1615 * space used both by the item structs and the item data
1617 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1619 int data_len;
1620 int nritems = btrfs_header_nritems(l);
1621 int end = min(nritems, start + nr) - 1;
1623 if (!nr)
1624 return 0;
1625 data_len = btrfs_item_end_nr(l, start);
1626 data_len = data_len - btrfs_item_offset_nr(l, end);
1627 data_len += sizeof(struct btrfs_item) * nr;
1628 WARN_ON(data_len < 0);
1629 return data_len;
1633 * The space between the end of the leaf items and
1634 * the start of the leaf data. IOW, how much room
1635 * the leaf has left for both items and data
1637 int btrfs_leaf_free_space(struct extent_buffer *leaf)
1639 int nritems = btrfs_header_nritems(leaf);
1640 u32 leaf_data_size;
1641 int ret;
1643 BUG_ON(leaf->fs_info && leaf->fs_info->nodesize != leaf->len);
1644 leaf_data_size = __BTRFS_LEAF_DATA_SIZE(leaf->len);
1645 ret = leaf_data_size - leaf_space_used(leaf, 0 ,nritems);
1646 if (ret < 0) {
1647 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1648 ret, leaf_data_size, leaf_space_used(leaf, 0, nritems),
1649 nritems);
1651 return ret;
1655 * push some data in the path leaf to the right, trying to free up at
1656 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1658 * returns 1 if the push failed because the other node didn't have enough
1659 * room, 0 if everything worked out and < 0 if there were major errors.
1661 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1662 *root, struct btrfs_path *path, int data_size,
1663 int empty)
1665 struct extent_buffer *left = path->nodes[0];
1666 struct extent_buffer *right;
1667 struct extent_buffer *upper;
1668 struct btrfs_disk_key disk_key;
1669 struct btrfs_fs_info *fs_info = root->fs_info;
1670 int slot;
1671 u32 i;
1672 int free_space;
1673 int push_space = 0;
1674 int push_items = 0;
1675 struct btrfs_item *item;
1676 u32 left_nritems;
1677 u32 nr;
1678 u32 right_nritems;
1679 u32 data_end;
1680 u32 this_item_size;
1681 int ret;
1683 slot = path->slots[1];
1684 if (!path->nodes[1]) {
1685 return 1;
1687 upper = path->nodes[1];
1688 if (slot >= btrfs_header_nritems(upper) - 1)
1689 return 1;
1691 right = read_node_slot(fs_info, upper, slot + 1);
1692 if (!extent_buffer_uptodate(right)) {
1693 if (IS_ERR(right))
1694 return PTR_ERR(right);
1695 return -EIO;
1697 free_space = btrfs_leaf_free_space(right);
1698 if (free_space < data_size) {
1699 free_extent_buffer(right);
1700 return 1;
1703 /* cow and double check */
1704 ret = btrfs_cow_block(trans, root, right, upper,
1705 slot + 1, &right);
1706 if (ret) {
1707 free_extent_buffer(right);
1708 return 1;
1710 free_space = btrfs_leaf_free_space(right);
1711 if (free_space < data_size) {
1712 free_extent_buffer(right);
1713 return 1;
1716 left_nritems = btrfs_header_nritems(left);
1717 if (left_nritems == 0) {
1718 free_extent_buffer(right);
1719 return 1;
1722 if (empty)
1723 nr = 0;
1724 else
1725 nr = 1;
1727 i = left_nritems - 1;
1728 while (i >= nr) {
1729 item = btrfs_item_nr(i);
1731 if (path->slots[0] == i)
1732 push_space += data_size + sizeof(*item);
1734 this_item_size = btrfs_item_size(left, item);
1735 if (this_item_size + sizeof(*item) + push_space > free_space)
1736 break;
1737 push_items++;
1738 push_space += this_item_size + sizeof(*item);
1739 if (i == 0)
1740 break;
1741 i--;
1744 if (push_items == 0) {
1745 free_extent_buffer(right);
1746 return 1;
1749 if (!empty && push_items == left_nritems)
1750 WARN_ON(1);
1752 /* push left to right */
1753 right_nritems = btrfs_header_nritems(right);
1755 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1756 push_space -= leaf_data_end(fs_info, left);
1758 /* make room in the right data area */
1759 data_end = leaf_data_end(fs_info, right);
1760 memmove_extent_buffer(right,
1761 btrfs_leaf_data(right) + data_end - push_space,
1762 btrfs_leaf_data(right) + data_end,
1763 BTRFS_LEAF_DATA_SIZE(root->fs_info) - data_end);
1765 /* copy from the left data area */
1766 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1767 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space,
1768 btrfs_leaf_data(left) + leaf_data_end(fs_info, left),
1769 push_space);
1771 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1772 btrfs_item_nr_offset(0),
1773 right_nritems * sizeof(struct btrfs_item));
1775 /* copy the items from left to right */
1776 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1777 btrfs_item_nr_offset(left_nritems - push_items),
1778 push_items * sizeof(struct btrfs_item));
1780 /* update the item pointers */
1781 right_nritems += push_items;
1782 btrfs_set_header_nritems(right, right_nritems);
1783 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1784 for (i = 0; i < right_nritems; i++) {
1785 item = btrfs_item_nr(i);
1786 push_space -= btrfs_item_size(right, item);
1787 btrfs_set_item_offset(right, item, push_space);
1790 left_nritems -= push_items;
1791 btrfs_set_header_nritems(left, left_nritems);
1793 if (left_nritems)
1794 btrfs_mark_buffer_dirty(left);
1795 btrfs_mark_buffer_dirty(right);
1797 btrfs_item_key(right, &disk_key, 0);
1798 btrfs_set_node_key(upper, &disk_key, slot + 1);
1799 btrfs_mark_buffer_dirty(upper);
1801 /* then fixup the leaf pointer in the path */
1802 if (path->slots[0] >= left_nritems) {
1803 path->slots[0] -= left_nritems;
1804 free_extent_buffer(path->nodes[0]);
1805 path->nodes[0] = right;
1806 path->slots[1] += 1;
1807 } else {
1808 free_extent_buffer(right);
1810 return 0;
1813 * push some data in the path leaf to the left, trying to free up at
1814 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1816 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1817 *root, struct btrfs_path *path, int data_size,
1818 int empty)
1820 struct btrfs_disk_key disk_key;
1821 struct extent_buffer *right = path->nodes[0];
1822 struct extent_buffer *left;
1823 struct btrfs_fs_info *fs_info = root->fs_info;
1824 int slot;
1825 int i;
1826 int free_space;
1827 int push_space = 0;
1828 int push_items = 0;
1829 struct btrfs_item *item;
1830 u32 old_left_nritems;
1831 u32 right_nritems;
1832 u32 nr;
1833 int ret = 0;
1834 u32 this_item_size;
1835 u32 old_left_item_size;
1837 slot = path->slots[1];
1838 if (slot == 0)
1839 return 1;
1840 if (!path->nodes[1])
1841 return 1;
1843 right_nritems = btrfs_header_nritems(right);
1844 if (right_nritems == 0) {
1845 return 1;
1848 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
1849 free_space = btrfs_leaf_free_space(left);
1850 if (free_space < data_size) {
1851 free_extent_buffer(left);
1852 return 1;
1855 /* cow and double check */
1856 ret = btrfs_cow_block(trans, root, left,
1857 path->nodes[1], slot - 1, &left);
1858 if (ret) {
1859 /* we hit -ENOSPC, but it isn't fatal here */
1860 free_extent_buffer(left);
1861 return 1;
1864 free_space = btrfs_leaf_free_space(left);
1865 if (free_space < data_size) {
1866 free_extent_buffer(left);
1867 return 1;
1870 if (empty)
1871 nr = right_nritems;
1872 else
1873 nr = right_nritems - 1;
1875 for (i = 0; i < nr; i++) {
1876 item = btrfs_item_nr(i);
1878 if (path->slots[0] == i)
1879 push_space += data_size + sizeof(*item);
1881 this_item_size = btrfs_item_size(right, item);
1882 if (this_item_size + sizeof(*item) + push_space > free_space)
1883 break;
1885 push_items++;
1886 push_space += this_item_size + sizeof(*item);
1889 if (push_items == 0) {
1890 free_extent_buffer(left);
1891 return 1;
1893 if (!empty && push_items == btrfs_header_nritems(right))
1894 WARN_ON(1);
1896 /* push data from right to left */
1897 copy_extent_buffer(left, right,
1898 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1899 btrfs_item_nr_offset(0),
1900 push_items * sizeof(struct btrfs_item));
1902 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1903 btrfs_item_offset_nr(right, push_items -1);
1905 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1906 leaf_data_end(fs_info, left) - push_space,
1907 btrfs_leaf_data(right) +
1908 btrfs_item_offset_nr(right, push_items - 1),
1909 push_space);
1910 old_left_nritems = btrfs_header_nritems(left);
1911 BUG_ON(old_left_nritems == 0);
1913 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1914 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1915 u32 ioff;
1917 item = btrfs_item_nr(i);
1918 ioff = btrfs_item_offset(left, item);
1919 btrfs_set_item_offset(left, item,
1920 ioff - (BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1921 old_left_item_size));
1923 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1925 /* fixup right node */
1926 if (push_items > right_nritems) {
1927 printk("push items %d nr %u\n", push_items, right_nritems);
1928 WARN_ON(1);
1931 if (push_items < right_nritems) {
1932 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1933 leaf_data_end(fs_info, right);
1934 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1935 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1936 push_space,
1937 btrfs_leaf_data(right) +
1938 leaf_data_end(fs_info, right),
1939 push_space);
1941 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1942 btrfs_item_nr_offset(push_items),
1943 (btrfs_header_nritems(right) - push_items) *
1944 sizeof(struct btrfs_item));
1946 right_nritems -= push_items;
1947 btrfs_set_header_nritems(right, right_nritems);
1948 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1949 for (i = 0; i < right_nritems; i++) {
1950 item = btrfs_item_nr(i);
1951 push_space = push_space - btrfs_item_size(right, item);
1952 btrfs_set_item_offset(right, item, push_space);
1955 btrfs_mark_buffer_dirty(left);
1956 if (right_nritems)
1957 btrfs_mark_buffer_dirty(right);
1959 btrfs_item_key(right, &disk_key, 0);
1960 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1962 /* then fixup the leaf pointer in the path */
1963 if (path->slots[0] < push_items) {
1964 path->slots[0] += old_left_nritems;
1965 free_extent_buffer(path->nodes[0]);
1966 path->nodes[0] = left;
1967 path->slots[1] -= 1;
1968 } else {
1969 free_extent_buffer(left);
1970 path->slots[0] -= push_items;
1972 BUG_ON(path->slots[0] < 0);
1973 return ret;
1977 * split the path's leaf in two, making sure there is at least data_size
1978 * available for the resulting leaf level of the path.
1980 * returns 0 if all went well and < 0 on failure.
1982 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
1983 struct btrfs_root *root,
1984 struct btrfs_path *path,
1985 struct extent_buffer *l,
1986 struct extent_buffer *right,
1987 int slot, int mid, int nritems)
1989 int data_copy_size;
1990 int rt_data_off;
1991 int i;
1992 int ret = 0;
1993 int wret;
1994 struct btrfs_disk_key disk_key;
1996 nritems = nritems - mid;
1997 btrfs_set_header_nritems(right, nritems);
1998 data_copy_size = btrfs_item_end_nr(l, mid) -
1999 leaf_data_end(root->fs_info, l);
2001 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2002 btrfs_item_nr_offset(mid),
2003 nritems * sizeof(struct btrfs_item));
2005 copy_extent_buffer(right, l,
2006 btrfs_leaf_data(right) +
2007 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2008 data_copy_size, btrfs_leaf_data(l) +
2009 leaf_data_end(root->fs_info, l), data_copy_size);
2011 rt_data_off = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2012 btrfs_item_end_nr(l, mid);
2014 for (i = 0; i < nritems; i++) {
2015 struct btrfs_item *item = btrfs_item_nr(i);
2016 u32 ioff = btrfs_item_offset(right, item);
2017 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2020 btrfs_set_header_nritems(l, mid);
2021 ret = 0;
2022 btrfs_item_key(right, &disk_key, 0);
2023 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2024 path->slots[1] + 1, 1);
2025 if (wret)
2026 ret = wret;
2028 btrfs_mark_buffer_dirty(right);
2029 btrfs_mark_buffer_dirty(l);
2030 BUG_ON(path->slots[0] != slot);
2032 if (mid <= slot) {
2033 free_extent_buffer(path->nodes[0]);
2034 path->nodes[0] = right;
2035 path->slots[0] -= mid;
2036 path->slots[1] += 1;
2037 } else {
2038 free_extent_buffer(right);
2041 BUG_ON(path->slots[0] < 0);
2043 return ret;
2047 * split the path's leaf in two, making sure there is at least data_size
2048 * available for the resulting leaf level of the path.
2050 * returns 0 if all went well and < 0 on failure.
2052 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2053 struct btrfs_root *root,
2054 struct btrfs_key *ins_key,
2055 struct btrfs_path *path, int data_size,
2056 int extend)
2058 struct btrfs_disk_key disk_key;
2059 struct extent_buffer *l;
2060 u32 nritems;
2061 int mid;
2062 int slot;
2063 struct extent_buffer *right;
2064 int ret = 0;
2065 int wret;
2066 int split;
2067 int num_doubles = 0;
2069 l = path->nodes[0];
2070 slot = path->slots[0];
2071 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2072 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info))
2073 return -EOVERFLOW;
2075 /* first try to make some room by pushing left and right */
2076 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2077 wret = push_leaf_right(trans, root, path, data_size, 0);
2078 if (wret < 0)
2079 return wret;
2080 if (wret) {
2081 wret = push_leaf_left(trans, root, path, data_size, 0);
2082 if (wret < 0)
2083 return wret;
2085 l = path->nodes[0];
2087 /* did the pushes work? */
2088 if (btrfs_leaf_free_space(l) >= data_size)
2089 return 0;
2092 if (!path->nodes[1]) {
2093 ret = insert_new_root(trans, root, path, 1);
2094 if (ret)
2095 return ret;
2097 again:
2098 split = 1;
2099 l = path->nodes[0];
2100 slot = path->slots[0];
2101 nritems = btrfs_header_nritems(l);
2102 mid = (nritems + 1) / 2;
2104 if (mid <= slot) {
2105 if (nritems == 1 ||
2106 leaf_space_used(l, mid, nritems - mid) + data_size >
2107 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2108 if (slot >= nritems) {
2109 split = 0;
2110 } else {
2111 mid = slot;
2112 if (mid != nritems &&
2113 leaf_space_used(l, mid, nritems - mid) +
2114 data_size >
2115 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2116 split = 2;
2120 } else {
2121 if (leaf_space_used(l, 0, mid) + data_size >
2122 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2123 if (!extend && data_size && slot == 0) {
2124 split = 0;
2125 } else if ((extend || !data_size) && slot == 0) {
2126 mid = 1;
2127 } else {
2128 mid = slot;
2129 if (mid != nritems &&
2130 leaf_space_used(l, mid, nritems - mid) +
2131 data_size >
2132 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2133 split = 2 ;
2139 if (split == 0)
2140 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2141 else
2142 btrfs_item_key(l, &disk_key, mid);
2144 right = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
2145 root->root_key.objectid,
2146 &disk_key, 0, l->start, 0);
2147 if (IS_ERR(right)) {
2148 BUG_ON(1);
2149 return PTR_ERR(right);
2152 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2153 btrfs_set_header_bytenr(right, right->start);
2154 btrfs_set_header_generation(right, trans->transid);
2155 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2156 btrfs_set_header_owner(right, root->root_key.objectid);
2157 btrfs_set_header_level(right, 0);
2158 write_extent_buffer(right, root->fs_info->fsid,
2159 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2161 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2162 btrfs_header_chunk_tree_uuid(right),
2163 BTRFS_UUID_SIZE);
2165 if (split == 0) {
2166 if (mid <= slot) {
2167 btrfs_set_header_nritems(right, 0);
2168 wret = insert_ptr(trans, root, path,
2169 &disk_key, right->start,
2170 path->slots[1] + 1, 1);
2171 if (wret)
2172 ret = wret;
2174 free_extent_buffer(path->nodes[0]);
2175 path->nodes[0] = right;
2176 path->slots[0] = 0;
2177 path->slots[1] += 1;
2178 } else {
2179 btrfs_set_header_nritems(right, 0);
2180 wret = insert_ptr(trans, root, path,
2181 &disk_key,
2182 right->start,
2183 path->slots[1], 1);
2184 if (wret)
2185 ret = wret;
2186 free_extent_buffer(path->nodes[0]);
2187 path->nodes[0] = right;
2188 path->slots[0] = 0;
2189 if (path->slots[1] == 0) {
2190 btrfs_fixup_low_keys(root, path,
2191 &disk_key, 1);
2194 btrfs_mark_buffer_dirty(right);
2195 return ret;
2198 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2199 BUG_ON(ret);
2201 if (split == 2) {
2202 BUG_ON(num_doubles != 0);
2203 num_doubles++;
2204 goto again;
2207 return ret;
2211 * This function splits a single item into two items,
2212 * giving 'new_key' to the new item and splitting the
2213 * old one at split_offset (from the start of the item).
2215 * The path may be released by this operation. After
2216 * the split, the path is pointing to the old item. The
2217 * new item is going to be in the same node as the old one.
2219 * Note, the item being split must be smaller enough to live alone on
2220 * a tree block with room for one extra struct btrfs_item
2222 * This allows us to split the item in place, keeping a lock on the
2223 * leaf the entire time.
2225 int btrfs_split_item(struct btrfs_trans_handle *trans,
2226 struct btrfs_root *root,
2227 struct btrfs_path *path,
2228 struct btrfs_key *new_key,
2229 unsigned long split_offset)
2231 u32 item_size;
2232 struct extent_buffer *leaf;
2233 struct btrfs_key orig_key;
2234 struct btrfs_item *item;
2235 struct btrfs_item *new_item;
2236 int ret = 0;
2237 int slot;
2238 u32 nritems;
2239 u32 orig_offset;
2240 struct btrfs_disk_key disk_key;
2241 char *buf;
2243 leaf = path->nodes[0];
2244 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2245 if (btrfs_leaf_free_space(leaf) >=
2246 sizeof(struct btrfs_item))
2247 goto split;
2249 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2250 btrfs_release_path(path);
2252 path->search_for_split = 1;
2254 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2255 path->search_for_split = 0;
2257 /* if our item isn't there or got smaller, return now */
2258 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2259 path->slots[0])) {
2260 return -EAGAIN;
2263 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2264 BUG_ON(ret);
2266 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
2267 leaf = path->nodes[0];
2269 split:
2270 item = btrfs_item_nr(path->slots[0]);
2271 orig_offset = btrfs_item_offset(leaf, item);
2272 item_size = btrfs_item_size(leaf, item);
2275 buf = kmalloc(item_size, GFP_NOFS);
2276 BUG_ON(!buf);
2277 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2278 path->slots[0]), item_size);
2279 slot = path->slots[0] + 1;
2280 leaf = path->nodes[0];
2282 nritems = btrfs_header_nritems(leaf);
2284 if (slot < nritems) {
2285 /* shift the items */
2286 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2287 btrfs_item_nr_offset(slot),
2288 (nritems - slot) * sizeof(struct btrfs_item));
2292 btrfs_cpu_key_to_disk(&disk_key, new_key);
2293 btrfs_set_item_key(leaf, &disk_key, slot);
2295 new_item = btrfs_item_nr(slot);
2297 btrfs_set_item_offset(leaf, new_item, orig_offset);
2298 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2300 btrfs_set_item_offset(leaf, item,
2301 orig_offset + item_size - split_offset);
2302 btrfs_set_item_size(leaf, item, split_offset);
2304 btrfs_set_header_nritems(leaf, nritems + 1);
2306 /* write the data for the start of the original item */
2307 write_extent_buffer(leaf, buf,
2308 btrfs_item_ptr_offset(leaf, path->slots[0]),
2309 split_offset);
2311 /* write the data for the new item */
2312 write_extent_buffer(leaf, buf + split_offset,
2313 btrfs_item_ptr_offset(leaf, slot),
2314 item_size - split_offset);
2315 btrfs_mark_buffer_dirty(leaf);
2317 ret = 0;
2318 if (btrfs_leaf_free_space(leaf) < 0) {
2319 btrfs_print_leaf(leaf);
2320 BUG();
2322 kfree(buf);
2323 return ret;
2326 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2327 u32 new_size, int from_end)
2329 int ret = 0;
2330 int slot;
2331 struct extent_buffer *leaf;
2332 struct btrfs_item *item;
2333 u32 nritems;
2334 unsigned int data_end;
2335 unsigned int old_data_start;
2336 unsigned int old_size;
2337 unsigned int size_diff;
2338 int i;
2340 leaf = path->nodes[0];
2341 slot = path->slots[0];
2343 old_size = btrfs_item_size_nr(leaf, slot);
2344 if (old_size == new_size)
2345 return 0;
2347 nritems = btrfs_header_nritems(leaf);
2348 data_end = leaf_data_end(root->fs_info, leaf);
2350 old_data_start = btrfs_item_offset_nr(leaf, slot);
2352 size_diff = old_size - new_size;
2354 BUG_ON(slot < 0);
2355 BUG_ON(slot >= nritems);
2358 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2360 /* first correct the data pointers */
2361 for (i = slot; i < nritems; i++) {
2362 u32 ioff;
2363 item = btrfs_item_nr(i);
2364 ioff = btrfs_item_offset(leaf, item);
2365 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2368 /* shift the data */
2369 if (from_end) {
2370 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2371 data_end + size_diff, btrfs_leaf_data(leaf) +
2372 data_end, old_data_start + new_size - data_end);
2373 } else {
2374 struct btrfs_disk_key disk_key;
2375 u64 offset;
2377 btrfs_item_key(leaf, &disk_key, slot);
2379 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2380 unsigned long ptr;
2381 struct btrfs_file_extent_item *fi;
2383 fi = btrfs_item_ptr(leaf, slot,
2384 struct btrfs_file_extent_item);
2385 fi = (struct btrfs_file_extent_item *)(
2386 (unsigned long)fi - size_diff);
2388 if (btrfs_file_extent_type(leaf, fi) ==
2389 BTRFS_FILE_EXTENT_INLINE) {
2390 ptr = btrfs_item_ptr_offset(leaf, slot);
2391 memmove_extent_buffer(leaf, ptr,
2392 (unsigned long)fi,
2393 offsetof(struct btrfs_file_extent_item,
2394 disk_bytenr));
2398 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2399 data_end + size_diff, btrfs_leaf_data(leaf) +
2400 data_end, old_data_start - data_end);
2402 offset = btrfs_disk_key_offset(&disk_key);
2403 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2404 btrfs_set_item_key(leaf, &disk_key, slot);
2405 if (slot == 0)
2406 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2409 item = btrfs_item_nr(slot);
2410 btrfs_set_item_size(leaf, item, new_size);
2411 btrfs_mark_buffer_dirty(leaf);
2413 ret = 0;
2414 if (btrfs_leaf_free_space(leaf) < 0) {
2415 btrfs_print_leaf(leaf);
2416 BUG();
2418 return ret;
2421 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2422 u32 data_size)
2424 int ret = 0;
2425 int slot;
2426 struct extent_buffer *leaf;
2427 struct btrfs_item *item;
2428 u32 nritems;
2429 unsigned int data_end;
2430 unsigned int old_data;
2431 unsigned int old_size;
2432 int i;
2434 leaf = path->nodes[0];
2436 nritems = btrfs_header_nritems(leaf);
2437 data_end = leaf_data_end(root->fs_info, leaf);
2439 if (btrfs_leaf_free_space(leaf) < data_size) {
2440 btrfs_print_leaf(leaf);
2441 BUG();
2443 slot = path->slots[0];
2444 old_data = btrfs_item_end_nr(leaf, slot);
2446 BUG_ON(slot < 0);
2447 if (slot >= nritems) {
2448 btrfs_print_leaf(leaf);
2449 printk("slot %d too large, nritems %d\n", slot, nritems);
2450 BUG_ON(1);
2454 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2456 /* first correct the data pointers */
2457 for (i = slot; i < nritems; i++) {
2458 u32 ioff;
2459 item = btrfs_item_nr(i);
2460 ioff = btrfs_item_offset(leaf, item);
2461 btrfs_set_item_offset(leaf, item, ioff - data_size);
2464 /* shift the data */
2465 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2466 data_end - data_size, btrfs_leaf_data(leaf) +
2467 data_end, old_data - data_end);
2469 data_end = old_data;
2470 old_size = btrfs_item_size_nr(leaf, slot);
2471 item = btrfs_item_nr(slot);
2472 btrfs_set_item_size(leaf, item, old_size + data_size);
2473 btrfs_mark_buffer_dirty(leaf);
2475 ret = 0;
2476 if (btrfs_leaf_free_space(leaf) < 0) {
2477 btrfs_print_leaf(leaf);
2478 BUG();
2480 return ret;
2484 * Given a key and some data, insert an item into the tree.
2485 * This does all the path init required, making room in the tree if needed.
2487 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2488 struct btrfs_root *root,
2489 struct btrfs_path *path,
2490 struct btrfs_key *cpu_key, u32 *data_size,
2491 int nr)
2493 struct extent_buffer *leaf;
2494 struct btrfs_item *item;
2495 int ret = 0;
2496 int slot;
2497 int i;
2498 u32 nritems;
2499 u32 total_size = 0;
2500 u32 total_data = 0;
2501 unsigned int data_end;
2502 struct btrfs_disk_key disk_key;
2504 for (i = 0; i < nr; i++) {
2505 total_data += data_size[i];
2508 /* create a root if there isn't one */
2509 if (!root->node)
2510 BUG();
2512 total_size = total_data + nr * sizeof(struct btrfs_item);
2513 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2514 if (ret == 0) {
2515 return -EEXIST;
2517 if (ret < 0)
2518 goto out;
2520 leaf = path->nodes[0];
2522 nritems = btrfs_header_nritems(leaf);
2523 data_end = leaf_data_end(root->fs_info, leaf);
2525 if (btrfs_leaf_free_space(leaf) < total_size) {
2526 btrfs_print_leaf(leaf);
2527 printk("not enough freespace need %u have %d\n",
2528 total_size, btrfs_leaf_free_space(leaf));
2529 BUG();
2532 slot = path->slots[0];
2533 BUG_ON(slot < 0);
2535 if (slot < nritems) {
2536 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2538 if (old_data < data_end) {
2539 btrfs_print_leaf(leaf);
2540 printk("slot %d old_data %d data_end %d\n",
2541 slot, old_data, data_end);
2542 BUG_ON(1);
2545 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2547 /* first correct the data pointers */
2548 for (i = slot; i < nritems; i++) {
2549 u32 ioff;
2551 item = btrfs_item_nr(i);
2552 ioff = btrfs_item_offset(leaf, item);
2553 btrfs_set_item_offset(leaf, item, ioff - total_data);
2556 /* shift the items */
2557 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2558 btrfs_item_nr_offset(slot),
2559 (nritems - slot) * sizeof(struct btrfs_item));
2561 /* shift the data */
2562 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2563 data_end - total_data, btrfs_leaf_data(leaf) +
2564 data_end, old_data - data_end);
2565 data_end = old_data;
2568 /* setup the item for the new data */
2569 for (i = 0; i < nr; i++) {
2570 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2571 btrfs_set_item_key(leaf, &disk_key, slot + i);
2572 item = btrfs_item_nr(slot + i);
2573 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2574 data_end -= data_size[i];
2575 btrfs_set_item_size(leaf, item, data_size[i]);
2577 btrfs_set_header_nritems(leaf, nritems + nr);
2578 btrfs_mark_buffer_dirty(leaf);
2580 ret = 0;
2581 if (slot == 0) {
2582 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2583 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2586 if (btrfs_leaf_free_space(leaf) < 0) {
2587 btrfs_print_leaf(leaf);
2588 BUG();
2591 out:
2592 return ret;
2596 * Given a key and some data, insert an item into the tree.
2597 * This does all the path init required, making room in the tree if needed.
2599 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2600 *root, struct btrfs_key *cpu_key, void *data, u32
2601 data_size)
2603 int ret = 0;
2604 struct btrfs_path *path;
2605 struct extent_buffer *leaf;
2606 unsigned long ptr;
2608 path = btrfs_alloc_path();
2609 if (!path)
2610 return -ENOMEM;
2612 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2613 if (!ret) {
2614 leaf = path->nodes[0];
2615 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2616 write_extent_buffer(leaf, data, ptr, data_size);
2617 btrfs_mark_buffer_dirty(leaf);
2619 btrfs_free_path(path);
2620 return ret;
2624 * delete the pointer from a given node.
2626 * If the delete empties a node, the node is removed from the tree,
2627 * continuing all the way the root if required. The root is converted into
2628 * a leaf if all the nodes are emptied.
2630 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2631 int level, int slot)
2633 struct extent_buffer *parent = path->nodes[level];
2634 u32 nritems;
2635 int ret = 0;
2637 nritems = btrfs_header_nritems(parent);
2638 if (slot < nritems - 1) {
2639 /* shift the items */
2640 memmove_extent_buffer(parent,
2641 btrfs_node_key_ptr_offset(slot),
2642 btrfs_node_key_ptr_offset(slot + 1),
2643 sizeof(struct btrfs_key_ptr) *
2644 (nritems - slot - 1));
2646 nritems--;
2647 btrfs_set_header_nritems(parent, nritems);
2648 if (nritems == 0 && parent == root->node) {
2649 BUG_ON(btrfs_header_level(root->node) != 1);
2650 /* just turn the root into a leaf and break */
2651 btrfs_set_header_level(root->node, 0);
2652 } else if (slot == 0) {
2653 struct btrfs_disk_key disk_key;
2655 btrfs_node_key(parent, &disk_key, 0);
2656 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2658 btrfs_mark_buffer_dirty(parent);
2659 return ret;
2663 * a helper function to delete the leaf pointed to by path->slots[1] and
2664 * path->nodes[1].
2666 * This deletes the pointer in path->nodes[1] and frees the leaf
2667 * block extent. zero is returned if it all worked out, < 0 otherwise.
2669 * The path must have already been setup for deleting the leaf, including
2670 * all the proper balancing. path->nodes[1] must be locked.
2672 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2673 struct btrfs_root *root,
2674 struct btrfs_path *path,
2675 struct extent_buffer *leaf)
2677 int ret;
2679 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2680 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2681 if (ret)
2682 return ret;
2684 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2685 0, root->root_key.objectid, 0, 0);
2686 return ret;
2690 * delete the item at the leaf level in path. If that empties
2691 * the leaf, remove it from the tree
2693 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2694 struct btrfs_path *path, int slot, int nr)
2696 struct extent_buffer *leaf;
2697 struct btrfs_item *item;
2698 int last_off;
2699 int dsize = 0;
2700 int ret = 0;
2701 int wret;
2702 int i;
2703 u32 nritems;
2705 leaf = path->nodes[0];
2706 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2708 for (i = 0; i < nr; i++)
2709 dsize += btrfs_item_size_nr(leaf, slot + i);
2711 nritems = btrfs_header_nritems(leaf);
2713 if (slot + nr != nritems) {
2714 int data_end = leaf_data_end(root->fs_info, leaf);
2716 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2717 data_end + dsize,
2718 btrfs_leaf_data(leaf) + data_end,
2719 last_off - data_end);
2721 for (i = slot + nr; i < nritems; i++) {
2722 u32 ioff;
2724 item = btrfs_item_nr(i);
2725 ioff = btrfs_item_offset(leaf, item);
2726 btrfs_set_item_offset(leaf, item, ioff + dsize);
2729 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2730 btrfs_item_nr_offset(slot + nr),
2731 sizeof(struct btrfs_item) *
2732 (nritems - slot - nr));
2734 btrfs_set_header_nritems(leaf, nritems - nr);
2735 nritems -= nr;
2737 /* delete the leaf if we've emptied it */
2738 if (nritems == 0) {
2739 if (leaf == root->node) {
2740 btrfs_set_header_level(leaf, 0);
2741 } else {
2742 clean_tree_block(trans, root, leaf);
2743 wret = btrfs_del_leaf(trans, root, path, leaf);
2744 BUG_ON(ret);
2745 if (wret)
2746 ret = wret;
2748 } else {
2749 int used = leaf_space_used(leaf, 0, nritems);
2750 if (slot == 0) {
2751 struct btrfs_disk_key disk_key;
2753 btrfs_item_key(leaf, &disk_key, 0);
2754 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2757 /* delete the leaf if it is mostly empty */
2758 if (used < BTRFS_LEAF_DATA_SIZE(root->fs_info) / 4) {
2759 /* push_leaf_left fixes the path.
2760 * make sure the path still points to our leaf
2761 * for possible call to del_ptr below
2763 slot = path->slots[1];
2764 extent_buffer_get(leaf);
2766 wret = push_leaf_left(trans, root, path, 1, 1);
2767 if (wret < 0 && wret != -ENOSPC)
2768 ret = wret;
2770 if (path->nodes[0] == leaf &&
2771 btrfs_header_nritems(leaf)) {
2772 wret = push_leaf_right(trans, root, path, 1, 1);
2773 if (wret < 0 && wret != -ENOSPC)
2774 ret = wret;
2777 if (btrfs_header_nritems(leaf) == 0) {
2778 clean_tree_block(trans, root, leaf);
2779 path->slots[1] = slot;
2780 ret = btrfs_del_leaf(trans, root, path, leaf);
2781 BUG_ON(ret);
2782 free_extent_buffer(leaf);
2784 } else {
2785 btrfs_mark_buffer_dirty(leaf);
2786 free_extent_buffer(leaf);
2788 } else {
2789 btrfs_mark_buffer_dirty(leaf);
2792 return ret;
2796 * walk up the tree as far as required to find the previous leaf.
2797 * returns 0 if it found something or 1 if there are no lesser leaves.
2798 * returns < 0 on io errors.
2800 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2802 int slot;
2803 int level = 1;
2804 struct extent_buffer *c;
2805 struct extent_buffer *next = NULL;
2806 struct btrfs_fs_info *fs_info = root->fs_info;
2808 while(level < BTRFS_MAX_LEVEL) {
2809 if (!path->nodes[level])
2810 return 1;
2812 slot = path->slots[level];
2813 c = path->nodes[level];
2814 if (slot == 0) {
2815 level++;
2816 if (level == BTRFS_MAX_LEVEL)
2817 return 1;
2818 continue;
2820 slot--;
2822 next = read_node_slot(fs_info, c, slot);
2823 if (!extent_buffer_uptodate(next)) {
2824 if (IS_ERR(next))
2825 return PTR_ERR(next);
2826 return -EIO;
2828 break;
2830 path->slots[level] = slot;
2831 while(1) {
2832 level--;
2833 c = path->nodes[level];
2834 free_extent_buffer(c);
2835 slot = btrfs_header_nritems(next);
2836 if (slot != 0)
2837 slot--;
2838 path->nodes[level] = next;
2839 path->slots[level] = slot;
2840 if (!level)
2841 break;
2842 next = read_node_slot(fs_info, next, slot);
2843 if (!extent_buffer_uptodate(next)) {
2844 if (IS_ERR(next))
2845 return PTR_ERR(next);
2846 return -EIO;
2849 return 0;
2853 * walk up the tree as far as required to find the next leaf.
2854 * returns 0 if it found something or 1 if there are no greater leaves.
2855 * returns < 0 on io errors.
2857 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2859 int slot;
2860 int level = 1;
2861 struct extent_buffer *c;
2862 struct extent_buffer *next = NULL;
2863 struct btrfs_fs_info *fs_info = root->fs_info;
2865 while(level < BTRFS_MAX_LEVEL) {
2866 if (!path->nodes[level])
2867 return 1;
2869 slot = path->slots[level] + 1;
2870 c = path->nodes[level];
2871 if (slot >= btrfs_header_nritems(c)) {
2872 level++;
2873 if (level == BTRFS_MAX_LEVEL)
2874 return 1;
2875 continue;
2878 if (path->reada)
2879 reada_for_search(root, path, level, slot, 0);
2881 next = read_node_slot(fs_info, c, slot);
2882 if (!extent_buffer_uptodate(next))
2883 return -EIO;
2884 break;
2886 path->slots[level] = slot;
2887 while(1) {
2888 level--;
2889 c = path->nodes[level];
2890 free_extent_buffer(c);
2891 path->nodes[level] = next;
2892 path->slots[level] = 0;
2893 if (!level)
2894 break;
2895 if (path->reada)
2896 reada_for_search(root, path, level, 0, 0);
2897 next = read_node_slot(fs_info, next, 0);
2898 if (!extent_buffer_uptodate(next))
2899 return -EIO;
2901 return 0;
2904 int btrfs_previous_item(struct btrfs_root *root,
2905 struct btrfs_path *path, u64 min_objectid,
2906 int type)
2908 struct btrfs_key found_key;
2909 struct extent_buffer *leaf;
2910 u32 nritems;
2911 int ret;
2913 while(1) {
2914 if (path->slots[0] == 0) {
2915 ret = btrfs_prev_leaf(root, path);
2916 if (ret != 0)
2917 return ret;
2918 } else {
2919 path->slots[0]--;
2921 leaf = path->nodes[0];
2922 nritems = btrfs_header_nritems(leaf);
2923 if (nritems == 0)
2924 return 1;
2925 if (path->slots[0] == nritems)
2926 path->slots[0]--;
2928 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2929 if (found_key.objectid < min_objectid)
2930 break;
2931 if (found_key.type == type)
2932 return 0;
2933 if (found_key.objectid == min_objectid &&
2934 found_key.type < type)
2935 break;
2937 return 1;
2941 * search in extent tree to find a previous Metadata/Data extent item with
2942 * min objecitd.
2944 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2946 int btrfs_previous_extent_item(struct btrfs_root *root,
2947 struct btrfs_path *path, u64 min_objectid)
2949 struct btrfs_key found_key;
2950 struct extent_buffer *leaf;
2951 u32 nritems;
2952 int ret;
2954 while (1) {
2955 if (path->slots[0] == 0) {
2956 ret = btrfs_prev_leaf(root, path);
2957 if (ret != 0)
2958 return ret;
2959 } else {
2960 path->slots[0]--;
2962 leaf = path->nodes[0];
2963 nritems = btrfs_header_nritems(leaf);
2964 if (nritems == 0)
2965 return 1;
2966 if (path->slots[0] == nritems)
2967 path->slots[0]--;
2969 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2970 if (found_key.objectid < min_objectid)
2971 break;
2972 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2973 found_key.type == BTRFS_METADATA_ITEM_KEY)
2974 return 0;
2975 if (found_key.objectid == min_objectid &&
2976 found_key.type < BTRFS_EXTENT_ITEM_KEY)
2977 break;
2979 return 1;
2983 * Search in extent tree to found next meta/data extent
2984 * Caller needs to check for no-hole or skinny metadata features.
2986 int btrfs_next_extent_item(struct btrfs_root *root,
2987 struct btrfs_path *path, u64 max_objectid)
2989 struct btrfs_key found_key;
2990 int ret;
2992 while (1) {
2993 ret = btrfs_next_item(root, path);
2994 if (ret)
2995 return ret;
2996 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2997 path->slots[0]);
2998 if (found_key.objectid > max_objectid)
2999 return 1;
3000 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3001 found_key.type == BTRFS_METADATA_ITEM_KEY)
3002 return 0;