mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / fs / btrfs / ctree.c
blob7ff0eb63e6191705b6c731f5e28f209a6d5aa0dc
1 /*
2 * Copyright (C) 2007,2008 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.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/rbtree.h>
22 #include <linux/mm.h>
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26 #include "print-tree.h"
27 #include "locking.h"
29 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
30 *root, struct btrfs_path *path, int level);
31 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
32 const struct btrfs_key *ins_key, struct btrfs_path *path,
33 int data_size, int extend);
34 static int push_node_left(struct btrfs_trans_handle *trans,
35 struct btrfs_fs_info *fs_info,
36 struct extent_buffer *dst,
37 struct extent_buffer *src, int empty);
38 static int balance_node_right(struct btrfs_trans_handle *trans,
39 struct btrfs_fs_info *fs_info,
40 struct extent_buffer *dst_buf,
41 struct extent_buffer *src_buf);
42 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
43 int level, int slot);
44 static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
45 struct extent_buffer *eb);
47 struct btrfs_path *btrfs_alloc_path(void)
49 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
53 * set all locked nodes in the path to blocking locks. This should
54 * be done before scheduling
56 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
58 int i;
59 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
60 if (!p->nodes[i] || !p->locks[i])
61 continue;
62 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
63 if (p->locks[i] == BTRFS_READ_LOCK)
64 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
65 else if (p->locks[i] == BTRFS_WRITE_LOCK)
66 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
71 * reset all the locked nodes in the patch to spinning locks.
73 * held is used to keep lockdep happy, when lockdep is enabled
74 * we set held to a blocking lock before we go around and
75 * retake all the spinlocks in the path. You can safely use NULL
76 * for held
78 noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
79 struct extent_buffer *held, int held_rw)
81 int i;
83 if (held) {
84 btrfs_set_lock_blocking_rw(held, held_rw);
85 if (held_rw == BTRFS_WRITE_LOCK)
86 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
87 else if (held_rw == BTRFS_READ_LOCK)
88 held_rw = BTRFS_READ_LOCK_BLOCKING;
90 btrfs_set_path_blocking(p);
92 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
93 if (p->nodes[i] && p->locks[i]) {
94 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
95 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
96 p->locks[i] = BTRFS_WRITE_LOCK;
97 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
98 p->locks[i] = BTRFS_READ_LOCK;
102 if (held)
103 btrfs_clear_lock_blocking_rw(held, held_rw);
106 /* this also releases the path */
107 void btrfs_free_path(struct btrfs_path *p)
109 if (!p)
110 return;
111 btrfs_release_path(p);
112 kmem_cache_free(btrfs_path_cachep, p);
116 * path release drops references on the extent buffers in the path
117 * and it drops any locks held by this path
119 * It is safe to call this on paths that no locks or extent buffers held.
121 noinline void btrfs_release_path(struct btrfs_path *p)
123 int i;
125 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
126 p->slots[i] = 0;
127 if (!p->nodes[i])
128 continue;
129 if (p->locks[i]) {
130 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
131 p->locks[i] = 0;
133 free_extent_buffer(p->nodes[i]);
134 p->nodes[i] = NULL;
139 * safely gets a reference on the root node of a tree. A lock
140 * is not taken, so a concurrent writer may put a different node
141 * at the root of the tree. See btrfs_lock_root_node for the
142 * looping required.
144 * The extent buffer returned by this has a reference taken, so
145 * it won't disappear. It may stop being the root of the tree
146 * at any time because there are no locks held.
148 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
150 struct extent_buffer *eb;
152 while (1) {
153 rcu_read_lock();
154 eb = rcu_dereference(root->node);
157 * RCU really hurts here, we could free up the root node because
158 * it was COWed but we may not get the new root node yet so do
159 * the inc_not_zero dance and if it doesn't work then
160 * synchronize_rcu and try again.
162 if (atomic_inc_not_zero(&eb->refs)) {
163 rcu_read_unlock();
164 break;
166 rcu_read_unlock();
167 synchronize_rcu();
169 return eb;
172 /* loop around taking references on and locking the root node of the
173 * tree until you end up with a lock on the root. A locked buffer
174 * is returned, with a reference held.
176 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
178 struct extent_buffer *eb;
180 while (1) {
181 eb = btrfs_root_node(root);
182 btrfs_tree_lock(eb);
183 if (eb == root->node)
184 break;
185 btrfs_tree_unlock(eb);
186 free_extent_buffer(eb);
188 return eb;
191 /* loop around taking references on and locking the root node of the
192 * tree until you end up with a lock on the root. A locked buffer
193 * is returned, with a reference held.
195 static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
197 struct extent_buffer *eb;
199 while (1) {
200 eb = btrfs_root_node(root);
201 btrfs_tree_read_lock(eb);
202 if (eb == root->node)
203 break;
204 btrfs_tree_read_unlock(eb);
205 free_extent_buffer(eb);
207 return eb;
210 /* cowonly root (everything not a reference counted cow subvolume), just get
211 * put onto a simple dirty list. transaction.c walks this to make sure they
212 * get properly updated on disk.
214 static void add_root_to_dirty_list(struct btrfs_root *root)
216 struct btrfs_fs_info *fs_info = root->fs_info;
218 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
219 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
220 return;
222 spin_lock(&fs_info->trans_lock);
223 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
224 /* Want the extent tree to be the last on the list */
225 if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
226 list_move_tail(&root->dirty_list,
227 &fs_info->dirty_cowonly_roots);
228 else
229 list_move(&root->dirty_list,
230 &fs_info->dirty_cowonly_roots);
232 spin_unlock(&fs_info->trans_lock);
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
240 int btrfs_copy_root(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *buf,
243 struct extent_buffer **cow_ret, u64 new_root_objectid)
245 struct btrfs_fs_info *fs_info = root->fs_info;
246 struct extent_buffer *cow;
247 int ret = 0;
248 int level;
249 struct btrfs_disk_key disk_key;
251 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
252 trans->transid != fs_info->running_transaction->transid);
253 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
254 trans->transid != root->last_trans);
256 level = btrfs_header_level(buf);
257 if (level == 0)
258 btrfs_item_key(buf, &disk_key, 0);
259 else
260 btrfs_node_key(buf, &disk_key, 0);
262 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
263 &disk_key, level, buf->start, 0);
264 if (IS_ERR(cow))
265 return PTR_ERR(cow);
267 copy_extent_buffer_full(cow, buf);
268 btrfs_set_header_bytenr(cow, cow->start);
269 btrfs_set_header_generation(cow, trans->transid);
270 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
271 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
272 BTRFS_HEADER_FLAG_RELOC);
273 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
274 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
275 else
276 btrfs_set_header_owner(cow, new_root_objectid);
278 write_extent_buffer_fsid(cow, fs_info->fsid);
280 WARN_ON(btrfs_header_generation(buf) > trans->transid);
281 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
282 ret = btrfs_inc_ref(trans, root, cow, 1);
283 else
284 ret = btrfs_inc_ref(trans, root, cow, 0);
286 if (ret)
287 return ret;
289 btrfs_mark_buffer_dirty(cow);
290 *cow_ret = cow;
291 return 0;
294 enum mod_log_op {
295 MOD_LOG_KEY_REPLACE,
296 MOD_LOG_KEY_ADD,
297 MOD_LOG_KEY_REMOVE,
298 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
299 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
300 MOD_LOG_MOVE_KEYS,
301 MOD_LOG_ROOT_REPLACE,
304 struct tree_mod_move {
305 int dst_slot;
306 int nr_items;
309 struct tree_mod_root {
310 u64 logical;
311 u8 level;
314 struct tree_mod_elem {
315 struct rb_node node;
316 u64 logical;
317 u64 seq;
318 enum mod_log_op op;
320 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
321 int slot;
323 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
324 u64 generation;
326 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
327 struct btrfs_disk_key key;
328 u64 blockptr;
330 /* this is used for op == MOD_LOG_MOVE_KEYS */
331 struct tree_mod_move move;
333 /* this is used for op == MOD_LOG_ROOT_REPLACE */
334 struct tree_mod_root old_root;
338 * Pull a new tree mod seq number for our operation.
340 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
342 return atomic64_inc_return(&fs_info->tree_mod_seq);
346 * This adds a new blocker to the tree mod log's blocker list if the @elem
347 * passed does not already have a sequence number set. So when a caller expects
348 * to record tree modifications, it should ensure to set elem->seq to zero
349 * before calling btrfs_get_tree_mod_seq.
350 * Returns a fresh, unused tree log modification sequence number, even if no new
351 * blocker was added.
353 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
354 struct seq_list *elem)
356 write_lock(&fs_info->tree_mod_log_lock);
357 if (!elem->seq) {
358 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
359 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
361 write_unlock(&fs_info->tree_mod_log_lock);
363 return elem->seq;
366 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
367 struct seq_list *elem)
369 struct rb_root *tm_root;
370 struct rb_node *node;
371 struct rb_node *next;
372 struct seq_list *cur_elem;
373 struct tree_mod_elem *tm;
374 u64 min_seq = (u64)-1;
375 u64 seq_putting = elem->seq;
377 if (!seq_putting)
378 return;
380 write_lock(&fs_info->tree_mod_log_lock);
381 list_del(&elem->list);
382 elem->seq = 0;
384 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
385 if (cur_elem->seq < min_seq) {
386 if (seq_putting > cur_elem->seq) {
388 * blocker with lower sequence number exists, we
389 * cannot remove anything from the log
391 write_unlock(&fs_info->tree_mod_log_lock);
392 return;
394 min_seq = cur_elem->seq;
399 * anything that's lower than the lowest existing (read: blocked)
400 * sequence number can be removed from the tree.
402 tm_root = &fs_info->tree_mod_log;
403 for (node = rb_first(tm_root); node; node = next) {
404 next = rb_next(node);
405 tm = rb_entry(node, struct tree_mod_elem, node);
406 if (tm->seq >= min_seq)
407 continue;
408 rb_erase(node, tm_root);
409 kfree(tm);
411 write_unlock(&fs_info->tree_mod_log_lock);
415 * key order of the log:
416 * node/leaf start address -> sequence
418 * The 'start address' is the logical address of the *new* root node
419 * for root replace operations, or the logical address of the affected
420 * block for all other operations.
422 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
424 static noinline int
425 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
427 struct rb_root *tm_root;
428 struct rb_node **new;
429 struct rb_node *parent = NULL;
430 struct tree_mod_elem *cur;
432 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
434 tm_root = &fs_info->tree_mod_log;
435 new = &tm_root->rb_node;
436 while (*new) {
437 cur = rb_entry(*new, struct tree_mod_elem, node);
438 parent = *new;
439 if (cur->logical < tm->logical)
440 new = &((*new)->rb_left);
441 else if (cur->logical > tm->logical)
442 new = &((*new)->rb_right);
443 else if (cur->seq < tm->seq)
444 new = &((*new)->rb_left);
445 else if (cur->seq > tm->seq)
446 new = &((*new)->rb_right);
447 else
448 return -EEXIST;
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
453 return 0;
457 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
458 * returns zero with the tree_mod_log_lock acquired. The caller must hold
459 * this until all tree mod log insertions are recorded in the rb tree and then
460 * write unlock fs_info::tree_mod_log_lock.
462 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
463 struct extent_buffer *eb) {
464 smp_mb();
465 if (list_empty(&(fs_info)->tree_mod_seq_list))
466 return 1;
467 if (eb && btrfs_header_level(eb) == 0)
468 return 1;
470 write_lock(&fs_info->tree_mod_log_lock);
471 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
472 write_unlock(&fs_info->tree_mod_log_lock);
473 return 1;
476 return 0;
479 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
480 static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
481 struct extent_buffer *eb)
483 smp_mb();
484 if (list_empty(&(fs_info)->tree_mod_seq_list))
485 return 0;
486 if (eb && btrfs_header_level(eb) == 0)
487 return 0;
489 return 1;
492 static struct tree_mod_elem *
493 alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
494 enum mod_log_op op, gfp_t flags)
496 struct tree_mod_elem *tm;
498 tm = kzalloc(sizeof(*tm), flags);
499 if (!tm)
500 return NULL;
502 tm->logical = eb->start;
503 if (op != MOD_LOG_KEY_ADD) {
504 btrfs_node_key(eb, &tm->key, slot);
505 tm->blockptr = btrfs_node_blockptr(eb, slot);
507 tm->op = op;
508 tm->slot = slot;
509 tm->generation = btrfs_node_ptr_generation(eb, slot);
510 RB_CLEAR_NODE(&tm->node);
512 return tm;
515 static noinline int
516 tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
517 struct extent_buffer *eb, int slot,
518 enum mod_log_op op, gfp_t flags)
520 struct tree_mod_elem *tm;
521 int ret;
523 if (!tree_mod_need_log(fs_info, eb))
524 return 0;
526 tm = alloc_tree_mod_elem(eb, slot, op, flags);
527 if (!tm)
528 return -ENOMEM;
530 if (tree_mod_dont_log(fs_info, eb)) {
531 kfree(tm);
532 return 0;
535 ret = __tree_mod_log_insert(fs_info, tm);
536 write_unlock(&eb->fs_info->tree_mod_log_lock);
537 if (ret)
538 kfree(tm);
540 return ret;
543 static noinline int
544 tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
545 struct extent_buffer *eb, int dst_slot, int src_slot,
546 int nr_items)
548 struct tree_mod_elem *tm = NULL;
549 struct tree_mod_elem **tm_list = NULL;
550 int ret = 0;
551 int i;
552 int locked = 0;
554 if (!tree_mod_need_log(fs_info, eb))
555 return 0;
557 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
558 if (!tm_list)
559 return -ENOMEM;
561 tm = kzalloc(sizeof(*tm), GFP_NOFS);
562 if (!tm) {
563 ret = -ENOMEM;
564 goto free_tms;
567 tm->logical = eb->start;
568 tm->slot = src_slot;
569 tm->move.dst_slot = dst_slot;
570 tm->move.nr_items = nr_items;
571 tm->op = MOD_LOG_MOVE_KEYS;
573 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
574 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
575 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
576 if (!tm_list[i]) {
577 ret = -ENOMEM;
578 goto free_tms;
582 if (tree_mod_dont_log(fs_info, eb))
583 goto free_tms;
584 locked = 1;
587 * When we override something during the move, we log these removals.
588 * This can only happen when we move towards the beginning of the
589 * buffer, i.e. dst_slot < src_slot.
591 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
592 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
593 if (ret)
594 goto free_tms;
597 ret = __tree_mod_log_insert(fs_info, tm);
598 if (ret)
599 goto free_tms;
600 write_unlock(&eb->fs_info->tree_mod_log_lock);
601 kfree(tm_list);
603 return 0;
604 free_tms:
605 for (i = 0; i < nr_items; i++) {
606 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
607 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
608 kfree(tm_list[i]);
610 if (locked)
611 write_unlock(&eb->fs_info->tree_mod_log_lock);
612 kfree(tm_list);
613 kfree(tm);
615 return ret;
618 static inline int
619 __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
620 struct tree_mod_elem **tm_list,
621 int nritems)
623 int i, j;
624 int ret;
626 for (i = nritems - 1; i >= 0; i--) {
627 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
628 if (ret) {
629 for (j = nritems - 1; j > i; j--)
630 rb_erase(&tm_list[j]->node,
631 &fs_info->tree_mod_log);
632 return ret;
636 return 0;
639 static noinline int
640 tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
641 struct extent_buffer *old_root,
642 struct extent_buffer *new_root,
643 int log_removal)
645 struct tree_mod_elem *tm = NULL;
646 struct tree_mod_elem **tm_list = NULL;
647 int nritems = 0;
648 int ret = 0;
649 int i;
651 if (!tree_mod_need_log(fs_info, NULL))
652 return 0;
654 if (log_removal && btrfs_header_level(old_root) > 0) {
655 nritems = btrfs_header_nritems(old_root);
656 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
657 GFP_NOFS);
658 if (!tm_list) {
659 ret = -ENOMEM;
660 goto free_tms;
662 for (i = 0; i < nritems; i++) {
663 tm_list[i] = alloc_tree_mod_elem(old_root, i,
664 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
665 if (!tm_list[i]) {
666 ret = -ENOMEM;
667 goto free_tms;
672 tm = kzalloc(sizeof(*tm), GFP_NOFS);
673 if (!tm) {
674 ret = -ENOMEM;
675 goto free_tms;
678 tm->logical = new_root->start;
679 tm->old_root.logical = old_root->start;
680 tm->old_root.level = btrfs_header_level(old_root);
681 tm->generation = btrfs_header_generation(old_root);
682 tm->op = MOD_LOG_ROOT_REPLACE;
684 if (tree_mod_dont_log(fs_info, NULL))
685 goto free_tms;
687 if (tm_list)
688 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
689 if (!ret)
690 ret = __tree_mod_log_insert(fs_info, tm);
692 write_unlock(&fs_info->tree_mod_log_lock);
693 if (ret)
694 goto free_tms;
695 kfree(tm_list);
697 return ret;
699 free_tms:
700 if (tm_list) {
701 for (i = 0; i < nritems; i++)
702 kfree(tm_list[i]);
703 kfree(tm_list);
705 kfree(tm);
707 return ret;
710 static struct tree_mod_elem *
711 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
712 int smallest)
714 struct rb_root *tm_root;
715 struct rb_node *node;
716 struct tree_mod_elem *cur = NULL;
717 struct tree_mod_elem *found = NULL;
719 read_lock(&fs_info->tree_mod_log_lock);
720 tm_root = &fs_info->tree_mod_log;
721 node = tm_root->rb_node;
722 while (node) {
723 cur = rb_entry(node, struct tree_mod_elem, node);
724 if (cur->logical < start) {
725 node = node->rb_left;
726 } else if (cur->logical > start) {
727 node = node->rb_right;
728 } else if (cur->seq < min_seq) {
729 node = node->rb_left;
730 } else if (!smallest) {
731 /* we want the node with the highest seq */
732 if (found)
733 BUG_ON(found->seq > cur->seq);
734 found = cur;
735 node = node->rb_left;
736 } else if (cur->seq > min_seq) {
737 /* we want the node with the smallest seq */
738 if (found)
739 BUG_ON(found->seq < cur->seq);
740 found = cur;
741 node = node->rb_right;
742 } else {
743 found = cur;
744 break;
747 read_unlock(&fs_info->tree_mod_log_lock);
749 return found;
753 * this returns the element from the log with the smallest time sequence
754 * value that's in the log (the oldest log item). any element with a time
755 * sequence lower than min_seq will be ignored.
757 static struct tree_mod_elem *
758 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
759 u64 min_seq)
761 return __tree_mod_log_search(fs_info, start, min_seq, 1);
765 * this returns the element from the log with the largest time sequence
766 * value that's in the log (the most recent log item). any element with
767 * a time sequence lower than min_seq will be ignored.
769 static struct tree_mod_elem *
770 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
772 return __tree_mod_log_search(fs_info, start, min_seq, 0);
775 static noinline int
776 tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
777 struct extent_buffer *src, unsigned long dst_offset,
778 unsigned long src_offset, int nr_items)
780 int ret = 0;
781 struct tree_mod_elem **tm_list = NULL;
782 struct tree_mod_elem **tm_list_add, **tm_list_rem;
783 int i;
784 int locked = 0;
786 if (!tree_mod_need_log(fs_info, NULL))
787 return 0;
789 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
790 return 0;
792 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
793 GFP_NOFS);
794 if (!tm_list)
795 return -ENOMEM;
797 tm_list_add = tm_list;
798 tm_list_rem = tm_list + nr_items;
799 for (i = 0; i < nr_items; i++) {
800 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
801 MOD_LOG_KEY_REMOVE, GFP_NOFS);
802 if (!tm_list_rem[i]) {
803 ret = -ENOMEM;
804 goto free_tms;
807 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
808 MOD_LOG_KEY_ADD, GFP_NOFS);
809 if (!tm_list_add[i]) {
810 ret = -ENOMEM;
811 goto free_tms;
815 if (tree_mod_dont_log(fs_info, NULL))
816 goto free_tms;
817 locked = 1;
819 for (i = 0; i < nr_items; i++) {
820 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
821 if (ret)
822 goto free_tms;
823 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
824 if (ret)
825 goto free_tms;
828 write_unlock(&fs_info->tree_mod_log_lock);
829 kfree(tm_list);
831 return 0;
833 free_tms:
834 for (i = 0; i < nr_items * 2; i++) {
835 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
836 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
837 kfree(tm_list[i]);
839 if (locked)
840 write_unlock(&fs_info->tree_mod_log_lock);
841 kfree(tm_list);
843 return ret;
846 static inline void
847 tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
848 int dst_offset, int src_offset, int nr_items)
850 int ret;
851 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
852 nr_items);
853 BUG_ON(ret < 0);
856 static noinline void
857 tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
858 struct extent_buffer *eb, int slot, int atomic)
860 int ret;
862 ret = tree_mod_log_insert_key(fs_info, eb, slot,
863 MOD_LOG_KEY_REPLACE,
864 atomic ? GFP_ATOMIC : GFP_NOFS);
865 BUG_ON(ret < 0);
868 static noinline int
869 tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
871 struct tree_mod_elem **tm_list = NULL;
872 int nritems = 0;
873 int i;
874 int ret = 0;
876 if (btrfs_header_level(eb) == 0)
877 return 0;
879 if (!tree_mod_need_log(fs_info, NULL))
880 return 0;
882 nritems = btrfs_header_nritems(eb);
883 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
884 if (!tm_list)
885 return -ENOMEM;
887 for (i = 0; i < nritems; i++) {
888 tm_list[i] = alloc_tree_mod_elem(eb, i,
889 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
890 if (!tm_list[i]) {
891 ret = -ENOMEM;
892 goto free_tms;
896 if (tree_mod_dont_log(fs_info, eb))
897 goto free_tms;
899 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
900 write_unlock(&eb->fs_info->tree_mod_log_lock);
901 if (ret)
902 goto free_tms;
903 kfree(tm_list);
905 return 0;
907 free_tms:
908 for (i = 0; i < nritems; i++)
909 kfree(tm_list[i]);
910 kfree(tm_list);
912 return ret;
915 static noinline void
916 tree_mod_log_set_root_pointer(struct btrfs_root *root,
917 struct extent_buffer *new_root_node,
918 int log_removal)
920 int ret;
921 ret = tree_mod_log_insert_root(root->fs_info, root->node,
922 new_root_node, log_removal);
923 BUG_ON(ret < 0);
927 * check if the tree block can be shared by multiple trees
929 int btrfs_block_can_be_shared(struct btrfs_root *root,
930 struct extent_buffer *buf)
933 * Tree blocks not in reference counted trees and tree roots
934 * are never shared. If a block was allocated after the last
935 * snapshot and the block was not allocated by tree relocation,
936 * we know the block is not shared.
938 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
939 buf != root->node && buf != root->commit_root &&
940 (btrfs_header_generation(buf) <=
941 btrfs_root_last_snapshot(&root->root_item) ||
942 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
943 return 1;
944 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
945 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
946 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
947 return 1;
948 #endif
949 return 0;
952 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
953 struct btrfs_root *root,
954 struct extent_buffer *buf,
955 struct extent_buffer *cow,
956 int *last_ref)
958 struct btrfs_fs_info *fs_info = root->fs_info;
959 u64 refs;
960 u64 owner;
961 u64 flags;
962 u64 new_flags = 0;
963 int ret;
966 * Backrefs update rules:
968 * Always use full backrefs for extent pointers in tree block
969 * allocated by tree relocation.
971 * If a shared tree block is no longer referenced by its owner
972 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
973 * use full backrefs for extent pointers in tree block.
975 * If a tree block is been relocating
976 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
977 * use full backrefs for extent pointers in tree block.
978 * The reason for this is some operations (such as drop tree)
979 * are only allowed for blocks use full backrefs.
982 if (btrfs_block_can_be_shared(root, buf)) {
983 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
984 btrfs_header_level(buf), 1,
985 &refs, &flags);
986 if (ret)
987 return ret;
988 if (refs == 0) {
989 ret = -EROFS;
990 btrfs_handle_fs_error(fs_info, ret, NULL);
991 return ret;
993 } else {
994 refs = 1;
995 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
996 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
997 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
998 else
999 flags = 0;
1002 owner = btrfs_header_owner(buf);
1003 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1004 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1006 if (refs > 1) {
1007 if ((owner == root->root_key.objectid ||
1008 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1009 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
1010 ret = btrfs_inc_ref(trans, root, buf, 1);
1011 if (ret)
1012 return ret;
1014 if (root->root_key.objectid ==
1015 BTRFS_TREE_RELOC_OBJECTID) {
1016 ret = btrfs_dec_ref(trans, root, buf, 0);
1017 if (ret)
1018 return ret;
1019 ret = btrfs_inc_ref(trans, root, cow, 1);
1020 if (ret)
1021 return ret;
1023 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1024 } else {
1026 if (root->root_key.objectid ==
1027 BTRFS_TREE_RELOC_OBJECTID)
1028 ret = btrfs_inc_ref(trans, root, cow, 1);
1029 else
1030 ret = btrfs_inc_ref(trans, root, cow, 0);
1031 if (ret)
1032 return ret;
1034 if (new_flags != 0) {
1035 int level = btrfs_header_level(buf);
1037 ret = btrfs_set_disk_extent_flags(trans, fs_info,
1038 buf->start,
1039 buf->len,
1040 new_flags, level, 0);
1041 if (ret)
1042 return ret;
1044 } else {
1045 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1046 if (root->root_key.objectid ==
1047 BTRFS_TREE_RELOC_OBJECTID)
1048 ret = btrfs_inc_ref(trans, root, cow, 1);
1049 else
1050 ret = btrfs_inc_ref(trans, root, cow, 0);
1051 if (ret)
1052 return ret;
1053 ret = btrfs_dec_ref(trans, root, buf, 1);
1054 if (ret)
1055 return ret;
1057 clean_tree_block(fs_info, buf);
1058 *last_ref = 1;
1060 return 0;
1064 * does the dirty work in cow of a single block. The parent block (if
1065 * supplied) is updated to point to the new cow copy. The new buffer is marked
1066 * dirty and returned locked. If you modify the block it needs to be marked
1067 * dirty again.
1069 * search_start -- an allocation hint for the new block
1071 * empty_size -- a hint that you plan on doing more cow. This is the size in
1072 * bytes the allocator should try to find free next to the block it returns.
1073 * This is just a hint and may be ignored by the allocator.
1075 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1076 struct btrfs_root *root,
1077 struct extent_buffer *buf,
1078 struct extent_buffer *parent, int parent_slot,
1079 struct extent_buffer **cow_ret,
1080 u64 search_start, u64 empty_size)
1082 struct btrfs_fs_info *fs_info = root->fs_info;
1083 struct btrfs_disk_key disk_key;
1084 struct extent_buffer *cow;
1085 int level, ret;
1086 int last_ref = 0;
1087 int unlock_orig = 0;
1088 u64 parent_start = 0;
1090 if (*cow_ret == buf)
1091 unlock_orig = 1;
1093 btrfs_assert_tree_locked(buf);
1095 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1096 trans->transid != fs_info->running_transaction->transid);
1097 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1098 trans->transid != root->last_trans);
1100 level = btrfs_header_level(buf);
1102 if (level == 0)
1103 btrfs_item_key(buf, &disk_key, 0);
1104 else
1105 btrfs_node_key(buf, &disk_key, 0);
1107 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1108 parent_start = parent->start;
1110 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1111 root->root_key.objectid, &disk_key, level,
1112 search_start, empty_size);
1113 if (IS_ERR(cow))
1114 return PTR_ERR(cow);
1116 /* cow is set to blocking by btrfs_init_new_buffer */
1118 copy_extent_buffer_full(cow, buf);
1119 btrfs_set_header_bytenr(cow, cow->start);
1120 btrfs_set_header_generation(cow, trans->transid);
1121 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1122 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1123 BTRFS_HEADER_FLAG_RELOC);
1124 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1125 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1126 else
1127 btrfs_set_header_owner(cow, root->root_key.objectid);
1129 write_extent_buffer_fsid(cow, fs_info->fsid);
1131 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1132 if (ret) {
1133 btrfs_tree_unlock(cow);
1134 free_extent_buffer(cow);
1135 btrfs_abort_transaction(trans, ret);
1136 return ret;
1139 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
1140 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1141 if (ret) {
1142 btrfs_tree_unlock(cow);
1143 free_extent_buffer(cow);
1144 btrfs_abort_transaction(trans, ret);
1145 return ret;
1149 if (buf == root->node) {
1150 WARN_ON(parent && parent != buf);
1151 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1152 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1153 parent_start = buf->start;
1155 extent_buffer_get(cow);
1156 tree_mod_log_set_root_pointer(root, cow, 1);
1157 rcu_assign_pointer(root->node, cow);
1159 btrfs_free_tree_block(trans, root, buf, parent_start,
1160 last_ref);
1161 free_extent_buffer(buf);
1162 add_root_to_dirty_list(root);
1163 } else {
1164 WARN_ON(trans->transid != btrfs_header_generation(parent));
1165 tree_mod_log_insert_key(fs_info, parent, parent_slot,
1166 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1167 btrfs_set_node_blockptr(parent, parent_slot,
1168 cow->start);
1169 btrfs_set_node_ptr_generation(parent, parent_slot,
1170 trans->transid);
1171 btrfs_mark_buffer_dirty(parent);
1172 if (last_ref) {
1173 ret = tree_mod_log_free_eb(fs_info, buf);
1174 if (ret) {
1175 btrfs_tree_unlock(cow);
1176 free_extent_buffer(cow);
1177 btrfs_abort_transaction(trans, ret);
1178 return ret;
1181 btrfs_free_tree_block(trans, root, buf, parent_start,
1182 last_ref);
1184 if (unlock_orig)
1185 btrfs_tree_unlock(buf);
1186 free_extent_buffer_stale(buf);
1187 btrfs_mark_buffer_dirty(cow);
1188 *cow_ret = cow;
1189 return 0;
1193 * returns the logical address of the oldest predecessor of the given root.
1194 * entries older than time_seq are ignored.
1196 static struct tree_mod_elem *
1197 __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
1198 struct extent_buffer *eb_root, u64 time_seq)
1200 struct tree_mod_elem *tm;
1201 struct tree_mod_elem *found = NULL;
1202 u64 root_logical = eb_root->start;
1203 int looped = 0;
1205 if (!time_seq)
1206 return NULL;
1209 * the very last operation that's logged for a root is the
1210 * replacement operation (if it is replaced at all). this has
1211 * the logical address of the *new* root, making it the very
1212 * first operation that's logged for this root.
1214 while (1) {
1215 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1216 time_seq);
1217 if (!looped && !tm)
1218 return NULL;
1220 * if there are no tree operation for the oldest root, we simply
1221 * return it. this should only happen if that (old) root is at
1222 * level 0.
1224 if (!tm)
1225 break;
1228 * if there's an operation that's not a root replacement, we
1229 * found the oldest version of our root. normally, we'll find a
1230 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1232 if (tm->op != MOD_LOG_ROOT_REPLACE)
1233 break;
1235 found = tm;
1236 root_logical = tm->old_root.logical;
1237 looped = 1;
1240 /* if there's no old root to return, return what we found instead */
1241 if (!found)
1242 found = tm;
1244 return found;
1248 * tm is a pointer to the first operation to rewind within eb. then, all
1249 * previous operations will be rewound (until we reach something older than
1250 * time_seq).
1252 static void
1253 __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1254 u64 time_seq, struct tree_mod_elem *first_tm)
1256 u32 n;
1257 struct rb_node *next;
1258 struct tree_mod_elem *tm = first_tm;
1259 unsigned long o_dst;
1260 unsigned long o_src;
1261 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1263 n = btrfs_header_nritems(eb);
1264 read_lock(&fs_info->tree_mod_log_lock);
1265 while (tm && tm->seq >= time_seq) {
1267 * all the operations are recorded with the operator used for
1268 * the modification. as we're going backwards, we do the
1269 * opposite of each operation here.
1271 switch (tm->op) {
1272 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1273 BUG_ON(tm->slot < n);
1274 /* Fallthrough */
1275 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1276 case MOD_LOG_KEY_REMOVE:
1277 btrfs_set_node_key(eb, &tm->key, tm->slot);
1278 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1279 btrfs_set_node_ptr_generation(eb, tm->slot,
1280 tm->generation);
1281 n++;
1282 break;
1283 case MOD_LOG_KEY_REPLACE:
1284 BUG_ON(tm->slot >= n);
1285 btrfs_set_node_key(eb, &tm->key, tm->slot);
1286 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1287 btrfs_set_node_ptr_generation(eb, tm->slot,
1288 tm->generation);
1289 break;
1290 case MOD_LOG_KEY_ADD:
1291 /* if a move operation is needed it's in the log */
1292 n--;
1293 break;
1294 case MOD_LOG_MOVE_KEYS:
1295 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1296 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1297 memmove_extent_buffer(eb, o_dst, o_src,
1298 tm->move.nr_items * p_size);
1299 break;
1300 case MOD_LOG_ROOT_REPLACE:
1302 * this operation is special. for roots, this must be
1303 * handled explicitly before rewinding.
1304 * for non-roots, this operation may exist if the node
1305 * was a root: root A -> child B; then A gets empty and
1306 * B is promoted to the new root. in the mod log, we'll
1307 * have a root-replace operation for B, a tree block
1308 * that is no root. we simply ignore that operation.
1310 break;
1312 next = rb_next(&tm->node);
1313 if (!next)
1314 break;
1315 tm = rb_entry(next, struct tree_mod_elem, node);
1316 if (tm->logical != first_tm->logical)
1317 break;
1319 read_unlock(&fs_info->tree_mod_log_lock);
1320 btrfs_set_header_nritems(eb, n);
1324 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
1325 * is returned. If rewind operations happen, a fresh buffer is returned. The
1326 * returned buffer is always read-locked. If the returned buffer is not the
1327 * input buffer, the lock on the input buffer is released and the input buffer
1328 * is freed (its refcount is decremented).
1330 static struct extent_buffer *
1331 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1332 struct extent_buffer *eb, u64 time_seq)
1334 struct extent_buffer *eb_rewin;
1335 struct tree_mod_elem *tm;
1337 if (!time_seq)
1338 return eb;
1340 if (btrfs_header_level(eb) == 0)
1341 return eb;
1343 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1344 if (!tm)
1345 return eb;
1347 btrfs_set_path_blocking(path);
1348 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1350 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1351 BUG_ON(tm->slot != 0);
1352 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
1353 if (!eb_rewin) {
1354 btrfs_tree_read_unlock_blocking(eb);
1355 free_extent_buffer(eb);
1356 return NULL;
1358 btrfs_set_header_bytenr(eb_rewin, eb->start);
1359 btrfs_set_header_backref_rev(eb_rewin,
1360 btrfs_header_backref_rev(eb));
1361 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1362 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1363 } else {
1364 eb_rewin = btrfs_clone_extent_buffer(eb);
1365 if (!eb_rewin) {
1366 btrfs_tree_read_unlock_blocking(eb);
1367 free_extent_buffer(eb);
1368 return NULL;
1372 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1373 btrfs_tree_read_unlock_blocking(eb);
1374 free_extent_buffer(eb);
1376 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
1377 eb_rewin, btrfs_header_level(eb_rewin));
1378 btrfs_tree_read_lock(eb_rewin);
1379 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
1380 WARN_ON(btrfs_header_nritems(eb_rewin) >
1381 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1383 return eb_rewin;
1387 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1388 * value. If there are no changes, the current root->root_node is returned. If
1389 * anything changed in between, there's a fresh buffer allocated on which the
1390 * rewind operations are done. In any case, the returned buffer is read locked.
1391 * Returns NULL on error (with no locks held).
1393 static inline struct extent_buffer *
1394 get_old_root(struct btrfs_root *root, u64 time_seq)
1396 struct btrfs_fs_info *fs_info = root->fs_info;
1397 struct tree_mod_elem *tm;
1398 struct extent_buffer *eb = NULL;
1399 struct extent_buffer *eb_root;
1400 u64 eb_root_owner = 0;
1401 struct extent_buffer *old;
1402 struct tree_mod_root *old_root = NULL;
1403 u64 old_generation = 0;
1404 u64 logical;
1406 eb_root = btrfs_read_lock_root_node(root);
1407 tm = __tree_mod_log_oldest_root(fs_info, eb_root, time_seq);
1408 if (!tm)
1409 return eb_root;
1411 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1412 old_root = &tm->old_root;
1413 old_generation = tm->generation;
1414 logical = old_root->logical;
1415 } else {
1416 logical = eb_root->start;
1419 tm = tree_mod_log_search(fs_info, logical, time_seq);
1420 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1421 btrfs_tree_read_unlock(eb_root);
1422 free_extent_buffer(eb_root);
1423 old = read_tree_block(fs_info, logical, 0);
1424 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1425 if (!IS_ERR(old))
1426 free_extent_buffer(old);
1427 btrfs_warn(fs_info,
1428 "failed to read tree block %llu from get_old_root",
1429 logical);
1430 } else {
1431 eb = btrfs_clone_extent_buffer(old);
1432 free_extent_buffer(old);
1434 } else if (old_root) {
1435 eb_root_owner = btrfs_header_owner(eb_root);
1436 btrfs_tree_read_unlock(eb_root);
1437 free_extent_buffer(eb_root);
1438 eb = alloc_dummy_extent_buffer(fs_info, logical);
1439 } else {
1440 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
1441 eb = btrfs_clone_extent_buffer(eb_root);
1442 btrfs_tree_read_unlock_blocking(eb_root);
1443 free_extent_buffer(eb_root);
1446 if (!eb)
1447 return NULL;
1448 if (old_root) {
1449 btrfs_set_header_bytenr(eb, eb->start);
1450 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1451 btrfs_set_header_owner(eb, eb_root_owner);
1452 btrfs_set_header_level(eb, old_root->level);
1453 btrfs_set_header_generation(eb, old_generation);
1455 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
1456 btrfs_header_level(eb));
1457 btrfs_tree_read_lock(eb);
1458 if (tm)
1459 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1460 else
1461 WARN_ON(btrfs_header_level(eb) != 0);
1462 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1464 return eb;
1467 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1469 struct tree_mod_elem *tm;
1470 int level;
1471 struct extent_buffer *eb_root = btrfs_root_node(root);
1473 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
1474 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1475 level = tm->old_root.level;
1476 } else {
1477 level = btrfs_header_level(eb_root);
1479 free_extent_buffer(eb_root);
1481 return level;
1484 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1485 struct btrfs_root *root,
1486 struct extent_buffer *buf)
1488 if (btrfs_is_testing(root->fs_info))
1489 return 0;
1491 /* ensure we can see the force_cow */
1492 smp_rmb();
1495 * We do not need to cow a block if
1496 * 1) this block is not created or changed in this transaction;
1497 * 2) this block does not belong to TREE_RELOC tree;
1498 * 3) the root is not forced COW.
1500 * What is forced COW:
1501 * when we create snapshot during committing the transaction,
1502 * after we've finished coping src root, we must COW the shared
1503 * block to ensure the metadata consistency.
1505 if (btrfs_header_generation(buf) == trans->transid &&
1506 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1507 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1508 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1509 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
1510 return 0;
1511 return 1;
1515 * cows a single block, see __btrfs_cow_block for the real work.
1516 * This version of it has extra checks so that a block isn't COWed more than
1517 * once per transaction, as long as it hasn't been written yet
1519 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1520 struct btrfs_root *root, struct extent_buffer *buf,
1521 struct extent_buffer *parent, int parent_slot,
1522 struct extent_buffer **cow_ret)
1524 struct btrfs_fs_info *fs_info = root->fs_info;
1525 u64 search_start;
1526 int ret;
1528 if (trans->transaction != fs_info->running_transaction)
1529 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1530 trans->transid,
1531 fs_info->running_transaction->transid);
1533 if (trans->transid != fs_info->generation)
1534 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1535 trans->transid, fs_info->generation);
1537 if (!should_cow_block(trans, root, buf)) {
1538 trans->dirty = true;
1539 *cow_ret = buf;
1540 return 0;
1543 search_start = buf->start & ~((u64)SZ_1G - 1);
1545 if (parent)
1546 btrfs_set_lock_blocking(parent);
1547 btrfs_set_lock_blocking(buf);
1549 ret = __btrfs_cow_block(trans, root, buf, parent,
1550 parent_slot, cow_ret, search_start, 0);
1552 trace_btrfs_cow_block(root, buf, *cow_ret);
1554 return ret;
1558 * helper function for defrag to decide if two blocks pointed to by a
1559 * node are actually close by
1561 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1563 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1564 return 1;
1565 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1566 return 1;
1567 return 0;
1571 * compare two keys in a memcmp fashion
1573 static int comp_keys(const struct btrfs_disk_key *disk,
1574 const struct btrfs_key *k2)
1576 struct btrfs_key k1;
1578 btrfs_disk_key_to_cpu(&k1, disk);
1580 return btrfs_comp_cpu_keys(&k1, k2);
1584 * same as comp_keys only with two btrfs_key's
1586 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
1588 if (k1->objectid > k2->objectid)
1589 return 1;
1590 if (k1->objectid < k2->objectid)
1591 return -1;
1592 if (k1->type > k2->type)
1593 return 1;
1594 if (k1->type < k2->type)
1595 return -1;
1596 if (k1->offset > k2->offset)
1597 return 1;
1598 if (k1->offset < k2->offset)
1599 return -1;
1600 return 0;
1604 * this is used by the defrag code to go through all the
1605 * leaves pointed to by a node and reallocate them so that
1606 * disk order is close to key order
1608 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1609 struct btrfs_root *root, struct extent_buffer *parent,
1610 int start_slot, u64 *last_ret,
1611 struct btrfs_key *progress)
1613 struct btrfs_fs_info *fs_info = root->fs_info;
1614 struct extent_buffer *cur;
1615 u64 blocknr;
1616 u64 gen;
1617 u64 search_start = *last_ret;
1618 u64 last_block = 0;
1619 u64 other;
1620 u32 parent_nritems;
1621 int end_slot;
1622 int i;
1623 int err = 0;
1624 int parent_level;
1625 int uptodate;
1626 u32 blocksize;
1627 int progress_passed = 0;
1628 struct btrfs_disk_key disk_key;
1630 parent_level = btrfs_header_level(parent);
1632 WARN_ON(trans->transaction != fs_info->running_transaction);
1633 WARN_ON(trans->transid != fs_info->generation);
1635 parent_nritems = btrfs_header_nritems(parent);
1636 blocksize = fs_info->nodesize;
1637 end_slot = parent_nritems - 1;
1639 if (parent_nritems <= 1)
1640 return 0;
1642 btrfs_set_lock_blocking(parent);
1644 for (i = start_slot; i <= end_slot; i++) {
1645 int close = 1;
1647 btrfs_node_key(parent, &disk_key, i);
1648 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1649 continue;
1651 progress_passed = 1;
1652 blocknr = btrfs_node_blockptr(parent, i);
1653 gen = btrfs_node_ptr_generation(parent, i);
1654 if (last_block == 0)
1655 last_block = blocknr;
1657 if (i > 0) {
1658 other = btrfs_node_blockptr(parent, i - 1);
1659 close = close_blocks(blocknr, other, blocksize);
1661 if (!close && i < end_slot) {
1662 other = btrfs_node_blockptr(parent, i + 1);
1663 close = close_blocks(blocknr, other, blocksize);
1665 if (close) {
1666 last_block = blocknr;
1667 continue;
1670 cur = find_extent_buffer(fs_info, blocknr);
1671 if (cur)
1672 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1673 else
1674 uptodate = 0;
1675 if (!cur || !uptodate) {
1676 if (!cur) {
1677 cur = read_tree_block(fs_info, blocknr, gen);
1678 if (IS_ERR(cur)) {
1679 return PTR_ERR(cur);
1680 } else if (!extent_buffer_uptodate(cur)) {
1681 free_extent_buffer(cur);
1682 return -EIO;
1684 } else if (!uptodate) {
1685 err = btrfs_read_buffer(cur, gen);
1686 if (err) {
1687 free_extent_buffer(cur);
1688 return err;
1692 if (search_start == 0)
1693 search_start = last_block;
1695 btrfs_tree_lock(cur);
1696 btrfs_set_lock_blocking(cur);
1697 err = __btrfs_cow_block(trans, root, cur, parent, i,
1698 &cur, search_start,
1699 min(16 * blocksize,
1700 (end_slot - i) * blocksize));
1701 if (err) {
1702 btrfs_tree_unlock(cur);
1703 free_extent_buffer(cur);
1704 break;
1706 search_start = cur->start;
1707 last_block = cur->start;
1708 *last_ret = search_start;
1709 btrfs_tree_unlock(cur);
1710 free_extent_buffer(cur);
1712 return err;
1716 * search for key in the extent_buffer. The items start at offset p,
1717 * and they are item_size apart. There are 'max' items in p.
1719 * the slot in the array is returned via slot, and it points to
1720 * the place where you would insert key if it is not found in
1721 * the array.
1723 * slot may point to max if the key is bigger than all of the keys
1725 static noinline int generic_bin_search(struct extent_buffer *eb,
1726 unsigned long p, int item_size,
1727 const struct btrfs_key *key,
1728 int max, int *slot)
1730 int low = 0;
1731 int high = max;
1732 int mid;
1733 int ret;
1734 struct btrfs_disk_key *tmp = NULL;
1735 struct btrfs_disk_key unaligned;
1736 unsigned long offset;
1737 char *kaddr = NULL;
1738 unsigned long map_start = 0;
1739 unsigned long map_len = 0;
1740 int err;
1742 if (low > high) {
1743 btrfs_err(eb->fs_info,
1744 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1745 __func__, low, high, eb->start,
1746 btrfs_header_owner(eb), btrfs_header_level(eb));
1747 return -EINVAL;
1750 while (low < high) {
1751 mid = (low + high) / 2;
1752 offset = p + mid * item_size;
1754 if (!kaddr || offset < map_start ||
1755 (offset + sizeof(struct btrfs_disk_key)) >
1756 map_start + map_len) {
1758 err = map_private_extent_buffer(eb, offset,
1759 sizeof(struct btrfs_disk_key),
1760 &kaddr, &map_start, &map_len);
1762 if (!err) {
1763 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1764 map_start);
1765 } else if (err == 1) {
1766 read_extent_buffer(eb, &unaligned,
1767 offset, sizeof(unaligned));
1768 tmp = &unaligned;
1769 } else {
1770 return err;
1773 } else {
1774 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1775 map_start);
1777 ret = comp_keys(tmp, key);
1779 if (ret < 0)
1780 low = mid + 1;
1781 else if (ret > 0)
1782 high = mid;
1783 else {
1784 *slot = mid;
1785 return 0;
1788 *slot = low;
1789 return 1;
1793 * simple bin_search frontend that does the right thing for
1794 * leaves vs nodes
1796 static int bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1797 int level, int *slot)
1799 if (level == 0)
1800 return generic_bin_search(eb,
1801 offsetof(struct btrfs_leaf, items),
1802 sizeof(struct btrfs_item),
1803 key, btrfs_header_nritems(eb),
1804 slot);
1805 else
1806 return generic_bin_search(eb,
1807 offsetof(struct btrfs_node, ptrs),
1808 sizeof(struct btrfs_key_ptr),
1809 key, btrfs_header_nritems(eb),
1810 slot);
1813 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1814 int level, int *slot)
1816 return bin_search(eb, key, level, slot);
1819 static void root_add_used(struct btrfs_root *root, u32 size)
1821 spin_lock(&root->accounting_lock);
1822 btrfs_set_root_used(&root->root_item,
1823 btrfs_root_used(&root->root_item) + size);
1824 spin_unlock(&root->accounting_lock);
1827 static void root_sub_used(struct btrfs_root *root, u32 size)
1829 spin_lock(&root->accounting_lock);
1830 btrfs_set_root_used(&root->root_item,
1831 btrfs_root_used(&root->root_item) - size);
1832 spin_unlock(&root->accounting_lock);
1835 /* given a node and slot number, this reads the blocks it points to. The
1836 * extent buffer is returned with a reference taken (but unlocked).
1838 static noinline struct extent_buffer *
1839 read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
1840 int slot)
1842 int level = btrfs_header_level(parent);
1843 struct extent_buffer *eb;
1845 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1846 return ERR_PTR(-ENOENT);
1848 BUG_ON(level == 0);
1850 eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
1851 btrfs_node_ptr_generation(parent, slot));
1852 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1853 free_extent_buffer(eb);
1854 eb = ERR_PTR(-EIO);
1857 return eb;
1861 * node level balancing, used to make sure nodes are in proper order for
1862 * item deletion. We balance from the top down, so we have to make sure
1863 * that a deletion won't leave an node completely empty later on.
1865 static noinline int balance_level(struct btrfs_trans_handle *trans,
1866 struct btrfs_root *root,
1867 struct btrfs_path *path, int level)
1869 struct btrfs_fs_info *fs_info = root->fs_info;
1870 struct extent_buffer *right = NULL;
1871 struct extent_buffer *mid;
1872 struct extent_buffer *left = NULL;
1873 struct extent_buffer *parent = NULL;
1874 int ret = 0;
1875 int wret;
1876 int pslot;
1877 int orig_slot = path->slots[level];
1878 u64 orig_ptr;
1880 if (level == 0)
1881 return 0;
1883 mid = path->nodes[level];
1885 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1886 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1887 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1889 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1891 if (level < BTRFS_MAX_LEVEL - 1) {
1892 parent = path->nodes[level + 1];
1893 pslot = path->slots[level + 1];
1897 * deal with the case where there is only one pointer in the root
1898 * by promoting the node below to a root
1900 if (!parent) {
1901 struct extent_buffer *child;
1903 if (btrfs_header_nritems(mid) != 1)
1904 return 0;
1906 /* promote the child to a root */
1907 child = read_node_slot(fs_info, mid, 0);
1908 if (IS_ERR(child)) {
1909 ret = PTR_ERR(child);
1910 btrfs_handle_fs_error(fs_info, ret, NULL);
1911 goto enospc;
1914 btrfs_tree_lock(child);
1915 btrfs_set_lock_blocking(child);
1916 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1917 if (ret) {
1918 btrfs_tree_unlock(child);
1919 free_extent_buffer(child);
1920 goto enospc;
1923 tree_mod_log_set_root_pointer(root, child, 1);
1924 rcu_assign_pointer(root->node, child);
1926 add_root_to_dirty_list(root);
1927 btrfs_tree_unlock(child);
1929 path->locks[level] = 0;
1930 path->nodes[level] = NULL;
1931 clean_tree_block(fs_info, mid);
1932 btrfs_tree_unlock(mid);
1933 /* once for the path */
1934 free_extent_buffer(mid);
1936 root_sub_used(root, mid->len);
1937 btrfs_free_tree_block(trans, root, mid, 0, 1);
1938 /* once for the root ptr */
1939 free_extent_buffer_stale(mid);
1940 return 0;
1942 if (btrfs_header_nritems(mid) >
1943 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1944 return 0;
1946 left = read_node_slot(fs_info, parent, pslot - 1);
1947 if (IS_ERR(left))
1948 left = NULL;
1950 if (left) {
1951 btrfs_tree_lock(left);
1952 btrfs_set_lock_blocking(left);
1953 wret = btrfs_cow_block(trans, root, left,
1954 parent, pslot - 1, &left);
1955 if (wret) {
1956 ret = wret;
1957 goto enospc;
1961 right = read_node_slot(fs_info, parent, pslot + 1);
1962 if (IS_ERR(right))
1963 right = NULL;
1965 if (right) {
1966 btrfs_tree_lock(right);
1967 btrfs_set_lock_blocking(right);
1968 wret = btrfs_cow_block(trans, root, right,
1969 parent, pslot + 1, &right);
1970 if (wret) {
1971 ret = wret;
1972 goto enospc;
1976 /* first, try to make some room in the middle buffer */
1977 if (left) {
1978 orig_slot += btrfs_header_nritems(left);
1979 wret = push_node_left(trans, fs_info, left, mid, 1);
1980 if (wret < 0)
1981 ret = wret;
1985 * then try to empty the right most buffer into the middle
1987 if (right) {
1988 wret = push_node_left(trans, fs_info, mid, right, 1);
1989 if (wret < 0 && wret != -ENOSPC)
1990 ret = wret;
1991 if (btrfs_header_nritems(right) == 0) {
1992 clean_tree_block(fs_info, right);
1993 btrfs_tree_unlock(right);
1994 del_ptr(root, path, level + 1, pslot + 1);
1995 root_sub_used(root, right->len);
1996 btrfs_free_tree_block(trans, root, right, 0, 1);
1997 free_extent_buffer_stale(right);
1998 right = NULL;
1999 } else {
2000 struct btrfs_disk_key right_key;
2001 btrfs_node_key(right, &right_key, 0);
2002 tree_mod_log_set_node_key(fs_info, parent,
2003 pslot + 1, 0);
2004 btrfs_set_node_key(parent, &right_key, pslot + 1);
2005 btrfs_mark_buffer_dirty(parent);
2008 if (btrfs_header_nritems(mid) == 1) {
2010 * we're not allowed to leave a node with one item in the
2011 * tree during a delete. A deletion from lower in the tree
2012 * could try to delete the only pointer in this node.
2013 * So, pull some keys from the left.
2014 * There has to be a left pointer at this point because
2015 * otherwise we would have pulled some pointers from the
2016 * right
2018 if (!left) {
2019 ret = -EROFS;
2020 btrfs_handle_fs_error(fs_info, ret, NULL);
2021 goto enospc;
2023 wret = balance_node_right(trans, fs_info, mid, left);
2024 if (wret < 0) {
2025 ret = wret;
2026 goto enospc;
2028 if (wret == 1) {
2029 wret = push_node_left(trans, fs_info, left, mid, 1);
2030 if (wret < 0)
2031 ret = wret;
2033 BUG_ON(wret == 1);
2035 if (btrfs_header_nritems(mid) == 0) {
2036 clean_tree_block(fs_info, mid);
2037 btrfs_tree_unlock(mid);
2038 del_ptr(root, path, level + 1, pslot);
2039 root_sub_used(root, mid->len);
2040 btrfs_free_tree_block(trans, root, mid, 0, 1);
2041 free_extent_buffer_stale(mid);
2042 mid = NULL;
2043 } else {
2044 /* update the parent key to reflect our changes */
2045 struct btrfs_disk_key mid_key;
2046 btrfs_node_key(mid, &mid_key, 0);
2047 tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
2048 btrfs_set_node_key(parent, &mid_key, pslot);
2049 btrfs_mark_buffer_dirty(parent);
2052 /* update the path */
2053 if (left) {
2054 if (btrfs_header_nritems(left) > orig_slot) {
2055 extent_buffer_get(left);
2056 /* left was locked after cow */
2057 path->nodes[level] = left;
2058 path->slots[level + 1] -= 1;
2059 path->slots[level] = orig_slot;
2060 if (mid) {
2061 btrfs_tree_unlock(mid);
2062 free_extent_buffer(mid);
2064 } else {
2065 orig_slot -= btrfs_header_nritems(left);
2066 path->slots[level] = orig_slot;
2069 /* double check we haven't messed things up */
2070 if (orig_ptr !=
2071 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
2072 BUG();
2073 enospc:
2074 if (right) {
2075 btrfs_tree_unlock(right);
2076 free_extent_buffer(right);
2078 if (left) {
2079 if (path->nodes[level] != left)
2080 btrfs_tree_unlock(left);
2081 free_extent_buffer(left);
2083 return ret;
2086 /* Node balancing for insertion. Here we only split or push nodes around
2087 * when they are completely full. This is also done top down, so we
2088 * have to be pessimistic.
2090 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2091 struct btrfs_root *root,
2092 struct btrfs_path *path, int level)
2094 struct btrfs_fs_info *fs_info = root->fs_info;
2095 struct extent_buffer *right = NULL;
2096 struct extent_buffer *mid;
2097 struct extent_buffer *left = NULL;
2098 struct extent_buffer *parent = NULL;
2099 int ret = 0;
2100 int wret;
2101 int pslot;
2102 int orig_slot = path->slots[level];
2104 if (level == 0)
2105 return 1;
2107 mid = path->nodes[level];
2108 WARN_ON(btrfs_header_generation(mid) != trans->transid);
2110 if (level < BTRFS_MAX_LEVEL - 1) {
2111 parent = path->nodes[level + 1];
2112 pslot = path->slots[level + 1];
2115 if (!parent)
2116 return 1;
2118 left = read_node_slot(fs_info, parent, pslot - 1);
2119 if (IS_ERR(left))
2120 left = NULL;
2122 /* first, try to make some room in the middle buffer */
2123 if (left) {
2124 u32 left_nr;
2126 btrfs_tree_lock(left);
2127 btrfs_set_lock_blocking(left);
2129 left_nr = btrfs_header_nritems(left);
2130 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2131 wret = 1;
2132 } else {
2133 ret = btrfs_cow_block(trans, root, left, parent,
2134 pslot - 1, &left);
2135 if (ret)
2136 wret = 1;
2137 else {
2138 wret = push_node_left(trans, fs_info,
2139 left, mid, 0);
2142 if (wret < 0)
2143 ret = wret;
2144 if (wret == 0) {
2145 struct btrfs_disk_key disk_key;
2146 orig_slot += left_nr;
2147 btrfs_node_key(mid, &disk_key, 0);
2148 tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
2149 btrfs_set_node_key(parent, &disk_key, pslot);
2150 btrfs_mark_buffer_dirty(parent);
2151 if (btrfs_header_nritems(left) > orig_slot) {
2152 path->nodes[level] = left;
2153 path->slots[level + 1] -= 1;
2154 path->slots[level] = orig_slot;
2155 btrfs_tree_unlock(mid);
2156 free_extent_buffer(mid);
2157 } else {
2158 orig_slot -=
2159 btrfs_header_nritems(left);
2160 path->slots[level] = orig_slot;
2161 btrfs_tree_unlock(left);
2162 free_extent_buffer(left);
2164 return 0;
2166 btrfs_tree_unlock(left);
2167 free_extent_buffer(left);
2169 right = read_node_slot(fs_info, parent, pslot + 1);
2170 if (IS_ERR(right))
2171 right = NULL;
2174 * then try to empty the right most buffer into the middle
2176 if (right) {
2177 u32 right_nr;
2179 btrfs_tree_lock(right);
2180 btrfs_set_lock_blocking(right);
2182 right_nr = btrfs_header_nritems(right);
2183 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2184 wret = 1;
2185 } else {
2186 ret = btrfs_cow_block(trans, root, right,
2187 parent, pslot + 1,
2188 &right);
2189 if (ret)
2190 wret = 1;
2191 else {
2192 wret = balance_node_right(trans, fs_info,
2193 right, mid);
2196 if (wret < 0)
2197 ret = wret;
2198 if (wret == 0) {
2199 struct btrfs_disk_key disk_key;
2201 btrfs_node_key(right, &disk_key, 0);
2202 tree_mod_log_set_node_key(fs_info, parent,
2203 pslot + 1, 0);
2204 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2205 btrfs_mark_buffer_dirty(parent);
2207 if (btrfs_header_nritems(mid) <= orig_slot) {
2208 path->nodes[level] = right;
2209 path->slots[level + 1] += 1;
2210 path->slots[level] = orig_slot -
2211 btrfs_header_nritems(mid);
2212 btrfs_tree_unlock(mid);
2213 free_extent_buffer(mid);
2214 } else {
2215 btrfs_tree_unlock(right);
2216 free_extent_buffer(right);
2218 return 0;
2220 btrfs_tree_unlock(right);
2221 free_extent_buffer(right);
2223 return 1;
2227 * readahead one full node of leaves, finding things that are close
2228 * to the block in 'slot', and triggering ra on them.
2230 static void reada_for_search(struct btrfs_fs_info *fs_info,
2231 struct btrfs_path *path,
2232 int level, int slot, u64 objectid)
2234 struct extent_buffer *node;
2235 struct btrfs_disk_key disk_key;
2236 u32 nritems;
2237 u64 search;
2238 u64 target;
2239 u64 nread = 0;
2240 struct extent_buffer *eb;
2241 u32 nr;
2242 u32 blocksize;
2243 u32 nscan = 0;
2245 if (level != 1)
2246 return;
2248 if (!path->nodes[level])
2249 return;
2251 node = path->nodes[level];
2253 search = btrfs_node_blockptr(node, slot);
2254 blocksize = fs_info->nodesize;
2255 eb = find_extent_buffer(fs_info, search);
2256 if (eb) {
2257 free_extent_buffer(eb);
2258 return;
2261 target = search;
2263 nritems = btrfs_header_nritems(node);
2264 nr = slot;
2266 while (1) {
2267 if (path->reada == READA_BACK) {
2268 if (nr == 0)
2269 break;
2270 nr--;
2271 } else if (path->reada == READA_FORWARD) {
2272 nr++;
2273 if (nr >= nritems)
2274 break;
2276 if (path->reada == READA_BACK && objectid) {
2277 btrfs_node_key(node, &disk_key, nr);
2278 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2279 break;
2281 search = btrfs_node_blockptr(node, nr);
2282 if ((search <= target && target - search <= 65536) ||
2283 (search > target && search - target <= 65536)) {
2284 readahead_tree_block(fs_info, search);
2285 nread += blocksize;
2287 nscan++;
2288 if ((nread > 65536 || nscan > 32))
2289 break;
2293 static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
2294 struct btrfs_path *path, int level)
2296 int slot;
2297 int nritems;
2298 struct extent_buffer *parent;
2299 struct extent_buffer *eb;
2300 u64 gen;
2301 u64 block1 = 0;
2302 u64 block2 = 0;
2304 parent = path->nodes[level + 1];
2305 if (!parent)
2306 return;
2308 nritems = btrfs_header_nritems(parent);
2309 slot = path->slots[level + 1];
2311 if (slot > 0) {
2312 block1 = btrfs_node_blockptr(parent, slot - 1);
2313 gen = btrfs_node_ptr_generation(parent, slot - 1);
2314 eb = find_extent_buffer(fs_info, block1);
2316 * if we get -eagain from btrfs_buffer_uptodate, we
2317 * don't want to return eagain here. That will loop
2318 * forever
2320 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2321 block1 = 0;
2322 free_extent_buffer(eb);
2324 if (slot + 1 < nritems) {
2325 block2 = btrfs_node_blockptr(parent, slot + 1);
2326 gen = btrfs_node_ptr_generation(parent, slot + 1);
2327 eb = find_extent_buffer(fs_info, block2);
2328 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2329 block2 = 0;
2330 free_extent_buffer(eb);
2333 if (block1)
2334 readahead_tree_block(fs_info, block1);
2335 if (block2)
2336 readahead_tree_block(fs_info, block2);
2341 * when we walk down the tree, it is usually safe to unlock the higher layers
2342 * in the tree. The exceptions are when our path goes through slot 0, because
2343 * operations on the tree might require changing key pointers higher up in the
2344 * tree.
2346 * callers might also have set path->keep_locks, which tells this code to keep
2347 * the lock if the path points to the last slot in the block. This is part of
2348 * walking through the tree, and selecting the next slot in the higher block.
2350 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2351 * if lowest_unlock is 1, level 0 won't be unlocked
2353 static noinline void unlock_up(struct btrfs_path *path, int level,
2354 int lowest_unlock, int min_write_lock_level,
2355 int *write_lock_level)
2357 int i;
2358 int skip_level = level;
2359 int no_skips = 0;
2360 struct extent_buffer *t;
2362 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2363 if (!path->nodes[i])
2364 break;
2365 if (!path->locks[i])
2366 break;
2367 if (!no_skips && path->slots[i] == 0) {
2368 skip_level = i + 1;
2369 continue;
2371 if (!no_skips && path->keep_locks) {
2372 u32 nritems;
2373 t = path->nodes[i];
2374 nritems = btrfs_header_nritems(t);
2375 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2376 skip_level = i + 1;
2377 continue;
2380 if (skip_level < i && i >= lowest_unlock)
2381 no_skips = 1;
2383 t = path->nodes[i];
2384 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
2385 btrfs_tree_unlock_rw(t, path->locks[i]);
2386 path->locks[i] = 0;
2387 if (write_lock_level &&
2388 i > min_write_lock_level &&
2389 i <= *write_lock_level) {
2390 *write_lock_level = i - 1;
2397 * This releases any locks held in the path starting at level and
2398 * going all the way up to the root.
2400 * btrfs_search_slot will keep the lock held on higher nodes in a few
2401 * corner cases, such as COW of the block at slot zero in the node. This
2402 * ignores those rules, and it should only be called when there are no
2403 * more updates to be done higher up in the tree.
2405 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2407 int i;
2409 if (path->keep_locks)
2410 return;
2412 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2413 if (!path->nodes[i])
2414 continue;
2415 if (!path->locks[i])
2416 continue;
2417 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2418 path->locks[i] = 0;
2423 * helper function for btrfs_search_slot. The goal is to find a block
2424 * in cache without setting the path to blocking. If we find the block
2425 * we return zero and the path is unchanged.
2427 * If we can't find the block, we set the path blocking and do some
2428 * reada. -EAGAIN is returned and the search must be repeated.
2430 static int
2431 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2432 struct extent_buffer **eb_ret, int level, int slot,
2433 const struct btrfs_key *key)
2435 struct btrfs_fs_info *fs_info = root->fs_info;
2436 u64 blocknr;
2437 u64 gen;
2438 struct extent_buffer *b = *eb_ret;
2439 struct extent_buffer *tmp;
2440 int ret;
2442 blocknr = btrfs_node_blockptr(b, slot);
2443 gen = btrfs_node_ptr_generation(b, slot);
2445 tmp = find_extent_buffer(fs_info, blocknr);
2446 if (tmp) {
2447 /* first we do an atomic uptodate check */
2448 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2449 *eb_ret = tmp;
2450 return 0;
2453 /* the pages were up to date, but we failed
2454 * the generation number check. Do a full
2455 * read for the generation number that is correct.
2456 * We must do this without dropping locks so
2457 * we can trust our generation number
2459 btrfs_set_path_blocking(p);
2461 /* now we're allowed to do a blocking uptodate check */
2462 ret = btrfs_read_buffer(tmp, gen);
2463 if (!ret) {
2464 *eb_ret = tmp;
2465 return 0;
2467 free_extent_buffer(tmp);
2468 btrfs_release_path(p);
2469 return -EIO;
2473 * reduce lock contention at high levels
2474 * of the btree by dropping locks before
2475 * we read. Don't release the lock on the current
2476 * level because we need to walk this node to figure
2477 * out which blocks to read.
2479 btrfs_unlock_up_safe(p, level + 1);
2480 btrfs_set_path_blocking(p);
2482 free_extent_buffer(tmp);
2483 if (p->reada != READA_NONE)
2484 reada_for_search(fs_info, p, level, slot, key->objectid);
2486 ret = -EAGAIN;
2487 tmp = read_tree_block(fs_info, blocknr, gen);
2488 if (!IS_ERR(tmp)) {
2490 * If the read above didn't mark this buffer up to date,
2491 * it will never end up being up to date. Set ret to EIO now
2492 * and give up so that our caller doesn't loop forever
2493 * on our EAGAINs.
2495 if (!btrfs_buffer_uptodate(tmp, 0, 0))
2496 ret = -EIO;
2497 free_extent_buffer(tmp);
2498 } else {
2499 ret = PTR_ERR(tmp);
2502 btrfs_release_path(p);
2503 return ret;
2507 * helper function for btrfs_search_slot. This does all of the checks
2508 * for node-level blocks and does any balancing required based on
2509 * the ins_len.
2511 * If no extra work was required, zero is returned. If we had to
2512 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2513 * start over
2515 static int
2516 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2517 struct btrfs_root *root, struct btrfs_path *p,
2518 struct extent_buffer *b, int level, int ins_len,
2519 int *write_lock_level)
2521 struct btrfs_fs_info *fs_info = root->fs_info;
2522 int ret;
2524 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2525 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
2526 int sret;
2528 if (*write_lock_level < level + 1) {
2529 *write_lock_level = level + 1;
2530 btrfs_release_path(p);
2531 goto again;
2534 btrfs_set_path_blocking(p);
2535 reada_for_balance(fs_info, p, level);
2536 sret = split_node(trans, root, p, level);
2537 btrfs_clear_path_blocking(p, NULL, 0);
2539 BUG_ON(sret > 0);
2540 if (sret) {
2541 ret = sret;
2542 goto done;
2544 b = p->nodes[level];
2545 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2546 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
2547 int sret;
2549 if (*write_lock_level < level + 1) {
2550 *write_lock_level = level + 1;
2551 btrfs_release_path(p);
2552 goto again;
2555 btrfs_set_path_blocking(p);
2556 reada_for_balance(fs_info, p, level);
2557 sret = balance_level(trans, root, p, level);
2558 btrfs_clear_path_blocking(p, NULL, 0);
2560 if (sret) {
2561 ret = sret;
2562 goto done;
2564 b = p->nodes[level];
2565 if (!b) {
2566 btrfs_release_path(p);
2567 goto again;
2569 BUG_ON(btrfs_header_nritems(b) == 1);
2571 return 0;
2573 again:
2574 ret = -EAGAIN;
2575 done:
2576 return ret;
2579 static void key_search_validate(struct extent_buffer *b,
2580 const struct btrfs_key *key,
2581 int level)
2583 #ifdef CONFIG_BTRFS_ASSERT
2584 struct btrfs_disk_key disk_key;
2586 btrfs_cpu_key_to_disk(&disk_key, key);
2588 if (level == 0)
2589 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2590 offsetof(struct btrfs_leaf, items[0].key),
2591 sizeof(disk_key)));
2592 else
2593 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2594 offsetof(struct btrfs_node, ptrs[0].key),
2595 sizeof(disk_key)));
2596 #endif
2599 static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
2600 int level, int *prev_cmp, int *slot)
2602 if (*prev_cmp != 0) {
2603 *prev_cmp = bin_search(b, key, level, slot);
2604 return *prev_cmp;
2607 key_search_validate(b, key, level);
2608 *slot = 0;
2610 return 0;
2613 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2614 u64 iobjectid, u64 ioff, u8 key_type,
2615 struct btrfs_key *found_key)
2617 int ret;
2618 struct btrfs_key key;
2619 struct extent_buffer *eb;
2621 ASSERT(path);
2622 ASSERT(found_key);
2624 key.type = key_type;
2625 key.objectid = iobjectid;
2626 key.offset = ioff;
2628 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
2629 if (ret < 0)
2630 return ret;
2632 eb = path->nodes[0];
2633 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2634 ret = btrfs_next_leaf(fs_root, path);
2635 if (ret)
2636 return ret;
2637 eb = path->nodes[0];
2640 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2641 if (found_key->type != key.type ||
2642 found_key->objectid != key.objectid)
2643 return 1;
2645 return 0;
2649 * look for key in the tree. path is filled in with nodes along the way
2650 * if key is found, we return zero and you can find the item in the leaf
2651 * level of the path (level 0)
2653 * If the key isn't found, the path points to the slot where it should
2654 * be inserted, and 1 is returned. If there are other errors during the
2655 * search a negative error number is returned.
2657 * if ins_len > 0, nodes and leaves will be split as we walk down the
2658 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2659 * possible)
2661 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2662 const struct btrfs_key *key, struct btrfs_path *p,
2663 int ins_len, int cow)
2665 struct btrfs_fs_info *fs_info = root->fs_info;
2666 struct extent_buffer *b;
2667 int slot;
2668 int ret;
2669 int err;
2670 int level;
2671 int lowest_unlock = 1;
2672 int root_lock;
2673 /* everything at write_lock_level or lower must be write locked */
2674 int write_lock_level = 0;
2675 u8 lowest_level = 0;
2676 int min_write_lock_level;
2677 int prev_cmp;
2679 lowest_level = p->lowest_level;
2680 WARN_ON(lowest_level && ins_len > 0);
2681 WARN_ON(p->nodes[0] != NULL);
2682 BUG_ON(!cow && ins_len);
2684 if (ins_len < 0) {
2685 lowest_unlock = 2;
2687 /* when we are removing items, we might have to go up to level
2688 * two as we update tree pointers Make sure we keep write
2689 * for those levels as well
2691 write_lock_level = 2;
2692 } else if (ins_len > 0) {
2694 * for inserting items, make sure we have a write lock on
2695 * level 1 so we can update keys
2697 write_lock_level = 1;
2700 if (!cow)
2701 write_lock_level = -1;
2703 if (cow && (p->keep_locks || p->lowest_level))
2704 write_lock_level = BTRFS_MAX_LEVEL;
2706 min_write_lock_level = write_lock_level;
2708 again:
2709 prev_cmp = -1;
2711 * we try very hard to do read locks on the root
2713 root_lock = BTRFS_READ_LOCK;
2714 level = 0;
2715 if (p->search_commit_root) {
2717 * the commit roots are read only
2718 * so we always do read locks
2720 if (p->need_commit_sem)
2721 down_read(&fs_info->commit_root_sem);
2722 b = root->commit_root;
2723 extent_buffer_get(b);
2724 level = btrfs_header_level(b);
2725 if (p->need_commit_sem)
2726 up_read(&fs_info->commit_root_sem);
2727 if (!p->skip_locking)
2728 btrfs_tree_read_lock(b);
2729 } else {
2730 if (p->skip_locking) {
2731 b = btrfs_root_node(root);
2732 level = btrfs_header_level(b);
2733 } else {
2734 /* we don't know the level of the root node
2735 * until we actually have it read locked
2737 b = btrfs_read_lock_root_node(root);
2738 level = btrfs_header_level(b);
2739 if (level <= write_lock_level) {
2740 /* whoops, must trade for write lock */
2741 btrfs_tree_read_unlock(b);
2742 free_extent_buffer(b);
2743 b = btrfs_lock_root_node(root);
2744 root_lock = BTRFS_WRITE_LOCK;
2746 /* the level might have changed, check again */
2747 level = btrfs_header_level(b);
2751 p->nodes[level] = b;
2752 if (!p->skip_locking)
2753 p->locks[level] = root_lock;
2755 while (b) {
2756 level = btrfs_header_level(b);
2759 * setup the path here so we can release it under lock
2760 * contention with the cow code
2762 if (cow) {
2763 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2766 * if we don't really need to cow this block
2767 * then we don't want to set the path blocking,
2768 * so we test it here
2770 if (!should_cow_block(trans, root, b)) {
2771 trans->dirty = true;
2772 goto cow_done;
2776 * must have write locks on this node and the
2777 * parent
2779 if (level > write_lock_level ||
2780 (level + 1 > write_lock_level &&
2781 level + 1 < BTRFS_MAX_LEVEL &&
2782 p->nodes[level + 1])) {
2783 write_lock_level = level + 1;
2784 btrfs_release_path(p);
2785 goto again;
2788 btrfs_set_path_blocking(p);
2789 if (last_level)
2790 err = btrfs_cow_block(trans, root, b, NULL, 0,
2791 &b);
2792 else
2793 err = btrfs_cow_block(trans, root, b,
2794 p->nodes[level + 1],
2795 p->slots[level + 1], &b);
2796 if (err) {
2797 ret = err;
2798 goto done;
2801 cow_done:
2802 p->nodes[level] = b;
2803 btrfs_clear_path_blocking(p, NULL, 0);
2806 * we have a lock on b and as long as we aren't changing
2807 * the tree, there is no way to for the items in b to change.
2808 * It is safe to drop the lock on our parent before we
2809 * go through the expensive btree search on b.
2811 * If we're inserting or deleting (ins_len != 0), then we might
2812 * be changing slot zero, which may require changing the parent.
2813 * So, we can't drop the lock until after we know which slot
2814 * we're operating on.
2816 if (!ins_len && !p->keep_locks) {
2817 int u = level + 1;
2819 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2820 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2821 p->locks[u] = 0;
2825 ret = key_search(b, key, level, &prev_cmp, &slot);
2826 if (ret < 0)
2827 goto done;
2829 if (level != 0) {
2830 int dec = 0;
2831 if (ret && slot > 0) {
2832 dec = 1;
2833 slot -= 1;
2835 p->slots[level] = slot;
2836 err = setup_nodes_for_search(trans, root, p, b, level,
2837 ins_len, &write_lock_level);
2838 if (err == -EAGAIN)
2839 goto again;
2840 if (err) {
2841 ret = err;
2842 goto done;
2844 b = p->nodes[level];
2845 slot = p->slots[level];
2848 * slot 0 is special, if we change the key
2849 * we have to update the parent pointer
2850 * which means we must have a write lock
2851 * on the parent
2853 if (slot == 0 && ins_len &&
2854 write_lock_level < level + 1) {
2855 write_lock_level = level + 1;
2856 btrfs_release_path(p);
2857 goto again;
2860 unlock_up(p, level, lowest_unlock,
2861 min_write_lock_level, &write_lock_level);
2863 if (level == lowest_level) {
2864 if (dec)
2865 p->slots[level]++;
2866 goto done;
2869 err = read_block_for_search(root, p, &b, level,
2870 slot, key);
2871 if (err == -EAGAIN)
2872 goto again;
2873 if (err) {
2874 ret = err;
2875 goto done;
2878 if (!p->skip_locking) {
2879 level = btrfs_header_level(b);
2880 if (level <= write_lock_level) {
2881 err = btrfs_try_tree_write_lock(b);
2882 if (!err) {
2883 btrfs_set_path_blocking(p);
2884 btrfs_tree_lock(b);
2885 btrfs_clear_path_blocking(p, b,
2886 BTRFS_WRITE_LOCK);
2888 p->locks[level] = BTRFS_WRITE_LOCK;
2889 } else {
2890 err = btrfs_tree_read_lock_atomic(b);
2891 if (!err) {
2892 btrfs_set_path_blocking(p);
2893 btrfs_tree_read_lock(b);
2894 btrfs_clear_path_blocking(p, b,
2895 BTRFS_READ_LOCK);
2897 p->locks[level] = BTRFS_READ_LOCK;
2899 p->nodes[level] = b;
2901 } else {
2902 p->slots[level] = slot;
2903 if (ins_len > 0 &&
2904 btrfs_leaf_free_space(fs_info, b) < ins_len) {
2905 if (write_lock_level < 1) {
2906 write_lock_level = 1;
2907 btrfs_release_path(p);
2908 goto again;
2911 btrfs_set_path_blocking(p);
2912 err = split_leaf(trans, root, key,
2913 p, ins_len, ret == 0);
2914 btrfs_clear_path_blocking(p, NULL, 0);
2916 BUG_ON(err > 0);
2917 if (err) {
2918 ret = err;
2919 goto done;
2922 if (!p->search_for_split)
2923 unlock_up(p, level, lowest_unlock,
2924 min_write_lock_level, &write_lock_level);
2925 goto done;
2928 ret = 1;
2929 done:
2931 * we don't really know what they plan on doing with the path
2932 * from here on, so for now just mark it as blocking
2934 if (!p->leave_spinning)
2935 btrfs_set_path_blocking(p);
2936 if (ret < 0 && !p->skip_release_on_error)
2937 btrfs_release_path(p);
2938 return ret;
2942 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2943 * current state of the tree together with the operations recorded in the tree
2944 * modification log to search for the key in a previous version of this tree, as
2945 * denoted by the time_seq parameter.
2947 * Naturally, there is no support for insert, delete or cow operations.
2949 * The resulting path and return value will be set up as if we called
2950 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2952 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2953 struct btrfs_path *p, u64 time_seq)
2955 struct btrfs_fs_info *fs_info = root->fs_info;
2956 struct extent_buffer *b;
2957 int slot;
2958 int ret;
2959 int err;
2960 int level;
2961 int lowest_unlock = 1;
2962 u8 lowest_level = 0;
2963 int prev_cmp = -1;
2965 lowest_level = p->lowest_level;
2966 WARN_ON(p->nodes[0] != NULL);
2968 if (p->search_commit_root) {
2969 BUG_ON(time_seq);
2970 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2973 again:
2974 b = get_old_root(root, time_seq);
2975 if (!b) {
2976 ret = -EIO;
2977 goto done;
2979 level = btrfs_header_level(b);
2980 p->locks[level] = BTRFS_READ_LOCK;
2982 while (b) {
2983 level = btrfs_header_level(b);
2984 p->nodes[level] = b;
2985 btrfs_clear_path_blocking(p, NULL, 0);
2988 * we have a lock on b and as long as we aren't changing
2989 * the tree, there is no way to for the items in b to change.
2990 * It is safe to drop the lock on our parent before we
2991 * go through the expensive btree search on b.
2993 btrfs_unlock_up_safe(p, level + 1);
2996 * Since we can unwind ebs we want to do a real search every
2997 * time.
2999 prev_cmp = -1;
3000 ret = key_search(b, key, level, &prev_cmp, &slot);
3002 if (level != 0) {
3003 int dec = 0;
3004 if (ret && slot > 0) {
3005 dec = 1;
3006 slot -= 1;
3008 p->slots[level] = slot;
3009 unlock_up(p, level, lowest_unlock, 0, NULL);
3011 if (level == lowest_level) {
3012 if (dec)
3013 p->slots[level]++;
3014 goto done;
3017 err = read_block_for_search(root, p, &b, level,
3018 slot, key);
3019 if (err == -EAGAIN)
3020 goto again;
3021 if (err) {
3022 ret = err;
3023 goto done;
3026 level = btrfs_header_level(b);
3027 err = btrfs_tree_read_lock_atomic(b);
3028 if (!err) {
3029 btrfs_set_path_blocking(p);
3030 btrfs_tree_read_lock(b);
3031 btrfs_clear_path_blocking(p, b,
3032 BTRFS_READ_LOCK);
3034 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3035 if (!b) {
3036 ret = -ENOMEM;
3037 goto done;
3039 p->locks[level] = BTRFS_READ_LOCK;
3040 p->nodes[level] = b;
3041 } else {
3042 p->slots[level] = slot;
3043 unlock_up(p, level, lowest_unlock, 0, NULL);
3044 goto done;
3047 ret = 1;
3048 done:
3049 if (!p->leave_spinning)
3050 btrfs_set_path_blocking(p);
3051 if (ret < 0)
3052 btrfs_release_path(p);
3054 return ret;
3058 * helper to use instead of search slot if no exact match is needed but
3059 * instead the next or previous item should be returned.
3060 * When find_higher is true, the next higher item is returned, the next lower
3061 * otherwise.
3062 * When return_any and find_higher are both true, and no higher item is found,
3063 * return the next lower instead.
3064 * When return_any is true and find_higher is false, and no lower item is found,
3065 * return the next higher instead.
3066 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3067 * < 0 on error
3069 int btrfs_search_slot_for_read(struct btrfs_root *root,
3070 const struct btrfs_key *key,
3071 struct btrfs_path *p, int find_higher,
3072 int return_any)
3074 int ret;
3075 struct extent_buffer *leaf;
3077 again:
3078 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3079 if (ret <= 0)
3080 return ret;
3082 * a return value of 1 means the path is at the position where the
3083 * item should be inserted. Normally this is the next bigger item,
3084 * but in case the previous item is the last in a leaf, path points
3085 * to the first free slot in the previous leaf, i.e. at an invalid
3086 * item.
3088 leaf = p->nodes[0];
3090 if (find_higher) {
3091 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3092 ret = btrfs_next_leaf(root, p);
3093 if (ret <= 0)
3094 return ret;
3095 if (!return_any)
3096 return 1;
3098 * no higher item found, return the next
3099 * lower instead
3101 return_any = 0;
3102 find_higher = 0;
3103 btrfs_release_path(p);
3104 goto again;
3106 } else {
3107 if (p->slots[0] == 0) {
3108 ret = btrfs_prev_leaf(root, p);
3109 if (ret < 0)
3110 return ret;
3111 if (!ret) {
3112 leaf = p->nodes[0];
3113 if (p->slots[0] == btrfs_header_nritems(leaf))
3114 p->slots[0]--;
3115 return 0;
3117 if (!return_any)
3118 return 1;
3120 * no lower item found, return the next
3121 * higher instead
3123 return_any = 0;
3124 find_higher = 1;
3125 btrfs_release_path(p);
3126 goto again;
3127 } else {
3128 --p->slots[0];
3131 return 0;
3135 * adjust the pointers going up the tree, starting at level
3136 * making sure the right key of each node is points to 'key'.
3137 * This is used after shifting pointers to the left, so it stops
3138 * fixing up pointers when a given leaf/node is not in slot 0 of the
3139 * higher levels
3142 static void fixup_low_keys(struct btrfs_fs_info *fs_info,
3143 struct btrfs_path *path,
3144 struct btrfs_disk_key *key, int level)
3146 int i;
3147 struct extent_buffer *t;
3149 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
3150 int tslot = path->slots[i];
3151 if (!path->nodes[i])
3152 break;
3153 t = path->nodes[i];
3154 tree_mod_log_set_node_key(fs_info, t, tslot, 1);
3155 btrfs_set_node_key(t, key, tslot);
3156 btrfs_mark_buffer_dirty(path->nodes[i]);
3157 if (tslot != 0)
3158 break;
3163 * update item key.
3165 * This function isn't completely safe. It's the caller's responsibility
3166 * that the new key won't break the order
3168 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3169 struct btrfs_path *path,
3170 const struct btrfs_key *new_key)
3172 struct btrfs_disk_key disk_key;
3173 struct extent_buffer *eb;
3174 int slot;
3176 eb = path->nodes[0];
3177 slot = path->slots[0];
3178 if (slot > 0) {
3179 btrfs_item_key(eb, &disk_key, slot - 1);
3180 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
3182 if (slot < btrfs_header_nritems(eb) - 1) {
3183 btrfs_item_key(eb, &disk_key, slot + 1);
3184 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
3187 btrfs_cpu_key_to_disk(&disk_key, new_key);
3188 btrfs_set_item_key(eb, &disk_key, slot);
3189 btrfs_mark_buffer_dirty(eb);
3190 if (slot == 0)
3191 fixup_low_keys(fs_info, path, &disk_key, 1);
3195 * try to push data from one node into the next node left in the
3196 * tree.
3198 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3199 * error, and > 0 if there was no room in the left hand block.
3201 static int push_node_left(struct btrfs_trans_handle *trans,
3202 struct btrfs_fs_info *fs_info,
3203 struct extent_buffer *dst,
3204 struct extent_buffer *src, int empty)
3206 int push_items = 0;
3207 int src_nritems;
3208 int dst_nritems;
3209 int ret = 0;
3211 src_nritems = btrfs_header_nritems(src);
3212 dst_nritems = btrfs_header_nritems(dst);
3213 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3214 WARN_ON(btrfs_header_generation(src) != trans->transid);
3215 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3217 if (!empty && src_nritems <= 8)
3218 return 1;
3220 if (push_items <= 0)
3221 return 1;
3223 if (empty) {
3224 push_items = min(src_nritems, push_items);
3225 if (push_items < src_nritems) {
3226 /* leave at least 8 pointers in the node if
3227 * we aren't going to empty it
3229 if (src_nritems - push_items < 8) {
3230 if (push_items <= 8)
3231 return 1;
3232 push_items -= 8;
3235 } else
3236 push_items = min(src_nritems - 8, push_items);
3238 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
3239 push_items);
3240 if (ret) {
3241 btrfs_abort_transaction(trans, ret);
3242 return ret;
3244 copy_extent_buffer(dst, src,
3245 btrfs_node_key_ptr_offset(dst_nritems),
3246 btrfs_node_key_ptr_offset(0),
3247 push_items * sizeof(struct btrfs_key_ptr));
3249 if (push_items < src_nritems) {
3251 * don't call tree_mod_log_eb_move here, key removal was already
3252 * fully logged by tree_mod_log_eb_copy above.
3254 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3255 btrfs_node_key_ptr_offset(push_items),
3256 (src_nritems - push_items) *
3257 sizeof(struct btrfs_key_ptr));
3259 btrfs_set_header_nritems(src, src_nritems - push_items);
3260 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3261 btrfs_mark_buffer_dirty(src);
3262 btrfs_mark_buffer_dirty(dst);
3264 return ret;
3268 * try to push data from one node into the next node right in the
3269 * tree.
3271 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3272 * error, and > 0 if there was no room in the right hand block.
3274 * this will only push up to 1/2 the contents of the left node over
3276 static int balance_node_right(struct btrfs_trans_handle *trans,
3277 struct btrfs_fs_info *fs_info,
3278 struct extent_buffer *dst,
3279 struct extent_buffer *src)
3281 int push_items = 0;
3282 int max_push;
3283 int src_nritems;
3284 int dst_nritems;
3285 int ret = 0;
3287 WARN_ON(btrfs_header_generation(src) != trans->transid);
3288 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3290 src_nritems = btrfs_header_nritems(src);
3291 dst_nritems = btrfs_header_nritems(dst);
3292 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3293 if (push_items <= 0)
3294 return 1;
3296 if (src_nritems < 4)
3297 return 1;
3299 max_push = src_nritems / 2 + 1;
3300 /* don't try to empty the node */
3301 if (max_push >= src_nritems)
3302 return 1;
3304 if (max_push < push_items)
3305 push_items = max_push;
3307 tree_mod_log_eb_move(fs_info, dst, push_items, 0, dst_nritems);
3308 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3309 btrfs_node_key_ptr_offset(0),
3310 (dst_nritems) *
3311 sizeof(struct btrfs_key_ptr));
3313 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
3314 src_nritems - push_items, push_items);
3315 if (ret) {
3316 btrfs_abort_transaction(trans, ret);
3317 return ret;
3319 copy_extent_buffer(dst, src,
3320 btrfs_node_key_ptr_offset(0),
3321 btrfs_node_key_ptr_offset(src_nritems - push_items),
3322 push_items * sizeof(struct btrfs_key_ptr));
3324 btrfs_set_header_nritems(src, src_nritems - push_items);
3325 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3327 btrfs_mark_buffer_dirty(src);
3328 btrfs_mark_buffer_dirty(dst);
3330 return ret;
3334 * helper function to insert a new root level in the tree.
3335 * A new node is allocated, and a single item is inserted to
3336 * point to the existing root
3338 * returns zero on success or < 0 on failure.
3340 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3341 struct btrfs_root *root,
3342 struct btrfs_path *path, int level)
3344 struct btrfs_fs_info *fs_info = root->fs_info;
3345 u64 lower_gen;
3346 struct extent_buffer *lower;
3347 struct extent_buffer *c;
3348 struct extent_buffer *old;
3349 struct btrfs_disk_key lower_key;
3351 BUG_ON(path->nodes[level]);
3352 BUG_ON(path->nodes[level-1] != root->node);
3354 lower = path->nodes[level-1];
3355 if (level == 1)
3356 btrfs_item_key(lower, &lower_key, 0);
3357 else
3358 btrfs_node_key(lower, &lower_key, 0);
3360 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3361 &lower_key, level, root->node->start, 0);
3362 if (IS_ERR(c))
3363 return PTR_ERR(c);
3365 root_add_used(root, fs_info->nodesize);
3367 memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
3368 btrfs_set_header_nritems(c, 1);
3369 btrfs_set_header_level(c, level);
3370 btrfs_set_header_bytenr(c, c->start);
3371 btrfs_set_header_generation(c, trans->transid);
3372 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
3373 btrfs_set_header_owner(c, root->root_key.objectid);
3375 write_extent_buffer_fsid(c, fs_info->fsid);
3376 write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
3378 btrfs_set_node_key(c, &lower_key, 0);
3379 btrfs_set_node_blockptr(c, 0, lower->start);
3380 lower_gen = btrfs_header_generation(lower);
3381 WARN_ON(lower_gen != trans->transid);
3383 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3385 btrfs_mark_buffer_dirty(c);
3387 old = root->node;
3388 tree_mod_log_set_root_pointer(root, c, 0);
3389 rcu_assign_pointer(root->node, c);
3391 /* the super has an extra ref to root->node */
3392 free_extent_buffer(old);
3394 add_root_to_dirty_list(root);
3395 extent_buffer_get(c);
3396 path->nodes[level] = c;
3397 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
3398 path->slots[level] = 0;
3399 return 0;
3403 * worker function to insert a single pointer in a node.
3404 * the node should have enough room for the pointer already
3406 * slot and level indicate where you want the key to go, and
3407 * blocknr is the block the key points to.
3409 static void insert_ptr(struct btrfs_trans_handle *trans,
3410 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
3411 struct btrfs_disk_key *key, u64 bytenr,
3412 int slot, int level)
3414 struct extent_buffer *lower;
3415 int nritems;
3416 int ret;
3418 BUG_ON(!path->nodes[level]);
3419 btrfs_assert_tree_locked(path->nodes[level]);
3420 lower = path->nodes[level];
3421 nritems = btrfs_header_nritems(lower);
3422 BUG_ON(slot > nritems);
3423 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
3424 if (slot != nritems) {
3425 if (level)
3426 tree_mod_log_eb_move(fs_info, lower, slot + 1,
3427 slot, nritems - slot);
3428 memmove_extent_buffer(lower,
3429 btrfs_node_key_ptr_offset(slot + 1),
3430 btrfs_node_key_ptr_offset(slot),
3431 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3433 if (level) {
3434 ret = tree_mod_log_insert_key(fs_info, lower, slot,
3435 MOD_LOG_KEY_ADD, GFP_NOFS);
3436 BUG_ON(ret < 0);
3438 btrfs_set_node_key(lower, key, slot);
3439 btrfs_set_node_blockptr(lower, slot, bytenr);
3440 WARN_ON(trans->transid == 0);
3441 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3442 btrfs_set_header_nritems(lower, nritems + 1);
3443 btrfs_mark_buffer_dirty(lower);
3447 * split the node at the specified level in path in two.
3448 * The path is corrected to point to the appropriate node after the split
3450 * Before splitting this tries to make some room in the node by pushing
3451 * left and right, if either one works, it returns right away.
3453 * returns 0 on success and < 0 on failure
3455 static noinline int split_node(struct btrfs_trans_handle *trans,
3456 struct btrfs_root *root,
3457 struct btrfs_path *path, int level)
3459 struct btrfs_fs_info *fs_info = root->fs_info;
3460 struct extent_buffer *c;
3461 struct extent_buffer *split;
3462 struct btrfs_disk_key disk_key;
3463 int mid;
3464 int ret;
3465 u32 c_nritems;
3467 c = path->nodes[level];
3468 WARN_ON(btrfs_header_generation(c) != trans->transid);
3469 if (c == root->node) {
3471 * trying to split the root, lets make a new one
3473 * tree mod log: We don't log_removal old root in
3474 * insert_new_root, because that root buffer will be kept as a
3475 * normal node. We are going to log removal of half of the
3476 * elements below with tree_mod_log_eb_copy. We're holding a
3477 * tree lock on the buffer, which is why we cannot race with
3478 * other tree_mod_log users.
3480 ret = insert_new_root(trans, root, path, level + 1);
3481 if (ret)
3482 return ret;
3483 } else {
3484 ret = push_nodes_for_insert(trans, root, path, level);
3485 c = path->nodes[level];
3486 if (!ret && btrfs_header_nritems(c) <
3487 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3488 return 0;
3489 if (ret < 0)
3490 return ret;
3493 c_nritems = btrfs_header_nritems(c);
3494 mid = (c_nritems + 1) / 2;
3495 btrfs_node_key(c, &disk_key, mid);
3497 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3498 &disk_key, level, c->start, 0);
3499 if (IS_ERR(split))
3500 return PTR_ERR(split);
3502 root_add_used(root, fs_info->nodesize);
3504 memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
3505 btrfs_set_header_level(split, btrfs_header_level(c));
3506 btrfs_set_header_bytenr(split, split->start);
3507 btrfs_set_header_generation(split, trans->transid);
3508 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
3509 btrfs_set_header_owner(split, root->root_key.objectid);
3510 write_extent_buffer_fsid(split, fs_info->fsid);
3511 write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
3513 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
3514 if (ret) {
3515 btrfs_abort_transaction(trans, ret);
3516 return ret;
3518 copy_extent_buffer(split, c,
3519 btrfs_node_key_ptr_offset(0),
3520 btrfs_node_key_ptr_offset(mid),
3521 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3522 btrfs_set_header_nritems(split, c_nritems - mid);
3523 btrfs_set_header_nritems(c, mid);
3524 ret = 0;
3526 btrfs_mark_buffer_dirty(c);
3527 btrfs_mark_buffer_dirty(split);
3529 insert_ptr(trans, fs_info, path, &disk_key, split->start,
3530 path->slots[level + 1] + 1, level + 1);
3532 if (path->slots[level] >= mid) {
3533 path->slots[level] -= mid;
3534 btrfs_tree_unlock(c);
3535 free_extent_buffer(c);
3536 path->nodes[level] = split;
3537 path->slots[level + 1] += 1;
3538 } else {
3539 btrfs_tree_unlock(split);
3540 free_extent_buffer(split);
3542 return ret;
3546 * how many bytes are required to store the items in a leaf. start
3547 * and nr indicate which items in the leaf to check. This totals up the
3548 * space used both by the item structs and the item data
3550 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3552 struct btrfs_item *start_item;
3553 struct btrfs_item *end_item;
3554 struct btrfs_map_token token;
3555 int data_len;
3556 int nritems = btrfs_header_nritems(l);
3557 int end = min(nritems, start + nr) - 1;
3559 if (!nr)
3560 return 0;
3561 btrfs_init_map_token(&token);
3562 start_item = btrfs_item_nr(start);
3563 end_item = btrfs_item_nr(end);
3564 data_len = btrfs_token_item_offset(l, start_item, &token) +
3565 btrfs_token_item_size(l, start_item, &token);
3566 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
3567 data_len += sizeof(struct btrfs_item) * nr;
3568 WARN_ON(data_len < 0);
3569 return data_len;
3573 * The space between the end of the leaf items and
3574 * the start of the leaf data. IOW, how much room
3575 * the leaf has left for both items and data
3577 noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
3578 struct extent_buffer *leaf)
3580 int nritems = btrfs_header_nritems(leaf);
3581 int ret;
3583 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3584 if (ret < 0) {
3585 btrfs_crit(fs_info,
3586 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3587 ret,
3588 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3589 leaf_space_used(leaf, 0, nritems), nritems);
3591 return ret;
3595 * min slot controls the lowest index we're willing to push to the
3596 * right. We'll push up to and including min_slot, but no lower
3598 static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
3599 struct btrfs_path *path,
3600 int data_size, int empty,
3601 struct extent_buffer *right,
3602 int free_space, u32 left_nritems,
3603 u32 min_slot)
3605 struct extent_buffer *left = path->nodes[0];
3606 struct extent_buffer *upper = path->nodes[1];
3607 struct btrfs_map_token token;
3608 struct btrfs_disk_key disk_key;
3609 int slot;
3610 u32 i;
3611 int push_space = 0;
3612 int push_items = 0;
3613 struct btrfs_item *item;
3614 u32 nr;
3615 u32 right_nritems;
3616 u32 data_end;
3617 u32 this_item_size;
3619 btrfs_init_map_token(&token);
3621 if (empty)
3622 nr = 0;
3623 else
3624 nr = max_t(u32, 1, min_slot);
3626 if (path->slots[0] >= left_nritems)
3627 push_space += data_size;
3629 slot = path->slots[1];
3630 i = left_nritems - 1;
3631 while (i >= nr) {
3632 item = btrfs_item_nr(i);
3634 if (!empty && push_items > 0) {
3635 if (path->slots[0] > i)
3636 break;
3637 if (path->slots[0] == i) {
3638 int space = btrfs_leaf_free_space(fs_info, left);
3639 if (space + push_space * 2 > free_space)
3640 break;
3644 if (path->slots[0] == i)
3645 push_space += data_size;
3647 this_item_size = btrfs_item_size(left, item);
3648 if (this_item_size + sizeof(*item) + push_space > free_space)
3649 break;
3651 push_items++;
3652 push_space += this_item_size + sizeof(*item);
3653 if (i == 0)
3654 break;
3655 i--;
3658 if (push_items == 0)
3659 goto out_unlock;
3661 WARN_ON(!empty && push_items == left_nritems);
3663 /* push left to right */
3664 right_nritems = btrfs_header_nritems(right);
3666 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3667 push_space -= leaf_data_end(fs_info, left);
3669 /* make room in the right data area */
3670 data_end = leaf_data_end(fs_info, right);
3671 memmove_extent_buffer(right,
3672 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3673 BTRFS_LEAF_DATA_OFFSET + data_end,
3674 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3676 /* copy from the left data area */
3677 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
3678 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3679 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
3680 push_space);
3682 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3683 btrfs_item_nr_offset(0),
3684 right_nritems * sizeof(struct btrfs_item));
3686 /* copy the items from left to right */
3687 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3688 btrfs_item_nr_offset(left_nritems - push_items),
3689 push_items * sizeof(struct btrfs_item));
3691 /* update the item pointers */
3692 right_nritems += push_items;
3693 btrfs_set_header_nritems(right, right_nritems);
3694 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3695 for (i = 0; i < right_nritems; i++) {
3696 item = btrfs_item_nr(i);
3697 push_space -= btrfs_token_item_size(right, item, &token);
3698 btrfs_set_token_item_offset(right, item, push_space, &token);
3701 left_nritems -= push_items;
3702 btrfs_set_header_nritems(left, left_nritems);
3704 if (left_nritems)
3705 btrfs_mark_buffer_dirty(left);
3706 else
3707 clean_tree_block(fs_info, left);
3709 btrfs_mark_buffer_dirty(right);
3711 btrfs_item_key(right, &disk_key, 0);
3712 btrfs_set_node_key(upper, &disk_key, slot + 1);
3713 btrfs_mark_buffer_dirty(upper);
3715 /* then fixup the leaf pointer in the path */
3716 if (path->slots[0] >= left_nritems) {
3717 path->slots[0] -= left_nritems;
3718 if (btrfs_header_nritems(path->nodes[0]) == 0)
3719 clean_tree_block(fs_info, path->nodes[0]);
3720 btrfs_tree_unlock(path->nodes[0]);
3721 free_extent_buffer(path->nodes[0]);
3722 path->nodes[0] = right;
3723 path->slots[1] += 1;
3724 } else {
3725 btrfs_tree_unlock(right);
3726 free_extent_buffer(right);
3728 return 0;
3730 out_unlock:
3731 btrfs_tree_unlock(right);
3732 free_extent_buffer(right);
3733 return 1;
3737 * push some data in the path leaf to the right, trying to free up at
3738 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3740 * returns 1 if the push failed because the other node didn't have enough
3741 * room, 0 if everything worked out and < 0 if there were major errors.
3743 * this will push starting from min_slot to the end of the leaf. It won't
3744 * push any slot lower than min_slot
3746 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3747 *root, struct btrfs_path *path,
3748 int min_data_size, int data_size,
3749 int empty, u32 min_slot)
3751 struct btrfs_fs_info *fs_info = root->fs_info;
3752 struct extent_buffer *left = path->nodes[0];
3753 struct extent_buffer *right;
3754 struct extent_buffer *upper;
3755 int slot;
3756 int free_space;
3757 u32 left_nritems;
3758 int ret;
3760 if (!path->nodes[1])
3761 return 1;
3763 slot = path->slots[1];
3764 upper = path->nodes[1];
3765 if (slot >= btrfs_header_nritems(upper) - 1)
3766 return 1;
3768 btrfs_assert_tree_locked(path->nodes[1]);
3770 right = read_node_slot(fs_info, upper, slot + 1);
3772 * slot + 1 is not valid or we fail to read the right node,
3773 * no big deal, just return.
3775 if (IS_ERR(right))
3776 return 1;
3778 btrfs_tree_lock(right);
3779 btrfs_set_lock_blocking(right);
3781 free_space = btrfs_leaf_free_space(fs_info, right);
3782 if (free_space < data_size)
3783 goto out_unlock;
3785 /* cow and double check */
3786 ret = btrfs_cow_block(trans, root, right, upper,
3787 slot + 1, &right);
3788 if (ret)
3789 goto out_unlock;
3791 free_space = btrfs_leaf_free_space(fs_info, right);
3792 if (free_space < data_size)
3793 goto out_unlock;
3795 left_nritems = btrfs_header_nritems(left);
3796 if (left_nritems == 0)
3797 goto out_unlock;
3799 if (path->slots[0] == left_nritems && !empty) {
3800 /* Key greater than all keys in the leaf, right neighbor has
3801 * enough room for it and we're not emptying our leaf to delete
3802 * it, therefore use right neighbor to insert the new item and
3803 * no need to touch/dirty our left leaft. */
3804 btrfs_tree_unlock(left);
3805 free_extent_buffer(left);
3806 path->nodes[0] = right;
3807 path->slots[0] = 0;
3808 path->slots[1]++;
3809 return 0;
3812 return __push_leaf_right(fs_info, path, min_data_size, empty,
3813 right, free_space, left_nritems, min_slot);
3814 out_unlock:
3815 btrfs_tree_unlock(right);
3816 free_extent_buffer(right);
3817 return 1;
3821 * push some data in the path leaf to the left, trying to free up at
3822 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3824 * max_slot can put a limit on how far into the leaf we'll push items. The
3825 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3826 * items
3828 static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
3829 struct btrfs_path *path, int data_size,
3830 int empty, struct extent_buffer *left,
3831 int free_space, u32 right_nritems,
3832 u32 max_slot)
3834 struct btrfs_disk_key disk_key;
3835 struct extent_buffer *right = path->nodes[0];
3836 int i;
3837 int push_space = 0;
3838 int push_items = 0;
3839 struct btrfs_item *item;
3840 u32 old_left_nritems;
3841 u32 nr;
3842 int ret = 0;
3843 u32 this_item_size;
3844 u32 old_left_item_size;
3845 struct btrfs_map_token token;
3847 btrfs_init_map_token(&token);
3849 if (empty)
3850 nr = min(right_nritems, max_slot);
3851 else
3852 nr = min(right_nritems - 1, max_slot);
3854 for (i = 0; i < nr; i++) {
3855 item = btrfs_item_nr(i);
3857 if (!empty && push_items > 0) {
3858 if (path->slots[0] < i)
3859 break;
3860 if (path->slots[0] == i) {
3861 int space = btrfs_leaf_free_space(fs_info, right);
3862 if (space + push_space * 2 > free_space)
3863 break;
3867 if (path->slots[0] == i)
3868 push_space += data_size;
3870 this_item_size = btrfs_item_size(right, item);
3871 if (this_item_size + sizeof(*item) + push_space > free_space)
3872 break;
3874 push_items++;
3875 push_space += this_item_size + sizeof(*item);
3878 if (push_items == 0) {
3879 ret = 1;
3880 goto out;
3882 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3884 /* push data from right to left */
3885 copy_extent_buffer(left, right,
3886 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3887 btrfs_item_nr_offset(0),
3888 push_items * sizeof(struct btrfs_item));
3890 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3891 btrfs_item_offset_nr(right, push_items - 1);
3893 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3894 leaf_data_end(fs_info, left) - push_space,
3895 BTRFS_LEAF_DATA_OFFSET +
3896 btrfs_item_offset_nr(right, push_items - 1),
3897 push_space);
3898 old_left_nritems = btrfs_header_nritems(left);
3899 BUG_ON(old_left_nritems <= 0);
3901 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3902 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3903 u32 ioff;
3905 item = btrfs_item_nr(i);
3907 ioff = btrfs_token_item_offset(left, item, &token);
3908 btrfs_set_token_item_offset(left, item,
3909 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
3910 &token);
3912 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3914 /* fixup right node */
3915 if (push_items > right_nritems)
3916 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3917 right_nritems);
3919 if (push_items < right_nritems) {
3920 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3921 leaf_data_end(fs_info, right);
3922 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
3923 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3924 BTRFS_LEAF_DATA_OFFSET +
3925 leaf_data_end(fs_info, right), push_space);
3927 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3928 btrfs_item_nr_offset(push_items),
3929 (btrfs_header_nritems(right) - push_items) *
3930 sizeof(struct btrfs_item));
3932 right_nritems -= push_items;
3933 btrfs_set_header_nritems(right, right_nritems);
3934 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3935 for (i = 0; i < right_nritems; i++) {
3936 item = btrfs_item_nr(i);
3938 push_space = push_space - btrfs_token_item_size(right,
3939 item, &token);
3940 btrfs_set_token_item_offset(right, item, push_space, &token);
3943 btrfs_mark_buffer_dirty(left);
3944 if (right_nritems)
3945 btrfs_mark_buffer_dirty(right);
3946 else
3947 clean_tree_block(fs_info, right);
3949 btrfs_item_key(right, &disk_key, 0);
3950 fixup_low_keys(fs_info, path, &disk_key, 1);
3952 /* then fixup the leaf pointer in the path */
3953 if (path->slots[0] < push_items) {
3954 path->slots[0] += old_left_nritems;
3955 btrfs_tree_unlock(path->nodes[0]);
3956 free_extent_buffer(path->nodes[0]);
3957 path->nodes[0] = left;
3958 path->slots[1] -= 1;
3959 } else {
3960 btrfs_tree_unlock(left);
3961 free_extent_buffer(left);
3962 path->slots[0] -= push_items;
3964 BUG_ON(path->slots[0] < 0);
3965 return ret;
3966 out:
3967 btrfs_tree_unlock(left);
3968 free_extent_buffer(left);
3969 return ret;
3973 * push some data in the path leaf to the left, trying to free up at
3974 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3976 * max_slot can put a limit on how far into the leaf we'll push items. The
3977 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3978 * items
3980 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3981 *root, struct btrfs_path *path, int min_data_size,
3982 int data_size, int empty, u32 max_slot)
3984 struct btrfs_fs_info *fs_info = root->fs_info;
3985 struct extent_buffer *right = path->nodes[0];
3986 struct extent_buffer *left;
3987 int slot;
3988 int free_space;
3989 u32 right_nritems;
3990 int ret = 0;
3992 slot = path->slots[1];
3993 if (slot == 0)
3994 return 1;
3995 if (!path->nodes[1])
3996 return 1;
3998 right_nritems = btrfs_header_nritems(right);
3999 if (right_nritems == 0)
4000 return 1;
4002 btrfs_assert_tree_locked(path->nodes[1]);
4004 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
4006 * slot - 1 is not valid or we fail to read the left node,
4007 * no big deal, just return.
4009 if (IS_ERR(left))
4010 return 1;
4012 btrfs_tree_lock(left);
4013 btrfs_set_lock_blocking(left);
4015 free_space = btrfs_leaf_free_space(fs_info, left);
4016 if (free_space < data_size) {
4017 ret = 1;
4018 goto out;
4021 /* cow and double check */
4022 ret = btrfs_cow_block(trans, root, left,
4023 path->nodes[1], slot - 1, &left);
4024 if (ret) {
4025 /* we hit -ENOSPC, but it isn't fatal here */
4026 if (ret == -ENOSPC)
4027 ret = 1;
4028 goto out;
4031 free_space = btrfs_leaf_free_space(fs_info, left);
4032 if (free_space < data_size) {
4033 ret = 1;
4034 goto out;
4037 return __push_leaf_left(fs_info, path, min_data_size,
4038 empty, left, free_space, right_nritems,
4039 max_slot);
4040 out:
4041 btrfs_tree_unlock(left);
4042 free_extent_buffer(left);
4043 return ret;
4047 * split the path's leaf in two, making sure there is at least data_size
4048 * available for the resulting leaf level of the path.
4050 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4051 struct btrfs_fs_info *fs_info,
4052 struct btrfs_path *path,
4053 struct extent_buffer *l,
4054 struct extent_buffer *right,
4055 int slot, int mid, int nritems)
4057 int data_copy_size;
4058 int rt_data_off;
4059 int i;
4060 struct btrfs_disk_key disk_key;
4061 struct btrfs_map_token token;
4063 btrfs_init_map_token(&token);
4065 nritems = nritems - mid;
4066 btrfs_set_header_nritems(right, nritems);
4067 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
4069 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4070 btrfs_item_nr_offset(mid),
4071 nritems * sizeof(struct btrfs_item));
4073 copy_extent_buffer(right, l,
4074 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4075 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
4076 leaf_data_end(fs_info, l), data_copy_size);
4078 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4080 for (i = 0; i < nritems; i++) {
4081 struct btrfs_item *item = btrfs_item_nr(i);
4082 u32 ioff;
4084 ioff = btrfs_token_item_offset(right, item, &token);
4085 btrfs_set_token_item_offset(right, item,
4086 ioff + rt_data_off, &token);
4089 btrfs_set_header_nritems(l, mid);
4090 btrfs_item_key(right, &disk_key, 0);
4091 insert_ptr(trans, fs_info, path, &disk_key, right->start,
4092 path->slots[1] + 1, 1);
4094 btrfs_mark_buffer_dirty(right);
4095 btrfs_mark_buffer_dirty(l);
4096 BUG_ON(path->slots[0] != slot);
4098 if (mid <= slot) {
4099 btrfs_tree_unlock(path->nodes[0]);
4100 free_extent_buffer(path->nodes[0]);
4101 path->nodes[0] = right;
4102 path->slots[0] -= mid;
4103 path->slots[1] += 1;
4104 } else {
4105 btrfs_tree_unlock(right);
4106 free_extent_buffer(right);
4109 BUG_ON(path->slots[0] < 0);
4113 * double splits happen when we need to insert a big item in the middle
4114 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4115 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4116 * A B C
4118 * We avoid this by trying to push the items on either side of our target
4119 * into the adjacent leaves. If all goes well we can avoid the double split
4120 * completely.
4122 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4123 struct btrfs_root *root,
4124 struct btrfs_path *path,
4125 int data_size)
4127 struct btrfs_fs_info *fs_info = root->fs_info;
4128 int ret;
4129 int progress = 0;
4130 int slot;
4131 u32 nritems;
4132 int space_needed = data_size;
4134 slot = path->slots[0];
4135 if (slot < btrfs_header_nritems(path->nodes[0]))
4136 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
4139 * try to push all the items after our slot into the
4140 * right leaf
4142 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
4143 if (ret < 0)
4144 return ret;
4146 if (ret == 0)
4147 progress++;
4149 nritems = btrfs_header_nritems(path->nodes[0]);
4151 * our goal is to get our slot at the start or end of a leaf. If
4152 * we've done so we're done
4154 if (path->slots[0] == 0 || path->slots[0] == nritems)
4155 return 0;
4157 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4158 return 0;
4160 /* try to push all the items before our slot into the next leaf */
4161 slot = path->slots[0];
4162 space_needed = data_size;
4163 if (slot > 0)
4164 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
4165 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
4166 if (ret < 0)
4167 return ret;
4169 if (ret == 0)
4170 progress++;
4172 if (progress)
4173 return 0;
4174 return 1;
4178 * split the path's leaf in two, making sure there is at least data_size
4179 * available for the resulting leaf level of the path.
4181 * returns 0 if all went well and < 0 on failure.
4183 static noinline int split_leaf(struct btrfs_trans_handle *trans,
4184 struct btrfs_root *root,
4185 const struct btrfs_key *ins_key,
4186 struct btrfs_path *path, int data_size,
4187 int extend)
4189 struct btrfs_disk_key disk_key;
4190 struct extent_buffer *l;
4191 u32 nritems;
4192 int mid;
4193 int slot;
4194 struct extent_buffer *right;
4195 struct btrfs_fs_info *fs_info = root->fs_info;
4196 int ret = 0;
4197 int wret;
4198 int split;
4199 int num_doubles = 0;
4200 int tried_avoid_double = 0;
4202 l = path->nodes[0];
4203 slot = path->slots[0];
4204 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4205 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
4206 return -EOVERFLOW;
4208 /* first try to make some room by pushing left and right */
4209 if (data_size && path->nodes[1]) {
4210 int space_needed = data_size;
4212 if (slot < btrfs_header_nritems(l))
4213 space_needed -= btrfs_leaf_free_space(fs_info, l);
4215 wret = push_leaf_right(trans, root, path, space_needed,
4216 space_needed, 0, 0);
4217 if (wret < 0)
4218 return wret;
4219 if (wret) {
4220 space_needed = data_size;
4221 if (slot > 0)
4222 space_needed -= btrfs_leaf_free_space(fs_info,
4224 wret = push_leaf_left(trans, root, path, space_needed,
4225 space_needed, 0, (u32)-1);
4226 if (wret < 0)
4227 return wret;
4229 l = path->nodes[0];
4231 /* did the pushes work? */
4232 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
4233 return 0;
4236 if (!path->nodes[1]) {
4237 ret = insert_new_root(trans, root, path, 1);
4238 if (ret)
4239 return ret;
4241 again:
4242 split = 1;
4243 l = path->nodes[0];
4244 slot = path->slots[0];
4245 nritems = btrfs_header_nritems(l);
4246 mid = (nritems + 1) / 2;
4248 if (mid <= slot) {
4249 if (nritems == 1 ||
4250 leaf_space_used(l, mid, nritems - mid) + data_size >
4251 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4252 if (slot >= nritems) {
4253 split = 0;
4254 } else {
4255 mid = slot;
4256 if (mid != nritems &&
4257 leaf_space_used(l, mid, nritems - mid) +
4258 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4259 if (data_size && !tried_avoid_double)
4260 goto push_for_double;
4261 split = 2;
4265 } else {
4266 if (leaf_space_used(l, 0, mid) + data_size >
4267 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4268 if (!extend && data_size && slot == 0) {
4269 split = 0;
4270 } else if ((extend || !data_size) && slot == 0) {
4271 mid = 1;
4272 } else {
4273 mid = slot;
4274 if (mid != nritems &&
4275 leaf_space_used(l, mid, nritems - mid) +
4276 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4277 if (data_size && !tried_avoid_double)
4278 goto push_for_double;
4279 split = 2;
4285 if (split == 0)
4286 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4287 else
4288 btrfs_item_key(l, &disk_key, mid);
4290 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4291 &disk_key, 0, l->start, 0);
4292 if (IS_ERR(right))
4293 return PTR_ERR(right);
4295 root_add_used(root, fs_info->nodesize);
4297 memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
4298 btrfs_set_header_bytenr(right, right->start);
4299 btrfs_set_header_generation(right, trans->transid);
4300 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
4301 btrfs_set_header_owner(right, root->root_key.objectid);
4302 btrfs_set_header_level(right, 0);
4303 write_extent_buffer_fsid(right, fs_info->fsid);
4304 write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid);
4306 if (split == 0) {
4307 if (mid <= slot) {
4308 btrfs_set_header_nritems(right, 0);
4309 insert_ptr(trans, fs_info, path, &disk_key,
4310 right->start, path->slots[1] + 1, 1);
4311 btrfs_tree_unlock(path->nodes[0]);
4312 free_extent_buffer(path->nodes[0]);
4313 path->nodes[0] = right;
4314 path->slots[0] = 0;
4315 path->slots[1] += 1;
4316 } else {
4317 btrfs_set_header_nritems(right, 0);
4318 insert_ptr(trans, fs_info, path, &disk_key,
4319 right->start, path->slots[1], 1);
4320 btrfs_tree_unlock(path->nodes[0]);
4321 free_extent_buffer(path->nodes[0]);
4322 path->nodes[0] = right;
4323 path->slots[0] = 0;
4324 if (path->slots[1] == 0)
4325 fixup_low_keys(fs_info, path, &disk_key, 1);
4328 * We create a new leaf 'right' for the required ins_len and
4329 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4330 * the content of ins_len to 'right'.
4332 return ret;
4335 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
4337 if (split == 2) {
4338 BUG_ON(num_doubles != 0);
4339 num_doubles++;
4340 goto again;
4343 return 0;
4345 push_for_double:
4346 push_for_double_split(trans, root, path, data_size);
4347 tried_avoid_double = 1;
4348 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4349 return 0;
4350 goto again;
4353 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4354 struct btrfs_root *root,
4355 struct btrfs_path *path, int ins_len)
4357 struct btrfs_fs_info *fs_info = root->fs_info;
4358 struct btrfs_key key;
4359 struct extent_buffer *leaf;
4360 struct btrfs_file_extent_item *fi;
4361 u64 extent_len = 0;
4362 u32 item_size;
4363 int ret;
4365 leaf = path->nodes[0];
4366 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4368 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4369 key.type != BTRFS_EXTENT_CSUM_KEY);
4371 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
4372 return 0;
4374 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4375 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4376 fi = btrfs_item_ptr(leaf, path->slots[0],
4377 struct btrfs_file_extent_item);
4378 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4380 btrfs_release_path(path);
4382 path->keep_locks = 1;
4383 path->search_for_split = 1;
4384 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4385 path->search_for_split = 0;
4386 if (ret > 0)
4387 ret = -EAGAIN;
4388 if (ret < 0)
4389 goto err;
4391 ret = -EAGAIN;
4392 leaf = path->nodes[0];
4393 /* if our item isn't there, return now */
4394 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4395 goto err;
4397 /* the leaf has changed, it now has room. return now */
4398 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
4399 goto err;
4401 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4402 fi = btrfs_item_ptr(leaf, path->slots[0],
4403 struct btrfs_file_extent_item);
4404 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4405 goto err;
4408 btrfs_set_path_blocking(path);
4409 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4410 if (ret)
4411 goto err;
4413 path->keep_locks = 0;
4414 btrfs_unlock_up_safe(path, 1);
4415 return 0;
4416 err:
4417 path->keep_locks = 0;
4418 return ret;
4421 static noinline int split_item(struct btrfs_fs_info *fs_info,
4422 struct btrfs_path *path,
4423 const struct btrfs_key *new_key,
4424 unsigned long split_offset)
4426 struct extent_buffer *leaf;
4427 struct btrfs_item *item;
4428 struct btrfs_item *new_item;
4429 int slot;
4430 char *buf;
4431 u32 nritems;
4432 u32 item_size;
4433 u32 orig_offset;
4434 struct btrfs_disk_key disk_key;
4436 leaf = path->nodes[0];
4437 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
4439 btrfs_set_path_blocking(path);
4441 item = btrfs_item_nr(path->slots[0]);
4442 orig_offset = btrfs_item_offset(leaf, item);
4443 item_size = btrfs_item_size(leaf, item);
4445 buf = kmalloc(item_size, GFP_NOFS);
4446 if (!buf)
4447 return -ENOMEM;
4449 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4450 path->slots[0]), item_size);
4452 slot = path->slots[0] + 1;
4453 nritems = btrfs_header_nritems(leaf);
4454 if (slot != nritems) {
4455 /* shift the items */
4456 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4457 btrfs_item_nr_offset(slot),
4458 (nritems - slot) * sizeof(struct btrfs_item));
4461 btrfs_cpu_key_to_disk(&disk_key, new_key);
4462 btrfs_set_item_key(leaf, &disk_key, slot);
4464 new_item = btrfs_item_nr(slot);
4466 btrfs_set_item_offset(leaf, new_item, orig_offset);
4467 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4469 btrfs_set_item_offset(leaf, item,
4470 orig_offset + item_size - split_offset);
4471 btrfs_set_item_size(leaf, item, split_offset);
4473 btrfs_set_header_nritems(leaf, nritems + 1);
4475 /* write the data for the start of the original item */
4476 write_extent_buffer(leaf, buf,
4477 btrfs_item_ptr_offset(leaf, path->slots[0]),
4478 split_offset);
4480 /* write the data for the new item */
4481 write_extent_buffer(leaf, buf + split_offset,
4482 btrfs_item_ptr_offset(leaf, slot),
4483 item_size - split_offset);
4484 btrfs_mark_buffer_dirty(leaf);
4486 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
4487 kfree(buf);
4488 return 0;
4492 * This function splits a single item into two items,
4493 * giving 'new_key' to the new item and splitting the
4494 * old one at split_offset (from the start of the item).
4496 * The path may be released by this operation. After
4497 * the split, the path is pointing to the old item. The
4498 * new item is going to be in the same node as the old one.
4500 * Note, the item being split must be smaller enough to live alone on
4501 * a tree block with room for one extra struct btrfs_item
4503 * This allows us to split the item in place, keeping a lock on the
4504 * leaf the entire time.
4506 int btrfs_split_item(struct btrfs_trans_handle *trans,
4507 struct btrfs_root *root,
4508 struct btrfs_path *path,
4509 const struct btrfs_key *new_key,
4510 unsigned long split_offset)
4512 int ret;
4513 ret = setup_leaf_for_split(trans, root, path,
4514 sizeof(struct btrfs_item));
4515 if (ret)
4516 return ret;
4518 ret = split_item(root->fs_info, path, new_key, split_offset);
4519 return ret;
4523 * This function duplicate a item, giving 'new_key' to the new item.
4524 * It guarantees both items live in the same tree leaf and the new item
4525 * is contiguous with the original item.
4527 * This allows us to split file extent in place, keeping a lock on the
4528 * leaf the entire time.
4530 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4531 struct btrfs_root *root,
4532 struct btrfs_path *path,
4533 const struct btrfs_key *new_key)
4535 struct extent_buffer *leaf;
4536 int ret;
4537 u32 item_size;
4539 leaf = path->nodes[0];
4540 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4541 ret = setup_leaf_for_split(trans, root, path,
4542 item_size + sizeof(struct btrfs_item));
4543 if (ret)
4544 return ret;
4546 path->slots[0]++;
4547 setup_items_for_insert(root, path, new_key, &item_size,
4548 item_size, item_size +
4549 sizeof(struct btrfs_item), 1);
4550 leaf = path->nodes[0];
4551 memcpy_extent_buffer(leaf,
4552 btrfs_item_ptr_offset(leaf, path->slots[0]),
4553 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4554 item_size);
4555 return 0;
4559 * make the item pointed to by the path smaller. new_size indicates
4560 * how small to make it, and from_end tells us if we just chop bytes
4561 * off the end of the item or if we shift the item to chop bytes off
4562 * the front.
4564 void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4565 struct btrfs_path *path, u32 new_size, int from_end)
4567 int slot;
4568 struct extent_buffer *leaf;
4569 struct btrfs_item *item;
4570 u32 nritems;
4571 unsigned int data_end;
4572 unsigned int old_data_start;
4573 unsigned int old_size;
4574 unsigned int size_diff;
4575 int i;
4576 struct btrfs_map_token token;
4578 btrfs_init_map_token(&token);
4580 leaf = path->nodes[0];
4581 slot = path->slots[0];
4583 old_size = btrfs_item_size_nr(leaf, slot);
4584 if (old_size == new_size)
4585 return;
4587 nritems = btrfs_header_nritems(leaf);
4588 data_end = leaf_data_end(fs_info, leaf);
4590 old_data_start = btrfs_item_offset_nr(leaf, slot);
4592 size_diff = old_size - new_size;
4594 BUG_ON(slot < 0);
4595 BUG_ON(slot >= nritems);
4598 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4600 /* first correct the data pointers */
4601 for (i = slot; i < nritems; i++) {
4602 u32 ioff;
4603 item = btrfs_item_nr(i);
4605 ioff = btrfs_token_item_offset(leaf, item, &token);
4606 btrfs_set_token_item_offset(leaf, item,
4607 ioff + size_diff, &token);
4610 /* shift the data */
4611 if (from_end) {
4612 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4613 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4614 data_end, old_data_start + new_size - data_end);
4615 } else {
4616 struct btrfs_disk_key disk_key;
4617 u64 offset;
4619 btrfs_item_key(leaf, &disk_key, slot);
4621 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4622 unsigned long ptr;
4623 struct btrfs_file_extent_item *fi;
4625 fi = btrfs_item_ptr(leaf, slot,
4626 struct btrfs_file_extent_item);
4627 fi = (struct btrfs_file_extent_item *)(
4628 (unsigned long)fi - size_diff);
4630 if (btrfs_file_extent_type(leaf, fi) ==
4631 BTRFS_FILE_EXTENT_INLINE) {
4632 ptr = btrfs_item_ptr_offset(leaf, slot);
4633 memmove_extent_buffer(leaf, ptr,
4634 (unsigned long)fi,
4635 BTRFS_FILE_EXTENT_INLINE_DATA_START);
4639 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4640 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4641 data_end, old_data_start - data_end);
4643 offset = btrfs_disk_key_offset(&disk_key);
4644 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4645 btrfs_set_item_key(leaf, &disk_key, slot);
4646 if (slot == 0)
4647 fixup_low_keys(fs_info, path, &disk_key, 1);
4650 item = btrfs_item_nr(slot);
4651 btrfs_set_item_size(leaf, item, new_size);
4652 btrfs_mark_buffer_dirty(leaf);
4654 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4655 btrfs_print_leaf(leaf);
4656 BUG();
4661 * make the item pointed to by the path bigger, data_size is the added size.
4663 void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
4664 u32 data_size)
4666 int slot;
4667 struct extent_buffer *leaf;
4668 struct btrfs_item *item;
4669 u32 nritems;
4670 unsigned int data_end;
4671 unsigned int old_data;
4672 unsigned int old_size;
4673 int i;
4674 struct btrfs_map_token token;
4676 btrfs_init_map_token(&token);
4678 leaf = path->nodes[0];
4680 nritems = btrfs_header_nritems(leaf);
4681 data_end = leaf_data_end(fs_info, leaf);
4683 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
4684 btrfs_print_leaf(leaf);
4685 BUG();
4687 slot = path->slots[0];
4688 old_data = btrfs_item_end_nr(leaf, slot);
4690 BUG_ON(slot < 0);
4691 if (slot >= nritems) {
4692 btrfs_print_leaf(leaf);
4693 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4694 slot, nritems);
4695 BUG_ON(1);
4699 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4701 /* first correct the data pointers */
4702 for (i = slot; i < nritems; i++) {
4703 u32 ioff;
4704 item = btrfs_item_nr(i);
4706 ioff = btrfs_token_item_offset(leaf, item, &token);
4707 btrfs_set_token_item_offset(leaf, item,
4708 ioff - data_size, &token);
4711 /* shift the data */
4712 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4713 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
4714 data_end, old_data - data_end);
4716 data_end = old_data;
4717 old_size = btrfs_item_size_nr(leaf, slot);
4718 item = btrfs_item_nr(slot);
4719 btrfs_set_item_size(leaf, item, old_size + data_size);
4720 btrfs_mark_buffer_dirty(leaf);
4722 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4723 btrfs_print_leaf(leaf);
4724 BUG();
4729 * this is a helper for btrfs_insert_empty_items, the main goal here is
4730 * to save stack depth by doing the bulk of the work in a function
4731 * that doesn't call btrfs_search_slot
4733 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4734 const struct btrfs_key *cpu_key, u32 *data_size,
4735 u32 total_data, u32 total_size, int nr)
4737 struct btrfs_fs_info *fs_info = root->fs_info;
4738 struct btrfs_item *item;
4739 int i;
4740 u32 nritems;
4741 unsigned int data_end;
4742 struct btrfs_disk_key disk_key;
4743 struct extent_buffer *leaf;
4744 int slot;
4745 struct btrfs_map_token token;
4747 if (path->slots[0] == 0) {
4748 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4749 fixup_low_keys(fs_info, path, &disk_key, 1);
4751 btrfs_unlock_up_safe(path, 1);
4753 btrfs_init_map_token(&token);
4755 leaf = path->nodes[0];
4756 slot = path->slots[0];
4758 nritems = btrfs_header_nritems(leaf);
4759 data_end = leaf_data_end(fs_info, leaf);
4761 if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
4762 btrfs_print_leaf(leaf);
4763 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4764 total_size, btrfs_leaf_free_space(fs_info, leaf));
4765 BUG();
4768 if (slot != nritems) {
4769 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4771 if (old_data < data_end) {
4772 btrfs_print_leaf(leaf);
4773 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
4774 slot, old_data, data_end);
4775 BUG_ON(1);
4778 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4780 /* first correct the data pointers */
4781 for (i = slot; i < nritems; i++) {
4782 u32 ioff;
4784 item = btrfs_item_nr(i);
4785 ioff = btrfs_token_item_offset(leaf, item, &token);
4786 btrfs_set_token_item_offset(leaf, item,
4787 ioff - total_data, &token);
4789 /* shift the items */
4790 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4791 btrfs_item_nr_offset(slot),
4792 (nritems - slot) * sizeof(struct btrfs_item));
4794 /* shift the data */
4795 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4796 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
4797 data_end, old_data - data_end);
4798 data_end = old_data;
4801 /* setup the item for the new data */
4802 for (i = 0; i < nr; i++) {
4803 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4804 btrfs_set_item_key(leaf, &disk_key, slot + i);
4805 item = btrfs_item_nr(slot + i);
4806 btrfs_set_token_item_offset(leaf, item,
4807 data_end - data_size[i], &token);
4808 data_end -= data_size[i];
4809 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4812 btrfs_set_header_nritems(leaf, nritems + nr);
4813 btrfs_mark_buffer_dirty(leaf);
4815 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4816 btrfs_print_leaf(leaf);
4817 BUG();
4822 * Given a key and some data, insert items into the tree.
4823 * This does all the path init required, making room in the tree if needed.
4825 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4826 struct btrfs_root *root,
4827 struct btrfs_path *path,
4828 const struct btrfs_key *cpu_key, u32 *data_size,
4829 int nr)
4831 int ret = 0;
4832 int slot;
4833 int i;
4834 u32 total_size = 0;
4835 u32 total_data = 0;
4837 for (i = 0; i < nr; i++)
4838 total_data += data_size[i];
4840 total_size = total_data + (nr * sizeof(struct btrfs_item));
4841 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4842 if (ret == 0)
4843 return -EEXIST;
4844 if (ret < 0)
4845 return ret;
4847 slot = path->slots[0];
4848 BUG_ON(slot < 0);
4850 setup_items_for_insert(root, path, cpu_key, data_size,
4851 total_data, total_size, nr);
4852 return 0;
4856 * Given a key and some data, insert an item into the tree.
4857 * This does all the path init required, making room in the tree if needed.
4859 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4860 const struct btrfs_key *cpu_key, void *data,
4861 u32 data_size)
4863 int ret = 0;
4864 struct btrfs_path *path;
4865 struct extent_buffer *leaf;
4866 unsigned long ptr;
4868 path = btrfs_alloc_path();
4869 if (!path)
4870 return -ENOMEM;
4871 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4872 if (!ret) {
4873 leaf = path->nodes[0];
4874 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4875 write_extent_buffer(leaf, data, ptr, data_size);
4876 btrfs_mark_buffer_dirty(leaf);
4878 btrfs_free_path(path);
4879 return ret;
4883 * delete the pointer from a given node.
4885 * the tree should have been previously balanced so the deletion does not
4886 * empty a node.
4888 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4889 int level, int slot)
4891 struct btrfs_fs_info *fs_info = root->fs_info;
4892 struct extent_buffer *parent = path->nodes[level];
4893 u32 nritems;
4894 int ret;
4896 nritems = btrfs_header_nritems(parent);
4897 if (slot != nritems - 1) {
4898 if (level)
4899 tree_mod_log_eb_move(fs_info, parent, slot,
4900 slot + 1, nritems - slot - 1);
4901 memmove_extent_buffer(parent,
4902 btrfs_node_key_ptr_offset(slot),
4903 btrfs_node_key_ptr_offset(slot + 1),
4904 sizeof(struct btrfs_key_ptr) *
4905 (nritems - slot - 1));
4906 } else if (level) {
4907 ret = tree_mod_log_insert_key(fs_info, parent, slot,
4908 MOD_LOG_KEY_REMOVE, GFP_NOFS);
4909 BUG_ON(ret < 0);
4912 nritems--;
4913 btrfs_set_header_nritems(parent, nritems);
4914 if (nritems == 0 && parent == root->node) {
4915 BUG_ON(btrfs_header_level(root->node) != 1);
4916 /* just turn the root into a leaf and break */
4917 btrfs_set_header_level(root->node, 0);
4918 } else if (slot == 0) {
4919 struct btrfs_disk_key disk_key;
4921 btrfs_node_key(parent, &disk_key, 0);
4922 fixup_low_keys(fs_info, path, &disk_key, level + 1);
4924 btrfs_mark_buffer_dirty(parent);
4928 * a helper function to delete the leaf pointed to by path->slots[1] and
4929 * path->nodes[1].
4931 * This deletes the pointer in path->nodes[1] and frees the leaf
4932 * block extent. zero is returned if it all worked out, < 0 otherwise.
4934 * The path must have already been setup for deleting the leaf, including
4935 * all the proper balancing. path->nodes[1] must be locked.
4937 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4938 struct btrfs_root *root,
4939 struct btrfs_path *path,
4940 struct extent_buffer *leaf)
4942 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4943 del_ptr(root, path, 1, path->slots[1]);
4946 * btrfs_free_extent is expensive, we want to make sure we
4947 * aren't holding any locks when we call it
4949 btrfs_unlock_up_safe(path, 0);
4951 root_sub_used(root, leaf->len);
4953 extent_buffer_get(leaf);
4954 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4955 free_extent_buffer_stale(leaf);
4958 * delete the item at the leaf level in path. If that empties
4959 * the leaf, remove it from the tree
4961 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4962 struct btrfs_path *path, int slot, int nr)
4964 struct btrfs_fs_info *fs_info = root->fs_info;
4965 struct extent_buffer *leaf;
4966 struct btrfs_item *item;
4967 u32 last_off;
4968 u32 dsize = 0;
4969 int ret = 0;
4970 int wret;
4971 int i;
4972 u32 nritems;
4973 struct btrfs_map_token token;
4975 btrfs_init_map_token(&token);
4977 leaf = path->nodes[0];
4978 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4980 for (i = 0; i < nr; i++)
4981 dsize += btrfs_item_size_nr(leaf, slot + i);
4983 nritems = btrfs_header_nritems(leaf);
4985 if (slot + nr != nritems) {
4986 int data_end = leaf_data_end(fs_info, leaf);
4988 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4989 data_end + dsize,
4990 BTRFS_LEAF_DATA_OFFSET + data_end,
4991 last_off - data_end);
4993 for (i = slot + nr; i < nritems; i++) {
4994 u32 ioff;
4996 item = btrfs_item_nr(i);
4997 ioff = btrfs_token_item_offset(leaf, item, &token);
4998 btrfs_set_token_item_offset(leaf, item,
4999 ioff + dsize, &token);
5002 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
5003 btrfs_item_nr_offset(slot + nr),
5004 sizeof(struct btrfs_item) *
5005 (nritems - slot - nr));
5007 btrfs_set_header_nritems(leaf, nritems - nr);
5008 nritems -= nr;
5010 /* delete the leaf if we've emptied it */
5011 if (nritems == 0) {
5012 if (leaf == root->node) {
5013 btrfs_set_header_level(leaf, 0);
5014 } else {
5015 btrfs_set_path_blocking(path);
5016 clean_tree_block(fs_info, leaf);
5017 btrfs_del_leaf(trans, root, path, leaf);
5019 } else {
5020 int used = leaf_space_used(leaf, 0, nritems);
5021 if (slot == 0) {
5022 struct btrfs_disk_key disk_key;
5024 btrfs_item_key(leaf, &disk_key, 0);
5025 fixup_low_keys(fs_info, path, &disk_key, 1);
5028 /* delete the leaf if it is mostly empty */
5029 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
5030 /* push_leaf_left fixes the path.
5031 * make sure the path still points to our leaf
5032 * for possible call to del_ptr below
5034 slot = path->slots[1];
5035 extent_buffer_get(leaf);
5037 btrfs_set_path_blocking(path);
5038 wret = push_leaf_left(trans, root, path, 1, 1,
5039 1, (u32)-1);
5040 if (wret < 0 && wret != -ENOSPC)
5041 ret = wret;
5043 if (path->nodes[0] == leaf &&
5044 btrfs_header_nritems(leaf)) {
5045 wret = push_leaf_right(trans, root, path, 1,
5046 1, 1, 0);
5047 if (wret < 0 && wret != -ENOSPC)
5048 ret = wret;
5051 if (btrfs_header_nritems(leaf) == 0) {
5052 path->slots[1] = slot;
5053 btrfs_del_leaf(trans, root, path, leaf);
5054 free_extent_buffer(leaf);
5055 ret = 0;
5056 } else {
5057 /* if we're still in the path, make sure
5058 * we're dirty. Otherwise, one of the
5059 * push_leaf functions must have already
5060 * dirtied this buffer
5062 if (path->nodes[0] == leaf)
5063 btrfs_mark_buffer_dirty(leaf);
5064 free_extent_buffer(leaf);
5066 } else {
5067 btrfs_mark_buffer_dirty(leaf);
5070 return ret;
5074 * search the tree again to find a leaf with lesser keys
5075 * returns 0 if it found something or 1 if there are no lesser leaves.
5076 * returns < 0 on io errors.
5078 * This may release the path, and so you may lose any locks held at the
5079 * time you call it.
5081 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
5083 struct btrfs_key key;
5084 struct btrfs_disk_key found_key;
5085 int ret;
5087 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5089 if (key.offset > 0) {
5090 key.offset--;
5091 } else if (key.type > 0) {
5092 key.type--;
5093 key.offset = (u64)-1;
5094 } else if (key.objectid > 0) {
5095 key.objectid--;
5096 key.type = (u8)-1;
5097 key.offset = (u64)-1;
5098 } else {
5099 return 1;
5102 btrfs_release_path(path);
5103 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5104 if (ret < 0)
5105 return ret;
5106 btrfs_item_key(path->nodes[0], &found_key, 0);
5107 ret = comp_keys(&found_key, &key);
5109 * We might have had an item with the previous key in the tree right
5110 * before we released our path. And after we released our path, that
5111 * item might have been pushed to the first slot (0) of the leaf we
5112 * were holding due to a tree balance. Alternatively, an item with the
5113 * previous key can exist as the only element of a leaf (big fat item).
5114 * Therefore account for these 2 cases, so that our callers (like
5115 * btrfs_previous_item) don't miss an existing item with a key matching
5116 * the previous key we computed above.
5118 if (ret <= 0)
5119 return 0;
5120 return 1;
5124 * A helper function to walk down the tree starting at min_key, and looking
5125 * for nodes or leaves that are have a minimum transaction id.
5126 * This is used by the btree defrag code, and tree logging
5128 * This does not cow, but it does stuff the starting key it finds back
5129 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5130 * key and get a writable path.
5132 * This does lock as it descends, and path->keep_locks should be set
5133 * to 1 by the caller.
5135 * This honors path->lowest_level to prevent descent past a given level
5136 * of the tree.
5138 * min_trans indicates the oldest transaction that you are interested
5139 * in walking through. Any nodes or leaves older than min_trans are
5140 * skipped over (without reading them).
5142 * returns zero if something useful was found, < 0 on error and 1 if there
5143 * was nothing in the tree that matched the search criteria.
5145 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5146 struct btrfs_path *path,
5147 u64 min_trans)
5149 struct btrfs_fs_info *fs_info = root->fs_info;
5150 struct extent_buffer *cur;
5151 struct btrfs_key found_key;
5152 int slot;
5153 int sret;
5154 u32 nritems;
5155 int level;
5156 int ret = 1;
5157 int keep_locks = path->keep_locks;
5159 path->keep_locks = 1;
5160 again:
5161 cur = btrfs_read_lock_root_node(root);
5162 level = btrfs_header_level(cur);
5163 WARN_ON(path->nodes[level]);
5164 path->nodes[level] = cur;
5165 path->locks[level] = BTRFS_READ_LOCK;
5167 if (btrfs_header_generation(cur) < min_trans) {
5168 ret = 1;
5169 goto out;
5171 while (1) {
5172 nritems = btrfs_header_nritems(cur);
5173 level = btrfs_header_level(cur);
5174 sret = bin_search(cur, min_key, level, &slot);
5176 /* at the lowest level, we're done, setup the path and exit */
5177 if (level == path->lowest_level) {
5178 if (slot >= nritems)
5179 goto find_next_key;
5180 ret = 0;
5181 path->slots[level] = slot;
5182 btrfs_item_key_to_cpu(cur, &found_key, slot);
5183 goto out;
5185 if (sret && slot > 0)
5186 slot--;
5188 * check this node pointer against the min_trans parameters.
5189 * If it is too old, old, skip to the next one.
5191 while (slot < nritems) {
5192 u64 gen;
5194 gen = btrfs_node_ptr_generation(cur, slot);
5195 if (gen < min_trans) {
5196 slot++;
5197 continue;
5199 break;
5201 find_next_key:
5203 * we didn't find a candidate key in this node, walk forward
5204 * and find another one
5206 if (slot >= nritems) {
5207 path->slots[level] = slot;
5208 btrfs_set_path_blocking(path);
5209 sret = btrfs_find_next_key(root, path, min_key, level,
5210 min_trans);
5211 if (sret == 0) {
5212 btrfs_release_path(path);
5213 goto again;
5214 } else {
5215 goto out;
5218 /* save our key for returning back */
5219 btrfs_node_key_to_cpu(cur, &found_key, slot);
5220 path->slots[level] = slot;
5221 if (level == path->lowest_level) {
5222 ret = 0;
5223 goto out;
5225 btrfs_set_path_blocking(path);
5226 cur = read_node_slot(fs_info, cur, slot);
5227 if (IS_ERR(cur)) {
5228 ret = PTR_ERR(cur);
5229 goto out;
5232 btrfs_tree_read_lock(cur);
5234 path->locks[level - 1] = BTRFS_READ_LOCK;
5235 path->nodes[level - 1] = cur;
5236 unlock_up(path, level, 1, 0, NULL);
5237 btrfs_clear_path_blocking(path, NULL, 0);
5239 out:
5240 path->keep_locks = keep_locks;
5241 if (ret == 0) {
5242 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5243 btrfs_set_path_blocking(path);
5244 memcpy(min_key, &found_key, sizeof(found_key));
5246 return ret;
5249 static int tree_move_down(struct btrfs_fs_info *fs_info,
5250 struct btrfs_path *path,
5251 int *level)
5253 struct extent_buffer *eb;
5255 BUG_ON(*level == 0);
5256 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
5257 if (IS_ERR(eb))
5258 return PTR_ERR(eb);
5260 path->nodes[*level - 1] = eb;
5261 path->slots[*level - 1] = 0;
5262 (*level)--;
5263 return 0;
5266 static int tree_move_next_or_upnext(struct btrfs_path *path,
5267 int *level, int root_level)
5269 int ret = 0;
5270 int nritems;
5271 nritems = btrfs_header_nritems(path->nodes[*level]);
5273 path->slots[*level]++;
5275 while (path->slots[*level] >= nritems) {
5276 if (*level == root_level)
5277 return -1;
5279 /* move upnext */
5280 path->slots[*level] = 0;
5281 free_extent_buffer(path->nodes[*level]);
5282 path->nodes[*level] = NULL;
5283 (*level)++;
5284 path->slots[*level]++;
5286 nritems = btrfs_header_nritems(path->nodes[*level]);
5287 ret = 1;
5289 return ret;
5293 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5294 * or down.
5296 static int tree_advance(struct btrfs_fs_info *fs_info,
5297 struct btrfs_path *path,
5298 int *level, int root_level,
5299 int allow_down,
5300 struct btrfs_key *key)
5302 int ret;
5304 if (*level == 0 || !allow_down) {
5305 ret = tree_move_next_or_upnext(path, level, root_level);
5306 } else {
5307 ret = tree_move_down(fs_info, path, level);
5309 if (ret >= 0) {
5310 if (*level == 0)
5311 btrfs_item_key_to_cpu(path->nodes[*level], key,
5312 path->slots[*level]);
5313 else
5314 btrfs_node_key_to_cpu(path->nodes[*level], key,
5315 path->slots[*level]);
5317 return ret;
5320 static int tree_compare_item(struct btrfs_path *left_path,
5321 struct btrfs_path *right_path,
5322 char *tmp_buf)
5324 int cmp;
5325 int len1, len2;
5326 unsigned long off1, off2;
5328 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5329 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5330 if (len1 != len2)
5331 return 1;
5333 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5334 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5335 right_path->slots[0]);
5337 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5339 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5340 if (cmp)
5341 return 1;
5342 return 0;
5345 #define ADVANCE 1
5346 #define ADVANCE_ONLY_NEXT -1
5349 * This function compares two trees and calls the provided callback for
5350 * every changed/new/deleted item it finds.
5351 * If shared tree blocks are encountered, whole subtrees are skipped, making
5352 * the compare pretty fast on snapshotted subvolumes.
5354 * This currently works on commit roots only. As commit roots are read only,
5355 * we don't do any locking. The commit roots are protected with transactions.
5356 * Transactions are ended and rejoined when a commit is tried in between.
5358 * This function checks for modifications done to the trees while comparing.
5359 * If it detects a change, it aborts immediately.
5361 int btrfs_compare_trees(struct btrfs_root *left_root,
5362 struct btrfs_root *right_root,
5363 btrfs_changed_cb_t changed_cb, void *ctx)
5365 struct btrfs_fs_info *fs_info = left_root->fs_info;
5366 int ret;
5367 int cmp;
5368 struct btrfs_path *left_path = NULL;
5369 struct btrfs_path *right_path = NULL;
5370 struct btrfs_key left_key;
5371 struct btrfs_key right_key;
5372 char *tmp_buf = NULL;
5373 int left_root_level;
5374 int right_root_level;
5375 int left_level;
5376 int right_level;
5377 int left_end_reached;
5378 int right_end_reached;
5379 int advance_left;
5380 int advance_right;
5381 u64 left_blockptr;
5382 u64 right_blockptr;
5383 u64 left_gen;
5384 u64 right_gen;
5386 left_path = btrfs_alloc_path();
5387 if (!left_path) {
5388 ret = -ENOMEM;
5389 goto out;
5391 right_path = btrfs_alloc_path();
5392 if (!right_path) {
5393 ret = -ENOMEM;
5394 goto out;
5397 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
5398 if (!tmp_buf) {
5399 ret = -ENOMEM;
5400 goto out;
5403 left_path->search_commit_root = 1;
5404 left_path->skip_locking = 1;
5405 right_path->search_commit_root = 1;
5406 right_path->skip_locking = 1;
5409 * Strategy: Go to the first items of both trees. Then do
5411 * If both trees are at level 0
5412 * Compare keys of current items
5413 * If left < right treat left item as new, advance left tree
5414 * and repeat
5415 * If left > right treat right item as deleted, advance right tree
5416 * and repeat
5417 * If left == right do deep compare of items, treat as changed if
5418 * needed, advance both trees and repeat
5419 * If both trees are at the same level but not at level 0
5420 * Compare keys of current nodes/leafs
5421 * If left < right advance left tree and repeat
5422 * If left > right advance right tree and repeat
5423 * If left == right compare blockptrs of the next nodes/leafs
5424 * If they match advance both trees but stay at the same level
5425 * and repeat
5426 * If they don't match advance both trees while allowing to go
5427 * deeper and repeat
5428 * If tree levels are different
5429 * Advance the tree that needs it and repeat
5431 * Advancing a tree means:
5432 * If we are at level 0, try to go to the next slot. If that's not
5433 * possible, go one level up and repeat. Stop when we found a level
5434 * where we could go to the next slot. We may at this point be on a
5435 * node or a leaf.
5437 * If we are not at level 0 and not on shared tree blocks, go one
5438 * level deeper.
5440 * If we are not at level 0 and on shared tree blocks, go one slot to
5441 * the right if possible or go up and right.
5444 down_read(&fs_info->commit_root_sem);
5445 left_level = btrfs_header_level(left_root->commit_root);
5446 left_root_level = left_level;
5447 left_path->nodes[left_level] =
5448 btrfs_clone_extent_buffer(left_root->commit_root);
5449 if (!left_path->nodes[left_level]) {
5450 up_read(&fs_info->commit_root_sem);
5451 ret = -ENOMEM;
5452 goto out;
5454 extent_buffer_get(left_path->nodes[left_level]);
5456 right_level = btrfs_header_level(right_root->commit_root);
5457 right_root_level = right_level;
5458 right_path->nodes[right_level] =
5459 btrfs_clone_extent_buffer(right_root->commit_root);
5460 if (!right_path->nodes[right_level]) {
5461 up_read(&fs_info->commit_root_sem);
5462 ret = -ENOMEM;
5463 goto out;
5465 extent_buffer_get(right_path->nodes[right_level]);
5466 up_read(&fs_info->commit_root_sem);
5468 if (left_level == 0)
5469 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5470 &left_key, left_path->slots[left_level]);
5471 else
5472 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5473 &left_key, left_path->slots[left_level]);
5474 if (right_level == 0)
5475 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5476 &right_key, right_path->slots[right_level]);
5477 else
5478 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5479 &right_key, right_path->slots[right_level]);
5481 left_end_reached = right_end_reached = 0;
5482 advance_left = advance_right = 0;
5484 while (1) {
5485 cond_resched();
5486 if (advance_left && !left_end_reached) {
5487 ret = tree_advance(fs_info, left_path, &left_level,
5488 left_root_level,
5489 advance_left != ADVANCE_ONLY_NEXT,
5490 &left_key);
5491 if (ret == -1)
5492 left_end_reached = ADVANCE;
5493 else if (ret < 0)
5494 goto out;
5495 advance_left = 0;
5497 if (advance_right && !right_end_reached) {
5498 ret = tree_advance(fs_info, right_path, &right_level,
5499 right_root_level,
5500 advance_right != ADVANCE_ONLY_NEXT,
5501 &right_key);
5502 if (ret == -1)
5503 right_end_reached = ADVANCE;
5504 else if (ret < 0)
5505 goto out;
5506 advance_right = 0;
5509 if (left_end_reached && right_end_reached) {
5510 ret = 0;
5511 goto out;
5512 } else if (left_end_reached) {
5513 if (right_level == 0) {
5514 ret = changed_cb(left_root, right_root,
5515 left_path, right_path,
5516 &right_key,
5517 BTRFS_COMPARE_TREE_DELETED,
5518 ctx);
5519 if (ret < 0)
5520 goto out;
5522 advance_right = ADVANCE;
5523 continue;
5524 } else if (right_end_reached) {
5525 if (left_level == 0) {
5526 ret = changed_cb(left_root, right_root,
5527 left_path, right_path,
5528 &left_key,
5529 BTRFS_COMPARE_TREE_NEW,
5530 ctx);
5531 if (ret < 0)
5532 goto out;
5534 advance_left = ADVANCE;
5535 continue;
5538 if (left_level == 0 && right_level == 0) {
5539 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5540 if (cmp < 0) {
5541 ret = changed_cb(left_root, right_root,
5542 left_path, right_path,
5543 &left_key,
5544 BTRFS_COMPARE_TREE_NEW,
5545 ctx);
5546 if (ret < 0)
5547 goto out;
5548 advance_left = ADVANCE;
5549 } else if (cmp > 0) {
5550 ret = changed_cb(left_root, right_root,
5551 left_path, right_path,
5552 &right_key,
5553 BTRFS_COMPARE_TREE_DELETED,
5554 ctx);
5555 if (ret < 0)
5556 goto out;
5557 advance_right = ADVANCE;
5558 } else {
5559 enum btrfs_compare_tree_result result;
5561 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
5562 ret = tree_compare_item(left_path, right_path,
5563 tmp_buf);
5564 if (ret)
5565 result = BTRFS_COMPARE_TREE_CHANGED;
5566 else
5567 result = BTRFS_COMPARE_TREE_SAME;
5568 ret = changed_cb(left_root, right_root,
5569 left_path, right_path,
5570 &left_key, result, ctx);
5571 if (ret < 0)
5572 goto out;
5573 advance_left = ADVANCE;
5574 advance_right = ADVANCE;
5576 } else if (left_level == right_level) {
5577 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5578 if (cmp < 0) {
5579 advance_left = ADVANCE;
5580 } else if (cmp > 0) {
5581 advance_right = ADVANCE;
5582 } else {
5583 left_blockptr = btrfs_node_blockptr(
5584 left_path->nodes[left_level],
5585 left_path->slots[left_level]);
5586 right_blockptr = btrfs_node_blockptr(
5587 right_path->nodes[right_level],
5588 right_path->slots[right_level]);
5589 left_gen = btrfs_node_ptr_generation(
5590 left_path->nodes[left_level],
5591 left_path->slots[left_level]);
5592 right_gen = btrfs_node_ptr_generation(
5593 right_path->nodes[right_level],
5594 right_path->slots[right_level]);
5595 if (left_blockptr == right_blockptr &&
5596 left_gen == right_gen) {
5598 * As we're on a shared block, don't
5599 * allow to go deeper.
5601 advance_left = ADVANCE_ONLY_NEXT;
5602 advance_right = ADVANCE_ONLY_NEXT;
5603 } else {
5604 advance_left = ADVANCE;
5605 advance_right = ADVANCE;
5608 } else if (left_level < right_level) {
5609 advance_right = ADVANCE;
5610 } else {
5611 advance_left = ADVANCE;
5615 out:
5616 btrfs_free_path(left_path);
5617 btrfs_free_path(right_path);
5618 kvfree(tmp_buf);
5619 return ret;
5623 * this is similar to btrfs_next_leaf, but does not try to preserve
5624 * and fixup the path. It looks for and returns the next key in the
5625 * tree based on the current path and the min_trans parameters.
5627 * 0 is returned if another key is found, < 0 if there are any errors
5628 * and 1 is returned if there are no higher keys in the tree
5630 * path->keep_locks should be set to 1 on the search made before
5631 * calling this function.
5633 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5634 struct btrfs_key *key, int level, u64 min_trans)
5636 int slot;
5637 struct extent_buffer *c;
5639 WARN_ON(!path->keep_locks);
5640 while (level < BTRFS_MAX_LEVEL) {
5641 if (!path->nodes[level])
5642 return 1;
5644 slot = path->slots[level] + 1;
5645 c = path->nodes[level];
5646 next:
5647 if (slot >= btrfs_header_nritems(c)) {
5648 int ret;
5649 int orig_lowest;
5650 struct btrfs_key cur_key;
5651 if (level + 1 >= BTRFS_MAX_LEVEL ||
5652 !path->nodes[level + 1])
5653 return 1;
5655 if (path->locks[level + 1]) {
5656 level++;
5657 continue;
5660 slot = btrfs_header_nritems(c) - 1;
5661 if (level == 0)
5662 btrfs_item_key_to_cpu(c, &cur_key, slot);
5663 else
5664 btrfs_node_key_to_cpu(c, &cur_key, slot);
5666 orig_lowest = path->lowest_level;
5667 btrfs_release_path(path);
5668 path->lowest_level = level;
5669 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5670 0, 0);
5671 path->lowest_level = orig_lowest;
5672 if (ret < 0)
5673 return ret;
5675 c = path->nodes[level];
5676 slot = path->slots[level];
5677 if (ret == 0)
5678 slot++;
5679 goto next;
5682 if (level == 0)
5683 btrfs_item_key_to_cpu(c, key, slot);
5684 else {
5685 u64 gen = btrfs_node_ptr_generation(c, slot);
5687 if (gen < min_trans) {
5688 slot++;
5689 goto next;
5691 btrfs_node_key_to_cpu(c, key, slot);
5693 return 0;
5695 return 1;
5699 * search the tree again to find a leaf with greater keys
5700 * returns 0 if it found something or 1 if there are no greater leaves.
5701 * returns < 0 on io errors.
5703 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5705 return btrfs_next_old_leaf(root, path, 0);
5708 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5709 u64 time_seq)
5711 int slot;
5712 int level;
5713 struct extent_buffer *c;
5714 struct extent_buffer *next;
5715 struct btrfs_key key;
5716 u32 nritems;
5717 int ret;
5718 int old_spinning = path->leave_spinning;
5719 int next_rw_lock = 0;
5721 nritems = btrfs_header_nritems(path->nodes[0]);
5722 if (nritems == 0)
5723 return 1;
5725 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5726 again:
5727 level = 1;
5728 next = NULL;
5729 next_rw_lock = 0;
5730 btrfs_release_path(path);
5732 path->keep_locks = 1;
5733 path->leave_spinning = 1;
5735 if (time_seq)
5736 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5737 else
5738 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5739 path->keep_locks = 0;
5741 if (ret < 0)
5742 return ret;
5744 nritems = btrfs_header_nritems(path->nodes[0]);
5746 * by releasing the path above we dropped all our locks. A balance
5747 * could have added more items next to the key that used to be
5748 * at the very end of the block. So, check again here and
5749 * advance the path if there are now more items available.
5751 if (nritems > 0 && path->slots[0] < nritems - 1) {
5752 if (ret == 0)
5753 path->slots[0]++;
5754 ret = 0;
5755 goto done;
5758 * So the above check misses one case:
5759 * - after releasing the path above, someone has removed the item that
5760 * used to be at the very end of the block, and balance between leafs
5761 * gets another one with bigger key.offset to replace it.
5763 * This one should be returned as well, or we can get leaf corruption
5764 * later(esp. in __btrfs_drop_extents()).
5766 * And a bit more explanation about this check,
5767 * with ret > 0, the key isn't found, the path points to the slot
5768 * where it should be inserted, so the path->slots[0] item must be the
5769 * bigger one.
5771 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5772 ret = 0;
5773 goto done;
5776 while (level < BTRFS_MAX_LEVEL) {
5777 if (!path->nodes[level]) {
5778 ret = 1;
5779 goto done;
5782 slot = path->slots[level] + 1;
5783 c = path->nodes[level];
5784 if (slot >= btrfs_header_nritems(c)) {
5785 level++;
5786 if (level == BTRFS_MAX_LEVEL) {
5787 ret = 1;
5788 goto done;
5790 continue;
5793 if (next) {
5794 btrfs_tree_unlock_rw(next, next_rw_lock);
5795 free_extent_buffer(next);
5798 next = c;
5799 next_rw_lock = path->locks[level];
5800 ret = read_block_for_search(root, path, &next, level,
5801 slot, &key);
5802 if (ret == -EAGAIN)
5803 goto again;
5805 if (ret < 0) {
5806 btrfs_release_path(path);
5807 goto done;
5810 if (!path->skip_locking) {
5811 ret = btrfs_try_tree_read_lock(next);
5812 if (!ret && time_seq) {
5814 * If we don't get the lock, we may be racing
5815 * with push_leaf_left, holding that lock while
5816 * itself waiting for the leaf we've currently
5817 * locked. To solve this situation, we give up
5818 * on our lock and cycle.
5820 free_extent_buffer(next);
5821 btrfs_release_path(path);
5822 cond_resched();
5823 goto again;
5825 if (!ret) {
5826 btrfs_set_path_blocking(path);
5827 btrfs_tree_read_lock(next);
5828 btrfs_clear_path_blocking(path, next,
5829 BTRFS_READ_LOCK);
5831 next_rw_lock = BTRFS_READ_LOCK;
5833 break;
5835 path->slots[level] = slot;
5836 while (1) {
5837 level--;
5838 c = path->nodes[level];
5839 if (path->locks[level])
5840 btrfs_tree_unlock_rw(c, path->locks[level]);
5842 free_extent_buffer(c);
5843 path->nodes[level] = next;
5844 path->slots[level] = 0;
5845 if (!path->skip_locking)
5846 path->locks[level] = next_rw_lock;
5847 if (!level)
5848 break;
5850 ret = read_block_for_search(root, path, &next, level,
5851 0, &key);
5852 if (ret == -EAGAIN)
5853 goto again;
5855 if (ret < 0) {
5856 btrfs_release_path(path);
5857 goto done;
5860 if (!path->skip_locking) {
5861 ret = btrfs_try_tree_read_lock(next);
5862 if (!ret) {
5863 btrfs_set_path_blocking(path);
5864 btrfs_tree_read_lock(next);
5865 btrfs_clear_path_blocking(path, next,
5866 BTRFS_READ_LOCK);
5868 next_rw_lock = BTRFS_READ_LOCK;
5871 ret = 0;
5872 done:
5873 unlock_up(path, 0, 1, 0, NULL);
5874 path->leave_spinning = old_spinning;
5875 if (!old_spinning)
5876 btrfs_set_path_blocking(path);
5878 return ret;
5882 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5883 * searching until it gets past min_objectid or finds an item of 'type'
5885 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5887 int btrfs_previous_item(struct btrfs_root *root,
5888 struct btrfs_path *path, u64 min_objectid,
5889 int type)
5891 struct btrfs_key found_key;
5892 struct extent_buffer *leaf;
5893 u32 nritems;
5894 int ret;
5896 while (1) {
5897 if (path->slots[0] == 0) {
5898 btrfs_set_path_blocking(path);
5899 ret = btrfs_prev_leaf(root, path);
5900 if (ret != 0)
5901 return ret;
5902 } else {
5903 path->slots[0]--;
5905 leaf = path->nodes[0];
5906 nritems = btrfs_header_nritems(leaf);
5907 if (nritems == 0)
5908 return 1;
5909 if (path->slots[0] == nritems)
5910 path->slots[0]--;
5912 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5913 if (found_key.objectid < min_objectid)
5914 break;
5915 if (found_key.type == type)
5916 return 0;
5917 if (found_key.objectid == min_objectid &&
5918 found_key.type < type)
5919 break;
5921 return 1;
5925 * search in extent tree to find a previous Metadata/Data extent item with
5926 * min objecitd.
5928 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5930 int btrfs_previous_extent_item(struct btrfs_root *root,
5931 struct btrfs_path *path, u64 min_objectid)
5933 struct btrfs_key found_key;
5934 struct extent_buffer *leaf;
5935 u32 nritems;
5936 int ret;
5938 while (1) {
5939 if (path->slots[0] == 0) {
5940 btrfs_set_path_blocking(path);
5941 ret = btrfs_prev_leaf(root, path);
5942 if (ret != 0)
5943 return ret;
5944 } else {
5945 path->slots[0]--;
5947 leaf = path->nodes[0];
5948 nritems = btrfs_header_nritems(leaf);
5949 if (nritems == 0)
5950 return 1;
5951 if (path->slots[0] == nritems)
5952 path->slots[0]--;
5954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5955 if (found_key.objectid < min_objectid)
5956 break;
5957 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5958 found_key.type == BTRFS_METADATA_ITEM_KEY)
5959 return 0;
5960 if (found_key.objectid == min_objectid &&
5961 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5962 break;
5964 return 1;