19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/rbtree.h>
46 u32 blocksize,
u64 parent_transid,
90 #ifdef CONFIG_DEBUG_LOCK_ALLOC
117 #ifdef CONFIG_DEBUG_LOCK_ALLOC
147 btrfs_tree_unlock_rw(p->
nodes[i], p->
locks[i]);
200 if (eb == root->
node)
219 if (eb == root->
node)
231 static void add_root_to_dirty_list(
struct btrfs_root *root)
233 spin_lock(&root->
fs_info->trans_lock);
236 &root->
fs_info->dirty_cowonly_roots);
238 spin_unlock(&root->
fs_info->trans_lock);
257 root->
fs_info->running_transaction->transid);
260 level = btrfs_header_level(buf);
262 btrfs_item_key(buf, &disk_key, 0);
267 new_root_objectid, &disk_key, level,
273 btrfs_set_header_bytenr(cow, cow->
start);
274 btrfs_set_header_generation(cow, trans->
transid);
281 btrfs_set_header_owner(cow, new_root_objectid);
284 (
unsigned long)btrfs_header_fsid(cow),
377 tree_mod_log_write_lock(fs_info);
380 elem->
seq = btrfs_inc_tree_mod_seq(fs_info);
383 seq = btrfs_inc_tree_mod_seq(fs_info);
385 tree_mod_log_write_unlock(fs_info);
399 u64 seq_putting = elem->
seq;
409 if (cur_elem->
seq < min_seq) {
410 if (seq_putting > cur_elem->
seq) {
418 min_seq = cur_elem->
seq;
427 tree_mod_log_write_lock(fs_info);
432 if (tm->
seq > min_seq)
437 tree_mod_log_write_unlock(fs_info);
464 new = &((*new)->rb_left);
466 new = &((*new)->rb_right);
467 else if (cur->
seq < tm->
seq)
468 new = &((*new)->rb_left);
469 else if (cur->
seq > tm->
seq)
470 new = &((*new)->rb_right);
477 rb_link_node(&tm->
node, parent,
new);
488 static inline int tree_mod_dont_log(
struct btrfs_fs_info *fs_info,
491 if (list_empty(&(fs_info)->tree_mod_seq_list))
493 if (eb && btrfs_header_level(eb) == 0)
496 tree_mod_log_write_lock(fs_info);
502 tree_mod_log_write_unlock(fs_info);
524 tm = *tm_ret = kzalloc(
sizeof(*tm),
GFP_ATOMIC);
528 tm->
seq = btrfs_inc_tree_mod_seq(fs_info);
540 ret = tree_mod_alloc(fs_info, flags, &tm);
547 tm->
blockptr = btrfs_node_blockptr(eb, slot);
551 tm->
generation = btrfs_node_ptr_generation(eb, slot);
553 return __tree_mod_log_insert(fs_info, tm);
563 if (tree_mod_dont_log(fs_info, eb))
566 ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
568 tree_mod_log_write_unlock(fs_info);
576 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op,
GFP_NOFS);
580 tree_mod_log_insert_key_locked(
struct btrfs_fs_info *fs_info,
584 return __tree_mod_log_insert_key(fs_info, eb, slot, op,
GFP_NOFS);
590 int nr_items,
gfp_t flags)
596 if (tree_mod_dont_log(fs_info, eb))
604 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
605 ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot,
610 ret = tree_mod_alloc(fs_info, flags, &tm);
616 tm->
move.dst_slot = dst_slot;
617 tm->
move.nr_items = nr_items;
620 ret = __tree_mod_log_insert(fs_info, tm);
622 tree_mod_log_write_unlock(fs_info);
633 if (btrfs_header_level(eb) == 0)
636 nritems = btrfs_header_nritems(eb);
637 for (i = nritems - 1; i >= 0; i--) {
638 ret = tree_mod_log_insert_key_locked(fs_info, eb, i,
652 if (tree_mod_dont_log(fs_info,
NULL))
655 ret = tree_mod_alloc(fs_info, flags, &tm);
661 tm->
old_root.level = btrfs_header_level(old_root);
662 tm->
generation = btrfs_header_generation(old_root);
665 ret = __tree_mod_log_insert(fs_info, tm);
667 tree_mod_log_write_unlock(fs_info);
681 tree_mod_log_read_lock(fs_info);
686 if (cur->
index < index) {
688 }
else if (cur->
index > index) {
690 }
else if (cur->
seq < min_seq) {
692 }
else if (!smallest) {
698 }
else if (cur->
seq > min_seq) {
709 tree_mod_log_read_unlock(fs_info);
723 return __tree_mod_log_search(fs_info, start, min_seq, 1);
734 return __tree_mod_log_search(fs_info, start, min_seq, 0);
740 unsigned long src_offset,
int nr_items)
745 if (tree_mod_dont_log(fs_info,
NULL))
748 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) {
749 tree_mod_log_write_unlock(fs_info);
753 for (i = 0; i < nr_items; i++) {
754 ret = tree_mod_log_insert_key_locked(fs_info, src,
758 ret = tree_mod_log_insert_key_locked(fs_info, dst,
764 tree_mod_log_write_unlock(fs_info);
769 int dst_offset,
int src_offset,
int nr_items)
772 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
784 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
793 if (tree_mod_dont_log(fs_info, eb))
796 __tree_mod_log_free_eb(fs_info, eb);
798 tree_mod_log_write_unlock(fs_info);
802 tree_mod_log_set_root_pointer(
struct btrfs_root *root,
806 ret = tree_mod_log_insert_root(root->
fs_info, root->
node,
825 (btrfs_header_generation(buf) <=
826 btrfs_root_last_snapshot(&root->
root_item) ||
829 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
868 buf->
len, &refs, &flags);
885 owner = btrfs_header_owner(buf);
890 if ((owner == root->
root_key.objectid ||
913 if (new_flags != 0) {
922 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
932 tree_mod_log_free_eb(root->
fs_info, buf);
956 u64 search_start,
u64 empty_size)
971 root->
fs_info->running_transaction->transid);
974 level = btrfs_header_level(buf);
977 btrfs_item_key(buf, &disk_key, 0);
983 parent_start = parent->
start;
991 level, search_start, empty_size);
998 btrfs_set_header_bytenr(cow, cow->
start);
999 btrfs_set_header_generation(cow, trans->
transid);
1006 btrfs_set_header_owner(cow, root->
root_key.objectid);
1009 (
unsigned long)btrfs_header_fsid(cow),
1012 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1021 if (buf == root->
node) {
1022 WARN_ON(parent && parent != buf);
1025 parent_start = buf->
start;
1029 extent_buffer_get(cow);
1030 tree_mod_log_set_root_pointer(root, cow);
1036 add_root_to_dirty_list(root);
1039 parent_start = parent->
start;
1044 tree_mod_log_insert_key(root->
fs_info, parent, parent_slot,
1046 btrfs_set_node_blockptr(parent, parent_slot,
1048 btrfs_set_node_ptr_generation(parent, parent_slot,
1072 u64 root_logical = root->
node->start;
1084 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1105 root_logical = tm->
old_root.logical;
1129 unsigned long o_dst;
1130 unsigned long o_src;
1133 n = btrfs_header_nritems(eb);
1134 while (tm && tm->
seq >= time_seq) {
1145 btrfs_set_node_key(eb, &tm->
key, tm->
slot);
1147 btrfs_set_node_ptr_generation(eb, tm->
slot,
1153 btrfs_set_node_key(eb, &tm->
key, tm->
slot);
1155 btrfs_set_node_ptr_generation(eb, tm->
slot,
1163 o_dst = btrfs_node_key_ptr_offset(tm->
slot);
1164 o_src = btrfs_node_key_ptr_offset(tm->
move.dst_slot);
1166 tm->
move.nr_items * p_size);
1187 btrfs_set_header_nritems(eb, n);
1200 if (btrfs_header_level(eb) == 0)
1203 tm = tree_mod_log_search(fs_info, eb->
start, time_seq);
1212 btrfs_set_header_bytenr(eb_rewin, eb->
start);
1213 btrfs_set_header_backref_rev(eb_rewin,
1214 btrfs_header_backref_rev(eb));
1215 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1216 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1222 extent_buffer_get(eb_rewin);
1225 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1226 WARN_ON(btrfs_header_nritems(eb_rewin) >
1246 u64 old_generation = 0;
1251 tm = __tree_mod_log_oldest_root(root->
fs_info, root, time_seq);
1260 logical = root->
node->start;
1263 tm = tree_mod_log_search(root->
fs_info, logical, time_seq);
1267 blocksize = btrfs_level_size(root, old_root->
level);
1270 pr_warn(
"btrfs: failed to read tree block %llu from get_old_root\n",
1277 }
else if (old_root) {
1289 extent_buffer_get(eb);
1292 btrfs_set_header_bytenr(eb, eb->
start);
1294 btrfs_set_header_owner(eb, root->
root_key.objectid);
1295 btrfs_set_header_level(eb, old_root->
level);
1296 btrfs_set_header_generation(eb, old_generation);
1299 __tree_mod_log_rewind(eb, time_seq, tm);
1301 WARN_ON(btrfs_header_level(eb) != 0);
1312 tm = __tree_mod_log_oldest_root(root->
fs_info, root, time_seq);
1317 level = btrfs_header_level(root->
node);
1342 if (btrfs_header_generation(buf) == trans->
transid &&
1366 (
unsigned long long)trans->
transid,
1367 (
unsigned long long)
1368 root->
fs_info->running_transaction->transid);
1373 (
unsigned long long)trans->
transid,
1374 (
unsigned long long)root->
fs_info->generation);
1378 if (!should_cow_block(trans, root, buf)) {
1383 search_start = buf->
start & ~((
u64)(1024 * 1024 * 1024) - 1);
1386 btrfs_set_lock_blocking(parent);
1387 btrfs_set_lock_blocking(buf);
1389 ret = __btrfs_cow_block(trans, root, buf, parent,
1390 parent_slot, cow_ret, search_start, 0);
1392 trace_btrfs_cow_block(root, buf, *cow_ret);
1403 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1405 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1417 btrfs_disk_key_to_cpu(&
k1, disk);
1449 int start_slot,
int cache_only,
u64 *last_ret,
1455 u64 search_start = *last_ret;
1465 int progress_passed = 0;
1468 parent_level = btrfs_header_level(parent);
1469 if (cache_only && parent_level != 1)
1477 parent_nritems = btrfs_header_nritems(parent);
1478 blocksize = btrfs_level_size(root, parent_level - 1);
1479 end_slot = parent_nritems;
1481 if (parent_nritems == 1)
1484 btrfs_set_lock_blocking(parent);
1486 for (i = start_slot; i < end_slot; i++) {
1490 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1493 progress_passed = 1;
1494 blocknr = btrfs_node_blockptr(parent, i);
1495 gen = btrfs_node_ptr_generation(parent, i);
1496 if (last_block == 0)
1500 other = btrfs_node_blockptr(parent, i - 1);
1501 close = close_blocks(blocknr, other, blocksize);
1503 if (!close && i < end_slot - 2) {
1504 other = btrfs_node_blockptr(parent, i + 1);
1505 close = close_blocks(blocknr, other, blocksize);
1517 if (!cur || !uptodate) {
1527 }
else if (!uptodate) {
1535 if (search_start == 0)
1536 search_start = last_block;
1539 btrfs_set_lock_blocking(cur);
1540 err = __btrfs_cow_block(trans, root, cur, parent, i,
1543 (end_slot - i) * blocksize));
1549 search_start = cur->
start;
1550 last_block = cur->
start;
1551 *last_ret = search_start;
1563 static inline unsigned int leaf_data_end(
struct btrfs_root *root,
1566 u32 nr = btrfs_header_nritems(leaf);
1569 return btrfs_item_offset_nr(leaf, nr - 1);
1596 unsigned long map_start = 0;
1597 unsigned long map_len = 0;
1600 while (low < high) {
1601 mid = (low +
high) / 2;
1604 if (!kaddr || offset < map_start ||
1606 map_start + map_len) {
1610 &kaddr, &map_start, &map_len);
1625 ret = comp_keys(tmp, key);
1645 int level,
int *slot)
1648 return generic_bin_search(eb,
1651 key, btrfs_header_nritems(eb),
1654 return generic_bin_search(eb,
1657 key, btrfs_header_nritems(eb),
1662 int level,
int *slot)
1664 return bin_search(eb, key, level, slot);
1671 btrfs_root_used(&root->
root_item) + size);
1675 static void root_sub_used(
struct btrfs_root *root,
u32 size)
1679 btrfs_root_used(&root->
root_item) - size);
1690 int level = btrfs_header_level(parent);
1693 if (slot >= btrfs_header_nritems(parent))
1699 btrfs_level_size(root, level - 1),
1700 btrfs_node_ptr_generation(parent, slot));
1731 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1734 parent = path->
nodes[level + 1];
1735 pslot = path->
slots[level + 1];
1745 if (btrfs_header_nritems(mid) != 1)
1749 child = read_node_slot(root, mid, 0);
1757 btrfs_set_lock_blocking(child);
1766 tree_mod_log_set_root_pointer(root, child);
1769 add_root_to_dirty_list(root);
1779 root_sub_used(root, mid->
len);
1785 if (btrfs_header_nritems(mid) >
1789 left = read_node_slot(root, parent, pslot - 1);
1792 btrfs_set_lock_blocking(left);
1794 parent, pslot - 1, &left);
1800 right = read_node_slot(root, parent, pslot + 1);
1803 btrfs_set_lock_blocking(right);
1805 parent, pslot + 1, &right);
1814 orig_slot += btrfs_header_nritems(left);
1815 wret = push_node_left(trans, root, left, mid, 1);
1824 wret = push_node_left(trans, root, mid, right, 1);
1825 if (wret < 0 && wret != -
ENOSPC)
1827 if (btrfs_header_nritems(right) == 0) {
1830 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
1831 root_sub_used(root, right->
len);
1838 tree_mod_log_set_node_key(root->
fs_info, parent,
1839 &right_key, pslot + 1, 0);
1840 btrfs_set_node_key(parent, &right_key, pslot + 1);
1844 if (btrfs_header_nritems(mid) == 1) {
1859 wret = balance_node_right(trans, root, mid, left);
1865 wret = push_node_left(trans, root, left, mid, 1);
1871 if (btrfs_header_nritems(mid) == 0) {
1874 del_ptr(trans, root, path, level + 1, pslot, 1);
1875 root_sub_used(root, mid->
len);
1883 tree_mod_log_set_node_key(root->
fs_info, parent, &mid_key,
1885 btrfs_set_node_key(parent, &mid_key, pslot);
1891 if (btrfs_header_nritems(left) > orig_slot) {
1892 extent_buffer_get(left);
1895 path->
slots[level + 1] -= 1;
1902 orig_slot -= btrfs_header_nritems(left);
1908 btrfs_node_blockptr(path->
nodes[level], path->
slots[level]))
1916 if (path->
nodes[level] != left)
1947 parent = path->
nodes[level + 1];
1948 pslot = path->
slots[level + 1];
1954 left = read_node_slot(root, parent, pslot - 1);
1961 btrfs_set_lock_blocking(left);
1963 left_nr = btrfs_header_nritems(left);
1972 wret = push_node_left(trans, root,
1980 orig_slot += left_nr;
1982 tree_mod_log_set_node_key(root->
fs_info, parent,
1983 &disk_key, pslot, 0);
1984 btrfs_set_node_key(parent, &disk_key, pslot);
1986 if (btrfs_header_nritems(left) > orig_slot) {
1988 path->
slots[level + 1] -= 1;
1994 btrfs_header_nritems(left);
2004 right = read_node_slot(root, parent, pslot + 1);
2013 btrfs_set_lock_blocking(right);
2015 right_nr = btrfs_header_nritems(right);
2025 wret = balance_node_right(trans, root,
2035 tree_mod_log_set_node_key(root->
fs_info, parent,
2036 &disk_key, pslot + 1, 0);
2037 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2040 if (btrfs_header_nritems(mid) <= orig_slot) {
2042 path->
slots[level + 1] += 1;
2044 btrfs_header_nritems(mid);
2063 static void reada_for_search(
struct btrfs_root *root,
2083 if (!path->
nodes[level])
2088 search = btrfs_node_blockptr(node, slot);
2089 blocksize = btrfs_level_size(root, level - 1);
2098 nritems = btrfs_header_nritems(node);
2102 if (direction < 0) {
2106 }
else if (direction > 0) {
2111 if (path->
reada < 0 && objectid) {
2113 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2116 search = btrfs_node_blockptr(node, nr);
2117 if ((search <= target && target - search <= 65536) ||
2118 (search > target && search - target <= 65536)) {
2119 gen = btrfs_node_ptr_generation(node, nr);
2124 if ((nread > 65536 || nscan > 32))
2146 parent = path->
nodes[level + 1];
2150 nritems = btrfs_header_nritems(parent);
2151 slot = path->
slots[level + 1];
2152 blocksize = btrfs_level_size(root, level);
2155 block1 = btrfs_node_blockptr(parent, slot - 1);
2156 gen = btrfs_node_ptr_generation(parent, slot - 1);
2167 if (slot + 1 < nritems) {
2168 block2 = btrfs_node_blockptr(parent, slot + 1);
2169 gen = btrfs_node_ptr_generation(parent, slot + 1);
2175 if (block1 || block2) {
2214 int lowest_unlock,
int min_write_lock_level,
2215 int *write_lock_level)
2218 int skip_level =
level;
2223 if (!path->
nodes[i])
2225 if (!path->
locks[i])
2227 if (!no_skips && path->
slots[i] == 0) {
2234 nritems = btrfs_header_nritems(t);
2235 if (nritems < 1 || path->
slots[i] >= nritems - 1) {
2240 if (skip_level < i && i >= lowest_unlock)
2244 if (i >= lowest_unlock && i > skip_level && path->
locks[i]) {
2245 btrfs_tree_unlock_rw(t, path->
locks[i]);
2247 if (write_lock_level &&
2248 i > min_write_lock_level &&
2249 i <= *write_lock_level) {
2250 *write_lock_level = i - 1;
2273 if (!path->
nodes[i])
2275 if (!path->
locks[i])
2277 btrfs_tree_unlock_rw(path->
nodes[i], path->
locks[i]);
2303 blocknr = btrfs_node_blockptr(b, slot);
2304 gen = btrfs_node_ptr_generation(b, slot);
2305 blocksize = btrfs_level_size(root, level - 1);
2353 reada_for_search(root, p, level, slot, key->
objectid);
2386 int *write_lock_level)
2393 if (*write_lock_level < level + 1) {
2394 *write_lock_level = level + 1;
2399 sret = reada_for_balance(root, p, level);
2404 sret = split_node(trans, root, p, level);
2413 }
else if (ins_len < 0 && btrfs_header_nritems(b) <
2417 if (*write_lock_level < level + 1) {
2418 *write_lock_level = level + 1;
2423 sret = reada_for_balance(root, p, level);
2428 sret = balance_level(trans, root, p, level);
2440 BUG_ON(btrfs_header_nritems(b) == 1);
2472 int lowest_unlock = 1;
2475 int write_lock_level = 0;
2476 u8 lowest_level = 0;
2477 int min_write_lock_level;
2480 WARN_ON(lowest_level && ins_len > 0);
2490 write_lock_level = 2;
2491 }
else if (ins_len > 0) {
2496 write_lock_level = 1;
2500 write_lock_level = -1;
2505 min_write_lock_level = write_lock_level;
2519 extent_buffer_get(b);
2520 level = btrfs_header_level(b);
2526 level = btrfs_header_level(b);
2532 level = btrfs_header_level(b);
2533 if (level <= write_lock_level) {
2541 level = btrfs_header_level(b);
2550 level = btrfs_header_level(b);
2562 if (!should_cow_block(trans, root, b))
2571 if (level + 1 > write_lock_level) {
2572 write_lock_level = level + 1;
2578 p->
nodes[level + 1],
2579 p->
slots[level + 1], &b);
2605 ret = bin_search(b, key, level, &slot);
2609 if (ret && slot > 0) {
2614 err = setup_nodes_for_search(trans, root, p, b, level,
2615 ins_len, &write_lock_level);
2631 if (slot == 0 && cow &&
2632 write_lock_level < level + 1) {
2633 write_lock_level = level + 1;
2638 unlock_up(p, level, lowest_unlock,
2639 min_write_lock_level, &write_lock_level);
2641 if (level == lowest_level) {
2647 err = read_block_for_search(trans, root, p,
2648 &b, level, slot, key, 0);
2657 level = btrfs_header_level(b);
2658 if (level <= write_lock_level) {
2683 if (write_lock_level < 1) {
2684 write_lock_level = 1;
2690 err = split_leaf(trans, root, key,
2691 p, ins_len, ret == 0);
2701 unlock_up(p, level, lowest_unlock,
2702 min_write_lock_level, &write_lock_level);
2738 int lowest_unlock = 1;
2739 u8 lowest_level = 0;
2750 b = get_old_root(root, time_seq);
2751 level = btrfs_header_level(b);
2755 level = btrfs_header_level(b);
2767 ret = bin_search(b, key, level, &slot);
2771 if (ret && slot > 0) {
2776 unlock_up(p, level, lowest_unlock, 0,
NULL);
2778 if (level == lowest_level) {
2784 err = read_block_for_search(
NULL, root, p, &b, level,
2785 slot, key, time_seq);
2793 level = btrfs_header_level(b);
2803 b = tree_mod_log_rewind(root->
fs_info, b, time_seq);
2804 if (b != p->
nodes[level]) {
2805 btrfs_tree_unlock_rw(p->
nodes[level],
2812 unlock_up(p, level, lowest_unlock, 0,
NULL);
2840 int find_higher,
int return_any)
2859 if (p->
slots[0] >= btrfs_header_nritems(leaf)) {
2875 if (p->
slots[0] == 0) {
2880 p->
slots[0] = btrfs_header_nritems(leaf) - 1;
2916 int tslot = path->
slots[
i];
2917 if (!path->
nodes[i])
2920 tree_mod_log_set_node_key(root->
fs_info, t, key, tslot, 1);
2921 btrfs_set_node_key(t, key, tslot);
2942 eb = path->
nodes[0];
2943 slot = path->
slots[0];
2945 btrfs_item_key(eb, &disk_key, slot - 1);
2946 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
2948 if (slot < btrfs_header_nritems(eb) - 1) {
2949 btrfs_item_key(eb, &disk_key, slot + 1);
2950 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
2953 btrfs_cpu_key_to_disk(&disk_key, new_key);
2954 btrfs_set_item_key(eb, &disk_key, slot);
2957 fixup_low_keys(trans, root, path, &disk_key, 1);
2976 src_nritems = btrfs_header_nritems(src);
2977 dst_nritems = btrfs_header_nritems(dst);
2982 if (!empty && src_nritems <= 8)
2985 if (push_items <= 0)
2989 push_items =
min(src_nritems, push_items);
2990 if (push_items < src_nritems) {
2994 if (src_nritems - push_items < 8) {
2995 if (push_items <= 8)
3001 push_items =
min(src_nritems - 8, push_items);
3003 tree_mod_log_eb_copy(root->
fs_info, dst, src, dst_nritems, 0,
3006 btrfs_node_key_ptr_offset(dst_nritems),
3007 btrfs_node_key_ptr_offset(0),
3010 if (push_items < src_nritems) {
3016 btrfs_node_key_ptr_offset(push_items),
3017 (src_nritems - push_items) *
3020 btrfs_set_header_nritems(src, src_nritems - push_items);
3021 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3051 src_nritems = btrfs_header_nritems(src);
3052 dst_nritems = btrfs_header_nritems(dst);
3054 if (push_items <= 0)
3057 if (src_nritems < 4)
3060 max_push = src_nritems / 2 + 1;
3062 if (max_push >= src_nritems)
3065 if (max_push < push_items)
3066 push_items = max_push;
3068 tree_mod_log_eb_move(root->
fs_info, dst, push_items, 0, dst_nritems);
3070 btrfs_node_key_ptr_offset(0),
3074 tree_mod_log_eb_copy(root->
fs_info, dst, src, 0,
3075 src_nritems - push_items, push_items);
3077 btrfs_node_key_ptr_offset(0),
3078 btrfs_node_key_ptr_offset(src_nritems - push_items),
3081 btrfs_set_header_nritems(src, src_nritems - push_items);
3082 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3110 lower = path->
nodes[level-1];
3112 btrfs_item_key(lower, &lower_key, 0);
3117 root->
root_key.objectid, &lower_key,
3118 level, root->
node->start, 0);
3122 root_add_used(root, root->
nodesize);
3125 btrfs_set_header_nritems(c, 1);
3126 btrfs_set_header_level(c, level);
3127 btrfs_set_header_bytenr(c, c->
start);
3128 btrfs_set_header_generation(c, trans->
transid);
3130 btrfs_set_header_owner(c, root->
root_key.objectid);
3133 (
unsigned long)btrfs_header_fsid(c),
3137 (
unsigned long)btrfs_header_chunk_tree_uuid(c),
3140 btrfs_set_node_key(c, &lower_key, 0);
3141 btrfs_set_node_blockptr(c, 0, lower->
start);
3142 lower_gen = btrfs_header_generation(lower);
3145 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3150 tree_mod_log_set_root_pointer(root, c);
3156 add_root_to_dirty_list(root);
3157 extent_buffer_get(c);
3174 int slot,
int level)
3183 nritems = btrfs_header_nritems(lower);
3186 if (slot != nritems) {
3188 tree_mod_log_eb_move(root->
fs_info, lower, slot + 1,
3189 slot, nritems - slot);
3191 btrfs_node_key_ptr_offset(slot + 1),
3192 btrfs_node_key_ptr_offset(slot),
3196 ret = tree_mod_log_insert_key(root->
fs_info, lower, slot,
3200 btrfs_set_node_key(lower, key, slot);
3201 btrfs_set_node_blockptr(lower, slot, bytenr);
3203 btrfs_set_node_ptr_generation(lower, slot, trans->
transid);
3204 btrfs_set_header_nritems(lower, nritems + 1);
3230 if (c == root->
node) {
3232 ret = insert_new_root(trans, root, path, level + 1);
3236 ret = push_nodes_for_insert(trans, root, path, level);
3238 if (!ret && btrfs_header_nritems(c) <
3245 c_nritems = btrfs_header_nritems(c);
3246 mid = (c_nritems + 1) / 2;
3251 &disk_key, level, c->
start, 0);
3253 return PTR_ERR(split);
3255 root_add_used(root, root->
nodesize);
3258 btrfs_set_header_level(split, btrfs_header_level(c));
3259 btrfs_set_header_bytenr(split, split->
start);
3260 btrfs_set_header_generation(split, trans->
transid);
3262 btrfs_set_header_owner(split, root->
root_key.objectid);
3264 (
unsigned long)btrfs_header_fsid(split),
3267 (
unsigned long)btrfs_header_chunk_tree_uuid(split),
3270 tree_mod_log_eb_copy(root->
fs_info, split, c, 0, mid, c_nritems - mid);
3272 btrfs_node_key_ptr_offset(0),
3273 btrfs_node_key_ptr_offset(mid),
3275 btrfs_set_header_nritems(split, c_nritems - mid);
3276 btrfs_set_header_nritems(c, mid);
3282 insert_ptr(trans, root, path, &disk_key, split->
start,
3283 path->
slots[level + 1] + 1, level + 1);
3285 if (path->
slots[level] >= mid) {
3290 path->
slots[level + 1] += 1;
3303 static int leaf_space_used(
struct extent_buffer *
l,
int start,
int nr)
3306 int nritems = btrfs_header_nritems(l);
3307 int end =
min(nritems, start + nr) - 1;
3311 data_len = btrfs_item_end_nr(l, start);
3312 data_len = data_len - btrfs_item_offset_nr(l, end);
3326 int nritems = btrfs_header_nritems(leaf);
3331 "used %d nritems %d\n",
3333 leaf_space_used(leaf, 0, nritems), nritems);
3347 int free_space,
u32 left_nritems,
3364 btrfs_init_map_token(&
token);
3371 if (path->
slots[0] >= left_nritems)
3374 slot = path->
slots[1];
3375 i = left_nritems - 1;
3377 item = btrfs_item_nr(left, i);
3379 if (!empty && push_items > 0) {
3380 if (path->
slots[0] > i)
3382 if (path->
slots[0] == i) {
3384 if (space + push_space * 2 > free_space)
3389 if (path->
slots[0] == i)
3392 this_item_size = btrfs_item_size(left, item);
3393 if (this_item_size +
sizeof(*item) + push_space > free_space)
3397 push_space += this_item_size +
sizeof(*item);
3403 if (push_items == 0)
3406 if (!empty && push_items == left_nritems)
3410 right_nritems = btrfs_header_nritems(right);
3412 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3413 push_space -= leaf_data_end(root, left);
3416 data_end = leaf_data_end(root, right);
3418 btrfs_leaf_data(right) + data_end - push_space,
3419 btrfs_leaf_data(right) + data_end,
3425 btrfs_leaf_data(left) + leaf_data_end(root, left),
3429 btrfs_item_nr_offset(0),
3434 btrfs_item_nr_offset(left_nritems - push_items),
3438 right_nritems += push_items;
3439 btrfs_set_header_nritems(right, right_nritems);
3441 for (i = 0; i < right_nritems; i++) {
3442 item = btrfs_item_nr(right, i);
3443 push_space -= btrfs_token_item_size(right, item, &
token);
3444 btrfs_set_token_item_offset(right, item, push_space, &
token);
3447 left_nritems -= push_items;
3448 btrfs_set_header_nritems(left, left_nritems);
3457 btrfs_item_key(right, &disk_key, 0);
3458 btrfs_set_node_key(upper, &disk_key, slot + 1);
3462 if (path->
slots[0] >= left_nritems) {
3463 path->
slots[0] -= left_nritems;
3464 if (btrfs_header_nritems(path->
nodes[0]) == 0)
3469 path->
slots[1] += 1;
3494 int min_data_size,
int data_size,
3495 int empty,
u32 min_slot)
3505 if (!path->
nodes[1])
3508 slot = path->
slots[1];
3509 upper = path->
nodes[1];
3510 if (slot >= btrfs_header_nritems(upper) - 1)
3515 right = read_node_slot(root, upper, slot + 1);
3520 btrfs_set_lock_blocking(right);
3523 if (free_space < data_size)
3533 if (free_space < data_size)
3536 left_nritems = btrfs_header_nritems(left);
3537 if (left_nritems == 0)
3540 return __push_leaf_right(trans, root, path, min_data_size, empty,
3541 right, free_space, left_nritems, min_slot);
3560 int free_space,
u32 right_nritems,
3569 u32 old_left_nritems;
3573 u32 old_left_item_size;
3576 btrfs_init_map_token(&
token);
3579 nr =
min(right_nritems, max_slot);
3581 nr =
min(right_nritems - 1, max_slot);
3583 for (i = 0; i <
nr; i++) {
3584 item = btrfs_item_nr(right, i);
3586 if (!empty && push_items > 0) {
3587 if (path->
slots[0] < i)
3589 if (path->
slots[0] == i) {
3591 if (space + push_space * 2 > free_space)
3596 if (path->
slots[0] == i)
3599 this_item_size = btrfs_item_size(right, item);
3600 if (this_item_size +
sizeof(*item) + push_space > free_space)
3604 push_space += this_item_size +
sizeof(*item);
3607 if (push_items == 0) {
3611 if (!empty && push_items == btrfs_header_nritems(right))
3616 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3617 btrfs_item_nr_offset(0),
3621 btrfs_item_offset_nr(right, push_items - 1);
3624 leaf_data_end(root, left) - push_space,
3625 btrfs_leaf_data(right) +
3626 btrfs_item_offset_nr(right, push_items - 1),
3628 old_left_nritems = btrfs_header_nritems(left);
3629 BUG_ON(old_left_nritems <= 0);
3631 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3632 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3635 item = btrfs_item_nr(left, i);
3637 ioff = btrfs_token_item_offset(left, item, &
token);
3638 btrfs_set_token_item_offset(left, item,
3642 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3645 if (push_items > right_nritems) {
3651 if (push_items < right_nritems) {
3652 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3653 leaf_data_end(root, right);
3656 btrfs_leaf_data(right) +
3657 leaf_data_end(root, right), push_space);
3660 btrfs_item_nr_offset(push_items),
3661 (btrfs_header_nritems(right) - push_items) *
3664 right_nritems -= push_items;
3665 btrfs_set_header_nritems(right, right_nritems);
3667 for (i = 0; i < right_nritems; i++) {
3668 item = btrfs_item_nr(right, i);
3670 push_space = push_space - btrfs_token_item_size(right,
3672 btrfs_set_token_item_offset(right, item, push_space, &
token);
3681 btrfs_item_key(right, &disk_key, 0);
3682 fixup_low_keys(trans, root, path, &disk_key, 1);
3685 if (path->
slots[0] < push_items) {
3686 path->
slots[0] += old_left_nritems;
3690 path->
slots[1] -= 1;
3694 path->
slots[0] -= push_items;
3713 *root,
struct btrfs_path *path,
int min_data_size,
3714 int data_size,
int empty,
u32 max_slot)
3723 slot = path->
slots[1];
3726 if (!path->
nodes[1])
3729 right_nritems = btrfs_header_nritems(right);
3730 if (right_nritems == 0)
3735 left = read_node_slot(root, path->
nodes[1], slot - 1);
3740 btrfs_set_lock_blocking(left);
3743 if (free_space < data_size) {
3750 path->
nodes[1], slot - 1, &left);
3759 if (free_space < data_size) {
3764 return __push_leaf_left(trans, root, path, min_data_size,
3765 empty, left, free_space, right_nritems,
3782 int slot,
int mid,
int nritems)
3790 btrfs_init_map_token(&
token);
3792 nritems = nritems -
mid;
3793 btrfs_set_header_nritems(right, nritems);
3794 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3797 btrfs_item_nr_offset(mid),
3802 data_copy_size, btrfs_leaf_data(l) +
3803 leaf_data_end(root, l), data_copy_size);
3806 btrfs_item_end_nr(l, mid);
3808 for (i = 0; i <
nritems; i++) {
3809 struct btrfs_item *item = btrfs_item_nr(right, i);
3812 ioff = btrfs_token_item_offset(right, item, &
token);
3813 btrfs_set_token_item_offset(right, item,
3814 ioff + rt_data_off, &
token);
3817 btrfs_set_header_nritems(l, mid);
3818 btrfs_item_key(right, &disk_key, 0);
3819 insert_ptr(trans, root, path, &disk_key, right->
start,
3820 path->
slots[1] + 1, 1);
3831 path->
slots[1] += 1;
3860 slot = path->
slots[0];
3866 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3873 nritems = btrfs_header_nritems(path->
nodes[0]);
3878 if (path->
slots[0] == 0 || path->
slots[0] == nritems)
3885 slot = path->
slots[0];
3886 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3919 int num_doubles = 0;
3920 int tried_avoid_double = 0;
3923 slot = path->
slots[0];
3924 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3930 wret = push_leaf_right(trans, root, path, data_size,
3935 wret = push_leaf_left(trans, root, path, data_size,
3936 data_size, 0, (
u32)-1);
3947 if (!path->
nodes[1]) {
3948 ret = insert_new_root(trans, root, path, 1);
3955 slot = path->
slots[0];
3956 nritems = btrfs_header_nritems(l);
3957 mid = (nritems + 1) / 2;
3961 leaf_space_used(l, mid, nritems - mid) + data_size >
3963 if (slot >= nritems) {
3967 if (mid != nritems &&
3968 leaf_space_used(l, mid, nritems - mid) +
3970 if (data_size && !tried_avoid_double)
3971 goto push_for_double;
3977 if (leaf_space_used(l, 0, mid) + data_size >
3979 if (!extend && data_size && slot == 0) {
3981 }
else if ((extend || !data_size) && slot == 0) {
3985 if (mid != nritems &&
3986 leaf_space_used(l, mid, nritems - mid) +
3988 if (data_size && !tried_avoid_double)
3989 goto push_for_double;
3997 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3999 btrfs_item_key(l, &disk_key, mid);
4003 &disk_key, 0, l->
start, 0);
4005 return PTR_ERR(right);
4007 root_add_used(root, root->
leafsize);
4010 btrfs_set_header_bytenr(right, right->
start);
4011 btrfs_set_header_generation(right, trans->
transid);
4013 btrfs_set_header_owner(right, root->
root_key.objectid);
4014 btrfs_set_header_level(right, 0);
4016 (
unsigned long)btrfs_header_fsid(right),
4020 (
unsigned long)btrfs_header_chunk_tree_uuid(right),
4025 btrfs_set_header_nritems(right, 0);
4026 insert_ptr(trans, root, path, &disk_key, right->
start,
4027 path->
slots[1] + 1, 1);
4032 path->
slots[1] += 1;
4034 btrfs_set_header_nritems(right, 0);
4035 insert_ptr(trans, root, path, &disk_key, right->
start,
4041 if (path->
slots[1] == 0)
4042 fixup_low_keys(trans, root, path,
4049 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
4052 BUG_ON(num_doubles != 0);
4060 push_for_double_split(trans, root, path, data_size);
4061 tried_avoid_double = 1;
4078 leaf = path->
nodes[0];
4079 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0]);
4087 item_size = btrfs_item_size_nr(leaf, path->
slots[0]);
4091 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4103 leaf = path->
nodes[0];
4105 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->
slots[0]))
4115 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4120 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4136 unsigned long split_offset)
4148 leaf = path->
nodes[0];
4153 item = btrfs_item_nr(leaf, path->
slots[0]);
4154 orig_offset = btrfs_item_offset(leaf, item);
4155 item_size = btrfs_item_size(leaf, item);
4162 path->
slots[0]), item_size);
4164 slot = path->
slots[0] + 1;
4165 nritems = btrfs_header_nritems(leaf);
4166 if (slot != nritems) {
4169 btrfs_item_nr_offset(slot),
4170 (nritems - slot) *
sizeof(
struct btrfs_item));
4173 btrfs_cpu_key_to_disk(&disk_key, new_key);
4174 btrfs_set_item_key(leaf, &disk_key, slot);
4176 new_item = btrfs_item_nr(leaf, slot);
4178 btrfs_set_item_offset(leaf, new_item, orig_offset);
4179 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4181 btrfs_set_item_offset(leaf, item,
4182 orig_offset + item_size - split_offset);
4183 btrfs_set_item_size(leaf, item, split_offset);
4185 btrfs_set_header_nritems(leaf, nritems + 1);
4195 item_size - split_offset);
4222 unsigned long split_offset)
4225 ret = setup_leaf_for_split(trans, root, path,
4230 ret = split_item(trans, root, path, new_key, split_offset);
4251 leaf = path->
nodes[0];
4252 item_size = btrfs_item_size_nr(leaf, path->
slots[0]);
4253 ret = setup_leaf_for_split(trans, root, path,
4260 item_size, item_size +
4262 leaf = path->
nodes[0];
4279 u32 new_size,
int from_end)
4285 unsigned int data_end;
4286 unsigned int old_data_start;
4288 unsigned int size_diff;
4292 btrfs_init_map_token(&token);
4294 leaf = path->
nodes[0];
4295 slot = path->
slots[0];
4297 old_size = btrfs_item_size_nr(leaf, slot);
4298 if (old_size == new_size)
4301 nritems = btrfs_header_nritems(leaf);
4302 data_end = leaf_data_end(root, leaf);
4304 old_data_start = btrfs_item_offset_nr(leaf, slot);
4306 size_diff = old_size - new_size;
4315 for (i = slot; i <
nritems; i++) {
4317 item = btrfs_item_nr(leaf, i);
4319 ioff = btrfs_token_item_offset(leaf, item, &token);
4320 btrfs_set_token_item_offset(leaf, item,
4321 ioff + size_diff, &token);
4327 data_end + size_diff, btrfs_leaf_data(leaf) +
4328 data_end, old_data_start + new_size - data_end);
4333 btrfs_item_key(leaf, &disk_key, slot);
4342 (
unsigned long)fi - size_diff);
4344 if (btrfs_file_extent_type(leaf, fi) ==
4355 data_end + size_diff, btrfs_leaf_data(leaf) +
4356 data_end, old_data_start - data_end);
4358 offset = btrfs_disk_key_offset(&disk_key);
4359 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4360 btrfs_set_item_key(leaf, &disk_key, slot);
4362 fixup_low_keys(trans, root, path, &disk_key, 1);
4365 item = btrfs_item_nr(leaf, slot);
4366 btrfs_set_item_size(leaf, item, new_size);
4386 unsigned int data_end;
4387 unsigned int old_data;
4392 btrfs_init_map_token(&token);
4394 leaf = path->
nodes[0];
4396 nritems = btrfs_header_nritems(leaf);
4397 data_end = leaf_data_end(root, leaf);
4403 slot = path->
slots[0];
4404 old_data = btrfs_item_end_nr(leaf, slot);
4407 if (slot >= nritems) {
4418 for (i = slot; i <
nritems; i++) {
4420 item = btrfs_item_nr(leaf, i);
4422 ioff = btrfs_token_item_offset(leaf, item, &token);
4423 btrfs_set_token_item_offset(leaf, item,
4424 ioff - data_size, &token);
4429 data_end - data_size, btrfs_leaf_data(leaf) +
4430 data_end, old_data - data_end);
4432 data_end = old_data;
4433 old_size = btrfs_item_size_nr(leaf, slot);
4434 item = btrfs_item_nr(leaf, slot);
4435 btrfs_set_item_size(leaf, item, old_size + data_size);
4457 unsigned int data_end;
4463 btrfs_init_map_token(&token);
4465 leaf = path->
nodes[0];
4466 slot = path->
slots[0];
4468 nritems = btrfs_header_nritems(leaf);
4469 data_end = leaf_data_end(root, leaf);
4478 if (slot != nritems) {
4479 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4481 if (old_data < data_end) {
4484 slot, old_data, data_end);
4491 for (i = slot; i <
nritems; i++) {
4494 item = btrfs_item_nr(leaf, i);
4495 ioff = btrfs_token_item_offset(leaf, item, &token);
4496 btrfs_set_token_item_offset(leaf, item,
4497 ioff - total_data, &token);
4501 btrfs_item_nr_offset(slot),
4502 (nritems - slot) *
sizeof(
struct btrfs_item));
4506 data_end - total_data, btrfs_leaf_data(leaf) +
4507 data_end, old_data - data_end);
4508 data_end = old_data;
4512 for (i = 0; i <
nr; i++) {
4513 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4514 btrfs_set_item_key(leaf, &disk_key, slot + i);
4515 item = btrfs_item_nr(leaf, slot + i);
4516 btrfs_set_token_item_offset(leaf, item,
4517 data_end - data_size[i], &token);
4518 data_end -= data_size[
i];
4519 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4522 btrfs_set_header_nritems(leaf, nritems + nr);
4525 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4526 fixup_low_keys(trans, root, path, &disk_key, 1);
4553 for (i = 0; i <
nr; i++)
4554 total_data += data_size[i];
4556 total_size = total_data + (nr *
sizeof(
struct btrfs_item));
4563 slot = path->
slots[0];
4567 total_data, total_size, nr);
4587 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4589 leaf = path->
nodes[0];
4605 struct btrfs_path *path,
int level,
int slot,
4612 nritems = btrfs_header_nritems(parent);
4613 if (slot != nritems - 1) {
4614 if (tree_mod_log && level)
4615 tree_mod_log_eb_move(root->
fs_info, parent, slot,
4616 slot + 1, nritems - slot - 1);
4618 btrfs_node_key_ptr_offset(slot),
4619 btrfs_node_key_ptr_offset(slot + 1),
4621 (nritems - slot - 1));
4622 }
else if (tree_mod_log && level) {
4623 ret = tree_mod_log_insert_key(root->
fs_info, parent, slot,
4629 btrfs_set_header_nritems(parent, nritems);
4630 if (nritems == 0 && parent == root->
node) {
4633 btrfs_set_header_level(root->
node, 0);
4634 }
else if (slot == 0) {
4638 fixup_low_keys(trans, root, path, &disk_key, level + 1);
4659 del_ptr(trans, root, path, 1, path->
slots[1], 1);
4667 root_sub_used(root, leaf->
len);
4669 extent_buffer_get(leaf);
4690 btrfs_init_map_token(&token);
4692 leaf = path->
nodes[0];
4693 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4695 for (i = 0; i <
nr; i++)
4696 dsize += btrfs_item_size_nr(leaf, slot + i);
4698 nritems = btrfs_header_nritems(leaf);
4700 if (slot + nr != nritems) {
4701 int data_end = leaf_data_end(root, leaf);
4705 btrfs_leaf_data(leaf) + data_end,
4706 last_off - data_end);
4708 for (i = slot + nr; i <
nritems; i++) {
4711 item = btrfs_item_nr(leaf, i);
4712 ioff = btrfs_token_item_offset(leaf, item, &token);
4713 btrfs_set_token_item_offset(leaf, item,
4714 ioff + dsize, &token);
4718 btrfs_item_nr_offset(slot + nr),
4720 (nritems - slot - nr));
4722 btrfs_set_header_nritems(leaf, nritems - nr);
4727 if (leaf == root->
node) {
4728 btrfs_set_header_level(leaf, 0);
4732 btrfs_del_leaf(trans, root, path, leaf);
4735 int used = leaf_space_used(leaf, 0, nritems);
4739 btrfs_item_key(leaf, &disk_key, 0);
4740 fixup_low_keys(trans, root, path, &disk_key, 1);
4749 slot = path->
slots[1];
4750 extent_buffer_get(leaf);
4753 wret = push_leaf_left(trans, root, path, 1, 1,
4755 if (wret < 0 && wret != -
ENOSPC)
4758 if (path->
nodes[0] == leaf &&
4759 btrfs_header_nritems(leaf)) {
4760 wret = push_leaf_right(trans, root, path, 1,
4762 if (wret < 0 && wret != -
ENOSPC)
4766 if (btrfs_header_nritems(leaf) == 0) {
4768 btrfs_del_leaf(trans, root, path, leaf);
4777 if (path->
nodes[0] == leaf)
4802 btrfs_item_key_to_cpu(path->
nodes[0], &key, 0);
4806 else if (key.
type > 0)
4817 btrfs_item_key(path->
nodes[0], &found_key, 0);
4818 ret = comp_keys(&found_key, &key);
4862 level = btrfs_header_level(cur);
4867 if (btrfs_header_generation(cur) < min_trans) {
4872 nritems = btrfs_header_nritems(cur);
4873 level = btrfs_header_level(cur);
4874 sret = bin_search(cur, min_key, level, &slot);
4878 if (slot >= nritems)
4882 btrfs_item_key_to_cpu(cur, &found_key, slot);
4885 if (sret && slot > 0)
4892 while (slot < nritems) {
4898 blockptr = btrfs_node_blockptr(cur, slot);
4899 gen = btrfs_node_ptr_generation(cur, slot);
4900 if (gen < min_trans) {
4909 if (comp_keys(&disk_key, max_key) >= 0) {
4916 btrfs_level_size(root, level - 1));
4931 if (slot >= nritems) {
4935 cache_only, min_trans);
4944 btrfs_node_key_to_cpu(cur, &found_key, slot);
4948 unlock_up(path, level, 1, 0,
NULL);
4952 cur = read_node_slot(root, cur, slot);
4959 unlock_up(path, level, 1, 0,
NULL);
4964 memcpy(min_key, &found_key,
sizeof(found_key));
4969 static void tree_move_down(
struct btrfs_root *root,
4974 path->
nodes[*level - 1] = read_node_slot(root, path->
nodes[*level],
4975 path->
slots[*level]);
4976 path->
slots[*level - 1] = 0;
4980 static int tree_move_next_or_upnext(
struct btrfs_root *root,
4986 nritems = btrfs_header_nritems(path->
nodes[*level]);
4990 while (path->
slots[*level] >= nritems) {
4991 if (*level == root_level)
5001 nritems = btrfs_header_nritems(path->
nodes[*level]);
5011 static int tree_advance(
struct btrfs_root *root,
5013 int *level,
int root_level,
5019 if (*level == 0 || !allow_down) {
5020 ret = tree_move_next_or_upnext(root, path, level, root_level);
5022 tree_move_down(root, path, level, root_level);
5027 btrfs_item_key_to_cpu(path->
nodes[*level], key,
5028 path->
slots[*level]);
5030 btrfs_node_key_to_cpu(path->
nodes[*level], key,
5031 path->
slots[*level]);
5036 static int tree_compare_item(
struct btrfs_root *left_root,
5043 unsigned long off1, off2;
5045 len1 = btrfs_item_size_nr(left_path->
nodes[0], left_path->
slots[0]);
5046 len2 = btrfs_item_size_nr(right_path->
nodes[0], right_path->
slots[0]);
5052 right_path->
slots[0]);
5063 #define ADVANCE_ONLY_NEXT -1
5089 char *tmp_buf =
NULL;
5090 int left_root_level;
5091 int right_root_level;
5094 int left_end_reached;
5095 int right_end_reached;
5100 u64 left_start_ctransid;
5101 u64 right_start_ctransid;
5127 left_start_ctransid = btrfs_root_ctransid(&left_root->
root_item);
5131 right_start_ctransid = btrfs_root_ctransid(&right_root->
root_item);
5135 if (IS_ERR(trans)) {
5136 ret = PTR_ERR(trans);
5177 left_level = btrfs_header_level(left_root->
commit_root);
5178 left_root_level = left_level;
5180 extent_buffer_get(left_path->
nodes[left_level]);
5182 right_level = btrfs_header_level(right_root->
commit_root);
5183 right_root_level = right_level;
5185 extent_buffer_get(right_path->
nodes[right_level]);
5187 if (left_level == 0)
5188 btrfs_item_key_to_cpu(left_path->
nodes[left_level],
5189 &left_key, left_path->
slots[left_level]);
5191 btrfs_node_key_to_cpu(left_path->
nodes[left_level],
5192 &left_key, left_path->
slots[left_level]);
5193 if (right_level == 0)
5194 btrfs_item_key_to_cpu(right_path->
nodes[right_level],
5195 &right_key, right_path->
slots[right_level]);
5197 btrfs_node_key_to_cpu(right_path->
nodes[right_level],
5198 &right_key, right_path->
slots[right_level]);
5200 left_end_reached = right_end_reached = 0;
5201 advance_left = advance_right = 0;
5221 if (IS_ERR(trans)) {
5222 ret = PTR_ERR(trans);
5228 ctransid = btrfs_root_ctransid(&left_root->
root_item);
5230 if (ctransid != left_start_ctransid)
5231 left_start_ctransid = 0;
5234 ctransid = btrfs_root_ctransid(&right_root->
root_item);
5236 if (ctransid != right_start_ctransid)
5237 right_start_ctransid = 0;
5239 if (!left_start_ctransid || !right_start_ctransid) {
5241 "btrfs: btrfs_compare_tree detected "
5242 "a change in one of the trees while "
5243 "iterating. This is probably a "
5256 &left_key, left_path, 0, 0);
5260 &right_key, right_path, 0, 0);
5265 if (advance_left && !left_end_reached) {
5266 ret = tree_advance(left_root, left_path, &left_level,
5274 if (advance_right && !right_end_reached) {
5275 ret = tree_advance(right_root, right_path, &right_level,
5284 if (left_end_reached && right_end_reached) {
5287 }
else if (left_end_reached) {
5288 if (right_level == 0) {
5289 ret = changed_cb(left_root, right_root,
5290 left_path, right_path,
5299 }
else if (right_end_reached) {
5300 if (left_level == 0) {
5301 ret = changed_cb(left_root, right_root,
5302 left_path, right_path,
5313 if (left_level == 0 && right_level == 0) {
5316 ret = changed_cb(left_root, right_root,
5317 left_path, right_path,
5324 }
else if (cmp > 0) {
5325 ret = changed_cb(left_root, right_root,
5326 left_path, right_path,
5335 ret = tree_compare_item(left_root, left_path,
5336 right_path, tmp_buf);
5339 ret = changed_cb(left_root, right_root,
5340 left_path, right_path,
5350 }
else if (left_level == right_level) {
5354 }
else if (cmp > 0) {
5357 left_blockptr = btrfs_node_blockptr(
5358 left_path->
nodes[left_level],
5359 left_path->
slots[left_level]);
5360 right_blockptr = btrfs_node_blockptr(
5361 right_path->
nodes[right_level],
5362 right_path->
slots[right_level]);
5363 if (left_blockptr == right_blockptr) {
5375 }
else if (left_level < right_level) {
5411 int cache_only,
u64 min_trans)
5417 while (level < BTRFS_MAX_LEVEL) {
5418 if (!path->
nodes[level])
5424 if (slot >= btrfs_header_nritems(c)) {
5428 if (level + 1 >= BTRFS_MAX_LEVEL ||
5429 !path->
nodes[level + 1])
5432 if (path->
locks[level + 1]) {
5437 slot = btrfs_header_nritems(c) - 1;
5439 btrfs_item_key_to_cpu(c, &cur_key, slot);
5441 btrfs_node_key_to_cpu(c, &cur_key, slot);
5460 btrfs_item_key_to_cpu(c, key, slot);
5463 u64 gen = btrfs_node_ptr_generation(c, slot);
5468 btrfs_level_size(root, level - 1));
5478 if (gen < min_trans) {
5482 btrfs_node_key_to_cpu(c, key, slot);
5510 int next_rw_lock = 0;
5512 nritems = btrfs_header_nritems(path->
nodes[0]);
5516 btrfs_item_key_to_cpu(path->
nodes[0], &key, nritems - 1);
5535 nritems = btrfs_header_nritems(path->
nodes[0]);
5542 if (nritems > 0 && path->
slots[0] < nritems - 1) {
5549 while (level < BTRFS_MAX_LEVEL) {
5550 if (!path->
nodes[level]) {
5557 if (slot >= btrfs_header_nritems(c)) {
5559 if (level == BTRFS_MAX_LEVEL) {
5567 btrfs_tree_unlock_rw(next, next_rw_lock);
5573 ret = read_block_for_search(
NULL, root, path, &next, level,
5585 if (!ret && time_seq) {
5612 if (path->
locks[level])
5613 btrfs_tree_unlock_rw(c, path->
locks[level]);
5623 ret = read_block_for_search(
NULL, root, path, &next, level,
5646 unlock_up(path, 0, 1, 0,
NULL);
5670 if (path->
slots[0] == 0) {
5678 leaf = path->
nodes[0];
5679 nritems = btrfs_header_nritems(leaf);
5682 if (path->
slots[0] == nritems)
5685 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
5686 if (found_key.
objectid < min_objectid)
5688 if (found_key.
type == type)
5690 if (found_key.
objectid == min_objectid &&
5691 found_key.
type < type)