1 // SPDX-License-Identifier: GPL-2.0
4 #include "tree-mod-log.h"
8 #include "tree-checker.h"
10 struct tree_mod_root {
15 struct tree_mod_elem {
19 enum btrfs_mod_log_op op;
22 * This is used for BTRFS_MOD_LOG_KEY_* and BTRFS_MOD_LOG_MOVE_KEYS
27 /* This is used for BTRFS_MOD_LOG_KEY* and BTRFS_MOD_LOG_ROOT_REPLACE. */
30 /* Those are used for op == BTRFS_MOD_LOG_KEY_{REPLACE,REMOVE}. */
31 struct btrfs_disk_key key;
34 /* This is used for op == BTRFS_MOD_LOG_MOVE_KEYS. */
40 /* This is used for op == BTRFS_MOD_LOG_ROOT_REPLACE. */
41 struct tree_mod_root old_root;
45 * Pull a new tree mod seq number for our operation.
47 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
49 return atomic64_inc_return(&fs_info->tree_mod_seq);
53 * This adds a new blocker to the tree mod log's blocker list if the @elem
54 * passed does not already have a sequence number set. So when a caller expects
55 * to record tree modifications, it should ensure to set elem->seq to zero
56 * before calling btrfs_get_tree_mod_seq.
57 * Returns a fresh, unused tree log modification sequence number, even if no new
60 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
61 struct btrfs_seq_list *elem)
63 write_lock(&fs_info->tree_mod_log_lock);
65 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
66 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
67 set_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
69 write_unlock(&fs_info->tree_mod_log_lock);
74 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
75 struct btrfs_seq_list *elem)
77 struct rb_root *tm_root;
80 struct tree_mod_elem *tm;
81 u64 min_seq = BTRFS_SEQ_LAST;
82 u64 seq_putting = elem->seq;
87 write_lock(&fs_info->tree_mod_log_lock);
88 list_del(&elem->list);
91 if (list_empty(&fs_info->tree_mod_seq_list)) {
92 clear_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
94 struct btrfs_seq_list *first;
96 first = list_first_entry(&fs_info->tree_mod_seq_list,
97 struct btrfs_seq_list, list);
98 if (seq_putting > first->seq) {
100 * Blocker with lower sequence number exists, we cannot
101 * remove anything from the log.
103 write_unlock(&fs_info->tree_mod_log_lock);
106 min_seq = first->seq;
110 * Anything that's lower than the lowest existing (read: blocked)
111 * sequence number can be removed from the tree.
113 tm_root = &fs_info->tree_mod_log;
114 for (node = rb_first(tm_root); node; node = next) {
115 next = rb_next(node);
116 tm = rb_entry(node, struct tree_mod_elem, node);
117 if (tm->seq >= min_seq)
119 rb_erase(node, tm_root);
122 write_unlock(&fs_info->tree_mod_log_lock);
126 * Key order of the log:
127 * node/leaf start address -> sequence
129 * The 'start address' is the logical address of the *new* root node for root
130 * replace operations, or the logical address of the affected block for all
133 static noinline int tree_mod_log_insert(struct btrfs_fs_info *fs_info,
134 struct tree_mod_elem *tm)
136 struct rb_root *tm_root;
137 struct rb_node **new;
138 struct rb_node *parent = NULL;
139 struct tree_mod_elem *cur;
141 lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
143 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
145 tm_root = &fs_info->tree_mod_log;
146 new = &tm_root->rb_node;
148 cur = rb_entry(*new, struct tree_mod_elem, node);
150 if (cur->logical < tm->logical)
151 new = &((*new)->rb_left);
152 else if (cur->logical > tm->logical)
153 new = &((*new)->rb_right);
154 else if (cur->seq < tm->seq)
155 new = &((*new)->rb_left);
156 else if (cur->seq > tm->seq)
157 new = &((*new)->rb_right);
162 rb_link_node(&tm->node, parent, new);
163 rb_insert_color(&tm->node, tm_root);
168 * Determines if logging can be omitted. Returns true if it can. Otherwise, it
169 * returns false with the tree_mod_log_lock acquired. The caller must hold
170 * this until all tree mod log insertions are recorded in the rb tree and then
171 * write unlock fs_info::tree_mod_log_lock.
173 static inline bool tree_mod_dont_log(struct btrfs_fs_info *fs_info,
174 struct extent_buffer *eb)
176 if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
178 if (eb && btrfs_header_level(eb) == 0)
181 write_lock(&fs_info->tree_mod_log_lock);
182 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
183 write_unlock(&fs_info->tree_mod_log_lock);
190 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
191 static inline bool tree_mod_need_log(const struct btrfs_fs_info *fs_info,
192 struct extent_buffer *eb)
194 if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
196 if (eb && btrfs_header_level(eb) == 0)
202 static struct tree_mod_elem *alloc_tree_mod_elem(struct extent_buffer *eb,
204 enum btrfs_mod_log_op op)
206 struct tree_mod_elem *tm;
208 tm = kzalloc(sizeof(*tm), GFP_NOFS);
212 tm->logical = eb->start;
213 if (op != BTRFS_MOD_LOG_KEY_ADD) {
214 btrfs_node_key(eb, &tm->key, slot);
215 tm->blockptr = btrfs_node_blockptr(eb, slot);
219 tm->generation = btrfs_node_ptr_generation(eb, slot);
220 RB_CLEAR_NODE(&tm->node);
225 int btrfs_tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
226 enum btrfs_mod_log_op op)
228 struct tree_mod_elem *tm;
231 if (!tree_mod_need_log(eb->fs_info, eb))
234 tm = alloc_tree_mod_elem(eb, slot, op);
238 if (tree_mod_dont_log(eb->fs_info, eb)) {
241 * Don't error if we failed to allocate memory because we don't
245 } else if (ret != 0) {
247 * We previously failed to allocate memory and we need to log,
248 * so we have to fail.
253 ret = tree_mod_log_insert(eb->fs_info, tm);
255 write_unlock(&eb->fs_info->tree_mod_log_lock);
262 static struct tree_mod_elem *tree_mod_log_alloc_move(struct extent_buffer *eb,
263 int dst_slot, int src_slot,
266 struct tree_mod_elem *tm;
268 tm = kzalloc(sizeof(*tm), GFP_NOFS);
270 return ERR_PTR(-ENOMEM);
272 tm->logical = eb->start;
274 tm->move.dst_slot = dst_slot;
275 tm->move.nr_items = nr_items;
276 tm->op = BTRFS_MOD_LOG_MOVE_KEYS;
277 RB_CLEAR_NODE(&tm->node);
282 int btrfs_tree_mod_log_insert_move(struct extent_buffer *eb,
283 int dst_slot, int src_slot,
286 struct tree_mod_elem *tm = NULL;
287 struct tree_mod_elem **tm_list = NULL;
292 if (!tree_mod_need_log(eb->fs_info, eb))
295 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
301 tm = tree_mod_log_alloc_move(eb, dst_slot, src_slot, nr_items);
308 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
309 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
310 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING);
318 if (tree_mod_dont_log(eb->fs_info, eb)) {
320 * Don't error if we failed to allocate memory because we don't
329 * We previously failed to allocate memory and we need to log, so we
336 * When we override something during the move, we log these removals.
337 * This can only happen when we move towards the beginning of the
338 * buffer, i.e. dst_slot < src_slot.
340 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
341 ret = tree_mod_log_insert(eb->fs_info, tm_list[i]);
346 ret = tree_mod_log_insert(eb->fs_info, tm);
349 write_unlock(&eb->fs_info->tree_mod_log_lock);
356 for (i = 0; i < nr_items; i++) {
357 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
358 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
363 write_unlock(&eb->fs_info->tree_mod_log_lock);
370 static inline int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
371 struct tree_mod_elem **tm_list,
377 for (i = nritems - 1; i >= 0; i--) {
378 ret = tree_mod_log_insert(fs_info, tm_list[i]);
380 for (j = nritems - 1; j > i; j--)
381 rb_erase(&tm_list[j]->node,
382 &fs_info->tree_mod_log);
390 int btrfs_tree_mod_log_insert_root(struct extent_buffer *old_root,
391 struct extent_buffer *new_root,
394 struct btrfs_fs_info *fs_info = old_root->fs_info;
395 struct tree_mod_elem *tm = NULL;
396 struct tree_mod_elem **tm_list = NULL;
401 if (!tree_mod_need_log(fs_info, NULL))
404 if (log_removal && btrfs_header_level(old_root) > 0) {
405 nritems = btrfs_header_nritems(old_root);
406 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
412 for (i = 0; i < nritems; i++) {
413 tm_list[i] = alloc_tree_mod_elem(old_root, i,
414 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING);
422 tm = kzalloc(sizeof(*tm), GFP_NOFS);
428 tm->logical = new_root->start;
429 tm->old_root.logical = old_root->start;
430 tm->old_root.level = btrfs_header_level(old_root);
431 tm->generation = btrfs_header_generation(old_root);
432 tm->op = BTRFS_MOD_LOG_ROOT_REPLACE;
435 if (tree_mod_dont_log(fs_info, NULL)) {
437 * Don't error if we failed to allocate memory because we don't
442 } else if (ret != 0) {
444 * We previously failed to allocate memory and we need to log,
445 * so we have to fail.
451 ret = tree_mod_log_free_eb(fs_info, tm_list, nritems);
453 ret = tree_mod_log_insert(fs_info, tm);
456 write_unlock(&fs_info->tree_mod_log_lock);
465 for (i = 0; i < nritems; i++)
474 static struct tree_mod_elem *__tree_mod_log_search(struct btrfs_fs_info *fs_info,
475 u64 start, u64 min_seq,
478 struct rb_root *tm_root;
479 struct rb_node *node;
480 struct tree_mod_elem *cur = NULL;
481 struct tree_mod_elem *found = NULL;
483 read_lock(&fs_info->tree_mod_log_lock);
484 tm_root = &fs_info->tree_mod_log;
485 node = tm_root->rb_node;
487 cur = rb_entry(node, struct tree_mod_elem, node);
488 if (cur->logical < start) {
489 node = node->rb_left;
490 } else if (cur->logical > start) {
491 node = node->rb_right;
492 } else if (cur->seq < min_seq) {
493 node = node->rb_left;
494 } else if (!smallest) {
495 /* We want the node with the highest seq */
497 BUG_ON(found->seq > cur->seq);
499 node = node->rb_left;
500 } else if (cur->seq > min_seq) {
501 /* We want the node with the smallest seq */
503 BUG_ON(found->seq < cur->seq);
505 node = node->rb_right;
511 read_unlock(&fs_info->tree_mod_log_lock);
517 * This returns the element from the log with the smallest time sequence
518 * value that's in the log (the oldest log item). Any element with a time
519 * sequence lower than min_seq will be ignored.
521 static struct tree_mod_elem *tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info,
522 u64 start, u64 min_seq)
524 return __tree_mod_log_search(fs_info, start, min_seq, true);
528 * This returns the element from the log with the largest time sequence
529 * value that's in the log (the most recent log item). Any element with
530 * a time sequence lower than min_seq will be ignored.
532 static struct tree_mod_elem *tree_mod_log_search(struct btrfs_fs_info *fs_info,
533 u64 start, u64 min_seq)
535 return __tree_mod_log_search(fs_info, start, min_seq, false);
538 int btrfs_tree_mod_log_eb_copy(struct extent_buffer *dst,
539 struct extent_buffer *src,
540 unsigned long dst_offset,
541 unsigned long src_offset,
544 struct btrfs_fs_info *fs_info = dst->fs_info;
546 struct tree_mod_elem **tm_list = NULL;
547 struct tree_mod_elem **tm_list_add = NULL;
548 struct tree_mod_elem **tm_list_rem = NULL;
551 struct tree_mod_elem *dst_move_tm = NULL;
552 struct tree_mod_elem *src_move_tm = NULL;
553 u32 dst_move_nr_items = btrfs_header_nritems(dst) - dst_offset;
554 u32 src_move_nr_items = btrfs_header_nritems(src) - (src_offset + nr_items);
556 if (!tree_mod_need_log(fs_info, NULL))
559 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
562 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
569 if (dst_move_nr_items) {
570 dst_move_tm = tree_mod_log_alloc_move(dst, dst_offset + nr_items,
571 dst_offset, dst_move_nr_items);
572 if (IS_ERR(dst_move_tm)) {
573 ret = PTR_ERR(dst_move_tm);
578 if (src_move_nr_items) {
579 src_move_tm = tree_mod_log_alloc_move(src, src_offset,
580 src_offset + nr_items,
582 if (IS_ERR(src_move_tm)) {
583 ret = PTR_ERR(src_move_tm);
589 tm_list_add = tm_list;
590 tm_list_rem = tm_list + nr_items;
591 for (i = 0; i < nr_items; i++) {
592 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
593 BTRFS_MOD_LOG_KEY_REMOVE);
594 if (!tm_list_rem[i]) {
599 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
600 BTRFS_MOD_LOG_KEY_ADD);
601 if (!tm_list_add[i]) {
608 if (tree_mod_dont_log(fs_info, NULL)) {
610 * Don't error if we failed to allocate memory because we don't
619 * We previously failed to allocate memory and we need to log, so we
626 ret = tree_mod_log_insert(fs_info, dst_move_tm);
630 for (i = 0; i < nr_items; i++) {
631 ret = tree_mod_log_insert(fs_info, tm_list_rem[i]);
634 ret = tree_mod_log_insert(fs_info, tm_list_add[i]);
639 ret = tree_mod_log_insert(fs_info, src_move_tm);
644 write_unlock(&fs_info->tree_mod_log_lock);
650 if (dst_move_tm && !RB_EMPTY_NODE(&dst_move_tm->node))
651 rb_erase(&dst_move_tm->node, &fs_info->tree_mod_log);
653 if (src_move_tm && !RB_EMPTY_NODE(&src_move_tm->node))
654 rb_erase(&src_move_tm->node, &fs_info->tree_mod_log);
657 for (i = 0; i < nr_items * 2; i++) {
658 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
659 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
664 write_unlock(&fs_info->tree_mod_log_lock);
670 int btrfs_tree_mod_log_free_eb(struct extent_buffer *eb)
672 struct tree_mod_elem **tm_list = NULL;
677 if (!tree_mod_need_log(eb->fs_info, eb))
680 nritems = btrfs_header_nritems(eb);
681 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
687 for (i = 0; i < nritems; i++) {
688 tm_list[i] = alloc_tree_mod_elem(eb, i,
689 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING);
697 if (tree_mod_dont_log(eb->fs_info, eb)) {
699 * Don't error if we failed to allocate memory because we don't
704 } else if (ret != 0) {
706 * We previously failed to allocate memory and we need to log,
707 * so we have to fail.
712 ret = tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
714 write_unlock(&eb->fs_info->tree_mod_log_lock);
723 for (i = 0; i < nritems; i++)
732 * Returns the logical address of the oldest predecessor of the given root.
733 * Entries older than time_seq are ignored.
735 static struct tree_mod_elem *tree_mod_log_oldest_root(struct extent_buffer *eb_root,
738 struct tree_mod_elem *tm;
739 struct tree_mod_elem *found = NULL;
740 u64 root_logical = eb_root->start;
747 * The very last operation that's logged for a root is the replacement
748 * operation (if it is replaced at all). This has the logical address
749 * of the *new* root, making it the very first operation that's logged
753 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
758 * If there are no tree operation for the oldest root, we simply
759 * return it. This should only happen if that (old) root is at
766 * If there's an operation that's not a root replacement, we
767 * found the oldest version of our root. Normally, we'll find a
768 * BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
770 if (tm->op != BTRFS_MOD_LOG_ROOT_REPLACE)
774 root_logical = tm->old_root.logical;
778 /* If there's no old root to return, return what we found instead */
787 * tm is a pointer to the first operation to rewind within eb. Then, all
788 * previous operations will be rewound (until we reach something older than
791 static void tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
792 struct extent_buffer *eb,
794 struct tree_mod_elem *first_tm)
797 struct rb_node *next;
798 struct tree_mod_elem *tm = first_tm;
801 unsigned long p_size = sizeof(struct btrfs_key_ptr);
803 * max_slot tracks the maximum valid slot of the rewind eb at every
804 * step of the rewind. This is in contrast with 'n' which eventually
805 * matches the number of items, but can be wrong during moves or if
806 * removes overlap on already valid slots (which is probably separately
807 * a bug). We do this to validate the offsets of memmoves for rewinding
808 * moves and detect invalid memmoves.
810 * Since a rewind eb can start empty, max_slot is a signed integer with
811 * a special meaning for -1, which is that no slot is valid to move out
812 * of. Any other negative value is invalid.
815 int move_src_end_slot;
816 int move_dst_end_slot;
818 n = btrfs_header_nritems(eb);
820 read_lock(&fs_info->tree_mod_log_lock);
821 while (tm && tm->seq >= time_seq) {
822 ASSERT(max_slot >= -1);
824 * All the operations are recorded with the operator used for
825 * the modification. As we're going backwards, we do the
826 * opposite of each operation here.
829 case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING:
830 BUG_ON(tm->slot < n);
832 case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING:
833 case BTRFS_MOD_LOG_KEY_REMOVE:
834 btrfs_set_node_key(eb, &tm->key, tm->slot);
835 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
836 btrfs_set_node_ptr_generation(eb, tm->slot,
839 if (tm->slot > max_slot)
842 case BTRFS_MOD_LOG_KEY_REPLACE:
843 BUG_ON(tm->slot >= n);
844 btrfs_set_node_key(eb, &tm->key, tm->slot);
845 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
846 btrfs_set_node_ptr_generation(eb, tm->slot,
849 case BTRFS_MOD_LOG_KEY_ADD:
851 * It is possible we could have already removed keys
852 * behind the known max slot, so this will be an
853 * overestimate. In practice, the copy operation
854 * inserts them in increasing order, and overestimating
855 * just means we miss some warnings, so it's OK. It
856 * isn't worth carefully tracking the full array of
857 * valid slots to check against when moving.
859 if (tm->slot == max_slot)
861 /* if a move operation is needed it's in the log */
864 case BTRFS_MOD_LOG_MOVE_KEYS:
865 ASSERT(tm->move.nr_items > 0);
866 move_src_end_slot = tm->move.dst_slot + tm->move.nr_items - 1;
867 move_dst_end_slot = tm->slot + tm->move.nr_items - 1;
868 o_dst = btrfs_node_key_ptr_offset(eb, tm->slot);
869 o_src = btrfs_node_key_ptr_offset(eb, tm->move.dst_slot);
870 if (WARN_ON(move_src_end_slot > max_slot ||
871 tm->move.nr_items <= 0)) {
873 "move from invalid tree mod log slot eb %llu slot %d dst_slot %d nr_items %d seq %llu n %u max_slot %d",
875 tm->move.dst_slot, tm->move.nr_items,
876 tm->seq, n, max_slot);
878 memmove_extent_buffer(eb, o_dst, o_src,
879 tm->move.nr_items * p_size);
880 max_slot = move_dst_end_slot;
882 case BTRFS_MOD_LOG_ROOT_REPLACE:
884 * This operation is special. For roots, this must be
885 * handled explicitly before rewinding.
886 * For non-roots, this operation may exist if the node
887 * was a root: root A -> child B; then A gets empty and
888 * B is promoted to the new root. In the mod log, we'll
889 * have a root-replace operation for B, a tree block
890 * that is no root. We simply ignore that operation.
894 next = rb_next(&tm->node);
897 tm = rb_entry(next, struct tree_mod_elem, node);
898 if (tm->logical != first_tm->logical)
901 read_unlock(&fs_info->tree_mod_log_lock);
902 btrfs_set_header_nritems(eb, n);
906 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
907 * is returned. If rewind operations happen, a fresh buffer is returned. The
908 * returned buffer is always read-locked. If the returned buffer is not the
909 * input buffer, the lock on the input buffer is released and the input buffer
910 * is freed (its refcount is decremented).
912 struct extent_buffer *btrfs_tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
913 struct btrfs_path *path,
914 struct extent_buffer *eb,
917 struct extent_buffer *eb_rewin;
918 struct tree_mod_elem *tm;
923 if (btrfs_header_level(eb) == 0)
926 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
930 if (tm->op == BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
931 BUG_ON(tm->slot != 0);
932 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
934 btrfs_tree_read_unlock(eb);
935 free_extent_buffer(eb);
938 btrfs_set_header_bytenr(eb_rewin, eb->start);
939 btrfs_set_header_backref_rev(eb_rewin,
940 btrfs_header_backref_rev(eb));
941 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
942 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
944 eb_rewin = btrfs_clone_extent_buffer(eb);
946 btrfs_tree_read_unlock(eb);
947 free_extent_buffer(eb);
952 btrfs_tree_read_unlock(eb);
953 free_extent_buffer(eb);
955 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
956 eb_rewin, btrfs_header_level(eb_rewin));
957 btrfs_tree_read_lock(eb_rewin);
958 tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
959 WARN_ON(btrfs_header_nritems(eb_rewin) >
960 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
966 * Rewind the state of @root's root node to the given @time_seq value.
967 * If there are no changes, the current root->root_node is returned. If anything
968 * changed in between, there's a fresh buffer allocated on which the rewind
969 * operations are done. In any case, the returned buffer is read locked.
970 * Returns NULL on error (with no locks held).
972 struct extent_buffer *btrfs_get_old_root(struct btrfs_root *root, u64 time_seq)
974 struct btrfs_fs_info *fs_info = root->fs_info;
975 struct tree_mod_elem *tm;
976 struct extent_buffer *eb = NULL;
977 struct extent_buffer *eb_root;
978 u64 eb_root_owner = 0;
979 struct extent_buffer *old;
980 struct tree_mod_root *old_root = NULL;
981 u64 old_generation = 0;
985 eb_root = btrfs_read_lock_root_node(root);
986 tm = tree_mod_log_oldest_root(eb_root, time_seq);
990 if (tm->op == BTRFS_MOD_LOG_ROOT_REPLACE) {
991 old_root = &tm->old_root;
992 old_generation = tm->generation;
993 logical = old_root->logical;
994 level = old_root->level;
996 logical = eb_root->start;
997 level = btrfs_header_level(eb_root);
1000 tm = tree_mod_log_search(fs_info, logical, time_seq);
1001 if (old_root && tm && tm->op != BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1002 struct btrfs_tree_parent_check check = { 0 };
1004 btrfs_tree_read_unlock(eb_root);
1005 free_extent_buffer(eb_root);
1007 check.level = level;
1008 check.owner_root = root->root_key.objectid;
1010 old = read_tree_block(fs_info, logical, &check);
1011 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1013 free_extent_buffer(old);
1015 "failed to read tree block %llu from get_old_root",
1018 struct tree_mod_elem *tm2;
1020 btrfs_tree_read_lock(old);
1021 eb = btrfs_clone_extent_buffer(old);
1023 * After the lookup for the most recent tree mod operation
1024 * above and before we locked and cloned the extent buffer
1025 * 'old', a new tree mod log operation may have been added.
1026 * So lookup for a more recent one to make sure the number
1027 * of mod log operations we replay is consistent with the
1028 * number of items we have in the cloned extent buffer,
1029 * otherwise we can hit a BUG_ON when rewinding the extent
1032 tm2 = tree_mod_log_search(fs_info, logical, time_seq);
1033 btrfs_tree_read_unlock(old);
1034 free_extent_buffer(old);
1036 ASSERT(tm2 == tm || tm2->seq > tm->seq);
1037 if (!tm2 || tm2->seq < tm->seq) {
1038 free_extent_buffer(eb);
1043 } else if (old_root) {
1044 eb_root_owner = btrfs_header_owner(eb_root);
1045 btrfs_tree_read_unlock(eb_root);
1046 free_extent_buffer(eb_root);
1047 eb = alloc_dummy_extent_buffer(fs_info, logical);
1049 eb = btrfs_clone_extent_buffer(eb_root);
1050 btrfs_tree_read_unlock(eb_root);
1051 free_extent_buffer(eb_root);
1057 btrfs_set_header_bytenr(eb, eb->start);
1058 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1059 btrfs_set_header_owner(eb, eb_root_owner);
1060 btrfs_set_header_level(eb, old_root->level);
1061 btrfs_set_header_generation(eb, old_generation);
1063 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
1064 btrfs_header_level(eb));
1065 btrfs_tree_read_lock(eb);
1067 tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1069 WARN_ON(btrfs_header_level(eb) != 0);
1070 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1075 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1077 struct tree_mod_elem *tm;
1079 struct extent_buffer *eb_root = btrfs_root_node(root);
1081 tm = tree_mod_log_oldest_root(eb_root, time_seq);
1082 if (tm && tm->op == BTRFS_MOD_LOG_ROOT_REPLACE)
1083 level = tm->old_root.level;
1085 level = btrfs_header_level(eb_root);
1087 free_extent_buffer(eb_root);
1093 * Return the lowest sequence number in the tree modification log.
1095 * Return the sequence number of the oldest tree modification log user, which
1096 * corresponds to the lowest sequence number of all existing users. If there are
1097 * no users it returns 0.
1099 u64 btrfs_tree_mod_log_lowest_seq(struct btrfs_fs_info *fs_info)
1103 read_lock(&fs_info->tree_mod_log_lock);
1104 if (!list_empty(&fs_info->tree_mod_seq_list)) {
1105 struct btrfs_seq_list *elem;
1107 elem = list_first_entry(&fs_info->tree_mod_seq_list,
1108 struct btrfs_seq_list, list);
1111 read_unlock(&fs_info->tree_mod_log_lock);