btrfs-progs: recieve: add missing short option E to getopt
[btrfs-progs-unstable/devel.git] / ctree.c
blobf3c13e1ce263915cc732cacc53446c8fb8d2c354
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include "ctree.h"
19 #include "disk-io.h"
20 #include "transaction.h"
21 #include "print-tree.h"
22 #include "repair.h"
23 #include "internal.h"
24 #include "sizes.h"
26 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_key *ins_key,
30 struct btrfs_path *path, int data_size, int extend);
31 static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
33 struct extent_buffer *src, int empty);
34 static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
39 inline void btrfs_init_path(struct btrfs_path *p)
41 memset(p, 0, sizeof(*p));
44 struct btrfs_path *btrfs_alloc_path(void)
46 struct btrfs_path *path;
47 path = kzalloc(sizeof(struct btrfs_path), GFP_NOFS);
48 return path;
51 void btrfs_free_path(struct btrfs_path *p)
53 if (!p)
54 return;
55 btrfs_release_path(p);
56 kfree(p);
59 void btrfs_release_path(struct btrfs_path *p)
61 int i;
62 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
63 if (!p->nodes[i])
64 continue;
65 free_extent_buffer(p->nodes[i]);
67 memset(p, 0, sizeof(*p));
70 void add_root_to_dirty_list(struct btrfs_root *root)
72 if (root->track_dirty && list_empty(&root->dirty_list)) {
73 list_add(&root->dirty_list,
74 &root->fs_info->dirty_cowonly_roots);
78 int btrfs_copy_root(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root,
80 struct extent_buffer *buf,
81 struct extent_buffer **cow_ret, u64 new_root_objectid)
83 struct extent_buffer *cow;
84 int ret = 0;
85 int level;
86 struct btrfs_root *new_root;
87 struct btrfs_disk_key disk_key;
89 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
90 if (!new_root)
91 return -ENOMEM;
93 memcpy(new_root, root, sizeof(*new_root));
94 new_root->root_key.objectid = new_root_objectid;
96 WARN_ON(root->ref_cows && trans->transid !=
97 root->fs_info->running_transaction->transid);
98 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
100 level = btrfs_header_level(buf);
101 if (level == 0)
102 btrfs_item_key(buf, &disk_key, 0);
103 else
104 btrfs_node_key(buf, &disk_key, 0);
105 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
106 new_root_objectid, &disk_key,
107 level, buf->start, 0);
108 if (IS_ERR(cow)) {
109 kfree(new_root);
110 return PTR_ERR(cow);
113 copy_extent_buffer(cow, buf, 0, 0, cow->len);
114 btrfs_set_header_bytenr(cow, cow->start);
115 btrfs_set_header_generation(cow, trans->transid);
116 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
117 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
118 BTRFS_HEADER_FLAG_RELOC);
119 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
120 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
121 else
122 btrfs_set_header_owner(cow, new_root_objectid);
124 write_extent_buffer(cow, root->fs_info->fsid,
125 btrfs_header_fsid(), BTRFS_FSID_SIZE);
127 WARN_ON(btrfs_header_generation(buf) > trans->transid);
128 ret = btrfs_inc_ref(trans, new_root, cow, 0);
129 kfree(new_root);
131 if (ret)
132 return ret;
134 btrfs_mark_buffer_dirty(cow);
135 *cow_ret = cow;
136 return 0;
140 * check if the tree block can be shared by multiple trees
142 static int btrfs_block_can_be_shared(struct btrfs_root *root,
143 struct extent_buffer *buf)
146 * Tree blocks not in reference counted trees and tree roots
147 * are never shared. If a block was allocated after the last
148 * snapshot and the block was not allocated by tree relocation,
149 * we know the block is not shared.
151 if (root->ref_cows &&
152 buf != root->node && buf != root->commit_root &&
153 (btrfs_header_generation(buf) <=
154 btrfs_root_last_snapshot(&root->root_item) ||
155 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
156 return 1;
157 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
158 if (root->ref_cows &&
159 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
160 return 1;
161 #endif
162 return 0;
165 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
166 struct btrfs_root *root,
167 struct extent_buffer *buf,
168 struct extent_buffer *cow)
170 u64 refs;
171 u64 owner;
172 u64 flags;
173 u64 new_flags = 0;
174 int ret;
177 * Backrefs update rules:
179 * Always use full backrefs for extent pointers in tree block
180 * allocated by tree relocation.
182 * If a shared tree block is no longer referenced by its owner
183 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
184 * use full backrefs for extent pointers in tree block.
186 * If a tree block is been relocating
187 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
188 * use full backrefs for extent pointers in tree block.
189 * The reason for this is some operations (such as drop tree)
190 * are only allowed for blocks use full backrefs.
193 if (btrfs_block_can_be_shared(root, buf)) {
194 ret = btrfs_lookup_extent_info(trans, root, buf->start,
195 btrfs_header_level(buf), 1,
196 &refs, &flags);
197 BUG_ON(ret);
198 BUG_ON(refs == 0);
199 } else {
200 refs = 1;
201 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
202 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
203 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
204 else
205 flags = 0;
208 owner = btrfs_header_owner(buf);
209 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
210 owner == BTRFS_TREE_RELOC_OBJECTID);
212 if (refs > 1) {
213 if ((owner == root->root_key.objectid ||
214 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
215 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
216 ret = btrfs_inc_ref(trans, root, buf, 1);
217 BUG_ON(ret);
219 if (root->root_key.objectid ==
220 BTRFS_TREE_RELOC_OBJECTID) {
221 ret = btrfs_dec_ref(trans, root, buf, 0);
222 BUG_ON(ret);
223 ret = btrfs_inc_ref(trans, root, cow, 1);
224 BUG_ON(ret);
226 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
227 } else {
229 if (root->root_key.objectid ==
230 BTRFS_TREE_RELOC_OBJECTID)
231 ret = btrfs_inc_ref(trans, root, cow, 1);
232 else
233 ret = btrfs_inc_ref(trans, root, cow, 0);
234 BUG_ON(ret);
236 if (new_flags != 0) {
237 ret = btrfs_set_block_flags(trans, root, buf->start,
238 btrfs_header_level(buf),
239 new_flags);
240 BUG_ON(ret);
242 } else {
243 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
244 if (root->root_key.objectid ==
245 BTRFS_TREE_RELOC_OBJECTID)
246 ret = btrfs_inc_ref(trans, root, cow, 1);
247 else
248 ret = btrfs_inc_ref(trans, root, cow, 0);
249 BUG_ON(ret);
250 ret = btrfs_dec_ref(trans, root, buf, 1);
251 BUG_ON(ret);
253 clean_tree_block(trans, root, buf);
255 return 0;
258 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
259 struct btrfs_root *root,
260 struct extent_buffer *buf,
261 struct extent_buffer *parent, int parent_slot,
262 struct extent_buffer **cow_ret,
263 u64 search_start, u64 empty_size)
265 struct extent_buffer *cow;
266 struct btrfs_disk_key disk_key;
267 int level;
269 WARN_ON(root->ref_cows && trans->transid !=
270 root->fs_info->running_transaction->transid);
271 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
273 level = btrfs_header_level(buf);
275 if (level == 0)
276 btrfs_item_key(buf, &disk_key, 0);
277 else
278 btrfs_node_key(buf, &disk_key, 0);
280 cow = btrfs_alloc_free_block(trans, root, buf->len,
281 root->root_key.objectid, &disk_key,
282 level, search_start, empty_size);
283 if (IS_ERR(cow))
284 return PTR_ERR(cow);
286 copy_extent_buffer(cow, buf, 0, 0, cow->len);
287 btrfs_set_header_bytenr(cow, cow->start);
288 btrfs_set_header_generation(cow, trans->transid);
289 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
290 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
291 BTRFS_HEADER_FLAG_RELOC);
292 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
293 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
294 else
295 btrfs_set_header_owner(cow, root->root_key.objectid);
297 write_extent_buffer(cow, root->fs_info->fsid,
298 btrfs_header_fsid(), BTRFS_FSID_SIZE);
300 WARN_ON(!(buf->flags & EXTENT_BAD_TRANSID) &&
301 btrfs_header_generation(buf) > trans->transid);
303 update_ref_for_cow(trans, root, buf, cow);
305 if (buf == root->node) {
306 root->node = cow;
307 extent_buffer_get(cow);
309 btrfs_free_extent(trans, root, buf->start, buf->len,
310 0, root->root_key.objectid, level, 0);
311 free_extent_buffer(buf);
312 add_root_to_dirty_list(root);
313 } else {
314 btrfs_set_node_blockptr(parent, parent_slot,
315 cow->start);
316 WARN_ON(trans->transid == 0);
317 btrfs_set_node_ptr_generation(parent, parent_slot,
318 trans->transid);
319 btrfs_mark_buffer_dirty(parent);
320 WARN_ON(btrfs_header_generation(parent) != trans->transid);
322 btrfs_free_extent(trans, root, buf->start, buf->len,
323 0, root->root_key.objectid, level, 1);
325 if (!list_empty(&buf->recow)) {
326 list_del_init(&buf->recow);
327 free_extent_buffer(buf);
329 free_extent_buffer(buf);
330 btrfs_mark_buffer_dirty(cow);
331 *cow_ret = cow;
332 return 0;
335 static inline int should_cow_block(struct btrfs_trans_handle *trans,
336 struct btrfs_root *root,
337 struct extent_buffer *buf)
339 if (btrfs_header_generation(buf) == trans->transid &&
340 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
341 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
342 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
343 return 0;
344 return 1;
347 int btrfs_cow_block(struct btrfs_trans_handle *trans,
348 struct btrfs_root *root, struct extent_buffer *buf,
349 struct extent_buffer *parent, int parent_slot,
350 struct extent_buffer **cow_ret)
352 u64 search_start;
353 int ret;
355 if (trans->transaction != root->fs_info->running_transaction) {
356 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
357 root->fs_info->running_transaction->transid);
358 WARN_ON(1);
361 if (trans->transid != root->fs_info->generation) {
362 printk(KERN_CRIT "trans %llu running %llu\n",
363 (unsigned long long)trans->transid,
364 (unsigned long long)root->fs_info->generation);
365 WARN_ON(1);
367 if (!should_cow_block(trans, root, buf)) {
368 *cow_ret = buf;
369 return 0;
372 search_start = buf->start & ~((u64)SZ_1G - 1);
373 ret = __btrfs_cow_block(trans, root, buf, parent,
374 parent_slot, cow_ret, search_start, 0);
375 return ret;
378 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
380 if (k1->objectid > k2->objectid)
381 return 1;
382 if (k1->objectid < k2->objectid)
383 return -1;
384 if (k1->type > k2->type)
385 return 1;
386 if (k1->type < k2->type)
387 return -1;
388 if (k1->offset > k2->offset)
389 return 1;
390 if (k1->offset < k2->offset)
391 return -1;
392 return 0;
396 * compare two keys in a memcmp fashion
398 static int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
400 struct btrfs_key k1;
402 btrfs_disk_key_to_cpu(&k1, disk);
403 return btrfs_comp_cpu_keys(&k1, k2);
407 * The leaf data grows from end-to-front in the node.
408 * this returns the address of the start of the last item,
409 * which is the stop of the leaf data stack
411 static inline unsigned int leaf_data_end(struct btrfs_root *root,
412 struct extent_buffer *leaf)
414 u32 nr = btrfs_header_nritems(leaf);
415 if (nr == 0)
416 return BTRFS_LEAF_DATA_SIZE(root);
417 return btrfs_item_offset_nr(leaf, nr - 1);
420 enum btrfs_tree_block_status
421 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
422 struct extent_buffer *buf)
424 int i;
425 struct btrfs_key cpukey;
426 struct btrfs_disk_key key;
427 u32 nritems = btrfs_header_nritems(buf);
428 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
430 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root))
431 goto fail;
433 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
434 if (parent_key && parent_key->type) {
435 btrfs_node_key(buf, &key, 0);
436 if (memcmp(parent_key, &key, sizeof(key)))
437 goto fail;
439 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
440 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
441 btrfs_node_key(buf, &key, i);
442 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
443 if (btrfs_comp_keys(&key, &cpukey) >= 0)
444 goto fail;
446 return BTRFS_TREE_BLOCK_CLEAN;
447 fail:
448 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
449 if (parent_key)
450 btrfs_disk_key_to_cpu(&cpukey, parent_key);
451 else
452 btrfs_node_key_to_cpu(buf, &cpukey, 0);
453 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
454 buf->start, buf->len,
455 btrfs_header_level(buf));
457 return ret;
460 enum btrfs_tree_block_status
461 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
462 struct extent_buffer *buf)
464 int i;
465 struct btrfs_key cpukey;
466 struct btrfs_disk_key key;
467 u32 nritems = btrfs_header_nritems(buf);
468 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
470 if (nritems * sizeof(struct btrfs_item) > buf->len) {
471 fprintf(stderr, "invalid number of items %llu\n",
472 (unsigned long long)buf->start);
473 goto fail;
476 if (btrfs_header_level(buf) != 0) {
477 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
478 fprintf(stderr, "leaf is not a leaf %llu\n",
479 (unsigned long long)btrfs_header_bytenr(buf));
480 goto fail;
482 if (btrfs_leaf_free_space(root, buf) < 0) {
483 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
484 fprintf(stderr, "leaf free space incorrect %llu %d\n",
485 (unsigned long long)btrfs_header_bytenr(buf),
486 btrfs_leaf_free_space(root, buf));
487 goto fail;
490 if (nritems == 0)
491 return BTRFS_TREE_BLOCK_CLEAN;
493 btrfs_item_key(buf, &key, 0);
494 if (parent_key && parent_key->type &&
495 memcmp(parent_key, &key, sizeof(key))) {
496 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
497 fprintf(stderr, "leaf parent key incorrect %llu\n",
498 (unsigned long long)btrfs_header_bytenr(buf));
499 goto fail;
501 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
502 btrfs_item_key(buf, &key, i);
503 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
504 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
505 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
506 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
507 goto fail;
509 if (btrfs_item_offset_nr(buf, i) !=
510 btrfs_item_end_nr(buf, i + 1)) {
511 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
512 fprintf(stderr, "incorrect offsets %u %u\n",
513 btrfs_item_offset_nr(buf, i),
514 btrfs_item_end_nr(buf, i + 1));
515 goto fail;
517 if (i == 0 && btrfs_item_end_nr(buf, i) !=
518 BTRFS_LEAF_DATA_SIZE(root)) {
519 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
520 fprintf(stderr, "bad item end %u wanted %u\n",
521 btrfs_item_end_nr(buf, i),
522 (unsigned)BTRFS_LEAF_DATA_SIZE(root));
523 goto fail;
527 for (i = 0; i < nritems; i++) {
528 if (btrfs_item_end_nr(buf, i) > BTRFS_LEAF_DATA_SIZE(root)) {
529 btrfs_item_key(buf, &key, 0);
530 btrfs_print_key(&key);
531 fflush(stdout);
532 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
533 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
534 (unsigned long long)btrfs_item_end_nr(buf, i),
535 (unsigned long long)BTRFS_LEAF_DATA_SIZE(root));
536 goto fail;
540 return BTRFS_TREE_BLOCK_CLEAN;
541 fail:
542 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID) {
543 if (parent_key)
544 btrfs_disk_key_to_cpu(&cpukey, parent_key);
545 else
546 btrfs_item_key_to_cpu(buf, &cpukey, 0);
548 btrfs_add_corrupt_extent_record(root->fs_info, &cpukey,
549 buf->start, buf->len, 0);
551 return ret;
554 static int noinline check_block(struct btrfs_root *root,
555 struct btrfs_path *path, int level)
557 struct btrfs_disk_key key;
558 struct btrfs_disk_key *key_ptr = NULL;
559 struct extent_buffer *parent;
560 enum btrfs_tree_block_status ret;
562 if (path->skip_check_block)
563 return 0;
564 if (path->nodes[level + 1]) {
565 parent = path->nodes[level + 1];
566 btrfs_node_key(parent, &key, path->slots[level + 1]);
567 key_ptr = &key;
569 if (level == 0)
570 ret = btrfs_check_leaf(root, key_ptr, path->nodes[0]);
571 else
572 ret = btrfs_check_node(root, key_ptr, path->nodes[level]);
573 if (ret == BTRFS_TREE_BLOCK_CLEAN)
574 return 0;
575 return -EIO;
579 * search for key in the extent_buffer. The items start at offset p,
580 * and they are item_size apart. There are 'max' items in p.
582 * the slot in the array is returned via slot, and it points to
583 * the place where you would insert key if it is not found in
584 * the array.
586 * slot may point to max if the key is bigger than all of the keys
588 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
589 int item_size, struct btrfs_key *key,
590 int max, int *slot)
592 int low = 0;
593 int high = max;
594 int mid;
595 int ret;
596 unsigned long offset;
597 struct btrfs_disk_key *tmp;
599 while(low < high) {
600 mid = (low + high) / 2;
601 offset = p + mid * item_size;
603 tmp = (struct btrfs_disk_key *)(eb->data + offset);
604 ret = btrfs_comp_keys(tmp, key);
606 if (ret < 0)
607 low = mid + 1;
608 else if (ret > 0)
609 high = mid;
610 else {
611 *slot = mid;
612 return 0;
615 *slot = low;
616 return 1;
620 * simple bin_search frontend that does the right thing for
621 * leaves vs nodes
623 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
624 int level, int *slot)
626 if (level == 0)
627 return generic_bin_search(eb,
628 offsetof(struct btrfs_leaf, items),
629 sizeof(struct btrfs_item),
630 key, btrfs_header_nritems(eb),
631 slot);
632 else
633 return generic_bin_search(eb,
634 offsetof(struct btrfs_node, ptrs),
635 sizeof(struct btrfs_key_ptr),
636 key, btrfs_header_nritems(eb),
637 slot);
640 struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
641 struct extent_buffer *parent, int slot)
643 int level = btrfs_header_level(parent);
644 if (slot < 0)
645 return NULL;
646 if (slot >= btrfs_header_nritems(parent))
647 return NULL;
649 if (level == 0)
650 return NULL;
652 return read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
653 fs_info->nodesize,
654 btrfs_node_ptr_generation(parent, slot));
657 static int balance_level(struct btrfs_trans_handle *trans,
658 struct btrfs_root *root,
659 struct btrfs_path *path, int level)
661 struct extent_buffer *right = NULL;
662 struct extent_buffer *mid;
663 struct extent_buffer *left = NULL;
664 struct extent_buffer *parent = NULL;
665 struct btrfs_fs_info *fs_info = root->fs_info;
666 int ret = 0;
667 int wret;
668 int pslot;
669 int orig_slot = path->slots[level];
670 u64 orig_ptr;
672 if (level == 0)
673 return 0;
675 mid = path->nodes[level];
676 WARN_ON(btrfs_header_generation(mid) != trans->transid);
678 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
680 if (level < BTRFS_MAX_LEVEL - 1) {
681 parent = path->nodes[level + 1];
682 pslot = path->slots[level + 1];
686 * deal with the case where there is only one pointer in the root
687 * by promoting the node below to a root
689 if (!parent) {
690 struct extent_buffer *child;
692 if (btrfs_header_nritems(mid) != 1)
693 return 0;
695 /* promote the child to a root */
696 child = read_node_slot(fs_info, mid, 0);
697 BUG_ON(!extent_buffer_uptodate(child));
698 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
699 BUG_ON(ret);
701 root->node = child;
702 add_root_to_dirty_list(root);
703 path->nodes[level] = NULL;
704 clean_tree_block(trans, root, mid);
705 /* once for the path */
706 free_extent_buffer(mid);
708 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
709 0, root->root_key.objectid,
710 level, 1);
711 /* once for the root ptr */
712 free_extent_buffer(mid);
713 return ret;
715 if (btrfs_header_nritems(mid) >
716 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
717 return 0;
719 left = read_node_slot(fs_info, parent, pslot - 1);
720 if (extent_buffer_uptodate(left)) {
721 wret = btrfs_cow_block(trans, root, left,
722 parent, pslot - 1, &left);
723 if (wret) {
724 ret = wret;
725 goto enospc;
728 right = read_node_slot(fs_info, parent, pslot + 1);
729 if (extent_buffer_uptodate(right)) {
730 wret = btrfs_cow_block(trans, root, right,
731 parent, pslot + 1, &right);
732 if (wret) {
733 ret = wret;
734 goto enospc;
738 /* first, try to make some room in the middle buffer */
739 if (left) {
740 orig_slot += btrfs_header_nritems(left);
741 wret = push_node_left(trans, root, left, mid, 1);
742 if (wret < 0)
743 ret = wret;
747 * then try to empty the right most buffer into the middle
749 if (right) {
750 wret = push_node_left(trans, root, mid, right, 1);
751 if (wret < 0 && wret != -ENOSPC)
752 ret = wret;
753 if (btrfs_header_nritems(right) == 0) {
754 u64 bytenr = right->start;
755 u32 blocksize = right->len;
757 clean_tree_block(trans, root, right);
758 free_extent_buffer(right);
759 right = NULL;
760 wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
761 if (wret)
762 ret = wret;
763 wret = btrfs_free_extent(trans, root, bytenr,
764 blocksize, 0,
765 root->root_key.objectid,
766 level, 0);
767 if (wret)
768 ret = wret;
769 } else {
770 struct btrfs_disk_key right_key;
771 btrfs_node_key(right, &right_key, 0);
772 btrfs_set_node_key(parent, &right_key, pslot + 1);
773 btrfs_mark_buffer_dirty(parent);
776 if (btrfs_header_nritems(mid) == 1) {
778 * we're not allowed to leave a node with one item in the
779 * tree during a delete. A deletion from lower in the tree
780 * could try to delete the only pointer in this node.
781 * So, pull some keys from the left.
782 * There has to be a left pointer at this point because
783 * otherwise we would have pulled some pointers from the
784 * right
786 BUG_ON(!left);
787 wret = balance_node_right(trans, root, mid, left);
788 if (wret < 0) {
789 ret = wret;
790 goto enospc;
792 if (wret == 1) {
793 wret = push_node_left(trans, root, left, mid, 1);
794 if (wret < 0)
795 ret = wret;
797 BUG_ON(wret == 1);
799 if (btrfs_header_nritems(mid) == 0) {
800 /* we've managed to empty the middle node, drop it */
801 u64 bytenr = mid->start;
802 u32 blocksize = mid->len;
803 clean_tree_block(trans, root, mid);
804 free_extent_buffer(mid);
805 mid = NULL;
806 wret = btrfs_del_ptr(root, path, level + 1, pslot);
807 if (wret)
808 ret = wret;
809 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
810 0, root->root_key.objectid,
811 level, 0);
812 if (wret)
813 ret = wret;
814 } else {
815 /* update the parent key to reflect our changes */
816 struct btrfs_disk_key mid_key;
817 btrfs_node_key(mid, &mid_key, 0);
818 btrfs_set_node_key(parent, &mid_key, pslot);
819 btrfs_mark_buffer_dirty(parent);
822 /* update the path */
823 if (left) {
824 if (btrfs_header_nritems(left) > orig_slot) {
825 extent_buffer_get(left);
826 path->nodes[level] = left;
827 path->slots[level + 1] -= 1;
828 path->slots[level] = orig_slot;
829 if (mid)
830 free_extent_buffer(mid);
831 } else {
832 orig_slot -= btrfs_header_nritems(left);
833 path->slots[level] = orig_slot;
836 /* double check we haven't messed things up */
837 check_block(root, path, level);
838 if (orig_ptr !=
839 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
840 BUG();
841 enospc:
842 if (right)
843 free_extent_buffer(right);
844 if (left)
845 free_extent_buffer(left);
846 return ret;
849 /* returns zero if the push worked, non-zero otherwise */
850 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
851 struct btrfs_root *root,
852 struct btrfs_path *path, int level)
854 struct extent_buffer *right = NULL;
855 struct extent_buffer *mid;
856 struct extent_buffer *left = NULL;
857 struct extent_buffer *parent = NULL;
858 struct btrfs_fs_info *fs_info = root->fs_info;
859 int ret = 0;
860 int wret;
861 int pslot;
862 int orig_slot = path->slots[level];
864 if (level == 0)
865 return 1;
867 mid = path->nodes[level];
868 WARN_ON(btrfs_header_generation(mid) != trans->transid);
870 if (level < BTRFS_MAX_LEVEL - 1) {
871 parent = path->nodes[level + 1];
872 pslot = path->slots[level + 1];
875 if (!parent)
876 return 1;
878 left = read_node_slot(fs_info, parent, pslot - 1);
880 /* first, try to make some room in the middle buffer */
881 if (extent_buffer_uptodate(left)) {
882 u32 left_nr;
883 left_nr = btrfs_header_nritems(left);
884 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
885 wret = 1;
886 } else {
887 ret = btrfs_cow_block(trans, root, left, parent,
888 pslot - 1, &left);
889 if (ret)
890 wret = 1;
891 else {
892 wret = push_node_left(trans, root,
893 left, mid, 0);
896 if (wret < 0)
897 ret = wret;
898 if (wret == 0) {
899 struct btrfs_disk_key disk_key;
900 orig_slot += left_nr;
901 btrfs_node_key(mid, &disk_key, 0);
902 btrfs_set_node_key(parent, &disk_key, pslot);
903 btrfs_mark_buffer_dirty(parent);
904 if (btrfs_header_nritems(left) > orig_slot) {
905 path->nodes[level] = left;
906 path->slots[level + 1] -= 1;
907 path->slots[level] = orig_slot;
908 free_extent_buffer(mid);
909 } else {
910 orig_slot -=
911 btrfs_header_nritems(left);
912 path->slots[level] = orig_slot;
913 free_extent_buffer(left);
915 return 0;
917 free_extent_buffer(left);
919 right= read_node_slot(fs_info, parent, pslot + 1);
922 * then try to empty the right most buffer into the middle
924 if (extent_buffer_uptodate(right)) {
925 u32 right_nr;
926 right_nr = btrfs_header_nritems(right);
927 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
928 wret = 1;
929 } else {
930 ret = btrfs_cow_block(trans, root, right,
931 parent, pslot + 1,
932 &right);
933 if (ret)
934 wret = 1;
935 else {
936 wret = balance_node_right(trans, root,
937 right, mid);
940 if (wret < 0)
941 ret = wret;
942 if (wret == 0) {
943 struct btrfs_disk_key disk_key;
945 btrfs_node_key(right, &disk_key, 0);
946 btrfs_set_node_key(parent, &disk_key, pslot + 1);
947 btrfs_mark_buffer_dirty(parent);
949 if (btrfs_header_nritems(mid) <= orig_slot) {
950 path->nodes[level] = right;
951 path->slots[level + 1] += 1;
952 path->slots[level] = orig_slot -
953 btrfs_header_nritems(mid);
954 free_extent_buffer(mid);
955 } else {
956 free_extent_buffer(right);
958 return 0;
960 free_extent_buffer(right);
962 return 1;
966 * readahead one full node of leaves
968 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
969 int level, int slot, u64 objectid)
971 struct btrfs_fs_info *fs_info = root->fs_info;
972 struct extent_buffer *node;
973 struct btrfs_disk_key disk_key;
974 u32 nritems;
975 u64 search;
976 u64 lowest_read;
977 u64 highest_read;
978 u64 nread = 0;
979 int direction = path->reada;
980 struct extent_buffer *eb;
981 u32 nr;
982 u32 blocksize;
983 u32 nscan = 0;
985 if (level != 1)
986 return;
988 if (!path->nodes[level])
989 return;
991 node = path->nodes[level];
992 search = btrfs_node_blockptr(node, slot);
993 blocksize = fs_info->nodesize;
994 eb = btrfs_find_tree_block(fs_info, search, blocksize);
995 if (eb) {
996 free_extent_buffer(eb);
997 return;
1000 highest_read = search;
1001 lowest_read = search;
1003 nritems = btrfs_header_nritems(node);
1004 nr = slot;
1005 while(1) {
1006 if (direction < 0) {
1007 if (nr == 0)
1008 break;
1009 nr--;
1010 } else if (direction > 0) {
1011 nr++;
1012 if (nr >= nritems)
1013 break;
1015 if (path->reada < 0 && objectid) {
1016 btrfs_node_key(node, &disk_key, nr);
1017 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1018 break;
1020 search = btrfs_node_blockptr(node, nr);
1021 if ((search >= lowest_read && search <= highest_read) ||
1022 (search < lowest_read && lowest_read - search <= 32768) ||
1023 (search > highest_read && search - highest_read <= 32768)) {
1024 readahead_tree_block(fs_info, search, blocksize,
1025 btrfs_node_ptr_generation(node, nr));
1026 nread += blocksize;
1028 nscan++;
1029 if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
1030 break;
1031 if(nread > SZ_1M || nscan > 128)
1032 break;
1034 if (search < lowest_read)
1035 lowest_read = search;
1036 if (search > highest_read)
1037 highest_read = search;
1041 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1042 u64 iobjectid, u64 ioff, u8 key_type,
1043 struct btrfs_key *found_key)
1045 int ret;
1046 struct btrfs_key key;
1047 struct extent_buffer *eb;
1048 struct btrfs_path *path;
1050 key.type = key_type;
1051 key.objectid = iobjectid;
1052 key.offset = ioff;
1054 if (found_path == NULL) {
1055 path = btrfs_alloc_path();
1056 if (!path)
1057 return -ENOMEM;
1058 } else
1059 path = found_path;
1061 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1062 if ((ret < 0) || (found_key == NULL))
1063 goto out;
1065 eb = path->nodes[0];
1066 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1067 ret = btrfs_next_leaf(fs_root, path);
1068 if (ret)
1069 goto out;
1070 eb = path->nodes[0];
1073 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1074 if (found_key->type != key.type ||
1075 found_key->objectid != key.objectid) {
1076 ret = 1;
1077 goto out;
1080 out:
1081 if (path != found_path)
1082 btrfs_free_path(path);
1083 return ret;
1087 * look for key in the tree. path is filled in with nodes along the way
1088 * if key is found, we return zero and you can find the item in the leaf
1089 * level of the path (level 0)
1091 * If the key isn't found, the path points to the slot where it should
1092 * be inserted, and 1 is returned. If there are other errors during the
1093 * search a negative error number is returned.
1095 * if ins_len > 0, nodes and leaves will be split as we walk down the
1096 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1097 * possible)
1099 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1100 *root, struct btrfs_key *key, struct btrfs_path *p, int
1101 ins_len, int cow)
1103 struct extent_buffer *b;
1104 int slot;
1105 int ret;
1106 int level;
1107 int should_reada = p->reada;
1108 struct btrfs_fs_info *fs_info = root->fs_info;
1109 u8 lowest_level = 0;
1111 lowest_level = p->lowest_level;
1112 WARN_ON(lowest_level && ins_len > 0);
1113 WARN_ON(p->nodes[0] != NULL);
1115 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1117 again:
1118 b = root->node;
1119 extent_buffer_get(b);
1120 while (b) {
1121 level = btrfs_header_level(b);
1122 if (cow) {
1123 int wret;
1124 wret = btrfs_cow_block(trans, root, b,
1125 p->nodes[level + 1],
1126 p->slots[level + 1],
1127 &b);
1128 if (wret) {
1129 free_extent_buffer(b);
1130 return wret;
1133 BUG_ON(!cow && ins_len);
1134 if (level != btrfs_header_level(b))
1135 WARN_ON(1);
1136 level = btrfs_header_level(b);
1137 p->nodes[level] = b;
1138 ret = check_block(root, p, level);
1139 if (ret)
1140 return -1;
1141 ret = bin_search(b, key, level, &slot);
1142 if (level != 0) {
1143 if (ret && slot > 0)
1144 slot -= 1;
1145 p->slots[level] = slot;
1146 if ((p->search_for_split || ins_len > 0) &&
1147 btrfs_header_nritems(b) >=
1148 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1149 int sret = split_node(trans, root, p, level);
1150 BUG_ON(sret > 0);
1151 if (sret)
1152 return sret;
1153 b = p->nodes[level];
1154 slot = p->slots[level];
1155 } else if (ins_len < 0) {
1156 int sret = balance_level(trans, root, p,
1157 level);
1158 if (sret)
1159 return sret;
1160 b = p->nodes[level];
1161 if (!b) {
1162 btrfs_release_path(p);
1163 goto again;
1165 slot = p->slots[level];
1166 BUG_ON(btrfs_header_nritems(b) == 1);
1168 /* this is only true while dropping a snapshot */
1169 if (level == lowest_level)
1170 break;
1172 if (should_reada)
1173 reada_for_search(root, p, level, slot,
1174 key->objectid);
1176 b = read_node_slot(fs_info, b, slot);
1177 if (!extent_buffer_uptodate(b))
1178 return -EIO;
1179 } else {
1180 p->slots[level] = slot;
1181 if (ins_len > 0 &&
1182 ins_len > btrfs_leaf_free_space(root, b)) {
1183 int sret = split_leaf(trans, root, key,
1184 p, ins_len, ret == 0);
1185 BUG_ON(sret > 0);
1186 if (sret)
1187 return sret;
1189 return ret;
1192 return 1;
1196 * adjust the pointers going up the tree, starting at level
1197 * making sure the right key of each node is points to 'key'.
1198 * This is used after shifting pointers to the left, so it stops
1199 * fixing up pointers when a given leaf/node is not in slot 0 of the
1200 * higher levels
1202 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
1203 struct btrfs_disk_key *key, int level)
1205 int i;
1206 struct extent_buffer *t;
1208 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1209 int tslot = path->slots[i];
1210 if (!path->nodes[i])
1211 break;
1212 t = path->nodes[i];
1213 btrfs_set_node_key(t, key, tslot);
1214 btrfs_mark_buffer_dirty(path->nodes[i]);
1215 if (tslot != 0)
1216 break;
1221 * update item key.
1223 * This function isn't completely safe. It's the caller's responsibility
1224 * that the new key won't break the order
1226 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
1227 struct btrfs_key *new_key)
1229 struct btrfs_disk_key disk_key;
1230 struct extent_buffer *eb;
1231 int slot;
1233 eb = path->nodes[0];
1234 slot = path->slots[0];
1235 if (slot > 0) {
1236 btrfs_item_key(eb, &disk_key, slot - 1);
1237 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1238 return -1;
1240 if (slot < btrfs_header_nritems(eb) - 1) {
1241 btrfs_item_key(eb, &disk_key, slot + 1);
1242 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1243 return -1;
1246 btrfs_cpu_key_to_disk(&disk_key, new_key);
1247 btrfs_set_item_key(eb, &disk_key, slot);
1248 btrfs_mark_buffer_dirty(eb);
1249 if (slot == 0)
1250 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1251 return 0;
1255 * update an item key without the safety checks. This is meant to be called by
1256 * fsck only.
1258 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
1259 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];
1269 btrfs_cpu_key_to_disk(&disk_key, new_key);
1270 btrfs_set_item_key(eb, &disk_key, slot);
1271 btrfs_mark_buffer_dirty(eb);
1272 if (slot == 0)
1273 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1277 * try to push data from one node into the next node left in the
1278 * tree.
1280 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1281 * error, and > 0 if there was no room in the left hand block.
1283 static int push_node_left(struct btrfs_trans_handle *trans,
1284 struct btrfs_root *root, struct extent_buffer *dst,
1285 struct extent_buffer *src, int empty)
1287 int push_items = 0;
1288 int src_nritems;
1289 int dst_nritems;
1290 int ret = 0;
1292 src_nritems = btrfs_header_nritems(src);
1293 dst_nritems = btrfs_header_nritems(dst);
1294 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1295 WARN_ON(btrfs_header_generation(src) != trans->transid);
1296 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1298 if (!empty && src_nritems <= 8)
1299 return 1;
1301 if (push_items <= 0) {
1302 return 1;
1305 if (empty) {
1306 push_items = min(src_nritems, push_items);
1307 if (push_items < src_nritems) {
1308 /* leave at least 8 pointers in the node if
1309 * we aren't going to empty it
1311 if (src_nritems - push_items < 8) {
1312 if (push_items <= 8)
1313 return 1;
1314 push_items -= 8;
1317 } else
1318 push_items = min(src_nritems - 8, push_items);
1320 copy_extent_buffer(dst, src,
1321 btrfs_node_key_ptr_offset(dst_nritems),
1322 btrfs_node_key_ptr_offset(0),
1323 push_items * sizeof(struct btrfs_key_ptr));
1325 if (push_items < src_nritems) {
1326 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1327 btrfs_node_key_ptr_offset(push_items),
1328 (src_nritems - push_items) *
1329 sizeof(struct btrfs_key_ptr));
1331 btrfs_set_header_nritems(src, src_nritems - push_items);
1332 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1333 btrfs_mark_buffer_dirty(src);
1334 btrfs_mark_buffer_dirty(dst);
1336 return ret;
1340 * try to push data from one node into the next node right in the
1341 * tree.
1343 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1344 * error, and > 0 if there was no room in the right hand block.
1346 * this will only push up to 1/2 the contents of the left node over
1348 static int balance_node_right(struct btrfs_trans_handle *trans,
1349 struct btrfs_root *root,
1350 struct extent_buffer *dst,
1351 struct extent_buffer *src)
1353 int push_items = 0;
1354 int max_push;
1355 int src_nritems;
1356 int dst_nritems;
1357 int ret = 0;
1359 WARN_ON(btrfs_header_generation(src) != trans->transid);
1360 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1362 src_nritems = btrfs_header_nritems(src);
1363 dst_nritems = btrfs_header_nritems(dst);
1364 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1365 if (push_items <= 0) {
1366 return 1;
1369 if (src_nritems < 4) {
1370 return 1;
1373 max_push = src_nritems / 2 + 1;
1374 /* don't try to empty the node */
1375 if (max_push >= src_nritems) {
1376 return 1;
1379 if (max_push < push_items)
1380 push_items = max_push;
1382 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1383 btrfs_node_key_ptr_offset(0),
1384 (dst_nritems) *
1385 sizeof(struct btrfs_key_ptr));
1387 copy_extent_buffer(dst, src,
1388 btrfs_node_key_ptr_offset(0),
1389 btrfs_node_key_ptr_offset(src_nritems - push_items),
1390 push_items * sizeof(struct btrfs_key_ptr));
1392 btrfs_set_header_nritems(src, src_nritems - push_items);
1393 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1395 btrfs_mark_buffer_dirty(src);
1396 btrfs_mark_buffer_dirty(dst);
1398 return ret;
1402 * helper function to insert a new root level in the tree.
1403 * A new node is allocated, and a single item is inserted to
1404 * point to the existing root
1406 * returns zero on success or < 0 on failure.
1408 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1409 struct btrfs_root *root,
1410 struct btrfs_path *path, int level)
1412 u64 lower_gen;
1413 struct extent_buffer *lower;
1414 struct extent_buffer *c;
1415 struct extent_buffer *old;
1416 struct btrfs_disk_key lower_key;
1418 BUG_ON(path->nodes[level]);
1419 BUG_ON(path->nodes[level-1] != root->node);
1421 lower = path->nodes[level-1];
1422 if (level == 1)
1423 btrfs_item_key(lower, &lower_key, 0);
1424 else
1425 btrfs_node_key(lower, &lower_key, 0);
1427 c = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1428 root->root_key.objectid, &lower_key,
1429 level, root->node->start, 0);
1431 if (IS_ERR(c))
1432 return PTR_ERR(c);
1434 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1435 btrfs_set_header_nritems(c, 1);
1436 btrfs_set_header_level(c, level);
1437 btrfs_set_header_bytenr(c, c->start);
1438 btrfs_set_header_generation(c, trans->transid);
1439 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1440 btrfs_set_header_owner(c, root->root_key.objectid);
1442 write_extent_buffer(c, root->fs_info->fsid,
1443 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1445 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1446 btrfs_header_chunk_tree_uuid(c),
1447 BTRFS_UUID_SIZE);
1449 btrfs_set_node_key(c, &lower_key, 0);
1450 btrfs_set_node_blockptr(c, 0, lower->start);
1451 lower_gen = btrfs_header_generation(lower);
1452 WARN_ON(lower_gen != trans->transid);
1454 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1456 btrfs_mark_buffer_dirty(c);
1458 old = root->node;
1459 root->node = c;
1461 /* the super has an extra ref to root->node */
1462 free_extent_buffer(old);
1464 add_root_to_dirty_list(root);
1465 extent_buffer_get(c);
1466 path->nodes[level] = c;
1467 path->slots[level] = 0;
1468 return 0;
1472 * worker function to insert a single pointer in a node.
1473 * the node should have enough room for the pointer already
1475 * slot and level indicate where you want the key to go, and
1476 * blocknr is the block the key points to.
1478 * returns zero on success and < 0 on any error
1480 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1481 *root, struct btrfs_path *path, struct btrfs_disk_key
1482 *key, u64 bytenr, int slot, int level)
1484 struct extent_buffer *lower;
1485 int nritems;
1487 BUG_ON(!path->nodes[level]);
1488 lower = path->nodes[level];
1489 nritems = btrfs_header_nritems(lower);
1490 if (slot > nritems)
1491 BUG();
1492 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1493 BUG();
1494 if (slot < nritems) {
1495 /* shift the items */
1496 memmove_extent_buffer(lower,
1497 btrfs_node_key_ptr_offset(slot + 1),
1498 btrfs_node_key_ptr_offset(slot),
1499 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1501 btrfs_set_node_key(lower, key, slot);
1502 btrfs_set_node_blockptr(lower, slot, bytenr);
1503 WARN_ON(trans->transid == 0);
1504 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1505 btrfs_set_header_nritems(lower, nritems + 1);
1506 btrfs_mark_buffer_dirty(lower);
1507 return 0;
1511 * split the node at the specified level in path in two.
1512 * The path is corrected to point to the appropriate node after the split
1514 * Before splitting this tries to make some room in the node by pushing
1515 * left and right, if either one works, it returns right away.
1517 * returns 0 on success and < 0 on failure
1519 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1520 *root, struct btrfs_path *path, int level)
1522 struct extent_buffer *c;
1523 struct extent_buffer *split;
1524 struct btrfs_disk_key disk_key;
1525 int mid;
1526 int ret;
1527 int wret;
1528 u32 c_nritems;
1530 c = path->nodes[level];
1531 WARN_ON(btrfs_header_generation(c) != trans->transid);
1532 if (c == root->node) {
1533 /* trying to split the root, lets make a new one */
1534 ret = insert_new_root(trans, root, path, level + 1);
1535 if (ret)
1536 return ret;
1537 } else {
1538 ret = push_nodes_for_insert(trans, root, path, level);
1539 c = path->nodes[level];
1540 if (!ret && btrfs_header_nritems(c) <
1541 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1542 return 0;
1543 if (ret < 0)
1544 return ret;
1547 c_nritems = btrfs_header_nritems(c);
1548 mid = (c_nritems + 1) / 2;
1549 btrfs_node_key(c, &disk_key, mid);
1551 split = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
1552 root->root_key.objectid,
1553 &disk_key, level, c->start, 0);
1554 if (IS_ERR(split))
1555 return PTR_ERR(split);
1557 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
1558 btrfs_set_header_level(split, btrfs_header_level(c));
1559 btrfs_set_header_bytenr(split, split->start);
1560 btrfs_set_header_generation(split, trans->transid);
1561 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
1562 btrfs_set_header_owner(split, root->root_key.objectid);
1563 write_extent_buffer(split, root->fs_info->fsid,
1564 btrfs_header_fsid(), BTRFS_FSID_SIZE);
1565 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1566 btrfs_header_chunk_tree_uuid(split),
1567 BTRFS_UUID_SIZE);
1570 copy_extent_buffer(split, c,
1571 btrfs_node_key_ptr_offset(0),
1572 btrfs_node_key_ptr_offset(mid),
1573 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1574 btrfs_set_header_nritems(split, c_nritems - mid);
1575 btrfs_set_header_nritems(c, mid);
1576 ret = 0;
1578 btrfs_mark_buffer_dirty(c);
1579 btrfs_mark_buffer_dirty(split);
1581 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1582 path->slots[level + 1] + 1,
1583 level + 1);
1584 if (wret)
1585 ret = wret;
1587 if (path->slots[level] >= mid) {
1588 path->slots[level] -= mid;
1589 free_extent_buffer(c);
1590 path->nodes[level] = split;
1591 path->slots[level + 1] += 1;
1592 } else {
1593 free_extent_buffer(split);
1595 return ret;
1599 * how many bytes are required to store the items in a leaf. start
1600 * and nr indicate which items in the leaf to check. This totals up the
1601 * space used both by the item structs and the item data
1603 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1605 int data_len;
1606 int nritems = btrfs_header_nritems(l);
1607 int end = min(nritems, start + nr) - 1;
1609 if (!nr)
1610 return 0;
1611 data_len = btrfs_item_end_nr(l, start);
1612 data_len = data_len - btrfs_item_offset_nr(l, end);
1613 data_len += sizeof(struct btrfs_item) * nr;
1614 WARN_ON(data_len < 0);
1615 return data_len;
1619 * The space between the end of the leaf items and
1620 * the start of the leaf data. IOW, how much room
1621 * the leaf has left for both items and data
1623 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1625 u32 nodesize = (root ? BTRFS_LEAF_DATA_SIZE(root) : leaf->len);
1626 int nritems = btrfs_header_nritems(leaf);
1627 int ret;
1628 ret = nodesize - leaf_space_used(leaf, 0, nritems);
1629 if (ret < 0) {
1630 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1631 ret, nodesize, leaf_space_used(leaf, 0, nritems),
1632 nritems);
1634 return ret;
1638 * push some data in the path leaf to the right, trying to free up at
1639 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1641 * returns 1 if the push failed because the other node didn't have enough
1642 * room, 0 if everything worked out and < 0 if there were major errors.
1644 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1645 *root, struct btrfs_path *path, int data_size,
1646 int empty)
1648 struct extent_buffer *left = path->nodes[0];
1649 struct extent_buffer *right;
1650 struct extent_buffer *upper;
1651 struct btrfs_disk_key disk_key;
1652 struct btrfs_fs_info *fs_info = root->fs_info;
1653 int slot;
1654 u32 i;
1655 int free_space;
1656 int push_space = 0;
1657 int push_items = 0;
1658 struct btrfs_item *item;
1659 u32 left_nritems;
1660 u32 nr;
1661 u32 right_nritems;
1662 u32 data_end;
1663 u32 this_item_size;
1664 int ret;
1666 slot = path->slots[1];
1667 if (!path->nodes[1]) {
1668 return 1;
1670 upper = path->nodes[1];
1671 if (slot >= btrfs_header_nritems(upper) - 1)
1672 return 1;
1674 right = read_node_slot(fs_info, upper, slot + 1);
1675 if (!extent_buffer_uptodate(right)) {
1676 if (IS_ERR(right))
1677 return PTR_ERR(right);
1678 return -EIO;
1680 free_space = btrfs_leaf_free_space(root, right);
1681 if (free_space < data_size) {
1682 free_extent_buffer(right);
1683 return 1;
1686 /* cow and double check */
1687 ret = btrfs_cow_block(trans, root, right, upper,
1688 slot + 1, &right);
1689 if (ret) {
1690 free_extent_buffer(right);
1691 return 1;
1693 free_space = btrfs_leaf_free_space(root, right);
1694 if (free_space < data_size) {
1695 free_extent_buffer(right);
1696 return 1;
1699 left_nritems = btrfs_header_nritems(left);
1700 if (left_nritems == 0) {
1701 free_extent_buffer(right);
1702 return 1;
1705 if (empty)
1706 nr = 0;
1707 else
1708 nr = 1;
1710 i = left_nritems - 1;
1711 while (i >= nr) {
1712 item = btrfs_item_nr(i);
1714 if (path->slots[0] == i)
1715 push_space += data_size + sizeof(*item);
1717 this_item_size = btrfs_item_size(left, item);
1718 if (this_item_size + sizeof(*item) + push_space > free_space)
1719 break;
1720 push_items++;
1721 push_space += this_item_size + sizeof(*item);
1722 if (i == 0)
1723 break;
1724 i--;
1727 if (push_items == 0) {
1728 free_extent_buffer(right);
1729 return 1;
1732 if (!empty && push_items == left_nritems)
1733 WARN_ON(1);
1735 /* push left to right */
1736 right_nritems = btrfs_header_nritems(right);
1738 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1739 push_space -= leaf_data_end(root, left);
1741 /* make room in the right data area */
1742 data_end = leaf_data_end(root, right);
1743 memmove_extent_buffer(right,
1744 btrfs_leaf_data(right) + data_end - push_space,
1745 btrfs_leaf_data(right) + data_end,
1746 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1748 /* copy from the left data area */
1749 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1750 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1751 btrfs_leaf_data(left) + leaf_data_end(root, left),
1752 push_space);
1754 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1755 btrfs_item_nr_offset(0),
1756 right_nritems * sizeof(struct btrfs_item));
1758 /* copy the items from left to right */
1759 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1760 btrfs_item_nr_offset(left_nritems - push_items),
1761 push_items * sizeof(struct btrfs_item));
1763 /* update the item pointers */
1764 right_nritems += push_items;
1765 btrfs_set_header_nritems(right, right_nritems);
1766 push_space = BTRFS_LEAF_DATA_SIZE(root);
1767 for (i = 0; i < right_nritems; i++) {
1768 item = btrfs_item_nr(i);
1769 push_space -= btrfs_item_size(right, item);
1770 btrfs_set_item_offset(right, item, push_space);
1773 left_nritems -= push_items;
1774 btrfs_set_header_nritems(left, left_nritems);
1776 if (left_nritems)
1777 btrfs_mark_buffer_dirty(left);
1778 btrfs_mark_buffer_dirty(right);
1780 btrfs_item_key(right, &disk_key, 0);
1781 btrfs_set_node_key(upper, &disk_key, slot + 1);
1782 btrfs_mark_buffer_dirty(upper);
1784 /* then fixup the leaf pointer in the path */
1785 if (path->slots[0] >= left_nritems) {
1786 path->slots[0] -= left_nritems;
1787 free_extent_buffer(path->nodes[0]);
1788 path->nodes[0] = right;
1789 path->slots[1] += 1;
1790 } else {
1791 free_extent_buffer(right);
1793 return 0;
1796 * push some data in the path leaf to the left, trying to free up at
1797 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1799 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1800 *root, struct btrfs_path *path, int data_size,
1801 int empty)
1803 struct btrfs_disk_key disk_key;
1804 struct extent_buffer *right = path->nodes[0];
1805 struct extent_buffer *left;
1806 struct btrfs_fs_info *fs_info = root->fs_info;
1807 int slot;
1808 int i;
1809 int free_space;
1810 int push_space = 0;
1811 int push_items = 0;
1812 struct btrfs_item *item;
1813 u32 old_left_nritems;
1814 u32 right_nritems;
1815 u32 nr;
1816 int ret = 0;
1817 u32 this_item_size;
1818 u32 old_left_item_size;
1820 slot = path->slots[1];
1821 if (slot == 0)
1822 return 1;
1823 if (!path->nodes[1])
1824 return 1;
1826 right_nritems = btrfs_header_nritems(right);
1827 if (right_nritems == 0) {
1828 return 1;
1831 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
1832 free_space = btrfs_leaf_free_space(root, left);
1833 if (free_space < data_size) {
1834 free_extent_buffer(left);
1835 return 1;
1838 /* cow and double check */
1839 ret = btrfs_cow_block(trans, root, left,
1840 path->nodes[1], slot - 1, &left);
1841 if (ret) {
1842 /* we hit -ENOSPC, but it isn't fatal here */
1843 free_extent_buffer(left);
1844 return 1;
1847 free_space = btrfs_leaf_free_space(root, left);
1848 if (free_space < data_size) {
1849 free_extent_buffer(left);
1850 return 1;
1853 if (empty)
1854 nr = right_nritems;
1855 else
1856 nr = right_nritems - 1;
1858 for (i = 0; i < nr; i++) {
1859 item = btrfs_item_nr(i);
1861 if (path->slots[0] == i)
1862 push_space += data_size + sizeof(*item);
1864 this_item_size = btrfs_item_size(right, item);
1865 if (this_item_size + sizeof(*item) + push_space > free_space)
1866 break;
1868 push_items++;
1869 push_space += this_item_size + sizeof(*item);
1872 if (push_items == 0) {
1873 free_extent_buffer(left);
1874 return 1;
1876 if (!empty && push_items == btrfs_header_nritems(right))
1877 WARN_ON(1);
1879 /* push data from right to left */
1880 copy_extent_buffer(left, right,
1881 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1882 btrfs_item_nr_offset(0),
1883 push_items * sizeof(struct btrfs_item));
1885 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1886 btrfs_item_offset_nr(right, push_items -1);
1888 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1889 leaf_data_end(root, left) - push_space,
1890 btrfs_leaf_data(right) +
1891 btrfs_item_offset_nr(right, push_items - 1),
1892 push_space);
1893 old_left_nritems = btrfs_header_nritems(left);
1894 BUG_ON(old_left_nritems == 0);
1896 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1897 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1898 u32 ioff;
1900 item = btrfs_item_nr(i);
1901 ioff = btrfs_item_offset(left, item);
1902 btrfs_set_item_offset(left, item,
1903 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1905 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1907 /* fixup right node */
1908 if (push_items > right_nritems) {
1909 printk("push items %d nr %u\n", push_items, right_nritems);
1910 WARN_ON(1);
1913 if (push_items < right_nritems) {
1914 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1915 leaf_data_end(root, right);
1916 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1917 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1918 btrfs_leaf_data(right) +
1919 leaf_data_end(root, right), push_space);
1921 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1922 btrfs_item_nr_offset(push_items),
1923 (btrfs_header_nritems(right) - push_items) *
1924 sizeof(struct btrfs_item));
1926 right_nritems -= push_items;
1927 btrfs_set_header_nritems(right, right_nritems);
1928 push_space = BTRFS_LEAF_DATA_SIZE(root);
1929 for (i = 0; i < right_nritems; i++) {
1930 item = btrfs_item_nr(i);
1931 push_space = push_space - btrfs_item_size(right, item);
1932 btrfs_set_item_offset(right, item, push_space);
1935 btrfs_mark_buffer_dirty(left);
1936 if (right_nritems)
1937 btrfs_mark_buffer_dirty(right);
1939 btrfs_item_key(right, &disk_key, 0);
1940 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1942 /* then fixup the leaf pointer in the path */
1943 if (path->slots[0] < push_items) {
1944 path->slots[0] += old_left_nritems;
1945 free_extent_buffer(path->nodes[0]);
1946 path->nodes[0] = left;
1947 path->slots[1] -= 1;
1948 } else {
1949 free_extent_buffer(left);
1950 path->slots[0] -= push_items;
1952 BUG_ON(path->slots[0] < 0);
1953 return ret;
1957 * split the path's leaf in two, making sure there is at least data_size
1958 * available for the resulting leaf level of the path.
1960 * returns 0 if all went well and < 0 on failure.
1962 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
1963 struct btrfs_root *root,
1964 struct btrfs_path *path,
1965 struct extent_buffer *l,
1966 struct extent_buffer *right,
1967 int slot, int mid, int nritems)
1969 int data_copy_size;
1970 int rt_data_off;
1971 int i;
1972 int ret = 0;
1973 int wret;
1974 struct btrfs_disk_key disk_key;
1976 nritems = nritems - mid;
1977 btrfs_set_header_nritems(right, nritems);
1978 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1980 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1981 btrfs_item_nr_offset(mid),
1982 nritems * sizeof(struct btrfs_item));
1984 copy_extent_buffer(right, l,
1985 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1986 data_copy_size, btrfs_leaf_data(l) +
1987 leaf_data_end(root, l), data_copy_size);
1989 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1990 btrfs_item_end_nr(l, mid);
1992 for (i = 0; i < nritems; i++) {
1993 struct btrfs_item *item = btrfs_item_nr(i);
1994 u32 ioff = btrfs_item_offset(right, item);
1995 btrfs_set_item_offset(right, item, ioff + rt_data_off);
1998 btrfs_set_header_nritems(l, mid);
1999 ret = 0;
2000 btrfs_item_key(right, &disk_key, 0);
2001 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2002 path->slots[1] + 1, 1);
2003 if (wret)
2004 ret = wret;
2006 btrfs_mark_buffer_dirty(right);
2007 btrfs_mark_buffer_dirty(l);
2008 BUG_ON(path->slots[0] != slot);
2010 if (mid <= slot) {
2011 free_extent_buffer(path->nodes[0]);
2012 path->nodes[0] = right;
2013 path->slots[0] -= mid;
2014 path->slots[1] += 1;
2015 } else {
2016 free_extent_buffer(right);
2019 BUG_ON(path->slots[0] < 0);
2021 return ret;
2025 * split the path's leaf in two, making sure there is at least data_size
2026 * available for the resulting leaf level of the path.
2028 * returns 0 if all went well and < 0 on failure.
2030 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2031 struct btrfs_root *root,
2032 struct btrfs_key *ins_key,
2033 struct btrfs_path *path, int data_size,
2034 int extend)
2036 struct btrfs_disk_key disk_key;
2037 struct extent_buffer *l;
2038 u32 nritems;
2039 int mid;
2040 int slot;
2041 struct extent_buffer *right;
2042 int ret = 0;
2043 int wret;
2044 int split;
2045 int num_doubles = 0;
2047 l = path->nodes[0];
2048 slot = path->slots[0];
2049 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2050 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2051 return -EOVERFLOW;
2053 /* first try to make some room by pushing left and right */
2054 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2055 wret = push_leaf_right(trans, root, path, data_size, 0);
2056 if (wret < 0)
2057 return wret;
2058 if (wret) {
2059 wret = push_leaf_left(trans, root, path, data_size, 0);
2060 if (wret < 0)
2061 return wret;
2063 l = path->nodes[0];
2065 /* did the pushes work? */
2066 if (btrfs_leaf_free_space(root, l) >= data_size)
2067 return 0;
2070 if (!path->nodes[1]) {
2071 ret = insert_new_root(trans, root, path, 1);
2072 if (ret)
2073 return ret;
2075 again:
2076 split = 1;
2077 l = path->nodes[0];
2078 slot = path->slots[0];
2079 nritems = btrfs_header_nritems(l);
2080 mid = (nritems + 1) / 2;
2082 if (mid <= slot) {
2083 if (nritems == 1 ||
2084 leaf_space_used(l, mid, nritems - mid) + data_size >
2085 BTRFS_LEAF_DATA_SIZE(root)) {
2086 if (slot >= nritems) {
2087 split = 0;
2088 } else {
2089 mid = slot;
2090 if (mid != nritems &&
2091 leaf_space_used(l, mid, nritems - mid) +
2092 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2093 split = 2;
2097 } else {
2098 if (leaf_space_used(l, 0, mid) + data_size >
2099 BTRFS_LEAF_DATA_SIZE(root)) {
2100 if (!extend && data_size && slot == 0) {
2101 split = 0;
2102 } else if ((extend || !data_size) && slot == 0) {
2103 mid = 1;
2104 } else {
2105 mid = slot;
2106 if (mid != nritems &&
2107 leaf_space_used(l, mid, nritems - mid) +
2108 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2109 split = 2 ;
2115 if (split == 0)
2116 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2117 else
2118 btrfs_item_key(l, &disk_key, mid);
2120 right = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
2121 root->root_key.objectid,
2122 &disk_key, 0, l->start, 0);
2123 if (IS_ERR(right)) {
2124 BUG_ON(1);
2125 return PTR_ERR(right);
2128 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2129 btrfs_set_header_bytenr(right, right->start);
2130 btrfs_set_header_generation(right, trans->transid);
2131 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2132 btrfs_set_header_owner(right, root->root_key.objectid);
2133 btrfs_set_header_level(right, 0);
2134 write_extent_buffer(right, root->fs_info->fsid,
2135 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2137 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2138 btrfs_header_chunk_tree_uuid(right),
2139 BTRFS_UUID_SIZE);
2141 if (split == 0) {
2142 if (mid <= slot) {
2143 btrfs_set_header_nritems(right, 0);
2144 wret = insert_ptr(trans, root, path,
2145 &disk_key, right->start,
2146 path->slots[1] + 1, 1);
2147 if (wret)
2148 ret = wret;
2150 free_extent_buffer(path->nodes[0]);
2151 path->nodes[0] = right;
2152 path->slots[0] = 0;
2153 path->slots[1] += 1;
2154 } else {
2155 btrfs_set_header_nritems(right, 0);
2156 wret = insert_ptr(trans, root, path,
2157 &disk_key,
2158 right->start,
2159 path->slots[1], 1);
2160 if (wret)
2161 ret = wret;
2162 free_extent_buffer(path->nodes[0]);
2163 path->nodes[0] = right;
2164 path->slots[0] = 0;
2165 if (path->slots[1] == 0) {
2166 btrfs_fixup_low_keys(root, path,
2167 &disk_key, 1);
2170 btrfs_mark_buffer_dirty(right);
2171 return ret;
2174 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2175 BUG_ON(ret);
2177 if (split == 2) {
2178 BUG_ON(num_doubles != 0);
2179 num_doubles++;
2180 goto again;
2183 return ret;
2187 * This function splits a single item into two items,
2188 * giving 'new_key' to the new item and splitting the
2189 * old one at split_offset (from the start of the item).
2191 * The path may be released by this operation. After
2192 * the split, the path is pointing to the old item. The
2193 * new item is going to be in the same node as the old one.
2195 * Note, the item being split must be smaller enough to live alone on
2196 * a tree block with room for one extra struct btrfs_item
2198 * This allows us to split the item in place, keeping a lock on the
2199 * leaf the entire time.
2201 int btrfs_split_item(struct btrfs_trans_handle *trans,
2202 struct btrfs_root *root,
2203 struct btrfs_path *path,
2204 struct btrfs_key *new_key,
2205 unsigned long split_offset)
2207 u32 item_size;
2208 struct extent_buffer *leaf;
2209 struct btrfs_key orig_key;
2210 struct btrfs_item *item;
2211 struct btrfs_item *new_item;
2212 int ret = 0;
2213 int slot;
2214 u32 nritems;
2215 u32 orig_offset;
2216 struct btrfs_disk_key disk_key;
2217 char *buf;
2219 leaf = path->nodes[0];
2220 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2221 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2222 goto split;
2224 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2225 btrfs_release_path(path);
2227 path->search_for_split = 1;
2229 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2230 path->search_for_split = 0;
2232 /* if our item isn't there or got smaller, return now */
2233 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2234 path->slots[0])) {
2235 return -EAGAIN;
2238 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2239 BUG_ON(ret);
2241 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2242 leaf = path->nodes[0];
2244 split:
2245 item = btrfs_item_nr(path->slots[0]);
2246 orig_offset = btrfs_item_offset(leaf, item);
2247 item_size = btrfs_item_size(leaf, item);
2250 buf = kmalloc(item_size, GFP_NOFS);
2251 BUG_ON(!buf);
2252 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2253 path->slots[0]), item_size);
2254 slot = path->slots[0] + 1;
2255 leaf = path->nodes[0];
2257 nritems = btrfs_header_nritems(leaf);
2259 if (slot < nritems) {
2260 /* shift the items */
2261 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2262 btrfs_item_nr_offset(slot),
2263 (nritems - slot) * sizeof(struct btrfs_item));
2267 btrfs_cpu_key_to_disk(&disk_key, new_key);
2268 btrfs_set_item_key(leaf, &disk_key, slot);
2270 new_item = btrfs_item_nr(slot);
2272 btrfs_set_item_offset(leaf, new_item, orig_offset);
2273 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2275 btrfs_set_item_offset(leaf, item,
2276 orig_offset + item_size - split_offset);
2277 btrfs_set_item_size(leaf, item, split_offset);
2279 btrfs_set_header_nritems(leaf, nritems + 1);
2281 /* write the data for the start of the original item */
2282 write_extent_buffer(leaf, buf,
2283 btrfs_item_ptr_offset(leaf, path->slots[0]),
2284 split_offset);
2286 /* write the data for the new item */
2287 write_extent_buffer(leaf, buf + split_offset,
2288 btrfs_item_ptr_offset(leaf, slot),
2289 item_size - split_offset);
2290 btrfs_mark_buffer_dirty(leaf);
2292 ret = 0;
2293 if (btrfs_leaf_free_space(root, leaf) < 0) {
2294 btrfs_print_leaf(root, leaf);
2295 BUG();
2297 kfree(buf);
2298 return ret;
2301 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2302 u32 new_size, int from_end)
2304 int ret = 0;
2305 int slot;
2306 struct extent_buffer *leaf;
2307 struct btrfs_item *item;
2308 u32 nritems;
2309 unsigned int data_end;
2310 unsigned int old_data_start;
2311 unsigned int old_size;
2312 unsigned int size_diff;
2313 int i;
2315 leaf = path->nodes[0];
2316 slot = path->slots[0];
2318 old_size = btrfs_item_size_nr(leaf, slot);
2319 if (old_size == new_size)
2320 return 0;
2322 nritems = btrfs_header_nritems(leaf);
2323 data_end = leaf_data_end(root, leaf);
2325 old_data_start = btrfs_item_offset_nr(leaf, slot);
2327 size_diff = old_size - new_size;
2329 BUG_ON(slot < 0);
2330 BUG_ON(slot >= nritems);
2333 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2335 /* first correct the data pointers */
2336 for (i = slot; i < nritems; i++) {
2337 u32 ioff;
2338 item = btrfs_item_nr(i);
2339 ioff = btrfs_item_offset(leaf, item);
2340 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2343 /* shift the data */
2344 if (from_end) {
2345 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2346 data_end + size_diff, btrfs_leaf_data(leaf) +
2347 data_end, old_data_start + new_size - data_end);
2348 } else {
2349 struct btrfs_disk_key disk_key;
2350 u64 offset;
2352 btrfs_item_key(leaf, &disk_key, slot);
2354 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2355 unsigned long ptr;
2356 struct btrfs_file_extent_item *fi;
2358 fi = btrfs_item_ptr(leaf, slot,
2359 struct btrfs_file_extent_item);
2360 fi = (struct btrfs_file_extent_item *)(
2361 (unsigned long)fi - size_diff);
2363 if (btrfs_file_extent_type(leaf, fi) ==
2364 BTRFS_FILE_EXTENT_INLINE) {
2365 ptr = btrfs_item_ptr_offset(leaf, slot);
2366 memmove_extent_buffer(leaf, ptr,
2367 (unsigned long)fi,
2368 offsetof(struct btrfs_file_extent_item,
2369 disk_bytenr));
2373 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2374 data_end + size_diff, btrfs_leaf_data(leaf) +
2375 data_end, old_data_start - data_end);
2377 offset = btrfs_disk_key_offset(&disk_key);
2378 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2379 btrfs_set_item_key(leaf, &disk_key, slot);
2380 if (slot == 0)
2381 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2384 item = btrfs_item_nr(slot);
2385 btrfs_set_item_size(leaf, item, new_size);
2386 btrfs_mark_buffer_dirty(leaf);
2388 ret = 0;
2389 if (btrfs_leaf_free_space(root, leaf) < 0) {
2390 btrfs_print_leaf(root, leaf);
2391 BUG();
2393 return ret;
2396 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2397 u32 data_size)
2399 int ret = 0;
2400 int slot;
2401 struct extent_buffer *leaf;
2402 struct btrfs_item *item;
2403 u32 nritems;
2404 unsigned int data_end;
2405 unsigned int old_data;
2406 unsigned int old_size;
2407 int i;
2409 leaf = path->nodes[0];
2411 nritems = btrfs_header_nritems(leaf);
2412 data_end = leaf_data_end(root, leaf);
2414 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2415 btrfs_print_leaf(root, leaf);
2416 BUG();
2418 slot = path->slots[0];
2419 old_data = btrfs_item_end_nr(leaf, slot);
2421 BUG_ON(slot < 0);
2422 if (slot >= nritems) {
2423 btrfs_print_leaf(root, leaf);
2424 printk("slot %d too large, nritems %d\n", slot, nritems);
2425 BUG_ON(1);
2429 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2431 /* first correct the data pointers */
2432 for (i = slot; i < nritems; i++) {
2433 u32 ioff;
2434 item = btrfs_item_nr(i);
2435 ioff = btrfs_item_offset(leaf, item);
2436 btrfs_set_item_offset(leaf, item, ioff - data_size);
2439 /* shift the data */
2440 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2441 data_end - data_size, btrfs_leaf_data(leaf) +
2442 data_end, old_data - data_end);
2444 data_end = old_data;
2445 old_size = btrfs_item_size_nr(leaf, slot);
2446 item = btrfs_item_nr(slot);
2447 btrfs_set_item_size(leaf, item, old_size + data_size);
2448 btrfs_mark_buffer_dirty(leaf);
2450 ret = 0;
2451 if (btrfs_leaf_free_space(root, leaf) < 0) {
2452 btrfs_print_leaf(root, leaf);
2453 BUG();
2455 return ret;
2459 * Given a key and some data, insert an item into the tree.
2460 * This does all the path init required, making room in the tree if needed.
2462 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2463 struct btrfs_root *root,
2464 struct btrfs_path *path,
2465 struct btrfs_key *cpu_key, u32 *data_size,
2466 int nr)
2468 struct extent_buffer *leaf;
2469 struct btrfs_item *item;
2470 int ret = 0;
2471 int slot;
2472 int i;
2473 u32 nritems;
2474 u32 total_size = 0;
2475 u32 total_data = 0;
2476 unsigned int data_end;
2477 struct btrfs_disk_key disk_key;
2479 for (i = 0; i < nr; i++) {
2480 total_data += data_size[i];
2483 /* create a root if there isn't one */
2484 if (!root->node)
2485 BUG();
2487 total_size = total_data + nr * sizeof(struct btrfs_item);
2488 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2489 if (ret == 0) {
2490 return -EEXIST;
2492 if (ret < 0)
2493 goto out;
2495 leaf = path->nodes[0];
2497 nritems = btrfs_header_nritems(leaf);
2498 data_end = leaf_data_end(root, leaf);
2500 if (btrfs_leaf_free_space(root, leaf) < total_size) {
2501 btrfs_print_leaf(root, leaf);
2502 printk("not enough freespace need %u have %d\n",
2503 total_size, btrfs_leaf_free_space(root, leaf));
2504 BUG();
2507 slot = path->slots[0];
2508 BUG_ON(slot < 0);
2510 if (slot < nritems) {
2511 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2513 if (old_data < data_end) {
2514 btrfs_print_leaf(root, leaf);
2515 printk("slot %d old_data %d data_end %d\n",
2516 slot, old_data, data_end);
2517 BUG_ON(1);
2520 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2522 /* first correct the data pointers */
2523 for (i = slot; i < nritems; i++) {
2524 u32 ioff;
2526 item = btrfs_item_nr(i);
2527 ioff = btrfs_item_offset(leaf, item);
2528 btrfs_set_item_offset(leaf, item, ioff - total_data);
2531 /* shift the items */
2532 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2533 btrfs_item_nr_offset(slot),
2534 (nritems - slot) * sizeof(struct btrfs_item));
2536 /* shift the data */
2537 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2538 data_end - total_data, btrfs_leaf_data(leaf) +
2539 data_end, old_data - data_end);
2540 data_end = old_data;
2543 /* setup the item for the new data */
2544 for (i = 0; i < nr; i++) {
2545 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2546 btrfs_set_item_key(leaf, &disk_key, slot + i);
2547 item = btrfs_item_nr(slot + i);
2548 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2549 data_end -= data_size[i];
2550 btrfs_set_item_size(leaf, item, data_size[i]);
2552 btrfs_set_header_nritems(leaf, nritems + nr);
2553 btrfs_mark_buffer_dirty(leaf);
2555 ret = 0;
2556 if (slot == 0) {
2557 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2558 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2561 if (btrfs_leaf_free_space(root, leaf) < 0) {
2562 btrfs_print_leaf(root, leaf);
2563 BUG();
2566 out:
2567 return ret;
2571 * Given a key and some data, insert an item into the tree.
2572 * This does all the path init required, making room in the tree if needed.
2574 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2575 *root, struct btrfs_key *cpu_key, void *data, u32
2576 data_size)
2578 int ret = 0;
2579 struct btrfs_path *path;
2580 struct extent_buffer *leaf;
2581 unsigned long ptr;
2583 path = btrfs_alloc_path();
2584 if (!path)
2585 return -ENOMEM;
2587 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2588 if (!ret) {
2589 leaf = path->nodes[0];
2590 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2591 write_extent_buffer(leaf, data, ptr, data_size);
2592 btrfs_mark_buffer_dirty(leaf);
2594 btrfs_free_path(path);
2595 return ret;
2599 * delete the pointer from a given node.
2601 * If the delete empties a node, the node is removed from the tree,
2602 * continuing all the way the root if required. The root is converted into
2603 * a leaf if all the nodes are emptied.
2605 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2606 int level, int slot)
2608 struct extent_buffer *parent = path->nodes[level];
2609 u32 nritems;
2610 int ret = 0;
2612 nritems = btrfs_header_nritems(parent);
2613 if (slot < nritems - 1) {
2614 /* shift the items */
2615 memmove_extent_buffer(parent,
2616 btrfs_node_key_ptr_offset(slot),
2617 btrfs_node_key_ptr_offset(slot + 1),
2618 sizeof(struct btrfs_key_ptr) *
2619 (nritems - slot - 1));
2621 nritems--;
2622 btrfs_set_header_nritems(parent, nritems);
2623 if (nritems == 0 && parent == root->node) {
2624 BUG_ON(btrfs_header_level(root->node) != 1);
2625 /* just turn the root into a leaf and break */
2626 btrfs_set_header_level(root->node, 0);
2627 } else if (slot == 0) {
2628 struct btrfs_disk_key disk_key;
2630 btrfs_node_key(parent, &disk_key, 0);
2631 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2633 btrfs_mark_buffer_dirty(parent);
2634 return ret;
2638 * a helper function to delete the leaf pointed to by path->slots[1] and
2639 * path->nodes[1].
2641 * This deletes the pointer in path->nodes[1] and frees the leaf
2642 * block extent. zero is returned if it all worked out, < 0 otherwise.
2644 * The path must have already been setup for deleting the leaf, including
2645 * all the proper balancing. path->nodes[1] must be locked.
2647 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2648 struct btrfs_root *root,
2649 struct btrfs_path *path,
2650 struct extent_buffer *leaf)
2652 int ret;
2654 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2655 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2656 if (ret)
2657 return ret;
2659 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2660 0, root->root_key.objectid, 0, 0);
2661 return ret;
2665 * delete the item at the leaf level in path. If that empties
2666 * the leaf, remove it from the tree
2668 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2669 struct btrfs_path *path, int slot, int nr)
2671 struct extent_buffer *leaf;
2672 struct btrfs_item *item;
2673 int last_off;
2674 int dsize = 0;
2675 int ret = 0;
2676 int wret;
2677 int i;
2678 u32 nritems;
2680 leaf = path->nodes[0];
2681 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2683 for (i = 0; i < nr; i++)
2684 dsize += btrfs_item_size_nr(leaf, slot + i);
2686 nritems = btrfs_header_nritems(leaf);
2688 if (slot + nr != nritems) {
2689 int data_end = leaf_data_end(root, leaf);
2691 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2692 data_end + dsize,
2693 btrfs_leaf_data(leaf) + data_end,
2694 last_off - data_end);
2696 for (i = slot + nr; i < nritems; i++) {
2697 u32 ioff;
2699 item = btrfs_item_nr(i);
2700 ioff = btrfs_item_offset(leaf, item);
2701 btrfs_set_item_offset(leaf, item, ioff + dsize);
2704 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2705 btrfs_item_nr_offset(slot + nr),
2706 sizeof(struct btrfs_item) *
2707 (nritems - slot - nr));
2709 btrfs_set_header_nritems(leaf, nritems - nr);
2710 nritems -= nr;
2712 /* delete the leaf if we've emptied it */
2713 if (nritems == 0) {
2714 if (leaf == root->node) {
2715 btrfs_set_header_level(leaf, 0);
2716 } else {
2717 clean_tree_block(trans, root, leaf);
2718 wret = btrfs_del_leaf(trans, root, path, leaf);
2719 BUG_ON(ret);
2720 if (wret)
2721 ret = wret;
2723 } else {
2724 int used = leaf_space_used(leaf, 0, nritems);
2725 if (slot == 0) {
2726 struct btrfs_disk_key disk_key;
2728 btrfs_item_key(leaf, &disk_key, 0);
2729 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2732 /* delete the leaf if it is mostly empty */
2733 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2734 /* push_leaf_left fixes the path.
2735 * make sure the path still points to our leaf
2736 * for possible call to del_ptr below
2738 slot = path->slots[1];
2739 extent_buffer_get(leaf);
2741 wret = push_leaf_left(trans, root, path, 1, 1);
2742 if (wret < 0 && wret != -ENOSPC)
2743 ret = wret;
2745 if (path->nodes[0] == leaf &&
2746 btrfs_header_nritems(leaf)) {
2747 wret = push_leaf_right(trans, root, path, 1, 1);
2748 if (wret < 0 && wret != -ENOSPC)
2749 ret = wret;
2752 if (btrfs_header_nritems(leaf) == 0) {
2753 clean_tree_block(trans, root, leaf);
2754 path->slots[1] = slot;
2755 ret = btrfs_del_leaf(trans, root, path, leaf);
2756 BUG_ON(ret);
2757 free_extent_buffer(leaf);
2759 } else {
2760 btrfs_mark_buffer_dirty(leaf);
2761 free_extent_buffer(leaf);
2763 } else {
2764 btrfs_mark_buffer_dirty(leaf);
2767 return ret;
2771 * walk up the tree as far as required to find the previous leaf.
2772 * returns 0 if it found something or 1 if there are no lesser leaves.
2773 * returns < 0 on io errors.
2775 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2777 int slot;
2778 int level = 1;
2779 struct extent_buffer *c;
2780 struct extent_buffer *next = NULL;
2781 struct btrfs_fs_info *fs_info = root->fs_info;
2783 while(level < BTRFS_MAX_LEVEL) {
2784 if (!path->nodes[level])
2785 return 1;
2787 slot = path->slots[level];
2788 c = path->nodes[level];
2789 if (slot == 0) {
2790 level++;
2791 if (level == BTRFS_MAX_LEVEL)
2792 return 1;
2793 continue;
2795 slot--;
2797 next = read_node_slot(fs_info, c, slot);
2798 if (!extent_buffer_uptodate(next)) {
2799 if (IS_ERR(next))
2800 return PTR_ERR(next);
2801 return -EIO;
2803 break;
2805 path->slots[level] = slot;
2806 while(1) {
2807 level--;
2808 c = path->nodes[level];
2809 free_extent_buffer(c);
2810 slot = btrfs_header_nritems(next);
2811 if (slot != 0)
2812 slot--;
2813 path->nodes[level] = next;
2814 path->slots[level] = slot;
2815 if (!level)
2816 break;
2817 next = read_node_slot(fs_info, next, slot);
2818 if (!extent_buffer_uptodate(next)) {
2819 if (IS_ERR(next))
2820 return PTR_ERR(next);
2821 return -EIO;
2824 return 0;
2828 * walk up the tree as far as required to find the next leaf.
2829 * returns 0 if it found something or 1 if there are no greater leaves.
2830 * returns < 0 on io errors.
2832 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2834 int slot;
2835 int level = 1;
2836 struct extent_buffer *c;
2837 struct extent_buffer *next = NULL;
2838 struct btrfs_fs_info *fs_info = root->fs_info;
2840 while(level < BTRFS_MAX_LEVEL) {
2841 if (!path->nodes[level])
2842 return 1;
2844 slot = path->slots[level] + 1;
2845 c = path->nodes[level];
2846 if (slot >= btrfs_header_nritems(c)) {
2847 level++;
2848 if (level == BTRFS_MAX_LEVEL)
2849 return 1;
2850 continue;
2853 if (path->reada)
2854 reada_for_search(root, path, level, slot, 0);
2856 next = read_node_slot(fs_info, c, slot);
2857 if (!extent_buffer_uptodate(next))
2858 return -EIO;
2859 break;
2861 path->slots[level] = slot;
2862 while(1) {
2863 level--;
2864 c = path->nodes[level];
2865 free_extent_buffer(c);
2866 path->nodes[level] = next;
2867 path->slots[level] = 0;
2868 if (!level)
2869 break;
2870 if (path->reada)
2871 reada_for_search(root, path, level, 0, 0);
2872 next = read_node_slot(fs_info, next, 0);
2873 if (!extent_buffer_uptodate(next))
2874 return -EIO;
2876 return 0;
2879 int btrfs_previous_item(struct btrfs_root *root,
2880 struct btrfs_path *path, u64 min_objectid,
2881 int type)
2883 struct btrfs_key found_key;
2884 struct extent_buffer *leaf;
2885 u32 nritems;
2886 int ret;
2888 while(1) {
2889 if (path->slots[0] == 0) {
2890 ret = btrfs_prev_leaf(root, path);
2891 if (ret != 0)
2892 return ret;
2893 } else {
2894 path->slots[0]--;
2896 leaf = path->nodes[0];
2897 nritems = btrfs_header_nritems(leaf);
2898 if (nritems == 0)
2899 return 1;
2900 if (path->slots[0] == nritems)
2901 path->slots[0]--;
2903 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2904 if (found_key.objectid < min_objectid)
2905 break;
2906 if (found_key.type == type)
2907 return 0;
2908 if (found_key.objectid == min_objectid &&
2909 found_key.type < type)
2910 break;
2912 return 1;
2916 * search in extent tree to find a previous Metadata/Data extent item with
2917 * min objecitd.
2919 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2921 int btrfs_previous_extent_item(struct btrfs_root *root,
2922 struct btrfs_path *path, u64 min_objectid)
2924 struct btrfs_key found_key;
2925 struct extent_buffer *leaf;
2926 u32 nritems;
2927 int ret;
2929 while (1) {
2930 if (path->slots[0] == 0) {
2931 ret = btrfs_prev_leaf(root, path);
2932 if (ret != 0)
2933 return ret;
2934 } else {
2935 path->slots[0]--;
2937 leaf = path->nodes[0];
2938 nritems = btrfs_header_nritems(leaf);
2939 if (nritems == 0)
2940 return 1;
2941 if (path->slots[0] == nritems)
2942 path->slots[0]--;
2944 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2945 if (found_key.objectid < min_objectid)
2946 break;
2947 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2948 found_key.type == BTRFS_METADATA_ITEM_KEY)
2949 return 0;
2950 if (found_key.objectid == min_objectid &&
2951 found_key.type < BTRFS_EXTENT_ITEM_KEY)
2952 break;
2954 return 1;
2958 * Search in extent tree to found next meta/data extent
2959 * Caller needs to check for no-hole or skinny metadata features.
2961 int btrfs_next_extent_item(struct btrfs_root *root,
2962 struct btrfs_path *path, u64 max_objectid)
2964 struct btrfs_key found_key;
2965 int ret;
2967 while (1) {
2968 ret = btrfs_next_item(root, path);
2969 if (ret)
2970 return ret;
2971 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2972 path->slots[0]);
2973 if (found_key.objectid > max_objectid)
2974 return 1;
2975 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2976 found_key.type == BTRFS_METADATA_ITEM_KEY)
2977 return 0;