btrfs-progs: tests: add shell quotes to mkfs test scripts
[btrfs-progs-unstable/devel.git] / ctree.c
blobe26a1c29bb757964ae6eedfab9062acb7096b014
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(root, 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(root, 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(root, 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 btrfs_root *root, struct extent_buffer *leaf)
1639 u32 nodesize = (root ? BTRFS_LEAF_DATA_SIZE(root->fs_info) : leaf->len);
1640 int nritems = btrfs_header_nritems(leaf);
1641 int ret;
1642 ret = nodesize - leaf_space_used(leaf, 0, nritems);
1643 if (ret < 0) {
1644 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
1645 ret, nodesize, leaf_space_used(leaf, 0, nritems),
1646 nritems);
1648 return ret;
1652 * push some data in the path leaf to the right, trying to free up at
1653 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1655 * returns 1 if the push failed because the other node didn't have enough
1656 * room, 0 if everything worked out and < 0 if there were major errors.
1658 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1659 *root, struct btrfs_path *path, int data_size,
1660 int empty)
1662 struct extent_buffer *left = path->nodes[0];
1663 struct extent_buffer *right;
1664 struct extent_buffer *upper;
1665 struct btrfs_disk_key disk_key;
1666 struct btrfs_fs_info *fs_info = root->fs_info;
1667 int slot;
1668 u32 i;
1669 int free_space;
1670 int push_space = 0;
1671 int push_items = 0;
1672 struct btrfs_item *item;
1673 u32 left_nritems;
1674 u32 nr;
1675 u32 right_nritems;
1676 u32 data_end;
1677 u32 this_item_size;
1678 int ret;
1680 slot = path->slots[1];
1681 if (!path->nodes[1]) {
1682 return 1;
1684 upper = path->nodes[1];
1685 if (slot >= btrfs_header_nritems(upper) - 1)
1686 return 1;
1688 right = read_node_slot(fs_info, upper, slot + 1);
1689 if (!extent_buffer_uptodate(right)) {
1690 if (IS_ERR(right))
1691 return PTR_ERR(right);
1692 return -EIO;
1694 free_space = btrfs_leaf_free_space(root, right);
1695 if (free_space < data_size) {
1696 free_extent_buffer(right);
1697 return 1;
1700 /* cow and double check */
1701 ret = btrfs_cow_block(trans, root, right, upper,
1702 slot + 1, &right);
1703 if (ret) {
1704 free_extent_buffer(right);
1705 return 1;
1707 free_space = btrfs_leaf_free_space(root, right);
1708 if (free_space < data_size) {
1709 free_extent_buffer(right);
1710 return 1;
1713 left_nritems = btrfs_header_nritems(left);
1714 if (left_nritems == 0) {
1715 free_extent_buffer(right);
1716 return 1;
1719 if (empty)
1720 nr = 0;
1721 else
1722 nr = 1;
1724 i = left_nritems - 1;
1725 while (i >= nr) {
1726 item = btrfs_item_nr(i);
1728 if (path->slots[0] == i)
1729 push_space += data_size + sizeof(*item);
1731 this_item_size = btrfs_item_size(left, item);
1732 if (this_item_size + sizeof(*item) + push_space > free_space)
1733 break;
1734 push_items++;
1735 push_space += this_item_size + sizeof(*item);
1736 if (i == 0)
1737 break;
1738 i--;
1741 if (push_items == 0) {
1742 free_extent_buffer(right);
1743 return 1;
1746 if (!empty && push_items == left_nritems)
1747 WARN_ON(1);
1749 /* push left to right */
1750 right_nritems = btrfs_header_nritems(right);
1752 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1753 push_space -= leaf_data_end(fs_info, left);
1755 /* make room in the right data area */
1756 data_end = leaf_data_end(fs_info, right);
1757 memmove_extent_buffer(right,
1758 btrfs_leaf_data(right) + data_end - push_space,
1759 btrfs_leaf_data(right) + data_end,
1760 BTRFS_LEAF_DATA_SIZE(root->fs_info) - data_end);
1762 /* copy from the left data area */
1763 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1764 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space,
1765 btrfs_leaf_data(left) + leaf_data_end(fs_info, left),
1766 push_space);
1768 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1769 btrfs_item_nr_offset(0),
1770 right_nritems * sizeof(struct btrfs_item));
1772 /* copy the items from left to right */
1773 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1774 btrfs_item_nr_offset(left_nritems - push_items),
1775 push_items * sizeof(struct btrfs_item));
1777 /* update the item pointers */
1778 right_nritems += push_items;
1779 btrfs_set_header_nritems(right, right_nritems);
1780 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1781 for (i = 0; i < right_nritems; i++) {
1782 item = btrfs_item_nr(i);
1783 push_space -= btrfs_item_size(right, item);
1784 btrfs_set_item_offset(right, item, push_space);
1787 left_nritems -= push_items;
1788 btrfs_set_header_nritems(left, left_nritems);
1790 if (left_nritems)
1791 btrfs_mark_buffer_dirty(left);
1792 btrfs_mark_buffer_dirty(right);
1794 btrfs_item_key(right, &disk_key, 0);
1795 btrfs_set_node_key(upper, &disk_key, slot + 1);
1796 btrfs_mark_buffer_dirty(upper);
1798 /* then fixup the leaf pointer in the path */
1799 if (path->slots[0] >= left_nritems) {
1800 path->slots[0] -= left_nritems;
1801 free_extent_buffer(path->nodes[0]);
1802 path->nodes[0] = right;
1803 path->slots[1] += 1;
1804 } else {
1805 free_extent_buffer(right);
1807 return 0;
1810 * push some data in the path leaf to the left, trying to free up at
1811 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1813 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1814 *root, struct btrfs_path *path, int data_size,
1815 int empty)
1817 struct btrfs_disk_key disk_key;
1818 struct extent_buffer *right = path->nodes[0];
1819 struct extent_buffer *left;
1820 struct btrfs_fs_info *fs_info = root->fs_info;
1821 int slot;
1822 int i;
1823 int free_space;
1824 int push_space = 0;
1825 int push_items = 0;
1826 struct btrfs_item *item;
1827 u32 old_left_nritems;
1828 u32 right_nritems;
1829 u32 nr;
1830 int ret = 0;
1831 u32 this_item_size;
1832 u32 old_left_item_size;
1834 slot = path->slots[1];
1835 if (slot == 0)
1836 return 1;
1837 if (!path->nodes[1])
1838 return 1;
1840 right_nritems = btrfs_header_nritems(right);
1841 if (right_nritems == 0) {
1842 return 1;
1845 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
1846 free_space = btrfs_leaf_free_space(root, left);
1847 if (free_space < data_size) {
1848 free_extent_buffer(left);
1849 return 1;
1852 /* cow and double check */
1853 ret = btrfs_cow_block(trans, root, left,
1854 path->nodes[1], slot - 1, &left);
1855 if (ret) {
1856 /* we hit -ENOSPC, but it isn't fatal here */
1857 free_extent_buffer(left);
1858 return 1;
1861 free_space = btrfs_leaf_free_space(root, left);
1862 if (free_space < data_size) {
1863 free_extent_buffer(left);
1864 return 1;
1867 if (empty)
1868 nr = right_nritems;
1869 else
1870 nr = right_nritems - 1;
1872 for (i = 0; i < nr; i++) {
1873 item = btrfs_item_nr(i);
1875 if (path->slots[0] == i)
1876 push_space += data_size + sizeof(*item);
1878 this_item_size = btrfs_item_size(right, item);
1879 if (this_item_size + sizeof(*item) + push_space > free_space)
1880 break;
1882 push_items++;
1883 push_space += this_item_size + sizeof(*item);
1886 if (push_items == 0) {
1887 free_extent_buffer(left);
1888 return 1;
1890 if (!empty && push_items == btrfs_header_nritems(right))
1891 WARN_ON(1);
1893 /* push data from right to left */
1894 copy_extent_buffer(left, right,
1895 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1896 btrfs_item_nr_offset(0),
1897 push_items * sizeof(struct btrfs_item));
1899 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1900 btrfs_item_offset_nr(right, push_items -1);
1902 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1903 leaf_data_end(fs_info, left) - push_space,
1904 btrfs_leaf_data(right) +
1905 btrfs_item_offset_nr(right, push_items - 1),
1906 push_space);
1907 old_left_nritems = btrfs_header_nritems(left);
1908 BUG_ON(old_left_nritems == 0);
1910 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1911 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1912 u32 ioff;
1914 item = btrfs_item_nr(i);
1915 ioff = btrfs_item_offset(left, item);
1916 btrfs_set_item_offset(left, item,
1917 ioff - (BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1918 old_left_item_size));
1920 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1922 /* fixup right node */
1923 if (push_items > right_nritems) {
1924 printk("push items %d nr %u\n", push_items, right_nritems);
1925 WARN_ON(1);
1928 if (push_items < right_nritems) {
1929 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1930 leaf_data_end(fs_info, right);
1931 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1932 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
1933 push_space,
1934 btrfs_leaf_data(right) +
1935 leaf_data_end(fs_info, right),
1936 push_space);
1938 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1939 btrfs_item_nr_offset(push_items),
1940 (btrfs_header_nritems(right) - push_items) *
1941 sizeof(struct btrfs_item));
1943 right_nritems -= push_items;
1944 btrfs_set_header_nritems(right, right_nritems);
1945 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info);
1946 for (i = 0; i < right_nritems; i++) {
1947 item = btrfs_item_nr(i);
1948 push_space = push_space - btrfs_item_size(right, item);
1949 btrfs_set_item_offset(right, item, push_space);
1952 btrfs_mark_buffer_dirty(left);
1953 if (right_nritems)
1954 btrfs_mark_buffer_dirty(right);
1956 btrfs_item_key(right, &disk_key, 0);
1957 btrfs_fixup_low_keys(root, path, &disk_key, 1);
1959 /* then fixup the leaf pointer in the path */
1960 if (path->slots[0] < push_items) {
1961 path->slots[0] += old_left_nritems;
1962 free_extent_buffer(path->nodes[0]);
1963 path->nodes[0] = left;
1964 path->slots[1] -= 1;
1965 } else {
1966 free_extent_buffer(left);
1967 path->slots[0] -= push_items;
1969 BUG_ON(path->slots[0] < 0);
1970 return ret;
1974 * split the path's leaf in two, making sure there is at least data_size
1975 * available for the resulting leaf level of the path.
1977 * returns 0 if all went well and < 0 on failure.
1979 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
1980 struct btrfs_root *root,
1981 struct btrfs_path *path,
1982 struct extent_buffer *l,
1983 struct extent_buffer *right,
1984 int slot, int mid, int nritems)
1986 int data_copy_size;
1987 int rt_data_off;
1988 int i;
1989 int ret = 0;
1990 int wret;
1991 struct btrfs_disk_key disk_key;
1993 nritems = nritems - mid;
1994 btrfs_set_header_nritems(right, nritems);
1995 data_copy_size = btrfs_item_end_nr(l, mid) -
1996 leaf_data_end(root->fs_info, l);
1998 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1999 btrfs_item_nr_offset(mid),
2000 nritems * sizeof(struct btrfs_item));
2002 copy_extent_buffer(right, l,
2003 btrfs_leaf_data(right) +
2004 BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2005 data_copy_size, btrfs_leaf_data(l) +
2006 leaf_data_end(root->fs_info, l), data_copy_size);
2008 rt_data_off = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
2009 btrfs_item_end_nr(l, mid);
2011 for (i = 0; i < nritems; i++) {
2012 struct btrfs_item *item = btrfs_item_nr(i);
2013 u32 ioff = btrfs_item_offset(right, item);
2014 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2017 btrfs_set_header_nritems(l, mid);
2018 ret = 0;
2019 btrfs_item_key(right, &disk_key, 0);
2020 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2021 path->slots[1] + 1, 1);
2022 if (wret)
2023 ret = wret;
2025 btrfs_mark_buffer_dirty(right);
2026 btrfs_mark_buffer_dirty(l);
2027 BUG_ON(path->slots[0] != slot);
2029 if (mid <= slot) {
2030 free_extent_buffer(path->nodes[0]);
2031 path->nodes[0] = right;
2032 path->slots[0] -= mid;
2033 path->slots[1] += 1;
2034 } else {
2035 free_extent_buffer(right);
2038 BUG_ON(path->slots[0] < 0);
2040 return ret;
2044 * split the path's leaf in two, making sure there is at least data_size
2045 * available for the resulting leaf level of the path.
2047 * returns 0 if all went well and < 0 on failure.
2049 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2050 struct btrfs_root *root,
2051 struct btrfs_key *ins_key,
2052 struct btrfs_path *path, int data_size,
2053 int extend)
2055 struct btrfs_disk_key disk_key;
2056 struct extent_buffer *l;
2057 u32 nritems;
2058 int mid;
2059 int slot;
2060 struct extent_buffer *right;
2061 int ret = 0;
2062 int wret;
2063 int split;
2064 int num_doubles = 0;
2066 l = path->nodes[0];
2067 slot = path->slots[0];
2068 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2069 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info))
2070 return -EOVERFLOW;
2072 /* first try to make some room by pushing left and right */
2073 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2074 wret = push_leaf_right(trans, root, path, data_size, 0);
2075 if (wret < 0)
2076 return wret;
2077 if (wret) {
2078 wret = push_leaf_left(trans, root, path, data_size, 0);
2079 if (wret < 0)
2080 return wret;
2082 l = path->nodes[0];
2084 /* did the pushes work? */
2085 if (btrfs_leaf_free_space(root, l) >= data_size)
2086 return 0;
2089 if (!path->nodes[1]) {
2090 ret = insert_new_root(trans, root, path, 1);
2091 if (ret)
2092 return ret;
2094 again:
2095 split = 1;
2096 l = path->nodes[0];
2097 slot = path->slots[0];
2098 nritems = btrfs_header_nritems(l);
2099 mid = (nritems + 1) / 2;
2101 if (mid <= slot) {
2102 if (nritems == 1 ||
2103 leaf_space_used(l, mid, nritems - mid) + data_size >
2104 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2105 if (slot >= nritems) {
2106 split = 0;
2107 } else {
2108 mid = slot;
2109 if (mid != nritems &&
2110 leaf_space_used(l, mid, nritems - mid) +
2111 data_size >
2112 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2113 split = 2;
2117 } else {
2118 if (leaf_space_used(l, 0, mid) + data_size >
2119 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2120 if (!extend && data_size && slot == 0) {
2121 split = 0;
2122 } else if ((extend || !data_size) && slot == 0) {
2123 mid = 1;
2124 } else {
2125 mid = slot;
2126 if (mid != nritems &&
2127 leaf_space_used(l, mid, nritems - mid) +
2128 data_size >
2129 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
2130 split = 2 ;
2136 if (split == 0)
2137 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2138 else
2139 btrfs_item_key(l, &disk_key, mid);
2141 right = btrfs_alloc_free_block(trans, root, root->fs_info->nodesize,
2142 root->root_key.objectid,
2143 &disk_key, 0, l->start, 0);
2144 if (IS_ERR(right)) {
2145 BUG_ON(1);
2146 return PTR_ERR(right);
2149 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2150 btrfs_set_header_bytenr(right, right->start);
2151 btrfs_set_header_generation(right, trans->transid);
2152 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2153 btrfs_set_header_owner(right, root->root_key.objectid);
2154 btrfs_set_header_level(right, 0);
2155 write_extent_buffer(right, root->fs_info->fsid,
2156 btrfs_header_fsid(), BTRFS_FSID_SIZE);
2158 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2159 btrfs_header_chunk_tree_uuid(right),
2160 BTRFS_UUID_SIZE);
2162 if (split == 0) {
2163 if (mid <= slot) {
2164 btrfs_set_header_nritems(right, 0);
2165 wret = insert_ptr(trans, root, path,
2166 &disk_key, right->start,
2167 path->slots[1] + 1, 1);
2168 if (wret)
2169 ret = wret;
2171 free_extent_buffer(path->nodes[0]);
2172 path->nodes[0] = right;
2173 path->slots[0] = 0;
2174 path->slots[1] += 1;
2175 } else {
2176 btrfs_set_header_nritems(right, 0);
2177 wret = insert_ptr(trans, root, path,
2178 &disk_key,
2179 right->start,
2180 path->slots[1], 1);
2181 if (wret)
2182 ret = wret;
2183 free_extent_buffer(path->nodes[0]);
2184 path->nodes[0] = right;
2185 path->slots[0] = 0;
2186 if (path->slots[1] == 0) {
2187 btrfs_fixup_low_keys(root, path,
2188 &disk_key, 1);
2191 btrfs_mark_buffer_dirty(right);
2192 return ret;
2195 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2196 BUG_ON(ret);
2198 if (split == 2) {
2199 BUG_ON(num_doubles != 0);
2200 num_doubles++;
2201 goto again;
2204 return ret;
2208 * This function splits a single item into two items,
2209 * giving 'new_key' to the new item and splitting the
2210 * old one at split_offset (from the start of the item).
2212 * The path may be released by this operation. After
2213 * the split, the path is pointing to the old item. The
2214 * new item is going to be in the same node as the old one.
2216 * Note, the item being split must be smaller enough to live alone on
2217 * a tree block with room for one extra struct btrfs_item
2219 * This allows us to split the item in place, keeping a lock on the
2220 * leaf the entire time.
2222 int btrfs_split_item(struct btrfs_trans_handle *trans,
2223 struct btrfs_root *root,
2224 struct btrfs_path *path,
2225 struct btrfs_key *new_key,
2226 unsigned long split_offset)
2228 u32 item_size;
2229 struct extent_buffer *leaf;
2230 struct btrfs_key orig_key;
2231 struct btrfs_item *item;
2232 struct btrfs_item *new_item;
2233 int ret = 0;
2234 int slot;
2235 u32 nritems;
2236 u32 orig_offset;
2237 struct btrfs_disk_key disk_key;
2238 char *buf;
2240 leaf = path->nodes[0];
2241 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2242 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2243 goto split;
2245 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2246 btrfs_release_path(path);
2248 path->search_for_split = 1;
2250 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2251 path->search_for_split = 0;
2253 /* if our item isn't there or got smaller, return now */
2254 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2255 path->slots[0])) {
2256 return -EAGAIN;
2259 ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2260 BUG_ON(ret);
2262 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2263 leaf = path->nodes[0];
2265 split:
2266 item = btrfs_item_nr(path->slots[0]);
2267 orig_offset = btrfs_item_offset(leaf, item);
2268 item_size = btrfs_item_size(leaf, item);
2271 buf = kmalloc(item_size, GFP_NOFS);
2272 BUG_ON(!buf);
2273 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2274 path->slots[0]), item_size);
2275 slot = path->slots[0] + 1;
2276 leaf = path->nodes[0];
2278 nritems = btrfs_header_nritems(leaf);
2280 if (slot < nritems) {
2281 /* shift the items */
2282 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2283 btrfs_item_nr_offset(slot),
2284 (nritems - slot) * sizeof(struct btrfs_item));
2288 btrfs_cpu_key_to_disk(&disk_key, new_key);
2289 btrfs_set_item_key(leaf, &disk_key, slot);
2291 new_item = btrfs_item_nr(slot);
2293 btrfs_set_item_offset(leaf, new_item, orig_offset);
2294 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2296 btrfs_set_item_offset(leaf, item,
2297 orig_offset + item_size - split_offset);
2298 btrfs_set_item_size(leaf, item, split_offset);
2300 btrfs_set_header_nritems(leaf, nritems + 1);
2302 /* write the data for the start of the original item */
2303 write_extent_buffer(leaf, buf,
2304 btrfs_item_ptr_offset(leaf, path->slots[0]),
2305 split_offset);
2307 /* write the data for the new item */
2308 write_extent_buffer(leaf, buf + split_offset,
2309 btrfs_item_ptr_offset(leaf, slot),
2310 item_size - split_offset);
2311 btrfs_mark_buffer_dirty(leaf);
2313 ret = 0;
2314 if (btrfs_leaf_free_space(root, leaf) < 0) {
2315 btrfs_print_leaf(root, leaf);
2316 BUG();
2318 kfree(buf);
2319 return ret;
2322 int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
2323 u32 new_size, int from_end)
2325 int ret = 0;
2326 int slot;
2327 struct extent_buffer *leaf;
2328 struct btrfs_item *item;
2329 u32 nritems;
2330 unsigned int data_end;
2331 unsigned int old_data_start;
2332 unsigned int old_size;
2333 unsigned int size_diff;
2334 int i;
2336 leaf = path->nodes[0];
2337 slot = path->slots[0];
2339 old_size = btrfs_item_size_nr(leaf, slot);
2340 if (old_size == new_size)
2341 return 0;
2343 nritems = btrfs_header_nritems(leaf);
2344 data_end = leaf_data_end(root->fs_info, leaf);
2346 old_data_start = btrfs_item_offset_nr(leaf, slot);
2348 size_diff = old_size - new_size;
2350 BUG_ON(slot < 0);
2351 BUG_ON(slot >= nritems);
2354 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2356 /* first correct the data pointers */
2357 for (i = slot; i < nritems; i++) {
2358 u32 ioff;
2359 item = btrfs_item_nr(i);
2360 ioff = btrfs_item_offset(leaf, item);
2361 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2364 /* shift the data */
2365 if (from_end) {
2366 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2367 data_end + size_diff, btrfs_leaf_data(leaf) +
2368 data_end, old_data_start + new_size - data_end);
2369 } else {
2370 struct btrfs_disk_key disk_key;
2371 u64 offset;
2373 btrfs_item_key(leaf, &disk_key, slot);
2375 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2376 unsigned long ptr;
2377 struct btrfs_file_extent_item *fi;
2379 fi = btrfs_item_ptr(leaf, slot,
2380 struct btrfs_file_extent_item);
2381 fi = (struct btrfs_file_extent_item *)(
2382 (unsigned long)fi - size_diff);
2384 if (btrfs_file_extent_type(leaf, fi) ==
2385 BTRFS_FILE_EXTENT_INLINE) {
2386 ptr = btrfs_item_ptr_offset(leaf, slot);
2387 memmove_extent_buffer(leaf, ptr,
2388 (unsigned long)fi,
2389 offsetof(struct btrfs_file_extent_item,
2390 disk_bytenr));
2394 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2395 data_end + size_diff, btrfs_leaf_data(leaf) +
2396 data_end, old_data_start - data_end);
2398 offset = btrfs_disk_key_offset(&disk_key);
2399 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2400 btrfs_set_item_key(leaf, &disk_key, slot);
2401 if (slot == 0)
2402 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2405 item = btrfs_item_nr(slot);
2406 btrfs_set_item_size(leaf, item, new_size);
2407 btrfs_mark_buffer_dirty(leaf);
2409 ret = 0;
2410 if (btrfs_leaf_free_space(root, leaf) < 0) {
2411 btrfs_print_leaf(root, leaf);
2412 BUG();
2414 return ret;
2417 int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
2418 u32 data_size)
2420 int ret = 0;
2421 int slot;
2422 struct extent_buffer *leaf;
2423 struct btrfs_item *item;
2424 u32 nritems;
2425 unsigned int data_end;
2426 unsigned int old_data;
2427 unsigned int old_size;
2428 int i;
2430 leaf = path->nodes[0];
2432 nritems = btrfs_header_nritems(leaf);
2433 data_end = leaf_data_end(root->fs_info, leaf);
2435 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2436 btrfs_print_leaf(root, leaf);
2437 BUG();
2439 slot = path->slots[0];
2440 old_data = btrfs_item_end_nr(leaf, slot);
2442 BUG_ON(slot < 0);
2443 if (slot >= nritems) {
2444 btrfs_print_leaf(root, leaf);
2445 printk("slot %d too large, nritems %d\n", slot, nritems);
2446 BUG_ON(1);
2450 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2452 /* first correct the data pointers */
2453 for (i = slot; i < nritems; i++) {
2454 u32 ioff;
2455 item = btrfs_item_nr(i);
2456 ioff = btrfs_item_offset(leaf, item);
2457 btrfs_set_item_offset(leaf, item, ioff - data_size);
2460 /* shift the data */
2461 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2462 data_end - data_size, btrfs_leaf_data(leaf) +
2463 data_end, old_data - data_end);
2465 data_end = old_data;
2466 old_size = btrfs_item_size_nr(leaf, slot);
2467 item = btrfs_item_nr(slot);
2468 btrfs_set_item_size(leaf, item, old_size + data_size);
2469 btrfs_mark_buffer_dirty(leaf);
2471 ret = 0;
2472 if (btrfs_leaf_free_space(root, leaf) < 0) {
2473 btrfs_print_leaf(root, leaf);
2474 BUG();
2476 return ret;
2480 * Given a key and some data, insert an item into the tree.
2481 * This does all the path init required, making room in the tree if needed.
2483 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2484 struct btrfs_root *root,
2485 struct btrfs_path *path,
2486 struct btrfs_key *cpu_key, u32 *data_size,
2487 int nr)
2489 struct extent_buffer *leaf;
2490 struct btrfs_item *item;
2491 int ret = 0;
2492 int slot;
2493 int i;
2494 u32 nritems;
2495 u32 total_size = 0;
2496 u32 total_data = 0;
2497 unsigned int data_end;
2498 struct btrfs_disk_key disk_key;
2500 for (i = 0; i < nr; i++) {
2501 total_data += data_size[i];
2504 /* create a root if there isn't one */
2505 if (!root->node)
2506 BUG();
2508 total_size = total_data + nr * sizeof(struct btrfs_item);
2509 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2510 if (ret == 0) {
2511 return -EEXIST;
2513 if (ret < 0)
2514 goto out;
2516 leaf = path->nodes[0];
2518 nritems = btrfs_header_nritems(leaf);
2519 data_end = leaf_data_end(root->fs_info, leaf);
2521 if (btrfs_leaf_free_space(root, leaf) < total_size) {
2522 btrfs_print_leaf(root, leaf);
2523 printk("not enough freespace need %u have %d\n",
2524 total_size, btrfs_leaf_free_space(root, leaf));
2525 BUG();
2528 slot = path->slots[0];
2529 BUG_ON(slot < 0);
2531 if (slot < nritems) {
2532 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2534 if (old_data < data_end) {
2535 btrfs_print_leaf(root, leaf);
2536 printk("slot %d old_data %d data_end %d\n",
2537 slot, old_data, data_end);
2538 BUG_ON(1);
2541 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2543 /* first correct the data pointers */
2544 for (i = slot; i < nritems; i++) {
2545 u32 ioff;
2547 item = btrfs_item_nr(i);
2548 ioff = btrfs_item_offset(leaf, item);
2549 btrfs_set_item_offset(leaf, item, ioff - total_data);
2552 /* shift the items */
2553 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2554 btrfs_item_nr_offset(slot),
2555 (nritems - slot) * sizeof(struct btrfs_item));
2557 /* shift the data */
2558 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2559 data_end - total_data, btrfs_leaf_data(leaf) +
2560 data_end, old_data - data_end);
2561 data_end = old_data;
2564 /* setup the item for the new data */
2565 for (i = 0; i < nr; i++) {
2566 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2567 btrfs_set_item_key(leaf, &disk_key, slot + i);
2568 item = btrfs_item_nr(slot + i);
2569 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2570 data_end -= data_size[i];
2571 btrfs_set_item_size(leaf, item, data_size[i]);
2573 btrfs_set_header_nritems(leaf, nritems + nr);
2574 btrfs_mark_buffer_dirty(leaf);
2576 ret = 0;
2577 if (slot == 0) {
2578 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2579 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2582 if (btrfs_leaf_free_space(root, leaf) < 0) {
2583 btrfs_print_leaf(root, leaf);
2584 BUG();
2587 out:
2588 return ret;
2592 * Given a key and some data, insert an item into the tree.
2593 * This does all the path init required, making room in the tree if needed.
2595 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2596 *root, struct btrfs_key *cpu_key, void *data, u32
2597 data_size)
2599 int ret = 0;
2600 struct btrfs_path *path;
2601 struct extent_buffer *leaf;
2602 unsigned long ptr;
2604 path = btrfs_alloc_path();
2605 if (!path)
2606 return -ENOMEM;
2608 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2609 if (!ret) {
2610 leaf = path->nodes[0];
2611 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2612 write_extent_buffer(leaf, data, ptr, data_size);
2613 btrfs_mark_buffer_dirty(leaf);
2615 btrfs_free_path(path);
2616 return ret;
2620 * delete the pointer from a given node.
2622 * If the delete empties a node, the node is removed from the tree,
2623 * continuing all the way the root if required. The root is converted into
2624 * a leaf if all the nodes are emptied.
2626 int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
2627 int level, int slot)
2629 struct extent_buffer *parent = path->nodes[level];
2630 u32 nritems;
2631 int ret = 0;
2633 nritems = btrfs_header_nritems(parent);
2634 if (slot < nritems - 1) {
2635 /* shift the items */
2636 memmove_extent_buffer(parent,
2637 btrfs_node_key_ptr_offset(slot),
2638 btrfs_node_key_ptr_offset(slot + 1),
2639 sizeof(struct btrfs_key_ptr) *
2640 (nritems - slot - 1));
2642 nritems--;
2643 btrfs_set_header_nritems(parent, nritems);
2644 if (nritems == 0 && parent == root->node) {
2645 BUG_ON(btrfs_header_level(root->node) != 1);
2646 /* just turn the root into a leaf and break */
2647 btrfs_set_header_level(root->node, 0);
2648 } else if (slot == 0) {
2649 struct btrfs_disk_key disk_key;
2651 btrfs_node_key(parent, &disk_key, 0);
2652 btrfs_fixup_low_keys(root, path, &disk_key, level + 1);
2654 btrfs_mark_buffer_dirty(parent);
2655 return ret;
2659 * a helper function to delete the leaf pointed to by path->slots[1] and
2660 * path->nodes[1].
2662 * This deletes the pointer in path->nodes[1] and frees the leaf
2663 * block extent. zero is returned if it all worked out, < 0 otherwise.
2665 * The path must have already been setup for deleting the leaf, including
2666 * all the proper balancing. path->nodes[1] must be locked.
2668 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
2669 struct btrfs_root *root,
2670 struct btrfs_path *path,
2671 struct extent_buffer *leaf)
2673 int ret;
2675 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
2676 ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
2677 if (ret)
2678 return ret;
2680 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
2681 0, root->root_key.objectid, 0, 0);
2682 return ret;
2686 * delete the item at the leaf level in path. If that empties
2687 * the leaf, remove it from the tree
2689 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2690 struct btrfs_path *path, int slot, int nr)
2692 struct extent_buffer *leaf;
2693 struct btrfs_item *item;
2694 int last_off;
2695 int dsize = 0;
2696 int ret = 0;
2697 int wret;
2698 int i;
2699 u32 nritems;
2701 leaf = path->nodes[0];
2702 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2704 for (i = 0; i < nr; i++)
2705 dsize += btrfs_item_size_nr(leaf, slot + i);
2707 nritems = btrfs_header_nritems(leaf);
2709 if (slot + nr != nritems) {
2710 int data_end = leaf_data_end(root->fs_info, leaf);
2712 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2713 data_end + dsize,
2714 btrfs_leaf_data(leaf) + data_end,
2715 last_off - data_end);
2717 for (i = slot + nr; i < nritems; i++) {
2718 u32 ioff;
2720 item = btrfs_item_nr(i);
2721 ioff = btrfs_item_offset(leaf, item);
2722 btrfs_set_item_offset(leaf, item, ioff + dsize);
2725 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2726 btrfs_item_nr_offset(slot + nr),
2727 sizeof(struct btrfs_item) *
2728 (nritems - slot - nr));
2730 btrfs_set_header_nritems(leaf, nritems - nr);
2731 nritems -= nr;
2733 /* delete the leaf if we've emptied it */
2734 if (nritems == 0) {
2735 if (leaf == root->node) {
2736 btrfs_set_header_level(leaf, 0);
2737 } else {
2738 clean_tree_block(trans, root, leaf);
2739 wret = btrfs_del_leaf(trans, root, path, leaf);
2740 BUG_ON(ret);
2741 if (wret)
2742 ret = wret;
2744 } else {
2745 int used = leaf_space_used(leaf, 0, nritems);
2746 if (slot == 0) {
2747 struct btrfs_disk_key disk_key;
2749 btrfs_item_key(leaf, &disk_key, 0);
2750 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2753 /* delete the leaf if it is mostly empty */
2754 if (used < BTRFS_LEAF_DATA_SIZE(root->fs_info) / 4) {
2755 /* push_leaf_left fixes the path.
2756 * make sure the path still points to our leaf
2757 * for possible call to del_ptr below
2759 slot = path->slots[1];
2760 extent_buffer_get(leaf);
2762 wret = push_leaf_left(trans, root, path, 1, 1);
2763 if (wret < 0 && wret != -ENOSPC)
2764 ret = wret;
2766 if (path->nodes[0] == leaf &&
2767 btrfs_header_nritems(leaf)) {
2768 wret = push_leaf_right(trans, root, path, 1, 1);
2769 if (wret < 0 && wret != -ENOSPC)
2770 ret = wret;
2773 if (btrfs_header_nritems(leaf) == 0) {
2774 clean_tree_block(trans, root, leaf);
2775 path->slots[1] = slot;
2776 ret = btrfs_del_leaf(trans, root, path, leaf);
2777 BUG_ON(ret);
2778 free_extent_buffer(leaf);
2780 } else {
2781 btrfs_mark_buffer_dirty(leaf);
2782 free_extent_buffer(leaf);
2784 } else {
2785 btrfs_mark_buffer_dirty(leaf);
2788 return ret;
2792 * walk up the tree as far as required to find the previous leaf.
2793 * returns 0 if it found something or 1 if there are no lesser leaves.
2794 * returns < 0 on io errors.
2796 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2798 int slot;
2799 int level = 1;
2800 struct extent_buffer *c;
2801 struct extent_buffer *next = NULL;
2802 struct btrfs_fs_info *fs_info = root->fs_info;
2804 while(level < BTRFS_MAX_LEVEL) {
2805 if (!path->nodes[level])
2806 return 1;
2808 slot = path->slots[level];
2809 c = path->nodes[level];
2810 if (slot == 0) {
2811 level++;
2812 if (level == BTRFS_MAX_LEVEL)
2813 return 1;
2814 continue;
2816 slot--;
2818 next = read_node_slot(fs_info, c, slot);
2819 if (!extent_buffer_uptodate(next)) {
2820 if (IS_ERR(next))
2821 return PTR_ERR(next);
2822 return -EIO;
2824 break;
2826 path->slots[level] = slot;
2827 while(1) {
2828 level--;
2829 c = path->nodes[level];
2830 free_extent_buffer(c);
2831 slot = btrfs_header_nritems(next);
2832 if (slot != 0)
2833 slot--;
2834 path->nodes[level] = next;
2835 path->slots[level] = slot;
2836 if (!level)
2837 break;
2838 next = read_node_slot(fs_info, next, slot);
2839 if (!extent_buffer_uptodate(next)) {
2840 if (IS_ERR(next))
2841 return PTR_ERR(next);
2842 return -EIO;
2845 return 0;
2849 * walk up the tree as far as required to find the next leaf.
2850 * returns 0 if it found something or 1 if there are no greater leaves.
2851 * returns < 0 on io errors.
2853 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2855 int slot;
2856 int level = 1;
2857 struct extent_buffer *c;
2858 struct extent_buffer *next = NULL;
2859 struct btrfs_fs_info *fs_info = root->fs_info;
2861 while(level < BTRFS_MAX_LEVEL) {
2862 if (!path->nodes[level])
2863 return 1;
2865 slot = path->slots[level] + 1;
2866 c = path->nodes[level];
2867 if (slot >= btrfs_header_nritems(c)) {
2868 level++;
2869 if (level == BTRFS_MAX_LEVEL)
2870 return 1;
2871 continue;
2874 if (path->reada)
2875 reada_for_search(root, path, level, slot, 0);
2877 next = read_node_slot(fs_info, c, slot);
2878 if (!extent_buffer_uptodate(next))
2879 return -EIO;
2880 break;
2882 path->slots[level] = slot;
2883 while(1) {
2884 level--;
2885 c = path->nodes[level];
2886 free_extent_buffer(c);
2887 path->nodes[level] = next;
2888 path->slots[level] = 0;
2889 if (!level)
2890 break;
2891 if (path->reada)
2892 reada_for_search(root, path, level, 0, 0);
2893 next = read_node_slot(fs_info, next, 0);
2894 if (!extent_buffer_uptodate(next))
2895 return -EIO;
2897 return 0;
2900 int btrfs_previous_item(struct btrfs_root *root,
2901 struct btrfs_path *path, u64 min_objectid,
2902 int type)
2904 struct btrfs_key found_key;
2905 struct extent_buffer *leaf;
2906 u32 nritems;
2907 int ret;
2909 while(1) {
2910 if (path->slots[0] == 0) {
2911 ret = btrfs_prev_leaf(root, path);
2912 if (ret != 0)
2913 return ret;
2914 } else {
2915 path->slots[0]--;
2917 leaf = path->nodes[0];
2918 nritems = btrfs_header_nritems(leaf);
2919 if (nritems == 0)
2920 return 1;
2921 if (path->slots[0] == nritems)
2922 path->slots[0]--;
2924 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2925 if (found_key.objectid < min_objectid)
2926 break;
2927 if (found_key.type == type)
2928 return 0;
2929 if (found_key.objectid == min_objectid &&
2930 found_key.type < type)
2931 break;
2933 return 1;
2937 * search in extent tree to find a previous Metadata/Data extent item with
2938 * min objecitd.
2940 * returns 0 if something is found, 1 if nothing was found and < 0 on error
2942 int btrfs_previous_extent_item(struct btrfs_root *root,
2943 struct btrfs_path *path, u64 min_objectid)
2945 struct btrfs_key found_key;
2946 struct extent_buffer *leaf;
2947 u32 nritems;
2948 int ret;
2950 while (1) {
2951 if (path->slots[0] == 0) {
2952 ret = btrfs_prev_leaf(root, path);
2953 if (ret != 0)
2954 return ret;
2955 } else {
2956 path->slots[0]--;
2958 leaf = path->nodes[0];
2959 nritems = btrfs_header_nritems(leaf);
2960 if (nritems == 0)
2961 return 1;
2962 if (path->slots[0] == nritems)
2963 path->slots[0]--;
2965 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2966 if (found_key.objectid < min_objectid)
2967 break;
2968 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2969 found_key.type == BTRFS_METADATA_ITEM_KEY)
2970 return 0;
2971 if (found_key.objectid == min_objectid &&
2972 found_key.type < BTRFS_EXTENT_ITEM_KEY)
2973 break;
2975 return 1;
2979 * Search in extent tree to found next meta/data extent
2980 * Caller needs to check for no-hole or skinny metadata features.
2982 int btrfs_next_extent_item(struct btrfs_root *root,
2983 struct btrfs_path *path, u64 max_objectid)
2985 struct btrfs_key found_key;
2986 int ret;
2988 while (1) {
2989 ret = btrfs_next_item(root, path);
2990 if (ret)
2991 return ret;
2992 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2993 path->slots[0]);
2994 if (found_key.objectid > max_objectid)
2995 return 1;
2996 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
2997 found_key.type == BTRFS_METADATA_ITEM_KEY)
2998 return 0;