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 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 bool tree_mod_dont_log(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
175 if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
177 if (eb && btrfs_header_level(eb) == 0)
180 write_lock(&fs_info->tree_mod_log_lock);
181 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
182 write_unlock(&fs_info->tree_mod_log_lock);
189 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
190 static bool tree_mod_need_log(const struct btrfs_fs_info *fs_info,
191 struct extent_buffer *eb)
193 if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
195 if (eb && btrfs_header_level(eb) == 0)
201 static struct tree_mod_elem *alloc_tree_mod_elem(struct extent_buffer *eb,
203 enum btrfs_mod_log_op op)
205 struct tree_mod_elem *tm;
207 tm = kzalloc(sizeof(*tm), GFP_NOFS);
211 tm->logical = eb->start;
212 if (op != BTRFS_MOD_LOG_KEY_ADD) {
213 btrfs_node_key(eb, &tm->key, slot);
214 tm->blockptr = btrfs_node_blockptr(eb, slot);
218 tm->generation = btrfs_node_ptr_generation(eb, slot);
219 RB_CLEAR_NODE(&tm->node);
224 int btrfs_tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
225 enum btrfs_mod_log_op op)
227 struct tree_mod_elem *tm;
230 if (!tree_mod_need_log(eb->fs_info, eb))
233 tm = alloc_tree_mod_elem(eb, slot, op);
237 if (tree_mod_dont_log(eb->fs_info, eb)) {
240 * Don't error if we failed to allocate memory because we don't
244 } else if (ret != 0) {
246 * We previously failed to allocate memory and we need to log,
247 * so we have to fail.
252 ret = tree_mod_log_insert(eb->fs_info, tm);
254 write_unlock(&eb->fs_info->tree_mod_log_lock);
261 static struct tree_mod_elem *tree_mod_log_alloc_move(struct extent_buffer *eb,
262 int dst_slot, int src_slot,
265 struct tree_mod_elem *tm;
267 tm = kzalloc(sizeof(*tm), GFP_NOFS);
269 return ERR_PTR(-ENOMEM);
271 tm->logical = eb->start;
273 tm->move.dst_slot = dst_slot;
274 tm->move.nr_items = nr_items;
275 tm->op = BTRFS_MOD_LOG_MOVE_KEYS;
276 RB_CLEAR_NODE(&tm->node);
281 int btrfs_tree_mod_log_insert_move(struct extent_buffer *eb,
282 int dst_slot, int src_slot,
285 struct tree_mod_elem *tm = NULL;
286 struct tree_mod_elem **tm_list = NULL;
291 if (!tree_mod_need_log(eb->fs_info, eb))
294 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
300 tm = tree_mod_log_alloc_move(eb, dst_slot, src_slot, nr_items);
307 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
308 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
309 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING);
317 if (tree_mod_dont_log(eb->fs_info, eb)) {
319 * Don't error if we failed to allocate memory because we don't
328 * We previously failed to allocate memory and we need to log, so we
335 * When we override something during the move, we log these removals.
336 * This can only happen when we move towards the beginning of the
337 * buffer, i.e. dst_slot < src_slot.
339 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
340 ret = tree_mod_log_insert(eb->fs_info, tm_list[i]);
345 ret = tree_mod_log_insert(eb->fs_info, tm);
348 write_unlock(&eb->fs_info->tree_mod_log_lock);
355 for (i = 0; i < nr_items; i++) {
356 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
357 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
362 write_unlock(&eb->fs_info->tree_mod_log_lock);
369 static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
370 struct tree_mod_elem **tm_list,
376 for (i = nritems - 1; i >= 0; i--) {
377 ret = tree_mod_log_insert(fs_info, tm_list[i]);
379 for (j = nritems - 1; j > i; j--)
380 rb_erase(&tm_list[j]->node,
381 &fs_info->tree_mod_log);
389 int btrfs_tree_mod_log_insert_root(struct extent_buffer *old_root,
390 struct extent_buffer *new_root,
393 struct btrfs_fs_info *fs_info = old_root->fs_info;
394 struct tree_mod_elem *tm = NULL;
395 struct tree_mod_elem **tm_list = NULL;
400 if (!tree_mod_need_log(fs_info, NULL))
403 if (log_removal && btrfs_header_level(old_root) > 0) {
404 nritems = btrfs_header_nritems(old_root);
405 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
411 for (i = 0; i < nritems; i++) {
412 tm_list[i] = alloc_tree_mod_elem(old_root, i,
413 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING);
421 tm = kzalloc(sizeof(*tm), GFP_NOFS);
427 tm->logical = new_root->start;
428 tm->old_root.logical = old_root->start;
429 tm->old_root.level = btrfs_header_level(old_root);
430 tm->generation = btrfs_header_generation(old_root);
431 tm->op = BTRFS_MOD_LOG_ROOT_REPLACE;
434 if (tree_mod_dont_log(fs_info, NULL)) {
436 * Don't error if we failed to allocate memory because we don't
441 } else if (ret != 0) {
443 * We previously failed to allocate memory and we need to log,
444 * so we have to fail.
450 ret = tree_mod_log_free_eb(fs_info, tm_list, nritems);
452 ret = tree_mod_log_insert(fs_info, tm);
455 write_unlock(&fs_info->tree_mod_log_lock);
464 for (i = 0; i < nritems; i++)
473 static struct tree_mod_elem *__tree_mod_log_search(struct btrfs_fs_info *fs_info,
474 u64 start, u64 min_seq,
477 struct rb_root *tm_root;
478 struct rb_node *node;
479 struct tree_mod_elem *cur = NULL;
480 struct tree_mod_elem *found = NULL;
482 read_lock(&fs_info->tree_mod_log_lock);
483 tm_root = &fs_info->tree_mod_log;
484 node = tm_root->rb_node;
486 cur = rb_entry(node, struct tree_mod_elem, node);
487 if (cur->logical < start) {
488 node = node->rb_left;
489 } else if (cur->logical > start) {
490 node = node->rb_right;
491 } else if (cur->seq < min_seq) {
492 node = node->rb_left;
493 } else if (!smallest) {
494 /* We want the node with the highest seq */
496 BUG_ON(found->seq > cur->seq);
498 node = node->rb_left;
499 } else if (cur->seq > min_seq) {
500 /* We want the node with the smallest seq */
502 BUG_ON(found->seq < cur->seq);
504 node = node->rb_right;
510 read_unlock(&fs_info->tree_mod_log_lock);
516 * This returns the element from the log with the smallest time sequence
517 * value that's in the log (the oldest log item). Any element with a time
518 * sequence lower than min_seq will be ignored.
520 static struct tree_mod_elem *tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info,
521 u64 start, u64 min_seq)
523 return __tree_mod_log_search(fs_info, start, min_seq, true);
527 * This returns the element from the log with the largest time sequence
528 * value that's in the log (the most recent log item). Any element with
529 * a time sequence lower than min_seq will be ignored.
531 static struct tree_mod_elem *tree_mod_log_search(struct btrfs_fs_info *fs_info,
532 u64 start, u64 min_seq)
534 return __tree_mod_log_search(fs_info, start, min_seq, false);
537 int btrfs_tree_mod_log_eb_copy(struct extent_buffer *dst,
538 struct extent_buffer *src,
539 unsigned long dst_offset,
540 unsigned long src_offset,
543 struct btrfs_fs_info *fs_info = dst->fs_info;
545 struct tree_mod_elem **tm_list = NULL;
546 struct tree_mod_elem **tm_list_add = NULL;
547 struct tree_mod_elem **tm_list_rem = NULL;
550 struct tree_mod_elem *dst_move_tm = NULL;
551 struct tree_mod_elem *src_move_tm = NULL;
552 u32 dst_move_nr_items = btrfs_header_nritems(dst) - dst_offset;
553 u32 src_move_nr_items = btrfs_header_nritems(src) - (src_offset + nr_items);
555 if (!tree_mod_need_log(fs_info, NULL))
558 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
561 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
568 if (dst_move_nr_items) {
569 dst_move_tm = tree_mod_log_alloc_move(dst, dst_offset + nr_items,
570 dst_offset, dst_move_nr_items);
571 if (IS_ERR(dst_move_tm)) {
572 ret = PTR_ERR(dst_move_tm);
577 if (src_move_nr_items) {
578 src_move_tm = tree_mod_log_alloc_move(src, src_offset,
579 src_offset + nr_items,
581 if (IS_ERR(src_move_tm)) {
582 ret = PTR_ERR(src_move_tm);
588 tm_list_add = tm_list;
589 tm_list_rem = tm_list + nr_items;
590 for (i = 0; i < nr_items; i++) {
591 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
592 BTRFS_MOD_LOG_KEY_REMOVE);
593 if (!tm_list_rem[i]) {
598 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
599 BTRFS_MOD_LOG_KEY_ADD);
600 if (!tm_list_add[i]) {
607 if (tree_mod_dont_log(fs_info, NULL)) {
609 * Don't error if we failed to allocate memory because we don't
618 * We previously failed to allocate memory and we need to log, so we
625 ret = tree_mod_log_insert(fs_info, dst_move_tm);
629 for (i = 0; i < nr_items; i++) {
630 ret = tree_mod_log_insert(fs_info, tm_list_rem[i]);
633 ret = tree_mod_log_insert(fs_info, tm_list_add[i]);
638 ret = tree_mod_log_insert(fs_info, src_move_tm);
643 write_unlock(&fs_info->tree_mod_log_lock);
649 if (dst_move_tm && !RB_EMPTY_NODE(&dst_move_tm->node))
650 rb_erase(&dst_move_tm->node, &fs_info->tree_mod_log);
652 if (src_move_tm && !RB_EMPTY_NODE(&src_move_tm->node))
653 rb_erase(&src_move_tm->node, &fs_info->tree_mod_log);
656 for (i = 0; i < nr_items * 2; i++) {
657 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
658 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
663 write_unlock(&fs_info->tree_mod_log_lock);
669 int btrfs_tree_mod_log_free_eb(struct extent_buffer *eb)
671 struct tree_mod_elem **tm_list = NULL;
676 if (!tree_mod_need_log(eb->fs_info, eb))
679 nritems = btrfs_header_nritems(eb);
680 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
686 for (i = 0; i < nritems; i++) {
687 tm_list[i] = alloc_tree_mod_elem(eb, i,
688 BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING);
696 if (tree_mod_dont_log(eb->fs_info, eb)) {
698 * Don't error if we failed to allocate memory because we don't
703 } else if (ret != 0) {
705 * We previously failed to allocate memory and we need to log,
706 * so we have to fail.
711 ret = tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
713 write_unlock(&eb->fs_info->tree_mod_log_lock);
722 for (i = 0; i < nritems; i++)
731 * Returns the logical address of the oldest predecessor of the given root.
732 * Entries older than time_seq are ignored.
734 static struct tree_mod_elem *tree_mod_log_oldest_root(struct extent_buffer *eb_root,
737 struct tree_mod_elem *tm;
738 struct tree_mod_elem *found = NULL;
739 u64 root_logical = eb_root->start;
746 * The very last operation that's logged for a root is the replacement
747 * operation (if it is replaced at all). This has the logical address
748 * of the *new* root, making it the very first operation that's logged
752 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
757 * If there are no tree operation for the oldest root, we simply
758 * return it. This should only happen if that (old) root is at
765 * If there's an operation that's not a root replacement, we
766 * found the oldest version of our root. Normally, we'll find a
767 * BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
769 if (tm->op != BTRFS_MOD_LOG_ROOT_REPLACE)
773 root_logical = tm->old_root.logical;
777 /* If there's no old root to return, return what we found instead */
786 * tm is a pointer to the first operation to rewind within eb. Then, all
787 * previous operations will be rewound (until we reach something older than
790 static void tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
791 struct extent_buffer *eb,
793 struct tree_mod_elem *first_tm)
796 struct rb_node *next;
797 struct tree_mod_elem *tm = first_tm;
800 unsigned long p_size = sizeof(struct btrfs_key_ptr);
802 * max_slot tracks the maximum valid slot of the rewind eb at every
803 * step of the rewind. This is in contrast with 'n' which eventually
804 * matches the number of items, but can be wrong during moves or if
805 * removes overlap on already valid slots (which is probably separately
806 * a bug). We do this to validate the offsets of memmoves for rewinding
807 * moves and detect invalid memmoves.
809 * Since a rewind eb can start empty, max_slot is a signed integer with
810 * a special meaning for -1, which is that no slot is valid to move out
811 * of. Any other negative value is invalid.
814 int move_src_end_slot;
815 int move_dst_end_slot;
817 n = btrfs_header_nritems(eb);
819 read_lock(&fs_info->tree_mod_log_lock);
820 while (tm && tm->seq >= time_seq) {
821 ASSERT(max_slot >= -1);
823 * All the operations are recorded with the operator used for
824 * the modification. As we're going backwards, we do the
825 * opposite of each operation here.
828 case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING:
829 BUG_ON(tm->slot < n);
831 case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING:
832 case BTRFS_MOD_LOG_KEY_REMOVE:
833 btrfs_set_node_key(eb, &tm->key, tm->slot);
834 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
835 btrfs_set_node_ptr_generation(eb, tm->slot,
838 if (tm->slot > max_slot)
841 case BTRFS_MOD_LOG_KEY_REPLACE:
842 BUG_ON(tm->slot >= n);
843 btrfs_set_node_key(eb, &tm->key, tm->slot);
844 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
845 btrfs_set_node_ptr_generation(eb, tm->slot,
848 case BTRFS_MOD_LOG_KEY_ADD:
850 * It is possible we could have already removed keys
851 * behind the known max slot, so this will be an
852 * overestimate. In practice, the copy operation
853 * inserts them in increasing order, and overestimating
854 * just means we miss some warnings, so it's OK. It
855 * isn't worth carefully tracking the full array of
856 * valid slots to check against when moving.
858 if (tm->slot == max_slot)
860 /* if a move operation is needed it's in the log */
863 case BTRFS_MOD_LOG_MOVE_KEYS:
864 ASSERT(tm->move.nr_items > 0);
865 move_src_end_slot = tm->move.dst_slot + tm->move.nr_items - 1;
866 move_dst_end_slot = tm->slot + tm->move.nr_items - 1;
867 o_dst = btrfs_node_key_ptr_offset(eb, tm->slot);
868 o_src = btrfs_node_key_ptr_offset(eb, tm->move.dst_slot);
869 if (WARN_ON(move_src_end_slot > max_slot ||
870 tm->move.nr_items <= 0)) {
872 "move from invalid tree mod log slot eb %llu slot %d dst_slot %d nr_items %d seq %llu n %u max_slot %d",
874 tm->move.dst_slot, tm->move.nr_items,
875 tm->seq, n, max_slot);
877 memmove_extent_buffer(eb, o_dst, o_src,
878 tm->move.nr_items * p_size);
879 max_slot = move_dst_end_slot;
881 case BTRFS_MOD_LOG_ROOT_REPLACE:
883 * This operation is special. For roots, this must be
884 * handled explicitly before rewinding.
885 * For non-roots, this operation may exist if the node
886 * was a root: root A -> child B; then A gets empty and
887 * B is promoted to the new root. In the mod log, we'll
888 * have a root-replace operation for B, a tree block
889 * that is no root. We simply ignore that operation.
893 next = rb_next(&tm->node);
896 tm = rb_entry(next, struct tree_mod_elem, node);
897 if (tm->logical != first_tm->logical)
900 read_unlock(&fs_info->tree_mod_log_lock);
901 btrfs_set_header_nritems(eb, n);
905 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
906 * is returned. If rewind operations happen, a fresh buffer is returned. The
907 * returned buffer is always read-locked. If the returned buffer is not the
908 * input buffer, the lock on the input buffer is released and the input buffer
909 * is freed (its refcount is decremented).
911 struct extent_buffer *btrfs_tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
912 struct btrfs_path *path,
913 struct extent_buffer *eb,
916 struct extent_buffer *eb_rewin;
917 struct tree_mod_elem *tm;
922 if (btrfs_header_level(eb) == 0)
925 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
929 if (tm->op == BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
930 BUG_ON(tm->slot != 0);
931 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
933 btrfs_tree_read_unlock(eb);
934 free_extent_buffer(eb);
937 btrfs_set_header_bytenr(eb_rewin, eb->start);
938 btrfs_set_header_backref_rev(eb_rewin,
939 btrfs_header_backref_rev(eb));
940 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
941 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
943 eb_rewin = btrfs_clone_extent_buffer(eb);
945 btrfs_tree_read_unlock(eb);
946 free_extent_buffer(eb);
951 btrfs_tree_read_unlock(eb);
952 free_extent_buffer(eb);
954 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
955 eb_rewin, btrfs_header_level(eb_rewin));
956 btrfs_tree_read_lock(eb_rewin);
957 tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
958 WARN_ON(btrfs_header_nritems(eb_rewin) >
959 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
965 * Rewind the state of @root's root node to the given @time_seq value.
966 * If there are no changes, the current root->root_node is returned. If anything
967 * changed in between, there's a fresh buffer allocated on which the rewind
968 * operations are done. In any case, the returned buffer is read locked.
969 * Returns NULL on error (with no locks held).
971 struct extent_buffer *btrfs_get_old_root(struct btrfs_root *root, u64 time_seq)
973 struct btrfs_fs_info *fs_info = root->fs_info;
974 struct tree_mod_elem *tm;
975 struct extent_buffer *eb = NULL;
976 struct extent_buffer *eb_root;
977 u64 eb_root_owner = 0;
978 struct extent_buffer *old;
979 struct tree_mod_root *old_root = NULL;
980 u64 old_generation = 0;
984 eb_root = btrfs_read_lock_root_node(root);
985 tm = tree_mod_log_oldest_root(eb_root, time_seq);
989 if (tm->op == BTRFS_MOD_LOG_ROOT_REPLACE) {
990 old_root = &tm->old_root;
991 old_generation = tm->generation;
992 logical = old_root->logical;
993 level = old_root->level;
995 logical = eb_root->start;
996 level = btrfs_header_level(eb_root);
999 tm = tree_mod_log_search(fs_info, logical, time_seq);
1000 if (old_root && tm && tm->op != BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1001 struct btrfs_tree_parent_check check = { 0 };
1003 btrfs_tree_read_unlock(eb_root);
1004 free_extent_buffer(eb_root);
1006 check.level = level;
1007 check.owner_root = root->root_key.objectid;
1009 old = read_tree_block(fs_info, logical, &check);
1010 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1012 free_extent_buffer(old);
1014 "failed to read tree block %llu from get_old_root",
1017 struct tree_mod_elem *tm2;
1019 btrfs_tree_read_lock(old);
1020 eb = btrfs_clone_extent_buffer(old);
1022 * After the lookup for the most recent tree mod operation
1023 * above and before we locked and cloned the extent buffer
1024 * 'old', a new tree mod log operation may have been added.
1025 * So lookup for a more recent one to make sure the number
1026 * of mod log operations we replay is consistent with the
1027 * number of items we have in the cloned extent buffer,
1028 * otherwise we can hit a BUG_ON when rewinding the extent
1031 tm2 = tree_mod_log_search(fs_info, logical, time_seq);
1032 btrfs_tree_read_unlock(old);
1033 free_extent_buffer(old);
1035 ASSERT(tm2 == tm || tm2->seq > tm->seq);
1036 if (!tm2 || tm2->seq < tm->seq) {
1037 free_extent_buffer(eb);
1042 } else if (old_root) {
1043 eb_root_owner = btrfs_header_owner(eb_root);
1044 btrfs_tree_read_unlock(eb_root);
1045 free_extent_buffer(eb_root);
1046 eb = alloc_dummy_extent_buffer(fs_info, logical);
1048 eb = btrfs_clone_extent_buffer(eb_root);
1049 btrfs_tree_read_unlock(eb_root);
1050 free_extent_buffer(eb_root);
1056 btrfs_set_header_bytenr(eb, eb->start);
1057 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1058 btrfs_set_header_owner(eb, eb_root_owner);
1059 btrfs_set_header_level(eb, old_root->level);
1060 btrfs_set_header_generation(eb, old_generation);
1062 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
1063 btrfs_header_level(eb));
1064 btrfs_tree_read_lock(eb);
1066 tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1068 WARN_ON(btrfs_header_level(eb) != 0);
1069 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1074 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1076 struct tree_mod_elem *tm;
1078 struct extent_buffer *eb_root = btrfs_root_node(root);
1080 tm = tree_mod_log_oldest_root(eb_root, time_seq);
1081 if (tm && tm->op == BTRFS_MOD_LOG_ROOT_REPLACE)
1082 level = tm->old_root.level;
1084 level = btrfs_header_level(eb_root);
1086 free_extent_buffer(eb_root);
1092 * Return the lowest sequence number in the tree modification log.
1094 * Return the sequence number of the oldest tree modification log user, which
1095 * corresponds to the lowest sequence number of all existing users. If there are
1096 * no users it returns 0.
1098 u64 btrfs_tree_mod_log_lowest_seq(struct btrfs_fs_info *fs_info)
1102 read_lock(&fs_info->tree_mod_log_lock);
1103 if (!list_empty(&fs_info->tree_mod_seq_list)) {
1104 struct btrfs_seq_list *elem;
1106 elem = list_first_entry(&fs_info->tree_mod_seq_list,
1107 struct btrfs_seq_list, list);
1110 read_unlock(&fs_info->tree_mod_log_lock);